-
Notifications
You must be signed in to change notification settings - Fork 33
/
rocketchatctl
executable file
·1272 lines (1132 loc) · 37.4 KB
/
rocketchatctl
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
set -o nounset
errcho() {
echo "$@" >&2
}
show_help() {
cat << EOM
rocketchatctl command line tool to install and update RocketChat server
Usage: $(basename "$0") [options] [--root-url=ROOT_URL --port=PORT --letsencrypt-email=EMAIL --webserver=WEBSERVER --version=VERSION --install-node --use-mongo]
Installs node, mongo, RocketChat server and optionally a webserver (Caddy or Traefik), sets up directories and permissions to use Let's Encrypt certificates.
In case node or mongo already installed, it uses already installed versions though confirmation is required.
For node it set $NODE_VERSION as default in your system, for mongo wiredTiger storage engine and no authentication enabled is required during installation.
If you wish this script to run unattended, provide extra flags to the install option, server URL is required (--root-url).
OPTIONS
-h help Display this message
install Install latest RocketChat server version
update Update RocketChat server from current version to latest version
check-updates Check for updates of RocketChat server
upgrade-rocketchatctl Upgrade the rocketchatctl command line tool
configure Configures RocketChat server and Let's Encrypt
backup Makes a rocketchat database backup
FOR UNATTENDED INSTALLATION
--root-url=ROOT_URL the public URL where RocketChat server will be accessible on the Internet (REQUIRED)
--port=PORT port for the RocketChat server, default value 3000
--webserver=WEBSERVER webserver to install as reverse proxy for RocketChat server, options are caddy/traefik/none (REQUIRED)
--letsencrypt-email=EMAIL e-mail address to use for SSL certificates (REQUIRED if webserver is not none)
--version=VERSION RocketChat server version to install, default latest
--install-node in case node installed, sets node to RocketChat server recommended version, default behavoir cancel RocketChat server installation
--use-mongo in case mongo installed, and storage engine configured is wiredTiger, skip mongo installation but uses systems mongo for RocketChat server database,
default database name rocketchat
--mongo-version=4.x.x mongo 4 version, default value is latest (supported only for Debian and Ubuntu)
--bind-loopback=value value=(true|false) set to false to prevent from bind RocketChat server to loopback interface when installing a webserver (default true)
--reg-token=TOKEN This value can be obtained from https://cloud.rocket.chat to automatically register your workspace during startup
FOR CONFIGURE OPTION
--rocketchat --root-url=ROOT_URL --port=PORT --bind-loopback=value Reconfigures RocketChat server Site-URL and port (--root-url REQUIRED)
--lets-encrypt --root-url=ROOT_URL --letsencrypt-email=EMAIL --bind-loopback=value Reconfigures webserver with Let's Encrypt and RocketChat server Site-URL (--root-url and letsencrypt-email REQUIRED)
FOR BACKUP OPTION
--backup-dir=<path_to_dir> sets the directory for storing the backup files, default backup directory is /tmp
EOM
}
show_short_help() {
errcho "Run '$(basename "$0") -h' to see the full list of available options."
}
print_run_as_root_error_and_exit() {
errcho "This script must be run as root. Cancelling"
show_short_help
exit 1
}
print_input_from_pipe_error_and_exit() {
errcho "This script is interactive, it can't be run with stdin piped"
exit 1
}
print_incorrect_parameters_error_and_exit() {
errcho "Incorrect parameters passed. Select one option."
echo -e ""
show_help
exit 2
}
error_with_no_value_specified() {
echo "Command line option: '--${1}' must have an assigned value."
show_short_help
}
print_distro_not_supported_error_and_exit() {
errcho "The detected Linux distribution is not supported by rocketchatctl."
exit 1
}
print_mongo_version_not_supported_and_exit() {
errcho "The detected Linux distribution does not support choosing mongo version."
exit 1
}
print_check_mongo_storage_and_exit() {
errcho "IMPORTANT: Starting with Rocket.Chat version 4.X, a \"wiredTiger\" storage engine is required in MongoDB. Please migrate your existing database first: https://docs.rocket.chat/installation/manual-installation/mongodb-mmap-to-wiredtiger-migration"
exit 1
}
print_node_version_error_and_exit() {
errcho "Different nodejs version already exists in the system. Cancelling"
exit 1
}
print_use_mongo_for_rocketchat_error_and_exit() {
errcho "mongod already exists in the system. Cancelling"
exit 1
}
print_mongo_storage_engine_error_and_exit() {
errcho "Storage engine from previous mongo installation in your system is not wiredTiger. Cancelling"
exit 1
}
print_mongo_connection_failed_error_and_exit() {
errcho "Connection failed to previous installed mongo in your system. Cancelling"
exit 1
}
print_rocketchat_installed_error_and_exit() {
errcho "RocketChat server already installed. Cancelling"
exit 1
}
print_root_url_error_and_exit() {
errcho "root-url must have an assigned value. Use --root-url=<URL> for unattended install or the configure option. Cancelling"
show_short_help
exit 2
}
print_wrong_webserver_error_and_set_none() {
webserver=none
errcho "webserver assigned value should be traefik, caddy or none. Use --webserver=(traefik/caddy/none) for unattended install. Skipping webserver installation"
echo -e ""
}
print_email_error_and_exit() {
errcho "letsencrypt-email must have an assigned value if decided to install a webserver or use configure option. Cancelling"
show_short_help
exit 2
}
print_check_updates_error_and_exit() {
errcho "Could not determine if updates available for RocketChat server."
exit 1
}
print_update_install_failed_error_and_exit() {
errcho "Something went wrong backing up current RocketChat server version before update. Cancelling"
exit 1
}
print_upgrade_download_rocketchatctl_error_and_exit() {
errcho "Error downloading rocketchatctl. Cancelling"
exit 1
}
print_update_install_failed_exit() {
errcho "Error in updated RocketChat server health check, restoring backup."
exit 1
}
print_update_backup_failed_exit() {
errcho "Update failed, can't get RocketChat server API in port $PORT. Cancelling"
exit 1
}
print_download_traefik_error_and_exit() {
errcho "Error downloading traefik. Cancelling"
exit 1
}
print_rocketchat_not_running_error_and_exit() {
errcho "RocketChat server not running. Cancelling"
exit 1
}
print_rocketchat_in_latest_version_and_exit() {
echo "RocketChat server already in latest version."
exit 2
}
print_no_webserver_installed_and_exit() {
echo "Looks like you don't have either Caddy or Traefik installed."
exit 2
}
print_make_backup() {
echo "Updates could be risky, you can use the backup option # rocketchatctl backup, to create a backup of the rocketchat database first. Please check that you have enough space in your system to store the backup."
}
print_check_update_change_node_version() {
echo "IMPORTANT: Update from version 2.x to 3.x requires node update, if you decide to update, your node version will change from 8.x to 12.x."
}
print_update_available_and_exit() {
echo "Current update available for RocketChat server: from $current_rocketchat_version to $latest_rocketchat_version."
}
print_backup_ok() {
echo "Backup done successfully, check for the backup file in $backup_dir/dump_$sufix.gz. You can restore using # mongorestore --host localhost:27017 --db rocketchat --archive --gzip < $backup_dir/dump_$sufix.gz"
}
print_backup_dir_error_and_exit() {
echo "Can't write in the backup directory: $backup_dir"
exit 1
}
print_node_not_updated_error_and_exit() {
echo "Error: node version was not updated correctly. Cancelling ..."
exit 1
}
print_done() {
echo "Done :)"
}
command_exists() {
command -v "$@" > /dev/null 2>&1
}
get_os_distro() {
if [ -r /etc/os-release ]; then
distro="$(. /etc/os-release && echo "$ID")"
distro_version="$(. /etc/os-release && echo "$VERSION_ID")"
distro_codename="$(. /etc/os-release && echo ${VERSION_CODENAME:-})"
fi
}
os_supported() {
get_os_distro
case "$distro" in
ubuntu)
[[ "$distro_version" =~ (("18.04"|"19.04"|"19.10"|"20.04"|"21.04"|"21.10")) ]] || print_distro_not_supported_error_and_exit
;;
debian)
[[ "$distro_version" =~ (("9"|"10"|"11")) ]] || print_distro_not_supported_error_and_exit
;;
centos)
[[ "$distro_version" =~ (("7"|"8")) ]] || print_distro_not_supported_error_and_exit
;;
*)
print_distro_not_supported_error_and_exit
;;
esac
}
mongo_version_supported() {
if [[ -n $MONGO_VERSION ]]; then
{ [[ $distro == ubuntu ]] || [[ $distro == debian ]]; } || print_mongo_version_not_supported_and_exit
fi
}
interactive_mongo() {
if [ ${use_mongo_arg} -eq 0 ]; then
read -r -p "It appears you already have mongo installed, this script will skip mongo installation but can't assure successful RocketChat server installation.
Database name for RocketChat server will be rocketchat.
Would you like to use your mongo installation for the RocketChat server database? (y/n) " use_mongo_answer
echo -e ""
[ ${use_mongo_answer} == 'y' ] && use_mongo_arg=1 || print_use_mongo_for_rocketchat_error_and_exit
fi
}
interactive_rocketchat() {
if [ -z "${ROOT_URL}" ]; then
read -r -p "Enter ROOT_URL for your RocketChat server installation (for example https://www.mydomain.com) : " ROOT_URL
echo -e ""
fi
[ -z ${ROOT_URL} ] && print_root_url_error_and_exit
ROOT_URL=$(echo "$ROOT_URL" | tr '[:upper:]' '[:lower:]')
}
interactive_webserver() {
if [ ${webserver_arg} -eq 0 ]; then
read -r -p "Select a webserver to use as a reverse proxy for your RocketChat server installation [none]: (traefik/caddy/none) " webserver
echo -e ""
fi
webserver=$(echo "$webserver" | tr '[:upper:]' '[:lower:]')
! [[ ${webserver} =~ ((traefik)|(caddy)|(none)) ]] && print_wrong_webserver_error_and_set_none
}
interactive_mail() {
if [ ${webserver} != "none" ]; then
if [ "${rocket_mail_arg}" -eq 0 ]; then
read -r -p "Enter e-mail for Let's Encrypt certificates: " rocket_mail
echo -e ""
fi
[ -z ${rocket_mail} ] && print_email_error_and_exit
rocket_mail=$(echo "$rocket_mail" | tr '[:upper:]' '[:lower:]')
fi
}
check_arguments_unattended_install() {
while [ $# -gt 0 ]; do
IFS='=' read -r -a args <<< $1
case "${args[0]}" in
--install-node)
install_node_arg=1
;;
--use-mongo)
use_mongo_arg=1
;;
--webserver)
if [ ${#args[@]} -ne 2 ]; then
error_with_no_value_specified "${args[0]}"
exit 2
fi
webserver="${args[1]}"
webserver_arg=1
;;
--letsencrypt-email)
if [ ${#args[@]} -ne 2 ]; then
error_with_no_value_specified "${args[0]}"
exit 2
fi
rocket_mail="${args[1]}"
rocket_mail_arg=1
;;
--root-url)
if [ ${#args[@]} -ne 2 ]; then
error_with_no_value_specified "${args[0]}"
exit 2
fi
ROOT_URL="${args[1]}"
;;
--port)
if [ ${#args[@]} -ne 2 ]; then
error_with_no_value_specified "${args[0]}"
exit 2
fi
PORT="${args[1]}"
;;
--version)
if [ ${#args[@]} -ne 2 ]; then
error_with_no_value_specified "${args[0]}"
exit 2
fi
VERSION="${args[1]}"
ROCKETCHAT_DOWNLOAD_URL="https://releases.rocket.chat/$VERSION/download"
;;
--mongo-version)
if [ ${#args[@]} -ne 2 ]; then
error_with_no_value_specified "${args[0]}"
exit 2
fi
MONGO_VERSION="${args[1]}"
;;
--bind-loopback)
if [ ${#args[@]} -ne 2 ]; then
error_with_no_value_specified "${args[0]}"
exit 2
fi
bind_loopback="${args[1]}"
;;
--reg-token)
if [ ${#args[@]} -ne 2 ]; then
error_with_no_value_specified "${args[0]}"
exit 2
fi
REG_TOKEN="${args[1]}"
;;
*)
show_help
exit 2
;;
esac
shift
done
}
apt_configure_mongo() {
local key_url="https://www.mongodb.org/static/pgp/server-$MONGO_VERSION_WITHOUT_PATCH.asc"
local key_file="/usr/share/keyrings/mongodb-org-$MONGO_VERSION_WITHOUT_PATCH.gpg"
local repo_file="/etc/apt/sources.list.d/mongodb-org-$MONGO_VERSION_WITHOUT_PATCH.list"
declare -A repo=([ubuntu]=multiverse [debian]=main)
local repo_url="deb [ arch=amd64 signed-by=$key_file ] https://repo.mongodb.org/apt/$distro $distro_codename/mongodb-org/$MONGO_VERSION_WITHOUT_PATCH ${repo[$distro]}"
curl -fsSL "$key_url" | gpg --dearmor -o "$key_file"
echo $repo_url > $repo_file
}
yum_configure_mongo() {
local yum_mongo_url="https://repo.mongodb.org/yum/redhat/$distro_version/mongodb-org/$MONGO_VERSION_WITHOUT_PATCH/x86_64/"
local yum_key="https://www.mongodb.org/static/pgp/server-$MONGO_VERSION_WITHOUT_PATCH.asc"
cat << EOF | tee -a /etc/yum.repos.d/mongodb-org-$MONGO_VERSION_WITHOUT_PATCH.repo
[mongodb-org-$MONGO_VERSION_WITHOUT_PATCH]
name=MongoDB Repository
baseurl=$yum_mongo_url
gpgcheck=1
enabled=1
gpgkey=$yum_key
EOF
}
apt_install_mongo() {
apt-get update -qqq
local pkg="mongodb-org"
[[ -n $MONGO_VERSION ]] && pkg+="-$MONGO_VERSION" || true
apt install $pkg -y
printf "mongodb-org hold" | dpkg --set-selections
printf "mongodb-org-server hold" | dpkg --set-selections
printf "mongodb-org-shell hold" | dpkg --set-selections
printf "mongodb-org-mongos hold" | dpkg --set-selections
printf "mongodb-org-tools hold" | dpkg --set-selections
}
yum_install_mongo() {
local pkg="mongodb-org"
[[ -n $MONGO_VERSION ]] && pkg+="-$MONGO_VERSION" || true
yum install -y $pkg
}
configure_mongo() {
sed -i "s/^# engine:/ engine: wiredTiger/" /etc/mongod.conf
sed -i "s/^#replication:/replication:\n replSetName: rs01/" /etc/mongod.conf
if ! grep -Fq " fork: true" /etc/mongod.conf; then
sed -i "/^processManagement:/a\ fork: true" /etc/mongod.conf
fi
}
create_mongo_systemd_file() {
mkdir /etc/systemd/system/mongod.service.d/
cat << EOF | tee -a /etc/systemd/system/mongod.service.d/mongod.conf
[Service]
Type=oneshot
RemainAfterExit=yes
EOF
}
initiate_and_start_mongo() {
systemctl daemon-reload
systemctl enable mongod
systemctl start mongod
if [[ -n $(pgrep mongod) ]]; then
mongo --eval "printjson(rs.initiate())" >&2
fi
}
install_rocketchat() {
curl -L $ROCKETCHAT_DOWNLOAD_URL -o /tmp/rocket.chat.tgz
tar -xzf /tmp/rocket.chat.tgz -C /tmp
cd /tmp/bundle/programs/server && npm install
# add npm install [email protected] to fix 4 moderate severity vulnerabilities
mv /tmp/bundle $ROCKETCHAT_DIR
rm /tmp/rocket.chat.tgz
}
set_rocketchat_user_and_permissions() {
id -un rocketchat 1> /dev/null 2>&1
if [ $? -ne 0 ]; then
useradd -M rocketchat && usermod -L rocketchat
fi
chown -R rocketchat:rocketchat $ROCKETCHAT_DIR
}
create_rocketchat_systemd_file() {
if [ -f /lib/systemd/system/rocketchat.service ]; then
true > /lib/systemd/system/rocketchat.service
fi
if [[ $webserver != "none" ]] && $bind_loopback; then
BIND_IP="127.0.0.1"
fi
cat << EOF | tee -a /lib/systemd/system/rocketchat.service
[Unit]
Description=The Rocket.Chat server
After=network.target remote-fs.target nss-lookup.target mongod.service
[Service]
ExecStart=$NODE_BIN /opt/Rocket.Chat/main.js
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=rocketchat
User=rocketchat
Environment=MONGO_URL=$MONGO_URL
Environment=MONGO_OPLOG_URL=$MONGO_OPLOG_URL
Environment=ROOT_URL=$ROOT_URL
Environment=PORT=$PORT
Environment=BIND_IP=$BIND_IP
Environment=DEPLOY_PLATFORM=rocketchatctl
Environment=REG_TOKEN=$REG_TOKEN
[Install]
WantedBy=multi-user.target
EOF
}
start_rocketchat() {
systemctl daemon-reload
systemctl enable rocketchat
systemctl start rocketchat
}
restart_rocketchat() {
systemctl daemon-reload
systemctl restart rocketchat
}
get_rocketchat_domain() {
rocket_domain=$(echo $ROOT_URL | awk -F[/:] '{print $4}')
}
get_mongo_storage_engine() {
storage=$(mongo --eval "printjson(db.serverStatus().storageEngine)" | grep name | awk -F: '{print $2}' | tr -d , | tr -d \" | tr -d '[:space:]')
}
traefik_config_file() {
if [ -f /etc/traefik/traefik.toml ]; then
sufix=$(date +%s)
cp /etc/traefik/traefik.toml /etc/traefik/traefik.toml.$sufix
if [ $? == 0 ]; then
true > /etc/traefik/traefik.toml
fi
fi
cat << EOF | tee -a /etc/traefik/traefik.toml
defaultEntryPoints = ["https","http"]
[entryPoints]
[entryPoints.http]
address = ":80"
[entryPoints.http.redirect]
entryPoint = "https"
[entryPoints.https]
address = ":443"
[entryPoints.https.tls]
[file]
[backends]
[backends.rocketchat]
[backends.rocketchat.servers.server1]
url = "http://localhost:$PORT"
[frontends]
[frontends.rocketchat]
backend = "rocketchat"
passHostHeader = true
[frontends.rocketchat.routes.route0]
rule = "Host:$rocket_domain"
[acme]
email = "$rocket_mail"
storage = "etc/traefik/acme/acme.json"
caServer = "https://acme-v02.api.letsencrypt.org/directory"
entryPoint = "https"
[acme.httpChallenge]
entryPoint = "http"
[[acme.domains]]
main = "$rocket_domain"
EOF
}
traefik_systemd_file() {
cat << EOF | tee -a /lib/systemd/system/traefik.service
[Unit]
Description=Traefik
Documentation=https://docs.traefik.io
After=network-online.target
AssertFileIsExecutable=/usr/local/bin/traefik
AssertPathExists=/etc/traefik/traefik.toml
[Service]
User=traefik
Group=traefik
AmbientCapabilities=CAP_NET_BIND_SERVICE
CapabilityBoundingSet=CAP_NET_BIND_SERVICE
# configure service behavior
Type=notify
ExecStart=/usr/local/bin/traefik --configFile=/etc/traefik/traefik.toml
Restart=on-abnormal
WatchdogSec=1s
# lock down system access
# prohibit any operating system and configuration modification
ProtectSystem=full
# create separate, new (and empty) /tmp and /var/tmp filesystems
PrivateTmp=true
# make /home directories inaccessible
ProtectHome=true
# turns off access to physical devices (/dev/...)
PrivateDevices=false
# make kernel settings (procfs and sysfs) read-only
#ProtectKernelTunables=true
# make cgroups /sys/fs/cgroup read-only
#ProtectControlGroups=true
# allow writing of acme.json
ReadWritePaths=/etc/traefik/acme/acme.json
ReadWritePaths=/etc/traefik/acme
# depending on log and entrypoint configuration, you may need to allow writing to other paths, too
# limit number of processes in this unit
#LimitNPROC=1
[Install]
WantedBy=multi-user.target
EOF
}
install_traefik() {
curl -L $TRAEFIK_DOWNLOAD_URL -o /tmp/traefik
if [[ $? == 0 ]]; then
mv /tmp/traefik /usr/local/bin/
else
print_download_traefik_error_and_exit
fi
useradd -r -s /bin/false -U -M traefik
chown root:root /usr/local/bin/traefik
chmod 755 /usr/local/bin/traefik
setcap 'cap_net_bind_service=+ep' /usr/local/bin/traefik
mkdir /etc/traefik
mkdir /etc/traefik/acme
touch /etc/traefik/acme/acme.json
chown -R root:root /etc/traefik
chown -R traefik:traefik /etc/traefik/acme
chmod 600 /etc/traefik/acme/acme.json
get_rocketchat_domain
traefik_config_file
chown root:root /etc/traefik/traefik.toml
chmod 644 /etc/traefik/traefik.toml
traefik_systemd_file
chown root:root /lib/systemd/system/traefik.service
chmod 644 /lib/systemd/system/traefik.service
systemctl daemon-reload
systemctl enable traefik.service
systemctl start traefik.service
}
caddy_config_file() {
if [ -f /etc/caddy/Caddyfile ]; then
sufix=$(date +%s)
cp /etc/caddy/Caddyfile /etc/caddy/Caddyfile.$sufix
if [ $? == 0 ]; then
true > /etc/caddy/Caddyfile
fi
fi
cat << EOF | tee -a /etc/caddy/Caddyfile
$ROOT_URL
proxy / localhost:$PORT {
websocket
transparent
}
EOF
}
caddy_systemd_file() {
if [ -f /lib/systemd/system/caddy.service ]; then
sufix=$(date +%s)
cp /lib/systemd/system/caddy.service /etc/caddy/caddy.service.$sufix
if [ $? == 0 ]; then
true > /lib/systemd/system/caddy.service
fi
fi
cat << EOF | tee -a /lib/systemd/system/caddy.service
[Unit]
Description=Caddy HTTP/2 web server
Documentation=https://caddyserver.com/docs
After=network-online.target
Wants=network-online.target systemd-networkd-wait-online.service
[Service]
Restart=on-abnormal
; User and group the process will run as.
User=caddy
Group=caddy
; Letsencrypt-issued certificates will be written to this directory.
Environment=CADDYPATH=/etc/caddy/ssl
; Always set "-root" to something safe in case it gets forgotten in the Caddyfile.
ExecStart=/usr/local/bin/caddy -log stdout -agree=true -conf=/etc/caddy/Caddyfile -email=$rocket_mail
ExecReload=/bin/kill -USR1 \$MAINPID
; Use graceful shutdown with a reasonable timeout
KillMode=mixed
KillSignal=SIGQUIT
TimeoutStopSec=5s
; Limit the number of file descriptors.
LimitNOFILE=1048576
; Unmodified caddy is not expected to use more than that.
LimitNPROC=512
; Use private /tmp and /var/tmp, which are discarded after caddy stops.
PrivateTmp=true
; Use a minimal /dev (May bring additional security if switched to 'true', but it may not work on Raspberry Pi's or other devices, so it has been disabled in this dist.)
PrivateDevices=false
; Hide /home, /root, and /run/user. Nobody will steal your SSH-keys.
ProtectHome=true
; Make /usr, /boot, /etc and possibly some more folders read-only.
ProtectSystem=full
; … except /etc/ssl/caddy, because we want Letsencrypt-certificates there.
; This merely retains r/w access rights, it does not add any new. Must still be writable on the host!
ReadWriteDirectories=/etc/caddy/ssl
; The following additional security directives only work with systemd v229 or later.
; They further restrict privileges that can be gained by caddy. Uncomment if you like.
; Note that you may have to add capabilities required by any plugins in use.
CapabilityBoundingSet=CAP_NET_BIND_SERVICE
AmbientCapabilities=CAP_NET_BIND_SERVICE
;NoNewPrivileges=true
[Install]
WantedBy=multi-user.target
EOF
}
install_caddy() {
curl -L $CADDY_DOWNLOAD_URL -o /tmp/caddy.tar.gz
tar -xzf /tmp/caddy.tar.gz -C /tmp
mv /tmp/caddy /usr/local/bin
rm /tmp/caddy.tar.gz
chown root:root /usr/local/bin/caddy
chmod 755 /usr/local/bin/caddy
setcap cap_net_bind_service=+ep $(which caddy)
mkdir /etc/caddy
mkdir /etc/caddy/ssl
chown -R root:root /etc/caddy
useradd -r -s /bin/false -U -M caddy
chown -R caddy:caddy /etc/caddy/ssl
caddy_config_file
chown root:root /etc/caddy/Caddyfile
chmod 644 /etc/caddy/Caddyfile
caddy_systemd_file
chown root:root /lib/systemd/system/caddy.service
chmod 644 /lib/systemd/system/caddy.service
systemctl daemon-reload
systemctl enable caddy.service
systemctl start caddy.service
}
get_rocketchat_latest_version() {
if ! command_exists curl; then
apt-get install -y curl
fi
latest_rocketchat_version=$(curl $ROCKET_VERSION_INFO_URL 2> /dev/null | grep -w tag | awk -F: '{print $2}' | awk -F'"' '{print $2}')
}
get_rocketchat_current_version() {
if systemctl status rocketchat > /dev/null 2>&1; then
PORT=$(cat /lib/systemd/system/rocketchat.service | grep PORT | awk -F= '{print $3}')
current_rocketchat_version=$(curl http://localhost:$PORT/api/info 2> /dev/null | cut -d\" -f4)
else
print_rocketchat_not_running_error_and_exit
fi
}
version_gt() {
test "$(printf '%s\n' "$@" | sort -V | head -n 1)" != "$1"
}
rocketchat_health_check() {
local count=0
while ! ([ $rocket_healthy -eq 1 ] || [ $count -eq 150 ]); do
get_rocketchat_current_version
[[ $current_rocketchat_version =~ ^[0-9]+\.[0-9]+(\.[0-9]+)?$ ]] && rocket_healthy=1
sleep 2
((count++))
echo "Waiting up to 5 minutes for RocketChat server to be active ... $(($count * 2))"
done
}
do_setup() {
# checks system before install: node, mongo, rocketchat
# assigns user's values
# update environment and install necessary packages
# TODO: add gnupg; find out the package name for deb & rpm
case "$distro" in
ubuntu | debian)
apt update -qqq &&
apt install curl jq build-essential graphicsmagick -yqqq
;;
centos)
yum -y check-update || true
yum install gcc-c++ make curl -y
yum install -y epel-release
yum install GraphicsMagick jq -y
;;
esac
# node
get_supported_node_version
# mongodb
get_supported_mongo_version
# mongo
if command_exists mongod; then
if [[ -n $(pgrep mongod) ]]; then
if ! mongo --eval "printjson(db.getUsers())" | grep -q "Error: command usersInfo requires authentication"; then
get_mongo_storage_engine
if [ $storage == "wiredTiger" ]; then
interactive_mongo
else
print_mongo_storage_engine_error_and_exit
fi
else
print_mongo_connection_failed_error_and_exit
fi
else
print_mongo_connection_failed_error_and_exit
fi
else
# mongodb doesn't exist
mongo_version_supported
install_mongo=1
fi
# rocketchat
systemctl status rocketchat > /dev/null 2>&1
if [ $? -eq 4 ]; then
interactive_rocketchat
else
print_rocketchat_installed_error_and_exit
fi
# webserver
interactive_webserver
interactive_mail
}
do_install_mongo() {
if ((install_mongo)); then
case "$distro" in
ubuntu | debian)
apt_configure_mongo
apt_install_mongo
configure_mongo
create_mongo_systemd_file
initiate_and_start_mongo
;;
centos)
yum_configure_mongo
yum_install_mongo
configure_mongo
create_mongo_systemd_file
initiate_and_start_mongo
;;
*)
print_distro_not_supported_error_and_exit
;;
esac
fi
}
do_install() {
# install node out here since it doesn't depend on the distribution
do_install_node
do_install_mongo
# rocketchat
if ! [ -d ${ROCKETCHAT_DIR} ]; then
install_rocketchat
set_rocketchat_user_and_permissions
create_rocketchat_systemd_file
start_rocketchat
else
print_rocketchat_installed_error_and_exit
fi
# webserver
case "$webserver" in
traefik)
install_traefik
;;
caddy)
install_caddy
;;
esac
print_done
}
do_check_updates() {
get_rocketchat_latest_version
get_rocketchat_current_version
print_make_backup
get_mongo_storage_engine
if ([[ $storage == "mmapv1" ]] && [[ ${latest_rocketchat_version:0:1} -eq 4 ]]); then
print_check_mongo_storage_and_exit
elif version_gt $latest_rocketchat_version $current_rocketchat_version; then
print_update_available_and_exit
elif [ $latest_rocketchat_version == $current_rocketchat_version ]; then
print_rocketchat_in_latest_version_and_exit
else
print_check_updates_error_and_exit
fi
}
do_update() {
do_update_node
mv $ROCKETCHAT_DIR $ROCKETCHAT_DIR_UPDATE
if [ $? == 0 ]; then
systemctl stop rocketchat
install_rocketchat
set_rocketchat_user_and_permissions
systemctl start rocketchat
rocketchat_health_check
if [ $rocket_healthy -eq 1 ]; then
rm -rf $ROCKETCHAT_DIR_UPDATE
echo "RocketChat server updated to latest version :)"
exit 0
else
rm -rf $ROCKETCHAT_DIR && mv $ROCKETCHAT_DIR_UPDATE $ROCKETCHAT_DIR
systemctl restart rocketchat
print_update_install_failed_exit
fi
else
print_update_backup_failed_exit
fi
}
check_node_major_version() {
major_version=${NODE_VERSION%%.*} # Prefix up to the first dot
[[ $(node -v) =~ ^v$major_version\.[0-9]+\.[0-9]+$ ]]
}
do_update_node() {
get_supported_node_version
local currently_used_node_path=$(awk -F'[= ]' '$1 == "ExecStart" {print $2}' /lib/systemd/system/rocketchat.service)
[[ $($currently_used_node_path --version 2> /dev/null) == "v$NODE_VERSION" ]] && return
do_install_node
sed -Ei "s@^(ExecStart)=$currently_used_node_path {1,}(.+)\$@\1=$NODE_BIN \2@" /lib/systemd/system/rocketchat.service
check_node_major_version || print_node_not_updated_error_and_exit
}
do_upgrade_rocketchatctl() {
curl -L $ROCKETCHATCTL_DOWNLOAD_URL -o /tmp/rocketchatctl
if [ $? != 0 ]; then
print_upgrade_download_rocketchatctl_error_and_exit
fi
if cmp -s $ROCKETCHATCTL_DIRECTORY/rocketchatctl /tmp/rocketchatctl; then
rm /tmp/rocketchatctl
echo "rocketchatctl already in latest version."
exit 0
else
mv /tmp/rocketchatctl $ROCKETCHATCTL_DIRECTORY/
chmod 755 $ROCKETCHATCTL_DIRECTORY/rocketchatctl
echo "rocketchatctl upgraded to latest version."
exit 0
fi
}
check_arguments_configure() {
case $1 in
--rocketchat)
configure_rocketchat=1
shift
while [ $# -gt 0 ]; do
IFS='=' read -r -a args <<< $1
case "${args[0]}" in
--root-url)
if [ ${#args[@]} -ne 2 ]; then
error_with_no_value_specified "${args[0]}"
exit 2
fi
ROOT_URL="${args[1]}"
root_url_arg=1
;;
--port)
if [ ${#args[@]} -ne 2 ]; then
error_with_no_value_specified "${args[0]}"
exit 2
fi
PORT="${args[1]}"
;;
--bind-loopback)
if [ ${#args[@]} -ne 2 ]; then
error_with_no_value_specified "${args[0]}"
exit 2
fi
bind_loopback="${args[1]}"
bind_loopback_arg=1
;;
*)
show_help
exit 2
;;
esac
shift
done
;;
--lets-encrypt)
configure_webserver=1
shift
while [ $# -gt 0 ]; do
IFS='=' read -r -a args <<< $1
case "${args[0]}" in
--root-url)
if [ ${#args[@]} -ne 2 ]; then
error_with_no_value_specified "${args[0]}"
exit 2
fi
ROOT_URL="${args[1]}"
root_url_arg=1
;;
--letsencrypt-email)
if [ ${#args[@]} -ne 2 ]; then
error_with_no_value_specified "${args[0]}"
exit 2
fi
rocket_mail="${args[1]}"
rocket_mail_arg=1
;;