From 8424abdfa903875ee49f05482a37004096d5692e Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Tue, 18 Jul 2023 17:34:29 -0400 Subject: [PATCH] Switch k8s_snapshot to using arrays. --- .../roles/mrmanager/files/k8s_rollback.bash | 4 ++-- .../roles/mrmanager/files/k8s_snapshot.bash | 24 ++++++++++++++++--- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/ansible/roles/mrmanager/files/k8s_rollback.bash b/ansible/roles/mrmanager/files/k8s_rollback.bash index 69d9038..d241687 100644 --- a/ansible/roles/mrmanager/files/k8s_rollback.bash +++ b/ansible/roles/mrmanager/files/k8s_rollback.bash @@ -36,7 +36,7 @@ function main { kill_all_bhyve log "Rolling back to snapshot $1" while read target; do - zfs rollback "$target" + doas zfs rollback "$target" done<<<"$rollback_targets" launch_all_bhyve log "Done." @@ -49,7 +49,7 @@ function kill_all_bhyve { function launch_all_bhyve { log "Launching all virtual machines." - for vm in $VMS[@]; do tmux new -d -s $vm doas bhyve_netgraph_bridge start $vm zdata/vm/$vm /vm/$vm; sleep 5; done + 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 sanity_check_targets { diff --git a/ansible/roles/mrmanager/files/k8s_snapshot.bash b/ansible/roles/mrmanager/files/k8s_snapshot.bash index 2197fea..0055508 100644 --- a/ansible/roles/mrmanager/files/k8s_snapshot.bash +++ b/ansible/roles/mrmanager/files/k8s_snapshot.bash @@ -5,6 +5,9 @@ set -euo pipefail IFS=$'\n\t' DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +: ${VMS:="poudriere controller0 controller1 controller2 worker0 worker1 worker2"} +: ${WORKERS:="controller0 controller1 controller2 worker0 worker1 worker2"} + ############## Setup ######################### function die { @@ -21,10 +24,12 @@ function log { ############## Program ######################### function main { - snapshot_name="$1" + convert_to_array 'VMS' + convert_to_array 'WORKERS' + local snapshot_name="$1" kill_all_bhyve log "Taking snapshot $1" - for worker in worker0 worker1 worker2 controller0 controller1 controller2; do + for worker in "${WORKERS[@]}"; do doas zfs snapshot -r zdata/vm/${worker}/disk0@${snapshot_name} done doas zfs snapshot zdata/k8spersistent@${snapshot_name} @@ -39,7 +44,20 @@ function kill_all_bhyve { function launch_all_bhyve { log "Launching all virtual machines." - for vm in poudriere controller0 controller1 controller2 worker0 worker1 worker2; do tmux new -d -s $vm doas bhyve_netgraph_bridge start $vm zdata/vm/$vm /vm/$vm; sleep 5; done + 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 "$@"