Files
ProxmoxHelper/functions.sh
Bram Prieshof 170757a749 Fixed releaseUpgrade scripts
Updated Debian upgrade commands to be beter suited for scirpts

Set command timeout to 0 for VM's

Fixed container reboots

Disabled Debian VM's upgrades - qemu-guest-agent quit while upgrading

Checking if ID is alive before asking its distro
2023-11-19 20:10:04 +01:00

211 lines
8.0 KiB
Bash

#!/bin/bash
##########################
# ProxMox update tools #
# FUNCTIONS #
# @author: Bram Prieshof #
##########################
#Update Packages and Applications on CT
#CT-UpdateBoth () {
# local CTID=$1
# echo "Updating Packages and Applications for $CTID"
# CT-UpdatePackages
# CT-UpdateApplicatons
#}
##Update Packages and Applications on VM
#VM-UpdateBoth () {
# local VMID=$1
# echo "Updating Packages and Applications for $VMID"
# VM-UpdatePackages
# VM-UpdateApplicatons
#}
#Updating Packages on CT
CT-UpdatePackages () {
#If not called by UpdateBoth then set $1 as CTID
if [ -z ${CTID+x} ]; then local CTID=$1;fi
echo "Updating Packages for $CTID"
#Getting distributionname
local DIST=$(pct exec $CTID -- awk -F= '$1=="ID" { print $2 ;}' /etc/os-release)
local INSTCALL="pct exec $CTID"
#Call generic package updater
UpdatePackages
unset DIST INSTCALL
}
#Updating Packages on VM
VM-UpdatePackages () {
#If not called by UpdateBoth then set $1 as VMID
if [ -z ${VMID+x} ]; then local VMID=$1;fi
echo "Updating Packages for $VMID, No output is provided until task is completed"
#Getting distributionname
local DIST=$(qm guest exec $VMID -- awk -F= '$1=="ID" { print $2 ;}' /etc/os-release | jq '.["out-data"]')
local INSTCALL="qm guest exec $VMID --timeout 0"
#Call generic package updater
UpdatePackages
unset DIST INSTCALL
}
#Starts package manager, called by CT-UpdatePackages and VM-UpdatePackages
UpdatePackages () {
#Getting distributionname
if [[ "${DIST}" == *"ubuntu"* ]] || [[ "${DIST}" == *"debian"* ]]; then
$INSTCALL -- apt update
$INSTCALL -- DEBIAN_FRONTEND=noninteractive apt upgrade -y -o Dpkg::Options::="--force-confold"
elif [[ "${DIST}" == *"alpine"* ]]; then
$INSTCALL -- apk update
$INSTCALL -- apk upgrade
elif [[ "${DIST}" == *"centos"* ]]; then
$INSTCALL -- dnf update -y
else
echo "Warning: Package manager in not supported"
#exit 100
fi
}
#Updating CT applications using /opt/ProxMoxToolKitAppUpdate.sh in CT
CT-UpdateApplicatons () {
#If not called by UpdateBoth then set $1 as CTID
if [ -z ${CTID+x} ]; then local CTID=$1;fi
#Test if CT has Applicatons update script
if pct exec $CTID -- test -f /opt/ProxMoxToolKitAppUpdate.sh ; then
echo "Updating Applications for $CTID"
pct exec $CTID -- sh /opt/ProxMoxToolKitAppUpdate.sh
else
echo "Warning: Application updates not enabled in $CTID"
#exit 102
fi
}
#Updating VM applications using /opt/ProxMoxToolKitAppUpdate.sh in VM
VM-UpdateApplicatons () {
#If not called by UpdateBoth then set $1 as VMID
if [ -z ${VMID+x} ]; then local VMID=$1;fi
#Test if VM has Applicatons update script
if [ $(qm guest exec $VMID -- test -f /opt/ProxMoxToolKitAppUpdate.sh |jq '.exitcode') == 0 ] ; then
echo "Updating Applications for $VMID, No output is provided until task is completed"
qm guest exec $VMID --timeout 0 -- sh /opt/ProxMoxToolKitAppUpdate.sh
else
echo "Warning: application updates not enabled in $VMID"
#exit 102
fi
}
#Get state of CT as exitcode (0 when running 1 when stoped and 2 if CT is existend or other error occurs)
CT-State () {
if [ -z ${CTID+x} ]; then local CTID=$1;fi
case "$(pct status $CTID 2>&1)" in
*running* )
return 0;;
*stopped* )
return 1;;
* )
return 2;;
esac
}
#Get state of VM as exitcode (0 when running 1 when stoped and 2 if VM is existend or other error occurs)
VM-State (){
if [ -z ${VMID+x} ]; then local VMID=$1;fi
case "$(qm status $VMID 2>&1)" in
*running* )
return 0;;
*stopped* )
return 1;;
* )
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
pct reboot $CTID
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"]' | sed 's#\\n##g' | tr -d '"' | cut -d"." -f 1,2 )
local RELNAME=$(qm guest exec $VMID -- awk -F= '$1=="VERSION_CODENAME" { print $2 ;}' /etc/os-release | jq '.["out-data"]' | sed 's#\\n##g' | tr -d '"')
local INSTCALL="qm guest exec $VMID --timeout 0"
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 && [[ "${TYPE}" == "CT" ]]; 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 -- DEBIAN_FRONTEND=noninteractive apt upgrade -y -o Dpkg::Options::="--force-confold"
$INSTCALL -- DEBIAN_FRONTEND=noninteractive apt full-upgrade -y -o Dpkg::Options::="--force-confold"
# Update repo file(s)
$INSTCALL -- sed -i "s/$RELNAME/$NewDebianVersion/g" /etc/apt/sources.list
$INSTCALL -- sh -c "sed -i "s/$RELNAME/$NewDebianVersion/g" /etc/apt/sources.list.d/\*"
# Run upgrade to new release
$INSTCALL -- apt clean
$INSTCALL -- apt update
$INSTCALL -- DEBIAN_FRONTEND=noninteractive apt upgrade -y -o Dpkg::Options::="--force-confold"
$INSTCALL -- DEBIAN_FRONTEND=noninteractive apt full-upgrade -y -o Dpkg::Options::="--force-confold"
$INSTCALL -- apt autoremove -y
DOREBOOT=true
else
echo "Notice: Skiped, already up-to-date"
fi
elif [[ "${DIST}" == *"debian"* ]] && $DoDebianReleaseUpdate && [[ "${TYPE}" == "VM" ]]; then
echo "Notice: Release upgrade for Debian VM's is not supported"
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 -i "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
}