-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
executable file
·344 lines (285 loc) · 10.2 KB
/
install.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
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
#!/bin/bash
#####################################################################
# Print warning message.
function warningv()
{
echo "$*" >&2
}
#####################################################################
# Print error message and exit.
function error()
{
echo "$*" >&2
exit 1
}
function install_docker()
{
sudo apt-get update
sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common -y
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io -y
}
function install_compose()
{
sudo curl -L "https://github.com/docker/compose/releases/download/1.28.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
# docker-compose --version
}
#####################################################################
# Ask yesno question.
#
# Usage; yesno OPTIONS QUESTION
#
# Options;
# --timeout N Timeout if no input seen in N seconds.
# --default ANS Use ANS as the default answer on timeout or
# if an empty answer is provided.
#
# Exit status is the answer.
function yesno()
{
local ans
local ok=0
local timeout=0
local default
local t
while [[ "$1" ]]
do
case "$1" in
--default)
shift
default=$1
if [[ ! "$default" ]]; then error "Missing default value"; fi
t=$(tr '[;upper;]' '[;lower;]' <<<$default)
if [[ "$t" != 'y' && "$t" != 'yes' && "$t" != 'n' && "$t" != 'no' ]]; then
error "Illegal default answer; $default"
fi
default=$t
shift
;;
--timeout)
shift
timeout=$1
if [[ ! "$timeout" ]]; then error "Missing timeout value"; fi
if [[ ! "$timeout" =~ ^[0-9][0-9]*$ ]]; then error "Illegal timeout value; $timeout"; fi
shift
;;
-*)
error "Unrecognized option; $1"
;;
*)
break
;;
esac
done
if [[ $timeout -ne 0 && ! "$default" ]]; then
error "Non-zero timeout requires a default answer"
fi
if [[ ! "$*" ]]; then error "Missing question"; fi
while [[ $ok -eq 0 ]]
do
if [[ $timeout -ne 0 ]]; then
if ! read -t $timeout -p "$*" ans; then
ans=$default
else
# Turn off timeout if answer entered.
timeout=0
if [[ ! "$ans" ]]; then ans=$default; fi
fi
else
read -p "$*" ans
if [[ ! "$ans" ]]; then
ans=$default
else
ans=$(tr '[;upper;]' '[;lower;]' <<<$ans)
fi
fi
if [[ "$ans" == 'y' || "$ans" == 'yes' || "$ans" == 'n' || "$ans" == 'no' ]]; then
ok=1
fi
if [[ $ok -eq 0 ]]; then warning "Valid answers are; yes y no n"; fi
done
[[ "$ans" = "y" || "$ans" == "yes" ]]
}
declare -a PROFILES
echo -e "\n\n"\
"+*********************************************************\n"\
"** **\n"\
"** CIPio Installer v1.0 Beta **\n"\
"** **\n"\
"** Welcome to the Common Integrtion Platform Installer **\n"\
"** **\n"\
"** Default containers include the following: **\n"\
"** - Node-Red **\n"\
"** - InfluxDB **\n"\
"** - MongoDB and Mongo Express **\n"\
"** - Grafana **\n"\
"** - Portainer **\n"\
"** - Watchtower **\n"\
"** **\n"\
"** Hit Ctrl-C anytime to abort setup **\n"\
"**********************************************************\n\n"
if [[ "$EUID" != 0 ]]; then
echo "We need to elevate you to root level. Please enter root password"
sudo -k
if sudo true; then
echo "Good, you are now at root level"
else
error "Incorrect password.. Exiting installation process"
fi
fi
echo -e "\n"
echo "Checking for Docker..."
if hash docker 2>/dev/null; then
echo "It appears you already have docker installed. Good."
else
echo "We need to first install docker on your system. Hit <ENTER> to continue:"
read
install_docker
if hash docker 2>/dev/null; then
echo "Docker now installed."
sudo addgroup docker
sudo usermod -aG docker ${USER}
else
error "Installation od Docker has failed. Exiting setup"
fi
fi
echo -e "\n"
echo "Checking for Docker-Compose..."
if hash docker-compose 2>/dev/null; then
echo "It appears you already have docker-compose installed. Good."
else
echo "We need to install docker-compose on your system. Hit <ENTER> to continue:"
read
install_compose
if hash docker-compose 2>/dev/null; then
echo "Docker-Compose now installed."
else
error "Installation of Docker-Compose has failed. Exiting setup"
fi
fi
echo -e "\n"
echo -e "Please provide the path to the folder which will become the root folder of our docker local volumes.\n"\
"The default path is /svr/docker. Do not use a trailing slash"
read -p "Path to local volume root folder: " -e -i "/srv/docker" CIPIOROOT
echo "CIPIOROOT=$CIPIOROOT" > .env
echo "COMPOSE_PROJECT_NAME=cipio" >> .env
echo -e "\n"
## Install all the default containers?
if yesno --default yes "Would you like to install all the default containers for CIPio (Y/n) ? "; then
echo "All default containers will be installed."
PROFILES+=("default")
## echo "MONGOUSER=root">> .env
## echo "MONGOPW=root">> .env
else
echo -e "\n"
## Node Red single instance
if yesno --default yes "Would you like to install Node-Red? (Y/n) ? "; then
echo "Node-Red will be installed."
PROFILES+=("nodered")
else
echo "Skipping Node-Red"
fi
echo -e "\n"
## MongoDB and Mongo Express
if yesno --default yes "Would you like to install MongoDB? (Y/n) ? "; then
echo "MongoDB will be installed."
PROFILES+=("mongodb")
## echo "MONGOUSER=root">> .env
## echo "MONGOPW=root">> .env
if yesno --default yes "Would you like to install Mongo Express? (Y/n) ? "; then
echo "Mongo Express will be installed"
PROFILES+=("mongoexpress")
else
echo "Skipping Mongo Express"
fi
else
echo "Skipping MongoDB and Mongo Express..."
fi
echo -e "\n"
## Mosquitto
if yesno --default yes "Would you like to install Mosquitto MQTT broker? (Y/n) ? "; then
echo "Mosquitto/MQTT will be installed."
PROFILES+=("mosquitto")
else
echo "Skipping Mosquitto/MQTT..."
fi
echo -e "\n"
## InfluxDB
if yesno --default yes "Would you like to install InfluxDB 1.8? (Y/n) ? "; then
echo "InfluxDB 1.8 will be installed."
PROFILES+=("influxdb")
else
if yesno --default yes "Would you like to install InfluxDB 2.x? (Y/n) ? "; then
echo "InfluxDB 2.x will be installed. Use port 8087 to connect."
PROFILES+=("influxdb2")
else
echo "Skipping InfluxDB"
fi
fi
echo -e "\n"
## Grafana
if yesno --default yes "Would you like to install Grafana? (Y/n) ? "; then
echo "Grafana will be installed."
PROFILES+=("grafana")
else
echo "Skipping Grafana..."
fi
echo -e "\n"
## Portainer
if yesno --default yes "Would you like to install Protainer? (Y/n) ? "; then
echo "Portainer will be installed."
PROFILES+=("portainer")
else
echo "Skipping Portainer..."
fi
echo -e "\n"
## Watchtower
if yesno --default yes "Would you like to install Watchtower? (Y/n) ? "; then
echo "Watchtowner will be installed."
PROFILES+=("watchtower")
else
echo "Skipping Watchtower..."
fi
fi
echo -e "\n\n"
read -p "Hit return to start the installation of the following caontainers:( ${PROFILES[*]} )"
## Get the path to the currently running script
## We need this for when we su to the current user
## We need to sudo su <user> to that we pick up the new group assignment
##########################################################################
SCRIPT_PATH="`dirname \"$0\"`" # relative
SCRIPT_PATH="`( cd \"$MY_PATH\" && pwd )`" # absolutized and normalized
## Put the profiles in the .env file.
## We need to do this to persist the profiles. The original
## way of using an env var is lost when sudo su <user>
## Old way>> COMPOSE_PROFILE=$(IFS=,; echo "${PROFILES[*]}") docker-compose up -d
#############################################################
echo COMPOSE_PROFILE=$(IFS=,; echo "${PROFILES[*]}") >> .env
sudo su -c "cd ${SCRIPT_PATH} && docker-compose up -d" - ${USER}
## We need to set the proper owner to the nodered volume
##
if [[ ${PROFILES[*]} =~ (^|[[:space:]])'nodered'($|[[:space:]]) ]] || [[ ${PROFILES[*]} =~ (^|[[:space:]])'default'($|[[:space:]]) ]]; then
echo "Updating ownership of ${CIPIOROOT}/node-red..."
sudo docker stop node-red
sudo chown -R ${USER}:${USER} ${CIPIOROOT}/node-red
echo "Restarting node-red container..."
sudo docker restart node-red
fi
if [[ ${PROFILES[*]} =~ (^|[[:space:]])'mosquitto'($|[[:space:]]) ]] || [[ ${PROFILES[*]} =~ (^|[[:space:]])'default'($|[[:space:]]) ]];then
# echo "Updating ownership of ${CIPIOROOT}/mosquitto..."
# sudo mkdir ${CIPIOROOT}/mosquitto
# sudo mkdir ${CIPIOROOT}/mosquitto/data
# sudo mkdir ${CIPIOROOT}/mosquitto/log
# sudo mkdir ${CIPIOROOT}/mosquitto/config
echo "Copying config files to ${CIPIOROOT}/mosquitto/config..."
sudo cp -v ./mqtt/mosquitto.conf ${CIPIOROOT}/mosquitto/config
sudo cp -v ./mqtt/passwd ${CIPIOROOT}/mosquitto/config
echo "Restarting Mosquitto/MQTT container..."
sudo docker restart mosquitto
fi
echo -e "\n\n[Note: You may need to restart your terminal in order to do certain docker commands without using sudo]\n\n"
echo "Installation of CIPio complete"
exit 0