-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnexenta-fusion-installer.sh
More file actions
executable file
·434 lines (368 loc) · 13.5 KB
/
nexenta-fusion-installer.sh
File metadata and controls
executable file
·434 lines (368 loc) · 13.5 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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#!/bin/bash
defaultFusionPath="$(eval echo ~$SUDO_USER)/fusion"
containerName="nexenta-fusion"
# text formatting variables
textBold=$(tput bold)
textNormal=$(tput sgr0)
textRed='\033[0;31m'
textBlue='\033[0;36m'
textYellow='\033[38;5;226m'
textLightGray='\033[1;37m'
textNc='\033[0m' # N
# it is recommended to set heap size less or equal to 31gib
# 31g = 1000 * 1000 * 1000 * 31 bytes
defaultHeapSizeLimitBytes=$(( 1024 * 1024 * 1024 * 31 ))
defaultHeapSizeLimitG=31
ask() {
echo
# echo question
echo -e "${textBlue}$1${textNc}"
# echo description
echo "----------------"
echo -e "${textLightGray}$2${textNc}"
echo "----------------"
}
getIps() {
# CentOS 7 does not have ifconfig preinstalled
# MacOS does not have ip preinstalled
if [ -x "$(which ifconfig 2> /dev/null)" ]; then
ips=$(ifconfig)
elif [ -x "$(which ip)" ]; then
ips=$(ip addr)
fi
ips=($(echo "${ips}" | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*'))
}
getTimezone() {
# Ubuntu
if [ -f /etc/timezone ]; then
OLSONTZ=$(cat /etc/timezone)
elif [ -x "$(sudo command -v systemsetup)" ]; then
# MacOS
OLSONTZ=$(sudo systemsetup -gettimezone | grep -Eo '(\w+\/\w+)')
elif [ -h /etc/localtime ]; then
# CentOS
OLSONTZ=$(readlink /etc/localtime | grep -Eo "\w+\/\w+$")
else
checksum=$(md5sum /etc/localtime | cut -d' ' -f1)
OLSONTZ=$(find /usr/share/zoneinfo/ -type f -exec md5sum {} \; | grep "^$checksum" | sed "s/.*\/usr\/share\/zoneinfo\///" | head -n 1)
fi
}
calculateRAM() {
# NOTE: ESDB heap size is calculated in units defined as powers of 2
isLowMemory=false
if [ -x "/usr/sbin/system_profiler" ]; then
# mac os way to find total RAM
totalMemory=$(system_profiler SPHardwareDataType | grep " Memory:" | grep -Eo '[0-9]+')
# for mac users we expect that they all have RAM more than 1g and which can be divided by 2
# in other cases we set 1g as default
defaultHeapSize=$(( ${totalMemory} / 2 ))
if [ $defaultHeapSize -gt $defaultHeapSizeLimitG ]; then
defaultHeapSize=$defaultHeapSizeLimitG
fi
defaultHeapSize="${defaultHeapSize}g"
if ! [[ $defaultHeapSize =~ ^[0-9]+g$ ]]; then
defaultHeapSize="1g"
fi
totalMemory=${totalMemory}g
elif [ -f "/proc/meminfo" ]; then
# linux way to find total RAM
# total memory is presented in kb
totalMemory=$(cat /proc/meminfo | grep -Po "(?<=MemTotal:)(\s+)(\w+)" | grep -Eo "\w+")
# converting totalMemory to bytes
totalMemory=$(( totalMemory * 1000 ))
onegib=$(( 1024*1024*1024 ))
# if total memory is lower than 1gib
# we won't allow to run a container via this script
if [[ $totalMemory -lt $onegib ]]; then
isLowMemory=true
fi
defaultHeapSize=$(( totalMemory / 2))
if [ $defaultHeapSize -gt $defaultHeapSizeLimitBytes ]; then
# if half of memory is more than 31gib we set 31gib as default heap size
defaultHeapSize="31g"
# if there is more than 1gib but less than 2gib of total RAM
# set default heap size 1gib
# 1024^3bytes = 1gib
elif [[ $defaultHeapSize -lt $onegib ]]; then
defaultHeapSize="1g"
else
# converting to mibibytes since result of division may be not an integer number
# of gibibytes
# 1mib = 1024^2
defaultHeapSize=$( numfmt --to-unit=1048576 --suffix=m ${defaultHeapSize})
fi
# converting to human readable value
totalMemory=$( numfmt --round=nearest --to=iec ${totalMemory})
fi
}
displayIpOptions() {
local name=$1[@]
local options=("${!name}");
for ((i=0; i < ${#options[@]}; i++)) {
if [[ "$previouslyUsedManagementIp" == ${options[$i]} ]]; then
local comment="(previously used by NexentaFusion container)"
else
local comment=""
fi
echo "$(($i + 1))) ${options[$i]} ${comment}"
}
echo "Type a number and press enter"
}
readSelectedOption() {
local name=$1[@]
local options=("${!name}");
local correct=0
while [ $correct -eq 0 ]; do
read input
if [[ "$input" =~ [1-${#options[@]}] ]]; then
correct=1;
else
echoRed "Invalid option. Please enter valid option"
fi
done
return $input
}
getFusionUiStatus() {
if curl --output /dev/null --silent --head --insecure --fail http://$1:8457; then
echo up
else
echo down
fi
}
isMacOS() {
if [[ "$OSTYPE" = "darwin"* ]]; then
return 0
else
return 1
fi
}
isCentOS() {
if [ -f /etc/redhat-release ]; then
return 0
else
return 1
fi
}
# text functions
echoBlue() {
echo -e "${textBlue}${1}${textNc}"
}
echoRed() {
echo -e "${textRed}${1}${textNc}"
}
echoYellow() {
echo -e "${textYellow}${1}${textNc}"
}
echoError() {
echo -e "${textRed}Error: ${1}${textNc}" >&2
}
echoDefaults() {
ask "Defaults have been selected for the following parameters:" "ESDB heap size is the memory reserved for the analytics database. The default recommendation is half of the total system memory, but not more than 31g (your machine has $totalMemory of RAM)."
echo "ESDB heap size:${textBold} ${defaultHeapSize} ${textNormal}"
echo "Timezone:${textBold} $OLSONTZ ${textNormal}"
echo "NexentaFusion folders path:${textBold} $defaultFusionPath ${textNormal}"
}
#
prepareContainerParams() {
managementIp=${ips[$selectedIpIndex]}
heapSize=$typedHeapSize
if [ -z "$typedTZ" ]; then
tz=$OLSONTZ
else
tz=$typedTZ
fi
if [ -z "$typedFusionPath" ]; then
path=$defaultFusionPath
else
path=$typedFusionPath
fi
if [ -z "$typedHeapSize" ]; then
heapSize=$defaultHeapSize
else
heapSize=$typedHeapSize
fi
}
runContainer() {
# show summary if defaults were not accepted
if [ "$1" = "n" ]; then
echoBlue "The NexentaFusion container will be run with the following parameters:"
echo "Management IP:${textBold} $managementIp${textNormal}"
echo "ESDB heap size:${textBold} ${heapSize} ${textNormal}"
echo "Timezone:${textBold} $tz ${textNormal}"
echo "NexentaFusion folders path:${textBold} $path ${textNormal}"
echo
echo "Press any key to continue"
read
fi
# checking if there is new image
echoBlue "Checking the NexentaFusion container image..."
docker pull nexenta/fusion
echoBlue "Running the NexentaFusion container..."
dockerRunCommand="sudo docker run --name $containerName -v $path/elasticsearch:/var/lib/elasticsearch:z -v $path/nef:/var/lib/nef:z -e MGMT_IP=$managementIp --ulimit nofile=65536:65536 --ulimit memlock=-1:-1 -e ES_HEAP_SIZE=$heapSize -e TZ=$tz -p 8457:8457 -p 9200:9200 -p 8443:8443 -p 2000:2000 -i -d nexenta/fusion"
# hide docker run output in case of existing image (we don't want to display a created container id)
$dockerRunCommand 1> /dev/null
if [ $? -gt 0 ]; then
echoRed "Error during running a container"
exit 1
fi
echo -e "Container with name ${textLightGray}$containerName${textNc} was created"
echoBlue "Waiting for NexentaFusion to start"
uiStatus="down"
while [ $uiStatus = "down" ]; do
echo -n .
uiStatus=$(getFusionUiStatus ${managementIp})
sleep 1
done
echo
echoBlue "NexentaFusion is available at https://${managementIp}:8457"
}
### WIZARD START
# start script as sudo
if (( $EUID != 0 )); then
echo "Starting nexenta-fusion-installer as sudo"
echo
exec sudo $0 "$@"
fi
# welcome message
echo "This utility will walk you through installing NexentaFusion to run as a Docker container."
echo "Refer to the NexentaFusion Installation QuickStart guide for additional details."
echo
echo "The required information will be requested and minimums confirmed."
echo
echo "NexentaFusion uses ports 2000, 8443, 8457 and 9200"
echo "Ensure that your firewall allows access to the above."
if isCentOS; then
echo
echo "The container must be able to access port 9200 using the the management address. This may require changes to iptables."
fi
echo
echo "Press ^C at any time to quit."
echo
# check if there is enough memory
calculateRAM
if $isLowMemory; then
echoRed "The OS reports ${totalMemory}, which is less than the 1g minimum."
echoRed "NexentaFusion may not operate properly."
echo
echoRed "Exiting..."
exit 1
fi
# check if docker is installed
if ! [ -x "$(which docker)" ]; then
echoRed "Docker is not installed. Please install Docker (https://www.docker.com/community-edition#/download) and then run the nexenta-fusion-installer again" >&2
exit 1
fi
# check if docker daemon is up
dockerDaemonStarted=true
# docker info will exit with error code, if docker daemon is no up
docker info &> /devnull || dockerDaemonStarted=false
if ! $dockerDaemonStarted; then
echoRed "Docker daemon is not running"
echoBlue "Starting docker daemon..."
if isMacOS; then
open --background -a Docker
else
sudo service docker start
fi
# wait until docker daemon is up
while ! docker info &> /devnull; do sleep 1; done
fi
# check if fusion container is already exists
if [ -n "$(docker ps -a -f "name=${containerName}" | grep ${containerName})" ]; then
echo "You already have an existing NexentaFusion container"
echo "Do you want to stop and remove the current container and run a new one?"
echo "This will not effect your NexentaFusion data"
echo "[y/N]"
read removeCurrentContainer
previouslyUsedManagementIp=$(docker inspect nexenta-fusion | grep MGMT | grep -Eo '([0-9]+\.){3}[0-9]+')
if [ "$removeCurrentContainer" = "y" ]; then
echoBlue "Removing current container..."
sudo docker rm -f $containerName 1>/dev/null
else
echoBlue "Exiting..."
exit 0
fi
fi
getTimezone
### Questions
### Question 0
getIps
ask "Please select the management address:" "This IP address is used by appliances for pushing analytics data, logs, events"
displayIpOptions ips
readSelectedOption ips
selectedIpIndex=$(( $? - 1 ))
### Question 1
echoDefaults
echo
echo "Type y to accept the defaults. Type n to change the values"
echo "[Y/n]"
read isDefaultsAccpeted
if [ "$isDefaultsAccpeted" = "n" ]; then
### Question 2
ask "Type the timezone or press enter to retain the default ($OLSONTZ)" "Timezone is used for correct processing of logs"
read typedTZdd
### Question 3
ask "Type the ESDB heap size or press enter to retain the default ($defaultHeapSize)" "Enter the quantity of memory to reserve for the analytics database or press enter to accept the default. The default recommendation is half of the total system memory, with a minimum of 1g and a maximum of 31g.\nExample: 8g"
echo "Your machine has $totalMemory of RAM"
echo "Type a heap size and press enter"
read typedHeapSize
# check if heap size is correctly typed and more or equal to 1000m
if [ -n "$typedHeapSize" ]; then
heapSizeRegex="^[0-9]+[m,g]$"
while true; do
if ! [[ "$typedHeapSize" =~ $heapSizeRegex ]]; then
echoRed "Invalid value. Please enter valid value. Examples: 2048m, 16g"
# validate mebibytes input
elif [[ "$typedHeapSize" =~ [0-9]+m ]]; then
mebibytes=$(echo ${typedHeapSize} | grep -Eo "[0-9]+")
if [[ $mebibytes -lt 1024 ]]; then
echoRed "Invalid value. Heap size must be greater or equal to 1g. Please type correct value"
echo "Type a heap size and press enter"
elif [[ $mebibytes -gt 1024*31 ]]; then
echoRed "Invalid value. Heap size must be less or equal to 31g. Please type correct value"
echo "Type a heap size and press enter"
else
break
fi
# validate gibibytes input
elif [[ "$typedHeapSize" =~ [0-9]+g ]]; then
gibibytes=$(echo ${typedHeapSize} | grep -Eo "[0-9]+")
if [[ $gibibytes -gt $defaultHeapSizeLimitG ]]; then
echoRed "Invalid value. Heap size must be less or equal to 31g. Please type correct value"
echo "Type a heap size and press enter"
else
break
fi
else
break
fi
read typedHeapSize
done
fi
### Question 4
ask "Type NexentaFusion path or press enter to retain the default ($defaultFusionPath)" "This directory is used to store NexentaFusion and ESDB data"
read typedFusionPath
fi
prepareContainerParams
# clean install means that data is not exported from other NexentaFusion container
isCleanInstall=true
if [ -d "${path}/nef" ] && [ -d "${path}/elasticsearch" ]; then
echo
echo "There is data from previous NexentaFusion container in specified path";
echo "Do you want to use it?"
echo "[Y/n]"
read useOldData
if [ "$useOldData" = "n" ]; then
echoBlue "Removing previous NexentaFusion container data..."
rm -rf $path/nef
rm -rf $path/elasticsearch
else
isCleanInstall=false
fi
fi
runContainer $isDefaultsAccpeted
if "$isCleanInstall" = true; then
echo
echo "Default login/password: admin/nexenta"
echo
fi