autoNAS / deploy.sh
Newer Older
5b5a565 3 months ago History
409 lines | 12.877kb
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
179
        elif [ -f '/usr/local/bin/autonas' ]; then
180
            /usr/local/bin/autonas --version 2>/dev/null || echo 'AutoNAS instalat dar versiune necunoscută'
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" "
203
        if [ -f '/usr/local/bin/autonas-uninstall.sh' ]; then
204
            bash /usr/local/bin/autonas-uninstall.sh --force
205
        elif [ -f '/usr/local/lib/autonas/autonas-uninstall.sh' ]; then
206
            bash /usr/local/lib/autonas/autonas-uninstall.sh --force
207
        else
208
            echo 'WARNING: Nu s-a găsit uninstaller-ul AutoNAS'
209
        fi
210
    " || true
211

            
212
    ssh -o LogLevel=ERROR "$SSH_USER@$target" "rm -rf $REMOTE_DIR" || true
213
    log_success "Cleanup forțat completat pe $target"
214
}
215

            
216
# Funcție pentru uninstall AutoNAS (delegat către uninstaller)
217
uninstall_autonas() {
218
    local target="$1"
219
    log_info "Dezinstalarea AutoNAS pe $target..."
220

            
221
    if ssh -o LogLevel=ERROR "$SSH_USER@$target" "
222
        if [ -f '/usr/local/bin/autonas-uninstall.sh' ]; then
223
            bash /usr/local/bin/autonas-uninstall.sh
224
            rm -f '/usr/local/bin/autonas-uninstall.sh'
225
            echo 'AutoNAS dezinstalat cu succes!'
226
        elif [ -f '/usr/local/lib/autonas/autonas-uninstall.sh' ]; then
227
            bash /usr/local/lib/autonas/autonas-uninstall.sh
228
            echo 'AutoNAS dezinstalat cu succes!'
229
        else
230
            echo 'ERROR: Scriptul de dezinstalare nu a fost găsit!'
231
            exit 1
232
        fi
233
    "; then
234
        log_success "AutoNAS dezinstalat cu succes de pe $target"
235
    else
236
        log_error "Dezinstalarea AutoNAS a eșuat pe $target"
237
        return 1
238
    fi
239
}
240

            
241
# Funcții de delegare pentru operațiunile de servicii
242
install_autonas() {
243
    run_on_target "$1" "Instalarea AutoNAS" "AutoNAS instalat cu succes pe $1" "Instalarea AutoNAS a eșuat pe $1" "install"
244
}
245

            
246
start_services() {
247
    run_on_target "$1" "Pornirea serviciilor AutoNAS" "Serviciile AutoNAS pornite cu succes pe $1" "Pornirea serviciilor AutoNAS a eșuat pe $1" "start"
248
}
249

            
250
restart_services() {
251
    run_on_target "$1" "Restartarea serviciilor AutoNAS" "Serviciile AutoNAS restartate cu succes pe $1" "Restartarea serviciilor AutoNAS a eșuat pe $1" "restart"
252
}
253

            
254
stop_services() {
255
    run_on_target "$1" "Oprirea serviciilor AutoNAS" "Serviciile AutoNAS oprite cu succes pe $1" "Oprirea serviciilor AutoNAS a eșuat pe $1" "stop"
256
}
257

            
258
check_dependencies() {
259
    run_on_target "$1" "Verificarea dependințelor" "Verificarea dependințelor completată pe $1" "Verificarea dependințelor a eșuat pe $1" "check-deps"
260
}
261

            
262
install_dependencies() {
263
    run_on_target "$1" "Instalarea dependințelor" "Dependințele instalate cu succes pe $1" "Instalarea dependințelor a eșuat pe $1" "install-deps"
264
}
265

            
266
# Funcție pentru afișarea ajutorului
267
show_help() {
268
    echo "Utilizare: $0 [COMANDĂ] [ȚINTE]"
269
    echo ""
270
    echo "COMENZI:"
271
    echo "  install       - Instalare completă AutoNAS pe toate țintele"
272
    echo "  install-deps  - Instalare doar dependințe (nfs-kernel-server, autofs, etc.)"
273
    echo "  check-deps    - Verificare dependințe fără instalare"
274
    echo "  uninstall     - Dezinstalare completă AutoNAS de pe toate țintele"
275
    echo "  start         - Pornește serviciile AutoNAS"
276
    echo "  restart       - Restartează serviciile AutoNAS"
277
    echo "  stop          - Oprește serviciile AutoNAS"
278
    echo "  status        - Afișează statusul serviciilor"
279
    echo "  cleanup       - Șterge fișierele temporare"
280
    echo "  force-cleanup - Cleanup agresiv (inclusiv fișiere orfane)"
281
    echo "  help          - Afișează acest ajutor"
282
    echo ""
283
    echo "ȚINTE (opțional):"
284
    echo "  192.168.2.91, 192.168.2.92, 192.168.2.93"
285
    echo "  Dacă nu se specifică ținte, se rulează pe toate"
286
    echo ""
287
    echo "Exemple:"
288
    echo "  $0 check-deps                 # Verifică dependințele pe toate țintele"
289
    echo "  $0 install-deps               # Instalează dependințele pe toate țintele"
290
    echo "  $0 install                    # Instalează pe toate țintele"
291
    echo "  $0 install 192.168.2.91       # Instalează doar pe .91"
292
    echo "  $0 restart 192.168.2.91 192.168.2.92  # Restart pe .91 și .92"
293
    echo "  $0 status                     # Verifică statusul pe toate țintele"
294
    echo "  $0 force-cleanup              # Cleanup agresiv pe toate nodurile"
295
}
296

            
297
# Funcție pentru executarea unei comenzi pe ținte specifice
298
execute_command() {
299
    local command="$1"
300
    shift
301
    local targets_to_use=("$@")
302

            
303

            
304
    # Dacă nu sunt specificate ținte, folosește toate din cluster.json
305
    if [ ${#targets_to_use[@]} -eq 0 ]; then
306
        targets_to_use=( $(get_targets) )
307
    fi
308

            
309
    log_info "Executare comandă '$command' pe noduri: ${targets_to_use[*]}"
310

            
311
    for target in "${targets_to_use[@]}"; do
312
        local node_name
313
        node_name=$(get_node_name "$target")
314
        echo ""
315
        echo "======================================"
316
        echo "  $command pe $node_name ($target)"
317
        echo "======================================"
318

            
319
        if ! check_ssh_connectivity "$target"; then
320
            log_error "Nu se poate conecta la $target - skip"
321
            continue
322
        fi
323

            
324
        case "$command" in
325
            "install")
326
                copy_files "$target"
327
                install_autonas "$target"
328
                start_services "$target"
329
                cleanup "$target"
330
                ;;
331
            "install-deps")
332
                copy_files "$target"
333
                install_dependencies "$target"
334
                cleanup "$target"
335
                ;;
336
            "check-deps")
337
                copy_files "$target"
338
                check_dependencies "$target"
339
                cleanup "$target"
340
                ;;
341
            "uninstall")
342
                uninstall_autonas "$target"
343
                cleanup "$target"
344
                ;;
345
            "start")
346
                start_services "$target"
347
                ;;
348
            "restart")
349
                restart_services "$target"
350
                ;;
351
            "stop")
352
                stop_services "$target"
353
                ;;
354
            "status")
355
                check_status "$target"
356
                ;;
357
            "cleanup")
358
                cleanup "$target"
359
                ;;
360
            "force-cleanup")
361
                force_cleanup "$target"
362
                ;;
363
            *)
364
                log_error "Comandă necunoscută: $command"
365
                return 1
366
                ;;
367
        esac
368

            
369
        if [ $? -eq 0 ]; then
370
            log_success "Comandă '$command' executată cu succes pe $target"
371
        else
372
            log_error "Comandă '$command' a eșuat pe $target"
373
        fi
374
    done
375
}
376

            
377
# Main function
378
main() {
379
    print_banner
380

            
381
    # Verifică dacă avem argumente
382
    if [ $# -eq 0 ]; then
383
        show_help
384
        exit 1
385
    fi
386

            
387
    local command="$1"
388
    shift
389

            
390
    case "$command" in
391
        "install"|"install-deps"|"check-deps"|"uninstall"|"start"|"restart"|"stop"|"status"|"cleanup"|"force-cleanup")
392
            execute_command "$command" "$@"
393
            ;;
394
        "help"|"--help"|"-h")
395
            show_help
396
            ;;
397
        *)
398
            log_error "Comandă necunoscută: $command"
399
            echo ""
400
            show_help
401
            exit 1
402
            ;;
403
    esac
404
}
405

            
406
# Verifică dacă rulează ca script principal
407
if [ "${BASH_SOURCE[0]}" = "${0}" ]; then
408
    main "$@"
409
fi