-
Notifications
You must be signed in to change notification settings - Fork 304
Expand file tree
/
Copy pathinitialize.sh
More file actions
executable file
·1839 lines (1697 loc) · 84.9 KB
/
initialize.sh
File metadata and controls
executable file
·1839 lines (1697 loc) · 84.9 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
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
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /bin/bash
export HOME=/root
export DA_ROOT="${DA_ROOT:-/usr/share/docassemble}"
export DA_DEFAULT_LOCAL="local3.12"
export DA_ACTIVATE="${DA_PYTHON:-${DA_ROOT}/${DA_DEFAULT_LOCAL}}/bin/activate"
echo "initialize: Activating Python with ${DA_ACTIVATE}" >&2
source "${DA_ACTIVATE}"
export DA_CONFIG_FILE_DIST="${DA_CONFIG_FILE_DIST:-${DA_ROOT}/config/config.yml.dist}"
export DA_CONFIG_FILE="${DA_CONFIG:-${DA_ROOT}/config/config.yml}"
export CONTAINERROLE=":${CONTAINERROLE:-all}:"
echo "initialize: config.yml is at" $DA_CONFIG_FILE >&2
echo "initialize: initialize starting" >&2
RESTOREFROMBACKUP=true
if [ -f /var/run/docassemble/da_running ] || [ -f /var/run/docassemble/status-rabbitmq-running ]; then
echo "initialize: unsafe shutdown detected; will not restore from backup" >&2
RESTOREFROMBACKUP=false
fi
if [ "${DAREADONLYFILESYSTEM:-false}" == "true" ]; then
RESTOREFROMBACKUP=false
fi
mkdir -p /var/run/docassemble
rm -f /var/run/docassemble/status-*
touch /var/run/docassemble/da_running
export DEBIAN_FRONTEND=noninteractive
if [ "${DAALLOWUPDATES:-true}" == "true" ]; then
echo "initialize: Running apt-get clean" >&2
apt-get clean &> /dev/null
echo "initialize: Running apt-get update" >&2
apt-get -q -y update &> /dev/null
fi
if [[ '$(dpkg --print-architecture)' == 'amd64' ]]; then
CURRENTARCH=x86_64
else
CURRENTARCH=aarch64
fi
echo "initialize: Determining if web browser already running" >&2
if [ -f /var/run/apache2/apache2.pid ]; then
APACHE_PID=$(</var/run/apache2/apache2.pid)
if kill -0 $APACHE_PID &> /dev/null; then
APACHERUNNING=true
else
rm -f /var/run/apache2/apache2.pid
APACHERUNNING=false
fi
else
APACHERUNNING=false
fi
if [ -f /var/run/nginx.pid ]; then
NGINX_PID=$(</var/run/nginx.pid)
if kill -0 $NGINX_PID &> /dev/null; then
NGINXRUNNING=true
else
rm -f /var/run/nginx.pid
NGINXRUNNING=false
fi
else
NGINXRUNNING=false
fi
echo "initialize: Determining if redis already running" >&2
if [[ $CONTAINERROLE =~ .*:(all|redis):.* ]] && redis-cli ping &> /dev/null; then
REDISRUNNING=true
else
REDISRUNNING=false
fi
echo "initialize: Determining if cron already running" >&2
if [ -f /var/run/crond.pid ]; then
CRON_PID=$(</var/run/crond.pid)
if kill -0 $CRON_PID &> /dev/null; then
CRONRUNNING=true
else
rm -f /var/run/crond.pid
CRONRUNNING=false
fi
else
CRONRUNNING=false
fi
echo "initialize: Determining hostname" >&2
if [ "${USEHTTPS:-false}" == "false" ] && [ "${BEHINDHTTPSLOADBALANCER:-false}" == "false" ]; then
URLROOT="http:\\/\\/"
else
URLROOT="https:\\/\\/"
fi
if [ "${DAHOSTNAME:-none}" != "none" ]; then
URLROOT="${URLROOT}${DAHOSTNAME}"
else
if [ "${EC2:-false}" == "true" ]; then
PUBLIC_HOSTNAME=`curl -s http://169.254.169.254/latest/meta-data/public-hostname`
else
PUBLIC_HOSTNAME=`hostname --fqdn`
fi
URLROOT="${URLROOT}${PUBLIC_HOSTNAME}"
fi
echo "initialize: Testing if S3 is in use" >&2
if [ "${S3ENABLE:-null}" == "null" ] && [ "${S3BUCKET:-null}" != "null" ]; then
export S3ENABLE=true
fi
if [ "${S3ENABLE:-null}" == "true" ] && [ "${S3BUCKET:-null}" != "null" ] && [ "${S3ACCESSKEY:-null}" != "null" ] && [ "${S3SECRETACCESSKEY:-null}" != "null" ]; then
export S3_ACCESS_KEY="$S3ACCESSKEY"
export S3_SECRET_KEY="$S3SECRETACCESSKEY"
export AWS_ACCESS_KEY_ID="$S3ACCESSKEY"
export AWS_SECRET_ACCESS_KEY="$S3SECRETACCESSKEY"
export AWS_DEFAULT_REGION="$S3REGION"
fi
export AWS_REQUEST_CHECKSUM_CALCULATION=WHEN_REQUIRED
export AWS_RESPONSE_CHECKSUM_VALIDATION=WHEN_REQUIRED
S4CMD_OPTS=""
if [ "${S3ENDPOINTURL:-null}" != "null" ]; then
S4CMD_OPTS="${S4CMD_OPTS}--endpoint-url=\"${S3ENDPOINTURL}\" "
fi
if [ "${S3_SSE_ALGORITHM:-null}" != "null" ]; then
S4CMD_OPTS="${S4CMD_OPTS}--API-ServerSideEncryption=\"${S3_SSE_ALGORITHM}\" "
fi
if [ "${S3_SSE_CUSTOMER_ALGORITHM:-null}" != "null" ]; then
S4CMD_OPTS="${S4CMD_OPTS}--API-SSECustomerAlgorithm=\"${S3_SSE_CUSTOMER_ALGORITHM}\" "
fi
if [ "${S3_SSE_CUSTOMER_KEY:-null}" != "null" ]; then
S4CMD_OPTS="${S4CMD_OPTS}--API-SSECustomerKey=\"${S3_SSE_CUSTOMER_KEY}\" "
fi
if [ "${S3_SSE_KMS_KEY_ID:-null}" != "null" ]; then
S4CMD_OPTS="${S4CMD_OPTS}--API-SSEKMSKeyId=\"${S3_SSE_KMS_KEY_ID}\" "
fi
export S4CMD_OPTS
if [ "${S3ENABLE:-null}" == "true" ]; then
echo "initialize: Creating S3 bucket, which may already exist" >&2
if [ "${USEMINIO:-false}" == "true" ]; then
python -m docassemble.webapp.createminio "${S3ENDPOINTURL}" "${S3ACCESSKEY}" "${S3SECRETACCESSKEY}" "${S3BUCKET}" &> /dev/null
else
s4cmd mb "s3://${S3BUCKET}" &> /dev/null
fi
fi
echo "initialize: Testing if Azure Blob Storage is in use" >&2
if [ "${AZUREENABLE:-null}" == "null" ] && [ "${AZUREACCOUNTNAME:-null}" != "null" ] && [ "${AZUREACCOUNTKEY:-null}" != "null" ] && [ "${AZURECONTAINER:-null}" != "null" ]; then
echo "initialize: Enabling Azure Blob Storage" >&2
export AZUREENABLE=true
fi
echo "initialize: If S3 is in use, see if RabbitMQ is running in the cluster" >&2
if [ "${S3ENABLE:-false}" == "true" ] && [[ $CONTAINERROLE =~ .*:(web):.* ]] && [[ $(s4cmd ls s3://${S3BUCKET}/hostname-rabbitmq) ]] && [[ $(s4cmd ls s3://${S3BUCKET}/ip-rabbitmq) ]]; then
TEMPKEYFILE=`mktemp`
s4cmd -f get s3://${S3BUCKET}/hostname-rabbitmq $TEMPKEYFILE
HOSTNAMERABBITMQ=$(<$TEMPKEYFILE)
s4cmd -f get s3://${S3BUCKET}/ip-rabbitmq $TEMPKEYFILE
IPRABBITMQ=$(<$TEMPKEYFILE)
rm -f $TEMPKEYFILE
if [ "${DAREADONLYFILESYSTEM:-false}" == "false" ]; then
if [ -n "$(grep $HOSTNAMERABBITMQ /etc/hosts)" ]; then
sed -i "/$HOSTNAMERABBITMQ/d" /etc/hosts
fi
echo "$IPRABBITMQ $HOSTNAMERABBITMQ" >> /etc/hosts
fi
fi
if [ "${AZUREENABLE:-false}" == "true" ]; then
echo "initialize: Initializing azure" >&2
export AZURE_STORAGE_KEY="${AZUREACCOUNTKEY}"
export AZURE_STORAGE_ACCOUNT="${AZUREACCOUNTNAME}"
export AZURE_STORAGE_AUTH_MODE=key
fi
echo "initialize: If Azure Blob Storage is in use, see if RabbitMQ is running in the cluster" >&2
if [ "${AZUREENABLE:-false}" == "true" ] && [[ $CONTAINERROLE =~ .*:(web):.* ]] && [[ $(python -m docassemble.webapp.list-cloud hostname-rabbitmq) ]] && [[ $(python -m docassemble.webapp.list-cloud ip-rabbitmq) ]]; then
TEMPKEYFILE=`mktemp`
echo "initialize: Copying hostname-rabbitmq" >&2
az storage blob download --no-progress --only-show-errors --output none --container-name "${AZURECONTAINER}" -n "hostname-rabbitmq" -f "${TEMPKEYFILE}"
HOSTNAMERABBITMQ=$(<$TEMPKEYFILE)
echo "initialize: Copying ip-rabbitmq" >&2
az storage blob download --no-progress --only-show-errors --output none --container-name "${AZURECONTAINER}" -n "ip-rabbitmq" -f "${TEMPKEYFILE}"
IPRABBITMQ=$(<$TEMPKEYFILE)
rm -f "${TEMPKEYFILE}"
if [ "${DAREADONLYFILESYSTEM:-false}" == "false" ]; then
if [ -n "$(grep $HOSTNAMERABBITMQ /etc/hosts)" ]; then
sed -i "/$HOSTNAMERABBITMQ/d" /etc/hosts
fi
echo "$IPRABBITMQ $HOSTNAMERABBITMQ" >> /etc/hosts
fi
fi
echo "initialize: Determining public hostname" >&2
if [ "${EC2:-false}" == "true" ]; then
export LOCAL_HOSTNAME=`curl -s http://169.254.169.254/latest/meta-data/local-hostname`
export PUBLIC_HOSTNAME=`curl -s http://169.254.169.254/latest/meta-data/public-hostname`
else
export LOCAL_HOSTNAME=`hostname --fqdn`
export PUBLIC_HOSTNAME="${LOCAL_HOSTNAME}"
fi
if [ "${RESTOREFROMBACKUP}" == "true" ]; then
echo "initialize: Restoring from backup" >&2
if [ "${S3ENABLE:-false}" == "true" ]; then
echo "initialize: Restoring from S3" >&2
if [[ $CONTAINERROLE =~ .*:(all):.* ]] && [[ $(s4cmd ls "s3://${S3BUCKET}/letsencrypt.tar.gz") ]]; then
echo "initialize: Restoring Let's Encrypt information from S3" >&2
rm -f /tmp/letsencrypt.tar.gz
s4cmd get "s3://${S3BUCKET}/letsencrypt.tar.gz" /tmp/letsencrypt.tar.gz
cd /
tar -xf /tmp/letsencrypt.tar.gz
rm -f /tmp/letsencrypt.tar.gz
else
rm -f /etc/letsencrypt/da_using_lets_encrypt
fi
if [ "${DABACKUPDAYS}" != "0" ]; then
if [[ $CONTAINERROLE =~ .*:(all):.* ]]; then
if [[ $(s4cmd ls "s3://${S3BUCKET}/backup") ]]; then
echo "initialize: Restoring backup information from S3" >&2
s4cmd dsync "s3://${S3BUCKET}/backup" "${DA_ROOT}/backup"
fi
elif [[ $(s4cmd ls "s3://${S3BUCKET}/backup/${LOCAL_HOSTNAME}") ]]; then
echo "initialize: Restoring backup information from S3" >&2
s4cmd dsync "s3://${S3BUCKET}/backup/${LOCAL_HOSTNAME}" "${DA_ROOT}/backup/${LOCAL_HOSTNAME}"
fi
fi
if [[ $CONTAINERROLE =~ .*:(all|web|log):.* ]] && [[ $(s4cmd ls "s3://${S3BUCKET}/apache") ]]; then
echo "initialize: Restoring apache information from S3" >&2
s4cmd dsync "s3://${S3BUCKET}/apache" /etc/apache2/sites-available
fi
if [[ $CONTAINERROLE =~ .*:(all):.* ]]; then
if [[ $(s4cmd ls "s3://${S3BUCKET}/apachelogs") ]]; then
echo "initialize: Restoring apache logs from S3" >&2
s4cmd dsync "s3://${S3BUCKET}/apachelogs" /var/log/apache2
chown root:adm /var/log/apache2/*
chmod 640 /var/log/apache2/*
fi
if [[ $(s4cmd ls "s3://${S3BUCKET}/nginxlogs") ]]; then
echo "initialize: Restoring NGINX logs from S3" >&2
s4cmd dsync "s3://${S3BUCKET}/nginxlogs" /var/log/nginx
chown www-data:adm /var/log/nginx/*
chmod 640 /var/log/nginx/*
fi
fi
if [[ $CONTAINERROLE =~ .*:(all|log):.* ]] && [[ $(s4cmd ls "s3://${S3BUCKET}/log") ]]; then
echo "initialize: Restoring logs from S3" >&2
s4cmd dsync "s3://${S3BUCKET}/log" "${LOGDIRECTORY:-${DA_ROOT}/log}"
chown -R www-data:www-data "${LOGDIRECTORY:-${DA_ROOT}/log}"
fi
if [[ $(s4cmd ls "s3://${S3BUCKET}/config.yml") ]]; then
echo "initialize: Restoring configuration from S3" >&2
rm -f "$DA_CONFIG_FILE"
s4cmd get "s3://${S3BUCKET}/config.yml" "$DA_CONFIG_FILE"
chown www-data:www-data "$DA_CONFIG_FILE"
fi
if [[ $CONTAINERROLE =~ .*:(all|redis):.* ]] && [[ $(s4cmd ls "s3://${S3BUCKET}/redis.rdb") ]] && [ "$REDISRUNNING" == "false" ]; then
echo "initialize: Restoring Redis from S3" >&2
s4cmd -f get "s3://${S3BUCKET}/redis.rdb" "/var/lib/redis/dump.rdb"
chown redis:redis "/var/lib/redis/dump.rdb"
fi
elif [ "${AZUREENABLE:-false}" == "true" ]; then
echo "initialize: Restoring from Azure" >&2
if [[ $CONTAINERROLE =~ .*:(all):.* ]] && [[ $(python -m docassemble.webapp.list-cloud letsencrypt.tar.gz) ]]; then
echo "initialize: Restoring Let's Encrypt information from Azure Blob Storage" >&2
rm -f /tmp/letsencrypt.tar.gz
echo "initialize: Copying let's encrypt" >&2
az storage blob download --no-progress --only-show-errors --output none --container-name "${AZURECONTAINER}" -n "letsencrypt.tar.gz" -f "/tmp/letsencrypt.tar.gz"
cd /
tar -xf /tmp/letsencrypt.tar.gz
rm -f /tmp/letsencrypt.tar.gz
else
rm -f /etc/letsencrypt/da_using_lets_encrypt
fi
if [ "${DABACKUPDAYS}" != "0" ]; then
if [[ $CONTAINERROLE =~ .*:(all):.* ]]; then
if [[ $(python -m docassemble.webapp.list-cloud backup/) ]]; then
echo "initialize: Restoring backup information from Azure Blob Storage" >&2
BACKUPDIR="backup/"
let BACKUPDIRLENGTH=${#BACKUPDIR}+1
for the_file in $(python -m docassemble.webapp.list-cloud $BACKUPDIR | cut -c ${BACKUPDIRLENGTH}-); do
echo "initialize: Found $the_file on Azure" >&2
if ! [[ $the_file =~ /$ ]]; then
if [ ! -f "${DA_ROOT}/backup/${the_file}" ]; then
echo "initialize: Copying backup file" $the_file >&2
mkdir -p "`dirname \"${DA_ROOT}/backup/${the_file}\"`"
az storage blob download --no-progress --only-show-errors --output none --container-name "${AZURECONTAINER}" -n "backup/${the_file}" -f "${DA_ROOT}/backup/${the_file}"
fi
fi
done
fi
elif [[ $(python -m docassemble.webapp.list-cloud backup/${LOCAL_HOSTNAME}/) ]]; then
echo "initialize: Restoring backup information from Azure Blob Storage" >&2
BACKUPDIR="backup/${LOCAL_HOSTNAME}/"
let BACKUPDIRLENGTH=${#BACKUPDIR}+1
for the_file in $(python -m docassemble.webapp.list-cloud $BACKUPDIR | cut -c ${BACKUPDIRLENGTH}-); do
echo "initialize: Found $the_file on Azure" >&2
if ! [[ $the_file =~ /$ ]]; then
if [ ! -f "${DA_ROOT}/backup/${the_file}" ]; then
echo "initialize: Copying backup file" $the_file >&2
mkdir -p "`dirname \"${DA_ROOT}/backup/${the_file}\"`"
az storage blob download --no-progress --only-show-errors --output none --container-name "${AZURECONTAINER}" -n "backup/${LOCAL_HOSTNAME}/${the_file}" -f "${DA_ROOT}/backup/${the_file}"
fi
fi
done
fi
fi
if [[ $CONTAINERROLE =~ .*:(all|web|log):.* ]] && [[ $(python -m docassemble.webapp.list-cloud apache/) ]]; then
echo "initialize: Restoring apache information from Azure Blob Storage" >&2
for the_file in $(python -m docassemble.webapp.list-cloud apache/ | cut -c 8-); do
if ! [[ $the_file =~ /$ ]]; then
az storage blob download --no-progress --only-show-errors --output none --container-name "${AZURECONTAINER}" -n "apache/${the_file}" -f "/etc/apache2/sites-available/${the_file}"
fi
done
else
rm -f /etc/letsencrypt/da_using_lets_encrypt
fi
if [[ $CONTAINERROLE =~ .*:(all):.* ]]; then
if [[ $(python -m docassemble.webapp.list-cloud apachelogs/) ]]; then
echo "initialize: Restoring apache logs from Azure Blob Storage" >&2
for the_file in $(python -m docassemble.webapp.list-cloud apachelogs/ | cut -c 12-); do
if ! [[ $the_file =~ /$ ]]; then
az storage blob download --no-progress --only-show-errors --output none --container-name "${AZURECONTAINER}" -n "apachelogs/${the_file}" -f "/var/log/apache2/${the_file}"
fi
done
chown root:adm /var/log/apache2/*
chmod 640 /var/log/apache2/*
fi
if [[ $(python -m docassemble.webapp.list-cloud nginxlogs/) ]]; then
echo "initialize: Restoring NGINX logs from Azure Blob Storage" >&2
for the_file in $(python -m docassemble.webapp.list-cloud nginxlogs/ | cut -c 11-); do
if ! [[ $the_file =~ /$ ]]; then
az storage blob download --no-progress --only-show-errors --output none --container-name "${AZURECONTAINER}" -n "nginxlogs/${the_file}" -f "/var/log/nginx/${the_file}"
fi
done
chown www-data:adm /var/log/nginx/*
chmod 640 /var/log/nginx/*
fi
fi
if [[ $CONTAINERROLE =~ .*:(all|log):.* ]] && [[ $(python -m docassemble.webapp.list-cloud log) ]]; then
echo "initialize: Restoring logs from Azure Blob Storage" >&2
for the_file in $(python -m docassemble.webapp.list-cloud log/ | cut -c 5-); do
if ! [[ $the_file =~ /$ ]]; then
az storage blob download --no-progress --only-show-errors --output none --container-name "${AZURECONTAINER}" -n "log/${the_file}" -f "${LOGDIRECTORY:-${DA_ROOT}/log}/${the_file}"
fi
done
chown -R www-data:www-data "${LOGDIRECTORY:-${DA_ROOT}/log}"
fi
if [[ $(python -m docassemble.webapp.list-cloud config.yml) ]]; then
echo "initialize: Restoring configuration from Azure Blob Storage" >&2
rm -f "$DA_CONFIG_FILE"
echo "initialize: Copying config.yml" >&2
az storage blob download --no-progress --only-show-errors --output none --container-name "${AZURECONTAINER}" -n "config.yml" -f "${DA_CONFIG_FILE}"
chown www-data:www-data "${DA_CONFIG_FILE}"
fi
if [[ $CONTAINERROLE =~ .*:(all|redis):.* ]] && [[ $(python -m docassemble.webapp.list-cloud redis.rdb) ]] && [ "$REDISRUNNING" == "false" ]; then
echo "initialize: Restoring Redis from Azure Blob Storage" >&2
az storage blob download --no-progress --only-show-errors --output none --container-name "${AZURECONTAINER}" -n "redis.rdb" -f "/var/lib/redis/dump.rdb"
chown redis:redis "/var/lib/redis/dump.rdb"
fi
else
if [[ $CONTAINERROLE =~ .*:(all):.* ]] && [ -f "${DA_ROOT}/backup/letsencrypt.tar.gz" ]; then
echo "initialize: Restoring Let's Encrypt information from backup" >&2
cd /
tar -xf "${DA_ROOT}/backup/letsencrypt.tar.gz"
fi
if [[ $CONTAINERROLE =~ .*:(all|web|log):.* ]] && [ -d "${DA_ROOT}/backup/apache" ]; then
echo "initialize: Restoring Apache information from backup" >&2
rsync -auq "${DA_ROOT}/backup/apache/" /etc/apache2/sites-available/
fi
if [[ $CONTAINERROLE =~ .*:(all):.* ]] && [ -d "${DA_ROOT}/backup/apachelogs" ]; then
echo "initialize: Restoring Apache logs from backup" >&2
rsync -auq "${DA_ROOT}/backup/apachelogs/" /var/log/apache2/
chown root:adm /var/log/apache2/*
chmod 640 /var/log/apache2/*
fi
if [[ $CONTAINERROLE =~ .*:(all):.* ]] && [ -d "${DA_ROOT}/backup/nginxlogs" ]; then
echo "initialize: Restoring NGINX logs from backup" >&2
rsync -auq "${DA_ROOT}/backup/nginxlogs/" /var/log/nginx/
chown www-data:adm /var/log/nginx/*
chmod 640 /var/log/nginx/*
fi
if [[ $CONTAINERROLE =~ .*:(all|log):.* ]] && [ -d "${DA_ROOT}/backup/log" ]; then
echo "initialize: Restoring logs from backup" >&2
rsync -auq "${DA_ROOT}/backup/log/" "${LOGDIRECTORY:-${DA_ROOT}/log}/"
chown -R www-data:www-data "${LOGDIRECTORY:-${DA_ROOT}/log}"
fi
if [ -f "${DA_ROOT}/backup/config.yml" ]; then
echo "initialize: Restoring Configuration from backup" >&2
cp "${DA_ROOT}/backup/config.yml" "${DA_CONFIG_FILE}"
chown www-data:www-data "${DA_CONFIG_FILE}"
fi
if [ -d "${DA_ROOT}/backup/files" ]; then
echo "initialize: Restoring files from backup" >&2
rsync -auq "${DA_ROOT}/backup/files" "${DA_ROOT}/"
chown -R www-data:www-data "${DA_ROOT}/files"
fi
if [[ $CONTAINERROLE =~ .*:(all|redis):.* ]] && [ -f "${DA_ROOT}/backup/redis.rdb" ] && [ "$REDISRUNNING" == "false" ]; then
echo "initialize: Restoring Redis from backup" >&2
cp "${DA_ROOT}/backup/redis.rdb" /var/lib/redis/dump.rdb
chown redis:redis "/var/lib/redis/dump.rdb"
fi
fi
fi
if [ "${BEHINDHTTPSLOADBALANCER:-null}" == "true" ] && [ "${XSENDFILE:-null}" == "null" ]; then
export XSENDFILE=false
fi
if [ ! -f "$DA_CONFIG_FILE" ]; then
if [ "${DADEFAULTSECRET:-null}" = "null" ]; then
DADEFAULTSECRET=$(python -m docassemble.base.generate_key)
fi
echo "initialize: There is no config file. Creating one from source." >&2
sed -e 's@{{DBPREFIX}}@'"${DBPREFIX:-postgresql+psycopg2:\/\/}"'@' \
-e 's/{{DBNAME}}/'"${DBNAME:-docassemble}"'/' \
-e 's/{{DBUSER}}/'"${DBUSER:-docassemble}"'/' \
-e 's#{{DBPASSWORD}}#'"${DBPASSWORD:-abc123}"'#' \
-e 's/{{DBHOST}}/'"${DBHOST:-null}"'/' \
-e 's/{{DBPORT}}/'"${DBPORT:-null}"'/' \
-e 's/{{DBTABLEPREFIX}}/'"${DBTABLEPREFIX:-null}"'/' \
-e 's/{{DBBACKUP}}/'"${DBBACKUP:-true}"'/' \
-e 's/{{DBSSLMODE}}/'"${DBSSLMODE:-null}"'/' \
-e 's/{{DBSSLCERT}}/'"${DBSSLCERT:-null}"'/' \
-e 's#{{DBSSLKEY}}#'"${DBSSLKEY:-null}"'#' \
-e 's#{{DBSSLROOTCERT}}#'"${DBSSLROOTCERT:-null}"'#' \
-e 's#{{CONFIGFROM}}#'"${CONFIGFROM:-null}"'#' \
-e 's/{{S3ENABLE}}/'"${S3ENABLE:-false}"'/' \
-e 's#{{S3ACCESSKEY}}#'"${S3ACCESSKEY:-null}"'#' \
-e 's#{{S3SECRETACCESSKEY}}#'"${S3SECRETACCESSKEY:-null}"'#' \
-e 's@{{S3ENDPOINTURL}}@'"${S3ENDPOINTURL:-null}"'@' \
-e 's/{{S3BUCKET}}/'"${S3BUCKET:-null}"'/' \
-e 's/{{S3REGION}}/'"${S3REGION:-null}"'/' \
-e 's/{{S3_SSE_ALGORITHM}}/'"${S3_SSE_ALGORITHM:-null}"'/' \
-e 's/{{S3_SSE_CUSTOMER_ALGORITHM}}/'"${S3_SSE_CUSTOMER_ALGORITHM:-null}"'/' \
-e 's#{{S3_SSE_CUSTOMER_KEY}}#'"${S3_SSE_CUSTOMER_KEY:-null}"'#' \
-e 's#{{S3_SSE_KMS_KEY_ID}}#'"${S3_SSE_KMS_KEY_ID:-null}"'#' \
-e 's/{{AZUREENABLE}}/'"${AZUREENABLE:-false}"'/' \
-e 's/{{AZUREACCOUNTNAME}}/'"${AZUREACCOUNTNAME:-null}"'/' \
-e 's@{{AZUREACCOUNTKEY}}@'"${AZUREACCOUNTKEY:-null}"'@' \
-e 's/{{AZURECONTAINER}}/'"${AZURECONTAINER:-null}"'/' \
-e 's/{{DABACKUPDAYS}}/'"${DABACKUPDAYS:-14}"'/' \
-e 's#{{REDIS}}#'"${REDIS:-null}"'#' \
-e 's#{{RABBITMQ}}#'"${RABBITMQ:-null}"'#' \
-e 's@{{DACELERYWORKERS}}@'"${DACELERYWORKERS:-null}"'@' \
-e 's@{{DAMAXCELERYWORKERS}}@'"${DAMAXCELERYWORKERS:-null}"'@' \
-e 's@{{TIMEZONE}}@'"${TIMEZONE:-null}"'@' \
-e 's/{{EC2}}/'"${EC2:-false}"'/' \
-e 's/{{COLLECTSTATISTICS}}/'"${COLLECTSTATISTICS:-false}"'/' \
-e 's/{{KUBERNETES}}/'"${KUBERNETES:-false}"'/' \
-e 's/{{USECLOUDURLS}}/'"${USECLOUDURLS:-false}"'/' \
-e 's/{{USEMINIO}}/'"${USEMINIO:-false}"'/' \
-e 's/{{USEHTTPS}}/'"${USEHTTPS:-false}"'/' \
-e 's/{{USELETSENCRYPT}}/'"${USELETSENCRYPT:-false}"'/' \
-e 's/{{LETSENCRYPTEMAIL}}/'"${LETSENCRYPTEMAIL:-null}"'/' \
-e 's@{{LOGSERVER}}@'"${LOGSERVER:-null}"'@' \
-e 's/{{DAHOSTNAME}}/'"${DAHOSTNAME:-none}"'/' \
-e 's/{{LOCALE}}/'"${LOCALE:-null}"'/' \
-e 's/{{SERVERADMIN}}/'"${SERVERADMIN:-webmaster@localhost}"'/' \
-e 's@{{DASECRETKEY}}@'"${DADEFAULTSECRET}"'@' \
-e 's@{{URLROOT}}@'"${URLROOT:-null}"'@' \
-e 's@{{POSTURLROOT}}@'"${POSTURLROOT:-/}"'@' \
-e 's/{{BEHINDHTTPSLOADBALANCER}}/'"${BEHINDHTTPSLOADBALANCER:-false}"'/' \
-e 's/{{XSENDFILE}}/'"${XSENDFILE:-true}"'/' \
-e 's/{{DAEXPOSEWEBSOCKETS}}/'"${DAEXPOSEWEBSOCKETS:-false}"'/' \
-e 's/{{DAWEBSOCKETSIP}}/'"${DAWEBSOCKETSIP:-null}"'/' \
-e 's/{{DAWEBSOCKETSPORT}}/'"${DAWEBSOCKETSPORT:-null}"'/' \
-e 's/{{DAUPDATEONSTART}}/'"${DAUPDATEONSTART:-true}"'/' \
-e 's/{{DAALLOWUPDATES}}/'"${DAALLOWUPDATES:-true}"'/' \
-e 's/{{DAWEBSERVER}}/'"${DAWEBSERVER:-nginx}"'/' \
-e 's/{{DASTABLEVERSION}}/'"${DASTABLEVERSION:-false}"'/' \
-e 's/{{DASQLPING}}/'"${DASQLPING:-false}"'/' \
-e 's/{{ENABLEUNOCONV}}/'"${ENABLEUNOCONV:-true}"'/' \
-e 's/{{DAALLOWCONFIGURATIONEDITING}}/'"${DAALLOWCONFIGURATIONEDITING:-true}"'/' \
-e 's/{{DAENABLEPLAYGROUND}}/'"${DAENABLEPLAYGROUND:-true}"'/' \
-e 's/{{DAALLOWLOGVIEWING}}/'"${DAALLOWLOGVIEWING:-true}"'/' \
-e 's/{{DAROOTOWNED}}/'"${DAROOTOWNED:-false}"'/' \
-e 's/{{DAREADONLYFILESYSTEM}}/'"${DAREADONLYFILESYSTEM:-false}"'/' \
-e 's/{{DADEBUG}}/'"${DADEBUG:-true}"'/g' \
-e 's/{{DASUPERVISORUSERNAME}}/'"${DASUPERVISORUSERNAME:-null}"'/g' \
-e 's/{{DASUPERVISORPASSWORD}}/'"${DASUPERVISORPASSWORD:-null}"'/g' \
-e 's#{{GOTENBERGENABLE}}#'"${GOTENBERGENABLE:-false}"'#g' \
-e 's@{{GOTENBERGURL}}@'"${GOTENBERGURL:-null}"'@g' \
-e 's#{{GOTENBERGUSERNAME}}#'"${GOTENBERGUSERNAME:-null}"'#g' \
-e 's/{{GOTENBERGPASSWORD}}/'"${GOTENBERGPASSWORD:-null}"'/g' \
-e 's/{{USENGINXTOSERVEFILES}}/'"${USENGINXTOSERVEFILES:-true}"'/g' \
"$DA_CONFIG_FILE_DIST" > "$DA_CONFIG_FILE" || exit 1
fi
if [ "${DAROOTOWNED:-false}" == "false" ] && [ "${DAREADONLYFILESYSTEM:-false}" == "false" ]; then
chown www-data:www-data "$DA_CONFIG_FILE"
chsh -s /bin/bash www-data
fi
echo "initialize: Defining environment variables from Configuration" >&2
source /dev/stdin < <(su -c "source \"${DA_ACTIVATE}\" && python -m docassemble.base.read_config \"${DA_CONFIG_FILE}\"" www-data)
export LOGDIRECTORY="${LOGDIRECTORY:-${DA_ROOT}/log}"
echo "initialize: Checking to see if this is the first time the server was initialized" >&2
DAINSTALLASROOT=true
if [ "${DAROOTOWNED:-false}" == "true" ]; then
if [ "${DAALLOWUPDATES:-true}" == "true" ] || [ "${DAENABLEPLAYGROUND:-true}" == "true" ]; then
DAINSTALLASROOT=false
fi
else
DAINSTALLASROOT=false
fi
if [ "${DASUPERVISORUSERNAME:-null}" != "null" ]; then
export SUPERVISORCMD="supervisorctl --serverurl http://localhost:9001 --username ${DASUPERVISORUSERNAME} --password ${DASUPERVISORPASSWORD}"
else
export SUPERVISORCMD="supervisorctl --serverurl http://localhost:9001"
fi
if [ "${DAREADONLYFILESYSTEM:-false}" == "false" ]; then
if [ ! -f /etc/hasbeeninitialized ]; then
echo "initialize: This is the first time the server was initialized" >&2
if [ "${DAROOTOWNED:-false}" == "true" ]; then
if [ "${DAALLOWUPDATES:-true}" == "true" ] \
|| [ "${DAENABLEPLAYGROUND:-true}" == "true" ]; then
chown -R www-data:www-data /usr/share/docassemble/local3.12
else
echo "initialize: Python virtual environment is read-only" >&2
fi
if [ "${DAALLOWCONFIGURATIONEDITING:-true}" == "true" ]; then
chown -R www-data:www-data /usr/share/docassemble/config
else
echo "initialize: The config.yml file is read-only" >&2
fi
if [ "${DAALLOWUPDATES:-true}" == "true" ] \
|| [ "${DAENABLEPLAYGROUND:-true}" == "true" ] \
|| [ "${DAALLOWCONFIGURATIONEDITING:-true}" == "true" ]; then
chown www-data:www-data /usr/share/docassemble/webapp/docassemble.wsgi
else
echo "initialize: The WSGI file is read-only" >&2
fi
else
echo "initialize: No root ownership; changing file ownership to www-data (this takes a long time)" >&2
chsh -s /bin/bash www-data
chown -R www-data:www-data /usr/share/docassemble/local3.12
chown -R www-data:www-data /usr/share/docassemble/config \
/usr/share/docassemble/webapp/docassemble.wsgi
fi
echo "initialize: considering whether to install extra fonts" >&2
if [ "${DAEXTRAFONTS:-false}" == "true" ]; then
echo "initialize: installing extra fonts" >&2
apt-get -q -y install \
texlive-fonts-extra \
fonts-adf-accanthis \
fonts-adf-berenis \
fonts-adf-gillius \
fonts-adf-universalis \
fonts-arkpandora \
fonts-beng \
fonts-beng-extra \
fonts-cabin \
fonts-cantarell \
fonts-clear-sans \
fonts-comfortaa \
fonts-comic-neue \
fonts-deva \
fonts-deva-extra \
fonts-ebgaramond-extra \
fonts-font-awesome \
fonts-gargi \
fonts-gfs-artemisia \
fonts-gfs-complutum \
fonts-gfs-didot \
fonts-gfs-neohellenic \
fonts-gfs-olga \
fonts-gfs-solomos \
fonts-go \
fonts-gubbi \
fonts-gujr \
fonts-gujr-extra \
fonts-guru \
fonts-guru-extra \
fonts-indic \
fonts-inter \
fonts-kalapi \
fonts-knda \
fonts-lato \
fonts-liberation2 \
fonts-lobster \
fonts-lobstertwo \
fonts-lohit-beng-assamese \
fonts-lohit-beng-bengali \
fonts-lohit-deva \
fonts-lohit-gujr \
fonts-lohit-guru \
fonts-lohit-knda \
fonts-lohit-mlym \
fonts-lohit-orya \
fonts-lohit-taml \
fonts-lohit-taml-classical \
fonts-lohit-telu \
fonts-mlym \
fonts-nakula \
fonts-navilu \
fonts-noto \
fonts-noto-cjk \
fonts-noto-cjk-extra \
fonts-noto-core \
fonts-noto-extra \
fonts-noto-ui-extra \
fonts-noto-unhinted \
fonts-oflb-asana-math \
fonts-open-sans \
fonts-orya \
fonts-orya-extra \
fonts-pagul \
fonts-paratype \
fonts-roboto-slab \
fonts-roboto-unhinted \
fonts-sahadeva \
fonts-samyak \
fonts-samyak-deva \
fonts-samyak-gujr \
fonts-samyak-mlym \
fonts-samyak-orya \
fonts-samyak-taml \
fonts-sarai \
fonts-sil-andika \
fonts-sil-annapurna \
fonts-sil-charis \
fonts-sil-gentiumplus \
fonts-sil-gentiumplus-compact \
fonts-smc \
fonts-smc-anjalioldlipi \
fonts-smc-chilanka \
fonts-smc-dyuthi \
fonts-smc-gayathri \
fonts-smc-karumbi \
fonts-smc-keraleeyam \
fonts-smc-manjari \
fonts-smc-meera \
fonts-smc-rachana \
fonts-smc-raghumalayalamsans \
fonts-smc-suruma \
fonts-smc-uroob \
fonts-stix \
fonts-taml \
fonts-telu \
fonts-telu-extra \
fonts-teluguvijayam \
fonts-yrsa-rasa \
cm-super &> /dev/null
if [ $? -eq 0 ]; then
echo "initialize: extra fonts installed." >&2
else
echo "initialize: error while installing extra fonts." >&2
fi
fi
echo "initialize: considering whether to install google fonts" >&2
if [ "${DAGOOGLEFONTS:-false}" == "true" ]; then
echo "initialize: installing google fonts" >&2
{ cd /tmp \
&& wget -q -O google-fonts.tar.gz https://github.com/google/fonts/archive/main.tar.gz \
&& tar -zxf google-fonts.tar.gz \
&& rm google-fonts.tar.gz \
&& mkdir -p /usr/share/fonts/truetype/google-fonts \
&& find ./fonts-main/ -name "*.ttf" -exec install -m644 {} /usr/share/fonts/truetype/google-fonts/ \; \
&& rm -r ./fonts-main \
&& fc-cache -f -v; } &> /dev/null
if [ $? -eq 0 ]; then
echo "initialize: google fonts installed." >&2
else
echo "initialize: error while installing google fonts." >&2
fi
fi
touch /etc/hasbeeninitialized
else
echo "initialize: This is not the first time the server was initialized" >&2
fi
fi
echo "initialize: Running start hook" >&2
python -m docassemble.webapp.starthook "${DA_CONFIG_FILE}"
if [ "${DAWEBSERVER:-nginx}" = "nginx" ]; then
echo "initialize: Setting up NGINX basic configuration and uwsgi directory" >&2
if [ "${DAREADONLYFILESYSTEM:-false}" == "false" ]; then
sed -e 's@{{DA_PYTHON}}@'"${DA_PYTHON:-${DA_ROOT}/${DA_DEFAULT_LOCAL}}"'@' \
-e 's@{{DAWSGIROOT}}@'"${WSGIROOT}"'@' \
-e 's@{{DA_ROOT}}@'"${DA_ROOT}"'@' \
"${DA_ROOT}/config/docassemble.ini.dist" > "${DA_ROOT}/config/docassemble.ini"
sed -e 's@{{DA_PYTHON}}@'"${DA_PYTHON:-${DA_ROOT}/${DA_DEFAULT_LOCAL}}"'@' \
-e 's@{{DA_ROOT}}@'"${DA_ROOT}"'@' \
"${DA_ROOT}/config/docassemblelog.ini.dist" > "${DA_ROOT}/config/docassemblelog.ini"
fi
mkdir -p /var/run/uwsgi
chown www-data:www-data /var/run/uwsgi
fi
if [ "${DAREADONLYFILESYSTEM:-false}" == "false" ]; then
echo "initialize: If S3 is in use, save Configuration to S3" >&2
if [ "${S3ENABLE:-false}" == "true" ] && [[ ! $(s4cmd ls "s3://${S3BUCKET}/config.yml") ]]; then
s4cmd -f put "${DA_CONFIG_FILE}" "s3://${S3BUCKET}/config.yml"
fi
echo "initialize: If S3 is in use, test if the files folder is missing" >&2
if [ "${S3ENABLE:-false}" == "true" ] && [[ ! $(s4cmd ls "s3://${S3BUCKET}/files") ]]; then
echo "initialize: Test if a files folder is present locally" >&2
if [ -d "${DA_ROOT}/files" ]; then
echo "initialize: Copy files from local storage to S3" >&2
for the_file in $(ls "${DA_ROOT}/files"); do
if [[ $the_file =~ ^[0-9]+ ]]; then
for sub_file in $(find "${DA_ROOT}/files/$the_file" -type f); do
file_number="${sub_file#${DA_ROOT}/files/}"
file_number="${file_number:0:15}"
file_directory="${DA_ROOT}/files/$file_number"
target_file="${sub_file#${file_directory}}"
file_number="${file_number//\//}"
file_number=$((16#$file_number))
s4cmd -f put "${sub_file}" "s3://${S3BUCKET}/files/${file_number}/${target_file}"
done
else
s4cmd dsync "${DA_ROOT}/files/${the_file}" "s3://${S3BUCKET}/${the_file}"
fi
done
fi
fi
if [ "${AZUREENABLE:-false}" == "true" ]; then
echo "initialize: Initializing Azure Blob Storage if it is not already initialized" >&2
export AZURE_STORAGE_KEY="${AZUREACCOUNTKEY}"
export AZURE_STORAGE_ACCOUNT="${AZUREACCOUNTNAME}"
export AZURE_STORAGE_AUTH_MODE=key
fi
echo "initialize: If Azure Blob Storage is in use, save Configuration to Azure Blob Storage" >&2
if [ "${AZUREENABLE:-false}" == "true" ] && [[ ! $(python -m docassemble.webapp.list-cloud config.yml) ]]; then
az storage blob upload --no-progress --overwrite true --only-show-errors --output none --container-name "${AZURECONTAINER}" -f "${DA_CONFIG_FILE}" -n "config.yml"
fi
echo "initialize: If Azure Blob Storage is in use, test if the files folder is missing" >&2
if [ "${AZUREENABLE:-false}" == "true" ] && [[ ! $(python -m docassemble.webapp.list-cloud files) ]]; then
echo "initialize: Test if a files folder is present locally" >&2
if [ -d "${DA_ROOT}/files" ]; then
echo "initialize: Copy files from local storage to Azure Blob Storage" >&2
for the_file in $(ls "${DA_ROOT}/files"); do
if [[ $the_file =~ ^[0-9]+ ]]; then
for sub_file in $(find "${DA_ROOT}/files/$the_file" -type f); do
file_number="${sub_file#${DA_ROOT}/files/}"
file_number="${file_number:0:15}"
file_directory="${DA_ROOT}/files/$file_number/"
target_file="${sub_file#${file_directory}}"
file_number="${file_number//\//}"
file_number=$((16#$file_number))
az storage blob upload --no-progress --overwrite true --only-show-errors --output none --container-name "${AZURECONTAINER}" -f "${sub_file}" -n "files/${file_number}/${target_file}"
done
else
for sub_file in $(find "${DA_ROOT}/files/$the_file" -type f); do
target_file="${sub_file#${DA_ROOT}/files/}"
az storage blob upload --no-progress --overwrite true --only-show-errors --output none --container-name "${AZURECONTAINER}" -f "${sub_file}" -n "${target_file}"
done
fi
done
fi
fi
fi
if [ "${DAHOSTNAME:-none}" == "none" ]; then
export DAHOSTNAME="${PUBLIC_HOSTNAME}"
fi
if [ "${DAWEBSERVER:-nginx}" = "apache" ] && [ "${DAREADONLYFILESYSTEM:-false}" == "false" ]; then
rm -f /etc/cron.daily/apache2
ln -s /usr/share/docassemble/cron/apache2 /etc/cron.daily/apache2
if [[ $CONTAINERROLE =~ .*:(all|web|log):.* ]]; then
echo "initialize: Setting up Apache" >&2
a2dissite -q 000-default &> /dev/null
a2dissite -q default-ssl &> /dev/null
rm -f /etc/apache2/sites-available/000-default.conf
rm -f /etc/apache2/sites-available/default-ssl.conf
if [ "${DAHOSTNAME:-none}" != "none" ]; then
if [ ! -f "/etc/letsencrypt/live/${DAHOSTNAME}/fullchain.pem" ]; then
rm -f /etc/letsencrypt/da_using_lets_encrypt
fi
if [ ! -f /etc/apache2/sites-available/docassemble-ssl.conf ]; then
cp "${DA_ROOT}/config/docassemble-ssl.conf.dist" /etc/apache2/sites-available/docassemble-ssl.conf
rm -f /etc/letsencrypt/da_using_lets_encrypt
fi
if [ ! -f /etc/apache2/sites-available/docassemble-http.conf ]; then
cp "${DA_ROOT}/config/docassemble-http.conf.dist" /etc/apache2/sites-available/docassemble-http.conf
rm -f /etc/letsencrypt/da_using_lets_encrypt
fi
if [ ! -f /etc/apache2/sites-available/docassemble-log.conf ]; then
cp "${DA_ROOT}/config/docassemble-log.conf.dist" /etc/apache2/sites-available/docassemble-log.conf
fi
if [ ! -f /etc/apache2/sites-available/docassemble-redirect.conf ]; then
cp "${DA_ROOT}/config/docassemble-redirect.conf.dist" /etc/apache2/sites-available/docassemble-redirect.conf
fi
else
if [ ! -f /etc/apache2/sites-available/docassemble-http.conf ]; then
cp "${DA_ROOT}/config/docassemble-http.conf.dist" /etc/apache2/sites-available/docassemble-http.conf || exit 1
fi
fi
a2ensite docassemble-http
fi
elif [ "${DAREADONLYFILESYSTEM:-false}" == "false" ]; then
echo "initialize: Disabling apache2 cron" >&2
rm -f /etc/cron.daily/apache2
ln -s /usr/share/docassemble/cron/donothing /etc/cron.daily/apache2
fi
if [ "${DAWEBSERVER:-nginx}" = "nginx" ] && [ "${DAREADONLYFILESYSTEM:-false}" == "false" ]; then
echo "initialize: Setting up NGINX configuration" >&2
if [ "${USELETSENCRYPT:-false}" == "true" ] && [ -f "/etc/letsencrypt/live/${DAHOSTNAME}/fullchain.pem" ]; then
DASSLCERTIFICATE="/etc/letsencrypt/live/${DAHOSTNAME}/fullchain.pem; # managed by Certbot"
DASSLCERTIFICATEKEY="/etc/letsencrypt/live/${DAHOSTNAME}/privkey.pem; # managed by Certbot"
else
DASSLCERTIFICATE="/etc/ssl/docassemble/nginx.crt;"
DASSLCERTIFICATEKEY="/etc/ssl/docassemble/nginx.key;"
fi
DASSLPROTOCOLS=${DASSLPROTOCOLS:-TLSv1.2 TLSv1.3}
DASSLCIPHERS=${DASSLCIPHERS:-HIGH:!aNULL:!MD5}
if [ ! -f "/etc/letsencrypt/live/${DAHOSTNAME}/fullchain.pem" ]; then
rm -f /etc/letsencrypt/da_using_lets_encrypt
fi
if [ "${BEHINDHTTPSLOADBALANCER:-false}" == "true" ]; then
DAREALIP="include ${DA_ROOT}/config/nginx-realip;"
ln -sf /etc/nginx/sites-available/docassembleredirect /etc/nginx/sites-enabled/docassembleredirect
else
DAREALIP=""
rm -f /etc/nginx/sites-enabled/docassembleredirect
fi
if [ "${POSTURLROOT}" == "/" ]; then
DALOCATIONREWRITE=" "
else
DALOCATIONREWRITE="location = ${WSGIROOT} { rewrite ^ ${POSTURLROOT}; }"
fi
if [[ $CONTAINERROLE =~ .*:(all|web|log):.* ]]; then
rm -f /etc/nginx/sites-available/default
rm -f /etc/nginx/sites-enabled/default
if [ "${DAHOSTNAME:-none}" != "none" ]; then
if [ ! -f /etc/nginx/sites-available/docassemblessl ]; then
sed -e 's@{{DAHOSTNAME}}@'"${DAHOSTNAME:-localhost}"'@' \
-e 's@{{DALOCATIONREWRITE}}@'"${DALOCATIONREWRITE}"'@' \
-e 's@{{DAWSGIROOT}}@'"${WSGIROOT}"'@' \
-e 's@{{DAPOSTURLROOT}}@'"${POSTURLROOT}"'@' \
-e 's@{{DAREALIP}}@'"${DAREALIP}"'@' \
-e 's@{{DAMAXCONTENTLENGTH}}@'"${DAMAXCONTENTLENGTH}"'@' \
-e 's@{{DASSLCERTIFICATE}}@'"${DASSLCERTIFICATE}"'@' \
-e 's@{{DASSLCERTIFICATEKEY}}@'"${DASSLCERTIFICATEKEY}"'@' \
-e 's@{{DASSLPROTOCOLS}}@'"${DASSLPROTOCOLS}"'@' \
-e 's@{{DASSLCIPHERS}}@'"${DASSLCIPHERS}"'@' \
-e 's@{{DAWEBSOCKETSIP}}@'"${DAWEBSOCKETSIP:-127.0.0.1}"'@' \
-e 's@{{DAWEBSOCKETSPORT}}@'"${DAWEBSOCKETSPORT:-5000}"'@' \
-e 's@{{DALISTENPORT}}@'"${PORT:-80}"'@' \
"${DA_ROOT}/config/nginx-ssl.dist" > "/etc/nginx/sites-available/docassemblessl"
rm -f /etc/letsencrypt/da_using_lets_encrypt
fi
if [ ! -f /etc/nginx/sites-available/docassemblehttp ]; then
sed -e 's@{{DAHOSTNAME}}@'"${DAHOSTNAME:-localhost}"'@' \
-e 's@{{DALOCATIONREWRITE}}@'"${DALOCATIONREWRITE}"'@' \
-e 's@{{DAWSGIROOT}}@'"${WSGIROOT}"'@' \
-e 's@{{DAPOSTURLROOT}}@'"${POSTURLROOT}"'@' \
-e 's@{{DAREALIP}}@'"${DAREALIP}"'@' \
-e 's@{{DAMAXCONTENTLENGTH}}@'"${DAMAXCONTENTLENGTH}"'@' \
-e 's@{{DAWEBSOCKETSIP}}@'"${DAWEBSOCKETSIP:-127.0.0.1}"'@' \
-e 's@{{DAWEBSOCKETSPORT}}@'"${DAWEBSOCKETSPORT:-5000}"'@' \
-e 's@{{DALISTENPORT}}@'"${PORT:-80}"'@' \
"${DA_ROOT}/config/nginx-http.dist" > "/etc/nginx/sites-available/docassemblehttp"
rm -f /etc/letsencrypt/da_using_lets_encrypt
fi
if [ ! -f /etc/nginx/sites-available/docassemblelog ]; then
sed -e 's@{{DAHOSTNAME}}@'"${DAHOSTNAME:-localhost}"'@' \
-e 's@{{DAMAXCONTENTLENGTH}}@'"${DAMAXCONTENTLENGTH}"'@' \
"${DA_ROOT}/config/nginx-log.dist" > "/etc/nginx/sites-available/docassemblelog"
fi
if [ ! -f /etc/nginx/sites-available/docassembleredirect ]; then
sed -e 's@{{DAHOSTNAME}}@'"${DAHOSTNAME:-localhost}"'@' \
"${DA_ROOT}/config/nginx-redirect.dist" > "/etc/nginx/sites-available/docassembleredirect"
fi
if [ ! -f /etc/nginx/sites-available/docassemblesslredirect ]; then
sed -e 's@{{DAHOSTNAME}}@'"${DAHOSTNAME:-localhost}"'@' \
"${DA_ROOT}/config/nginx-ssl-redirect.dist" > "/etc/nginx/sites-available/docassemblesslredirect"
fi
else
if [ ! -f /etc/nginx/sites-available/docassemblehttp ]; then
sed -e 's@{{DAHOSTNAME}}@'"${DAHOSTNAME:-localhost}"'@' \
-e 's@{{DALOCATIONREWRITE}}@'"${DALOCATIONREWRITE}"'@' \
-e 's@{{DAWSGIROOT}}@'"${WSGIROOT}"'@' \
-e 's@{{DAPOSTURLROOT}}@'"${POSTURLROOT}"'@' \
-e 's@{{DAREALIP}}@'"${DAREALIP}"'@' \
-e 's@{{DAMAXCONTENTLENGTH}}@'"${DAMAXCONTENTLENGTH}"'@' \
-e 's@{{DAWEBSOCKETSIP}}@'"${DAWEBSOCKETSIP:-127.0.0.1}"'@' \
-e 's@{{DAWEBSOCKETSPORT}}@'"${DAWEBSOCKETSPORT:-5000}"'@' \
-e 's@{{DALISTENPORT}}@'"${PORT:-80}"'@' \
"${DA_ROOT}/config/nginx-http.dist" > "/etc/nginx/sites-available/docassemblehttp"
fi
fi
fi
fi
echo "initialize: Setting and updating locale" >&2
if [ "${LOCALE:-undefined}" == "undefined" ]; then
LOCALE="en_US.UTF-8 UTF-8"
fi
set -- $LOCALE
DA_LANGUAGE=$1
export LANG=$1
if [ "${DAREADONLYFILESYSTEM:-false}" == "true" ]; then
echo "initialize: not running anything related to update-locale, locale-gen, apt-get, dpkg, or pip because file system is read-only" >&2
else
grep -q "^$LOCALE" /etc/locale.gen || { echo $LOCALE >> /etc/locale.gen && locale-gen ; }
update-locale LANG="${DA_LANGUAGE}"
if [ -n "$OTHERLOCALES" ]; then
echo "initialize: Setting other locales" >&2
NEWLOCALE=false
for LOCALETOSET in "${OTHERLOCALES[@]}"; do
grep -q "^$LOCALETOSET" /etc/locale.gen || { echo $LOCALETOSET >> /etc/locale.gen; NEWLOCALE=true; }
done
if [ "$NEWLOCALE" == "true" ]; then
locale-gen
fi
fi
if [ -n "$PACKAGES" ]; then
echo "initialize: Installing Ubuntu packages specified in the Configuration" >&2
for PACKAGE in "${PACKAGES[@]}"; do
apt-get -q -y install $PACKAGE &> /dev/null
done
fi
if [ "${DAINSTALLASROOT}" == "false" ]; then
echo "initialize: Disabling pip version check" >&2
su -c "source \"${DA_ACTIVATE}\" && pip config set global.disable-pip-version-check true" www-data
echo "initialize: Checking to see if an alternative pip global index is used" >&2
if [ "${PIPINDEXURL:-null}" != "null" ]; then
echo "initialize: Setting the alternative pip global index" >&2
su -c "source \"${DA_ACTIVATE}\" && pip config set global.index-url \"${PIPINDEXURL}\"" www-data
else
echo "initialize: Using the standard pip global index" >&2
su -c "source \"${DA_ACTIVATE}\" && pip config unset global.index-url" www-data &> /dev/null
fi
echo "initialize: Checking to see if extra pip index urls are used" >&2
if [ "${PIPEXTRAINDEXURLS:-null}" != "null" ]; then
echo "initialize: Setting extra pip index urls" >&2
su -c "source \"${DA_ACTIVATE}\" && pip config set global.extra-index-url ${PIPEXTRAINDEXURLS}" www-data
else
echo "initialize: Not using extra pip index urls" >&2
su -c "source \"${DA_ACTIVATE}\" && pip config unset global.extra-index-url" www-data &> /dev/null
fi
if [ -n "$PYTHONPACKAGES" ]; then
echo "initialize: Installing Python packages specified in the Configuration" >&2
for PACKAGE in "${PYTHONPACKAGES[@]}"; do
su -c "source \"${DA_ACTIVATE}\" && pip install $PACKAGE" www-data
done
fi
else
echo "initialize: Disabling pip version check" >&2
pip config set global.disable-pip-version-check true