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