Skip to content

Instantly share code, notes, and snippets.

@marinnedea
Last active September 18, 2020 14:10
Show Gist options
  • Save marinnedea/572fbcfaa41d01d809ffbb897fec6676 to your computer and use it in GitHub Desktop.
Save marinnedea/572fbcfaa41d01d809ffbb897fec6676 to your computer and use it in GitHub Desktop.
#!/bin/bash
#########################################################################################################
# Description: Update walinuxagent from repository or, if latest version not available, from github #
# Author: Marin Nedea #
# Created: Sep 14th, 2020 #
# Usage: Just run the script with sh (e.g. sh script.sh) #
# Requires: AzCli 2.0 installed on the machine you're running this script on #
# https://docs.microsoft.com/en-us/cli/azure/install-azure-cli?view=azure-cli-latest #
# If enabled, you can run it through the bash Cloud Shell in your Azure Portal page. #
# WARNING: Tested only with CentOS/RHEL 7+, SLES 12+, Oracle 7+, Debian 9+, Ubuntu 16.04 +. #
#########################################################################################################
# Log execution
exec 3>&1 4>&2
trap 'exec 2>&4 1>&3' 0 1 2 3
exec 1>/var/log/wala_update.log 2>&1
#set -x
###########################
### FUNCTIONS ###
###########################
# Compare versions function (only works with numbers)
ver () {
printf "%03d%03d%03d%03d" $(echo "$1" | tr '.' ' ')
}
# Check distribution and install curl, wget and unzip if needed
distrocheck () {
case $DISTR in
[Uu]buntu|[Dd]ebian)
echo "Ubuntu/Debian"
! which curl && apt-get install -y curl
! which wget && apt-get install -y wget
! which unzip && apt-get install -y unzip
;;
[Cc]ent[Oo][Ss]|[Oo]racle)
echo "CentOS/Oracle"
! which curl && yum install -y curl
! which wget && yum install -y wget
! which unzip && yum install -y unzip
;;
[Rr][Hh][Ee][Ll]|[Rr]ed|[Rr]ed[Hh]at)
echo "RedHat"
curl -o epel-release-latest-7.noarch.rpm https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm 2>/dev/null
yum install epel-release-latest-7.noarch.rpm -y
! which wget && yum install -y --enablerepo=epel wget
! which unzip && yum install -y --enablerepo=epel unzip
yum remove epel-release -y
;;
[Ss][Uu][Ss][Ee]|[Ss][Ll][Ee][Ss])
echo "SLES"
! which curl && zypper -n install curl
! which wget && zypper -n install wget
! which unzip && zypper -n install unzip
;;
*)
echo "Unknown distribution. Aborting"
exit 0
;;
esac
}
get_python_version () {
if [[ ! -z $(command -v python) ]]
then
pver="2"
#echo 2
elif [[ ! -z $(command -v python3) ]]
then
pver="3"
#echo 3
else
pver="0"
echo "No python available on the machine. Aborting"
exit 0
fi
}
# Install pip, setuptools and wheel
pipinstall () {
get_python_version
if [[ "${pver}" == "2" ]]
then
case ${DISTR} in
[Uu]buntu|[Dd]ebian)
apt-get install python-pip -y
pip install --upgrade pip setuptools
;;
[Cc]ent[Oo][Ss]|[Oo]racle|rhel|[Rr]ed|[Rr]ed[Hh]at)
wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm 2>/dev/null
yum install epel-release-latest-7.noarch.rpm -y
yum install python-setuptools -y
yum remove epel-release -y
;;
[Ss][Uu][Ss][Ee]|SLES|sles)
zypper -n install python-pip
pip install --upgrade pip setuptools
;;
*)
echo "Unknown distribution. Aborting"
exit 0
;;
esac
fi
}
# Check redhat repo
repocheck () {
eus_check=$(yum repoinfo rhui-microsoft-azure-rhel7-eus | grep -i status| awk -F": " '{print $2}')
[[ "${eus_check}" == "enabled" ]] && reposet="1" || reposet="0"
}
# Set the repo to non EUS, if needed
reposetNONEUS () {
if [[ "${reposet}" == "1" ]]
then
rm -f /etc/yum/vars/releasever
yum --disablerepo='*' remove 'rhui-azure-rhel7-eus' -y
yum --config='https://rhelimage.blob.core.windows.net/repositories/rhui-microsoft-azure-rhel7.config' install 'rhui-azure-rhel7' -y
yum update --assumeno
fi
}
# Set the repo back to EUS, if needed
reposetEUS () {
if [[ "${reposet}" == "1" ]]
then
yum --disablerepo='*' remove 'rhui-azure-rhel7' -y
yum --config='https://rhelimage.blob.core.windows.net/repositories/rhui-microsoft-azure-rhel7-eus.config' install 'rhui-azure-rhel7-eus' -y
echo $(. /etc/os-release && echo $VERSION_ID) > /etc/yum/vars/releasever
yum update --assumeno
fi
}
# Install agent from repository
walainstall_repo () {
case $DISTR in
[Uu]buntu)
echo "Ubuntu/Debian"
apt-get install -y walinuxagent
;;
[Dd]ebian)
echo "Ubuntu/Debian"
apt-get install -y waagent
;;
[Cc][Ee][Nn][Tt][Oo][Ss]|[Oo][Rr][Aa][Cc][Ll][Ee])
echo "CentOS/Oracle"
yum install -y WALinuxAgent
;;
[Rr][Hh][Ee][Ll]|[Rr]ed|[Rr]ed[Hh]at)
echo "RedHat"
repocheck
reposetNONEUS
yum install -y WALinuxAgent
reposetEUS
;;
[Ss][Uu][Ss][Ee]|[Ss][Ll][Ee][Ss])
echo "SLES"
zypper install -n python-azure-agent
;;
*)
echo "Unknown distribution. Aborting"
exit 0
;;
esac
}
# Install waagent from github function
walainstall_git () {
# Backup existing WALinuxAgent files
echo "Backin-up ovf-env.xml"
cp /var/lib/waagent/ovf-env.xml /tmp/ovf-env.xml
# Install WALinuxAgent
echo "Downloading latest waagent release"
wget https://github.com/Azure/WALinuxAgent/archive/v${lastwala}.zip 2>/dev/null
echo "Extracting ${lastwala}.zip"
unzip v$lastwala.zip
echo "Starting installation"
cd WALinuxAgent-${lastwala}
get_python_version
# Run the installer
[[ "${pver}" == "2" ]] && python setup.py install
[[ "${pver}" == "3" ]] && python3 setup.py install
# Fixing bug where the agent installed via git is unable to correctly identify the python env, by re-installing, once more the agent from repository
}
# Restart walinuxagent
restartagentcron () {
#write out current crontab
crontab -l > /tmp/mycron
#echo new cron into cron file
case ${DISTR} in
[Uu]buntu|[Dd]ebian)
echo "*/10 * * * * systemctl restart walinuxagent ; crontab -l | grep -v walinuxagent | crontab -" >> /tmp/mycron
;;
*)
echo "*/10 * * * * systemctl restart waagent ; crontab -l | grep -v waagent | crontab -" >> /tmp/mycron
;;
esac
#install new cron file
crontab /tmp/mycron
crontab -l
rm /tmp/mycron
}
###############################################################################################
# START AGENT UPDATE
# Get the working directory
workingdir=$(pwd)
# Make sure autoupdate is disabled to avoid download bug.
oldstring=$(grep AutoUpdate.Enabled /etc/waagent.conf)
sed -i -e "s/${oldstring}/AutoUpdate.Enabled=n/g" /etc/waagent.conf
# distro check
DISTR=$(cat /etc/*release | grep -i pretty | awk -F"\"" '{print $2}' | awk '{print $1}')
distrocheck
# Get latest walinuxagent version from github (see https://github.com/Azure/WALinuxAgent/releases/latest )
lastwala=$(curl -s https://github.com/Azure/WALinuxAgent/releases/latest 2>/dev/null | grep -o -P '(?<=v).*(?=\")')
# Check running waaagent version
#waagentrunning=$(waagent --version | head -n1 | awk '{print $1}' | awk -F"-" '{print $2}')
waagentrunning=$(grep -i walinuxagent /var/log/waagent.log | grep -i running | grep -i "goal state agent$" | tail -1 | cut -d" " -f6 | cut -d"-" -f2)
# Compare versions
echo "Comparing agent running version with available one"
[ $(ver ${waagentrunning}) -lt $(ver ${lastwala}) ] && upagent="1" || upagent="0"
###############################################
### First install latest agent from repository ###
###############################################
[[ "${upagent}" == "1" ]] && walainstall_repo
# Check agent again post install from repo
waagentrunning=$(grep -i walinuxagent /var/log/waagent.log | grep -i running | grep -i "goal state agent$" | tail -1 | cut -d" " -f6 | cut -d"-" -f2)
# Compare versions
echo "Comparing agent running version with available one"
[ $(ver ${waagentrunning}) -lt $(ver ${lastwala}) ] && upagent="1" || upagent="0"
[[ "${upagent}" == "1" ]] && install_from_git="1" || install_from_git="0"
###############################################
### Install latest agent from git ###
###############################################
if [[ "${install_from_git}" == "1" ]]
then
# Prerequisites check
pipcheck=$(python -m pip -V | grep -i "not installed")
[[ -z "${pipcheck}" ]] && pipinstall
# Install agent from git
if walainstall_git
then
echo "Installation completed"
# Restore ovf-env.xml from backup
echo "Restoring ovf-env.xml file"
cp /tmp/ovf-env.xml /var/lib/waagent/ovf-env.xml
# Check new agent version
waagentrunning=$(waagent --version 2> /dev/null | head -n1 | awk '{print $1}' | awk -F"-" '{print $2}')
[[ ! -z ${waagentrunning} ]] && echo "Running agent version is now -- ${waagentrunning} -- " >> ${workingdir}/stdout
echo "Restarting agent 30 seconds after this script completes.
This should give enough time to Custom Script Extension to report status
and also to the waagent to comunicate with the portal the new version."
else
echo "Running agent version is now -- ${waagentrunning} -- " >> ${workingdir}/stdout
fi
else
echo "Running agent version is now -- ${waagentrunning} -- " >> ${workingdir}/stdout
fi
# Reload daemons
echo "Reloading daemons"
systemctl daemon-reload
# Restart waagent
restartagentcron
# END
echo "All done, exiting."
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment