Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
427 lines (391 loc) · 17.2 KB
/
Copy pathinstall.sh
File metadata and controls
427 lines (391 loc) · 17.2 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
#!/bin/sh
# Installs or updates selfhostit apps on this machine with Docker: a Raspberry Pi,
# a Linux server or VPS, or a NAS with SSH and Docker (Synology, QNAP, Unraid, ...).
#
# curl -fsSL https://github.com/selfhostit/docs/releases/latest/download/install.sh | sh
#
# That is the newest release of this installer. To read and check it before running it,
# download it first (curl -fsSLO <the same URL>), then run `sh install.sh`; security.md
# says what it does step by step and how to verify it.
#
# It installs a fixed release of each app, listed below with the SHA-256 of every file
# it downloads for it, and stops if a download doesn't match.
#
# It asks which apps you want, gives each one its own folder and port (the next
# free one when another program has the usual port), and runs
# that app's own installer (or its compose file). Then it can put them all on
# your Tailscale network with https:// addresses, which phones need: you sign in
# with a link, no keys or settings. Run it again to update: every app keeps its
# settings and data.
#
# Pick the apps without the menu:
# curl -fsSL .../install.sh | sh -s -- splitwiz flowy
# Other settings, all optional: SELFHOSTIT_DIR (the folder that holds one folder
# per app), TZ, and TS_AUTHKEY (a Tailscale auth key, to sign in without the link).
set -eu
apps="splitwiz should-we flowy troye shoppa calio reckon"
# name, port, what it does
info() {
case $1 in
splitwiz) echo "8090|Split shared costs with your partner or roommate" ;;
should-we) echo "8091|Pick one option together, as a group" ;;
flowy) echo "8092|A private period and cycle tracker" ;;
troye) echo "8093|A private calorie and weight tracker" ;;
shoppa) echo "8094|A shared shopping list for your household" ;;
calio) echo "8095|One calendar for your household: plans, pet sitting, trips" ;;
reckon) echo "8096|Practice for the numbers tests in job applications" ;;
*) return 1 ;;
esac
}
say() { printf '\033[1m%s\033[0m\n' "$*"; }
die() { printf 'Error: %s\n' "$*" >&2; exit 1; }
# interactive: true when there is a terminal to ask questions on (even with curl | sh)
interactive() { (: </dev/tty) 2>/dev/null; }
# ask "Question" default -> prints the answer
ask() {
if interactive; then
printf '%s [%s]: ' "$1" "$2" >/dev/tty
read -r answer </dev/tty || answer=
printf '%s\n' "${answer:-$2}"
else
printf '%s\n' "$2"
fi
}
fetch() {
if command -v curl >/dev/null; then curl -fsSL "$1"; else wget -qO- "$1"; fi
}
# --- Pinned releases ------------------------------------------------------------
# The release of each app this installer sets up, and the SHA-256 of every file it
# downloads from it: app, tag, file, SHA-256. scripts/pin-apps.sh writes this list
# after a release, and CI checks it against GitHub.
pins() {
cat <<-'EOF'
# pins start
splitwiz v1.0.6 scripts/install.sh 52b13f22478f03235122445f5e5af4e9a77c25b73973ae4e6c84e7a410f00592
splitwiz v1.0.6 docker-compose.yml 2daf2dd844f7766bbe1e145a4954f673299d0ede595d43d748c526a331cec752
should-we v2.2.6 docker-compose.yml 633a2e85d6e452107137797970f2ff841cebf3d6734f00cf1d941ccdbc48cc65
flowy v1.0.6 deploy/install.sh 8837d27156bbc06e0add12b73a7b0b95ce39734be0a744b7fbd771d12e3ad2ea
flowy v1.0.6 deploy/docker-compose.yml 168fd2068af64e78f6e974362fa9600cda5e56fbac732efc374ce641f5352a01
flowy v1.0.6 deploy/docker-compose.tailscale.yml 2d63ea7b9471d189d73cd48519a7b7421447595c106d89f746212f3393920ada
flowy v1.0.6 deploy/tailscale-serve.json e8340fb636e47ddd1c3bd6709d39303d32ab9aa2eecb9e9ca35ac3b82af102c9
flowy v1.0.6 deploy/.env.example 60a5432413fab9203a3f8dcc60f900b8448765ee708c8f1eede57e3e162ea6f5
troye v0.1.6 deploy/install.sh 89b7e96d50869f8c91b935d2507e87bc1119ff704884ed6943a79cc71e4b8220
troye v0.1.6 deploy/docker-compose.yml 479d3d020918ef56e6e24049e37e805d7d61e24efe147fb3878eb351d2ba7430
troye v0.1.6 deploy/docker-compose.tailscale.yml 0f6ec5614cd2c53345596ead390dc8890b6cc44ed531e737cd5f386e4d5bffbf
troye v0.1.6 deploy/tailscale-serve.json e8340fb636e47ddd1c3bd6709d39303d32ab9aa2eecb9e9ca35ac3b82af102c9
troye v0.1.6 deploy/docker-compose.host.yml 04812d2b8d78e5e0f8af6c94d71be23e246c03a39446fa853a262e13fdacc53a
troye v0.1.6 deploy/.env.example de3c1e179895c0737793537a9c1c416ba20221fafe55d648d92a78ac9c7c4759
shoppa v0.1.7 deploy/install.sh 8d06e189c1a0dbb13deb8c2ce9a6de26914fd41acecc6843ed2f1e585ed021fb
shoppa v0.1.7 deploy/docker-compose.yml 8e9ad2407bb197e63252774cd9e91782a9984fcd924290c8fc66ab8965cb9f0e
shoppa v0.1.7 deploy/docker-compose.tailscale.yml 8ef58d05b48b2026e4cac3bc10abf558b6229967efdd729d8e86907c80399c88
shoppa v0.1.7 deploy/tailscale-serve.json e8340fb636e47ddd1c3bd6709d39303d32ab9aa2eecb9e9ca35ac3b82af102c9
shoppa v0.1.7 deploy/.env.example c9714d3e7a599ff223f0bf8b5e61a41e0abb8a45145cb516e5f8e7ff67fae2e8
calio v0.1.8 deploy/install.sh a81550ca4fecf22d4bd032cbc0e3f93a59f05c209ab63664336ec7a6c1ee9643
calio v0.1.8 deploy/docker-compose.yml 60be31fe42b628f2f5561998146e1d222e9d18a4c348a72558cb48b4bb311b76
calio v0.1.8 deploy/docker-compose.tailscale.yml 8daad39c8a982aaa6b417ecb6b319d0c155bd8e1cc2a62ccee8e66c1d2b60a04
calio v0.1.8 deploy/docker-compose.funnel.yml 573b7b67cdd9347ef056e74e33558087b3e0b6c59379783b2bf288f1163207ff
calio v0.1.8 deploy/docker-compose.shoppa.yml 73ff94faf531055ad5ba7f694f878237f6c5213bd49eb0e95dd1e757acb457f6
calio v0.1.8 deploy/tailscale-serve.json e8340fb636e47ddd1c3bd6709d39303d32ab9aa2eecb9e9ca35ac3b82af102c9
calio v0.1.8 deploy/tailscale-feed-serve.json 64d154aae85ca2e9fe9a462446d9643459313adbabcc754b807ab1a1a4f2d7b2
calio v0.1.8 deploy/.env.example 1cf7b922f98d60ce8720b6d9801b9c96f07f942ebc026d7ca2c061289d9ad398
reckon v0.1.1 deploy/install.sh 09f2ffaff5556b05d992883ab4705490a265ede21c2ce548c586a350986c06ac
reckon v0.1.1 deploy/docker-compose.yml 19e086efc207d0f92344a4a97671a9ef4173867466c4b180cd3b9ee4fd990734
reckon v0.1.1 deploy/docker-compose.tailscale.yml 0e9f875b82f7bb5a11f575c6a579bca7d5bedd340fefdab9addc6854ca191517
reckon v0.1.1 deploy/tailscale-serve.json e8340fb636e47ddd1c3bd6709d39303d32ab9aa2eecb9e9ca35ac3b82af102c9
reckon v0.1.1 deploy/.env.example fe4d41bfaeea04bc4f2f9f377abc4af78672f5ae73020ea28b8150841038a514
# pins end
EOF
}
sha256() {
if command -v sha256sum >/dev/null; then
sha256sum | cut -d' ' -f1
elif command -v shasum >/dev/null; then
shasum -a 256 | cut -d' ' -f1
else
die "sha256sum or shasum is needed to check the downloads"
fi
}
# fetch_app APP DIR: downloads APP's pinned files into DIR, keeping their paths,
# and stops unless each one has its pinned SHA-256
fetch_app() {
pins | grep -v '^#' | while read -r name tag path sum; do
[ "$name" = "$1" ] || continue
mkdir -p "$2/$(dirname "$path")"
fetch "https://raw.githubusercontent.com/selfhostit/$name/$tag/$path" >"$2/$path" ||
die "could not download $path of $name $tag"
got=$(sha256 <"$2/$path")
[ "$got" = "$sum" ] || die "$path of $name $tag is not the reviewed file (SHA-256 $got); not installing it"
done
[ -n "$(pins | awk -v a="$1" '$1 == a')" ] || die "no pinned release of $1"
}
# app_tag APP: the pinned release of APP
app_tag() { pins | awk -v a="$1" '$1 == a { print $2; exit }'; }
# --- Docker -------------------------------------------------------------------
# Synology keeps docker in /usr/local/bin, which is not always on the PATH
[ -x /usr/local/bin/docker ] && PATH=$PATH:/usr/local/bin
if ! command -v docker >/dev/null; then
if [ "$(uname -s)" != Linux ] || [ -d /volume1 ]; then
die "Docker is not installed. Install it first (on a NAS, from its app store), then run this again."
fi
say "Docker is not installed yet."
case $(ask "Install it now with the official script from get.docker.com? (y/n)" y) in
y* | Y*) fetch https://get.docker.com | sudo sh ;;
*) die "Docker is needed: https://docs.docker.com/engine/install/" ;;
esac
fi
# Without the docker group (as right after installing Docker), use sudo.
sudo=
if ! docker info >/dev/null 2>&1; then
if [ "$(id -u)" != 0 ] && command -v sudo >/dev/null; then
say "Docker needs administrator rights here, so you may be asked for your password."
sudo=sudo
sudo docker info >/dev/null 2>&1 || die "Docker is installed but not running."
else
die "Docker is installed but not running, or this account may not use it."
fi
fi
$sudo docker compose version >/dev/null 2>&1 ||
die "Docker Compose v2 is missing: https://docs.docker.com/compose/install/linux/"
# taken_ports: the TCP ports that something on this machine listens on, and the
# ones Docker containers publish (stopped ones too, so they can start again)
taken_ports() {
{ ss -ltn 2>/dev/null || netstat -an 2>/dev/null | awk '/^tcp/ && /LISTEN/'; } |
awk '{print $4}' | sed -n 's/.*[:.]\([0-9][0-9]*\)$/\1/p'
ids=$($sudo docker ps -aq 2>/dev/null) || ids=
# shellcheck disable=SC2086 # one argument per container
[ -z "$ids" ] || $sudo docker inspect --format \
'{{range .HostConfig.PortBindings}}{{range .}}{{.HostPort}} {{end}}{{end}}' $ids 2>/dev/null |
tr ' ' '\n'
}
# free_port PORT: PORT if nothing uses it, else the first free port after it
free_port() {
taken=" $(taken_ports | tr '\n' ' ') "
p=$1
while :; do
case $taken in *" $p "*) p=$((p + 1)) ;; *) break ;; esac
done
echo "$p"
}
# --- Which apps ---------------------------------------------------------------
chosen=$*
if [ -z "$chosen" ]; then
echo
say "selfhostit installer"
echo
i=0
for app in $apps; do
i=$((i + 1))
printf ' %d) %-10s %s\n' "$i" "$app" "$(info "$app" | cut -d'|' -f2)"
done
echo
echo "Each app runs on its own, so pick only the ones you want; you can add more later."
answer=$(ask "Which apps? Numbers or names, separated by spaces, or all" all)
[ "$answer" = all ] && answer=$apps
for a in $answer; do
case $a in
*[!0-9]*) chosen="$chosen $a" ;;
*)
app=$(echo "$apps" | tr ' ' '\n' | sed -n "${a}p" 2>/dev/null)
[ -n "$app" ] || die "there is no app number $a; choose from 1 to $(echo "$apps" | wc -w)"
chosen="$chosen $app"
;;
esac
done
fi
for app in $chosen; do
info "$app" >/dev/null || die "unknown app '$app'; choose from: $apps"
done
[ -n "$chosen" ] || die "no apps chosen"
if [ -n "${SELFHOSTIT_DIR:-}" ]; then
base=$SELFHOSTIT_DIR
elif [ -d /volume1/docker ]; then
base=/volume1/docker # Synology
elif [ -d /mnt/user/appdata ]; then
base=/mnt/user/appdata # Unraid
else
base=$HOME/selfhostit
fi
base=$(ask "Folder for the apps (one folder each, with its data)" "$base")
mkdir -p "$base" 2>/dev/null || $sudo mkdir -p "$base" || die "could not create $base"
# Tailscale: once set up, keep it; with an auth key, set it up; otherwise ask.
if [ -d "$base/tailscale" ] || [ -n "${TS_AUTHKEY:-}" ]; then
tailscale=y
elif interactive; then
echo
echo "The apps only answer on this machine. To open them from your phones and other"
echo "computers, at home and away, Tailscale (free) gives each app an https:// address"
echo "that only your own devices can reach. Phones need https:// to install the apps."
tailscale=$(ask "Set up Tailscale for the apps? (y/n)" y)
else
tailscale=n
fi
# --- Install ------------------------------------------------------------------
# Each app's files are downloaded into a scratch folder and checked first (fetch_app).
# The plasto-based apps (Flowy, Troye, Shoppa, Calio, Reckon) come with deploy/install.sh, which takes
# the folder as its argument and PORT for a new install (or the next free port
# when that one is taken; Splitwiz's installer does the same). Run from the scratch
# folder, they use the checked files next to them. An empty TS_AUTHKEY skips their
# own Tailscale question: the shared node below covers them.
install_plasto() { # app port dir
$sudo env PORT="$2" TS_AUTHKEY= sh "$tmp/$1/deploy/install.sh" "$3"
}
install_splitwiz() { # port dir
SPLITWIZ_DIR="$2" SPLITWIZ_PORT="$1" sh "$tmp/splitwiz/scripts/install.sh"
}
install_should_we() { # port dir
$sudo mkdir -p "$2/pb_data"
$sudo cp "$tmp/should-we/docker-compose.yml" "$2/docker-compose.yml"
if [ ! -f "$2/.env" ]; then
port=$(free_port "$1")
[ "$port" = "$1" ] || echo "Port $1 is taken, so should-we gets port $port."
tz=${TZ:-$(cat /etc/timezone 2>/dev/null || readlink /etc/localtime 2>/dev/null | sed 's|.*zoneinfo/||' || true)}
printf 'SHOULD_WE_PORT=%s\nTZ=%s\n' "$port" "${tz:-UTC}" | $sudo tee "$2/.env" >/dev/null
fi
(cd "$2" && $sudo docker compose pull --quiet && $sudo docker compose up -d --remove-orphans)
}
tmp=$(mktemp -d)
trap 'rm -r "$tmp"' EXIT
for app in $chosen; do
port=$(info "$app" | cut -d'|' -f1)
dir=$base/$app
echo
say "== $app $(app_tag "$app")"
fetch_app "$app" "$tmp/$app"
$sudo mkdir -p "$dir"
# Run each installer from its own folder, so none of them mistakes this
# folder for a checkout of its repository.
(
cd "$dir"
case $app in
splitwiz) install_splitwiz "$port" "$dir" ;;
should-we) install_should_we "$port" "$dir" ;;
*) install_plasto "$app" "$port" "$dir" ;;
esac
) || die "$app did not install; see the messages above"
done
# app_port app: the port the app listens on, from its .env if it was changed
app_port() {
port=$(sed -n 's/^\(PORT\|SPLITWIZ_PORT\|SHOULD_WE_PORT\)=//p' "$base/$1/.env" 2>/dev/null | head -n 1)
echo "${port:-$(info "$1" | cut -d'|' -f1)}"
}
# --- Tailscale ----------------------------------------------------------------
# One Tailscale node, "selfhostit", for all the apps: one sign-in, and each app
# at https://selfhostit.<your-tailnet>.ts.net:<its port>. It uses the host's
# network to reach the apps on 127.0.0.1, where they listen.
ts() { $sudo docker exec selfhostit-tailscale tailscale --socket=/tmp/tailscaled.sock "$@"; }
# ts_status KEY: a string field of `tailscale status --json`
ts_status() {
ts status --json --peers=false 2>/dev/null |
sed -n "s/^ *\"$1\": *\"\([^\"]*\)\".*/\1/p" | head -n 1
}
setup_tailscale() {
dir=$base/tailscale
$sudo mkdir -p "$dir/state"
$sudo tee "$dir/docker-compose.yml" >/dev/null <<-'EOF'
# The shared Tailscale node of the selfhostit apps, written by install.sh.
# It signs in once; the login and the https:// addresses are kept in ./state.
name: selfhostit-tailscale
services:
tailscale:
image: tailscale/tailscale:stable
container_name: selfhostit-tailscale
restart: unless-stopped
network_mode: host
environment:
TS_HOSTNAME: selfhostit
TS_AUTHKEY: ${TS_AUTHKEY:-}
TS_AUTH_ONCE: "true"
TS_STATE_DIR: /var/lib/tailscale
volumes:
- ./state:/var/lib/tailscale
mem_limit: 256m
EOF
(cd "$dir" && $sudo docker compose pull --quiet &&
$sudo env TS_AUTHKEY="${TS_AUTHKEY:-}" docker compose up -d --remove-orphans) ||
return 1
shown=
i=0
while :; do
state=$(ts_status BackendState)
case $state in
Running) break ;;
NeedsLogin)
url=$(ts_status AuthURL)
if [ -n "$url" ] && [ "$url" != "$shown" ]; then
echo
say "Sign in to Tailscale (or make a free account) by opening this link:"
echo
echo " $url"
echo
echo "Waiting for you to sign in..."
shown=$url
fi
;;
NeedsMachineAuth)
[ "$shown" = approve ] || echo "Approve the device 'selfhostit' in https://login.tailscale.com/admin/machines"
shown=approve
;;
esac
i=$((i + 1))
if [ "$i" -gt 900 ]; then
echo "Tailscale is not signed in yet. Run this installer again when you are ready."
return 1
fi
sleep 1
done
# The first `tailscale serve` turns on HTTPS for the tailnet if needed: it
# shows a link to allow that and waits.
say "Giving each app an https:// address"
first=y
for app in $chosen; do
port=$(app_port "$app")
if [ $first = y ]; then
ts serve --bg --https="$port" "http://127.0.0.1:$port" || return 1
first=n
else
ts serve --bg --https="$port" "http://127.0.0.1:$port" >/dev/null || return 1
fi
done
ts serve status --json 2>/dev/null | grep -q "\"$port\"" || {
echo "HTTPS is not on for your tailnet yet. Allow it with the link above, then run this installer again."
return 1
}
ts_name=$(ts_status DNSName)
ts_name=${ts_name%.}
}
ts_name=
if [ "$tailscale" = y ] || [ "$tailscale" = Y ] || [ "$tailscale" = yes ]; then
echo
say "== Tailscale"
setup_tailscale || echo "Tailscale is not set up; the apps work on this machine meanwhile."
fi
# --- Done ---------------------------------------------------------------------
echo
say "Done."
for app in $chosen; do
port=$(app_port "$app")
if [ -n "$ts_name" ]; then
printf ' %-10s https://%s:%s\n' "$app" "$ts_name" "$port"
else
printf ' %-10s http://localhost:%s (%s)\n' "$app" "$port" "$base/$app"
fi
# a new install asks for this one-time code on its setup screen
code=$($sudo cat "$base/$app/pb_data/setup_code" 2>/dev/null || true)
[ -z "$code" ] || printf ' %-10s setup code: %s\n' "" "$code"
done
echo
say "Next:"
echo " 1. Open an address. A new app asks for its setup code once, then you make your"
echo " account and become its owner."
if [ -n "$ts_name" ]; then
echo " 2. On each phone, install the Tailscale app and sign in with the same account,"
echo " then open the addresses and add each app to the home screen."
else
echo " 2. These addresses only open on this machine. To open the apps from your phones"
echo " and other computers, run this installer again and say yes to Tailscale, or"
echo " set up a reverse proxy: https://github.com/selfhostit/docs/blob/main/phones.md"
fi
echo
echo "Adding your household, and what to do when an app doesn't open:"
echo " https://github.com/selfhostit/docs/blob/main/first-steps.md"
echo "The apps and their data are in $base. To update, run this installer again."