Newer Older
f16725e 3 months ago History
129 lines | 4.511kb
Bogdan Timofte authored 3 months ago
1
#!/usr/bin/env bash
2
# deploy_tb.sh — Thunderbolt bridge deploy (Bash 3 compatible)
3

            
4
set -eo pipefail
5

            
6
# ---------- EDIT THESE ----------
7
get_mgmt_ip() {
8
  case "$1" in
9
    baobab) echo "192.168.2.91" ;;
10
    ebony)  echo "192.168.2.92" ;;
11
    tapia)  echo "192.168.2.93" ;;
12
    *)      echo "" ;;
13
  esac
14
}
15
get_tb_ip() {
16
  case "$1" in
17
    baobab) echo "192.168.10.91" ;;
18
    ebony)  echo "192.168.10.92" ;;
19
    tapia)  echo "192.168.10.93" ;;
20
    *)      echo "" ;;
21
  esac
22
}
23
# --------------------------------
24

            
25
TARGETS=("$@")
26
if [ ${#TARGETS[@]} -eq 0 ]; then
27
  TARGETS=(baobab ebony tapia)
28
fi
29

            
30
SSH_USER="root"
31
SSH_OPTS="-o BatchMode=yes -o ConnectTimeout=5 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
32
BASE_DIR="$(pwd)"
33

            
34
COMMON_UDEV="$BASE_DIR/common/udev/rules.d/90-thunderbolt-net-systemd.rules"
35
COMMON_SVC1="$BASE_DIR/common/systemd/system/tb-enlist@.service"
36
COMMON_SVC2="$BASE_DIR/common/systemd/system/tb-bridge.service"
37
COMMON_SVC3="$BASE_DIR/common/systemd/system/tb-recover.service"
38
COMMON_TMR1="$BASE_DIR/common/systemd/system/tb-recover.timer"
39
COMMON_BIN1="$BASE_DIR/common/sbin/tb-recover.sh"
40

            
41
require() {
42
  for f in "$@"; do
43
    [ -f "$f" ] || { echo "Missing required file: $f" >&2; exit 1; }
44
  done
45
}
46

            
47
# try mgmt IP first, then TB IP; print chosen IP and return 0 if SSH works
48
pick_ip() {
49
  local host="$1" ip=""
50
  ip="$(get_mgmt_ip "$host")"
51
  if [ -n "$ip" ] && ssh $SSH_OPTS -q "${SSH_USER}@${ip}" true 2>/dev/null; then
52
    echo "$ip"; return 0
53
  fi
54
  ip="$(get_tb_ip "$host")"
55
  if [ -n "$ip" ] && ssh $SSH_OPTS -q "${SSH_USER}@${ip}" true 2>/dev/null; then
56
    echo "$ip"; return 0
57
  fi
58
  # fall back to mgmt for error messaging
59
  ip="$(get_mgmt_ip "$host")"
60
  [ -n "$ip" ] && echo "$ip"
61
  return 1
62
}
63

            
64
deploy_node() {
65
  local host="$1"
66
  local node_dir="$BASE_DIR/$host"
67
  [ -d "$node_dir" ] || { echo "No node directory: $node_dir" >&2; exit 1; }
68

            
69
  local ip
70
  ip="$(pick_ip "$host")" || {
71
    echo "!! [$host] SSH not reachable on $(get_mgmt_ip "$host") or $(get_tb_ip "$host")). Fix IPs or firewall." >&2
72
    exit 1
73
  }
74

            
75
  echo "==> [$host@$ip] prepare remote dirs"
76
  ssh $SSH_OPTS "${SSH_USER}@${ip}" "mkdir -p /etc/udev/rules.d /etc/systemd/system /etc/network/interfaces.d /usr/local/sbin"
77

            
78
  echo "==> [$host@$ip] copy COMMON files"
79
  scp -q "$COMMON_UDEV" "${SSH_USER}@${ip}:/etc/udev/rules.d/90-thunderbolt-net-systemd.rules"
80
  scp -q "$COMMON_SVC1" "${SSH_USER}@${ip}:/etc/systemd/system/tb-enlist@.service"
81
  scp -q "$COMMON_SVC2" "${SSH_USER}@${ip}:/etc/systemd/system/tb-bridge.service"
82
  scp -q "$COMMON_SVC3" "${SSH_USER}@${ip}:/etc/systemd/system/tb-recover.service"
83
  scp -q "$COMMON_TMR1" "${SSH_USER}@${ip}:/etc/systemd/system/tb-recover.timer"
84
  scp -q "$COMMON_BIN1" "${SSH_USER}@${ip}:/usr/local/sbin/tb-recover.sh"
85

            
86
  echo "==> [$host@$ip] copy NODE config"
87
  require "$node_dir/etc/network/interfaces" "$node_dir/etc/network/interfaces.d/10-thunderbolt"
88
  scp -q "$node_dir/etc/network/interfaces" "${SSH_USER}@${ip}:/etc/network/interfaces"
89
  scp -q "$node_dir/etc/network/interfaces.d/10-thunderbolt" "${SSH_USER}@${ip}:/etc/network/interfaces.d/10-thunderbolt"
90

            
91
  echo "==> [$host@$ip] enable + reload"
92
  ssh $SSH_OPTS "${SSH_USER}@${ip}" bash -s <<'EOF'
93
set -e
94
chmod 0644 /etc/udev/rules.d/90-thunderbolt-net-systemd.rules
95
chmod 0644 /etc/systemd/system/tb-enlist@.service
96
chmod 0644 /etc/systemd/system/tb-bridge.service
97
chmod 0644 /etc/systemd/system/tb-recover.service
98
chmod 0644 /etc/systemd/system/tb-recover.timer
99
chmod 0755 /usr/local/sbin/tb-recover.sh
100
systemctl daemon-reload
101
udevadm control --reload
102
command -v ifreload >/dev/null 2>&1 && ifreload -a || true
103
systemctl enable --now tb-bridge.service
104
systemctl enable --now tb-recover.timer
105
systemctl start tb-recover.service
106
udevadm trigger --subsystem-match=net --action=add
107
EOF
108

            
109
  echo "==> [$host@$ip] status"
110
  ssh $SSH_OPTS "${SSH_USER}@${ip}" bash -s <<'EOF'
111
set -e
112
systemctl --no-pager --plain --full status tb-bridge.service | sed -n '1,6p'
113
systemctl --no-pager --plain --full status tb-recover.timer | sed -n '1,8p'
114
systemctl --no-pager --plain --full list-units 'tb-enlist@*.service' | sed -n '1,12p' || true
115
ip -d link show thunderbridge | sed -n '1,3p'
116
bridge link | grep -E 'thunderbolt|thunderbridge' || true
117
EOF
118

            
119
  echo "==> [$host@$ip] done."
120
  echo
121
}
122

            
123
require "$COMMON_UDEV" "$COMMON_SVC1" "$COMMON_SVC2" "$COMMON_SVC3" "$COMMON_TMR1" "$COMMON_BIN1"
124

            
125
for h in "${TARGETS[@]}"; do
126
  deploy_node "$h"
127
done
128

            
129
echo "All done. Go poke the cables and watch systemd behave."