aboutsummaryrefslogtreecommitdiffstats
path: root/ransim/docker/ransim-docker/docker-install.sh
blob: cbfe0b17a436c93ca0894359b165678bc3d37676 (plain)
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
#!/bin/bash

#########################################################################
##
## Functions
##
#########################################################################

function usage() {
	echo -n "syntax: $(basename $0) "
	echo -n "--debug ("
	echo -n "[--install base|ransim|mysql] | "
	echo -n "[--configure base|ransim|mysql] | "
}

function check_java() {
	if [[ $DEBUG == y ]]; then
		echo "-- ${FUNCNAME[0]} $@ --"
		set -x
	fi

	TARGET_JAVA_VERSION=$1

	if [[ -z ${JAVA_HOME} ]]; then
		echo "error: ${JAVA_HOME} is not set"
		return 1
	fi

	if ! check_x_file "${JAVA_HOME}/bin/java"; then
		echo "error: ${JAVA_HOME}/bin/java is not accessible"
		return 1
	fi

	INSTALLED_JAVA_VERSION=$("${JAVA_HOME}/bin/java" -version 2>&1 | awk -F '"' '/version/ {print $2}')
	if [[ -z $INSTALLED_JAVA_VERSION ]]; then
		echo "error: ${JAVA_HOME}/bin/java is invalid"
		return 1
	fi

	if [[ "${INSTALLED_JAVA_VERSION}" != ${TARGET_JAVA_VERSION}* ]]; then
		echo "error: java version (${INSTALLED_JAVA_VERSION}) does not"\
			 "march desired version ${TARGET_JAVA_VERSION}"
		return 1
	fi

	echo "OK: java ${INSTALLED_JAVA_VERSION} installed"

}

function process_configuration() {
	if [[ $DEBUG == y ]]; then
		echo "-- ${FUNCNAME[0]} $@ --"
		set -x
	fi

	CONF_FILE=$1
	while read line || [ -n "${line}" ]; do
        if [[ -n ${line} ]] && [[ ${line} != \#* ]]; then
	        name=$(echo "${line%%=*}")
	        value=$(echo "${line#*=}")
	        # escape ampersand so that sed does not replace it with the search string
            value=${value//&/\\&}
	        if [[ -z ${name} ]] || [[ -z $value ]]; then
	        	echo "WARNING: ${line} missing name or value"
	    	fi
	    	export ${name}="${value}"
	        eval "${name}" "${value}" 2> /dev/null
        fi
	done < "${CONF_FILE}"
	return 0
}

function component_preconfigure() {
	if [[ $DEBUG == y ]]; then
		echo "-- ${FUNCNAME[0]} $@ --"
		set -x
	fi

	/bin/sed -i -e 's!${{POLICY_HOME}}!'"${POLICY_HOME}!g" \
		-e 's!${{FQDN}}!'"${FQDN}!g" \
		*.conf > /dev/null 2>&1
}

function tomcat_component() {
	if [[ $DEBUG == y ]]; then
		echo "-- ${FUNCNAME[0]} $@ --"
		set -x
	fi

	TOMCAT_TARGET_INSTALL_DIR=${POLICY_HOME}/servers/${COMPONENT_TYPE}
	if [[ -d ${TOMCAT_TARGET_INSTALL_DIR} ]]; then
		echo "error: ${TOMCAT_TARGET_INSTALL_DIR} exists."
		return 1
	fi

	TOMCAT_INSTALL_DIR=${POLICY_HOME}/install/3rdparty/${TOMCAT_PACKAGE_NAME}/
	if [[ -d ${TOMCAT_INSTALL_DIR} ]]; then
		echo "error: ${TOMCAT_INSTALL_DIR} exists."
		return 1
	fi

	tar -C "${POLICY_HOME}/servers" -xf "${POLICY_HOME}/install/3rdparty/${TOMCAT_PACKAGE_NAME}.tar.gz"

	mv "${POLICY_HOME}/servers/${TOMCAT_PACKAGE_NAME}" "${POLICY_HOME}/servers/${COMPONENT_TYPE}/"
	/bin/cp "${POLICY_HOME}"/install/servers/common/tomcat/bin/* "${POLICY_HOME}/servers/${COMPONENT_TYPE}/bin"
	/bin/cp "${POLICY_HOME}"/install/servers/common/tomcat/conf/* "${POLICY_HOME}/servers/${COMPONENT_TYPE}/conf"

	/bin/cp "${POLICY_HOME}/install/servers/common/tomcat/init.d/tomcatd" "${POLICY_HOME}/etc/init.d/${COMPONENT_TYPE}"
	/bin/sed -i -e "s!\${{COMPONENT_TYPE}}!${COMPONENT_TYPE}!g" "${POLICY_HOME}/etc/init.d/${COMPONENT_TYPE}" >/dev/null 2>&1


	/bin/cp -fr "${POLICY_HOME}"/install/servers/${COMPONENT_TYPE}/webapps/* "${POLICY_HOME}/servers/${COMPONENT_TYPE}/webapps"
	/bin/cp -fr "${POLICY_HOME}"/install/servers/${COMPONENT_TYPE}/bin/* "${POLICY_HOME}/servers/${COMPONENT_TYPE}/bin" >/dev/null 2>&1
	/bin/cp -fr "${POLICY_HOME}"/install/servers/${COMPONENT_TYPE}/conf/* "${POLICY_HOME}/servers/${COMPONENT_TYPE}/conf" >/dev/null 2>&1

	/bin/rm -fr "${POLICY_HOME}/servers/${COMPONENT_TYPE}/webapps/docs" \
		 "${POLICY_HOME}/servers/${COMPONENT_TYPE}/webapps/examples" \
		 "${POLICY_HOME}/servers/${COMPONENT_TYPE}/webapps/ROOT" \
		 "${POLICY_HOME}/servers/${COMPONENT_TYPE}/webapps/manager" \
		 "${POLICY_HOME}/servers/${COMPONENT_TYPE}/webapps/host-manager"

	return 0
}

function configure_tomcat_component() {
	configure_component "${COMPONENT_TYPE}.conf" "${POLICY_HOME}/servers/${COMPONENT_TYPE}/"
}

function configure_component() {
	if [[ $DEBUG == y ]]; then
		echo "-- ${FUNCNAME[0]} $@ --"
		set -x
	fi

	if ! process_configuration "${COMPONENT_TYPE}.conf"; then
		echo "error: aborting ${COMPONENT_TYPE} installation: cannot process configuration ${COMPONENT_TYPE}.conf"
		exit 1
	fi

	CONF_FILE=$1
	COMPONENT_ROOT_DIR=$2

	SED_LINE="sed -i"
	SED_LINE+=" -e 's!\${{POLICY_HOME}}!${POLICY_HOME}!g' "
	SED_LINE+=" -e 's!\${{POLICY_USER}}!${POLICY_USER}!g' "
	SED_LINE+=" -e 's!\${{POLICY_GROUP}}!${POLICY_GROUP}!g' "
	SED_LINE+=" -e 's!\${{KEYSTORE_PASSWD}}!${KEYSTORE_PASSWD}!g' "
	SED_LINE+=" -e 's!\${{JAVA_HOME}}!${JAVA_HOME}!g' "
	SED_LINE+=" -e 's!\${{COMPONENT_TYPE}}!${COMPONENT_TYPE}!g' "

	while read line || [ -n "${line}" ]; do
        if [[ -n $line ]] && [[ $line != \#* ]]; then
	        name=$(echo "${line%%=*}")
	        value=$(echo "${line#*=}")
	        # escape ampersand so that sed does not replace it with the search string
        	value=${value//&/\\&}
	    	if [[ -z ${name} ]] || [[ -z ${value} ]]; then
	        	echo "WARNING: ${line} missing name or value"
	    	fi
	    	SED_LINE+=" -e 's!\${{${name}}}!${value}!g' "

        fi
	done < "$CONF_FILE"

	SED_FILES=""
	for sed_file in $(find "${COMPONENT_ROOT_DIR}" -name '*.xml' -o -name '*.sh' -o -name '*.properties' -o -name '*.conf' -o -name '*.cfg' -o -name '*.template' -o -name '*.conf' -o -name '*.cron' -o -name '*.json' | grep -v /backup/); do
		if fgrep -l '${{' ${sed_file} > /dev/null 2>&1; then
			SED_FILES+="${sed_file} "
		fi
	done

	if [[ -f $HOME/.m2/settings.xml ]]; then
		SED_FILES+="$HOME/.m2/settings.xml "
	fi


	if [[ -z ${SED_FILES} ]]; then
		echo "WARNING: no xml, sh, properties, or conf files to perform configuration expansion"
	else
		SED_LINE+=${SED_FILES}
		eval "${SED_LINE}"
	fi

	list_unexpanded_files ${POLICY_HOME}
}

function check_r_file() {
	if [[ $DEBUG == y ]]; then
		echo "-- ${FUNCNAME[0]} $@ --"
		set -x
	fi

	FILE=$1
	if [[ ! -f ${FILE} || ! -r ${FILE} ]]; then
        return 1
	fi

	return 0
}

function check_x_file() {
	if [[ $DEBUG == y ]]; then
		echo "-- ${FUNCNAME[0]} $@ --"
		set -x
	fi

	FILE=$1
	if [[ ! -f ${FILE} || ! -x ${FILE} ]]; then
        return 1
	fi

	return 0
}

function install_prereqs() {
	if [[ $DEBUG == y ]]; then
		echo "-- ${FUNCNAME[0]} $@ --"
		set -x
	fi

	CONF_FILE=$1

	if ! check_r_file "${CONF_FILE}"; then
		echo "error: aborting ${COMPONENT_TYPE} installation: ${CONF_FILE} is not accessible"
		exit 1
	fi

	if ! process_configuration "${CONF_FILE}"; then
		echo "error: aborting ${COMPONENT_TYPE} installation: cannot process configuration ${CONF_FILE}"
		exit 1
	fi

#	if ! check_java "1.8"; then
#		echo "error: aborting ${COMPONENT_TYPE} installation: invalid java version"
#		exit 1
#	fi

	if [[ -z ${POLICY_HOME} ]]; then
		echo "error: aborting ${COMPONENT_TYPE} installation: ${POLICY_HOME} is not set"
		exit 1
	fi

	HOME_OWNER=$(ls -ld "${POLICY_HOME}" | awk '{print $3}')
	if [[ ${HOME_OWNER} != ${POLICY_USER} ]]; then
		echo "error: aborting ${COMPONENT_TYPE} installation: ${POLICY_USER} does not own ${POLICY_HOME} directory"
		exit 1
	fi

	echo -n "Starting ${OPERATION} of ${COMPONENT_TYPE} under ${POLICY_USER}:${POLICY_GROUP} "
	echo "ownership with umask $(umask)."
}

function list_unexpanded_files() {
	ROOT_DIR=$1
	SEARCH_LIST=$(find ${ROOT_DIR} -type f -name '*.properties' -o -name '*.sh'  -o -name '*.conf' -o -name '*.yml' -o -name '*.template' -o -name '*.xml' -o -name '*.cfg' -o -name '*.json' -o -path "${ROOT_DIR}/etc/init.d/*" | egrep -v '/m2/|/install/|/logs/')
    NOT_EXPANDED_BASE_FILES=$(grep -l '${{' ${SEARCH_LIST} 2> /dev/null)
	if [[ -n ${NOT_EXPANDED_BASE_FILES} ]]; then
		echo "error: component installation has completed but some base files have not been expanded:"
		echo "${NOT_EXPANDED_BASE_FILES}"
		return 1
	fi
	return 0
}

function install_base() {
	if [[ $DEBUG == y ]]; then
		echo "-- ${FUNCNAME[0]} $@ --"
		set -x
	fi

	install_prereqs "${BASE_CONF}"

	if [[ -z ${POLICY_HOME} ]]; then
		echo "error: ${POLICY_HOME} is not set"
		exit 1
	fi

	POLICY_HOME_CONTENTS=$(ls -A "${POLICY_HOME}" 2> /dev/null)
	if [[ -n ${POLICY_HOME_CONTENTS} ]]; then
		echo "error: aborting base installation: ${POLICY_HOME} directory is not empty"
		exit 1
	fi

	if [[ ! -d ${POLICY_HOME} ]]; then
		echo "error: aborting base installation: ${POLICY_HOME} is not a directory."
		exit 1
	fi

	if ! /bin/mkdir -p "${POLICY_HOME}/servers/" > /dev/null 2>&1; then	
		echo "error: aborting base installation: cannot create ${POLICY_HOME}/servers/"
		exit 1
	fi

	if ! /bin/mkdir -p "${POLICY_HOME}/logs/" > /dev/null 2>&1; then	
		echo "error: aborting base installation: cannot create ${POLICY_HOME}/logs/"
		exit 1
	fi

	BASE_TGZ=$(ls base-*.tar.gz)
	if [ ! -r ${BASE_TGZ} ]; then
		echo "error: aborting base installation: ${POLICY_USER} cannot access tar file: ${BASE_TGZ}"
		exit 1
	fi

	tar -tzf ${BASE_TGZ} > /dev/null 2>&1
	if [[ $? != 0 ]]; then
		echo >&2 "error: aborting base installation: invalid base package tar file: ${BASE_TGZ}"
		exit 1
	fi

	BASH_PROFILE_LINE=". ${POLICY_HOME}/etc/profile.d/env.sh"
	PROFILE_LINE="ps -p \$\$ | grep -q bash || . ${POLICY_HOME}/etc/profile.d/env.sh"

	tar -C ${POLICY_HOME} -xf ${BASE_TGZ} --no-same-owner
	if [[ $? != 0 ]]; then
		# this should not happened
		echo "error: aborting base installation: base package cannot be unpacked: ${BASE_TGZ}"
		exit 1
	fi

	/bin/mkdir -p ${POLICY_HOME}/etc/ssl > /dev/null 2>&1
	/bin/mkdir -p ${POLICY_HOME}/etc/init.d > /dev/null 2>&1
	/bin/mkdir -p ${POLICY_HOME}/tmp > /dev/null 2>&1
	/bin/mkdir -p ${POLICY_HOME}/var > /dev/null 2>&1

	#list_unexpanded_files ${POLICY_HOME}
}


function configure_base() {
	if [[ $DEBUG == y ]]; then
		echo "-- ${FUNCNAME[0]} $@ --"
		set -x
	fi

	# check if fqdn is set in base.conf and use that value if set
	if [[ -z ${INSTALL_FQDN} ]]
	then
		echo "FQDN not set in config...using the default FQDN ${FQDN}"
	else
		echo "Using FQDN ${INSTALL_FQDN} from config"
		FQDN=${INSTALL_FQDN}
	fi

	configure_component "${BASE_CONF}" "${POLICY_HOME}"

	BASH_PROFILE_LINE=". ${POLICY_HOME}/etc/profile.d/env.sh"
	PROFILE_LINE="ps -p \$\$ | grep -q bash || . ${POLICY_HOME}/etc/profile.d/env.sh"

	if ! fgrep -x "${BASH_PROFILE_LINE}" "${HOME}/.bash_profile" >/dev/null 2>&1; then
		echo "${BASH_PROFILE_LINE}" >> "${HOME}/.bash_profile"
	fi

	if ! fgrep -x "${PROFILE_LINE}" "${HOME}/.profile" >/dev/null 2>&1; then
		echo "${PROFILE_LINE}" >> "${HOME}/.profile"
	fi
}

function install_tomcat_component() {
	if [[ $DEBUG == y ]]; then
		echo "-- ${FUNCNAME[0]} $@ --"
		set -x
	fi

	install_prereqs "${BASE_CONF}"

	if ! process_configuration "${COMPONENT_TYPE}.conf"; then
		echo "error: aborting ${COMPONENT_TYPE} installation: cannot process configuration ${COMPONENT_TYPE}.conf"
		exit 1
	fi

	if ! tomcat_component; then
		echo "error: aborting ${COMPONENT_TYPE} installation: tomcat installation failed."
		exit 1
	fi
	
}

# This function installs mysql related shell scripts and sql files in the proper locations
# under $POLICY_HOME. It also adds the MySQL client bin to the PATH based on configuration.
#
function install_mysql() {
	if [[ $DEBUG == y ]]; then
		echo "-- ${FUNCNAME[0]} $@ --"
		set -x
	fi

	install_prereqs "${BASE_CONF}"

	if ! process_configuration "${COMPONENT_TYPE}.conf"; then
		echo "error: aborting ${COMPONENT_TYPE} installation: cannot process configuration ${COMPONENT_TYPE}.conf"
		exit 1
	fi

	MYSQL_DATA_PATH=${POLICY_HOME}/data/mysql
	/bin/mkdir -p ${MYSQL_DATA_PATH} > /dev/null 2>&1

	/bin/cp -f "${POLICY_HOME}"/install/mysql/data/* "${MYSQL_DATA_PATH}"
	/bin/chmod 555 "${MYSQL_DATA_PATH}"/*

	MYSQL_BIN_SOURCE=${POLICY_HOME}/install/mysql/bin
	/bin/mkdir -p ${POLICY_HOME}/bin > /dev/null 2>&1
	for script in $(/bin/ls "${MYSQL_BIN_SOURCE}"); do
		/bin/cp ${MYSQL_BIN_SOURCE}/${script} ${POLICY_HOME}/bin
		/bin/chmod 555 "${POLICY_HOME}/bin/${script}"
	done
}

function configure_mysql() {
	if [[ $DEBUG == y ]]; then
		echo "-- ${FUNCNAME[0]} $@ --"
		set -x
	fi

	# nothing to do
}


#########################################################################
##
## script execution body
##
#########################################################################


OPERATION=none
COMPONENT_TYPE=none
DEBUG=n

BASE_CONF=base.conf

TOMCAT_PACKAGE_NAME=apache-tomcat-8.0.23

INSTALL_DIR="$(pwd)"

export POLICY_USER=$(/usr/bin/id -un)

# command line options parsing
until [[ -z "$1" ]]; do
	case $1 in
		-d|--debug) 	DEBUG=y
						set -x
						;;
		-i|--install) 	OPERATION=install
						shift
						COMPONENT_TYPE=$1
						;;
		-c|--configure)	OPERATION=configure
						shift
						COMPONENT_TYPE=$1
						;;
		*)				usage
						exit 1
						;;
	esac
	shift
done

# component-type validation
case $COMPONENT_TYPE in
	base)	;;
	ransim)	;;
	mysql)	;;
	skip)	;;
	*)		echo "invalid component type (${COMPONENT_TYPE}): must be in {base|ransim|mysql}";
			usage
			exit 1
			;;
esac

# operation validation
case $OPERATION in
	install|configure)	;;
	*)		echo "invalid operation (${OPERATION}): must be in {install|configure}";
			usage
			exit 1
			;;
esac

if [[ -n ${POLICY_GROUP} ]]; then
	groups=$(groups)
	if ! echo ${groups} | grep -qP "\b${POLICY_GROUP}"; then
		echo "error: ${POLICY_GROUP} is not a valid group for account ${POLICY_USER}"
		exit 1
	fi
fi

if [[ -z ${POLICY_GROUP} ]]; then
	numGroups=$(groups | sed "s/^.*: *//g" | wc -w)
	if [ ${numGroups} -eq 1 ]; then
		export POLICY_GROUP=$(groups ${POLICY_USER} | sed "s/^.*: *//g")
	else
		echo "error: ${POLICY_USER} belongs to multiple groups, one group \
              must be provided for the installation"
		usage
		exit 1
	fi
fi

if [[ -z ${POLICY_GROUP} ]]; then
	echo "error: installation of root section must not provide the \
	      installation group owner argument."
	usage
	exit 1
fi

FQDN=$(hostname -f 2> /dev/null)
if [[ $? != 0 || -z ${FQDN} ]]; then
	echo "error: cannot determine the FQDN for this host $(hostname)."
	exit 1
fi

if [[ ${OPERATION} == install ]]; then
	case $COMPONENT_TYPE in
		base)
			install_base
			;;
		ransim)
			install_tomcat_component
			;;
		mysql)
			install_mysql
			;;
		*)
			echo "invalid component type (${COMPONENT_TYPE}): must be in {base|ransim|mysql}";
			usage
			exit 1
			;;
	esac
fi
if [[ ${OPERATION} == configure ]]; then

	install_prereqs "${BASE_CONF}"

	case $COMPONENT_TYPE in
		base)
			configure_base
			component_preconfigure
			;;
		ransim)
			configure_component "${COMPONENT_TYPE}.conf" "${POLICY_HOME}/servers/${COMPONENT_TYPE}/"
			;;
		mysql)
			configure_mysql
			;;
		*)
			echo "invalid component type (${COMPONENT_TYPE}): must be in {base|ransim|mysql}";
			usage
			exit 1
			;;
	esac
fi


echo -n "Successful ${OPERATION} of ${COMPONENT_TYPE} under ${POLICY_USER}:${POLICY_GROUP} "
echo "ownership with umask $(umask)."