209 lines
7.4 KiB
Bash
209 lines
7.4 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 -- apt upgrade -y
|
|
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
|
|
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
|
|
} |