cook policy-keystore into pe image
[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/init.d > /dev/null 2>&1
360         /bin/mkdir -p ${POLICY_HOME}/tmp > /dev/null 2>&1
361         /bin/mkdir -p ${POLICY_HOME}/var > /dev/null 2>&1
362                         
363         #list_unexpanded_files ${POLICY_HOME}
364 }
365
366
367 function configure_base() {
368         if [[ $DEBUG == y ]]; then
369                 echo "-- ${FUNCNAME[0]} $@ --"
370                 set -x
371         fi
372         
373         # check if fqdn is set in base.conf and use that value if set
374         if [[ -z ${INSTALL_FQDN} ]]
375         then
376                 echo "FQDN not set in config...using the default FQDN ${FQDN}"
377         else
378                 echo "Using FQDN ${INSTALL_FQDN} from config"
379                 FQDN=${INSTALL_FQDN}
380         fi
381
382         configure_component "${BASE_CONF}" "${POLICY_HOME}"
383         
384         BASH_PROFILE_LINE=". ${POLICY_HOME}/etc/profile.d/env.sh"
385         PROFILE_LINE="ps -p \$\$ | grep -q bash || . ${POLICY_HOME}/etc/profile.d/env.sh"
386         
387         if ! fgrep -x "${BASH_PROFILE_LINE}" "${HOME}/.bash_profile" >/dev/null 2>&1; then
388                 echo "${BASH_PROFILE_LINE}" >> "${HOME}/.bash_profile"
389         fi
390         
391         if ! fgrep -x "${PROFILE_LINE}" "${HOME}/.profile" >/dev/null 2>&1; then
392                 echo "${PROFILE_LINE}" >> "${HOME}/.profile"
393         fi
394 }
395
396 function configure_keystore() {
397         if [[ $DEBUG == y ]]; then
398                 echo "-- ${FUNCNAME[0]} --"
399                 set -x
400         fi
401
402     local DEFAULT_KEYSTORE_PASSWORD="Pol1cy_0nap"
403
404         if [[ -n ${KEYSTORE_PASSWD} ]]; then
405             keytool -storepasswd -storepass ${DEFAULT_KEYSTORE_PASSWORD} -keystore ${POLICY_HOME}/etc/ssl/policy-keystore -new ${KEYSTORE_PASSWD}
406             keytool -list -keystore ${POLICY_HOME}/etc/ssl/policy-keystore -storepass ${KEYSTORE_PASSWD}
407         fi
408 }
409
410
411 function install_tomcat_component() {
412         if [[ $DEBUG == y ]]; then
413                 echo "-- ${FUNCNAME[0]} $@ --"
414                 set -x
415         fi
416         
417         install_prereqs "${BASE_CONF}"
418
419         if ! process_configuration "${COMPONENT_TYPE}.conf"; then
420                 echo "error: aborting ${COMPONENT_TYPE} installation: cannot process configuration ${COMPONENT_TYPE}.conf"
421                 exit 1
422         fi
423         
424         if ! tomcat_component; then
425                 echo "error: aborting ${COMPONENT_TYPE} installation: tomcat installation failed."
426                 exit 1                  
427         fi
428         
429 }
430
431 # This function installs mysql related shell scripts and sql files in the proper locations
432 # under $POLICY_HOME. It also adds the MySQL client bin to the PATH based on configuration.
433 #
434 function install_mysql() {
435         if [[ $DEBUG == y ]]; then
436                 echo "-- ${FUNCNAME[0]} $@ --"
437                 set -x
438         fi
439         
440         install_prereqs "${BASE_CONF}"
441
442         if ! process_configuration "${COMPONENT_TYPE}.conf"; then
443                 echo "error: aborting ${COMPONENT_TYPE} installation: cannot process configuration ${COMPONENT_TYPE}.conf"
444                 exit 1
445         fi
446         
447         MYSQL_DATA_PATH=${POLICY_HOME}/data/mysql
448         /bin/mkdir -p ${MYSQL_DATA_PATH} > /dev/null 2>&1
449         
450         /bin/cp -f "${POLICY_HOME}"/install/mysql/data/* "${MYSQL_DATA_PATH}"
451         /bin/chmod 555 "${MYSQL_DATA_PATH}"/*
452         
453         MYSQL_BIN_SOURCE=${POLICY_HOME}/install/mysql/bin
454         /bin/mkdir -p ${POLICY_HOME}/bin > /dev/null 2>&1
455         for script in $(/bin/ls "${MYSQL_BIN_SOURCE}"); do
456                 /bin/cp ${MYSQL_BIN_SOURCE}/${script} ${POLICY_HOME}/bin
457                 /bin/chmod 555 "${POLICY_HOME}/bin/${script}"
458         done
459 }
460
461 function configure_mysql() {
462         if [[ $DEBUG == y ]]; then
463                 echo "-- ${FUNCNAME[0]} $@ --"
464                 set -x
465         fi
466         
467         # nothing to do
468 }
469
470 # This function installs elk related shell scripts and sql files in the proper locations
471 # under $POLICY_HOME. It also adds the Elk to the PATH based on configuration.
472 #
473 function configure_elk() {
474         if [[ $DEBUG == y ]]; then
475                 echo "-- ${FUNCNAME[0]} $@ --"
476                 set -x
477         fi
478         
479         # nothing to do
480 }
481
482 function install_elk() {
483         if [[ $DEBUG == y ]]; then
484                 echo "-- ${FUNCNAME[0]} $@ --"
485                 set -x
486         fi
487         
488         if [[ -f "${HOME}/.bash_profile" ]]; then
489                 source "${HOME}/.bash_profile"
490         fi
491         
492         if [[ -f "${HOME}/.profile" ]]; then
493                 source "${HOME}/.profile"
494         fi
495         
496         ELK_TARGET_INSTALL_DIR="${POLICY_HOME}"/elk
497         
498         if [[ -d ${ELK_TARGET_INSTALL_DIR} ]]; then
499                 echo "WARNING: ${ELK_TARGET_INSTALL_DIR} exists."
500                 return 1
501         fi
502         
503         /bin/mkdir -p "${ELK_TARGET_INSTALL_DIR}" > /dev/null 2>&1
504         
505         if [[ ! -d ${ELK_TARGET_INSTALL_DIR} ]]; then
506                 echo "WARNING: ${ELK_TARGET_INSTALL_DIR} doesn't exist."
507                 return 1
508         fi
509         
510         cd ${ELK_TARGET_INSTALL_DIR}
511         curl -L -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.4.0.tar.gz
512         
513         tar xvzf elasticsearch-5.4.0.tar.gz -C .
514         /bin/rm -fr elasticsearch-5.4.0.tar.gz
515         /bin/mv  ${ELK_TARGET_INSTALL_DIR}/elasticsearch-5.4.0/* .
516         /bin/rm -fr ${ELK_TARGET_INSTALL_DIR}/elasticsearch-5.4.0
517         
518         /bin/cp "${POLICY_HOME}"/install/elk/bin/* "${POLICY_HOME}/bin" 
519         /bin/cp -f "${POLICY_HOME}"/install/elk/config/* "${ELK_TARGET_INSTALL_DIR}/config"
520         /bin/cp -f "${POLICY_HOME}/install/elk/init.d/elkd" "${POLICY_HOME}/etc/init.d/elk"
521         
522         install_prereqs "${COMPONENT_TYPE}.conf"
523         
524         /bin/sed -i -e "s!\${{POLICY_HOME}}!${POLICY_HOME}!g" \
525                 -e "s!\${{FQDN}}!${FQDN}!g" \
526                 -e "s!\${{ELK_JMX_PORT}}!${ELK_JMX_PORT}!g" \
527                 "${ELK_TARGET_INSTALL_DIR}"/config/* "${POLICY_HOME}/etc/init.d/elk" > /dev/null 2>&1
528                 
529
530         list_unexpanded_files ${POLICY_HOME}
531                 
532         return $?
533 }
534
535 # This function installs brmsgw related shell scripts and config files in the proper
536 # locations under $POLICY_HOME. 
537 #
538
539 function install_brmsgw() {
540         if [[ $DEBUG == y ]]; then
541                 echo "-- ${FUNCNAME[0]} $@ --"
542                 set -x
543         fi
544         
545         install_prereqs "${BASE_CONF}"
546
547         if ! process_configuration "${COMPONENT_TYPE}.conf"; then
548                 echo "error: aborting ${COMPONENT_TYPE} installation: cannot process configuration ${COMPONENT_TYPE}.conf"
549                 exit 1
550         fi
551         
552         if [ -z "$M2_HOME" ]; then
553                 echo "error: aborting ${COMPONENT_TYPE} installation: M2_HOME must be set in brmsgw.conf"
554                 exit 1
555         fi
556         
557         echo "export M2_HOME=$M2_HOME" >>$POLICY_HOME/etc/profile.d/env.sh
558
559         /bin/cp -f "${POLICY_HOME}/install/servers/brmsgw/init.d/brmsgw" "${POLICY_HOME}/etc/init.d/brmsgw"
560         
561         if ! /bin/mkdir -p "${POLICY_HOME}/servers/${COMPONENT_TYPE}" > /dev/null 2>&1; then    
562                 echo "error: aborting base installation: cannot create ${POLICY_HOME}/servers/${COMPONENT_TYPE}"
563                 exit 1
564         fi      
565         
566         /bin/cp -fr "${POLICY_HOME}"/install/servers/${COMPONENT_TYPE}/BRMSGateway.jar "${POLICY_HOME}/servers/${COMPONENT_TYPE}"
567         /bin/cp -fr "${POLICY_HOME}"/install/servers/${COMPONENT_TYPE}/*.properties "${POLICY_HOME}/servers/${COMPONENT_TYPE}"
568         /bin/cp -fr "${POLICY_HOME}"/install/servers/${COMPONENT_TYPE}/config "${POLICY_HOME}/servers/${COMPONENT_TYPE}"
569         /bin/cp -fr "${POLICY_HOME}"/install/servers/${COMPONENT_TYPE}/dependency.json "${POLICY_HOME}/servers/${COMPONENT_TYPE}"
570         
571         /bin/mv $POLICY_HOME/m2 $HOME/.m2
572
573         return 0
574 }
575
576
577 function install_logparser() {
578         if [[ $DEBUG == y ]]; then
579                 echo "-- ${FUNCNAME[0]} $@ --"
580                 set -x
581         fi
582         
583         install_prereqs "${BASE_CONF}"
584
585         if ! process_configuration "${COMPONENT_TYPE}.conf"; then
586                 echo "error: aborting ${COMPONENT_TYPE} installation: cannot process configuration ${COMPONENT_TYPE}.conf"
587                 exit 1
588         fi
589         
590         LP_TARGET_DIR=${POLICY_HOME}/servers/${COMPONENT_TYPE}
591         /bin/mkdir -p ${LP_TARGET_DIR}/bin > /dev/null 2>&1
592         /bin/mkdir -p ${LP_TARGET_DIR}/logs > /dev/null 2>&1
593         
594         # copy binaries, initialization script and configuration
595         /bin/cp "${POLICY_HOME}"/install/servers/common/logparser/bin/*jar "${LP_TARGET_DIR}/bin"
596         /bin/cp "${POLICY_HOME}/install/servers/common/logparser/init.d/logparserd" "${POLICY_HOME}/etc/init.d/${COMPONENT_TYPE}"
597         /bin/cp "${POLICY_HOME}/install/servers/${COMPONENT_TYPE}/bin/parserlog.properties" "${LP_TARGET_DIR}/bin"
598         /bin/cp -fr "${POLICY_HOME}"/install/servers/${COMPONENT_TYPE}/bin/config "${POLICY_HOME}/servers/${COMPONENT_TYPE}/bin"
599         
600 }
601
602 #########################################################################
603 ##
604 ## script execution body
605 ##
606 #########################################################################
607
608
609 OPERATION=none
610 COMPONENT_TYPE=none
611 DEBUG=n
612
613 BASE_CONF=base.conf
614
615 TOMCAT_PACKAGE_NAME=apache-tomcat-8.0.50
616
617 INSTALL_DIR="$(pwd)"
618
619 export POLICY_USER=$(/usr/bin/id -un)
620
621 # command line options parsing
622 until [[ -z "$1" ]]; do
623         case $1 in
624                 -d|--debug)     DEBUG=y
625                                                 set -x
626                                                 ;;
627                 -i|--install)   OPERATION=install
628                                                 shift
629                                                 COMPONENT_TYPE=$1
630                                                 ;;
631                 -c|--configure) OPERATION=configure
632                                                 shift
633                                                 COMPONENT_TYPE=$1
634                                                 ;;
635                 *)                              usage
636                                                 exit 1
637                                                 ;;
638         esac
639         shift
640 done
641
642 # component-type validation
643 case $COMPONENT_TYPE in
644         base)   ;;
645         pdp)    ;;
646         pap)    ;;
647         console)        ;;
648         mysql)  ;;
649         elk)    ;;
650         brmsgw) ;;
651         paplp)  ;;
652         pdplp)  ;;
653         skip)   ;;
654         *)              echo "invalid component type (${COMPONENT_TYPE}): must be in {base|pdp|pap|console|mysql|elk|brmsgw|paplp|pdplp}";
655                         usage
656                         exit 1
657                         ;;
658 esac
659
660 # operation validation
661 case $OPERATION in
662         install|configure)      ;;
663         *)              echo "invalid operation (${OPERATION}): must be in {install|configure}";
664                         usage
665                         exit 1
666                         ;;
667 esac
668
669 if [[ -n ${POLICY_GROUP} ]]; then
670         groups=$(groups)
671         if ! echo ${groups} | grep -qP "\b${POLICY_GROUP}"; then
672                 echo "error: ${POLICY_GROUP} is not a valid group for account ${POLICY_USER}"
673                 exit 1
674         fi
675 fi
676
677 if [[ -z ${POLICY_GROUP} ]]; then
678         numGroups=$(groups | sed "s/^.*: *//g" | wc -w)
679         if [ ${numGroups} -eq 1 ]; then
680                 export POLICY_GROUP=$(groups ${POLICY_USER} | sed "s/^.*: *//g")
681         else
682                 echo "error: ${POLICY_USER} belongs to multiple groups, one group \
683               must be provided for the installation"
684                 usage
685                 exit 1
686         fi
687 fi
688
689 if [[ -z ${POLICY_GROUP} ]]; then
690         echo "error: installation of root section must not provide the \
691               installation group owner argument."
692         usage
693         exit 1
694 fi
695
696 if [[ -z ${POLICY_LOGS} ]]; then
697     echo "POLICY_LOGS environment variable NOT set, default to /var/log/onap"
698     export POLICY_LOGS="/var/log/onap"
699 fi
700
701 FQDN=$(hostname -f 2> /dev/null)
702 if [[ $? != 0 || -z ${FQDN} ]]; then
703         echo "error: cannot determine the FQDN for this host $(hostname)."
704         exit 1
705 fi
706
707 if [[ ${OPERATION} == install ]]; then
708         case $COMPONENT_TYPE in
709                 base)   
710                         install_base
711                         ;;
712                 pdp)    
713                         install_tomcat_component
714                         ;;
715                 pap)
716                         install_tomcat_component
717                         ;;
718                 console)
719                         install_tomcat_component
720                         ;;
721                 mysql)
722                         install_mysql
723                         ;;
724                 elk)
725                         install_elk
726                         ;;              
727                 brmsgw)
728                         install_brmsgw
729                         ;;
730                 paplp|pdplp)
731                         install_logparser
732                         ;;
733                 *)              
734                         echo "invalid component type (${COMPONENT_TYPE}): must be in {base|pdp|pap|console|mysql|elk|brmsgw|paplp|pdplp}";
735                         usage
736                         exit 1
737                         ;;
738         esac
739 fi
740 if [[ ${OPERATION} == configure ]]; then
741
742         install_prereqs "${BASE_CONF}"
743
744         case $COMPONENT_TYPE in
745                 base)   
746                         configure_base
747                         component_preconfigure
748                         configure_keystore
749                         ;;
750                 pdp)    
751                         configure_component "${COMPONENT_TYPE}.conf" "${POLICY_HOME}/servers/${COMPONENT_TYPE}/"
752                         ;;
753                 pap)
754                         configure_component "${COMPONENT_TYPE}.conf" "${POLICY_HOME}/servers/${COMPONENT_TYPE}/"
755                         ;;
756                 console)
757                         configure_component "${COMPONENT_TYPE}.conf" "${POLICY_HOME}/servers/${COMPONENT_TYPE}/"
758                         ;;
759                 mysql)
760                         configure_mysql
761                         ;;
762                 elk)
763                         configure_elk
764                         ;;      
765                 brmsgw)
766                         configure_component "${COMPONENT_TYPE}.conf" "${POLICY_HOME}/servers/${COMPONENT_TYPE}/"
767                         ;;
768                 paplp|pdplp)
769                         configure_component "${COMPONENT_TYPE}.conf" "${POLICY_HOME}/servers/${COMPONENT_TYPE}/"
770                         ;;
771                 *)              
772                         echo "invalid component type (${COMPONENT_TYPE}): must be in {base|pdp|pap|console|mysql|elk|brmsgw|paplp|pdplp}";
773                         usage
774                         exit 1
775                         ;;
776         esac
777 fi
778
779
780 echo -n "Successful ${OPERATION} of ${COMPONENT_TYPE} under ${POLICY_USER}:${POLICY_GROUP} "
781 echo "ownership with umask $(umask)."