commit f58f6a2953a937254668a8ec46c2aacc91a23840 Author: Bram Prieshof Date: Fri Apr 15 22:42:28 2022 +0200 Inital commit diff --git a/Playground/testCTfile.sh b/Playground/testCTfile.sh new file mode 100644 index 0000000..b4d570e --- /dev/null +++ b/Playground/testCTfile.sh @@ -0,0 +1,9 @@ +#!/bin/bash +CTID=100 +#CTID=$1 +FILE=/opt/test +if pct exec $CTID -- test -f $FILE ; then + echo "File exists in $CTID" +else + echo "File does not exist in $CTID." +fi diff --git a/Playground/testCTloop.sh b/Playground/testCTloop.sh new file mode 100644 index 0000000..91aaf09 --- /dev/null +++ b/Playground/testCTloop.sh @@ -0,0 +1,4 @@ +#!/bin/bash +for CTID in $(pct list | tail -n+2 | awk '{print $1}'); do + echo "Task for $CTID" +done diff --git a/Playground/testCTloopExclude.sh b/Playground/testCTloopExclude.sh new file mode 100644 index 0000000..4ed9374 --- /dev/null +++ b/Playground/testCTloopExclude.sh @@ -0,0 +1,8 @@ +#!/bin/bash +ExcludeList=(100 900) + +for CTID in $(pct list | tail -n+2 | awk '{print $1}'); do + if [[ "${ExcludeList[*]}" =~ $CTID ]]; then continue; fi + echo "Task for $CTID" +done + diff --git a/Playground/testCTpkgm.sh b/Playground/testCTpkgm.sh new file mode 100644 index 0000000..c0782b7 --- /dev/null +++ b/Playground/testCTpkgm.sh @@ -0,0 +1,15 @@ +#!/bin/bash +#CTID=$1 +CTID=100 +DIST=$(pct exec $CTID -- awk -F= '$1=="ID" { print $2 ;}' /etc/os-release) + + +if [[ "${DIST}" == *"ubuntu"* ]] || [[ "${DIST}" == *"debian"* ]]; then + echo "Apt Detected" +elif [[ "${DIST}" == *"alpine"* ]]; then + echo "APK Detected" +#elif [[ "${DIST}" == *"centos"* ]]; then +# echo "DNF Detected" +else + echo "Package manager in not supported" +fi diff --git a/Playground/testDetermineIfExists.sh b/Playground/testDetermineIfExists.sh new file mode 100644 index 0000000..64faece --- /dev/null +++ b/Playground/testDetermineIfExists.sh @@ -0,0 +1,20 @@ +#!/bin/bash +ID=100 +#ID=$1 +VMS=$(qm list | tail -n+2 | awk '{print $1}') +CTS=$(pct list | tail -n+2 | awk '{print $1}') +EXISTINGIDS=(${CTS[@]} ${VMS[@]}) + +#for ExistingID in $(qm list | tail -n+2 | awk '{print $1}' &&pct list | tail -n+2 | awk '{print $1}'); do +for ExistingID in "${EXISTINGIDS[@]}"; do + echo $ExistingID + if [ $ExistingID -eq $ID ]; then + echo Exists + IdExists=true + break + fi +IdExists=false +done +if ! $IdExists; then echo "This ID does not exist"; exit; fi + +echo end diff --git a/Playground/testDetermineType.sh b/Playground/testDetermineType.sh new file mode 100644 index 0000000..f70879e --- /dev/null +++ b/Playground/testDetermineType.sh @@ -0,0 +1,9 @@ +#!/bin/bash +ID=100 +#ID=$1 +VMS=$(qm list | tail -n+2 | awk '{print $1}') +CTS=$(pct list | tail -n+2 | awk '{print $1}') +#Determine id type +if [[ $VMS =~ $ID ]]; then TYPE=VM; fi +if [[ $CTS =~ $ID ]]; then TYPE=CT; fi +echo ID is $TYPE diff --git a/Playground/testVMfile.sh b/Playground/testVMfile.sh new file mode 100644 index 0000000..e069e3f --- /dev/null +++ b/Playground/testVMfile.sh @@ -0,0 +1,9 @@ +#!/bin/bash +#VMID=$1 +VMID=101 +FILE=/opt/test +if [ $(qm guest exec $VMID -- test -f $FILE |jq '.exitcode') == 0 ] ; then + echo "File exists in $VMID" +else + echo "File does not exist in $VMID." +fi diff --git a/Playground/testVMloop.sh b/Playground/testVMloop.sh new file mode 100644 index 0000000..5c81048 --- /dev/null +++ b/Playground/testVMloop.sh @@ -0,0 +1,4 @@ +#!/bin/bash +for VMID in $(qm list | tail -n+2 | awk '{print $1}'); do + echo "Task for $VMID" +done diff --git a/Playground/testVMpkgm.sh b/Playground/testVMpkgm.sh new file mode 100644 index 0000000..633b052 --- /dev/null +++ b/Playground/testVMpkgm.sh @@ -0,0 +1,14 @@ +#!/bin/bash +#VMID=$1 +VMID=100 +DIST=$(qm guest exec $VMID -- awk -F= '$1=="ID" { print $2 ;}' /etc/os-release | jq '.["out-data"]') + +if [[ "${DIST}" == *"ubuntu"* ]] || [[ "${DIST}" == *"debian"* ]]; then + echo "Apt Detected" +elif [[ "${DIST}" == *"alpine"* ]]; then + echo "APK Detected" +#elif [[ "${DIST}" == *"centos"* ]]; then +# echo "DNF Detected" +else + echo "Package manager in not supported" +fi diff --git a/ReadMe.md b/ReadMe.md new file mode 100644 index 0000000..4427a1c --- /dev/null +++ b/ReadMe.md @@ -0,0 +1,20 @@ +# Proxmox helper scrips + + +### Scripts and their usage +| Name | Usage | +|:------------:|:-------------------------------------------------:| +| UpdateAll.sh | Update all CT's and VM's | +| UpdateOne.sh | Update one CT or VM by passing its ID as argument | +| functions.sh | Shared functions for both scripts | + + +### Installation +* Install jq `apt install jq` +* Clone this repo to your Proxmox service + +### Features +* Upgrade all CT's/VM's at once +* Exclude list in `UpdateAll.sh`, to skip once you don't want to update +* Updates using package manager +* Application updates by checking for and running /opt/ProxMoxToolKitAppUpdate.sh _!This script runs using the sh shell_ \ No newline at end of file diff --git a/UpdateAll.sh b/UpdateAll.sh new file mode 100644 index 0000000..10f89d9 --- /dev/null +++ b/UpdateAll.sh @@ -0,0 +1,85 @@ +#!/bin/bash + +########################## +# ProxMox update tools # +# Update all CT`s/VM`s # +# @author: Bram Prieshof # +########################## + +#Load functions +source $(dirname $0)/functions.sh + +#ID list of excluded VM/CT's +ExcludeList=() + +#Get what sould be updated +UpdateVM=true +UpdateCT=true + +#while true; do +# read -p "Wich systems should be updated (A)ll, (C)T's only or (V)M's only? " sysq +# case $sysq in +# [Aa]* ) +# UpdateVM=true +# UpdateCT=true +# break;; +# [Cc]* ) +# UpdateVM=false +# UpdateCT=true +# break;; +# [Vv]* ) +# UpdateVM=true +# UpdateCT=false +# break;; +# * ) echo "Please answer with (A)ll, (C)T's or (V)M's.";; +# esac +#done + +while true; do + read -p "Wich software should be updated (P)ackages, (A)pplications or (B)oth? " sofq + case $sofq in + [Pp]* ) + UpdatePKG=true + UpdateAPP=false + break;; + [Aa]* ) + UpdatePKG=false + UpdateAPP=true + break;; + [Bb]* ) + UpdatePKG=true + UpdateAPP=true + break;; + * ) echo "Please answer with (A)ll, (C)T`s or (V)M`s.";; + esac +done + +#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 + #DEBUG echo "Task for $CTID" + if $UpdatePKG; then + CT-UpdatePackages $CTID + fi + if $UpdateAPP; then + CT-UpdateApplicatons $CTID + fi + done +fi + +#VM Updates +if $UpdateVM; then + for VMID in $(qm list | tail -n+2 | awk '{print $1}'); do + #Skip CT if in ExcludeList + if [[ "${ExcludeList[*]}" =~ $VMID ]]; then echo "Notice: $VMID excluded"; continue; fi + #DEBUG echo "Task for $VMID" + if $UpdatePKG; then + VM-UpdatePackages $VMID + fi + if $UpdateAPP; then + VM-UpdateApplicatons $VMID + fi + done +fi diff --git a/UpdateOne.sh b/UpdateOne.sh new file mode 100644 index 0000000..e94f130 --- /dev/null +++ b/UpdateOne.sh @@ -0,0 +1,57 @@ +#!/bin/bash + +########################## +# ProxMox update tools # +# Update one CT/VM # +# @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; fi +if [[ $CTS =~ $ID ]]; then TYPE=CT; fi + +#Ask what should be updated +while true; do + read -p "Wich software should be updated (P)ackages, (A)pplications or (B)oth? " sofq + case $sofq in + [Pp]* ) + #Update system packages + $TYPE-UpdatePackages $ID + break;; + [Aa]* ) + #Update applications + $TYPE-UpdateApplicatons $ID + break;; + [Bb]* ) + #Update both + $TYPE-UpdatePackages $ID + $TYPE-UpdateApplicatons $ID + break;; + * ) echo "Please answer with (A)ll, (C)T`s or (V)M`s.";; + esac +done diff --git a/functions.sh b/functions.sh new file mode 100644 index 0000000..7542ccc --- /dev/null +++ b/functions.sh @@ -0,0 +1,94 @@ +#!/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" + #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 -- sh /opt/ProxMoxToolKitAppUpdate.sh + else + echo "Warning: application updates not enabled in $VMID" + #exit 102 + fi +} \ No newline at end of file