Created
April 4, 2023 15:06
-
-
Save eliroca/8e24eda44361870fea688fb6853c8672 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -euo pipefail | |
RED='\033[0;31m' | |
GREEN='\033[0;32m' | |
NC='\033[0m' # No Color | |
verbose_mode=false | |
test_mode=false | |
# set for testing the script | |
#workers=(worker-foo{1..3} worker-ovce0{1..3}) | |
workers=(worker-arm0{1..2} worker-ovce0{1..9} worker-ovce{10..20}) | |
workers_unreachable='' | |
workers_failed='' | |
function usage { | |
cat <<EOF | |
Usage: $(basename "$0") [-v] [-t] | |
-v verbose mode prints everything | |
-t test mode prints efibootmgr commands if changes are needed | |
EOF | |
} | |
function print_error { | |
echo -e "${RED}Error: $*${NC}" >&2 | |
} | |
function print_success { | |
echo -e "${GREEN}$*${NC}" | |
} | |
while getopts ':vth' opt; do | |
case "$opt" in | |
h) | |
usage | |
exit 0 | |
;; | |
v) | |
verbose_mode=true | |
;; | |
t) | |
test_mode=true | |
;; | |
\?) | |
print_error "invalid option -$OPTARG" | |
usage | |
exit 1 | |
;; | |
esac | |
done | |
shift "$((OPTIND - 1))" | |
if $test_mode && $verbose_mode; then | |
echo "Running in test mode: not changing boot order" | |
fi | |
for worker in "${workers[@]}"; do | |
if ! ping -c 1 -W 1 "$worker" >/dev/null 2>&1; then | |
[[ $verbose_mode = true ]] && print_error "could not reach $worker" | |
workers_unreachable+="$worker " | |
continue | |
fi | |
boot_config=$(ssh "$worker" 'efibootmgr') | |
boot_order=$(echo "$boot_config" | grep -Po 'BootOrder: \K(.*)') | |
boot_ipv4=$(echo "$boot_config" | sed -n "s/Boot\(000[0-9]\).*IPv4.*/\1,/p") | |
boot_ipv6=$(echo "$boot_config" | sed -n "s/Boot\(000[0-9]\).*IPv6.*/\1,/p") | |
boot_shell=$(echo "$boot_config" | sed -n "s/Boot\(000[0-9]\).*EFI Shell.*/\1,/p") | |
new_boot_order=$(echo "$boot_ipv4 $boot_ipv6 $boot_shell" | tr -d '\n ') | |
new_boot_order=${new_boot_order::-1} | |
if $verbose_mode || ($verbose_mode && $test_mode); then | |
echo -e "${GREEN}Running efibootmgr on $worker...${NC}" | |
fi | |
[[ $verbose_mode = true ]] && echo "$boot_config" | |
if [[ "$new_boot_order" == "$boot_order" ]]; then | |
[[ $verbose_mode = true ]] && print_success "No need to update boot order on $worker" | |
continue | |
fi | |
set_efibootmgr="ssh $worker 'efibootmgr -o $new_boot_order'" | |
if $test_mode; then | |
[[ $verbose_mode = true ]] && set_efibootmgr="Command would be: $set_efibootmgr" | |
echo "$set_efibootmgr" | |
else | |
[[ $verbose_mode = true ]] && echo "Command used: $set_efibootmgr" | |
if eval "$set_efibootmgr" >/dev/null; then | |
print_success "Boot order updated successfully on $worker" | |
else | |
workers_failed+="$worker " | |
print_error "failed to update boot order on $worker" | |
fi | |
fi | |
done | |
if [[ -n "$workers_failed" ]]; then | |
print_error "failed boot order update: $workers_failed" | |
fi | |
if [[ -n "$workers_unreachable" ]]; then | |
print_error "could not reach: $workers_unreachable" | |
fi | |
([[ -n "$workers_failed" ]] || [[ -n "$workers_unreachable" ]]) && exit 1 | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment