-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathuser-data.sh
More file actions
404 lines (342 loc) · 10 KB
/
user-data.sh
File metadata and controls
404 lines (342 loc) · 10 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
#!/bin/bash
set -ex
# Check if /extra exists
ls /extra || sudo mkdir /extra
# Create bs user
ls /home/bs || sudo useradd -m -s /bin/bash bs
# Bitcoin and C-Lightning conf
cat <<- EOF > /home/bs/bitcoin.conf
# https://en.bitcoin.it/wiki/Running_Bitcoin
testnet=1
rpcuser=core
rpcpassword=chang3m3
txindex=1
dbcache=2000
EOF
chmod 0644 /home/bs/bitcoin.conf
cat <<- EOF > /home/bs/lightning.conf
# https://github.com/ElementsProject/lightning/tree/master/doc
network=testnet
alias=LN Store in a Box
bitcoin-rpcuser=core
bitcoin-rpcpassword=chang3m3
bind-addr=0.0.0.0
EOF
chmod 0644 /home/bs/lightning.conf
# Systemd services for bitcoin, c-lightning, charge
cat <<- EOF > /etc/systemd/system/bitcoin.service
[Unit]
Description=Bitcoin node
Wants=docker.service
[Service]
Restart=always
RestartSec=10
Environment=HOME=/home/bs
ExecStartPre=/usr/bin/docker pull blockstream/bitcoind:latest
ExecStart=/usr/bin/docker run \
--network=host \
--pid=host \
--name=bitcoin \
--log-driver json-file --log-opt max-size=1g \
-v /home/bs/bitcoin.conf:/root/.bitcoin/bitcoin.conf:ro \
-v /extra/bitcoin:/root/.bitcoin \
"blockstream/bitcoind:latest" bitcoind -printtoconsole
ExecStop=/usr/bin/docker exec bitcoin bitcoin-cli stop
ExecStopPost=/bin/sleep 3
ExecStopPost=/usr/bin/docker rm -f bitcoin
[Install]
WantedBy=multi-user.target
EOF
chmod 0644 /etc/systemd/system/bitcoin.service
cat <<- EOF > /etc/systemd/system/lightning.service
[Unit]
Description=Lightning node
Wants=docker.service
After=bitcoin.service
[Service]
Restart=always
RestartSec=10
Environment=HOME=/home/bs
ExecStartPre=/usr/bin/docker pull blockstream/lightningd:latest
ExecStartPre=/sbin/iptables -A INPUT -p tcp --dport 9735 -j ACCEPT
ExecStart=/usr/bin/docker run \
--network=host \
--pid=host \
--name=lightning \
--log-driver json-file --log-opt max-size=1g \
-v /home/bs/lightning.conf:/root/.lightning/lightning.conf:ro \
-v /extra/lightning:/root/.lightning \
"blockstream/lightningd:latest" lightningd --conf=/root/.lightning/lightning.conf --plugin-dir=/usr/local/bin/plugins
ExecStop=/usr/bin/docker exec lightning lightning-cli stop
ExecStopPost=/bin/sleep 3
ExecStopPost=/usr/bin/docker rm -f lightning
ExecStopPost=/sbin/iptables -D INPUT -p tcp --dport 9735 -j ACCEPT
[Install]
WantedBy=multi-user.target
EOF
chmod 0644 /etc/systemd/system/lightning.service
cat <<- EOF > /etc/systemd/system/charge.service
[Unit]
Description=Charge instance
Wants=docker.service
After=lightning.service
[Service]
Restart=always
RestartSec=10
Environment=HOME=/home/bs
ExecStartPre=/usr/bin/docker pull blockstream/charged:latest
ExecStartPre=/sbin/iptables -A INPUT -p tcp -s localhost --dport 9112 -j ACCEPT
ExecStart=/usr/bin/docker run \
--pid=host \
--name=charge \
-p 9112:9112 \
-v /extra/lightning:/root/.lightning:ro \
-v /extra/charge:/data \
-e "API_TOKEN=SECRETAPITOKEN" \
-e "DB_PATH=/data/charge.db" \
-e "LN_PATH=/root/.lightning" \
-e "HOST=0.0.0.0" \
"blockstream/charged:latest" charged
ExecStop=/usr/bin/docker stop charge
ExecStopPost=/usr/bin/docker rm -f charge
ExecStopPost=/sbin/iptables -D INPUT -p tcp -s localhost --dport 9112 -j ACCEPT
[Install]
WantedBy=multi-user.target
EOF
chmod 0644 /etc/systemd/system/charge.service
# Wordpress stuff
cat <<- EOF > /etc/systemd/system/wp.service
[Unit]
Description=Wordpress instance
Wants=docker.service
[Service]
Restart=always
RestartSec=5
Environment=HOME=/home/bs
ExecStartPre=/usr/bin/docker pull wordpress:php7.3-fpm-alpine
ExecStartPre=/usr/bin/docker run \
--user root \
-v /extra/data/wp:/var/www/html \
--entrypoint bash \
--rm \
"wordpress:php7.3-fpm-alpine" -c 'chown -R www-data:www-data /var/www/html'
ExecStart=/usr/bin/docker run \
--pid=host \
--name=wp \
--link=wp-mysql \
-v /extra/data/wp:/var/www/html \
-e "WORDPRESS_DB_USER=root" \
-e "WORDPRESS_DB_PASSWORD=my-secret-pw" \
-e "WORDPRESS_DB_HOST=wp-mysql" \
"wordpress:php7.3-fpm-alpine"
ExecStop=/usr/bin/docker stop wp
ExecStopPost=/usr/bin/docker rm -f wp
[Install]
WantedBy=multi-user.target
EOF
chmod 0644 /etc/systemd/system/wp.service
cat <<- EOF > /etc/systemd/system/mysql.service
[Unit]
Description=MySQL db for Wordpress
Wants=docker.service
[Service]
Restart=always
RestartSec=5
Environment=HOME=/home/bs
ExecStartPre=/usr/bin/docker pull mysql:5.7
ExecStartPre=/usr/bin/docker run \
--user root \
-v /extra/data/db:/var/lib/mysql \
--entrypoint bash \
--rm \
"mysql:5.7" -c 'chown -R mysql:mysql /var/lib/mysql'
ExecStart=/usr/bin/docker run \
--pid=host \
--name=wp-mysql \
-v /extra/data/db:/var/lib/mysql \
-p 3306:3306 \
-e "MYSQL_ROOT_PASSWORD=my-secret-pw" \
"mysql:5.7"
ExecStop=/usr/bin/docker stop wp-mysql
ExecStopPost=/usr/bin/docker rm -f wp-mysql
[Install]
WantedBy=multi-user.target
EOF
chmod 0644 /etc/systemd/system/mysql.service
cat <<- EOF > /etc/systemd/system/nginx.service
[Unit]
Description=Nginx reverse proxy
Wants=docker.service
After=wp.service
[Service]
Restart=always
RestartSec=5
Environment=HOME=/home/bs
ExecStartPre=/usr/bin/docker pull nginx:latest
ExecStart=/usr/bin/docker run \
--pid=host \
--name=wp-nginx \
--link=wp \
-v /extra/data/wp:/var/www/html:ro \
-v /etc/letsencrypt/archive/example.com/:/var/www/html/letsencrypt:ro \
-v /home/bs/nginx.conf:/etc/nginx/conf.d/default.conf:ro \
-p 80:8080 \
-p 443:443 \
"nginx:latest"
ExecStop=/usr/bin/docker stop wp-nginx
ExecStopPost=/usr/bin/docker rm -f wp-nginx
[Install]
WantedBy=multi-user.target
EOF
chmod 0644 /etc/systemd/system/nginx.service
cat <<- EOF > /etc/systemd/system/wp-cli.service
[Unit]
Description=Wordpress CLI for setup
Wants=docker.service
After=wp.service
[Service]
Type=simple
Environment=HOME=/home/bs
ExecStartPre=/usr/bin/docker pull wordpress:cli
ExecStart=/usr/bin/docker run \
--pid=host \
--name=wp-cli \
--link=wp-mysql \
-v /extra/data/wp:/var/www/html \
-e "WORDPRESS_DB_PASSWORD=my-secret-pw" \
"wordpress:cli" bash -c 'sleep 600'
ExecStop=/usr/bin/docker stop wp-cli
ExecStopPost=/usr/bin/docker rm -f wp-cli
[Install]
WantedBy=multi-user.target
EOF
chmod 0644 /etc/systemd/system/wp-cli.service
cat <<- 'EOF' > /home/bs/nginx.conf
log_format withtime '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" '
'rt="$request_time" uct="$upstream_connect_time" uht="$upstream_header_time" urt="$upstream_response_time"';
server {
index index.php index.html index.htm index.nginx-debian.html;
access_log /dev/stdout withtime;
error_log /dev/stdout info;
server_name _;
listen 8080 default_server;
set_real_ip_from 130.211.0.0/22;
set_real_ip_from 35.191.0.0/16;
set_real_ip_from 10.0.0.0/8;
real_ip_recursive on;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
location /.well-known {
alias /var/www/html/.well-known;
auth_basic off;
allow all; # Allow all to see content
}
location / {
return 301 https://$host$request_uri;
}
location /health.html {
return 200;
}
location ~ /\.ht {
deny all;
}
}
# server {
# index index.php index.html index.htm index.nginx-debian.html;
#
# access_log /dev/stdout withtime;
# error_log /dev/stdout info;
#
# server_name _;
# listen 443 ssl;
#
# client_max_body_size 100M;
# root /var/www/html;
# server_tokens off;
# set_real_ip_from 130.211.0.0/22;
# set_real_ip_from 35.191.0.0/16;
# set_real_ip_from 10.0.0.0/8;
# real_ip_recursive on;
# proxy_set_header Host $host;
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#
# ssl_certificate /var/www/html/letsencrypt/fullchain1.pem;
# ssl_certificate_key /var/www/html/letsencrypt/privkey1.pem;
# ssl_prefer_server_ciphers on;
#
# location / {
# try_files $uri $uri/ /index.php$is_args$args;
# }
#
# location ~ \.php$ {
# # regex to split $uri to $fastcgi_script_name and $fastcgi_path
# fastcgi_split_path_info ^(.+\.php)(/.+)$;
#
# # Check that the PHP script exists before passing it
# try_files $fastcgi_script_name =404;
#
# # Bypass the fact that try_files resets $fastcgi_path_info
# # see: http://trac.nginx.org/nginx/ticket/321
# set $path_info $fastcgi_path_info;
# fastcgi_param PATH_INFO $path_info;
#
# fastcgi_index index.php;
#
# include fastcgi_params;
# fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# fastcgi_param PATH_INFO $fastcgi_path_info;
#
# fastcgi_pass wp:9000;
# }
#
# location ~ /\.ht {
# deny all;
# }
#}
EOF
chmod 0644 /home/bs/nginx.conf
# Wordpress setup script
cat <<- EOF > /home/bs/wp-setup.sh
#!/bin/env bash
systemctl start wp-cli
sleep 5 # wait for service
# Install wp-config.php
docker exec wp-cli sh -c 'wp config create --dbname=wordpress --dbuser=root --dbpass=my-secret-pw --dbhost=wp-mysql --force'
# Update domain
# If you want to test on localhost, you can replace "example.com" with just "localhost"
docker exec wp-cli sh -c 'wp search-replace "localhost:8080" "example.com" --skip-columns=guid'
EOF
chmod 0744 /home/bs/wp-setup.sh
# Install docker, certbot, mysql-client
cat <<- 'EOF' > /home/bs/prereqs.sh
#!/bin/env bash
set -ex
SUCCESS_INDICATOR=/opt/.provision_success
# confirm this is an ubuntu box
[[ ! -f /etc/lsb-release ]] && exit 1
# check if vagrant_provision has run before
[[ -f $SUCCESS_INDICATOR ]] && exit 0
# install docker
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
add-apt-repository ppa:certbot/certbot -y
apt-get update && apt-get upgrade -y && apt-get install -qfy docker-ce software-properties-common certbot mysql-client
echo ""
# Successful run
touch $SUCCESS_INDICATOR
exit 0
EOF
chmod 0744 /home/bs/prereqs.sh
bash /home/bs/prereqs.sh
systemctl daemon-reload
systemctl enable bitcoin.service
systemctl enable lightning.service
systemctl enable charge.service
systemctl enable wp.service
systemctl enable mysql.service
systemctl enable nginx.service