-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions
862 lines (597 loc) · 21.8 KB
/
functions
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
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
#!/usr/bin/env bash
### ############################################################################
### openspace42 synthia bootstrap framework.
### [https://github.com/openspace42/synthia].
### v0.4.0.
dna_version="v0.3.14"
### ############################################################################
### 1 | synthia global functions
### ############################################################################
synthia-define_vars() {
### define default variables.
### 1 | synthia global variables.
### ########################################################################
export project_name="nimbo"
export author_name="openspace42"
export git_host="https://github.com"
### ########################################################################
### 2 | synthia project_specific variales.
### ########################################################################
#### backup / restore.
#### #######################################################################
#### set this to 'disabled' to disable local backups and only upload to S3
#### [useful if your project handles large amounts of data for example].
#### S3 must be set up by the user or else backups will not work at all.
#### this is only the default, which will be suggested to the user during
#### install, and it can be overridden.
export backup_locally_mode_default="disabled"
#### set this to 'disabled' to disable restore test during S3 backup.
#### this is only the default, which will be suggested to the user during
#### install, and it can be overridden.
export backup_test_restore_mode_default="disabled"
#### set this to 'n' if your project stores no data on end users' machines
#### that could go lost during a re-install or update.
export backup_prompt_during_install="n"
#### #######################################################################
### ########################################################################
### 3 | define dna variables.
dna-define_vars
### 4 | define any additional custom variables.
### ########################################################################
export user_data_dir_path="/home/user-data"
### ########################################################################
### 5 | define final variables that depend on custom ones.
### ########################################################################
#### set default backup paths.
### [absolute, mark whether existing or generated].
declare -gA backup_paths_default_array=(
[${user_data_dir_path}]="existing"
)
### ########################################################################
}
#### bootstrap functions.
#### ###########################################################################
synthia-define_formatting() {
r=$'\e[1;31m'
g=$'\e[1;32m'
l=$'\e[1;34m'
m=$'\e[1;35m'
y=$'\e[1;33m'
o=$'\e[38;5;208m'
c=$'\e[1;36m'
n=$'\e[1;39m'
x=$'\e[0m'
b=$'\033[1m'
}
synthia-check_root() {
if [[ $EUID -ne 0 ]]
then
echo "${r-}${b-}This script must be run as root.${x-}"
echo
echo "${b-}Exiting...${x-}"
echo
exit 1
fi
}
synthia-download_dna() {
clone_host="https://github.com"
clone_author="openspace42"
clone_name="dna"
clone_base_dir="/root/${clone_author}"
clone_dir="${clone_base_dir}/${clone_name}"
mkdir -p "${clone_base_dir}"
if [ "${custom_dna_version-null}" = "l" ]
then
export dna_selected_vers="v0.0.1-custom-local"
echo "${r-}${b-}Sourcing | ${n-}local version${r-} | of | ${n-}dna${r-} | respecting any local alteration performed as per | ${n-}-d l${r-} |.${x-}"
echo "${dna_selected_vers}" > "./version_installed"
else
if [ -d "${clone_dir}" ]
then
rm -r "${clone_dir:?}"
fi
( cd "${clone_base_dir}" && git clone "${clone_host}/${clone_author}/${clone_name}" && echo && cd "${clone_name}"
. ./functions/snippets
git config advice.detachedHead false
### Define latest version
export dna_latest_vers_final="$(semtag getfinal)"
export dna_latest_vers_tagged="$(semtag getlast)"
export dna_latest_vers_bl_edge="$(semtag getcurrent)"
if [ "${custom_dna_version-null}" = "b" ]
then
export dna_selected_vers="${dna_latest_vers_bl_edge}"
echo "${r-}${b-}Installing | ${n-}bleeding-edge version${r-} | of | ${n-}dna${r-} | up to latest git commit as per | ${n-}-d b${r-} |.${x-}"
echo
git fetch
git reset --hard origin/master
elif [[ "${custom_dna_version-null}" == *"v"* ]]
then
export dna_selected_vers="${custom_dna_version}"
echo "${c-}${b-}Installing | ${n-}version ${dna_selected_vers}${c-} | of | ${n-}dna${c-} | as per | ${n-}-d ${dna_selected_vers}${c-} |.${x-}"
echo
git checkout "tags/${dna_selected_vers}"
elif [ "${custom_dna_version-null}" = "t" ]
then
export dna_selected_vers="${dna_latest_vers_tagged}"
echo "${o-}${b-}Installing | ${n-}latest-tagged version [${dna_selected_vers}]${o-} | of | ${n-}dna${o-} | [independently of its stability type] as per | ${n-}-d t${o-} |.${x-}"
echo
git checkout "tags/${dna_latest_vers_tagged}"
elif [ "${custom_dna_version-null}" = "f" ]
then
export dna_selected_vers="${dna_latest_vers_final}"
echo "${y-}${b-}Installing | ${n-}latest-final version [${dna_selected_vers}]${y-} | of | ${n-}dna${y-} | as per | ${n-}-d f${y-} |.${x-}"
echo
git checkout "tags/${dna_latest_vers_final}"
else
export dna_selected_vers="${dna_version}"
echo "${b-}Installing | ${g-}version [${dna_selected_vers}]${n-} | of | ${g-}dna${n-} |.${x-}"
echo
git checkout "tags/${dna_version}"
fi
echo "${dna_selected_vers}" > "./version_installed"
)
fi
}
synthia-source_dna() {
for f in /root/openspace42/dna/functions/*
do
. $f
done
}
#### ###########################################################################
#### backup_restore_functions
synthia-backup() {
### evaluate arguments.
### ########################################################################
while getopts ":lsri" arguments
do
case $arguments in
l) export backup_locally_only="y"
;;
s) export backup_s3_only="y"
;;
r) export backup_last_before_restore="y"
;;
i) export backup_skip_install_complete_check="y"
;;
\?) dna-echo -e "Invalid option | ${n-}-${OPTARG}${r-} | for function | ${n-}${FUNCNAME[0]}${r-} |."
dna-exit
;;
esac
done
OPTIND=1
if [ -n "${backup_last_before_restore-}" ]
then
if [ -n "${backup_s3_only-}" ]
then
dna-echo -e "Option | ${n-}backup_s3_only [-s]${r-} | is incompatible with option | ${n-}backup_last_before_restore [-r]${r-} |."
dna-exit
fi
export backup_locally_only="y"
fi
### ########################################################################
dna-backup_check_set_lockfile
if ! dna-backup
then
dna-echo -e "backup subloop function exited with errors."
dna-echo -m "continuing anyway to perform post-backup operations if any and exit backup function gracefully."
synthia-backup_post_operations
fi
dna-backup_cleanup
}
#### ###########################################################################
### ############################################################################
### 2 | synthia project-specific functions.
### ############################################################################
#### backup / restore.
#### ###########################################################################
synthia-backup_pre_operations() {
### perform pre-backup operations.
dna-echo_operation -h "performing pre-backup operations"
### ########################################################################
service nginx stop || true
service php7.2-fpm stop || true
### ########################################################################
dna-echo_operation -t
}
synthia-backup_post_operations() {
dna-echo_operation -h "performing post-backup operations"
### perform post-backup operations.
### ########################################################################
service nginx start || true
service php7.2-fpm start || true
### ########################################################################
dna-echo_operation -t
}
synthia-calculate_backup_size() {
### ########################################################################
### calculate backup_paths size.
dna-calculate_backup_paths_size
### ########################################################################
### calculate any additional custom size.
### ########################################################################
### ########################################################################
### add it up.
backup_total_size_estimate="${backup_paths_size_estimate}"
### ########################################################################
}
synthia-backup_generate_additional_paths() {
### generate backups for any additional custom item by populating their
### respective backup_paths.
dna-echo_operation -h "backing up any additional custom backup_path"
### ########################################################################
### create paths
dna-backup_generate_additional_paths_dirs
### ########################################################################
### perform backups of every item individually
### ########################################################################
### ########################################################################
dna-echo_operation -t
}
synthia-backup_locally() {
dna-echo_operation -h "backing up locally"
### generate backups for any additional custom item
synthia-backup_generate_additional_paths
### backup backup paths locally.
dna-backup_locally_data_backup_paths
### backup any additional custom item.
### ########################################################################
### ########################################################################
dna-echo_operation -t
}
restore_loop() {
rsync -aAXx --delete $restore_file_path/conf/ $conf_dir/
### Perform raptor specific restore on this running machine
echo "${b}Now restoring your old EasyEngine installation from backup...${x}"
echo
### Add hardcoded sites to exclude to the user-specified ones
restore_exclude_sites="22222, html, roundcubemail, ${exclude_sites-null}"
for site in $restore_file_path/data/*
do
if [ -d "$site" ]
then
this_site="$(echo "$site" | sed "s|${restore_file_path}/data/||")"
if [ "${restore_exclude_sites/$this_site}" = "$restore_exclude_sites" ]
then
echo "${b}*] Now evaluating site | ${g}$this_site${x}${b} |: ${x}"
echo
www_site_dir="$www_dir/$this_site"
proceed=y
if [ -d $www_site_dir ]
then
echo "${r}${b} This site already exists on this machine.${x}"
echo
if [ "${delete_existing:-null}" = "y" ]
then
echo "${b} Deleting site and re-creating it restoring its respective backup...${x}"
echo
ee site delete $this_site --no-prompt
else
read -rp "${b} Delete it and re-create it restoring its respective backup? (Y/n): ${x}" -n 1
echo
if [[ ! $REPLY =~ ^[Nn]$ ]]
then
echo "${b} Ok, proceeding...${x}"
echo
ee site delete $this_site --no-prompt
else
echo
echo "${b} Ok, skipping...${x}"
echo
proceed=n
fi
fi
fi
if [ $proceed = y ]
then
ee site create $this_site
echo
### Test to see if site to be restored is a wordpress site and if it is restore it while replacing new database details
real_wp_config_file="$site/wp-config.php"
if [ -f $real_wp_config_file ]
then
echo "${b} Proceeding with ${l}wordpress${x}${b} site restore...${x}"
echo
ee site update $this_site --wpfc
echo
www_wp_config_file="$www_site_dir/wp-config.php"
www_wp_db_name=`cat $www_wp_config_file | grep DB_NAME | cut -d \' -f 4`
www_wp_db_user=`cat $www_wp_config_file | grep DB_USER | cut -d \' -f 4`
www_wp_db_pass=`cat $www_wp_config_file | grep DB_PASSWORD | cut -d \' -f 4`
old_wp_db_name=`cat $real_wp_config_file | grep DB_NAME | cut -d \' -f 4`
old_wp_db_user=`cat $real_wp_config_file | grep DB_USER | cut -d \' -f 4`
old_wp_db_pass=`cat $real_wp_config_file | grep DB_PASSWORD | cut -d \' -f 4`
rm "$www_wp_config_file"
cp "$real_wp_config_file" "$www_wp_config_file"
sed -i "s|${old_wp_db_name}|${www_wp_db_name}|g" "$www_wp_config_file"
sed -i "s|${old_wp_db_user}|${www_wp_db_user}|g" "$www_wp_config_file"
sed -i "s|${old_wp_db_pass}|${www_wp_db_pass}|g" "$www_wp_config_file"
mysql -u"$www_wp_db_user" -p"${www_wp_db_pass}" "${www_wp_db_name}" < "$site/mysqldump.sql"
else
echo "${b} Proceeding with ${m}NON wordpress${x}${b} site restore...${x}"
echo
if [ "${enable_php:-null}" = "y" ]
then
echo "${b} Activating PHP on this site...${x}"
echo
ee site update $this_site --php
echo
elif [ "${enable_php:-null}" = "n" ]
then
echo "${b} NOT activating PHP on this site...${x}"
echo
else
read -rp "${b} Activate PHP on restored site? (Y/n): ${x}" -n 1
echo
if [[ ! $REPLY =~ ^[Nn]$ ]]
then
echo "${b} Ok, proceeding...${x}"
echo
ee site update $this_site --php
echo
echo
else
echo
echo "${b} Ok, skipping...${x}"
echo
fi
fi
fi
rsync -aAXx --exclude wp-config.php --exclude mysqldump.sql "$site"/ "$www_site_dir"
chown -R www-data:www-data "$www_site_dir"
if [ "${enable_le:-null}" = "y" ]
then
echo "${b} Provisioning LetsEncrypt TLS certificate for restored site...${x}"
echo
activate_le_proceed=y
elif [ "${enable_le:-null}" = "o" ]
then
echo "${b} Provisioning LetsEncrypt TLS certificate for restored site if DNS checks succeed...${x}"
echo
activate_le_proceed=o
elif [ "${enable_le:-null}" = "n" ]
then
echo "${b} NOT provisioning LetsEncrypt TLS certificate for restored site...${x}"
echo
else
read -rp "${b} Also provision LetsEncrypt TLS certificate for restored site? (Y/n): ${x}" -n 1
echo
if [[ ! $REPLY =~ ^[Nn]$ ]]
then
echo "${b} Ok, proceeding...${x}"
echo
activate_le_proceed=y
else
echo
echo "${b} Ok, skipping...${x}"
echo
fi
fi
if [ "${activate_le_proceed:-null}" = "y" ] || [ "${activate_le_proceed:-null}" = "o" ]
then
hostname="$this_site"
declare -a dnsarray=(
"hostname"
)
# Skip IPv6 checks as EasyEngine doesn't yet support it [WTF!!!]
declare -a iparray=(
"v4"
)
declare -A digarray=(
[HN]=""
[www]="www."
)
dnschecktype=$hostname
thisipv4="$(dig +short myip.opendns.com @resolver1.opendns.com)"
for dnstype in "${dnsarray[@]}"
do
for ipversion in "${iparray[@]}"
do
echo "${b} Now let's make sure your site's IP$ipversion DNS settings are correct.${x}"
echo
digarrayiter=0
for i in "${!digarray[@]}"
do
digarrayiter=$((digarrayiter + 1))
echo "${b} $digarrayiter] Now checking the ${digarray[$i]}$dnschecktype DNS record in IP$ipversion...${x}"
echo
accept=n
until [ $accept = "y" ]
do
if [ "$ipversion" = "v4" ]
then
thisip=$thisipv4
digresult="$(getent ahostsv4 "${digarray[$i]}""$dnschecktype". | head -1 |sed 's/ .*//')"
rectype="A"
else
thisip=$thisipv6
digresult="$(getent ahostsv6 "${digarray[$i]}""$dnschecktype". | grep -v "$thisipv4" | head -1 | sed 's/ .*//')"
rectype="AAAA"
fi
if [ -z "$digresult" ]
then
if [ ! "$dnstype" = "SRV" ]
then
echo "${r}${b} The ${digarray[$i]}$dnschecktype IP$ipversion DNS record does NOT appear to be at all set.${x}"
echo
else
echo "${r}${b} The ${digarray[$i]}$dnschecktype SRV record does NOT appear to be at all set.${x}"
echo
fi
echo "${b} Please ensure you set your DNS record as follows:${x}"
echo
echo -e " | ${digarray[$i]}$dnschecktype\\t\\t$rectype\\t$thisip |"
echo
if [ ! "${digarray[$i]}$dnschecktype" = "$hostname" ]
then
if [ $rectype = "A" ] || [ $rectype = "AAAA" ]
then
if [ "$ipversion" = "v4" ]
then
echo "${g}${b} You can also set this record as a CNAME [suggested choice]:${x}"
echo
echo -e " | ${digarray[$i]}$dnschecktype\\t\\tCNAME\\t$hostname |"
echo
fi
fi
fi
result=notset
else
if [ "$digresult" = "$thisip" ]
then
if [ ! "$dnstype" = "SRV" ]
then
echo "${g}${b} The ${digarray[$i]}$dnschecktype IP$ipversion DNS record appears to resolve correctly to this server.${x}"
echo
else
echo "${g}${b} The ${digarray[$i]}$dnschecktype SRV record appears to resolve correctly to this server.${x}"
echo
fi
echo -e " | ${digarray[$i]}$dnschecktype\\t\\t$rectype\\t$thisip |"
echo
result=ok
else
if [ ! "$dnstype" = "SRV" ]
then
echo "${r}${b} The ${digarray[$i]}$dnschecktype IP$ipversion DNS record does NOT appear to correctly resolve to this server.${x}"
echo
else
echo "${r}${b} The ${digarray[$i]}$dnschecktype SRV record does NOT appear to correctly resolve to this server.${x}"
echo
fi
echo "${b} This is the result of a DNS query for ${digarray[$i]}$dnschecktype:${x}"
echo
echo " $digresult"
echo
echo "${b} Please set it instead to:${x}"
echo
echo -e " | ${digarray[$i]}$dnschecktype\\t\\t$rectype\\t$thisip |"
echo
if [ ! "${digarray[$i]}$dnschecktype" = "$hostname" ]
then
if [ $rectype = "A" ] || [ $rectype = "AAAA" ]
then
if [ "$ipversion" = "v4" ]
then
echo "${g}${b} You can also set this record as a CNAME [suggested choice]:${x}"
echo
echo -e " | ${digarray[$i]}$dnschecktype\\t\\tCNAME\\t$hostname |"
echo
fi
fi
fi
result=incorrect
fi
fi
if [ ! $result = "ok" ]
then
if [ "${activate_le_proceed:-null}" = "y" ]
then
valid=n
until [ $valid = "y" ]
do
read -n 1 -rp "${b} Test again?${x} (${b}Y${x}[es]/${b}s${x}[kip]/${b}e${x}[xit]) " answer;
case $answer in
"")
echo
valid=y
accept=n
;;
y)
echo -e "\\n"
valid=y
accept=n
;;
s)
echo -e "\\n"
echo "${b} Skipping DNS check for this record...${x}"
echo
valid=y
accept=y
;;
e)
echo -e "\\n"
echo "${b} Exiting...${x}"
echo
exit
;;
*)
echo -e "\\n"
echo "${b} Invalid option. Retry...${x}"
echo
valid=n
accept=n
;;
esac
done
else
echo "${r}${b} DNS test failed for this record. Continuing anyway...${x}"
echo
accept=y
fi
else
accept=y
fi
done
if [ $result = ok ]
then
if [ $i = "HN" ]
then
le_dns_check_HN=ok
else
le_dns_check_www=ok
fi
fi
done
echo "${b} Finished checking your $dnstype IP$ipversion DNS settings.${x}"
echo
done
done
if [ "${le_dns_check_HN:-null}" = "ok" ] && [ "${le_dns_check_www:-null}" = "ok" ]
then
echo "${g}${b} Activating LetsEncrypt for $this_site...${x}"
echo
ee site update $this_site --le --experimental || true
echo
else
echo "${r}${b} One or more DNS checks for $this_site failed and was skipped.${x}"
echo
echo "${b} NOT activating LetsEncrypt for $this_site."
echo
fi
if [ ! -z "${le_dns_check_HN-}" ]
then
unset le_dns_check_HN
fi
if [ ! -z "${le_dns_check_www-}" ]
then
unset le_dns_check_www
fi
if [ ! -z "${activate_le_proceed-}" ]
then
unset activate_le_proceed
fi
fi
echo "${b} Finished restore for | $this_site |.${x}"
echo
fi
else
echo "${b}*] Site | ${r}$this_site${x}${b} | is on the exclusion list.${x}"
echo
echo "${b} Skipping...${x}"
echo
fi
fi
done
}
#### ###########################################################################
### ############################################################################
### 3 | synthia installation.
### ############################################################################
synthia-perform_installation() {
dna-link_tools
dna-set_admin_email
dna-setup_backup_restore
dna-add_cron_job -n "${project_name}-backup" -t "8 4 * * * " -c "bash ${tools_dir}/${project_name}-backup"
}
### ############################################################################