-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmart-handler.sh
More file actions
executable file
·204 lines (171 loc) · 5.3 KB
/
smart-handler.sh
File metadata and controls
executable file
·204 lines (171 loc) · 5.3 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env bash
set -Eeuo pipefail
IFS=$'\n\t'
usage() {
cat <<'USAGE'
Usage:
./smart-handler.sh --handler <ssh-target> [options]
Description:
Synchronize the Lilith repository to a remote handler machine and start
the benchmark workflow in a detached tmux session.
Required:
-H, --handler <HOST> SSH target used as the handler machine.
It can be a Host alias configured in your SSH client,
user@host, or user@ip.
Options:
-d, --remote-dir <DIR> Remote directory where Lilith is synchronized.
Default: lilith
-s, --session <NAME> tmux session name.
Default: benchmark_session
-c, --command <COMMAND> Command executed inside the tmux session.
Default: ./multi-run.sh
--install-deps Install minimal handler dependencies using apt:
tmux, rsync, openssh-client.
Requires sudo on the handler.
-h, --help Show this help message.
Examples:
./smart-handler.sh --handler user@handler.example.com
./smart-handler.sh \
--handler my-handler \
--remote-dir lilith \
--session lilith_benchmark \
--command './multi-run.sh'
Notes:
This script intentionally does NOT copy local SSH credentials and does NOT modify
the handler system SSH daemon configuration. The handler must already be reachable via SSH.
USAGE
}
handler="${LILITH_HANDLER:-}"
remote_dir="${LILITH_REMOTE_DIR:-lilith}"
session_name="${LILITH_SESSION:-benchmark_session}"
remote_command="${LILITH_COMMAND:-./multi-run.sh}"
install_deps=0
while [[ $# -gt 0 ]]; do
case "$1" in
-H|--handler)
handler="${2:-}"
shift 2
;;
-d|--remote-dir)
remote_dir="${2:-}"
shift 2
;;
-s|--session)
session_name="${2:-}"
shift 2
;;
-c|--command)
remote_command="${2:-}"
shift 2
;;
--install-deps)
install_deps=1
shift
;;
-h|--help)
usage
exit 0
;;
*)
echo "Unknown argument: $1" >&2
usage
exit 1
;;
esac
done
if [[ -z "$handler" ]]; then
echo "Error: missing handler." >&2
echo "Pass it with --handler <ssh-target> or set LILITH_HANDLER." >&2
exit 1
fi
if [[ -z "$remote_dir" ]]; then
echo "Error: remote directory cannot be empty." >&2
exit 1
fi
if [[ -z "$session_name" ]]; then
echo "Error: tmux session name cannot be empty." >&2
exit 1
fi
if [[ -z "$remote_command" ]]; then
echo "Error: remote command cannot be empty." >&2
exit 1
fi
required_local_items=(
"kollaps"
"misc"
"scripts"
"run.sh"
"multi-run.sh"
"machines.txt"
)
for item in "${required_local_items[@]}"; do
if [[ ! -e "$item" ]]; then
echo "Error: required local item not found: $item" >&2
if [[ "$item" == "machines.txt" ]]; then
echo "Create it from machines.example.txt and fill it with your cluster SSH targets." >&2
fi
exit 1
fi
done
if ! command -v rsync >/dev/null 2>&1; then
echo "Error: rsync is not installed locally." >&2
exit 1
fi
if ! command -v ssh >/dev/null 2>&1; then
echo "Error: ssh is not installed locally." >&2
exit 1
fi
mkdir -p misc/logs/ssh
safe_handler_name="$(printf '%s' "$handler" | tr -c 'A-Za-z0-9_.-' '_')"
ssh_log_path="misc/logs/ssh/ssh_${safe_handler_name}.log"
remote() {
local command="$1"
ssh \
-C \
-T \
-o LogLevel=ERROR \
-E "$ssh_log_path" \
"$handler" \
"$command"
}
echo "Checking SSH connectivity to handler: $handler"
remote "echo 'Connected to handler: '\"\$(hostname)\""
if [[ "$install_deps" -eq 1 ]]; then
echo "Installing minimal dependencies on handler..."
remote "sudo apt-get update -y && sudo apt-get install -y tmux rsync openssh-client"
fi
echo "Checking handler dependencies..."
remote "command -v tmux >/dev/null 2>&1 || { echo 'Error: tmux is not installed on the handler. Re-run with --install-deps or install tmux manually.' >&2; exit 1; }"
remote "command -v rsync >/dev/null 2>&1 || { echo 'Error: rsync is not installed on the handler. Re-run with --install-deps or install rsync manually.' >&2; exit 1; }"
echo "Creating remote directory: $remote_dir"
remote "mkdir -p '$remote_dir'"
echo "Synchronizing Lilith repository to handler..."
rsync -avzh --delete \
--exclude='.git/' \
--exclude='.vscode/' \
--exclude='.idea/' \
--exclude='venv/' \
--exclude='.venv/' \
--exclude='results/' \
--exclude='temp/' \
--exclude='misc/logs/' \
--exclude='tmux-session.log' \
--exclude='struttura.txt' \
--exclude='machines.local.backup' \
--exclude='*.pem' \
--exclude='*.key' \
--exclude='id_ed25519' \
--exclude='id_ed25519.pub' \
./ "$handler:$remote_dir/"
echo "Starting tmux session: $session_name"
remote "cd '$remote_dir' && tmux kill-session -t '$session_name' >/dev/null 2>&1 || true"
remote "cd '$remote_dir' && tmux new-session -d -s '$session_name'"
remote "tmux pipe-pane -t '$session_name' -o 'exec cat > $remote_dir/tmux-session.log'"
remote "tmux send-keys -t '$session_name' 'cd $remote_dir && $remote_command' Enter"
echo "Lilith workflow started on handler."
echo ""
echo "Attach to the remote session with:"
echo " ssh -t $handler 'tmux attach -t $session_name'"
echo ""
echo "Remote log:"
echo " $remote_dir/tmux-session.log"