machine_setup/ansible/roles/mrmanager/files/k8s_snapshot.bash

64 lines
1.9 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
#
# Kill the virtual machines, take a snapshot, and start the virtual machines up again.
set -euo pipefail
IFS=$'\n\t'
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
2023-07-18 21:34:29 +00:00
: ${VMS:="poudriere controller0 controller1 controller2 worker0 worker1 worker2"}
: ${WORKERS:="controller0 controller1 controller2 worker0 worker1 worker2"}
############## Setup #########################
function die {
local status_code="$1"
shift
(>&2 echo "${@}")
exit "$status_code"
}
function log {
(>&2 echo "${@}")
}
############## Program #########################
function main {
2023-07-18 21:34:29 +00:00
convert_to_array 'VMS'
convert_to_array 'WORKERS'
local snapshot_name="$1"
kill_all_bhyve
log "Taking snapshot $1"
2023-07-18 21:34:29 +00:00
for worker in "${WORKERS[@]}"; do
doas zfs snapshot -r zdata/vm/${worker}/disk0@${snapshot_name}
done
doas zfs snapshot zdata/k8spersistent@${snapshot_name}
launch_all_bhyve
log "Done."
}
function kill_all_bhyve {
log "Killing all virtual machines."
doas killall bhyve; while true; do tmux ls || break; sleep 2; done;
}
function launch_all_bhyve {
log "Launching all virtual machines."
2023-07-18 21:34:29 +00:00
for vm in "${VMS[@]}"; do log "starting $vm"; tmux new -d -s $vm doas bhyve_netgraph_bridge start $vm zdata/vm/$vm /vm/$vm; sleep 5; done
}
function convert_to_array {
# Converts a string separated by a value into an array. An example use-case would be for parsing environment variables like $PATH.
#
# WARNING: This uses nameref to modify the env variable at the first parameter, making this function very much not "functional". It does not return anything on stdout so it cannot be used with $().
local -n string_to_convert="$1"
local ifs_in_string="${2:- }"
local current_ifs="$IFS"
IFS="$ifs_in_string"
string_to_convert=($string_to_convert)
# Restore IFS to the original value in case this function is not run inside a subshell
IFS="$current_ifs"
}
main "$@"