From 9e8b4a89fd878b19e8dff8ddd38a96d582278e24 Mon Sep 17 00:00:00 2001 From: Bram Prieshof Date: Tue, 30 Aug 2022 21:34:58 +0200 Subject: [PATCH] Added check for state of VM/CT to avoid errors --- UpdateAll.sh | 8 ++++++-- UpdateOne.sh | 5 ++++- functions.sh | 28 +++++++++++++++++++++++++++- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/UpdateAll.sh b/UpdateAll.sh index 0772be5..1ad1128 100644 --- a/UpdateAll.sh +++ b/UpdateAll.sh @@ -57,7 +57,7 @@ while true; do UpdatePKG=true UpdateAPP=true break;; - * ) echo "Please answer with (A)ll, (C)T`s or (V)M`s.";; + * ) echo "Please answer with (P)ackages, (A)pplications or (B)oth.";; esac done @@ -66,6 +66,8 @@ 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" if $UpdatePKG; then CT-UpdatePackages $CTID | tee "$LogDir"/"$CTID"_Pkgs.log @@ -79,8 +81,10 @@ fi #VM Updates if $UpdateVM; then for VMID in $(qm list | tail -n+2 | awk '{print $1}'); do - #Skip CT if in ExcludeList + #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" if $UpdatePKG; then VM-UpdatePackages $VMID | tee "$LogDir"/"$VMID"_Pkgs.log diff --git a/UpdateOne.sh b/UpdateOne.sh index e94f130..83b450f 100644 --- a/UpdateOne.sh +++ b/UpdateOne.sh @@ -35,6 +35,9 @@ if ! $IdExists; then echo "This ID does not exist"; exit; fi if [[ $VMS =~ $ID ]]; then TYPE=VM; fi if [[ $CTS =~ $ID ]]; then TYPE=CT; fi +check#Check if VM/CT is running +if ! $($TYPE-State $ID); then echo "This ID is not runnig"; exit; fi + #Ask what should be updated while true; do read -p "Wich software should be updated (P)ackages, (A)pplications or (B)oth? " sofq @@ -52,6 +55,6 @@ while true; do $TYPE-UpdatePackages $ID $TYPE-UpdateApplicatons $ID break;; - * ) echo "Please answer with (A)ll, (C)T`s or (V)M`s.";; + * ) echo "Please answer with (P)ackages, (A)pplications or (B)oth.";; esac done diff --git a/functions.sh b/functions.sh index 7542ccc..fc1a61c 100644 --- a/functions.sh +++ b/functions.sh @@ -2,7 +2,7 @@ ########################## # ProxMox update tools # -# FUNCTIONS # +# FUNCTIONS # # @author: Bram Prieshof # ########################## @@ -91,4 +91,30 @@ VM-UpdateApplicatons () { 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 } \ No newline at end of file