aboutsummaryrefslogtreecommitdiffstats
path: root/bin/switch_wfs_os
blob: c24382649814ffc9550f74bf5b81f21f21a01140 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env bash

set -euo pipefail
IFS=$'\n\t'

os_new="arch-rt"
os_old="ubuntu-studio"
os_current=""
os_set=""

syslinux_location_arch="/boot/syslinux/syslinux.cfg"
syslinux_location_ubuntu="/mnt/boot/syslinux/syslinux.cfg"
syslinux_location=""

reboot_cmd_arch="sudo systemctl reboot"
reboot_cmd_ubuntu="/usr/bin/sudo /sbin/reboot -q"
reboot_cmd=""

hostnames_arch=( "wfs-master" "wfs-node1" "wfs-node2" )
hostnames_ubuntu=( "wfs" "n101" "n102" )
host=$(hostname)

if [ $UID -ne 0 ]; then
  echo "You need to be root or call this script with sudo to do this."
  exit 1
fi

echo "Host: $host."

if [ -f /etc/arch-release ]; then
  os_current=$os_new
  syslinux_location=$syslinux_location_arch
  reboot_cmd=$reboot_cmd_arch
else
  os_current=$os_old
  syslinux_location=$syslinux_location_ubuntu
  reboot_cmd=$reboot_cmd_ubuntu
fi
echo "Currently running: $os_current"

if [ -f "${syslinux_location}" ]; then
  os_set=$(sed -ne '/^DEFAULT/p' $syslinux_location| cut -d' ' -f2)
  echo "Currently set up for next boot: $os_set"
else
  echo "Error: Syslinux configuration does not exist ($syslinux_location)"
  exit 1
fi

if [ "$os_set" == "${os_new}" ] && [ "$os_current" == "${os_new}" ]; then
  if [[ " ${hostnames_arch[@]} " =~ " $host " ]]; then
    echo "Switching to: $os_old"
    sed -i 's/^DEFAULT .*/DEFAULT '${os_old}'/' $syslinux_location
    if [[ "${hostnames_arch[0]}" == "$host" ]]; then
      echo "Calling script on nodes:"
      set +e
      ssh wfs@${hostnames_arch[1]} "/usr/bin/sudo bin/$(basename $0)"
      ssh wfs@${hostnames_arch[2]} "/usr/bin/sudo bin/$(basename $0)"
      set -e
    fi
  else
    echo "Error: Script is not supposed to be running on this host!"
    echo "$(hostname) is not one of the following: ${hostnames_arch[@]}"
    echo "Exiting."
    exit 1
  fi
elif [ "$os_set" == "${os_old}" ] && [ "$os_current" == "${os_old}" ]; then
  if [[ " ${hostnames_ubuntu[@]} " =~ " $host " ]]; then
    echo "Switching to: $os_new"
    sed -i 's/^DEFAULT .*/DEFAULT '${os_new}'/' $syslinux_location
    if [[ "${hostnames_ubuntu[0]}" == "$host" ]]; then
      echo "Calling script on nodes:"
      ssh wfs@${hostnames_ubuntu[1]} "/usr/bin/sudo bin/$(basename $0)"
      ssh wfs@${hostnames_ubuntu[2]} "/usr/bin/sudo bin/$(basename $0)"
    fi
  else
    echo "Error: Script is not supposed to be running on this host!"
    echo "$host is not one of the following: ${hostnames_ubuntu[@]}"
    echo "Exiting."
    exit 1
  fi
else
  echo "Error: Current OS ($os_current) and OS to be booted ($os_set) are not the same."
  echo "Fix this manually in $syslinux_location or just reboot (into the other OS)."
  echo "Exiting."
  exit 1
fi


echo "Rebooting."
eval $reboot_cmd
exit 0