Newer Older
127 lines | 4.34kb
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_BIN1="$BASE_DIR/common/sbin/tb-recover.sh"
39

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

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

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

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

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

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

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

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

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

            
117
  echo "==> [$host@$ip] done."
118
  echo
119
}
120

            
Bogdan Timofte authored 2 weeks ago
121
require "$COMMON_UDEV" "$COMMON_SVC1" "$COMMON_SVC2" "$COMMON_SVC3" "$COMMON_BIN1"
Bogdan Timofte authored 3 months ago
122

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

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