From 2e265703bd6ef03f015cbd94806b13bca1359be6 Mon Sep 17 00:00:00 2001 From: j3s Date: Tue, 16 Feb 2021 20:58:23 -0600 Subject: [PATCH] Add vm state management scripts --- capsulflask/shell_scripts/force-stop.sh | 39 ++++++++++++++++++++++ capsulflask/shell_scripts/restart.sh | 43 +++++++++++++++++++++++++ capsulflask/shell_scripts/start.sh | 40 +++++++++++++++++++++++ capsulflask/shell_scripts/stop.sh | 39 ++++++++++++++++++++++ 4 files changed, 161 insertions(+) create mode 100755 capsulflask/shell_scripts/force-stop.sh create mode 100755 capsulflask/shell_scripts/restart.sh create mode 100755 capsulflask/shell_scripts/start.sh create mode 100755 capsulflask/shell_scripts/stop.sh diff --git a/capsulflask/shell_scripts/force-stop.sh b/capsulflask/shell_scripts/force-stop.sh new file mode 100755 index 0000000..7e162b0 --- /dev/null +++ b/capsulflask/shell_scripts/force-stop.sh @@ -0,0 +1,39 @@ +#!/bin/sh -e +# +# force-stop.sh - pull the plug on a capsul +# if it is already stopped, do nothing + +# State Key +# NOSTATE: 0 - no state +# RUNNING: 1 - the domain is running +# BLOCKED: 2 - the domain is blocked on resource +# PAUSED: 3 - the domain is paused by user +# SHUTDOWN: 4 - the domain is being shut down +# SHUTOFF: 5 - the domain is shut off +# CRASHED: 6 - the domain is crashed +# PMSUSPENDED: 7 - the domain is suspended by guest power management +# 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. + +vmname="$1" + +[ "$vmname" = '' ] && printf 'you must set $vmname\n' && exit 1 + +if printf "$vmname" | grep -vqE '^(cvm|capsul)-[a-z0-9]{10}$'; then + printf 'vmname %s must match ^capsul-[a-z0-9]{10}$\n' "$vmname" + exit 1 +fi + +state="$(virsh domstats $vmname | grep state.state | cut -d '=' -f 2)" + +[ "$state" = '' ] && printf 'state was not detected. must match ^[0-8]$\n' && exit 1 + +if printf "$state" | grep -vqE '^[0-8]$'; then + printf 'state %s must match ^[0-8]$\n' "$state" + exit 1 +fi + +case "$state" in + [1-47]) virsh destroy "$vmname" > /dev/null ;; # virsh destroy == pull the plug + [5-6]) printf "%s is already off\n" "$vmname" ;; +esac + diff --git a/capsulflask/shell_scripts/restart.sh b/capsulflask/shell_scripts/restart.sh new file mode 100755 index 0000000..22e9363 --- /dev/null +++ b/capsulflask/shell_scripts/restart.sh @@ -0,0 +1,43 @@ +#!/bin/sh -e +# +# restart.sh - restarts a capsul +# if it is stopping, do nothing + +# State Key +# NOSTATE: 0 - no state +# RUNNING: 1 - the domain is running +# BLOCKED: 2 - the domain is blocked on resource +# PAUSED: 3 - the domain is paused by user +# SHUTDOWN: 4 - the domain is being shut down +# SHUTOFF: 5 - the domain is shut off +# CRASHED: 6 - the domain is crashed +# PMSUSPENDED: 7 - the domain is suspended by guest power management +# 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. + +vmname="$1" + +[ "$vmname" = '' ] && printf 'you must set $vmname\n' && exit 1 + +if printf "$vmname" | grep -vqE '^(cvm|capsul)-[a-z0-9]{10}$'; then + printf 'vmname %s must match ^capsul-[a-z0-9]{10}$\n' "$vmname" + exit 1 +fi + +state="$(virsh domstats $vmname | grep state.state | cut -d '=' -f 2)" + +[ "$state" = '' ] && printf 'state was not detected. must match ^[0-8]$\n' && exit 1 + +if printf "$state" | grep -vqE '^[0-8]$'; then + printf 'state %s must match ^[0-8]$\n' "$state" + exit 1 +fi + +case "$state" in + 1) virsh reboot "$vmname" > /dev/null ;; + 2) printf '%s cannot be rebooted while it is blocked\n' "$vmname" ;; + 37) printf '%s cannot be rebooted while it is paused\n' "$vmname" ;; + 4) printf '%s cannot be rebooted while it is shutting down\n' "$vmname" ;; + 56) printf '%s cannot be rebooted while it is stopped\n' "$vmname" ;; + *) printf 'unknown script behavior. state: %s\n' "$state" ;; +esac + diff --git a/capsulflask/shell_scripts/start.sh b/capsulflask/shell_scripts/start.sh new file mode 100755 index 0000000..f9e01d1 --- /dev/null +++ b/capsulflask/shell_scripts/start.sh @@ -0,0 +1,40 @@ +#!/bin/sh -e +# +# start.sh - starts a capsul +# if it is already started, do nothing +# if it is stopping, do nothing + +# State Key +# NOSTATE: 0 - no state +# RUNNING: 1 - the domain is running +# BLOCKED: 2 - the domain is blocked on resource +# PAUSED: 3 - the domain is paused by user +# SHUTDOWN: 4 - the domain is being shut down +# SHUTOFF: 5 - the domain is shut off +# CRASHED: 6 - the domain is crashed +# PMSUSPENDED: 7 - the domain is suspended by guest power management +# 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. + +vmname="$1" + +[ "$vmname" = '' ] && printf 'you must set $vmname\n' && exit 1 + +if printf "$vmname" | grep -vqE '^(cvm|capsul)-[a-z0-9]{10}$'; then + printf 'vmname %s must match ^capsul-[a-z0-9]{10}$\n' "$vmname" + exit 1 +fi + +state="$(virsh domstats $vmname | grep state.state | cut -d '=' -f 2)" + +[ "$state" = '' ] && printf 'state was not detected. must match ^[0-8]$\n' && exit 1 + +if printf "$state" | grep -vqE '^[0-8]$'; then + printf 'state %s must match ^[0-8]$\n' "$state" + exit 1 +fi + +case "$state" in + 1) printf '%s is already running\n' "$vmname" ;; + 4) printf '%s cannot be started while it is shutting down\n' "$vmname" ;; + [235-7]) virsh start "$vmname" > /dev/null ;; +esac diff --git a/capsulflask/shell_scripts/stop.sh b/capsulflask/shell_scripts/stop.sh new file mode 100755 index 0000000..3cdb2ac --- /dev/null +++ b/capsulflask/shell_scripts/stop.sh @@ -0,0 +1,39 @@ +#!/bin/sh -e +# +# stop.sh - stops a capsul +# if it is already stopped, do nothing + +# State Key +# NOSTATE: 0 - no state +# RUNNING: 1 - the domain is running +# BLOCKED: 2 - the domain is blocked on resource +# PAUSED: 3 - the domain is paused by user +# SHUTDOWN: 4 - the domain is being shut down +# SHUTOFF: 5 - the domain is shut off +# CRASHED: 6 - the domain is crashed +# PMSUSPENDED: 7 - the domain is suspended by guest power management +# 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. + +vmname="$1" + +[ "$vmname" = '' ] && printf 'you must set $vmname\n' && exit 1 + +if printf "$vmname" | grep -vqE '^(cvm|capsul)-[a-z0-9]{10}$'; then + printf 'vmname %s must match ^capsul-[a-z0-9]{10}$\n' "$vmname" + exit 1 +fi + +state="$(virsh domstats $vmname | grep state.state | cut -d '=' -f 2)" + +[ "$state" = '' ] && printf 'state was not detected. must match ^[0-8]$\n' && exit 1 + +if printf "$state" | grep -vqE '^[0-8]$'; then + printf 'state %s must match ^[0-8]$\n' "$state" + exit 1 +fi + +case "$state" in + [1-37]) virsh shutdown "$vmname" > /dev/null ;; + [4-6]) printf '%s is already shutting down\n' "$vmname" ;; +esac +