|
Bogdan Timofte
authored
3 months ago
|
1
|
#!/bin/bash
|
|
|
2
|
|
|
|
3
|
# AutoNAS Remote Deploy Script
|
|
|
4
|
# Instalează și gestionează AutoNAS pe serverele remote 192.168.2.91-93
|
|
|
5
|
|
|
|
6
|
set -e # Exit on any error
|
|
|
7
|
|
|
|
8
|
|
|
|
9
|
# Configurații
|
|
|
10
|
SSH_USER="root"
|
|
|
11
|
REMOTE_DIR="/tmp/autonas-deploy"
|
|
|
12
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
|
13
|
|
|
|
14
|
# Parse cluster.json for nodes
|
|
|
15
|
parse_cluster_nodes() {
|
|
|
16
|
local json_file="$SCRIPT_DIR/cluster.json"
|
|
|
17
|
if ! [ -f "$json_file" ]; then
|
|
|
18
|
log_error "cluster.json not found in $SCRIPT_DIR"
|
|
|
19
|
exit 1
|
|
|
20
|
fi
|
|
|
21
|
jq -c '.cluster.nodes[]' "$json_file" 2>/dev/null
|
|
|
22
|
}
|
|
|
23
|
|
|
|
24
|
# Build arrays of IPs and hostnames
|
|
|
25
|
get_targets() {
|
|
|
26
|
local nodes_json
|
|
|
27
|
nodes_json=$(parse_cluster_nodes)
|
|
|
28
|
local ips=()
|
|
|
29
|
local names=()
|
|
|
30
|
while IFS= read -r node; do
|
|
|
31
|
local ip name
|
|
|
32
|
ip=$(echo "$node" | jq -r '.ip')
|
|
|
33
|
name=$(echo "$node" | jq -r '.hostname')
|
|
|
34
|
ips+=("$ip")
|
|
|
35
|
names+=("$name")
|
|
|
36
|
done <<< "$nodes_json"
|
|
|
37
|
echo "${ips[@]}"
|
|
|
38
|
}
|
|
|
39
|
|
|
|
40
|
# Get node name by IP
|
|
|
41
|
get_node_name() {
|
|
|
42
|
local search_ip="$1"
|
|
|
43
|
local nodes_json
|
|
|
44
|
nodes_json=$(parse_cluster_nodes)
|
|
|
45
|
while IFS= read -r node; do
|
|
|
46
|
local ip name
|
|
|
47
|
ip=$(echo "$node" | jq -r '.ip')
|
|
|
48
|
name=$(echo "$node" | jq -r '.hostname')
|
|
|
49
|
if [[ "$ip" == "$search_ip" ]]; then
|
|
|
50
|
echo "$name"
|
|
|
51
|
return
|
|
|
52
|
fi
|
|
|
53
|
done <<< "$nodes_json"
|
|
|
54
|
echo "$search_ip"
|
|
|
55
|
}
|
|
|
56
|
|
|
|
57
|
# Culori pentru output
|
|
|
58
|
RED='\033[0;31m'
|
|
|
59
|
GREEN='\033[0;32m'
|
|
|
60
|
YELLOW='\033[1;33m'
|
|
|
61
|
BLUE='\033[0;34m'
|
|
|
62
|
NC='\033[0m' # No Color
|
|
|
63
|
|
|
|
64
|
# Funcții helper
|
|
|
65
|
log_info() {
|
|
|
66
|
echo -e "${BLUE}[INFO]${NC} $1"
|
|
|
67
|
}
|
|
|
68
|
|
|
|
69
|
log_success() {
|
|
|
70
|
echo -e "${GREEN}[SUCCESS]${NC} $1"
|
|
|
71
|
}
|
|
|
72
|
|
|
|
73
|
log_warning() {
|
|
|
74
|
echo -e "${YELLOW}[WARNING]${NC} $1"
|
|
|
75
|
}
|
|
|
76
|
|
|
|
77
|
log_error() {
|
|
|
78
|
echo -e "${RED}[ERROR]${NC} $1"
|
|
|
79
|
}
|
|
|
80
|
|
|
|
81
|
print_banner() {
|
|
|
82
|
echo "======================================"
|
|
|
83
|
echo " AutoNAS Remote Deploy Script"
|
|
|
84
|
echo "======================================"
|
|
|
85
|
echo ""
|
|
|
86
|
}
|
|
|
87
|
|
|
|
88
|
# Funcție pentru verificarea dacă hostul este up
|
|
|
89
|
check_host_up() {
|
|
|
90
|
local target="$1"
|
|
|
91
|
log_info "Verificare dacă hostul $target este accesibil..."
|
|
|
92
|
|
|
|
93
|
# Pentru simplitate, verificăm direct SSH în loc de ping pe macOS
|
|
|
94
|
if [[ "$OSTYPE" == "darwin"* ]]; then
|
|
|
95
|
# macOS: verificare directă SSH cu timeout rapid
|
|
|
96
|
if ssh -o ConnectTimeout=3 -o BatchMode=yes -o LogLevel=ERROR "$SSH_USER@$target" "echo 'OK'" >/dev/null 2>&1; then
|
|
|
97
|
log_success "Host $target este UP și SSH funcționează"
|
|
|
98
|
return 0
|
|
|
99
|
else
|
|
|
100
|
log_warning "Host $target nu este accesibil prin SSH"
|
|
|
101
|
return 1
|
|
|
102
|
fi
|
|
|
103
|
else
|
|
|
104
|
# Linux: folosim ping normal
|
|
|
105
|
if timeout 5 ping -c 2 -W 3 "$target" >/dev/null 2>&1; then
|
|
|
106
|
log_success "Host $target este UP"
|
|
|
107
|
return 0
|
|
|
108
|
else
|
|
|
109
|
log_warning "Host $target este DOWN sau nu răspunde la ping"
|
|
|
110
|
return 1
|
|
|
111
|
fi
|
|
|
112
|
fi
|
|
|
113
|
}
|
|
|
114
|
|
|
|
115
|
# Funcție pentru verificarea conectivității SSH
|
|
|
116
|
check_ssh_connectivity() {
|
|
|
117
|
local target="$1"
|
|
|
118
|
|
|
|
119
|
# Verifică mai întâi dacă hostul este up
|
|
|
120
|
if ! check_host_up "$target"; then
|
|
|
121
|
log_warning "Skipping SSH test pentru $target - hostul nu este up"
|
|
|
122
|
return 1
|
|
|
123
|
fi
|
|
|
124
|
|
|
|
125
|
log_info "Verificare conectivitate SSH pentru $target..."
|
|
|
126
|
|
|
|
127
|
if ssh -o ConnectTimeout=5 -o BatchMode=yes -o LogLevel=ERROR "$SSH_USER@$target" "echo 'SSH OK'" >/dev/null 2>&1; then
|
|
|
128
|
log_success "SSH conectare OK pentru $target"
|
|
|
129
|
return 0
|
|
|
130
|
else
|
|
|
131
|
log_error "SSH conectare FAILED pentru $target - verifică cheia SSH"
|
|
|
132
|
return 1
|
|
|
133
|
fi
|
|
|
134
|
}
|
|
|
135
|
|
|
|
136
|
# Funcție pentru copierea fișierelor pe ținta remotă
|
|
|
137
|
copy_files() {
|
|
|
138
|
local target="$1"
|
|
|
139
|
log_info "Copierea fișierelor pe $target..."
|
|
|
140
|
|
|
|
141
|
# Creare director temporar pe țintă
|
|
|
142
|
ssh -o LogLevel=ERROR "$SSH_USER@$target" "mkdir -p $REMOTE_DIR"
|
|
|
143
|
|
|
|
144
|
# Copierea tuturor fișierelor proiectului
|
|
|
145
|
rsync -avz --delete --quiet \
|
|
|
146
|
--exclude='.git' \
|
|
|
147
|
--exclude='deploy.sh' \
|
|
|
148
|
--exclude='*.log' \
|
|
|
149
|
"$SCRIPT_DIR/" "$SSH_USER@$target:$REMOTE_DIR/"
|
|
|
150
|
|
|
|
151
|
log_success "Fișiere copiate pe $target"
|
|
|
152
|
}
|
|
|
153
|
|
|
|
154
|
# Funcție generică pentru executarea unei comenzi pe ținta remotă
|
|
|
155
|
run_on_target() {
|
|
|
156
|
local target="$1"
|
|
|
157
|
local action="$2"
|
|
|
158
|
local success_msg="$3"
|
|
|
159
|
local error_msg="$4"
|
|
|
160
|
local cmd_arg="${5:-$action}"
|
|
|
161
|
|
|
|
162
|
log_info "$action pe $target..."
|
|
|
163
|
if ssh -o LogLevel=ERROR "$SSH_USER@$target" "bash $REMOTE_DIR/scripts/install.sh $cmd_arg"; then
|
|
|
164
|
log_success "$success_msg"
|
|
|
165
|
else
|
|
|
166
|
log_error "$error_msg"
|
|
|
167
|
return 1
|
|
|
168
|
fi
|
|
|
169
|
}
|
|
|
170
|
|
|
|
171
|
# Funcție pentru verificarea statusului (nu necesită rsync)
|
|
|
172
|
check_status() {
|
|
|
173
|
local target="$1"
|
|
|
174
|
log_info "Verificarea statusului AutoNAS pe $target..."
|
|
|
175
|
# Încearcă să folosească uninstaller pentru status sau install.sh dacă există
|
|
|
176
|
ssh -o LogLevel=ERROR "$SSH_USER@$target" "
|
|
|
177
|
if [ -f '$REMOTE_DIR/scripts/install.sh' ]; then
|
|
|
178
|
bash $REMOTE_DIR/scripts/install.sh status
|
|
Bogdan Timofte
authored
2 weeks ago
|
179
|
elif [ -f '/usr/local/sbin/autonas' ]; then
|
|
|
180
|
/usr/local/sbin/autonas --version 2>/dev/null || echo 'AutoNAS instalat dar versiune necunoscută'
|
|
Bogdan Timofte
authored
3 months ago
|
181
|
else
|
|
|
182
|
echo 'AutoNAS nu este instalat'
|
|
|
183
|
fi
|
|
|
184
|
"
|
|
|
185
|
}
|
|
|
186
|
|
|
|
187
|
# Funcție pentru cleanup (ștergerea fișierelor temporare)
|
|
|
188
|
cleanup() {
|
|
|
189
|
local target="$1"
|
|
|
190
|
log_info "Cleanup fișiere temporare pe $target..."
|
|
|
191
|
|
|
|
192
|
ssh -o LogLevel=ERROR "$SSH_USER@$target" "rm -rf $REMOTE_DIR" || true
|
|
|
193
|
log_success "Cleanup realizat pe $target"
|
|
|
194
|
}
|
|
|
195
|
|
|
|
196
|
# Funcție pentru cleanup complet (delegat către uninstaller)
|
|
|
197
|
force_cleanup() {
|
|
|
198
|
local target="$1"
|
|
|
199
|
log_info "Cleanup forțat AutoNAS pe $target..."
|
|
|
200
|
|
|
|
201
|
# Încearcă să folosească uninstaller-ul existent
|
|
|
202
|
ssh -o LogLevel=ERROR "$SSH_USER@$target" "
|
|
Bogdan Timofte
authored
2 weeks ago
|
203
|
if [ -f '/usr/local/lib/xdev/autonas/uninstall.sh' ]; then
|
|
|
204
|
bash /usr/local/lib/xdev/autonas/uninstall.sh --force
|
|
|
205
|
elif [ -f '/usr/local/bin/autonas-uninstall.sh' ]; then
|
|
Bogdan Timofte
authored
3 months ago
|
206
|
bash /usr/local/bin/autonas-uninstall.sh --force
|
|
|
207
|
elif [ -f '/usr/local/lib/autonas/autonas-uninstall.sh' ]; then
|
|
|
208
|
bash /usr/local/lib/autonas/autonas-uninstall.sh --force
|
|
|
209
|
else
|
|
|
210
|
echo 'WARNING: Nu s-a găsit uninstaller-ul AutoNAS'
|
|
|
211
|
fi
|
|
|
212
|
" || true
|
|
|
213
|
|
|
|
214
|
ssh -o LogLevel=ERROR "$SSH_USER@$target" "rm -rf $REMOTE_DIR" || true
|
|
|
215
|
log_success "Cleanup forțat completat pe $target"
|
|
|
216
|
}
|
|
|
217
|
|
|
|
218
|
# Funcție pentru uninstall AutoNAS (delegat către uninstaller)
|
|
|
219
|
uninstall_autonas() {
|
|
|
220
|
local target="$1"
|
|
|
221
|
log_info "Dezinstalarea AutoNAS pe $target..."
|
|
|
222
|
|
|
|
223
|
if ssh -o LogLevel=ERROR "$SSH_USER@$target" "
|
|
Bogdan Timofte
authored
2 weeks ago
|
224
|
if [ -f '/usr/local/lib/xdev/autonas/uninstall.sh' ]; then
|
|
|
225
|
bash /usr/local/lib/xdev/autonas/uninstall.sh
|
|
|
226
|
echo 'AutoNAS dezinstalat cu succes!'
|
|
|
227
|
elif [ -f '/usr/local/bin/autonas-uninstall.sh' ]; then
|
|
Bogdan Timofte
authored
3 months ago
|
228
|
bash /usr/local/bin/autonas-uninstall.sh
|
|
|
229
|
rm -f '/usr/local/bin/autonas-uninstall.sh'
|
|
|
230
|
echo 'AutoNAS dezinstalat cu succes!'
|
|
|
231
|
elif [ -f '/usr/local/lib/autonas/autonas-uninstall.sh' ]; then
|
|
|
232
|
bash /usr/local/lib/autonas/autonas-uninstall.sh
|
|
|
233
|
echo 'AutoNAS dezinstalat cu succes!'
|
|
|
234
|
else
|
|
|
235
|
echo 'ERROR: Scriptul de dezinstalare nu a fost găsit!'
|
|
|
236
|
exit 1
|
|
|
237
|
fi
|
|
|
238
|
"; then
|
|
|
239
|
log_success "AutoNAS dezinstalat cu succes de pe $target"
|
|
|
240
|
else
|
|
|
241
|
log_error "Dezinstalarea AutoNAS a eșuat pe $target"
|
|
|
242
|
return 1
|
|
|
243
|
fi
|
|
|
244
|
}
|
|
|
245
|
|
|
|
246
|
# Funcții de delegare pentru operațiunile de servicii
|
|
|
247
|
install_autonas() {
|
|
|
248
|
run_on_target "$1" "Instalarea AutoNAS" "AutoNAS instalat cu succes pe $1" "Instalarea AutoNAS a eșuat pe $1" "install"
|
|
|
249
|
}
|
|
|
250
|
|
|
|
251
|
start_services() {
|
|
|
252
|
run_on_target "$1" "Pornirea serviciilor AutoNAS" "Serviciile AutoNAS pornite cu succes pe $1" "Pornirea serviciilor AutoNAS a eșuat pe $1" "start"
|
|
|
253
|
}
|
|
|
254
|
|
|
|
255
|
restart_services() {
|
|
|
256
|
run_on_target "$1" "Restartarea serviciilor AutoNAS" "Serviciile AutoNAS restartate cu succes pe $1" "Restartarea serviciilor AutoNAS a eșuat pe $1" "restart"
|
|
|
257
|
}
|
|
|
258
|
|
|
|
259
|
stop_services() {
|
|
|
260
|
run_on_target "$1" "Oprirea serviciilor AutoNAS" "Serviciile AutoNAS oprite cu succes pe $1" "Oprirea serviciilor AutoNAS a eșuat pe $1" "stop"
|
|
|
261
|
}
|
|
|
262
|
|
|
|
263
|
check_dependencies() {
|
|
|
264
|
run_on_target "$1" "Verificarea dependințelor" "Verificarea dependințelor completată pe $1" "Verificarea dependințelor a eșuat pe $1" "check-deps"
|
|
|
265
|
}
|
|
|
266
|
|
|
|
267
|
install_dependencies() {
|
|
|
268
|
run_on_target "$1" "Instalarea dependințelor" "Dependințele instalate cu succes pe $1" "Instalarea dependințelor a eșuat pe $1" "install-deps"
|
|
|
269
|
}
|
|
|
270
|
|
|
|
271
|
# Funcție pentru afișarea ajutorului
|
|
|
272
|
show_help() {
|
|
|
273
|
echo "Utilizare: $0 [COMANDĂ] [ȚINTE]"
|
|
|
274
|
echo ""
|
|
|
275
|
echo "COMENZI:"
|
|
|
276
|
echo " install - Instalare completă AutoNAS pe toate țintele"
|
|
|
277
|
echo " install-deps - Instalare doar dependințe (nfs-kernel-server, autofs, etc.)"
|
|
|
278
|
echo " check-deps - Verificare dependințe fără instalare"
|
|
|
279
|
echo " uninstall - Dezinstalare completă AutoNAS de pe toate țintele"
|
|
|
280
|
echo " start - Pornește serviciile AutoNAS"
|
|
|
281
|
echo " restart - Restartează serviciile AutoNAS"
|
|
|
282
|
echo " stop - Oprește serviciile AutoNAS"
|
|
|
283
|
echo " status - Afișează statusul serviciilor"
|
|
|
284
|
echo " cleanup - Șterge fișierele temporare"
|
|
|
285
|
echo " force-cleanup - Cleanup agresiv (inclusiv fișiere orfane)"
|
|
|
286
|
echo " help - Afișează acest ajutor"
|
|
|
287
|
echo ""
|
|
|
288
|
echo "ȚINTE (opțional):"
|
|
|
289
|
echo " 192.168.2.91, 192.168.2.92, 192.168.2.93"
|
|
|
290
|
echo " Dacă nu se specifică ținte, se rulează pe toate"
|
|
|
291
|
echo ""
|
|
|
292
|
echo "Exemple:"
|
|
|
293
|
echo " $0 check-deps # Verifică dependințele pe toate țintele"
|
|
|
294
|
echo " $0 install-deps # Instalează dependințele pe toate țintele"
|
|
|
295
|
echo " $0 install # Instalează pe toate țintele"
|
|
|
296
|
echo " $0 install 192.168.2.91 # Instalează doar pe .91"
|
|
|
297
|
echo " $0 restart 192.168.2.91 192.168.2.92 # Restart pe .91 și .92"
|
|
|
298
|
echo " $0 status # Verifică statusul pe toate țintele"
|
|
|
299
|
echo " $0 force-cleanup # Cleanup agresiv pe toate nodurile"
|
|
|
300
|
}
|
|
|
301
|
|
|
|
302
|
# Funcție pentru executarea unei comenzi pe ținte specifice
|
|
|
303
|
execute_command() {
|
|
|
304
|
local command="$1"
|
|
|
305
|
shift
|
|
|
306
|
local targets_to_use=("$@")
|
|
|
307
|
|
|
|
308
|
|
|
|
309
|
# Dacă nu sunt specificate ținte, folosește toate din cluster.json
|
|
|
310
|
if [ ${#targets_to_use[@]} -eq 0 ]; then
|
|
|
311
|
targets_to_use=( $(get_targets) )
|
|
|
312
|
fi
|
|
|
313
|
|
|
|
314
|
log_info "Executare comandă '$command' pe noduri: ${targets_to_use[*]}"
|
|
|
315
|
|
|
|
316
|
for target in "${targets_to_use[@]}"; do
|
|
|
317
|
local node_name
|
|
|
318
|
node_name=$(get_node_name "$target")
|
|
|
319
|
echo ""
|
|
|
320
|
echo "======================================"
|
|
|
321
|
echo " $command pe $node_name ($target)"
|
|
|
322
|
echo "======================================"
|
|
|
323
|
|
|
|
324
|
if ! check_ssh_connectivity "$target"; then
|
|
|
325
|
log_error "Nu se poate conecta la $target - skip"
|
|
|
326
|
continue
|
|
|
327
|
fi
|
|
|
328
|
|
|
|
329
|
case "$command" in
|
|
|
330
|
"install")
|
|
|
331
|
copy_files "$target"
|
|
|
332
|
install_autonas "$target"
|
|
|
333
|
cleanup "$target"
|
|
|
334
|
;;
|
|
|
335
|
"install-deps")
|
|
|
336
|
copy_files "$target"
|
|
|
337
|
install_dependencies "$target"
|
|
|
338
|
cleanup "$target"
|
|
|
339
|
;;
|
|
|
340
|
"check-deps")
|
|
|
341
|
copy_files "$target"
|
|
|
342
|
check_dependencies "$target"
|
|
|
343
|
cleanup "$target"
|
|
|
344
|
;;
|
|
|
345
|
"uninstall")
|
|
|
346
|
uninstall_autonas "$target"
|
|
|
347
|
cleanup "$target"
|
|
|
348
|
;;
|
|
|
349
|
"start")
|
|
|
350
|
start_services "$target"
|
|
|
351
|
;;
|
|
|
352
|
"restart")
|
|
|
353
|
restart_services "$target"
|
|
|
354
|
;;
|
|
|
355
|
"stop")
|
|
|
356
|
stop_services "$target"
|
|
|
357
|
;;
|
|
|
358
|
"status")
|
|
|
359
|
check_status "$target"
|
|
|
360
|
;;
|
|
|
361
|
"cleanup")
|
|
|
362
|
cleanup "$target"
|
|
|
363
|
;;
|
|
|
364
|
"force-cleanup")
|
|
|
365
|
force_cleanup "$target"
|
|
|
366
|
;;
|
|
|
367
|
*)
|
|
|
368
|
log_error "Comandă necunoscută: $command"
|
|
|
369
|
return 1
|
|
|
370
|
;;
|
|
|
371
|
esac
|
|
|
372
|
|
|
|
373
|
if [ $? -eq 0 ]; then
|
|
|
374
|
log_success "Comandă '$command' executată cu succes pe $target"
|
|
|
375
|
else
|
|
|
376
|
log_error "Comandă '$command' a eșuat pe $target"
|
|
|
377
|
fi
|
|
|
378
|
done
|
|
|
379
|
}
|
|
|
380
|
|
|
|
381
|
# Main function
|
|
|
382
|
main() {
|
|
|
383
|
print_banner
|
|
|
384
|
|
|
|
385
|
# Verifică dacă avem argumente
|
|
|
386
|
if [ $# -eq 0 ]; then
|
|
|
387
|
show_help
|
|
|
388
|
exit 1
|
|
|
389
|
fi
|
|
|
390
|
|
|
|
391
|
local command="$1"
|
|
|
392
|
shift
|
|
|
393
|
|
|
|
394
|
case "$command" in
|
|
|
395
|
"install"|"install-deps"|"check-deps"|"uninstall"|"start"|"restart"|"stop"|"status"|"cleanup"|"force-cleanup")
|
|
|
396
|
execute_command "$command" "$@"
|
|
|
397
|
;;
|
|
|
398
|
"help"|"--help"|"-h")
|
|
|
399
|
show_help
|
|
|
400
|
;;
|
|
|
401
|
*)
|
|
|
402
|
log_error "Comandă necunoscută: $command"
|
|
|
403
|
echo ""
|
|
|
404
|
show_help
|
|
|
405
|
exit 1
|
|
|
406
|
;;
|
|
|
407
|
esac
|
|
|
408
|
}
|
|
|
409
|
|
|
|
410
|
# Verifică dacă rulează ca script principal
|
|
|
411
|
if [ "${BASH_SOURCE[0]}" = "${0}" ]; then
|
|
|
412
|
main "$@"
|
|
|
413
|
fi
|