-
Notifications
You must be signed in to change notification settings - Fork 0
/
provision-ubuntu.sh
96 lines (84 loc) · 2.04 KB
/
provision-ubuntu.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
#!/bin/bash
set -eux
export DEBIAN_FRONTEND=noninteractive
domain=$(hostname --fqdn)
if [ "$(hostname)" = 'moon-ubuntu' ]; then
local='moon-ubuntu'
remote='sun-ubuntu'
remote_ip='10.2.0.4'
else
local='sun-ubuntu'
remote='moon-ubuntu'
remote_ip='10.1.0.4'
fi
# install node LTS.
# see https://github.com/nodesource/distributions#debinstall
apt-get install -y curl
curl -sL https://deb.nodesource.com/setup_10.x | bash
apt-get install -y nodejs
node --version
npm --version
# add the hello-world user.
groupadd --system hello-world
adduser \
--system \
--disabled-login \
--no-create-home \
--gecos '' \
--ingroup hello-world \
--home /opt/hello-world \
hello-world
install -d -o root -g hello-world -m 750 /opt/hello-world
# create an hello world http server and run it as a systemd service.
cat >/opt/hello-world/main.js <<EOF
const http = require("http");
const server = http.createServer((request, response) => {
const serverAddress = request.socket.localAddress;
const clientAddress = request.socket.remoteAddress;
response.writeHead(200, {"Content-Type": "text/plain"});
response.write(\`Hello World from $local!
Server Address: \${serverAddress}
Client Address: \${clientAddress}
\`);
response.end();
});
server.listen(3000);
EOF
cat >package.json <<'EOF'
{
"name": "hello-world",
"description": "the classic hello world",
"version": "1.0.0",
"license": "MIT",
"main": "main.js",
"dependencies": {}
}
EOF
npm install
# launch hello-world.
cat >/etc/systemd/system/hello-world.service <<'EOF'
[Unit]
Description=Hello World
After=network.target
[Service]
Type=simple
User=hello-world
Group=hello-world
Environment=NODE_ENV=production
ExecStart=/usr/bin/node main.js
WorkingDirectory=/opt/hello-world
Restart=on-abort
[Install]
WantedBy=multi-user.target
EOF
systemctl enable hello-world
systemctl start hello-world
# try it.
sleep .2
wget -qO- localhost:3000
#
# add useful commands to the shell history.
cat >>~/.bash_history <<EOF
wget -qO- $remote_ip:3000
wget -qO- localhost:3000
EOF