This repository has been archived by the owner on Dec 8, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.sh
executable file
·706 lines (638 loc) · 19.2 KB
/
init.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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
#!/bin/bash
clear
shopt -s expand_aliases
alias GETTEXT='gettext "init"'
function valid_ip() {
local ip=$1
local stat=1
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
OIFS=$IFS
IFS='.'
ip=($ip)
IFS=$OIFS
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 && ${ip[2]} -le 255 && ${ip[3]} -lt 255 && ${ip[3]} -gt 0 ]]
stat=$?
fi
return $stat
}
read -p "$(GETTEXT "Continuing will erase the current configuration! Continue? [yes, no]: ")" continue
if [[ $continue == "yes" ]]; then
if [[ -e host_vars ]]; then
rm -R host_vars
fi
if [[ -e group_vars ]]; then
rm -R group_vars
fi
if [[ -e hosts.ini ]]; then
rm hosts.ini
fi
if [[ ! -f ~/.ssh/id_rsa && ! -f ~/.ssh/id_rsa.pub ]]; then
echo ""
read -p "$(GETTEXT "Enter the password for the key: ")" key_password
ssh-keygen -t rsa -P "$key_password" -f ~/.ssh/id_rsa -q
fi
echo ""
read -p "$(GETTEXT "Enter the number of hosts: ")" hosts_number
count=0
mkdir {host_vars,group_vars}
echo "[debian]" > hosts.ini
while [ "$count" -lt "$hosts_number" ]
do
echo ""
echo "$(GETTEXT "Configure host")" $((count + 1))
echo ""
while [[ true ]]; do
read -p "$(GETTEXT "IP Address: ")" server_ip
valid_ip "$server_ip"
if [[ $? -eq 0 ]]; then
echo ""
break
else
echo "$(GETTEXT "Incorrect IP address!")"
echo ""
fi
done
echo "$(GETTEXT "Parameters for connecting via SSH:")"
echo ""
read -p "$(GETTEXT "Port (default: 22): ")" ansible_port
ansible_port=${ansible_port:-22}
read -p "$(GETTEXT "User (default: root): ")" ansible_user
ansible_user=${ansible_user:-root}
while [[ true ]]; do
read -p "$(GETTEXT "Password: ")" ansible_password
if [[ -z $ansible_password ]]; then
echo "$(GETTEXT "The field cannot be empty!")"
echo ""
else
echo ""
break
fi
done
while [[ true ]]; do
read -p "$(GETTEXT "Domain Name: ")" domain_name
if [[ -z $domain_name ]]; then
echo "$(GETTEXT "The field cannot be empty!")"
echo ""
else
echo ""
break
fi
done
while [[ true ]]; do
read -p "$(GETTEXT "Hostname: ")" hostname
if [[ -z $hostname ]]; then
echo "$(GETTEXT "The field cannot be empty!")"
echo ""
else
echo ""
break
fi
done
while [[ true ]]; do
read -p "$(GETTEXT "Enable HTTPS? [yes, no]: ")" https_enable
case $https_enable in
[Yy]* )
https_enable=true
echo ""
echo "$(GETTEXT "1) Use a certificate obtained in advance")"
echo "$(GETTEXT "2) Get a certificate from Let's Encrypt")"
echo "$(GETTEXT "3) Generate a self-signed certificate")"
echo ""
while [[ true ]]; do
read -p "$(GETTEXT "Choose the right option: ")" ssl_option;
case $ssl_option in
1 )
while [[ true ]]; do
echo ""
read -p "$(GETTEXT "SSL Certificate Path (fullchain): ")" ssl_certificate
if [[ -z $ssl_certificate ]]; then
echo "$(GETTEXT "The field cannot be empty!")"
echo ""
else
break
fi
done
while [[ true ]]; do
read -p "$(GETTEXT "SSL Trusted Certificate Path (chain): ")" ssl_trusted_certificate
if [[ -z $ssl_trusted_certificate ]]; then
echo "$(GETTEXT "The field cannot be empty!")"
echo ""
else
break
fi
done
while [[ true ]]; do
read -p "$(GETTEXT "SSL Certificate Key Path: ")" ssl_certificate_key
if [[ -z $ssl_certificate_key ]]; then
echo "$(GETTEXT "The field cannot be empty!")"
echo ""
else
break
fi
done
break
;;
2 )
ssl_certificate="/etc/letsencrypt/live/{{ domain['name'] }}/fullchain.pem"
ssl_trusted_certificate="/etc/letsencrypt/live/{{ domain['name'] }}/chain.pem"
ssl_certificate_key="/etc/letsencrypt/live/{{ domain['name'] }}/privkey.pem"
echo ""
while [[ true ]]; do
read -p "$(GETTEXT "Enable OCSP Must Staple? [yes, no]: ")" ocsp_must_staple
case $ocsp_must_staple in
[Yy]* )
ocsp_must_staple=true
break
;;
[Nn]* )
ocsp_must_staple=false
break
;;
* )
echo "$(GETTEXT "Incorrect answer!")"
;;
esac
done
break
;;
3 )
ssl_certificate="/etc/ssl/self-signed/{{ domain['name'] }}/certificate.pem"
ssl_certificate_key="/etc/ssl/self-signed/{{ domain['name'] }}/privkey.pem"
break
;;
* )
echo "$(GETTEXT "Incorrect answer!")"
echo ""
;;
esac
done
echo ""
break
;;
[Nn]* )
https_enable=false
echo ""
break
;;
* )
echo "$(GETTEXT "Incorrect answer!")"
echo ""
;;
esac
done
while [[ true ]]; do
read -p "$(GETTEXT "Install DBMS? [yes, no]: ")" dbms_install
case $dbms_install in
[Yy]* )
dbms_install=true
echo ""
echo "$(GETTEXT "Available DBMS:")"
echo ""
echo "$(GETTEXT "1) MariaDB")"
echo "$(GETTEXT "2) MySQL")"
echo "$(GETTEXT "3) PostgreSQL")"
echo ""
while [[ true ]]; do
read -p "$(GETTEXT "Choose the right option: ")" dbms
case $dbms in
1 )
dbms_name="mariadb"
echo ""
read -p "$(GETTEXT "Old password (if exists): ")" dbms_old_password
while [[ true ]]; do
read -p "$(GETTEXT "Password: ")" dbms_new_password
if [[ -z $dbms_new_password ]]; then
echo "$(GETTEXT "The field cannot be empty!")"
echo ""
else
break
fi
done
echo ""
break
;;
2 )
dbms_name="mysql"
echo ""
read -p "$(GETTEXT "Old password (if exists): ")" dbms_old_password
while [[ true ]]; do
read -p "$(GETTEXT "Password: ")" dbms_new_password
if [[ -z $dbms_new_password ]]; then
echo "$(GETTEXT "The field cannot be empty!")"
echo ""
else
break
fi
done
echo ""
break
;;
3 )
dbms_name="postgresql"
echo ""
read -p "$(GETTEXT "Version (default: 12): ")" postgresql_version
postgresql_version=${postgresql_version:-12}
while [[ true ]]; do
read -p "$(GETTEXT "Password: ")" dbms_new_password
if [[ -z $dbms_new_password ]]; then
echo "$(GETTEXT "The field cannot be empty!")"
echo ""
else
break
fi
done
echo ""
break
;;
* )
echo "$(GETTEXT "Incorrect answer!")"
echo ""
;;
esac
done
if [[ $dbms_name != "postgresql" ]]; then
while [[ true ]]; do
read -p "$(GETTEXT "Install phpMyAdmin? [yes, no]: ")" phpmyadmin_install
case $phpmyadmin_install in
[Yy]* )
phpmyadmin_install=true
read -p "$(GETTEXT "Version (default: 5.1.0): ")" phpmyadmin_version
phpmyadmin_version=${phpmyadmin_version:-5.1.0}
while [[ true ]]; do
read -p "$(GETTEXT "Protecting phpMyAdmin? [yes, no]: ")" phpmyadmin_protect
case $phpmyadmin_protect in
[Yy]* )
phpmyadmin_protect=true
read -p "$(GETTEXT "Login: ")" htpasswd_username
read -p "$(GETTEXT "Password: ")" htpasswd_password
echo ""
break
;;
[Nn]* )
phpmyadmin_protect=false
echo ""
break
;;
* )
echo "$(GETTEXT "Incorrect answer!")"
echo ""
;;
esac
done
break
;;
[Nn]* )
phpmyadmin_install=false
echo ""
break
;;
* )
echo "$(GETTEXT "Incorrect answer!")"
echo ""
;;
esac
done
fi
# if [[ $dbms_name == "postgresql" ]]; then
# while [[ true ]]; do
# read -p "$(GETTEXT "Install pgAdmin? [yes, no]: ")" pgadmin_install
# case $pgadmin_install in
# [Yy]* )
# pgadmin_install=true
#
# while [[ true ]]; do
# read -p "$(GETTEXT "Protecting pgAdmin? [yes, no]: ")" pgadmin_protect
# case $pgadmin_protect in
# [Yy]* )
# pgadmin_protect=true
# read -p "$(GETTEXT "Login: ")" htpasswd_username
# read -p "$(GETTEXT "Password: ")" htpasswd_password
# echo ""
# break
# ;;
# [Nn]* )
# pgadmin_protect=false
# echo ""
# break
# ;;
# * )
# echo "$(GETTEXT "Incorrect answer!")"
# echo ""
# ;;
# esac
# done
#
# break
# ;;
# [Nn]* )
# pgadmin_install=false
# break
# ;;
# * )
# echo "$(GETTEXT "Incorrect answer!")"
# echo ""
# ;;
# esac
# done
# fi
break
;;
[Nn]* )
dbms_install=false
echo ""
break
;;
* )
echo "$(GETTEXT "Incorrect answer!")"
echo ""
;;
esac
done
# while [[ true ]]; do
# read -p "Install fail2ban? [yes, no]: " fail2ban_install
# case $fail2ban_install in
# [Yy]* )
# fail2ban_install=true
# echo ""
# break
# ;;
# [Nn]* )
# fail2ban_install=false
# echo ""
# break
# ;;
# * )
# echo "$(GETTEXT "Incorrect answer!")"
# echo ""
# ;;
# esac
# done
while [[ true ]]; do
read -p "$(GETTEXT "Install knockd? [yes, no]: ")" knockd_install
case $knockd_install in
[Yy]* )
knockd_install=true
read -p "$(GETTEXT "Port sequence (default: 500,1001,456): ")" port_sequence
port_sequence=${port_sequence:-500,1001,456}
read -p "$(GETTEXT "Sequence timeout (default: 15): ")" sequence_timeout
sequence_timeout=${sequence_timeout:-15}
read -p "$(GETTEXT "Command timeout (default: 10): ")" command_timeout
command_timeout=${command_timeout:-10}
echo ""
break
;;
[Nn]* )
knockd_install=false
echo ""
break
;;
* )
echo "$(GETTEXT "Incorrect answer!")"
echo ""
;;
esac
done
while [[ true ]]; do
read -p "$(GETTEXT "Configure sftp? [yes, no]: ")" sftp_configure
case $sftp_configure in
[Yy]* )
sftp_configure=true
read -p "$(GETTEXT "Root directory: ")" sftp_root
read -p "$(GETTEXT "Login: ")" sftp_user
read -p "$(GETTEXT "Password: ")" sftp_password
break
echo ""
;;
[Nn]* )
sftp_configure=false
echo ""
break
;;
* )
echo "$(GETTEXT "Incorrect answer!")"
echo ""
;;
esac
done
while [[ true ]]; do
read -p "$(GETTEXT "Install a firewall? [yes, no]: ")" firewall_install
case $firewall_install in
[Yy]* )
firewall_install="true"
echo ""
echo "$(GETTEXT "Available firewalls:")"
echo ""
echo "$(GETTEXT "1) UFW")"
# echo "$(GETTEXT "2) Firewalld")"
echo ""
while [[ true ]]; do
read -p "$(GETTEXT "Choose the right option: ")" firewall
case $firewall in
1 )
firewall_name="ufw"
echo ""
break
;;
# 2 )
# firewall_name="firewalld"
# echo ""
# break
# ;;
* )
echo "$(GETTEXT "Incorrect answer!")"
echo ""
;;
esac
done
break
;;
[Nn]* )
firewall_install="false"
echo ""
break
;;
* )
echo "$(GETTEXT "Incorrect answer!")"
echo ""
;;
esac
done
while [[ true ]]; do
read -p "$(GETTEXT "Protect SSH? [yes, no]: ")" ssh_protection
case $ssh_protection in
[Yy]* )
ssh_protection=true
echo ""
while [[ true ]]; do
read -p "$(GETTEXT "Port: ")" ssh_port
if [[ -z $ssh_port ]]; then
echo "$(GETTEXT "The field cannot be empty!")"
echo ""
else
break
fi
done
while [[ true ]]; do
read -p "$(GETTEXT "Login: ")" ssh_username
if [[ -z $ssh_username ]]; then
echo "$(GETTEXT "The field cannot be empty!")"
echo ""
else
break
fi
done
while [[ true ]]; do
read -p "$(GETTEXT "Password: ")" ssh_password
if [[ -z $ssh_password ]]; then
echo "$(GETTEXT "The field cannot be empty!")"
echo ""
else
break
fi
done
echo ""
break
;;
[Nn]* )
ssh_protection=false
echo ""
break
;;
* )
echo "$(GETTEXT "Incorrect answer!")"
echo ""
;;
esac
done
echo "$server_ip" ansible_port="$ansible_port" ansible_user="$ansible_user" ansible_become_password="$ansible_password" >> hosts.ini
cat <<EOF> host_vars/"$server_ip".yml
---
host:
name: "$hostname"
domain:
name: "$domain_name"
https:
enable: "$https_enable"
option: "$ssl_option"
ssl:
certificate: "$ssl_certificate"
trusted_certificate: "$ssl_trusted_certificate"
certificate_key: "$ssl_certificate_key"
ocsp:
must_staple: "$ocsp_must_staple"
dbms:
install: "$dbms_install"
name: "$dbms_name"
credentials:
password:
old: "$dbms_old_password"
new: "$dbms_new_password"
postgresql:
version: "$postgresql_version"
phpmyadmin:
install: "$phpmyadmin_install"
version: "$phpmyadmin_version"
protect:
enable: "$phpmyadmin_protect"
credentials:
user: "$htpasswd_username"
password: "$htpasswd_password"
pgadmin:
install: "$pgadmin_install"
protect:
enable: "$pgadmin_protect"
credentials:
user: "$htpasswd_username"
password: "$htpasswd_password"
fail2ban:
install: "$fail2ban_install"
knockd:
install: "$knockd_install"
port_sequence: "$port_sequence"
port_sequence_whitespaces: "${port_sequence//,/ }"
command_timeout: "$command_timeout"
sftp:
configure: "$sftp_configure"
root: "$sftp_root"
credentials:
user: "$sftp_user"
password: "$sftp_password"
firewall:
install: "$firewall_install"
name: "$firewall_name"
ssh:
protect: "$ssh_protection"
port: "$ssh_port"
credentials:
user: "$ssh_username"
password: "$ssh_password"
EOF
sshpass -p "$ansible_password" ssh-copy-id -p "$ansible_port" "$ansible_user"@"$server_ip"
echo "$(GETTEXT "Host")" "$server_ip" "("$domain_name")" "$(GETTEXT "successfully configured!")"
echo " "
(( count++ ))
done
echo -e "\n[debian:vars]\nansible_python_interpreter=/usr/bin/python3" >> hosts.ini
echo ""
echo "$(GETTEXT "Configuration of global parameters")"
echo ""
# Web-Server Ports
echo "$(GETTEXT "Web-Server ports")"
echo ""
while [[ true ]]; do
read -p "$(GETTEXT "Apache port: ")" apache_port
if [[ -z $apache_port ]]; then
echo "$(GETTEXT "The field cannot be empty!")"
echo ""
else
break
fi
done
echo ""
# PHP Configuration
echo "$(GETTEXT "PHP configuration")"
echo ""
read -p "$(GETTEXT "Version (default: 8.0): ")" php_version
php_version=${php_version:-8.0}
echo ""
# Nginx Protection
echo "$(GETTEXT "Nginx protection")"
echo ""
read -p "$(GETTEXT "Client body timeout (default: 5): ")" client_body_timeout
client_body_timeout=${client_body_timeout:-5}
read -p "$(GETTEXT "Client header timeout (default: 5): ")" client_header_timeout
client_header_timeout=${client_header_timeout:-5}
echo ""
cat <<EOF> group_vars/vars.yml
---
apache:
port: "$apache_port"
nginx:
timeout:
client:
body: "$client_body_timeout"
header: "$client_header_timeout"
php:
version: "$php_version"
EOF
while [[ true ]]; do
read -p "$(GETTEXT "To start deploying? [yes, no]: ")" deploy
case $deploy in
[Yy]* )
ansible-playbook playbook.yml
break
;;
[Nn]* )
echo ""
echo "$(GETTEXT "The deployment was aborted. To start the process, run ansible-playbook playbook.yml")"
break
;;
* )
echo "$(GETTEXT "Incorrect answer!")"
echo ""
;;
esac
done
else
echo "$(GETTEXT "Execution interrupted. To restart, run ./init.sh")"
fi