e8b9aaf1451979aca92b167a4d69fbabc1f81125
[policy/engine.git] / packages / docker / src / main / docker / docker-install.sh
1 #!/bin/bash
2
3 #########################################################################
4 ##
5 ## Functions
6 ##
7 #########################################################################
8
9 function usage() {
10         echo -n "syntax: $(basename $0) "
11         echo -n "--debug ("
12         echo -n "[--install base|pap|pdp|console|mysql|elk|brmsgw|paplp|pdplp] | "
13         echo -n "[--configure base|pap|pdp|console|mysql|elk|brmsgw|paplp|pdplp] | "
14 }
15
16 function check_java() {
17         if [[ $DEBUG == y ]]; then
18                 echo "-- ${FUNCNAME[0]} $@ --"
19                 set -x
20         fi
21         
22         TARGET_JAVA_VERSION=$1
23         
24         if [[ -z ${JAVA_HOME} ]]; then
25                 echo "error: ${JAVA_HOME} is not set"
26                 return 1
27         fi
28         
29         if ! check_x_file "${JAVA_HOME}/bin/java"; then
30                 echo "error: ${JAVA_HOME}/bin/java is not accessible"
31                 return 1
32         fi
33         
34         INSTALLED_JAVA_VERSION=$("${JAVA_HOME}/bin/java" -version 2>&1 | awk -F '"' '/version/ {print $2}')
35         if [[ -z $INSTALLED_JAVA_VERSION ]]; then
36                 echo "error: ${JAVA_HOME}/bin/java is invalid"
37                 return 1
38         fi
39         
40         if [[ "${INSTALLED_JAVA_VERSION}" != ${TARGET_JAVA_VERSION}* ]]; then
41                 echo "error: java version (${INSTALLED_JAVA_VERSION}) does not"\
42                          "march desired version ${TARGET_JAVA_VERSION}"
43                 return 1
44         fi 
45         
46         echo "OK: java ${INSTALLED_JAVA_VERSION} installed"
47         
48 }
49
50 function process_configuration() {
51         if [[ $DEBUG == y ]]; then
52                 echo "-- ${FUNCNAME[0]} $@ --"
53                 set -x
54         fi
55         
56         CONF_FILE=$1
57         while read line || [ -n "${line}" ]; do
58         if [[ -n ${line} ]] && [[ ${line} != \#* ]]; then
59                 name=$(echo "${line%%=*}")
60                 value=$(echo "${line#*=}")
61                 # escape ampersand so that sed does not replace it with the search string
62             value=${value//&/\\&}
63                 if [[ -z ${name} ]] || [[ -z $value ]]; then
64                         echo "WARNING: ${line} missing name or value"
65                 fi
66                 export ${name}="${value}"
67                 eval "${name}" "${value}" 2> /dev/null
68         fi
69         done < "${CONF_FILE}"
70         return 0
71 }
72
73 function component_preconfigure() {
74         if [[ $DEBUG == y ]]; then
75                 echo "-- ${FUNCNAME[0]} $@ --"
76                 set -x
77         fi
78
79         /bin/sed -i -e 's!${{POLICY_HOME}}!'"${POLICY_HOME}!g" \
80                 -e 's!${{FQDN}}!'"${FQDN}!g" \
81                 *.conf > /dev/null 2>&1
82 }
83
84 function tomcat_component() {
85         if [[ $DEBUG == y ]]; then
86                 echo "-- ${FUNCNAME[0]} $@ --"
87                 set -x
88         fi
89         
90         TOMCAT_TARGET_INSTALL_DIR=${POLICY_HOME}/servers/${COMPONENT_TYPE}
91         if [[ -d ${TOMCAT_TARGET_INSTALL_DIR} ]]; then
92                 echo "error: ${TOMCAT_TARGET_INSTALL_DIR} exists."
93                 return 1
94         fi
95         
96         TOMCAT_INSTALL_DIR=${POLICY_HOME}/install/3rdparty/${TOMCAT_PACKAGE_NAME}/
97         if [[ -d ${TOMCAT_INSTALL_DIR} ]]; then
98                 echo "error: ${TOMCAT_INSTALL_DIR} exists."
99                 return 1                
100         fi
101         
102         tar -C "${POLICY_HOME}/servers" -xf "${POLICY_HOME}/install/3rdparty/${TOMCAT_PACKAGE_NAME}.tar.gz"
103         
104         mv "${POLICY_HOME}/servers/${TOMCAT_PACKAGE_NAME}" "${POLICY_HOME}/servers/${COMPONENT_TYPE}/"
105         /bin/cp "${POLICY_HOME}"/install/servers/common/tomcat/bin/* "${POLICY_HOME}/servers/${COMPONENT_TYPE}/bin"
106         /bin/cp "${POLICY_HOME}"/install/servers/common/tomcat/conf/* "${POLICY_HOME}/servers/${COMPONENT_TYPE}/conf"
107         
108         /bin/cp "${POLICY_HOME}/install/servers/common/tomcat/init.d/tomcatd" "${POLICY_HOME}/etc/init.d/${COMPONENT_TYPE}"
109         /bin/sed -i -e "s!\${{COMPONENT_TYPE}}!${COMPONENT_TYPE}!g" "${POLICY_HOME}/etc/init.d/${COMPONENT_TYPE}" >/dev/null 2>&1
110
111
112         /bin/cp -fr "${POLICY_HOME}"/install/servers/${COMPONENT_TYPE}/webapps/* "${POLICY_HOME}/servers/${COMPONENT_TYPE}/webapps"
113         /bin/cp -fr "${POLICY_HOME}"/install/servers/${COMPONENT_TYPE}/bin/* "${POLICY_HOME}/servers/${COMPONENT_TYPE}/bin" >/dev/null 2>&1
114         /bin/cp -fr "${POLICY_HOME}"/install/servers/${COMPONENT_TYPE}/conf/* "${POLICY_HOME}/servers/${COMPONENT_TYPE}/conf" >/dev/null 2>&1
115         
116         /bin/rm -fr "${POLICY_HOME}/servers/${COMPONENT_TYPE}/webapps/docs" \
117                  "${POLICY_HOME}/servers/${COMPONENT_TYPE}/webapps/examples" \
118                  "${POLICY_HOME}/servers/${COMPONENT_TYPE}/webapps/ROOT" \
119                  "${POLICY_HOME}/servers/${COMPONENT_TYPE}/webapps/manager" \
120                  "${POLICY_HOME}/servers/${COMPONENT_TYPE}/webapps/host-manager"
121         
122         if [[ ${COMPONENT_TYPE} == console ]]; then
123                 install_onap_portal_settings
124         fi
125
126         return 0
127 }
128
129 function configure_tomcat_component() {
130         configure_component "${COMPONENT_TYPE}.conf" "${POLICY_HOME}/servers/${COMPONENT_TYPE}/"
131 }
132
133 function configure_component() {
134         if [[ $DEBUG == y ]]; then
135                 echo "-- ${FUNCNAME[0]} $@ --"
136                 set -x
137         fi
138                 
139         if ! process_configuration "${COMPONENT_TYPE}.conf"; then
140                 echo "error: aborting ${COMPONENT_TYPE} installation: cannot process configuration ${COMPONENT_TYPE}.conf"
141                 exit 1
142         fi
143         
144         CONF_FILE=$1
145         COMPONENT_ROOT_DIR=$2
146         
147         SED_LINE="sed -i"
148         SED_LINE+=" -e 's!\${{POLICY_HOME}}!${POLICY_HOME}!g' "
149         SED_LINE+=" -e 's!\${{POLICY_USER}}!${POLICY_USER}!g' "
150         SED_LINE+=" -e 's!\${{POLICY_GROUP}}!${POLICY_GROUP}!g' "
151         SED_LINE+=" -e 's!\${{KEYSTORE_PASSWD}}!${KEYSTORE_PASSWD}!g' "
152         SED_LINE+=" -e 's!\${{JAVA_HOME}}!${JAVA_HOME}!g' "
153         SED_LINE+=" -e 's!\${{COMPONENT_TYPE}}!${COMPONENT_TYPE}!g' "
154                 
155         while read line || [ -n "${line}" ]; do
156         if [[ -n $line ]] && [[ $line != \#* ]]; then
157                 name=$(echo "${line%%=*}")
158                 value=$(echo "${line#*=}")
159                 # escape ampersand so that sed does not replace it with the search string
160                 value=${value//&/\\&}
161                 if [[ -z ${name} ]] || [[ -z ${value} ]]; then
162                         echo "WARNING: ${line} missing name or value"
163                 fi
164                 SED_LINE+=" -e 's!\${{${name}}}!${value}!g' "
165                 
166         fi
167         done < "$CONF_FILE"
168         
169         SED_FILES=""
170         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
171                 if fgrep -l '${{' ${sed_file} > /dev/null 2>&1; then
172                         SED_FILES+="${sed_file} "
173                 fi
174         done
175
176         if [[ -f $HOME/.m2/settings.xml ]]; then
177                 SED_FILES+="$HOME/.m2/settings.xml "
178         fi
179         
180
181         if [[ -z ${SED_FILES} ]]; then
182                 echo "WARNING: no xml, sh, properties, or conf files to perform configuration expansion"
183         else
184                 SED_LINE+=${SED_FILES}
185                 eval "${SED_LINE}"
186         fi
187
188         list_unexpanded_files ${POLICY_HOME}
189 }
190
191 function install_onap_portal_settings() {
192         echo "Install onap portal settings"
193
194         # unpack onap war file
195         mkdir -p "${POLICY_HOME}"/servers/console/webapps/onap
196         cd "${POLICY_HOME}"/servers/console/webapps/onap
197         unzip -q ../onap.war
198         cd ${INSTALL_DIR}
199
200         # copy over the configured settings
201         /bin/cp -fr "${POLICY_HOME}"/install/servers/onap/* "${POLICY_HOME}/servers/console/webapps/onap"
202 }
203
204 function check_r_file() {
205         if [[ $DEBUG == y ]]; then
206                 echo "-- ${FUNCNAME[0]} $@ --"
207                 set -x
208         fi
209
210         FILE=$1
211         if [[ ! -f ${FILE} || ! -r ${FILE} ]]; then
212         return 1
213         fi
214
215         return 0
216 }
217
218 function check_x_file() {       
219         if [[ $DEBUG == y ]]; then
220                 echo "-- ${FUNCNAME[0]} $@ --"
221                 set -x
222         fi
223
224         FILE=$1
225         if [[ ! -f ${FILE} || ! -x ${FILE} ]]; then
226         return 1
227         fi
228
229         return 0
230 }
231
232 function install_prereqs() {
233         if [[ $DEBUG == y ]]; then
234                 echo "-- ${FUNCNAME[0]} $@ --"
235                 set -x
236         fi
237         
238         CONF_FILE=$1
239         
240         if ! check_r_file "${CONF_FILE}"; then
241                 echo "error: aborting ${COMPONENT_TYPE} installation: ${CONF_FILE} is not accessible"
242                 exit 1
243         fi
244         
245         if ! process_configuration "${CONF_FILE}"; then
246                 echo "error: aborting ${COMPONENT_TYPE} installation: cannot process configuration ${CONF_FILE}"
247                 exit 1
248         fi
249         
250 #       if ! check_java "1.8"; then
251 #               echo "error: aborting ${COMPONENT_TYPE} installation: invalid java version"
252 #               exit 1
253 #       fi
254         
255         if [[ -z ${POLICY_HOME} ]]; then
256                 echo "error: aborting ${COMPONENT_TYPE} installation: ${POLICY_HOME} is not set"
257                 exit 1  
258         fi
259
260         HOME_OWNER=$(ls -ld "${POLICY_HOME}" | awk '{print $3}')
261         if [[ ${HOME_OWNER} != ${POLICY_USER} ]]; then
262                 echo "error: aborting ${COMPONENT_TYPE} installation: ${POLICY_USER} does not own ${POLICY_HOME} directory"
263                 exit 1
264         fi
265         
266         echo -n "Starting ${OPERATION} of ${COMPONENT_TYPE} under ${POLICY_USER}:${POLICY_GROUP} "
267         echo "ownership with umask $(umask)."
268 }
269
270 function list_unexpanded_files() {
271         ROOT_DIR=$1
272         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/')
273     NOT_EXPANDED_BASE_FILES=$(grep -l '${{' ${SEARCH_LIST} 2> /dev/null)
274         if [[ -n ${NOT_EXPANDED_BASE_FILES} ]]; then
275                 echo "error: component installation has completed but some base files have not been expanded:"
276                 echo "${NOT_EXPANDED_BASE_FILES}"
277                 return 1
278         fi
279         return 0
280 }
281
282 function install_base() {
283         if [[ $DEBUG == y ]]; then
284                 echo "-- ${FUNCNAME[0]} $@ --"
285                 set -x
286         fi
287         
288         install_prereqs "${BASE_CONF}"
289         
290         if [[ -z ${POLICY_HOME} ]]; then
291                 echo "error: ${POLICY_HOME} is not set"
292                 exit 1
293         fi
294         
295         POLICY_HOME_CONTENTS=$(ls -A "${POLICY_HOME}" 2> /dev/null)
296         if [[ -n ${POLICY_HOME_CONTENTS} ]]; then
297                 echo "error: aborting base installation: ${POLICY_HOME} directory is not empty"
298                 exit 1
299         fi
300         
301         if [[ ! -d ${POLICY_HOME} ]]; then
302                 echo "error: aborting base installation: ${POLICY_HOME} is not a directory."
303                 exit 1
304         fi
305         
306         if ! /bin/mkdir -p "${POLICY_HOME}/servers/" > /dev/null 2>&1; then     
307                 echo "error: aborting base installation: cannot create ${POLICY_HOME}/servers/"
308                 exit 1
309         fi      
310         
311         if ! /bin/mkdir -p "${POLICY_HOME}/logs/" > /dev/null 2>&1; then        
312                 echo "error: aborting base installation: cannot create ${POLICY_HOME}/logs/"
313                 exit 1
314         fi      
315         
316         BASE_TGZ=$(ls base-*.tar.gz)
317         if [ ! -r ${BASE_TGZ} ]; then
318                 echo "error: aborting base installation: ${POLICY_USER} cannot access tar file: ${BASE_TGZ}"
319                 exit 1                  
320         fi
321         
322         tar -tzf ${BASE_TGZ} > /dev/null 2>&1
323         if [[ $? != 0 ]]; then
324                 echo >&2 "error: aborting base installation: invalid base package tar file: ${BASE_TGZ}"
325                 exit 1
326         fi
327         
328         BASH_PROFILE_LINE=". ${POLICY_HOME}/etc/profile.d/env.sh"
329         PROFILE_LINE="ps -p \$\$ | grep -q bash || . ${POLICY_HOME}/etc/profile.d/env.sh"
330                 
331         tar -C ${POLICY_HOME} -xf ${BASE_TGZ} --no-same-owner
332         if [[ $? != 0 ]]; then
333                 # this should not happened
334                 echo "error: aborting base installation: base package cannot be unpacked: ${BASE_TGZ}"
335                 exit 1
336         fi
337
338         /bin/mkdir -p ${POLICY_HOME}/etc/ssl > /dev/null 2>&1
339         /bin/mkdir -p ${POLICY_HOME}/etc/init.d > /dev/null 2>&1
340         /bin/mkdir -p ${POLICY_HOME}/tmp > /dev/null 2>&1
341         /bin/mkdir -p ${POLICY_HOME}/var > /dev/null 2>&1
342                         
343         #list_unexpanded_files ${POLICY_HOME}
344 }
345
346
347 function configure_base() {
348         if [[ $DEBUG == y ]]; then
349                 echo "-- ${FUNCNAME[0]} $@ --"
350                 set -x
351         fi
352         
353         # check if fqdn is set in base.conf and use that value if set
354         if [[ -z ${INSTALL_FQDN} ]]
355         then
356                 echo "FQDN not set in config...using the default FQDN ${FQDN}"
357         else
358                 echo "Using FQDN ${INSTALL_FQDN} from config"
359                 FQDN=${INSTALL_FQDN}
360         fi
361
362         configure_component "${BASE_CONF}" "${POLICY_HOME}"
363         
364         BASH_PROFILE_LINE=". ${POLICY_HOME}/etc/profile.d/env.sh"
365         PROFILE_LINE="ps -p \$\$ | grep -q bash || . ${POLICY_HOME}/etc/profile.d/env.sh"
366         
367         if ! fgrep -x "${BASH_PROFILE_LINE}" "${HOME}/.bash_profile" >/dev/null 2>&1; then
368                 echo "${BASH_PROFILE_LINE}" >> "${HOME}/.bash_profile"
369         fi
370         
371         if ! fgrep -x "${PROFILE_LINE}" "${HOME}/.profile" >/dev/null 2>&1; then
372                 echo "${PROFILE_LINE}" >> "${HOME}/.profile"
373         fi
374 }
375
376 function install_tomcat_component() {
377         if [[ $DEBUG == y ]]; then
378                 echo "-- ${FUNCNAME[0]} $@ --"
379                 set -x
380         fi
381         
382         install_prereqs "${BASE_CONF}"
383
384         if ! process_configuration "${COMPONENT_TYPE}.conf"; then
385                 echo "error: aborting ${COMPONENT_TYPE} installation: cannot process configuration ${COMPONENT_TYPE}.conf"
386                 exit 1
387         fi
388         
389         if ! tomcat_component; then
390                 echo "error: aborting ${COMPONENT_TYPE} installation: tomcat installation failed."
391                 exit 1                  
392         fi
393         
394 }
395
396 # This function installs mysql related shell scripts and sql files in the proper locations
397 # under $POLICY_HOME. It also adds the MySQL client bin to the PATH based on configuration.
398 #
399 function install_mysql() {
400         if [[ $DEBUG == y ]]; then
401                 echo "-- ${FUNCNAME[0]} $@ --"
402                 set -x
403         fi
404         
405         install_prereqs "${BASE_CONF}"
406
407         if ! process_configuration "${COMPONENT_TYPE}.conf"; then
408                 echo "error: aborting ${COMPONENT_TYPE} installation: cannot process configuration ${COMPONENT_TYPE}.conf"
409                 exit 1
410         fi
411         
412         MYSQL_DATA_PATH=${POLICY_HOME}/data/mysql
413         /bin/mkdir -p ${MYSQL_DATA_PATH} > /dev/null 2>&1
414         
415         /bin/cp -f "${POLICY_HOME}"/install/mysql/data/* "${MYSQL_DATA_PATH}"
416         /bin/chmod 555 "${MYSQL_DATA_PATH}"/*
417         
418         MYSQL_BIN_SOURCE=${POLICY_HOME}/install/mysql/bin
419         /bin/mkdir -p ${POLICY_HOME}/bin > /dev/null 2>&1
420         for script in $(/bin/ls "${MYSQL_BIN_SOURCE}"); do
421                 /bin/cp ${MYSQL_BIN_SOURCE}/${script} ${POLICY_HOME}/bin
422                 /bin/chmod 555 "${POLICY_HOME}/bin/${script}"
423         done
424 }
425
426 function configure_mysql() {
427         if [[ $DEBUG == y ]]; then
428                 echo "-- ${FUNCNAME[0]} $@ --"
429                 set -x
430         fi
431         
432         # nothing to do
433 }
434
435 # This function installs elk related shell scripts and sql files in the proper locations
436 # under $POLICY_HOME. It also adds the Elk to the PATH based on configuration.
437 #
438 function configure_elk() {
439         if [[ $DEBUG == y ]]; then
440                 echo "-- ${FUNCNAME[0]} $@ --"
441                 set -x
442         fi
443         
444         # nothing to do
445 }
446
447 function install_elk() {
448         if [[ $DEBUG == y ]]; then
449                 echo "-- ${FUNCNAME[0]} $@ --"
450                 set -x
451         fi
452         
453         if [[ -f "${HOME}/.bash_profile" ]]; then
454                 source "${HOME}/.bash_profile"
455         fi
456         
457         if [[ -f "${HOME}/.profile" ]]; then
458                 source "${HOME}/.profile"
459         fi
460         
461         ELK_TARGET_INSTALL_DIR="${POLICY_HOME}"/elk
462         
463         if [[ -d ${ELK_TARGET_INSTALL_DIR} ]]; then
464                 echo "WARNING: ${ELK_TARGET_INSTALL_DIR} exists."
465                 return 1
466         fi
467         
468         /bin/mkdir -p "${ELK_TARGET_INSTALL_DIR}" > /dev/null 2>&1
469         
470         if [[ ! -d ${ELK_TARGET_INSTALL_DIR} ]]; then
471                 echo "WARNING: ${ELK_TARGET_INSTALL_DIR} doesn't exist."
472                 return 1
473         fi
474         
475         cd ${ELK_TARGET_INSTALL_DIR}
476         curl -L -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.4.0.tar.gz
477         
478         tar xvzf elasticsearch-5.4.0.tar.gz -C .
479         /bin/rm -fr elasticsearch-5.4.0.tar.gz
480         /bin/mv  ${ELK_TARGET_INSTALL_DIR}/elasticsearch-5.4.0/* .
481         /bin/rm -fr ${ELK_TARGET_INSTALL_DIR}/elasticsearch-5.4.0
482         
483         /bin/cp "${POLICY_HOME}"/install/elk/bin/* "${POLICY_HOME}/bin" 
484         /bin/cp -f "${POLICY_HOME}"/install/elk/config/* "${ELK_TARGET_INSTALL_DIR}/config"
485         /bin/cp -f "${POLICY_HOME}/install/elk/init.d/elkd" "${POLICY_HOME}/etc/init.d/elk"
486         
487         install_prereqs "${COMPONENT_TYPE}.conf"
488         
489         /bin/sed -i -e "s!\${{POLICY_HOME}}!${POLICY_HOME}!g" \
490                 -e "s!\${{FQDN}}!${FQDN}!g" \
491                 -e "s!\${{ELK_JMX_PORT}}!${ELK_JMX_PORT}!g" \
492                 "${ELK_TARGET_INSTALL_DIR}"/config/* "${POLICY_HOME}/etc/init.d/elk" > /dev/null 2>&1
493                 
494
495         list_unexpanded_files ${POLICY_HOME}
496                 
497         return $?
498 }
499
500 # This function installs brmsgw related shell scripts and config files in the proper
501 # locations under $POLICY_HOME. 
502 #
503
504 function install_brmsgw() {
505         if [[ $DEBUG == y ]]; then
506                 echo "-- ${FUNCNAME[0]} $@ --"
507                 set -x
508         fi
509         
510         install_prereqs "${BASE_CONF}"
511
512         if ! process_configuration "${COMPONENT_TYPE}.conf"; then
513                 echo "error: aborting ${COMPONENT_TYPE} installation: cannot process configuration ${COMPONENT_TYPE}.conf"
514                 exit 1
515         fi
516         
517         if [ -z "$M2_HOME" ]; then
518                 echo "error: aborting ${COMPONENT_TYPE} installation: M2_HOME must be set in brmsgw.conf"
519                 exit 1
520         fi
521         
522         echo "export M2_HOME=$M2_HOME" >>$POLICY_HOME/etc/profile.d/env.sh
523
524         /bin/cp -f "${POLICY_HOME}/install/servers/brmsgw/init.d/brmsgw" "${POLICY_HOME}/etc/init.d/brmsgw"
525         
526         if ! /bin/mkdir -p "${POLICY_HOME}/servers/${COMPONENT_TYPE}" > /dev/null 2>&1; then    
527                 echo "error: aborting base installation: cannot create ${POLICY_HOME}/servers/${COMPONENT_TYPE}"
528                 exit 1
529         fi      
530         
531         /bin/cp -fr "${POLICY_HOME}"/install/servers/${COMPONENT_TYPE}/BRMSGateway.jar "${POLICY_HOME}/servers/${COMPONENT_TYPE}"
532         /bin/cp -fr "${POLICY_HOME}"/install/servers/${COMPONENT_TYPE}/*.properties "${POLICY_HOME}/servers/${COMPONENT_TYPE}"
533         /bin/cp -fr "${POLICY_HOME}"/install/servers/${COMPONENT_TYPE}/config "${POLICY_HOME}/servers/${COMPONENT_TYPE}"
534         /bin/cp -fr "${POLICY_HOME}"/install/servers/${COMPONENT_TYPE}/dependency.json "${POLICY_HOME}/servers/${COMPONENT_TYPE}"
535         
536         /bin/mv $POLICY_HOME/m2 $HOME/.m2
537
538         return 0
539 }
540
541
542 function install_logparser() {
543         if [[ $DEBUG == y ]]; then
544                 echo "-- ${FUNCNAME[0]} $@ --"
545                 set -x
546         fi
547         
548         install_prereqs "${BASE_CONF}"
549
550         if ! process_configuration "${COMPONENT_TYPE}.conf"; then
551                 echo "error: aborting ${COMPONENT_TYPE} installation: cannot process configuration ${COMPONENT_TYPE}.conf"
552                 exit 1
553         fi
554         
555         LP_TARGET_DIR=${POLICY_HOME}/servers/${COMPONENT_TYPE}
556         /bin/mkdir -p ${LP_TARGET_DIR}/bin > /dev/null 2>&1
557         /bin/mkdir -p ${LP_TARGET_DIR}/logs > /dev/null 2>&1
558         
559         # copy binaries, initialization script and configuration
560         /bin/cp "${POLICY_HOME}"/install/servers/common/logparser/bin/*jar "${LP_TARGET_DIR}/bin"
561         /bin/cp "${POLICY_HOME}/install/servers/common/logparser/init.d/logparserd" "${POLICY_HOME}/etc/init.d/${COMPONENT_TYPE}"
562         /bin/cp "${POLICY_HOME}/install/servers/${COMPONENT_TYPE}/bin/parserlog.properties" "${LP_TARGET_DIR}/bin"
563         /bin/cp -fr "${POLICY_HOME}"/install/servers/${COMPONENT_TYPE}/bin/config "${POLICY_HOME}/servers/${COMPONENT_TYPE}/bin"
564         
565 }
566
567 #########################################################################
568 ##
569 ## script execution body
570 ##
571 #########################################################################
572
573
574 OPERATION=none
575 COMPONENT_TYPE=none
576 DEBUG=n
577
578 BASE_CONF=base.conf
579
580 TOMCAT_PACKAGE_NAME=apache-tomcat-8.0.23
581
582 INSTALL_DIR="$(pwd)"
583
584 export POLICY_USER=$(/usr/bin/id -un)
585
586 # command line options parsing
587 until [[ -z "$1" ]]; do
588         case $1 in
589                 -d|--debug)     DEBUG=y
590                                                 set -x
591                                                 ;;
592                 -i|--install)   OPERATION=install
593                                                 shift
594                                                 COMPONENT_TYPE=$1
595                                                 ;;
596                 -c|--configure) OPERATION=configure
597                                                 shift
598                                                 COMPONENT_TYPE=$1
599                                                 ;;
600                 *)                              usage
601                                                 exit 1
602                                                 ;;
603         esac
604         shift
605 done
606
607 # component-type validation
608 case $COMPONENT_TYPE in
609         base)   ;;
610         pdp)    ;;
611         pap)    ;;
612         console)        ;;
613         mysql)  ;;
614         elk)    ;;
615         brmsgw) ;;
616         paplp)  ;;
617         pdplp)  ;;
618         skip)   ;;
619         *)              echo "invalid component type (${COMPONENT_TYPE}): must be in {base|pdp|pap|console|mysql|elk|brmsgw|paplp|pdplp}";
620                         usage
621                         exit 1
622                         ;;
623 esac
624
625 # operation validation
626 case $OPERATION in
627         install|configure)      ;;
628         *)              echo "invalid operation (${OPERATION}): must be in {install|configure}";
629                         usage
630                         exit 1
631                         ;;
632 esac
633
634 if [[ -n ${POLICY_GROUP} ]]; then
635         groups=$(groups)
636         if ! echo ${groups} | grep -qP "\b${POLICY_GROUP}"; then
637                 echo "error: ${POLICY_GROUP} is not a valid group for account ${POLICY_USER}"
638                 exit 1
639         fi
640 fi
641
642 if [[ -z ${POLICY_GROUP} ]]; then
643         numGroups=$(groups | sed "s/^.*: *//g" | wc -w)
644         if [ ${numGroups} -eq 1 ]; then
645                 export POLICY_GROUP=$(groups ${POLICY_USER} | sed "s/^.*: *//g")
646         else
647                 echo "error: ${POLICY_USER} belongs to multiple groups, one group \
648               must be provided for the installation"
649                 usage
650                 exit 1
651         fi
652 fi
653
654 if [[ -z ${POLICY_GROUP} ]]; then
655         echo "error: installation of root section must not provide the \
656               installation group owner argument."
657         usage
658         exit 1
659 fi
660
661 FQDN=$(hostname -f 2> /dev/null)
662 if [[ $? != 0 || -z ${FQDN} ]]; then
663         echo "error: cannot determine the FQDN for this host $(hostname)."
664         exit 1
665 fi
666
667 if [[ ${OPERATION} == install ]]; then
668         case $COMPONENT_TYPE in
669                 base)   
670                         install_base
671                         ;;
672                 pdp)    
673                         install_tomcat_component
674                         ;;
675                 pap)
676                         install_tomcat_component
677                         ;;
678                 console)
679                         install_tomcat_component
680                         ;;
681                 mysql)
682                         install_mysql
683                         ;;
684                 elk)
685                         install_elk
686                         ;;              
687                 brmsgw)
688                         install_brmsgw
689                         ;;
690                 paplp|pdplp)
691                         install_logparser
692                         ;;
693                 *)              
694                         echo "invalid component type (${COMPONENT_TYPE}): must be in {base|pdp|pap|console|mysql|elk|brmsgw|paplp|pdplp}";
695                         usage
696                         exit 1
697                         ;;
698         esac
699 fi
700 if [[ ${OPERATION} == configure ]]; then
701
702         install_prereqs "${BASE_CONF}"
703
704         case $COMPONENT_TYPE in
705                 base)   
706                         configure_base
707                         component_preconfigure
708                         ;;
709                 pdp)    
710                         configure_component "${COMPONENT_TYPE}.conf" "${POLICY_HOME}/servers/${COMPONENT_TYPE}/"
711                         ;;
712                 pap)
713                         configure_component "${COMPONENT_TYPE}.conf" "${POLICY_HOME}/servers/${COMPONENT_TYPE}/"
714                         ;;
715                 console)
716                         configure_component "${COMPONENT_TYPE}.conf" "${POLICY_HOME}/servers/${COMPONENT_TYPE}/"
717                         ;;
718                 mysql)
719                         configure_mysql
720                         ;;
721                 elk)
722                         configure_elk
723                         ;;      
724                 brmsgw)
725                         configure_component "${COMPONENT_TYPE}.conf" "${POLICY_HOME}/servers/${COMPONENT_TYPE}/"
726                         ;;
727                 paplp|pdplp)
728                         configure_component "${COMPONENT_TYPE}.conf" "${POLICY_HOME}/servers/${COMPONENT_TYPE}/"
729                         ;;
730                 *)              
731                         echo "invalid component type (${COMPONENT_TYPE}): must be in {base|pdp|pap|console|mysql|elk|brmsgw|paplp|pdplp}";
732                         usage
733                         exit 1
734                         ;;
735         esac
736 fi
737
738
739 echo -n "Successful ${OPERATION} of ${COMPONENT_TYPE} under ${POLICY_USER}:${POLICY_GROUP} "
740 echo "ownership with umask $(umask)."