-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·332 lines (280 loc) · 8.34 KB
/
install.sh
File metadata and controls
executable file
·332 lines (280 loc) · 8.34 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
#!/bin/bash
set -e
declare -A settings
name="leds"
currentDir=$(pwd)
configFile="$(pwd)/web/config/install.cfg"
configurationString=""
helpPrint()
{
echo ""
echo "Usage: $0 [-i][-r][-u][-h]"
echo -e "\t-i pin=18,port=9000,ledCount=100:pin=21,port=9001,ledCount=30"
echo -e "\t Installs service with the provided configuration or reinstalls if .install.cfg already exists"
echo -e "\t-r \n\t Reinstalls current installation."
echo -e "\t-u \n\t Uninstall current installation."
echo -e "\t-h \n\t Display this help message"
exit 1 # Exit script after printing help
}
requireRoot()
{
if [ "$(id -u)" -ne "0" ] ; then
echo "This script must be executed with root privileges."
exit 1
fi
}
installDocker()
{
if [ ! -x "$(command -v docker)" ]
then
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
rm get-docker.sh
fi
}
parseCommands()
{
instances=0
configurations=$1
readarray -d ":" -t configArray <<<"$configurations"
for instanceCfgs in "${configArray[@]}"
do
instances=$((instances+1))
readarray -d "," -t instanceArgs <<<"$instanceCfgs"
for configs in "${instanceArgs[@]}"
do
readarray -d "=" -t configPair <<<"$configs"
if [ ${#configPair[@]} != 2 ];
then
echo "Invalid key value pair, should be: key=value"
exit 1
fi
key=${configPair[0]//$'\n'/}
value=${configPair[1]//$'\n'/}
if [ $key == "pin" ] || [ $key == "port" ] || [ $key == "ledCount" ];
then
if ! [[ $value =~ ^[0-9]+([.][0-9]+)?$ ]];
then
echo "Invalid value provided, shoud be number!"
exit 1
else
settings["${instances}-${key}"]+=$value
fi
else
echo "Only \"pin\", \"port\" and \"ledCount\" keys are supported"
exit 1
fi
done
done
}
getInstallConfig()
{
configString=""
if test -f "$configFile"
then
configString="$(cat ${configFile})"
fi
if [ "$1" == "reinstall" ] && [ -z "$configString" ]
then
echo "Error: .install.cfg does not exist - nothing to reinstall"
exit 1
elif [ -n "$configString" ] && [ -n "$configurationString" ]
then
echo "Error: .install.cfg exists. Uninstall with: \"$0 -u\" or delete the file manually."
exit 1
elif [ "$1" == "uninstall" ]
then
echo "Uninstalling.."
elif [ -n "$configString" ]
then
echo ".install.cfg exists -> reinstalling existing installation with ${configString}."
configurationString=$configString
fi
}
createDockerNetwork()
{
if [[ "$(docker network ls | grep ${name}-network)" == "" ]]
then
docker network create --driver bridge ${name}-network
fi
}
installDockerContainers()
{
sudo systemctl reset-failed
for (( instance=1; instance <= $instances; ++instance ))
do
containerName="${name}-${instance}"
pin=${settings["${instance}-pin"]}
port=${settings["${instance}-port"]}
ledCount=${settings["${instance}-ledCount"]}
if systemctl --all --type service | grep -q "${name}-${instance}.service"
then
echo "Stopping & disabling service: ${name}-${instance}.service..."
sudo systemctl stop ${name}-${instance}.service
sudo systemctl disable ${name}-${instance}.service
fi
if [ "$(docker ps -a | grep ${name}-${instance})" ]
then
echo "Deleting container: ${name}-${instance}..."
docker rm ${name}-${instance}
fi
echo "Building image: gradrix/${name}-${instance}..."
sudo docker build -t gradrix/${name}-${instance} --build-arg pin=${pin} --build-arg port=${port} --build-arg ledCount=${ledCount} -f ./docker/gpio_service/Dockerfile .
echo "Creating and running cointainer once..."
docker run --name ${name}-${instance} -it --device /dev/gpiomem -p ${port}:${port} --privileged -d --restart unless-stopped --network ${name}-network gradrix/${name}-${instance}
echo "Stopping container..."
docker stop ${name}-${instance}
installService ${name}-${instance} ${name}
done
echo "Building web images"
docker-compose build --force-rm --parallel
installWebService "${name}-web"
}
installWebService()
{
service=$1
cat << EOF | sudo tee /lib/systemd/system/${service}.service
[Unit]
Description=Leds-Web Service
After=network.target docker.service
[Service]
WorkingDirectory=$currentDir
Type=simple
Restart=always
ExecStart=/bin/docker-compose up --remove-orphans
ExecStop=/bin/docker-compose down --remove-orphans
[Install]
WantedBy=multi-user.target
EOF
startAndEnableService $service
}
installService()
{
service=$1
echo "Adding ${service}.service file to systemd..."
cat << EOF | sudo tee /lib/systemd/system/${service}.service
[Unit]
Description=${service} service
Requires=docker.service
After=docker.service
[Service]
Restart=always
ExecStart=docker start -a ${service}
ExecStop=docker stop -t 2 ${service}
[Install]
WantedBy=default.target
EOF
startAndEnableService $service
}
startAndEnableService()
{
service=$1
echo "Reloading systemd daemon..."
sudo systemctl daemon-reload
echo "Enabling ${service}.service"
sudo systemctl enable ${service}.service
echo "Starting ${service}.service"
sudo systemctl start ${service}.service
sleep 1
if systemctl --state=failed --type service | grep -q "${service}.service"
then
echo "Service ${service}.service failed to start.."
sudo systemctl status ${service}.service
exit 1
fi
}
install()
{
mkdir -p frontend-build
sudo chmod -R a+rwx frontend-build
getInstallConfig $1
parseCommands $configurationString
#requireRoot
installDocker
createDockerNetwork
installDockerContainers
saveConfig
}
uninstall()
{
removeConfigs
removeContainers
exit 1
}
removeConfigs()
{
getInstallConfig "uninstall"
if ! [ -z "$instances" ];
then
for (( instance=1; instance <= $instances; ++instance ))
do
containerName="${name}-${instance}"
if systemctl --all --type service | grep -q "${name}-${instance}.service"
then
echo "Stopping, disabling and removing service: ${name}-${instance}.service..."
sudo systemctl stop ${name}-${instance}.service || true
sudo systemctl disable ${name}-${instance}.service || true
sudo rm /lib/systemd/system/${name}-${instance}.service || true
fi
done
fi
sudo systemctl stop ${name}-web.service || true
sudo systemctl disable ${name}-web.service || true
sudo rm /lib/systemd/system/${name}-web.service || true
sudo systemctl daemon-reload
sudo systemctl reset-failed
rm -rf ${configFile} || true
}
removeContainers()
{
echo "Removing docker containers..."
if [ -z "$(docker ps -a | grep gradrix)" ];
then
echo "Nothing to remove.";
else
docker ps -a | grep gradrix | awk '{print $1}' | xargs docker stop | xargs docker rm
echo "Done."
fi
echo "Removing docker images..."
if [ -z "$(docker images -a | grep gradrix)" ];
then
echo "Nothing to remove.";
else
docker images -a | grep gradrix | awk '{print $1}' | xargs docker image rm
fi
}
saveConfig()
{
echo "Saving install config..."
echo "$configurationString" | tee ${configFile}
}
checkIfParametersProvided()
{
# Print helpFunction in case parameters are empty
if [ -z "$configurationString" ] && [ "$opt" != "u" ]
then
echo "Configurations were not provided!";
helpPrint
fi
}
handleCliCommands()
{
if [ $# -eq 0 ]; then
helpPrint
exit 1
fi
while getopts "i:ruh:" opt
do
case "$opt" in
i )
configurationString="$OPTARG"
install ;;
r )
install "reinstall" ;;
u )
uninstall ;;
h ) helpPrint ;; # Print helpFunction in case parameter is non-existent
esac
done
}
handleCliCommands "$@"