-
Notifications
You must be signed in to change notification settings - Fork 1
/
setupPi.sh
288 lines (228 loc) · 7.98 KB
/
setupPi.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
#!/bin/bash
#
# Download this file into the home directory of the device.
# Give this file execution rights (chmod +x setup.sh)
# Then execute this file with sudo permissions (sudo ./setup.sh)
# NB: All commands are run with sudo permissions.
####################
# BASH BOILERPLATE #
####################
# https://stackoverflow.com/a/5947802/7254995
printRed () {
echo -e "\033[0;31m$1\033[0m"
}
printBlue () {
echo -e "\033[1;34m$1\033[0m"
}
printGreen () {
echo -e "\033[1;32m$1\033[0m"
}
set -e
set -o pipefail
exitingDueToError() {
printRed "ERROR: Setup did not succeed."
}
trap exitingDueToError ERR
##############
# SUDO CHECK #
##############
if [[ $EUID -ne 0 ]]
then
printRed "Please run this script as root, otherwise some parts won't work."
exit 1
fi
######################################
# IF SETUP IS NOT RUN THE FIRST TIME #
######################################
printBlue "SECTION: IF SETUP IS NOT RUN THE FIRST TIME"
# || true prevents tripping our set -e setting. If these fail, it's ok.
systemctl stop gunicorn.socket || true
systemctl stop nginx || true
pkill -f python3 || true # stop previously running "passive.py"s
#####################
# SETUP /deltaForce #
#####################
printBlue "SECTION: SETUP /deltaForce"
if [[ ! -e /deltaForce ]]
then
mkdir /deltaForce
printGreen "Successfully created /deltaForce"
else
if [[ ! -e /deltaForceDBBackups ]]
then
mkdir /deltaForceDBBackups
fi
if [[ -e /deltaForce/exhibition-inference/site/exhibitionInferenceSite/db.sqlite3 ]]
then
cp /deltaForce/exhibition-inference/site/exhibitionInferenceSite/db.sqlite3 /deltaForceDBBackups/db.sqlite3.bak
printGreen "Successfully backed up database to /deltaForceDBBackups/db.sqlite3.bak"
fi
rm -rf /deltaForce
printGreen "/deltaForce already existed; deleting it"
mkdir /deltaForce
printGreen "Successfully recreated /deltaForce"
fi
cd /deltaForce
################
# DEPENDENCIES #
################
printBlue "SECTION: DEPENDENCIES"
# Install dependencies if not already installed
apt install -y --upgrade git python3 python3-distutils
printGreen "Successfully (re)installed dependencies"
# Install nginx
# https://nginx.org/en/linux_packages.html#Debian
if [[ -z "$(which nginx)" ]]
then
apt install -y --upgrade curl gnupg2 ca-certificates lsb-release debian-archive-keyring
curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor | tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
checksum=$(echo $(gpg --dry-run --quiet --import --import-options import-show /usr/share/keyrings/nginx-archive-keyring.gpg))
correctChecksum=$(echo "pub rsa2048 2011-08-19 [SC] [expires: 2024-06-14] 573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62 uid nginx signing key <[email protected]>")
if [[ "$checksum" != "$correctChecksum" ]]
then
printRed "NGINX installation failed: unexpected checksum"
exit 1
fi
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] http://nginx.org/packages/debian `lsb_release -cs` nginx" | tee /etc/apt/sources.list.d/nginx.list
echo -e "Package: *\nPin: origin nginx.org\nPin: release o=nginx\nPin-Priority: 900\n" | tee /etc/apt/preferences.d/99nginx
apt update
apt install -y nginx
printGreen "Successfully installed NGINX"
else
apt update
apt install -y --upgrade nginx
printGreen "Successfully upgraded NGINX"
fi
# Install pip if not already installed
if [[ -z "$(which pip)" ]]
then
wget https://bootstrap.pypa.io/get-pip.py
python3 get-pip.py
if [[ "$PATH" != *"$(dirname $(which pip))"* ]] # if parent directory of pip not in PATH
then
echo 'export PATH="/home/pi/.local/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
fi
rm get-pip.py
printGreen "Successfully installed pip"
else
printGreen "pip already installed"
pip install --upgrade pip
printGreen "Successfully upgraded pip"
fi
#############
# GIT CLONE #
#############
printBlue "SECTION: GIT CLONE"
git clone -b main --single-branch https://github.com/Marchhill/exhibition-inference.git
printGreen "Successfully cloned exhibition-inference github repository"
GITHUB_BASE_DIR="/deltaForce/exhibition-inference/"
cd exhibition-inference
pip install -r requirements.txt
printGreen "Successfully installed exhibition-inference pip dependencies"
python3 site/exhibitionInferenceSite/manage.py collectstatic # Collect static files
####################
# RESTORE DATABASE #
####################
if [[ -e /deltaForceDBBackups/db.sqlite3.bak ]]
then
mv /deltaForceDBBackups/db.sqlite3.bak /deltaForce/exhibition-inference/site/exhibitionInferenceSite/db.sqlite3
printGreen "Successfully restored database from /deltaForceDBBackups/db.sqlite3.bak"
fi
python3 site/exhibitionInferenceSite/manage.py migrate
printGreen "Successfully performed database migrations (if any)"
#################
# SYSTEMD SETUP #
#################
printBlue "SECTION: SYSTEMD SETUP"
cat <<EOF > /etc/systemd/system/gunicorn.socket
[Unit]
Description=delta force gunicorn socket
[Socket]
ListenStream=/run/gunicorn.sock
[Install]
WantedBy=sockets.target
EOF
cat <<EOF > /etc/systemd/system/gunicorn.service
[Unit]
Description=delta force gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=$(whoami)
Group=www-data
WorkingDirectory=${GITHUB_BASE_DIR}site/exhibitionInferenceSite
ExecStart=$(which gunicorn) \\
--access-logfile - \\
--workers 3 \\
--bind unix:/run/gunicorn.sock \\
exhibitionInferenceSite.wsgi:application
[Install]
WantedBy=multi-user.target
EOF
cat <<EOF > /etc/nginx/nginx.conf
user $(whoami);
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '\$remote_addr - \$remote_user [\$time_local] "\$request" '
'\$status \$body_bytes_sent "\$http_referer" '
'"\$http_user_agent" "\$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
# include /etc/nginx/conf.d/*.conf; # this one also binds to port 80, DO NOT include.
server {
listen 80;
# Ignore problems related to finding favicon
location = /favicon.ico {
access_log off;
log_not_found off;
}
location /static/ {
autoindex on;
autoindex_exact_size off;
alias ${GITHUB_BASE_DIR}site/exhibitionInferenceSite/static_root/;
}
location / {
# https://docs.gunicorn.org/en/stable/deploy.html#nginx-configuration
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
proxy_set_header Host \$http_host;
# we don't want nginx trying to do something clever with
# redirects, we set the Host: header above already.
proxy_redirect off;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
}
EOF
systemctl daemon-reload
printGreen "Successfully wrote and reloaded gunicorn & nginx configuration files for systemd"
systemctl start gunicorn.socket
systemctl enable gunicorn.socket
systemctl start nginx
printGreen "Successfully started gunicorn and nginx"
##################
# RUN PASSIVE.PY #
##################
# NB This assumes the Green passive tag remains a passive tag, and isn't reconfigured
python3 "${GITHUB_BASE_DIR}hardwareInterface/passive.py" Green &
printGreen "Successfully started passive.py. Now polling of data from tags."
########
# DONE #
########
printBlue "SECTION: DONE"
echo
printGreen "Installation success :) Server is up and running at one of: $(hostname -I)."
# printGreen "Next, run this command to finish deployment:"
# printGreen "python3 ${GITHUB_BASE_DIR}hardwareInterface/passive.py Green &"
echo