Merge "fix: Make onm-bootstrap install dependencies"

This commit is contained in:
Zuul 2024-04-29 07:40:50 +00:00 committed by Gerrit Code Review
commit c4375dbd20
6 changed files with 51 additions and 477 deletions

View File

@ -1,50 +0,0 @@
#!/bin/bash
# Set up the script variables
STARTTIME=$(date +%s)
PID=$(echo $$)
LOGFILE="/var/log/k8s-master-init.$PID.log"
# Set up the logging for the script
sudo touch $LOGFILE
sudo chown $USER:$USER $LOGFILE
# All the output of this shell script is redirected to the LOGFILE
exec 3>&1 4>&2
trap 'exec 2>&4 1>&3' 0 1 2 3
exec 1>$LOGFILE 2>&1
# A function to print a message to the stdout as well as as the LOGFILE
log_print(){
level=$1
Message=$2
echo "$level [$(date)]: $Message"
echo "$level [$(date)]: $Message" >&3
}
WIREGUARD_VPN_IP=`ip a | grep wg | grep inet | awk '{print $2}' | cut -d'/' -f1`
log_print INFO "k8s-master-init.sh ($PID): Initializing Kubernetes using kubeadm..."
# Init kubernetes
sudo kubeadm init --apiserver-advertise-address ${WIREGUARD_VPN_IP} --service-cidr 10.96.0.0/16 --pod-network-cidr 10.244.0.0/16
# Set kubeconfig file
mkdir -p $HOME/.kube
sudo cp /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
log_print INFO "k8s-master-init.sh ($PID): Installing Cilium"
# Install Cilium with Wireguard parameters
helm install cilium cilium/cilium --namespace kube-system --set encryption.enabled=true --set encryption.type=wireguard
log_print INFO "k8s-master-init.sh ($PID): Installing Kubevela"
curl -fsSL -o $HOME/k8s-deps/kubevela_install.sh https://kubevela.io/script/install.sh && chmod 700 $HOME/k8s-deps/kubevela_install.sh && $HOME/k8s-deps/kubevela_install.sh
log_print INFO "k8s-master-init.sh ($PID): Save K8s join command to $HOME/k8s-deps"
# Save join command to specific path for the worker nodes to SCP
kubeadm token create --print-join-command > $HOME/k8s-deps/k8s-join-command.sh
# Declare configuration done successfully
ENDTIME=$(date +%s)
ELAPSED=$(( ENDTIME - STARTTIME ))
log_print INFO "k8s-master-init.sh ($PID): k8s-master-init.sh: Configuration done successfully in $ELAPSED seconds "

View File

@ -1,136 +0,0 @@
#!/bin/bash
# Set up the script variables
STARTTIME=$(date +%s)
PID=$(echo $$)
EXITCODE=$PID
LOGFILE="/var/log/k8s-preinstall.$PID.log"
# Set up the logging for the script
sudo touch $LOGFILE
sudo chown $USER:$USER $LOGFILE
# All the output of this shell script is redirected to the LOGFILE
exec 3>&1 4>&2
trap 'exec 2>&4 1>&3' 0 1 2 3
exec 1>$LOGFILE 2>&1
# A function to print a message to the stdout as well as as the LOGFILE
log_print(){
level=$1
Message=$2
echo "$level [$(date)]: $Message"
echo "$level [$(date)]: $Message" >&3
}
# A function to check for the apt lock
Check_lock() {
i=0
log_print INFO "Checking for apt lock"
while [ `ps aux | grep [l]ock_is_held | wc -l` != 0 ]; do
echo "Lock_is_held $i"
ps aux | grep [l]ock_is_held
sleep 10
((i=i+10));
done
log_print INFO "Exited the while loop, time spent: $i"
echo "ps aux | grep apt"
ps aux | grep apt
log_print INFO "Waiting for lock task ended properly."
}
# Find Architecture
ARCH_COMMAND=$(sudo arch)
AMD_ARCH="x86_64"
ARM_ARCH="aarch64"
if [ "$ARCH_COMMAND" = "$AMD_ARCH" ]; then
ARCHITECTURE="amd64"
elif [ "$ARCH_COMMAND" = "$ARM_ARCH" ]; then
ARCHITECTURE="arm64"
fi
# Check for lock
Check_lock
# Update the package list
log_print INFO "Updating the package list."
sudo apt-get update
# Start the Configuration
log_print INFO "Configuration started!"
log_print INFO "Logs are saved at: $LOGFILE"
# Check for lock
Check_lock
# Install curl
log_print INFO "Installing curl"
sudo apt-get install -y curl || { log_print ERROR "curl installation failed!"; exit $EXITCODE; }
# Adding Kubernetes Repo
log_print INFO "Adding Kubernetes Repo"
echo "deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.26/deb/ /" | sudo tee /etc/apt/sources.list.d/kubernetes.list
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.26/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg || { log_print ERROR "Kubernetes repo can't be added!"; exit $EXITCODE; }
sudo apt-get update
# Check for lock
Check_lock
# Install Kubernetes
log_print INFO "Installing Kubernetes"
sudo apt-get install -y kubeadm=1.26.15-1.1 --allow-downgrades || { log_print ERROR "kubeadm installation failed!"; exit $EXITCODE; }
sudo apt-get install -y kubelet=1.26.15-1.1 --allow-downgrades || { log_print ERROR "kubectl installation failed!"; exit $EXITCODE; }
sudo apt-get install -y kubectl=1.26.15-1.1 --allow-downgrades || { log_print ERROR "kubelet installation failed!"; exit $EXITCODE; }
# Install Containerd
wget https://github.com/containerd/containerd/releases/download/v1.7.2/containerd-1.7.2-linux-$ARCHITECTURE.tar.gz
tar xvf containerd-1.7.2-linux-$ARCHITECTURE.tar.gz
sudo tar Cxzvf /usr/local containerd-1.7.2-linux-$ARCHITECTURE.tar.gz
wget https://github.com/opencontainers/runc/releases/download/v1.1.3/runc.$ARCHITECTURE
sudo install -m 755 runc.$ARCHITECTURE /usr/local/sbin/runc
wget https://github.com/containernetworking/plugins/releases/download/v1.1.1/cni-plugins-linux-$ARCHITECTURE-v1.1.1.tgz
sudo mkdir -p /opt/cni/bin
sudo tar Cxzvf /opt/cni/bin cni-plugins-linux-$ARCHITECTURE-v1.1.1.tgz
sudo mkdir /etc/containerd
containerd config default | sudo tee /etc/containerd/config.toml
sudo sed -i 's/SystemdCgroup = false/SystemdCgroup = true/' /etc/containerd/config.toml
sudo curl -L https://raw.githubusercontent.com/containerd/containerd/main/containerd.service -o /etc/systemd/system/containerd.service
sudo systemctl daemon-reload
sudo systemctl enable --now containerd
sudo systemctl restart containerd
sudo systemctl status containerd
# Holding upgrades for Kubernetes software (versions to updated manually)
sudo apt-mark hold kubeadm kubelet kubectl containerd
WIREGUARD_VPN_IP=`ip a | grep wg | grep inet | awk '{print $2}' | cut -d'/' -f1`
echo "KUBELET_EXTRA_ARGS=--node-ip=${WIREGUARD_VPN_IP} --container-runtime-endpoint=unix:///run/containerd/containerd.sock" | sudo tee -a /etc/default/kubelet
sudo systemctl restart kubelet
log_print INFO "Checking Kubernetes versions"
kubeadm version || { log_print ERROR "kubeadm installation failed!"; exit $EXITCODE; }
kubelet --version || { log_print ERROR "kubelet installation failed!"; exit $EXITCODE; }
kubectl version
if [ $? -gt 1 ]; then
log_print ERROR "kubectl installation failed!"; exit $EXITCODE;
fi
# Turn off the swap memory
if [ `grep Swap /proc/meminfo | grep SwapTotal: | cut -d" " -f14` == "0" ]; then
log_print INFO "The swap memory is Off"
else
sudo swapoff a || { log_print ERROR "swap memory can't be turned off "; exit $EXITCODE; }
fi
log_print INFO "Installing Helm..."
curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 && chmod 700 get_helm.sh && ./get_helm.sh
# Add Cilium Helm Repo
helm repo add cilium https://helm.cilium.io/
helm repo update
# Declare configuration done successfully
ENDTIME=$(date +%s)
ELAPSED=$(( ENDTIME - STARTTIME ))
log_print INFO "k8s-preinstall.sh: Configuration done successfully in $ELAPSED seconds "

View File

@ -1,40 +0,0 @@
#!/bin/sh
# Set up the script variables
STARTTIME=$(date +%s)
PID=$(echo $$)
LOGFILE="/var/log/k8s-worker-init.$PID.log"
# Set up the logging for the script
sudo touch $LOGFILE
sudo chown $USER:$USER $LOGFILE
# All the output of this shell script is redirected to the LOGFILE
exec 3>&1 4>&2
trap 'exec 2>&4 1>&3' 0 1 2 3
exec 1>$LOGFILE 2>&1
# A function to print a message to the stdout as well as as the LOGFILE
log_print(){
level=$1
Message=$2
echo "$level [$(date)]: $Message"
echo "$level [$(date)]: $Message" >&3
}
MASTER_IP=$1
MASTER_USERNAME=$2
log_print INFO "k8s-worker-init.sh ($PID): SCP to Master Node to get the k8s-join-command"
# Join Kubernetes Cluster
sudo scp -o StrictHostKeyChecking=no -i $HOME/wg-private-key.key $MASTER_USERNAME@$MASTER_IP:/home/$MASTER_USERNAME/k8s-deps/k8s-join-command.sh /home/$USER/k8s-deps/k8s-join-command.sh
sudo chmod +x $HOME/k8s-deps/k8s-join-command.sh
log_print "k8s-worker-init.sh ($PID): Executing k8s-join-command.sh to join the cluster"
sudo $HOME/k8s-deps/k8s-join-command.sh
# Declare configuration done successfully
ENDTIME=$(date +%s)
ELAPSED=$(( ENDTIME - STARTTIME ))
log_print INFO "k8s-worker-init.sh ($PID): k8s-worker-init.sh: Configuration done successfully in $ELAPSED seconds "

View File

@ -25,6 +25,36 @@ log_print(){
echo "$level [$(date)]: $Message" >&3
}
# A function to check for the apt lock
Check_lock() {
i=0
log_print INFO "onm-bootstrap($PID): Checking for apt lock"
while [ `ps aux | grep [l]ock_is_held | wc -l` != 0 ]; do
log_print INFO "onm-bootstrap($PID): Lock_is_held $i"
ps aux | grep [l]ock_is_held
sleep 10
((i=i+10));
done
log_print INFO "onm-bootstrap($PID): Exited the while loop, time spent: $i"
log_print INFO "onm-bootstrap($PID): ps aux | grep apt"
ps aux | grep apt
log_print INFO "onm-bootstrap($PID): Waiting for lock task ended properly."
}
# Function to check for the wg command
check_wg_installed() {
log_print "onm-bootstrap($PID): Checking if WireGuard (wg) is installed..."
# Using command -v to check for the wg command
if command -v wg >/dev/null 2>&1; then
log_print "onm-bootstrap($PID): WireGuard (wg) is installed."
log_print "onm-bootstrap($PID): Location: $(which wg)"
else
log_print "onm-bootstrap($PID): WireGuard (wg) is not installed."
fi
}
# "CREATE" or "DELETE" Overlay Node
ACTION=$1
# Define Application Node Type ("MASTER","WORKER")
@ -42,10 +72,24 @@ application_uuid=$APPLICATION_UUID
# Get the currently logged in user (assuming single user login)
logged_in_user=$(whoami)
log_print INFO "Starting onm-bootstrap with the following parameters: ACTION=$ACTION, NODE_TYPE=$NODE_TYPE,
# Start the Configuration
log_print INFO "onm-bootstrap ($PID): Configuration started!"
log_print INFO "onm-bootstrap ($PID): Logs are saved at: $LOGFILE"
log_print INFO "onm-bootstrap($PID): Starting onm-bootstrap with the following parameters: ACTION=$ACTION, NODE_TYPE=$NODE_TYPE,
APPLICATION_UUID=$APPLICATION_UUID, ONM_IP=$ONM_IP, PUBLIC_IP=$public_ip,
LOGGED_IN_USER=$logged_in_user, SSH_PORT=$SSH_PORT"
log_print INFO "onm-bootstrap($PID): Installing wireguard and resolvconf..."
Check_lock
# Install WireGuard package
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y wireguard
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y resolvconf
# Check if Wireguard is installed
check_wg_installed
# Get the isMaster variable from the environment variable
if [ "$NODE_TYPE" == "MASTER" ]; then
IS_MASTER="true";
@ -55,15 +99,15 @@ fi
# Check if string1 is equal to string2
if [ "$ACTION" == "CREATE" ]; then
log_print INFO "Creating Wireguard folder to home directory..."
log_print INFO "onm-bootstrap($PID): Creating Wireguard folder to home directory..."
# Create Wireguard Folder to accept the wireguard scripts
mkdir -p /home/${logged_in_user}/wireguard
log_print INFO "Creating OpenSSH Public/Private Key Pair..."
log_print INFO "onm-bootstrap($PID): Creating OpenSSH Public/Private Key Pair..."
# Create OpenSSH Public/Private Key files
ssh-keygen -C wireguard-pub -t rsa -b 4096 -f /home/${logged_in_user}/wireguard/wireguard -N ""
log_print INFO "Moving wireguard.pub file to authorized_keys file..."
log_print INFO "onm-bootstrap($PID): Moving wireguard.pub file to authorized_keys file..."
cat /home/${logged_in_user}/wireguard/wireguard.pub >> /home/${logged_in_user}/.ssh/authorized_keys
fi
@ -82,12 +126,12 @@ PAYLOAD=$(cat <<EOF
EOF
)
log_print INFO "Current Payload is: $PAYLOAD"
log_print INFO "onm-bootstrap($PID): Current Payload is: $PAYLOAD"
if [ "$ACTION" == "CREATE" ]; then
curl -v -X POST -H "Content-Type: application/json" -d "$PAYLOAD" http://${ONM_IP}:8082/api/v1/node/create
log_print INFO "Setting Wireguard IP to Kubelet. Restarting Kubelet..."
log_print INFO "onm-bootstrap($PID): Setting Wireguard IP to Kubelet. Restarting Kubelet..."
WIREGUARD_VPN_IP=`ip a | grep wg | grep inet | awk '{print $2}' | cut -d'/' -f1`
echo "KUBELET_EXTRA_ARGS=--node-ip=${WIREGUARD_VPN_IP} --container-runtime-endpoint=unix:///run/containerd/containerd.sock" | sudo tee -a /etc/default/kubelet
sudo systemctl restart kubelet
@ -98,4 +142,4 @@ fi
# Declare configuration done successfully
ENDTIME=$(date +%s)
ELAPSED=$(( ENDTIME - STARTTIME ))
log_print INFO "onm-bootstrap.sh: Configuration done successfully in $ELAPSED seconds "
log_print INFO "onm-bootstrap($PID): onm-bootstrap.sh: Configuration done successfully in $ELAPSED seconds "

View File

@ -1,175 +0,0 @@
#!/bin/bash
# Set up the script variables
STARTTIME=$(date +%s)
PID=$(echo $$)
LOGFILE="/var/log/preinstall.$PID.log"
# Set up the logging for the script
sudo touch $LOGFILE
sudo chown $USER:$USER $LOGFILE
# All the output of this shell script is redirected to the LOGFILE
exec 3>&1 4>&2
trap 'exec 2>&4 1>&3' 0 1 2 3
exec 1>$LOGFILE 2>&1
# Find Architecture
ARCH_COMMAND=$(sudo arch)
AMD_ARCH="x86_64"
ARM_ARCH="aarch64"
if [ "$ARCH_COMMAND" = "$AMD_ARCH" ]; then
ARCHITECTURE="amd64"
elif [ "$ARCH_COMMAND" = "$ARM_ARCH" ]; then
ARCHITECTURE="arm64"
fi
# A function to print a message to the stdout as well as as the LOGFILE
log_print(){
level=$1
Message=$2
echo "$level [$(date)]: $Message"
echo "$level [$(date)]: $Message" >&3
}
# A function to check for the apt lock
Check_lock() {
i=0
log_print INFO "Preinstall (check_lock.$PID): Checking for apt lock"
while [ `ps aux | grep [l]ock_is_held | wc -l` != 0 ]; do
echo "Lock_is_held $i"
ps aux | grep [l]ock_is_held
sleep 10
((i=i+10));
done
log_print INFO "Preinstall (check_lock.$PID): Exited the while loop, time spent: $i"
echo "ps aux | grep apt"
ps aux | grep apt
log_print INFO "Preinstall (check_lock.$PID): Waiting for lock task ended properly."
}
# Function to check for the wg command
check_wg_installed() {
# Using command -v to check for the wg command
if command -v wg >/dev/null 2>&1; then
log_print INFO "Preinstall (check_wg_installed.$PID): WireGuard (wg) is installed."
log_print INFO "Preinstall (check_wg_installed.$PID): Location: $(which wg)"
else
log_print INFO "Preinstall (check_wg_installed.$PID): WireGuard (wg) is not installed."
fi
}
# Start the Configuration
log_print INFO "Preinstall ($PID): Configuration started!"
log_print INFO "Preinstall ($PID): Logs are saved at: $LOGFILE"
log_print INFO "Preinstall ($PID): Step 1: Adding modprobe br_netfilter and setting ip_forward = 1..."
# Modbprobe and ip_forward
sudo modprobe br_netfilter
echo "net.ipv4.ip_forward = 1" | sudo tee /etc/sysctl.conf
sudo sysctl -p
log_print INFO "Preinstall ($PID) Step 2: Installing wireguard and resolvconf"
Check_lock
# Step 1: Install WireGuard package
if ! command -v wg > /dev/null; then
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y wireguard
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y resolvconf
fi
# Step 2: Check if Wireguard is installed
log_print INFO "Preinstall (check_wg_installed.$PID) Step 2: Checking if WireGuard (wg) is installed..."
check_wg_installed
# Check for lock
Check_lock
# Step 3: Update the package list
log_print INFO "Preinstall ($PID) Step 3: Updating the package list"
sudo apt-get update
# Check for lock
Check_lock
# Install curl
log_print INFO "Preinstall ($PID) Step 4: Installing ca-certificates curl"
sudo apt-get install -y ca-certificates curl || { log_print ERROR "Preinstall ($PID) Step 4: curl installation failed!"; exit $EXITCODE; }
# Check for lock
Check_lock
log_print INFO "Preinstall ($PID) Step 5: Installing /etc/apt/keyrings"
sudo install -m 0755 -d /etc/apt/keyrings
# Check for lock
Check_lock
# Adding Kubernetes Repo
log_print INFO "Preinstall ($PID) Step 6: Adding Kubernetes Repo"
echo "deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.26/deb/ /" | sudo tee /etc/apt/sources.list.d/kubernetes.list
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.26/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg || { log_print ERROR "Preinstall ($PID) Step 5: Kubernetes repo can't be added!"; exit $EXITCODE; }
sudo apt-get update
# Check for lock
Check_lock
# Install Kubernetes
log_print INFO "Preinstall ($PID) Step 7: Installing Kubernetes"
sudo apt-get install -y kubeadm=1.26.15-1.1 --allow-downgrades || { log_print ERROR "Preinstall ($PID) Step 6: kubeadm installation failed!"; exit $EXITCODE; }
sudo apt-get install -y kubelet=1.26.15-1.1 --allow-downgrades || { log_print ERROR "Preinstall ($PID) Step 6: kubectl installation failed!"; exit $EXITCODE; }
sudo apt-get install -y kubectl=1.26.15-1.1 --allow-downgrades || { log_print ERROR "Preinstall ($PID) Step 6: kubelet installation failed!"; exit $EXITCODE; }
# Install Containerd
log_print INFO "Preinstall ($PID) Step 8: Installing Containerd"
mkdir -p $HOME/k8s-deps
wget https://github.com/containerd/containerd/releases/download/v1.7.2/containerd-1.7.2-linux-$ARCHITECTURE.tar.gz -P $HOME/k8s-deps
tar xvf $HOME/k8s-deps/containerd-1.7.2-linux-$ARCHITECTURE.tar.gz
sudo tar Cxzvf /usr/local $HOME/k8s-deps/containerd-1.7.2-linux-$ARCHITECTURE.tar.gz
wget https://github.com/opencontainers/runc/releases/download/v1.1.3/runc.$ARCHITECTURE -P $HOME/k8s-deps
sudo install -m 755 $HOME/k8s-deps/runc.$ARCHITECTURE /usr/local/sbin/runc
wget https://github.com/containernetworking/plugins/releases/download/v1.1.1/cni-plugins-linux-$ARCHITECTURE-v1.1.1.tgz -P $HOME/k8s-deps
sudo mkdir -p /opt/cni/bin
sudo tar Cxzvf /opt/cni/bin $HOME/k8s-deps/cni-plugins-linux-$ARCHITECTURE-v1.1.1.tgz
sudo mkdir /etc/containerd
containerd config default | sudo tee /etc/containerd/config.toml
sudo sed -i 's/SystemdCgroup = false/SystemdCgroup = true/' /etc/containerd/config.toml
sudo curl -L https://raw.githubusercontent.com/containerd/containerd/main/containerd.service -o /etc/systemd/system/containerd.service
sudo systemctl daemon-reload
sudo systemctl enable --now containerd
sudo systemctl restart containerd
sudo systemctl status containerd
# Holding upgrades for Kubernetes software (versions to updated manually)
log_print INFO "Preinstall ($PID) Step 9: Holding Packages"
sudo apt-mark hold kubeadm kubelet kubectl containerd
log_print INFO "Preinstall ($PID) Step 10: Checking Kubernetes versions"
kubeadm version || { log_print ERROR "Preinstall ($PID) Step 9: kubeadm installation failed!"; exit $EXITCODE; }
kubelet --version || { log_print ERROR "Preinstall ($PID) Step 9: kubelet installation failed!"; exit $EXITCODE; }
kubectl version
if [ $? -gt 1 ]; then
log_print ERROR "Preinstall ($PID) Step 10: kubectl installation failed!"; exit $EXITCODE;
fi
# Turn off the swap memory
log_print INFO "Preinstall ($PID) Step 11: Turn off swap..."
if [ `grep Swap /proc/meminfo | grep SwapTotal: | cut -d" " -f14` == "0" ]; then
log_print INFO "Preinstall ($PID) Step 11: The swap memory is Off"
else
sudo swapoff a || { log_print ERROR "Preinstall ($PID) Step 11: swap memory can't be turned off "; exit $EXITCODE; }
fi
log_print INFO "Preinstall ($PID) Step 12: Installing Helm..."
curl -fsSL -o $HOME/k8s-deps/get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 && chmod 700 $HOME/k8s-deps/get_helm.sh && $HOME/k8s-deps/get_helm.sh
# Add Cilium Helm Repo
helm repo add cilium https://helm.cilium.io/
helm repo update
# Declare configuration done successfully
ENDTIME=$(date +%s)
ELAPSED=$(( ENDTIME - STARTTIME ))
log_print INFO "Preinstall ($PID) Step 13: k8s-preinstall.sh: Configuration done successfully in $ELAPSED seconds "

View File

@ -1,69 +0,0 @@
#!/bin/bash
# Set up the script variables
STARTTIME=$(date +%s)
PID=$(echo $$)
LOGFILE="/var/log/onm-preinstall.$PID.log"
# Set up the logging for the script
sudo touch $LOGFILE
sudo chown $USER:$USER $LOGFILE
# All the output of this shell script is redirected to the LOGFILE
exec 3>&1 4>&2
trap 'exec 2>&4 1>&3' 0 1 2 3
exec 1>$LOGFILE 2>&1
# A function to print a message to the stdout as well as as the LOGFILE
log_print(){
level=$1
Message=$2
echo "$level [$(date)]: $Message"
echo "$level [$(date)]: $Message" >&3
}
# A function to check for the apt lock
Check_lock() {
i=0
log_print INFO "Checking for apt lock"
while [ `ps aux | grep [l]ock_is_held | wc -l` != 0 ]; do
echo "Lock_is_held $i"
ps aux | grep [l]ock_is_held
sleep 10
((i=i+10));
done
log_print INFO "Exited the while loop, time spent: $i"
echo "ps aux | grep apt"
ps aux | grep apt
log_print INFO "Waiting for lock task ended properly."
}
# Function to check for the wg command
check_wg_installed() {
echo "Checking if WireGuard (wg) is installed..."
# Using command -v to check for the wg command
if command -v wg >/dev/null 2>&1; then
echo "WireGuard (wg) is installed."
echo "Location: $(which wg)"
else
echo "WireGuard (wg) is not installed."
fi
}
log_print INFO "Installing wireguard and resolvconf"
Check_lock
# Step 1: Install WireGuard package
if ! command -v wg > /dev/null; then
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y wireguard
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y resolvconf
fi
# Step 2: Check if Wireguard is installed
check_wg_installed
# Declare configuration done successfully
ENDTIME=$(date +%s)
ELAPSED=$(( ENDTIME - STARTTIME ))
log_print INFO "onm-preinstall.sh: Configuration done successfully in $ELAPSED seconds "