diff --git a/ReleaseUpgradeALL.sh b/ReleaseUpgradeALL.sh new file mode 100755 index 0000000..a550482 --- /dev/null +++ b/ReleaseUpgradeALL.sh @@ -0,0 +1,99 @@ +#!/bin/bash + +######################################### +# ProxMox update tools # +# Upgrade OS release on all CT`s/VM`s # +# @author: Bram Prieshof # +######################################### + +#Dirs +WorkDir=$(dirname $0) +LogDir=$WorkDir/RelUpgradelogs +#Load functions +source $WorkDir/functions.sh +#Create Log folder +if [ ! -d $LogDir ]; then + mkdir $LogDir +fi + +#ID list of excluded VM/CT's +ExcludeList=() + +#Get what sould be updated +UpdateVM=true +UpdateCT=true + +while true; do + read -p "Wich software should be updated (D)Debian, (A)Alpine or (B)oth? " sofq + case $sofq in + [Dd]* ) + DoDebianReleaseUpdate=true + DoAlpineReleaseUpdate=false + break;; + [Aa]* ) + DoDebianReleaseUpdate=true + DoAlpineReleaseUpdate=true + break;; + [Bb]* ) + DoDebianReleaseUpdate=true + DoAlpineReleaseUpdate=true + break;; + * ) echo "Please answer with (D)Debian, (A)Alpine or (B)oth.";; + esac +done + +#Get required versions to upgrade to +if $DoDebianReleaseUpdate; then + echo "Enter the Debian version to upgrade to" + read NewDebianVersion + while true; do + read -p "Upgrade to Debian $NewDebianVersion, is this correct? -> yes/no?" yn + case $yn in + [Nn]* ) + echo "Enter the Debian version to upgrade to" + read NewDebianVersion + ;; + [Yy]* ) break;; + * ) echo "Choose yes or no.";; + esac + done +fi +if $DoAlpineReleaseUpdate; then +echo "Enter the Alpine version to upgrade to" + read NewAlpineVersion + while true; do + read -p "Upgrade to Alpine $NewAlpineVersion, is this correct? -> yes/no?" yn + case $yn in + [Nn]* ) + echo "Enter the Alpine version to upgrade to" + read NewAlpineVersion + ;; + [Yy]* ) break;; + * ) echo "Choose yes or no.";; + esac + done +fi + +#CT updates +if $UpdateCT; then + for CTID in $(pct list | tail -n+2 | awk '{print $1}'); do + #Skip CT if in ExcludeList + if [[ "${ExcludeList[*]}" =~ $CTID ]]; then echo "Notice: $CTID excluded"; continue; fi + # Skip CT if not running + if ! $(CT-State $CTID); then echo "Notice: $CTID not running"; continue; fi + #DEBUG echo "Task for $CTID" + CT-UpgradeRelease $CTID | tee "$LogDir"/"$CTID"_Release.log + done +fi + +#VM Updates +if $UpdateVM; then + for VMID in $(qm list | tail -n+2 | awk '{print $1}'); do + #Skip VM if in ExcludeList + if [[ "${ExcludeList[*]}" =~ $VMID ]]; then echo "Notice: $VMID excluded"; continue; fi + # Skip VM if not running + if ! $(VM-State $VMID); then echo "Notice: $VMID not running"; continue; fi + #DEBUG echo "Task for $VMID" + VM-UpgradeRelease $VMID | tee "$LogDir"/"$VMID"_Release.log + done +fi diff --git a/ReleaseUpgradeOne.sh b/ReleaseUpgradeOne.sh new file mode 100644 index 0000000..6d7f290 --- /dev/null +++ b/ReleaseUpgradeOne.sh @@ -0,0 +1,89 @@ +#!/bin/bash + +##################################### +# ProxMox update tools # +# Upgrade one CT/VM OS release # +# @author: Bram Prieshof # +##################################### + +#Load functions +source $(dirname $0)/functions.sh + +#InputChecks +## Check if exists +if [ -z ${1+x} ]; then echo "No input, usage: $0 "; exit; fi +## Check if valid (number/integer) +if [ -z "${1##*[!0-9]*}" ]; then echo "Invalid ID, usage: $0 "; exit; fi + +#Setup Vars, since a seemingly valid id is given +ID=$1 +VMS=$(qm list | tail -n+2 | awk '{print $1}') +CTS=$(pct list | tail -n+2 | awk '{print $1}') +ALLIDS=(${CTS[@]} ${VMS[@]}) + +## Check if ID exists +for ExistingID in "${ALLIDS[@]}"; do + if [ $ExistingID -eq $ID ]; then + IdExists=true + break + fi +IdExists=false +done +if ! $IdExists; then echo "This ID does not exist"; exit; fi + +#Check type of ID +if [[ $VMS =~ $ID ]];then + TYPE=VM; + DIST=$(qm guest exec $VMID -- awk -F= '$1=="ID" { print $2 ;}' /etc/os-release | jq '.["out-data"]') +fi +if [[ $CTS =~ $ID ]]; then + TYPE=CT + DIST=$(pct exec $CTID -- awk -F= '$1=="ID" { print $2 ;}' /etc/os-release) +fi + +#Check if VM/CT is running +if ! $($TYPE-State $ID); then echo "This ID is not runnig"; exit; fi + +#Check wich distro the CT/VM is running +if [[ "${DIST}" == *"debian"* ]]; then + #Ask the wanted Debian version + DoDebianReleaseUpdate=true + DoAlpineReleaseUpdate=false + + echo "Enter the Debian version to upgrade to" + read NewDebianVersion + while true; do + read -p "Upgrade to Debian $NewDebianVersion, is this correct? -> yes/no?" yn + case $yn in + [Nn]* ) + echo "Enter the Debian version to upgrade to" + read NewDebianVersion + ;; + [Yy]* ) break;; + * ) echo "Choose yes or no.";; + esac + done +elif [[ "${DIST}" == *"alpine"* ]]; then + #Ask the wanted Alpine version + DoAlpineReleaseUpdate=true + DoDebianReleaseUpdate=false + + echo "Enter the Alpine version to upgrade to" + read NewAlpineVersion + while true; do + read -p "Upgrade to Alpine $NewAlpineVersion, is this correct? -> yes/no?" yn + case $yn in + [Nn]* ) + echo "Enter the Alpine version to upgrade to" + read NewAlpineVersion + ;; + [Yy]* ) break;; + * ) echo "Choose yes or no.";; + esac + done +else + echo "Warning: Release upgrade are supported for this distro" + exit +fi + +$TYPE-UpgradeRelease $ID \ No newline at end of file diff --git a/functions.sh b/functions.sh index 782fa1c..4f186eb 100644 --- a/functions.sh +++ b/functions.sh @@ -117,4 +117,93 @@ VM-State (){ * ) return 2;; esac +} + +#Upgrading Release on CT +CT-UpgradeRelease () { + #If yet set then set CTID as $1 + if [ -z ${CTID+x} ]; then local CTID=$1;fi + echo "Upgrading release for $CTID" + #Getting distribution info + local DIST=$(pct exec $CTID -- awk -F= '$1=="ID" { print $2 ;}' /etc/os-release) + local RELVERSION=$(pct exec $CTID -- awk -F= '$1=="VERSION_ID" { print $2 ;}' /etc/os-release | cut -d"." -f 1,2) + local RELNAME=$(pct exec $CTID -- awk -F= '$1=="VERSION_CODENAME" { print $2 ;}' /etc/os-release) + local INSTCALL="pct exec $CTID" + local SNAPSHOTCMD="pct snapshot $CTID BeforeReleaseUpgrade --description" + local DOREBOOT=false + #Call generic release upgrader + UpgradeRelease + if $DOREBOOT; then + qm reboot $VMID + fi + unset DIST RELVERSION RELNAME INSTCALL DOREBOOT +} + +#Upgrading Release on VM +VM-UpgradeRelease () { + #If yet set then set VMID as $1 + if [ -z ${VMID+x} ]; then local VMID=$1;fi + echo "Upgrading release for $VMID, No output is provided until task is completed" + #Getting distribution info + local DIST=$(qm guest exec $VMID -- awk -F= '$1=="ID" { print $2 ;}' /etc/os-release | jq '.["out-data"]') + local RELVERSION=$(qm guest exec $VMID -- awk -F= '$1=="VERSION_ID" { print $2 ;}' /etc/os-release | jq '.["out-data"]' | cut -d"." -f 1,2 ) + local RELNAME=$(qm guest exec $VMID -- awk -F= '$1=="VERSION_CODENAME" { print $2 ;}' /etc/os-release | jq '.["out-data"]') + local INSTCALL="qm guest exec $VMID" + local SNAPSHOTCMD="qm snapshot $VMID BeforeReleaseUpgrade --description" + local DOREBOOT=false + #Call generic release upgrader + UpgradeRelease + if $DOREBOOT; then + qm reboot $VMID + fi + unset DIST RELVERSION RELNAME INSTCALL +} + +#Run release update, called by CT-UpgradeRelease and VM-UpgradeRelease +UpgradeRelease () { + #Getting distribution name + if [[ "${DIST}" == *"debian"* ]] && $DoDebianReleaseUpdate; then + if [ $RELNAME != $NewDebianVersion ]; then + #Create snapshot before upgrading + SNAPSHOTCMD "Before upgrade form Debian $RELNAME to Debian $NewDebianVersion" + #Fully update current version + $INSTCALL -- apt update + $INSTCALL -- apt upgrade -y + $INSTCALL -- apt full-upgrade -y + # Update repo file(s) + $INSTCALL -- sed -i "s/$RELNAME/$NewDebianVersion/g" /etc/apt/sources.list + $INSTCALL -- sed -i "s/$RELNAME/$NewDebianVersion/g" /etc/apt/sources.list.d/* + # Run upgrade to new release + $INSTCALL -- apt clean + $INSTCALL -- apt update + $INSTCALL -- apt upgrade -y + $INSTCALL -- apt full-upgrade -y + $INSTCALL -- apt autoremove -y + DOREBOOT=true + else + echo "Notice: Skiped, already up-to-date" + fi + elif [[ "${DIST}" == *"debian"* ]]; then + echo "Notice: Release upgrade disabled for Debian" + elif [[ "${DIST}" == *"alpine"* ]] && $DoAlpineReleaseUpdate ; then + if [ $RELVERSION != $NewAlpineVersion ]; then + #Create snapshot before upgrading + SNAPSHOTCMD "Before upgrade form Alpine $RELVERSION to Alpine $NewAlpineVersion" + #Fully update current version + $INSTCALL -- apk update + $INSTCALL -- apk upgrade + $INSTCALL -- sed "s/$RELVERSION/$NewAlpineVersion/g" /etc/apk/repositories + $INSTCALL -- apk update + $INSTCALL -- apk add --upgrade apk-tools + $INSTCALL -- apk upgrade --available + DOREBOOT=true + else + echo "Notice: Skiped, already up-to-date" + fi + elif [[ "${DIST}" == *"alpine"* ]]; then + echo "Notice: Release upgrade disabled for Alpine" + else + echo "Warning: Release upgrade are supported for this distro" + #exit 100 + fi } \ No newline at end of file