···11+#!/bin/sh -e
22+#
33+# force-stop.sh - pull the plug on a capsul
44+# if it is already stopped, do nothing
55+66+# State Key
77+# NOSTATE: 0 - no state
88+# RUNNING: 1 - the domain is running
99+# BLOCKED: 2 - the domain is blocked on resource
1010+# PAUSED: 3 - the domain is paused by user
1111+# SHUTDOWN: 4 - the domain is being shut down
1212+# SHUTOFF: 5 - the domain is shut off
1313+# CRASHED: 6 - the domain is crashed
1414+# PMSUSPENDED: 7 - the domain is suspended by guest power management
1515+# 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.
1616+1717+vmname="$1"
1818+1919+[ "$vmname" = '' ] && printf 'you must set $vmname\n' && exit 1
2020+2121+if printf "$vmname" | grep -vqE '^(cvm|capsul)-[a-z0-9]{10}$'; then
2222+ printf 'vmname %s must match ^capsul-[a-z0-9]{10}$\n' "$vmname"
2323+ exit 1
2424+fi
2525+2626+state="$(virsh domstats $vmname | grep state.state | cut -d '=' -f 2)"
2727+2828+[ "$state" = '' ] && printf 'state was not detected. must match ^[0-8]$\n' && exit 1
2929+3030+if printf "$state" | grep -vqE '^[0-8]$'; then
3131+ printf 'state %s must match ^[0-8]$\n' "$state"
3232+ exit 1
3333+fi
3434+3535+case "$state" in
3636+ [1-47]) virsh destroy "$vmname" > /dev/null ;; # virsh destroy == pull the plug
3737+ [5-6]) printf "%s is already off\n" "$vmname" ;;
3838+esac
3939+
+43
capsulflask/shell_scripts/restart.sh
···11+#!/bin/sh -e
22+#
33+# restart.sh - restarts a capsul
44+# if it is stopping, do nothing
55+66+# State Key
77+# NOSTATE: 0 - no state
88+# RUNNING: 1 - the domain is running
99+# BLOCKED: 2 - the domain is blocked on resource
1010+# PAUSED: 3 - the domain is paused by user
1111+# SHUTDOWN: 4 - the domain is being shut down
1212+# SHUTOFF: 5 - the domain is shut off
1313+# CRASHED: 6 - the domain is crashed
1414+# PMSUSPENDED: 7 - the domain is suspended by guest power management
1515+# 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.
1616+1717+vmname="$1"
1818+1919+[ "$vmname" = '' ] && printf 'you must set $vmname\n' && exit 1
2020+2121+if printf "$vmname" | grep -vqE '^(cvm|capsul)-[a-z0-9]{10}$'; then
2222+ printf 'vmname %s must match ^capsul-[a-z0-9]{10}$\n' "$vmname"
2323+ exit 1
2424+fi
2525+2626+state="$(virsh domstats $vmname | grep state.state | cut -d '=' -f 2)"
2727+2828+[ "$state" = '' ] && printf 'state was not detected. must match ^[0-8]$\n' && exit 1
2929+3030+if printf "$state" | grep -vqE '^[0-8]$'; then
3131+ printf 'state %s must match ^[0-8]$\n' "$state"
3232+ exit 1
3333+fi
3434+3535+case "$state" in
3636+ 1) virsh reboot "$vmname" > /dev/null ;;
3737+ 2) printf '%s cannot be rebooted while it is blocked\n' "$vmname" ;;
3838+ 37) printf '%s cannot be rebooted while it is paused\n' "$vmname" ;;
3939+ 4) printf '%s cannot be rebooted while it is shutting down\n' "$vmname" ;;
4040+ 56) printf '%s cannot be rebooted while it is stopped\n' "$vmname" ;;
4141+ *) printf 'unknown script behavior. state: %s\n' "$state" ;;
4242+esac
4343+
+40
capsulflask/shell_scripts/start.sh
···11+#!/bin/sh -e
22+#
33+# start.sh - starts a capsul
44+# if it is already started, do nothing
55+# if it is stopping, do nothing
66+77+# State Key
88+# NOSTATE: 0 - no state
99+# RUNNING: 1 - the domain is running
1010+# BLOCKED: 2 - the domain is blocked on resource
1111+# PAUSED: 3 - the domain is paused by user
1212+# SHUTDOWN: 4 - the domain is being shut down
1313+# SHUTOFF: 5 - the domain is shut off
1414+# CRASHED: 6 - the domain is crashed
1515+# PMSUSPENDED: 7 - the domain is suspended by guest power management
1616+# 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.
1717+1818+vmname="$1"
1919+2020+[ "$vmname" = '' ] && printf 'you must set $vmname\n' && exit 1
2121+2222+if printf "$vmname" | grep -vqE '^(cvm|capsul)-[a-z0-9]{10}$'; then
2323+ printf 'vmname %s must match ^capsul-[a-z0-9]{10}$\n' "$vmname"
2424+ exit 1
2525+fi
2626+2727+state="$(virsh domstats $vmname | grep state.state | cut -d '=' -f 2)"
2828+2929+[ "$state" = '' ] && printf 'state was not detected. must match ^[0-8]$\n' && exit 1
3030+3131+if printf "$state" | grep -vqE '^[0-8]$'; then
3232+ printf 'state %s must match ^[0-8]$\n' "$state"
3333+ exit 1
3434+fi
3535+3636+case "$state" in
3737+ 1) printf '%s is already running\n' "$vmname" ;;
3838+ 4) printf '%s cannot be started while it is shutting down\n' "$vmname" ;;
3939+ [235-7]) virsh start "$vmname" > /dev/null ;;
4040+esac
+39
capsulflask/shell_scripts/stop.sh
···11+#!/bin/sh -e
22+#
33+# stop.sh - stops a capsul
44+# if it is already stopped, do nothing
55+66+# State Key
77+# NOSTATE: 0 - no state
88+# RUNNING: 1 - the domain is running
99+# BLOCKED: 2 - the domain is blocked on resource
1010+# PAUSED: 3 - the domain is paused by user
1111+# SHUTDOWN: 4 - the domain is being shut down
1212+# SHUTOFF: 5 - the domain is shut off
1313+# CRASHED: 6 - the domain is crashed
1414+# PMSUSPENDED: 7 - the domain is suspended by guest power management
1515+# 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.
1616+1717+vmname="$1"
1818+1919+[ "$vmname" = '' ] && printf 'you must set $vmname\n' && exit 1
2020+2121+if printf "$vmname" | grep -vqE '^(cvm|capsul)-[a-z0-9]{10}$'; then
2222+ printf 'vmname %s must match ^capsul-[a-z0-9]{10}$\n' "$vmname"
2323+ exit 1
2424+fi
2525+2626+state="$(virsh domstats $vmname | grep state.state | cut -d '=' -f 2)"
2727+2828+[ "$state" = '' ] && printf 'state was not detected. must match ^[0-8]$\n' && exit 1
2929+3030+if printf "$state" | grep -vqE '^[0-8]$'; then
3131+ printf 'state %s must match ^[0-8]$\n' "$state"
3232+ exit 1
3333+fi
3434+3535+case "$state" in
3636+ [1-37]) virsh shutdown "$vmname" > /dev/null ;;
3737+ [4-6]) printf '%s is already shutting down\n' "$vmname" ;;
3838+esac
3939+