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