-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathvmsshall.sh
executable file
·62 lines (49 loc) · 1.65 KB
/
vmsshall.sh
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
#!/usr/bin/env bash
# This script launches a multi-window, multi-pane tmux session and connects to all the VMs with SSH.
set -xe
dir=$(dirname "$0")
# Grab the helpers
source "$dir/helpers.sh"
# The script takes tmux session name as an argument
sname=$1
if [[ -z $sname ]]; then
echo "usage: $(basename "$0") <tmux-session-name>"
exit 1
fi
# Wait for VMs to start up and expose their SSH port. Scan their keys and add them to known_hosts.
for vmid in $(seq 0 6); do
"$dir/vmsshsetup.sh" $vmid
done
# A function that prepares a pane for connecting to a VM with SSH, and connects to it.
# Takes VM ID as an argument.
vm_ssh() {
local vmid=$1
local vmname=$(id_to_name "$vmid")
tmux send-keys -t "$sname" "ssh ubuntu@$vmname" C-m
}
# Launch a new session with initial window named "ssh-gateway"
tmux new-session -s "$sname" -n ssh-gateway -d
# Connect to `gateway` VM
vm_ssh 0
ssh_window() {
local window_name=$1
local first_vmid=$2
local last_vmid=$3
local layout=$4
tmux new-window -t "$sname" -n "$window_name"
for vmid in $(seq "$first_vmid" "$last_vmid"); do
vm_ssh "$vmid"
if [[ "$vmid" != "$last_vmid" ]]; then
tmux split-window -t "$sname" -v
fi
tmux select-layout -t "$sname" "$layout"
done
}
# Create a window with SSH connections to `control` VMs
ssh_window ssh-controls 1 3 even-vertical
# Create a window with SSH connections to `worker` VMs
ssh_window ssh-workers 4 6 even-vertical
# Create a window with SSH connections to both `control` and `worker` VMs
ssh_window ssh-nodes 1 6 tiled
# Finally, attach the session back to the current terminal and activate the second window
tmux attach -t "$sname:ssh-controls"