forked from sandstorm-io/sandstorm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·2148 lines (1860 loc) · 75.8 KB
/
install.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
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
# This script installs the Sandstorm Personal Cloud Server on your Linux
# machine. You can run the latest installer directly from the web by doing:
#
# curl https://install.sandstorm.io | bash
#
# If `curl|bash` makes you uncomfortable, see other options here:
#
# https://docs.sandstorm.io/en/latest/install/
#
# This script only modifies your system in the following ways:
# - Install Sandstorm into the directory you choose, typically /opt/sandstorm.
# - Optionally add an initscript or systemd service:
# /etc/init.d/sandstorm
# /etc/systemd/system/sandstorm.service
# - Add commands "sandstorm" and "spk" to /usr/local/bin.
#
# Once installed, you may uninstall with the command: sandstorm uninstall
#
# The script will ask you whether you're OK with giving it root privileges.
# If you refuse, the script can still install Sandstorm (to a directory you
# own), but will not be able to install the initscript or shortcut commands,
# and the dev tools will not work (due to limitations with using FUSE in a
# sandbox).
#
# This script downloads and installs binaries. This means that to use this
# script, you need to trust that the authors are not evil, or you must use
# an isolated machine or VM. Of course, since the Sandstorm authors'
# identities are widely known, if they did try to do anything evil, you
# could easily get them arrested. That said, if you'd rather install from
# 100% auditable source code, please check out the Github repository instead.
#
# All downloads occur over HTTPS from Sandstorm's servers and are further
# verified using PGP.
if test -z "$BASH_VERSION"; then
echo "Please run this script using bash, not sh or any other shell." >&2
exit 1
fi
# We wrap the entire script in a big function which we only call at the very end, in order to
# protect against the possibility of the connection dying mid-script. This protects us against
# the problem described in this blog post:
# http://blog.existentialize.com/dont-pipe-to-your-shell.html
_() {
set -euo pipefail
# Declare an array so that we can capture the original arguments.
declare -a ORIGINAL_ARGS
# Allow the environment to override curl's User-Agent parameter. We
# use this to distinguish probably-actual-users installing Sandstorm
# from the automated test suite, which invokes the install script with
# this environment variable set.
CURL_USER_AGENT="${CURL_USER_AGENT:-sandstorm-install-script}"
# Define I/O helper functions.
error() {
if [ $# != 0 ]; then
echo -en '\e[0;31m' >&2
echo "$@" | (fold -s || cat) >&2
echo -en '\e[0m' >&2
fi
}
fail() {
local error_code="$1"
shift
if [ "${SHOW_FAILURE_MSG:-yes}" = "yes" ] ; then
echo "*** INSTALLATION FAILED ***" >&2
echo ""
fi
error "$@"
echo "" >&2
if [ "$error_code" = E_CURL_MISSING ] ; then
# There's no point in asking the user if they want to report an issue, since
# (1) there isn't one, they just need to install curl, and (2) doing so will
# fail anyway, since we use curl to send the report. We've already displayed
# the error, so just exit now.
exit 1
fi
# Users can export REPORT=no to avoid the error-reporting behavior, if they need to.
if [ "${REPORT:-yes}" = "yes" ] ; then
if USE_DEFAULTS=no prompt-yesno "Hmm, installation failed. Would it be OK to send an anonymous error report to the sandstorm.io team so we know something is wrong?
It would only contain this error code: $error_code" "yes" ; then
echo "Sending problem report..." >&2
local BEARER_TOKEN="ZiV1jbwHBPfpIjF3LNFv9-glp53F7KcsvVvljgKxQAL"
local API_ENDPOINT="https://api.oasis.sandstorm.io/api"
local HTTP_STATUS=$(
dotdotdot_curl \
--silent \
--max-time 20 \
--data-binary "{\"error_code\":\"$error_code\",\"user-agent\":\"$CURL_USER_AGENT\"}" \
-H "Authorization: Bearer $BEARER_TOKEN" \
-X POST \
--output "/dev/null" \
-w '%{http_code}' \
"$API_ENDPOINT")
if [ "200" == "$HTTP_STATUS" ] ; then
echo "... problem reported successfully. Your installation did not succeed." >&2
elif [ "000" == "$HTTP_STATUS" ] ; then
error "Submitting error report failed. Maybe there is a connectivity problem."
else
error "Submitting error report resulted in strange HTTP status: $HTTP_STATUS"
fi
else
echo "Not sending report." >&2
fi
echo ""
fi
echo "You can report bugs at: http://github.com/sandstorm-io/sandstorm" >&2
exit 1
}
retryable_curl() {
# This function calls curl to download a file. If the file download fails, it asks the user if it
# is OK to retry.
local CURL_FAILED="no"
curl -A "${CURL_USER_AGENT}" -f "$1" > "$2" || CURL_FAILED="yes"
if [ "yes" = "${CURL_FAILED}" ] ; then
if prompt-yesno "Downloading $1 failed. OK to retry?" "yes" ; then
echo "" >&2
echo "Download failed. Waiting one second before retrying..." >&2
sleep 1
retryable_curl "$1" "$2"
fi
fi
}
dotdotdot_curl() {
# This function calls curl, but first prints "..." to the screen, in
# an attempt to indicate to the user that the script is waiting on
# something.
#
# It then moves the cursor to the start of the line, so that future
# echo-ing will overwrite those dots.
#
# Since the script is -e, and in general we don't have a reliable
# thing that we do in the case that curl exits with a non-zero
# status code, we don't capture the status code; we allow the script
# to abort if curl exits with a non-zero status.
# Functions calling dotdotdot_curl expect to capture curl's own
# stdout. Therefore we do our echo-ing to stderr.
echo -n '...' >&2
curl "$@"
echo -ne '\r' >&2
}
is_port_bound() {
local SCAN_HOST="$1"
local SCAN_PORT="$2"
if [ "${DEV_TCP_USABLE}" = "unchecked" ] ; then
REPORT=no fail "E_DEV_TCP_UNCHECKED" "Programmer error. The author of install.sh used an uninitialized variable."
fi
# We also use timeout(1) from coreutils to avoid this process taking a very long
# time in the case of e.g. weird network rules or something.
if [ "${DEV_TCP_USABLE}" = "yes" ] ; then
if timeout 1 bash -c ": < /dev/tcp/${SCAN_HOST}/${SCAN_PORT}" 2>/dev/null; then
return 0
else
return 1
fi
fi
# If we are using a traditional netcat, then -z (zero i/o mode)
# works for scanning-type uses. (Debian defaults to this.)
#
# If we are using the netcat from the nmap package, then we can use
# --recv-only --send-only to get the same behavior. (Fedora defaults
# to this.)
#
# nc will either:
#
# - return true (exit 0) if it connected to the port, or
#
# - return false (exit 1) if it failed to connect to the port, or
#
# - return false (exit 1) if we are passing it the wrong flags.
#
# So if either if these invocations returns true, then we know the
# port is bound.
local DEBIAN_STYLE_INDICATED_BOUND="no"
${NC_PATH} -z "$SCAN_HOST" "$SCAN_PORT" >/dev/null 2>/dev/null && DEBIAN_STYLE_INDICATED_BOUND=yes
if [ "$DEBIAN_STYLE_INDICATED_BOUND" == "yes" ] ; then
return 0
fi
# Not sure yet. Let's try the nmap-style way.
local NMAP_STYLE_INDICATED_BOUND="no"
${NC_PATH} --wait 1 --recv-only --send-only "$SCAN_HOST" "$SCAN_PORT" >/dev/null 2>/dev/null && \
NMAP_STYLE_INDICATED_BOUND=yes
if [ "$NMAP_STYLE_INDICATED_BOUND" == "yes" ] ; then
return 0
fi
# As far as we can tell, nmap can't connect to the port, so return 1
# to indicate it is not bound.
return 1
}
# writeConfig takes a list of shell variable names and saves them, and
# their contents, to stdout. Therefore, the caller should redirect its
# output to a config file.
writeConfig() {
while [ $# -gt 0 ]; do
eval echo "$1=\$$1"
shift
done
}
prompt() {
local VALUE
# Hack: We read from FD 3 because when reading the script from a pipe, FD 0 is the script, not
# the terminal. We checked above that FD 1 (stdout) is in fact a terminal and then dup it to
# FD 3, thus we can input from FD 3 here.
if [ "yes" = "$USE_DEFAULTS" ] ; then
# Print the default.
echo "$2"
return
fi
# We use "bold", rather than any particular color, to maximize readability. See #2037.
echo -en '\e[1m' >&3
echo -n "$1 [$2]" >&3
echo -en '\e[0m ' >&3
read -u 3 VALUE
if [ -z "$VALUE" ]; then
VALUE=$2
fi
echo "$VALUE"
}
prompt-numeric() {
local NUMERIC_REGEX="^[0-9]+$"
while true; do
local VALUE=$(prompt "$@")
if ! [[ "$VALUE" =~ $NUMERIC_REGEX ]] ; then
echo "You entered '$VALUE'. Please enter a number." >&3
else
echo "$VALUE"
return
fi
done
}
prompt-yesno() {
while true; do
local VALUE=$(prompt "$@")
case $VALUE in
y | Y | yes | YES | Yes )
return 0
;;
n | N | no | NO | No )
return 1
;;
esac
echo "*** Please answer \"yes\" or \"no\"."
done
}
# Define global variables that the install script will use to mark its
# own progress.
USE_DEFAULTS="no"
USE_EXTERNAL_INTERFACE="no"
USE_SANDCATS="no"
SANDCATS_SUCCESSFUL="no"
USE_HTTPS="no"
CURRENTLY_UID_ZERO="no"
PREFER_ROOT="yes"
SHOW_MESSAGE_ABOUT_NEEDING_PORTS_OPEN="no"
STARTED_SANDSTORM="no"
# Allow the test suite to override the path to netcat in order to
# reproduce a compatibility issue between different nc versions.
NC_PATH="${OVERRIDE_NC_PATH:-nc}"
# Allow install.sh to store if bash /dev/tcp works.
DEV_TCP_USABLE="unchecked"
# Defaults for some config options, so that if the user requests no
# prompting, they get these values.
DEFAULT_DIR_FOR_ROOT="${OVERRIDE_SANDSTORM_DEFAULT_DIR:-/opt/sandstorm}"
DEFAULT_DIR_FOR_NON_ROOT="${OVERRIDE_SANDSTORM_DEFAULT_DIR:-${HOME:-opt}/sandstorm}"
DEFAULT_SMTP_PORT="30025"
DEFAULT_UPDATE_CHANNEL="dev"
DEFAULT_SERVER_USER="${OVERRIDE_SANDSTORM_DEFAULT_SERVER_USER:-sandstorm}"
SANDCATS_BASE_DOMAIN="${OVERRIDE_SANDCATS_BASE_DOMAIN:-sandcats.io}"
ALLOW_DEV_ACCOUNTS="false"
SANDCATS_GETCERTIFICATE="${OVERRIDE_SANDCATS_GETCERTIFICATE:-yes}"
# Define functions for each stage of the install process.
usage() {
echo "usage: $SCRIPT_NAME [-d] [-e] [-p PORT_NUMBER] [-u] [<bundle>]" >&2
echo "If <bundle> is provided, it must be the name of a Sandstorm bundle file," >&2
echo "like 'sandstorm-123.tar.xz', which will be installed. Otherwise, the script" >&2
echo "downloads a bundle from the internet via HTTPS." >&2
echo '' >&2
echo 'If -d is specified, the auto-installs with defaults suitable for app development.' >&2
echo 'If -e is specified, default to listening on an external interface, not merely loopback.' >&2
echo 'If -i is specified, default to (i)nsecure mode where we do not request a HTTPS certificate.' >&2
echo 'If -p is specified, use its argument (PORT_NUMBER) as the default port for HTTP. Otherwise, use 6080. Note that if the install script enables HTTPS, it will use 443 instead!'
echo 'If -u is specified, default to avoiding root priviliges. Note that the dev tools only work if the server has root privileges.' >&2
exit 1
}
detect_current_uid() {
if [ $(id -u) = 0 ]; then
CURRENTLY_UID_ZERO="yes"
fi
}
disable_smtp_port_25_if_port_unavailable() {
PORT_25_AVAILABLE="no"
if is_port_bound 0.0.0.0 25; then
return
fi
if is_port_bound 127.0.0.1 25; then
return
fi
PORT_25_AVAILABLE="yes"
}
disable_https_if_ports_unavailable() {
# If port 80 and 443 are both available, then let's use DEFAULT_PORT=80. This value is what the
# Sandstorm installer will write to PORT= in the Sandstorm configuration file.
#
# If either 80 or 443 is not available, then we set SANDCATS_GETCERTIFICATE to no.
#
# From the rest of the installer's perspective, if SANDCATS_GETCERTIFICATE is yes, it is safe to
# bind to port 443.
#
# There is a theoretical race condition here. I think that's life.
#
# This also means that if a user has port 443 taken but port 80 available, we will use port 6080
# as the default port. If the user wants to override that, they can run install.sh with "-p 80".
local PORT_80_AVAILABLE="no"
is_port_bound 0.0.0.0 80 || PORT_80_AVAILABLE="yes"
local PORT_443_AVAILABLE="no"
is_port_bound 0.0.0.0 443 || PORT_443_AVAILABLE="yes"
if [ "$PORT_443_AVAILABLE" == "no" -o "$PORT_80_AVAILABLE" == "no" ] ; then
SANDCATS_GETCERTIFICATE="no"
SHOW_MESSAGE_ABOUT_NEEDING_PORTS_OPEN="yes"
fi
}
handle_args() {
SCRIPT_NAME=$1
shift
while getopts ":deiup:" opt; do
case $opt in
d)
USE_DEFAULTS="yes"
;;
e)
USE_EXTERNAL_INTERFACE="yes"
;;
i)
SANDCATS_GETCERTIFICATE="no"
;;
u)
PREFER_ROOT=no
;;
p)
DEFAULT_PORT="${OPTARG}"
;;
*)
usage
;;
esac
done
# If DEFAULT_PORT didn't get set above, set it to 6080 here.
DEFAULT_PORT="${DEFAULT_PORT:-6080}"
# Keep a copy of the ORIGINAL_ARGS so that, when re-execing ourself,
# we can pass them in.
ORIGINAL_ARGS=("$@")
# Pass positional parameters through
shift "$((OPTIND - 1))"
if [ $# = 1 ] && [[ ! $1 =~ ^- ]]; then
BUNDLE_FILE="$1"
elif [ $# != 0 ]; then
usage
fi
}
rerun_script_as_root() {
# Note: This function assumes that the caller has requested
# permission to use sudo!
# Pass $@ here to enable the caller to provide environment
# variables to bash, which will affect the execution plan of
# the resulting install script run.
# Remove newlines in $@, otherwise when we try to use $@ in a string passed
# to 'bash -c' the command gets cut off at the newline. ($@ contains newlines
# because at the call site we used escaped newlines for readability.)
local ENVVARS=$(echo $@)
# Add CURL_USER_AGENT to ENVVARS, since we always need to pass this
# through.
ENVVARS="$ENVVARS CURL_USER_AGENT=$CURL_USER_AGENT"
if [ "$(basename $SCRIPT_NAME)" == bash ]; then
# Probably ran like "curl https://sandstorm.io/install.sh | bash"
echo "Re-running script as root..."
exec sudo bash -euo pipefail -c "curl -fs -A $CURL_USER_AGENT https://install.sandstorm.io | $ENVVARS bash"
elif [ "$(basename $SCRIPT_NAME)" == install.sh ] && [ -e "$0" ]; then
# Probably ran like "bash install.sh" or "./install.sh".
echo "Re-running script as root..."
if [ ${#ORIGINAL_ARGS[@]} = 0 ]; then
exec sudo $ENVVARS bash "$SCRIPT_NAME"
else
exec sudo $ENVVARS bash "$SCRIPT_NAME" "${ORIGINAL_ARGS[@]}"
fi
fi
# Don't know how to run the script. Let the user figure it out.
REPORT=no fail "E_CANT_SWITCH_TO_ROOT" "ERROR: This script could not detect its own filename, so could not switch to root. \
Please download a copy and name it 'install.sh' and run that as root, perhaps using sudo. \
Try this command:
curl https://install.sandstorm.io/ > install.sh && sudo bash install.sh"
}
set_umask() {
# Use umask 0022, to minimize how much 'mkdir -m' we have to do, etc. See #2300.
umask 0022
}
assert_on_terminal() {
if [ "no" = "$USE_DEFAULTS" ] && [ ! -t 1 ]; then
REPORT=no fail "E_NO_TTY" "This script is interactive. Please run it on a terminal."
fi
# Hack: If the script is being read in from a pipe, then FD 0 is not the terminal input. But we
# need input from the user! We just verified that FD 1 is a terminal, therefore we expect that
# we can actually read from it instead. However, "read -u 1" in a script results in
# "Bad file descriptor", even though it clearly isn't bad (weirdly, in an interactive shell,
# "read -u 1" works fine). So, we clone FD 1 to FD 3 and then use that -- bash seems OK with
# this.
exec 3<&1
}
assert_linux_x86_64() {
if [ "$(uname)" != Linux ]; then
fail "E_NON_LINUX" "Sandstorm requires Linux. If you want to run Sandstorm on a Windows or
Mac system, you can use Vagrant or another virtualization tool. See our install documentation:
- https://docs.sandstorm.io/en/latest/install/"
fi
if [ "$(uname -m)" != x86_64 ]; then
fail "E_NON_X86_64" "Sorry, the Sandstorm server currently only runs on x86_64 machines."
fi
}
assert_usable_kernel() {
KVERSION=( $(uname -r | grep -o '^[0-9.]*' | tr . ' ') )
if (( KVERSION[0] < 3 || (KVERSION[0] == 3 && KVERSION[1] < 10) )); then
error "Detected Linux kernel version: $(uname -r)"
fail "E_KERNEL_OLDER_THAN_310" "Sorry, your kernel is too old to run Sandstorm. We require kernel" \
"version 3.10 or newer."
fi
}
maybe_enable_userns_sysctl() {
# This function enables the Debian/Ubuntu-specific unprivileged
# userns sysctl, if the system has it and we want it.
if [ "$USE_DEFAULTS" != "yes" ] ; then
# Only do this when -d is passed. -d means "use defaults suitable for app development", and
# we want userns enabled for app development if possible since it enables UID randomization
# which helps catch app bugs. For the rest of the world, we're fine using the privileged
# sandbox instead.
return
fi
if [ "no" = "$CURRENTLY_UID_ZERO" ] ; then
# Not root. Can't do anything about it.
return
fi
if [ ! -e /proc/sys/kernel/unprivileged_userns_clone ]; then
# No such sysctl on this system.
return
fi
local OLD_VALUE="$(< /proc/sys/kernel/unprivileged_userns_clone)"
if [ "$OLD_VALUE" = "1" ]; then
# Already enabled.
return
fi
# Enable it.
if sysctl -wq kernel.unprivileged_userns_clone=1 2>/dev/null; then
echo "NOTE: Enabled unprivileged user namespaces because you passed -d."
else
# Apparently we can't. Maybe we're in a Docker container. Give up and use privileged sandbox.
return
fi
# Also make sure it is re-enabled on boot. If sysctl.d exists, we drop our own config in there.
# Otherwise we edit sysctl.conf, but that's less polite.
local SYSCTL_FILENAME="/etc/sysctl.conf"
if [ -d /etc/sysctl.d ] ; then
SYSCTL_FILENAME="/etc/sysctl.d/50-sandstorm.conf"
fi
if ! cat >> "$SYSCTL_FILENAME" << __EOF__
# Enable non-root users to create sandboxes (needed by Sandstorm).
kernel.unprivileged_userns_clone = 1
__EOF__
then
# We couldn't make the change permanent, so undo the change. Probably everything will work
# fine with the privileged sandbox. But if it doesn't, it's better that things fail now rather
# than wait for a reboot.
echo "NOTE: Never mind, not enabling userns because can't write /etc/sysctl.d."
sysctl -wq "kernel.unprivileged_userns_clone=$OLD_VALUE" || true
return
fi
}
test_if_dev_tcp_works() {
# In is_port_bound(), we prefer to use bash /dev/tcp to check if the port is bound. This is
# available on most Linux distributions, but it is a compile-time flag for bash and at least
# Debian historically disabled it.
#
# To test availability, we connect to localhost port 0, which is never available, hoping for a
# TCP-related error message from bash. We use a subshell here because we don't care that timeout
# will return false; we care if the grep returns false.
if (timeout 1 bash -c ': < /dev/tcp/localhost/0' 2>&1 || true) | grep -q 'connect:' ; then
# Good! bash should get "Connection refused" on this, and this message is prefixed
# by the syscall it was trying to do, so therefore it tried to connect!
DEV_TCP_USABLE="yes"
else
DEV_TCP_USABLE="no"
fi
}
assert_dependencies() {
if [ -z "${BUNDLE_FILE:-}" ]; then
which curl > /dev/null|| fail "E_CURL_MISSING" "Please install curl(1). Sandstorm uses it to download updates."
fi
# To find out if port 80 and 443 are available, we need a working bash /dev/net or `nc` on
# the path.
if [ "${DEV_TCP_USABLE}" = "unchecked" ] ; then
test_if_dev_tcp_works
fi
if [ "${DEV_TCP_USABLE}" = "no" ] ; then
which nc > /dev/null || fail "E_NC_MISSING" "Please install nc(1). (Package may be called 'netcat-traditional' or 'netcat-openbsd'.)"
fi
which tar > /dev/null || fail "E_TAR_MISSING" "Please install tar(1)."
which xz > /dev/null || fail "E_XZ_MISSING" "Please install xz(1). (Package may be called 'xz-utils'.)"
}
assert_valid_bundle_file() {
# ========================================================================================
# Validate bundle file, if provided
if [ -n "${BUNDLE_FILE:-}" ]; then
# Read the first filename out of the bundle, which should be the root directory name.
# We use "|| true" here because tar is going to SIGPIPE when `head` exits.
BUNDLE_DIR=$( (tar Jtf "$BUNDLE_FILE" || true) | head -n 1)
if [[ ! "$BUNDLE_DIR" =~ sandstorm-([0-9]+)/ ]]; then
fail "E_INVALID_BUNDLE" "$BUNDLE_FILE: Not a valid Sandstorm bundle"
fi
BUILD=${BASH_REMATCH[1]}
# We're going to change directory, so note the bundle's full name.
BUNDLE_FILE=$(readlink -f "$BUNDLE_FILE")
fi
}
detect_init_system() {
# We start out by not knowing which init system is in use.
INIT_SYSTEM="unknown"
# We look for systemd, since we have a nice way to generate a unit file.
if grep -q systemd /proc/1/comm; then
INIT_SYSTEM="systemd"
return
fi
# We look for sysvinit, as a convenient fallback. Note that this
# should work fine with Upstart (on e.g. Ubuntu 14.04), too.
if [ -e /etc/init.d ]; then
INIT_SYSTEM="sysvinit"
return
fi
# If we got this far, and we couldn't figure out the init system
# in use, that's life.
}
choose_install_mode() {
echo -n 'Sandstorm makes it easy to run web apps on your own server. '
if [ "yes" = "$USE_DEFAULTS" ] ; then
CHOSEN_INSTALL_MODE="${CHOSEN_INSTALL_MODE:-2}" # dev server mode by default
fi
if [ "no" = "${PREFER_ROOT:-}" ] ; then
echo ""
echo "NOTE: Showing you all options, including development options, but omitting "
echo " init script automation, because you chose to install without using root."
CHOSEN_INSTALL_MODE="${CHOSEN_INSTALL_MODE:-2}" # dev server mode by default
fi
if [ -z "${CHOSEN_INSTALL_MODE:-}" ]; then
echo "You can have:"
echo ""
echo "1. A typical install, to use Sandstorm (press enter to accept this default)"
echo "2. A development server, for working on Sandstorm itself or localhost-based app development"
echo ""
CHOSEN_INSTALL_MODE=$(prompt-numeric "How are you going to use this Sandstorm install?" "1")
fi
if [ "1" = "$CHOSEN_INSTALL_MODE" ] ; then
assert_full_server_dependencies
full_server_install
else
dev_server_install
fi
}
assert_full_server_dependencies() {
# To set up sandcats, we need `openssl` on the path. Check for that,
# and if it is missing, bail out and tell the user they have to
# install it.
which openssl > /dev/null|| fail "E_OPENSSL_MISSING" "Please install openssl(1). Sandstorm uses it for the Sandcats.io dynamic DNS service."
}
dev_server_install() {
# Use these settings for a dev-server-oriented install.
#
# Users will find themselves going through this flow if they
# manually choose a dev-server-related flow, but also if they pass
# -d on the command line. (The Vagrantfile and the test suite both
# use -d. The test suite runs install.sh with -d -u.)
#
# A "dev server install" must be run as root, unless you pass
# -u. That's because app development (aka spk dev) requires running
# as root, at the moment.
if [ "yes" = "$PREFER_ROOT" ] && [ "no" = "$CURRENTLY_UID_ZERO" ] ; then
# We are not root, but we would like to be root.
echo "If you want app developer mode for a Sandstorm install, you need root"
echo "due to limitations in the Linux kernel."
echo ""
echo "To set up Sandstorm, we will use sudo to switch to root, then"
echo "provide further information before doing the install."
echo "Sandstorm's database and web interface won't run as root."
# If we are running in USE_DEFAULTS mode, then it is not OK to ask
# for permission to use sudo.
if [ "yes" = "$USE_DEFAULTS" ] ; then
ACCEPTED_SUDO_FOR_DEV_SERVER="no"
else
if prompt-yesno "OK to continue?" "yes" ; then
ACCEPTED_SUDO_FOR_DEV_SERVER="yes"
else
ACCEPTED_SUDO_FOR_DEV_SERVER="no"
fi
fi
if [ "yes" = "$ACCEPTED_SUDO_FOR_DEV_SERVER" ] ; then
rerun_script_as_root CHOSEN_INSTALL_MODE=2
else
# Print a message that allows people to make an informed decision.
SHOW_FAILURE_MSG=no REPORT=no fail "E_NEED_ROOT" "
One development feature does require root. To install anyway, run:
install.sh -u
to install without using root access. In that case, Sandstorm will operate OK but package tracing ('spk dev') will not work."
fi
fi
# If they did not pass -d, then let them opt into that, but only if
# PREFER_ROOT is still enabled.
#
# If they pass -u without -d, then they can answer the questions one
# by one.
if [ "yes" != "$USE_DEFAULTS" ] && [ "yes" = "$PREFER_ROOT" ] ; then
echo "We're going to:"
echo ""
echo "* Install Sandstorm in ${DEFAULT_DIR_FOR_ROOT}."
echo "* Automatically keep Sandstorm up-to-date (with signed updates)."
echo "* Create a service user ($DEFAULT_SERVER_USER) that owns Sandstorm's files."
if [ -n "${SUDO_USER:-}" ]; then
echo "* Add you ($SUDO_USER) to the $DEFAULT_SERVER_USER group so you can read/write app data."
fi
echo "* Expose the service only on localhost aka local.sandstorm.io, not the public Internet."
echo "* Enable 'dev accounts', for easy developer login."
if [ "unknown" == "$INIT_SYSTEM" ]; then
echo "*** WARNING: Could not detect how to run Sandstorm at startup on your system. ***"
else
echo "* Configure Sandstorm to start on system boot (with $INIT_SYSTEM)."
fi
echo "* Listen for inbound email on port ${DEFAULT_SMTP_PORT}."
echo ""
if prompt-yesno "Press enter to accept defaults. Type 'no' to customize." "yes" ; then
USE_DEFAULTS="yes"
else
echo ""
echo "OK. We will prompt you with every question."
echo ""
fi
fi
if [ "yes" = "$USE_DEFAULTS" ] ; then
# Use the default UPDATE_CHANNEL for auto-updates.
UPDATE_CHANNEL="$DEFAULT_UPDATE_CHANNEL"
# Bind to localhost, unless -e specified in argv.
USE_EXTERNAL_INTERFACE="${USE_EXTERNAL_INTERFACE:-no}"
# Use local.sandstorm.io as hostname unless environment variable declared otherwise. This
# short-circuits the code elsewhere that uses the system hostname if USE_EXTERNAL_INTERFACE is
# "yes".
SS_HOSTNAME="${SS_HOSTNAME:-local.sandstorm.io}"
# Use 30025 as the default SMTP_LISTEN_PORT.
SMTP_LISTEN_PORT="${DEFAULT_SMTP_PORT}"
# Start the service at boot, if we can.
START_AT_BOOT="yes"
# Do not ask questions about our dynamic DNS service.
USE_SANDCATS="no"
# Reasonable default ports.
PORT="${DEFAULT_PORT}"
# Allow the mongo prompting part to determine a reasonable MONGO_PORT.
# Use the ALLOW_DEV_ACCOUNTS feature, which allows people to log
# into a Sandstorm instance without setting up any accounts.
ALLOW_DEV_ACCOUNTS="yes"
# Do not bother setting a DESIRED_SERVER_USER. This way, the
# existing prompting will pick if this should be "sandstorm" (which
# it should be if we're running the install script as root) or the
# currently-logged-in user (which it should be if we're not root).
# Do not bother setting a DIR. This way, the existing prompting will
# pick between /opt/sandstorm and $HOME/sandstorm, depending on if
# the install is being done as root or not. It will use /opt/sandstorm
# in all cases if the script is run without the HOME environment variable.
fi
}
full_server_install() {
# The full server install assumes you are OK with using root. If
# you're not, you should choose the development server and customize
# it to your heart's content.
if [ "yes" != "${PREFER_ROOT}" ] ; then
REPORT=no fail "E_AUTO_NEEDS_SUDO" "The automatic setup process requires sudo. Try again with option 2, development server, to customize."
fi
if [ "yes" = "$USE_DEFAULTS" ] ; then
if [ -z "${DESIRED_SANDCATS_NAME-}" ] ; then
local MSG="For now, USE_DEFAULTS for full server installs requires a DESIRED_SANDCATS_NAME variable."
MSG="$MSG If you need support for non-sandcats full-server unattended installs, please file a bug."
fail "E_USE_DEFAULTS_NEEDS_DESIRED_SANDCATS_NAME" "$MSG"
else
if [ -z "${SANDCATS_DOMAIN_RESERVATION_TOKEN:-}" ] ; then
local MSG="When operating in USE_DEFAULTS mode, if you want a sandcats.io domain,"
MSG="$MSG you must pre-reserve it before running this script. Specify it via the"
MSG="$MSG SANDCATS_DOMAIN_RESERVATION_TOKEN environment variable."
fail "E_USE_DEFAULTS_NEEDS_DESIRED_SANDCATS_NAME" "$MSG"
fi
fi
# If they said USE_DEFAULTS then they don't need to be prompted.
ACCEPTED_FULL_SERVER_INSTALL="yes"
fi
# Use port 25 for email, if we can. This logic only gets executed for "full servers."
disable_smtp_port_25_if_port_unavailable
local PLANNED_SMTP_PORT="30025"
if [ "yes" = "$PORT_25_AVAILABLE" ] ; then
PLANNED_SMTP_PORT="25"
fi
if [ "yes" != "${ACCEPTED_FULL_SERVER_INSTALL:-}" ]; then
# Disable Sandcats HTTPS if ports 80 or 443 aren't available.
disable_https_if_ports_unavailable
echo "We're going to:"
echo ""
echo "* Install Sandstorm in $DEFAULT_DIR_FOR_ROOT"
echo "* Automatically keep Sandstorm up-to-date"
if [ "yes" == "$SANDCATS_GETCERTIFICATE" ] ; then
echo "* Configure auto-renewing HTTPS if you use a subdomain of sandcats.io"
fi
echo "* Create a service user ($DEFAULT_SERVER_USER) that owns Sandstorm's files"
if [ "unknown" == "$INIT_SYSTEM" ]; then
echo "*** WARNING: Could not detect how to run Sandstorm at startup on your system. ***"
else
echo "* Configure Sandstorm to start on system boot (with $INIT_SYSTEM)"
fi
echo "* Listen for inbound email on port ${PLANNED_SMTP_PORT}."
echo ""
# If we're not root, we will ask if it's OK to use sudo.
if [ "yes" != "$CURRENTLY_UID_ZERO" ]; then
echo "To set up Sandstorm, we will need to use sudo."
else
echo "Rest assured that Sandstorm itself won't run as root."
fi
if prompt-yesno "OK to continue?" "yes"; then
ACCEPTED_FULL_SERVER_INSTALL=yes
else
ACCEPTED_FULL_SERVER_INSTALL=no
fi
if [ "yes" = "$ACCEPTED_FULL_SERVER_INSTALL" ] &&
[ "yes" = "$SHOW_MESSAGE_ABOUT_NEEDING_PORTS_OPEN" ] ; then
echo ""
echo "NOTE: It looks like your system already has some other web server installed"
echo " (port 80 and/or 443 are taken), so Sandstorm cannot act as your main"
echo " web server."
echo ""
echo " This script can set up Sandstorm to run on port $DEFAULT_PORT instead,"
echo " without HTTPS. This makes sense if you're OK with typing the port number"
echo " into your browser whenever you access Sandstorm and you don't need"
echo " security. This also makes sense if you are going to set up a reverse proxy;"
echo " if so, see https://docs.sandstorm.io/en/latest/administering/reverse-proxy/"
echo ""
echo " If you want, you can quit this script with Ctrl-C now, and go uninstall"
echo " your other web server, and then run this script again. It is also OK to"
echo " proceed if you want."
echo ""
if ! prompt-yesno "OK to skip automatic HTTPS setup & bind to port $DEFAULT_PORT instead?" "yes" ; then
fail "E_USER_REFUSED_DEFAULT_PORT" "Exiting now. You can re-run the installer whenever you are ready."
fi
fi
# If they are OK continuing, and the script is not running as root
# at the moment, then re-run ourselves as root.
#
# Pass along enough information so that the script will keep
# executing smoothly, so the user doesn't have to re-answer
# questions.
if [ "yes" != "$CURRENTLY_UID_ZERO" ] ; then
if [ "yes" = "$ACCEPTED_FULL_SERVER_INSTALL" ] ; then
rerun_script_as_root CHOSEN_INSTALL_MODE=1 \
ACCEPTED_FULL_SERVER_INSTALL=yes \
OVERRIDE_SANDCATS_BASE_DOMAIN="${OVERRIDE_SANDCATS_BASE_DOMAIN:-}" \
OVERRIDE_SANDCATS_API_BASE="${OVERRIDE_SANDCATS_API_BASE:-}" \
OVERRIDE_SANDCATS_GETCERTIFICATE="${SANDCATS_GETCERTIFICATE}" \
OVERRIDE_NC_PATH="${OVERRIDE_NC_PATH:-}" \
OVERRIDE_SANDCATS_CURL_PARAMS="${OVERRIDE_SANDCATS_CURL_PARAMS:-}"
fi
# If we're still around, it means they declined to run us as root.
echo ""
echo "The automatic setup script needs root in order to:"
echo "* Create a separate user to run Sandstorm as, and"
echo "* Set up Sandstorm to start on system boot."
echo ""
fail "E_DECLINED_AUTO_SETUP_DETAILS" "For a customized install, please re-run install.sh, and choose option (2) "\
"to do a development install."
fi
fi
# Accepting this indicates a few things.
if [ "yes" = "${ACCEPTED_FULL_SERVER_INSTALL}" ]; then
UPDATE_CHANNEL="$DEFAULT_UPDATE_CHANNEL"
DIR="$DEFAULT_DIR_FOR_ROOT"
USE_EXTERNAL_INTERFACE="yes"
USE_SANDCATS="yes"
START_AT_BOOT="yes"
DESIRED_SERVER_USER="$DEFAULT_SERVER_USER"
PORT="${DEFAULT_PORT}"
MONGO_PORT="6081"
SMTP_LISTEN_PORT="${PLANNED_SMTP_PORT}"
else
REPORT=no fail "E_USER_WANTS_CUSTOM_SETTINGS" "If you prefer a more manual setup experience, try installing in development mode."
fi
}
sandcats_configure() {
# We generate the public key before prompting for a desired hostname
# so that when the user presses enter, we can try to register the
# hostname, and if that succeeds, we are totally done. This avoids a
# possible time-of-check-time-of-use race.
echo -n "As a Sandstorm user, you are invited to use a free Internet hostname "
echo "as a subdomain of sandcats.io,"
echo "a service operated by the Sandstorm development team."
sandcats_generate_keys
echo ""
echo "Sandcats.io protects your privacy and is subject to terms of use. By using it,"
echo "you agree to the terms of service & privacy policy available here:"
echo "https://sandcats.io/terms https://sandcats.io/privacy"
echo ""
# Having set up the keys, we run the function to register a name
# with Sandcats. This function handles tail-recursing itself until
# it succeeds and/or returning when the user expresses a desire to
# cancel the process.
sandcats_register_name
}
configure_hostnames() {
if [ "yes" = "$USE_SANDCATS" ] ; then
# If we're lucky, the user will be happy with the Sandcats
# hostname configuration. If not, then we'll have to actually
# prompt them.
sandcats_configure
fi
# Ask the user for port number information. (These functions
# optionally skip the questions if the details have already been
# filled in.)
choose_port
choose_mongo_port
# If we are supposed to use the external network interface, then
# configure the hostname and IP address accordingly.
if [ "yes" = "$USE_EXTERNAL_INTERFACE" ]; then
BIND_IP=0.0.0.0
SS_HOSTNAME="${SS_HOSTNAME:-$(hostname -f 2>/dev/null || hostname)}"
else
BIND_IP=127.0.0.1
SS_HOSTNAME=local.sandstorm.io
if [ "yes" != "$USE_DEFAULTS" ] ; then
echo "Note: local.sandstorm.io maps to 127.0.0.1, i.e. your local machine."
echo "For reasons that will become clear in the next step, you should use this"
echo "instead of 'localhost'."
fi
fi
# A typical server's DEFAULT_BASE_URL is its hostname plus port over HTTP. If the port is 80, then
# don't add it to BASE_URL to avoid triggering this bug:
# https://github.com/sandstorm-io/sandstorm/issues/2252
local PORT_SUFFIX=""
if [ "$PORT" = "80" ] ; then
PORT_SUFFIX=""
else
PORT_SUFFIX=":${PORT}"
fi
DEFAULT_BASE_URL="http://${SS_HOSTNAME}${PORT_SUFFIX}"
if [ "$USE_HTTPS" = "yes" ]; then
DEFAULT_BASE_URL="https://$SS_HOSTNAME"
HTTPS_PORT=443
PORT=80
fi
if [ "yes" = "$SANDCATS_SUCCESSFUL" ] ; then
# Do not prompt for BASE_URL configuration if Sandcats bringup
# succeeded.
BASE_URL="$DEFAULT_BASE_URL"
else
BASE_URL=$(prompt "URL users will enter in browser:" "$DEFAULT_BASE_URL")
if ! [[ "$BASE_URL" =~ ^http(s?):// ]] ; then
local PROPOSED_BASE_URL="http://${BASE_URL}"
echo "** You entered ${BASE_URL}, which needs http:// at the front. I can use:" >&2
echo " ${PROPOSED_BASE_URL}" >&2