capsul.org webapp

Add vm state management scripts

Changed files
+161
capsulflask
+39
capsulflask/shell_scripts/force-stop.sh
··· 1 + #!/bin/sh -e 2 + # 3 + # force-stop.sh - pull the plug on a capsul 4 + # if it is already stopped, do nothing 5 + 6 + # State Key 7 + # NOSTATE: 0 - no state 8 + # RUNNING: 1 - the domain is running 9 + # BLOCKED: 2 - the domain is blocked on resource 10 + # PAUSED: 3 - the domain is paused by user 11 + # SHUTDOWN: 4 - the domain is being shut down 12 + # SHUTOFF: 5 - the domain is shut off 13 + # CRASHED: 6 - the domain is crashed 14 + # PMSUSPENDED: 7 - the domain is suspended by guest power management 15 + # LAST: 8 NB: this enum value will increase over time as new events are added to the libvirt API. It reflects the last state supported by this version of the libvirt API. 16 + 17 + vmname="$1" 18 + 19 + [ "$vmname" = '' ] && printf 'you must set $vmname\n' && exit 1 20 + 21 + if printf "$vmname" | grep -vqE '^(cvm|capsul)-[a-z0-9]{10}$'; then 22 + printf 'vmname %s must match ^capsul-[a-z0-9]{10}$\n' "$vmname" 23 + exit 1 24 + fi 25 + 26 + state="$(virsh domstats $vmname | grep state.state | cut -d '=' -f 2)" 27 + 28 + [ "$state" = '' ] && printf 'state was not detected. must match ^[0-8]$\n' && exit 1 29 + 30 + if printf "$state" | grep -vqE '^[0-8]$'; then 31 + printf 'state %s must match ^[0-8]$\n' "$state" 32 + exit 1 33 + fi 34 + 35 + case "$state" in 36 + [1-47]) virsh destroy "$vmname" > /dev/null ;; # virsh destroy == pull the plug 37 + [5-6]) printf "%s is already off\n" "$vmname" ;; 38 + esac 39 +
+43
capsulflask/shell_scripts/restart.sh
··· 1 + #!/bin/sh -e 2 + # 3 + # restart.sh - restarts a capsul 4 + # if it is stopping, do nothing 5 + 6 + # State Key 7 + # NOSTATE: 0 - no state 8 + # RUNNING: 1 - the domain is running 9 + # BLOCKED: 2 - the domain is blocked on resource 10 + # PAUSED: 3 - the domain is paused by user 11 + # SHUTDOWN: 4 - the domain is being shut down 12 + # SHUTOFF: 5 - the domain is shut off 13 + # CRASHED: 6 - the domain is crashed 14 + # PMSUSPENDED: 7 - the domain is suspended by guest power management 15 + # LAST: 8 NB: this enum value will increase over time as new events are added to the libvirt API. It reflects the last state supported by this version of the libvirt API. 16 + 17 + vmname="$1" 18 + 19 + [ "$vmname" = '' ] && printf 'you must set $vmname\n' && exit 1 20 + 21 + if printf "$vmname" | grep -vqE '^(cvm|capsul)-[a-z0-9]{10}$'; then 22 + printf 'vmname %s must match ^capsul-[a-z0-9]{10}$\n' "$vmname" 23 + exit 1 24 + fi 25 + 26 + state="$(virsh domstats $vmname | grep state.state | cut -d '=' -f 2)" 27 + 28 + [ "$state" = '' ] && printf 'state was not detected. must match ^[0-8]$\n' && exit 1 29 + 30 + if printf "$state" | grep -vqE '^[0-8]$'; then 31 + printf 'state %s must match ^[0-8]$\n' "$state" 32 + exit 1 33 + fi 34 + 35 + case "$state" in 36 + 1) virsh reboot "$vmname" > /dev/null ;; 37 + 2) printf '%s cannot be rebooted while it is blocked\n' "$vmname" ;; 38 + 37) printf '%s cannot be rebooted while it is paused\n' "$vmname" ;; 39 + 4) printf '%s cannot be rebooted while it is shutting down\n' "$vmname" ;; 40 + 56) printf '%s cannot be rebooted while it is stopped\n' "$vmname" ;; 41 + *) printf 'unknown script behavior. state: %s\n' "$state" ;; 42 + esac 43 +
+40
capsulflask/shell_scripts/start.sh
··· 1 + #!/bin/sh -e 2 + # 3 + # start.sh - starts a capsul 4 + # if it is already started, do nothing 5 + # if it is stopping, do nothing 6 + 7 + # State Key 8 + # NOSTATE: 0 - no state 9 + # RUNNING: 1 - the domain is running 10 + # BLOCKED: 2 - the domain is blocked on resource 11 + # PAUSED: 3 - the domain is paused by user 12 + # SHUTDOWN: 4 - the domain is being shut down 13 + # SHUTOFF: 5 - the domain is shut off 14 + # CRASHED: 6 - the domain is crashed 15 + # PMSUSPENDED: 7 - the domain is suspended by guest power management 16 + # LAST: 8 NB: this enum value will increase over time as new events are added to the libvirt API. It reflects the last state supported by this version of the libvirt API. 17 + 18 + vmname="$1" 19 + 20 + [ "$vmname" = '' ] && printf 'you must set $vmname\n' && exit 1 21 + 22 + if printf "$vmname" | grep -vqE '^(cvm|capsul)-[a-z0-9]{10}$'; then 23 + printf 'vmname %s must match ^capsul-[a-z0-9]{10}$\n' "$vmname" 24 + exit 1 25 + fi 26 + 27 + state="$(virsh domstats $vmname | grep state.state | cut -d '=' -f 2)" 28 + 29 + [ "$state" = '' ] && printf 'state was not detected. must match ^[0-8]$\n' && exit 1 30 + 31 + if printf "$state" | grep -vqE '^[0-8]$'; then 32 + printf 'state %s must match ^[0-8]$\n' "$state" 33 + exit 1 34 + fi 35 + 36 + case "$state" in 37 + 1) printf '%s is already running\n' "$vmname" ;; 38 + 4) printf '%s cannot be started while it is shutting down\n' "$vmname" ;; 39 + [235-7]) virsh start "$vmname" > /dev/null ;; 40 + esac
+39
capsulflask/shell_scripts/stop.sh
··· 1 + #!/bin/sh -e 2 + # 3 + # stop.sh - stops a capsul 4 + # if it is already stopped, do nothing 5 + 6 + # State Key 7 + # NOSTATE: 0 - no state 8 + # RUNNING: 1 - the domain is running 9 + # BLOCKED: 2 - the domain is blocked on resource 10 + # PAUSED: 3 - the domain is paused by user 11 + # SHUTDOWN: 4 - the domain is being shut down 12 + # SHUTOFF: 5 - the domain is shut off 13 + # CRASHED: 6 - the domain is crashed 14 + # PMSUSPENDED: 7 - the domain is suspended by guest power management 15 + # LAST: 8 NB: this enum value will increase over time as new events are added to the libvirt API. It reflects the last state supported by this version of the libvirt API. 16 + 17 + vmname="$1" 18 + 19 + [ "$vmname" = '' ] && printf 'you must set $vmname\n' && exit 1 20 + 21 + if printf "$vmname" | grep -vqE '^(cvm|capsul)-[a-z0-9]{10}$'; then 22 + printf 'vmname %s must match ^capsul-[a-z0-9]{10}$\n' "$vmname" 23 + exit 1 24 + fi 25 + 26 + state="$(virsh domstats $vmname | grep state.state | cut -d '=' -f 2)" 27 + 28 + [ "$state" = '' ] && printf 'state was not detected. must match ^[0-8]$\n' && exit 1 29 + 30 + if printf "$state" | grep -vqE '^[0-8]$'; then 31 + printf 'state %s must match ^[0-8]$\n' "$state" 32 + exit 1 33 + fi 34 + 35 + case "$state" in 36 + [1-37]) virsh shutdown "$vmname" > /dev/null ;; 37 + [4-6]) printf '%s is already shutting down\n' "$vmname" ;; 38 + esac 39 +