forked from Start9Labs/albyhub-startos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker_entrypoint.sh
143 lines (118 loc) · 4.11 KB
/
docker_entrypoint.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
#!/bin/sh
printf "\n\n [i] Starting Alby Hub ...\n\n"
# Read current LN setup from config
LN_VALUE=$(grep 'lightning:' /data/start9/config.yaml | cut -d ' ' -f 2)
# File to track initial setup (lnd or alby)
SETUP_FILE="/data/start9/initial_setup"
WORK_DIR="/data/albyhub"
BACKUP_DIR="/data/start9/backups"
# Ensure the backup directory exists
mkdir -p "$BACKUP_DIR"
# Determine the initial setup
if [ -f "$SETUP_FILE" ]; then
INITIAL_SETUP=$(cat "$SETUP_FILE")
else
INITIAL_SETUP="$LN_VALUE"
echo "$INITIAL_SETUP" >"$SETUP_FILE" # Store initial setup if it doesn't exist
fi
# Function to create a tar.gz backup of the albyhub directory
backup_dir() {
suffix="$INITIAL_SETUP"
tar_file="$BACKUP_DIR/${suffix}_backup.tar.gz"
# Create a tar.gz file containing the albyhub directory
tar -czf "$tar_file" -C "/data" albyhub 2>/dev/null
echo "[i] Created backup: $tar_file"
}
# Function to restore the albyhub directory from a tar.gz backup
restore_dir() {
suffix="$1" # Either 'lnd' or 'alby'
tar_file="$BACKUP_DIR/${suffix}_backup.tar.gz"
if [ -f "$tar_file" ]; then
rm -rf "$WORK_DIR"
tar -xzf "$tar_file" -C "/data"
echo "[i] Restored from backup: $tar_file"
else
echo "[i] No $suffix backup found."
fi
}
# Handling different setups
if [ "$INITIAL_SETUP" != "$LN_VALUE" ]; then
if [ "$INITIAL_SETUP" = "lnd" ] && [ "$LN_VALUE" = "alby" ]; then
echo "[i] Switching from LND to Alby/LDK..."
# Backup current LND directory
backup_dir
# Restore Alby backup if it exists, otherwise start fresh
if [ -f "$BACKUP_DIR/alby_backup.tar.gz" ]; then
restore_dir "alby"
else
echo "[i] No Alby/LDK backup found, starting fresh..."
rm -rf "$WORK_DIR"
mkdir -p "$WORK_DIR"
fi
# Update the initial setup to 'alby'
echo "alby" >"$SETUP_FILE"
elif [ "$INITIAL_SETUP" = "alby" ] && [ "$LN_VALUE" = "lnd" ]; then
echo "[i] Switching from Alby/LDK to LND..."
# Backup current Alby directory
backup_dir
# Restore LND backup if it exists, otherwise clean up the data directory for initial LND setup
if [ -f "$BACKUP_DIR/lnd_backup.tar.gz" ]; then
restore_dir "lnd"
else
echo "[i] No LND backup found, cleaning up the data directory for initial LND setup..."
rm -rf "$WORK_DIR"
mkdir -p "$WORK_DIR"
fi
# Update the initial setup to 'lnd'
echo "lnd" >"$SETUP_FILE"
fi
fi
# Set up environment variables based on LN_VALUE
if [ "$LN_VALUE" = "lnd" ]; then
export LN_BACKEND_TYPE="LND"
export LND_ADDRESS="lnd.embassy:10009" # the LND gRPC address
export LND_CERT_FILE="/mnt/lnd/tls.cert" # the location where LND's tls.cert file can be found
export LND_MACAROON_FILE="/mnt/lnd/admin.macaroon" # the location where LND's admin.macaroon file can be found
export ENABLE_ADVANCED_SETUP=false
else
# Default to Alby/LDK if lightning value is not "lnd"
export LN_BACKEND_TYPE="LDK"
unset LND_ADDRESS
unset LND_CERT_FILE
unset LND_MACAROON_FILE
fi
export WORK_DIR="/data/albyhub"
export PORT=8080 #the port on which the app should listen on (default='blah' #8080)
export LOG_EVENTS=true # makes debugging easier
# Reverse proxy configuration (aka Firefox patch)
cat <<EOF >/etc/caddy/Caddyfile
{
admin off
servers {
protocols h1 h2c h3
}
}
:8443 {
tls /mnt/cert/main.cert.pem /mnt/cert/main.key.pem
reverse_proxy albyhub.embassy:8080
}
EOF
# Output some debug information
echo "LN Backend Type: $LN_BACKEND_TYPE"
if [ "$LN_VALUE" = "lnd" ]; then
echo "LN Address: $LND_ADDRESS"
echo "LND Cert: $LND_CERT_FILE"
echo "LND Macaroon: $LND_MACAROON_FILE"
fi
# Set up a trap to catch INT signal for graceful shutdown and start
_term() {
echo "Caught INT signal!"
kill -INT "$alby_process" 2>/dev/null
kill -INT "$caddy_process" 2>/dev/null
}
main &
alby_process=$!
caddy run --config /etc/caddy/Caddyfile &
caddy_process=$!
trap _term INT
wait $caddy_process $alby_process