forked from openbaton/autoscaling-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap
executable file
·327 lines (289 loc) · 9.7 KB
/
bootstrap
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
#!/bin/sh
. /lib/lsb/init-functions
#general configuration
_PROJECT_NAME="autoscaling-engine"
_DEFAULT_PROPERTIES_FILE="${_PROJECT_BASE}/etc/ase.properties"
_BASE="/opt"
_PROJECT_ROOT_BASE="${_BASE}/openbaton"
_PROJECT_BASE="${_PROJECT_ROOT_BASE}/${_PROJECT_NAME}"
_PROJECT_PROPERTIES_FOLDER="/etc/openbaton"
_PROJECT_PROPERTIES_FILE="${_PROJECT_PROPERTIES_FOLDER}/ase.properties"
_PROJECT_LOG_FOLDER="/var/log/openbaton"
#debian configuration
_DEB_NAME="openbaton-ase"
#git configuration
_PROJECT_REPO="https://github.com/openbaton/autoscaling-engine.git"
_SCRIPT_NAME="autoscaling-engine.sh"
_TAG="develop"
#_TAG="tags/1.0.0"
_USER="$(id -un 2>/dev/null || true)"
checkBinary () {
if command -v $1 >/dev/null 2>&1 ; then
log_success_msg "Checking for '$1'"
return 0
else
log_failure_msg "Checking for '$1'"
return 1
fi
}
_ex='sh -c'
if [ "$_USER" != 'root' ]; then
if checkBinary sudo; then
_ex='sudo -E sh -c'
elif checkBinary su; then
_ex='su -c'
fi
fi
# Checks if the function call of setting properties got the expected values
# $1: description shown for entering value (mandatory)
# $2: configuration property (mandatory)
# $3: default configuration value (optional)
checkPropertyConfiguration () {
# Checking if the description is given
if [ -z "$1" ]; then
echo "-Parameter #1 is not set. Please provide the description of this configration parameter."
return 1
# Checking if the configuration parameter is given
elif [ -z "$2" ]; then
echo "-Parameter #2 is not set. Please provide the configration parameter."
return 1
fi
# Reading the value from the user
export _VALUE
read -p "$1 [$3]:" _VALUE
# Checking if at least one of both values is defined (either default or defined)
if [ -z "${_VALUE}" ] && [ -z "${3}" ]; then
log_failure_msg "You have to provide a value for \"$2\" since there is no default"
checkPropertyConfiguration "$1" "$2" "$3"
elif [ ! -z "${_VALUE}" ]; then
setProperty $2 $_VALUE
log_success_msg "Set parameter \"$2\" to value \"$_VALUE\""
elif [ ! -z "${3}" ]; then
setProperty $2 $3
log_success_msg "Kept parameter \"$2\" as the default value \"$3\""
fi
}
# Set the properties in the configuration file (no further checks)
# $1: configuration property (mandatory)
# $2: configuration property value (mandatory)
setProperty () {
$_ex 'sed -i "s|^'"$1"'=.*|'"$1"'="'$2'"|g" '"$_PROJECT_PROPERTIES_FILE"
}
#property configuration
configureProperties () {
echo "The properties file to change is: $_PROJECT_PROPERTIES_FILE"
echo "NFVO configuration"
checkPropertyConfiguration "Enter the IP of the NFVO" "nfvo.ip" "localhost"
checkPropertyConfiguration "Enter the port of the NFVO" "nfvo.port" "8080"
checkPropertyConfiguration "Enter the username to authorize against the NFVO" "nfvo.username" "admin"
checkPropertyConfiguration "Enter the password to authorize against the NFVO" "nfvo.password" ""
echo "RabbitMQ configuration"
checkPropertyConfiguration "Enter the host address where the RabbitMQ server is running" "autoscaling.rabbitmq.brokerIp" "localhost"
checkPropertyConfiguration "Enter the username used to authorize against the RabbitMQ server" "spring.rabbitmq.username" ""
checkPropertyConfiguration "Enter the password used to authorize against the RabbitMQ server" "spring.rabbitmq.password" ""
}
askForConfiguration () {
echo "$1"
echo "1) Yes"
echo "2) No (default)"
read -p "Your choice: " choice
case $choice in
1) return 0;;
2) return 1;;
*) return 1;;
esac
}
######################
#### Using DEBIAN ####
######################
#adding apt repository
#1. add key
#2. add repo
#3. update repositories
addAptRepository () {
# Add Open Baton Public Key to the APT keys
$_ex 'wget -O - http://get.openbaton.org/keys/public.gpg.key | apt-key add -'
# Add Open Baton Repo to sources.list file
result=$(grep /etc/apt/sources.list -e "deb http://get.openbaton.org/repos/apt/debian/ stable main" | wc -l)
if [ ${result} -eq 0 ]; then
$_ex 'echo "\ndeb http://get.openbaton.org/repos/apt/debian/ stable main" >> /etc/apt/sources.list'
fi
$_ex 'apt-get update'
}
#isntalls the project
installProject () {
$_ex 'apt-get install -y '"$_PROJECT_NAME"
}
#debian re-start of the project
restartProject () {
instance=$(ps aux | grep -v grep | grep "$_PROJECT_NAME" | grep jar | wc -l)
if [ ${instance} -ne 0 ] ; then
echo "restarting $_PROJECT_NAME ..."
"$_PROJECT_NAME" restart
else
echo "starting $_PROJECT_NAME ..."
"$_PROJECT_NAME" start
fi
}
#########################################
########## Using source code ############
#########################################
prereq () {
$_ex 'apt-get update; apt-get -y install openjdk-7-jdk screen git'
log_success_msg "Installed required software"
}
checkEnvironment () {
_error=0
echo "Checking environment..."
checkBinary java; _error=$(($_error + $?))
checkBinary javac; _error=$(($_error + $?))
checkBinary curl; _error=$(($_error + $?))
checkBinary screen; _error=$(($_error + $?))
checkBinary wget; _error=$(($_error + $?))
if [ "0" != "$_error" ]; then
log_failure_msg "FAILED. Please install the above mentioned binaries."
exit 1
fi
}
#create the base where the project is cloned to
createBase () {
echo "Creating the ${_PROJECT_NAME} root base folder \"${_PROJECT_ROOT_BASE}\""
if [ -d "${_PROJECT_ROOT_BASE}" ]; then
if [ -d "${_PROJECT_BASE}" ]; then
echo "Base folder \"${_PROJECT_BASE}\" exists already"
echo "How to proceed?"
echo "1) Keep it"
echo "2) Remove it (default)"
read -p "Your choice: " choice
case $choice in
1) return 0;;
2) $_ex 'rm -rf '"$_PROJECT_BASE"
log_success_msg "Removed old base folder";;
*) $_ex 'rm -rf '"$_PROJECT_BASE"
log_success_msg "Removed old base folder";;
esac
fi
else
$_ex 'mkdir -p '"$_PROJECT_ROOT_BASE"
log_success_msg "Created root base folder \"${_PROJECT_ROOT_BASE}\""
fi
$_ex 'chown -R '"$_USER $_PROJECT_ROOT_BASE"
log_success_msg "Configured permissions of root base folder \"${_PROJECT_ROOT_BASE}\""
}
createLogFolder () {
# create log folder and give permission
if [ -d "${_PROJECT_LOG_FOLDER}" ]; then
echo "Log folder \"${_PROJECT_LOG_FOLDER}\" exists already"
echo "How to proceed?"
echo "1) Keep it (default)"
echo "2) Remove it"
read -p "Your choice: " choice
case $choice in
1) return 0;;
2) $_ex 'rm -rf '"$_PROJECT_LOG_FOLDER"
log_success_msg "Removed log folder \"${_PROJECT_LOG_FOLDER}\""
$_ex 'mkdir -p '"$_PROJECT_LOG_FOLDER"
log_success_msg "Created log folder \"${_PROJECT_LOG_FOLDER}\"";;
*) return 0;;
esac
else
$_ex 'mkdir -p '"$_PROJECT_LOG_FOLDER"
log_success_msg "Created log folder \"${_PROJECT_LOG_FOLDER}\""
fi
$_ex 'chown -R '"$_USER $_PROJECT_LOG_FOLDER"
log_success_msg "Configured permissions of log folder \"${_PROJECT_LOG_FOLDER}\""
}
cloneProject () {
echo "Cloning ${_PROJECT_NAME} to ${_PROJECT_BASE} ..."
oldpath=`pwd`
cd "${_PROJECT_ROOT_BASE}"
git clone --recursive "${_PROJECT_REPO}" "${_PROJECT_NAME}"
cd $oldpath
}
checkoutVersion () {
echo "Choose version:"
echo "1) ${_TAG} (default)"
echo "2) master"
echo "3) develop"
read -p "Your choice: " choice
case $choice in
1) version=${_TAG};;
2) version=master;;
3) version=develop;;
*) version=${_TAG};;
esac
oldpath=`pwd`
cd "${_PROJECT_BASE}"
git checkout ${version}
cd $oldpath
}
copyConfigFiles () {
if [ ! -d "${_PROJECT_PROPERTIES_FOLDER}" ]; then
$_ex 'mkdir -p '"${_PROJECT_PROPERTIES_FOLDER}"
log_success_msg "created properties folder"
else
log_warning_msg "Properties folder \"${_PROJECT_PROPERTIES_FOLDER}\" exists already"
fi
$_ex 'cp '"${_DEFAULT_PROPERTIES_FILE} ${_PROJECT_PROPERTIES_FILE}"
log_success_msg "Copied default configuration file \"${_DEFAULT_PROPERTIES_FILE}\" to \"${_PROJECT_PROPERTIES_FOLDER}\""
if [ -d "${_PROJECT_BASE}/etc" ]; then
$_ex 'cp '"${_PROJECT_BASE}/etc/* ${_PROJECT_PROPERTIES_FOLDER}"
log_success_msg "Copied files from \"${_PROJECT_BASE}/etc\" to \"${_PROJECT_PROPERTIES_FOLDER}\""
fi
}
compileProject () {
echo "Compiling ${_PROJECT_NAME}"
oldpath=`pwd`
cd "${_PROJECT_BASE}"
./${_SCRIPT_NAME} compile
if [ $? -ne 0 ]; then
echo "ERROR: The compilation of ${_PROJECT_NAME} failed"
exit 1
fi
cd $oldpath
}
startProject () {
echo "Starting ${_PROJECT_NAME}"
oldpath=`pwd`
cd ${_PROJECT_BASE}
./${_SCRIPT_NAME} start
cd $oldpath
}
#################
### INSTALLER ###
#################
useDebian () {
#Add apt repository
addAptRepository
#Install project
installProject
#configure properties
configureProperties
#start or restart project
restartProject
}
useSourceCode () {
prereq
checkEnvironment
createBase
createLogFolder
cloneProject
checkoutVersion
copyConfigFiles
configureProperties
compileProject
startProject
}
bootstrap () {
echo "How to install ${_PROJECT_NAME}?"
echo "1) debian (default)"
echo "2) source code via git"
read -p "Your choice: " choice
case $choice in
1) useDebian;;
2) useSourceCode;;
*) useDebian;;
esac
echo "$_PROJECT_NAME is up and running now..."
}
bootstrap