blob: e65329daf50736cca806380d8e6c55f76539bd65 [file] [log] [blame]
Pamela Dragoshd1728dc2017-02-14 19:57:17 -05001#!/bin/bash
2
3###
4# ============LICENSE_START=======================================================
5# Installation Package
6# ================================================================================
7# Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
8# ================================================================================
9# Licensed under the Apache License, Version 2.0 (the "License");
10# you may not use this file except in compliance with the License.
11# You may obtain a copy of the License at
12#
13# http://www.apache.org/licenses/LICENSE-2.0
14#
15# Unless required by applicable law or agreed to in writing, software
16# distributed under the License is distributed on an "AS IS" BASIS,
17# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18# See the License for the specific language governing permissions and
19# limitations under the License.
20# ============LICENSE_END=========================================================
21###
22
23
24function JAVA_HOME() {
25 if [[ $DEBUG == y ]]; then
26 echo "-- ${FUNCNAME[0]} $@ --"
27 set -x
28 fi
29
30 if [[ -z ${JAVA_HOME} ]]; then
31 echo "error: aborting installation: JAVA_HOME variable must be present in base.conf"
32 exit 1;
33 fi
34
35 echo "JAVA_HOME is ${JAVA_HOME}"
36}
37
38function POLICY_HOME() {
39 if [[ $DEBUG == y ]]; then
40 echo "-- ${FUNCNAME[0]} $@ --"
41 set -x
42 fi
43
Jorge Hernandez013b0e72017-08-04 01:13:56 -050044 local POLICY_HOME_ABS
45
Pamela Dragoshd1728dc2017-02-14 19:57:17 -050046 if [[ -z ${POLICY_HOME} ]]; then
47 echo "error: aborting installation: the installation directory POLICY_HOME must be set"
48 exit 1
49 fi
50
51 POLICY_HOME_ABS=$(readlink -f "${POLICY_HOME}")
52 if [[ -n ${POLICY_HOME_ABS} ]]; then
53 export POLICY_HOME=${POLICY_HOME_ABS}
54 fi
55
56 echo "POLICY_HOME is ${POLICY_HOME}"
57
58 # Do not allow installations from within POLICY_HOME dir or sub-dirs
59 if [[ "$(pwd)/" == ${POLICY_HOME}/* ]]; then
60 echo "error: aborting installation: cannot be executed from '${POLICY_HOME}' or sub-directories. "
61 exit 1
62 fi
63}
64
65function check_java() {
66 if [[ $DEBUG == y ]]; then
67 echo "-- ${FUNCNAME[0]} $@ --"
68 set -x
69 fi
70
Jorge Hernandez013b0e72017-08-04 01:13:56 -050071 local TARGET_JAVA_VERSION INSTALLED_JAVA_VERSION
72
Pamela Dragoshd1728dc2017-02-14 19:57:17 -050073 TARGET_JAVA_VERSION=$1
74
75 if [[ -z ${JAVA_HOME} ]]; then
76 echo "error: ${JAVA_HOME} is not set"
77 return 1
78 fi
79
80 if ! check_x_file "${JAVA_HOME}/bin/java"; then
81 echo "error: ${JAVA_HOME}/bin/java is not accessible"
82 return 1
83 fi
84
85 INSTALLED_JAVA_VERSION=$("${JAVA_HOME}/bin/java" -version 2>&1 | awk -F '"' '/version/ {print $2}')
86 if [[ -z $INSTALLED_JAVA_VERSION ]]; then
87 echo "error: ${JAVA_HOME}/bin/java is invalid"
88 return 1
89 fi
90
91 if [[ "${INSTALLED_JAVA_VERSION}" != ${TARGET_JAVA_VERSION}* ]]; then
92 echo "error: java version (${INSTALLED_JAVA_VERSION}) does not"\
93 "march desired version ${TARGET_JAVA_VERSION}"
94 return 1
95 fi
96
97 echo "OK: java ${INSTALLED_JAVA_VERSION} installed"
98
99 if ! type -p "${JAVA_HOME}/bin/keytool" > /dev/null 2>&1; then
100 echo "error: {JAVA_HOME}/bin/keytool is not installed"
101 return 1
102 fi
103}
104
105function process_configuration() {
106 if [[ $DEBUG == y ]]; then
107 echo "-- ${FUNCNAME[0]} $@ --"
108 set -x
109 fi
Jorge Hernandez013b0e72017-08-04 01:13:56 -0500110
111 local CONF_FILE name value
Pamela Dragoshd1728dc2017-02-14 19:57:17 -0500112
113 CONF_FILE=$1
114 while read line || [ -n "${line}" ]; do
115 if [[ -n ${line} ]] && [[ ${line} != *#* ]]; then
116 name=$(echo "${line%%=*}")
117 value=$(echo "${line#*=}")
118 # escape ampersand so that sed does not replace it with the search string
119 value=${value//&/\\&}
120 if [[ -z ${name} ]] || [[ -z $value ]]; then
121 echo "WARNING: ${line} missing name or value"
122 fi
123 export ${name}="${value}"
124 eval "${name}" "${value}" 2> /dev/null
125 fi
126 done < "${CONF_FILE}"
127 return 0
128}
129
130function component_preinstall() {
131 if [[ $DEBUG == y ]]; then
132 echo "-- ${FUNCNAME[0]} $@ --"
133 set -x
134 fi
135
136 /bin/sed -i -e 's!${{POLICY_HOME}}!'"${POLICY_HOME}!g" \
137 -e 's!${{FQDN}}!'"${FQDN}!g" \
138 *.conf > /dev/null 2>&1
139}
140
141function configure_component() {
142 if [[ $DEBUG == y ]]; then
143 echo "-- ${FUNCNAME[0]} $@ --"
144 set -x
145 fi
Jorge Hernandez013b0e72017-08-04 01:13:56 -0500146
Jorge Hernandezdc4ffa22017-08-04 16:26:24 -0500147 local CONF_FILE COMPONENT_ROOT_DIR SED_LINE SED_FILES name value
Pamela Dragoshd1728dc2017-02-14 19:57:17 -0500148
149 CONF_FILE=$1
150 COMPONENT_ROOT_DIR=$2
151
152 SED_LINE="sed -i"
153 SED_LINE+=" -e 's!\${{POLICY_HOME}}!${POLICY_HOME}!g' "
154 SED_LINE+=" -e 's!\${{POLICY_USER}}!${POLICY_USER}!g' "
155 SED_LINE+=" -e 's!\${{POLICY_GROUP}}!${POLICY_GROUP}!g' "
156 SED_LINE+=" -e 's!\${{KEYSTORE_PASSWD}}!${KEYSTORE_PASSWD}!g' "
157 SED_LINE+=" -e 's!\${{JAVA_HOME}}!${JAVA_HOME}!g' "
158
159 while read line || [ -n "${line}" ]; do
Jorge Hernandez0c07ac42017-08-28 18:25:23 -0500160 if [[ -n ${line} ]] && [[ ${line:0:1} != \# ]]; then
161 name=$(echo "${line%%=*}")
162 value=$(echo "${line#*=}")
163 # escape ampersand so that sed does not replace it with the search string
164 value=$(echo "${value}" | sed -e 's/[\/&]/\\&/g')
Pamela Dragoshd1728dc2017-02-14 19:57:17 -0500165 if [[ -z ${name} ]] || [[ -z ${value} ]]; then
166 echo "WARNING: ${line} missing name or value"
167 fi
Jorge Hernandezdc4ffa22017-08-04 16:26:24 -0500168 SED_LINE+=" -e 's/\${{${name}}}/${value}/g' "
Pamela Dragoshd1728dc2017-02-14 19:57:17 -0500169 fi
170 done < "$CONF_FILE"
171
172 SED_FILES=""
Jorge Hernandez0c07ac42017-08-28 18:25:23 -0500173 for sed_file in $(find "${COMPONENT_ROOT_DIR}" -type f -exec grep -Iq . {} \; -print 2> /dev/null); do
Pamela Dragoshd1728dc2017-02-14 19:57:17 -0500174 if fgrep -l '${{' ${sed_file} > /dev/null 2>&1; then
175 SED_FILES+="${sed_file} "
176 fi
177 done
178
179 if [[ -z ${SED_FILES} ]]; then
Jorge Hernandez0c07ac42017-08-28 18:25:23 -0500180 echo "WARNING: no files to perform variable expansion"
Pamela Dragoshd1728dc2017-02-14 19:57:17 -0500181 else
182 SED_LINE+=${SED_FILES}
183 eval "${SED_LINE}"
184 fi
185}
186
187function configure_settings() {
188 if [[ $DEBUG == y ]]; then
189 echo "-- ${FUNCNAME[0]} $@ --"
190 set -x
191 fi
192
193 # The goal is to have repositories for both 'release' and 'snapshot'
194 # artifacts. These may either be remote (e.g. Nexus) repositories, or
195 # a local file-based repository.
196 local fileRepoID=file-repository
197 local fileRepoUrl=file:$HOME_M2/file-repository
198 mkdir -p "${fileRepoUrl#file:}"
199
200 # The following parameters are also used outside of this function.
201 # if snapshotRepositoryUrl and/or releaseRepositoryUrl is defined,
202 # the corresponding ID and url will be updated below
203 releaseRepoID=${fileRepoID}
204 releaseRepoUrl=${fileRepoUrl}
205 snapshotRepoID=${fileRepoID}
206 snapshotRepoUrl=${fileRepoUrl}
207
208 # if both snapshotRepositoryUrl and releaseRepositoryUrl are null,
209 # use standalone-settings.xml that just defines the file-based repo.
210 # if only one of them is specified, use file-based repo for the other.
211 if [[ -z "$snapshotRepositoryUrl" && -z $releaseRepositoryUrl ]]; then
212 echo "snapshotRepositoryUrl and releaseRepositoryUrl properties not set, configuring settings.xml for standalone operation"
213 mv $HOME_M2/standalone-settings.xml $HOME_M2/settings.xml
214 else
215 rm $HOME_M2/standalone-settings.xml
216
217 if [[ -n "${snapshotRepositoryUrl}" ]] ; then
218 snapshotRepoID=${snapshotRepositoryID}
219 snapshotRepoUrl=${snapshotRepositoryUrl}
220 fi
221 if [[ -n "${releaseRepositoryUrl}" ]] ; then
222 releaseRepoID=${releaseRepositoryID}
223 releaseRepoUrl=${releaseRepositoryUrl}
224 fi
225 fi
226
227 SED_LINE="sed -i"
228 SED_LINE+=" -e 's!\${{snapshotRepositoryID}}!${snapshotRepoID}!g' "
229 SED_LINE+=" -e 's!\${{snapshotRepositoryUrl}}!${snapshotRepoUrl}!g' "
230 SED_LINE+=" -e 's!\${{releaseRepositoryID}}!${releaseRepoID}!g' "
231 SED_LINE+=" -e 's!\${{releaseRepositoryUrl}}!${releaseRepoUrl}!g' "
232 SED_LINE+=" -e 's!\${{repositoryUsername}}!${repositoryUsername}!g' "
233 SED_LINE+=" -e 's!\${{repositoryPassword}}!${repositoryPassword}!g' "
234 SED_LINE+=" -e 's!\${{fileRepoID}}!${fileRepoID}!g' "
235 SED_LINE+=" -e 's!\${{fileRepoUrl}}!${fileRepoUrl}!g' "
236
237 SED_LINE+="$HOME_M2/settings.xml"
238 eval "${SED_LINE}"
239
240}
241
242
243function check_r_file() {
244 if [[ $DEBUG == y ]]; then
245 echo "-- ${FUNCNAME[0]} $@ --"
246 set -x
247 fi
248
249 FILE=$1
250 if [[ ! -f ${FILE} || ! -r ${FILE} ]]; then
251 return 1
252 fi
253
254 return 0
255}
256
257function check_x_file() {
258 if [[ $DEBUG == y ]]; then
259 echo "-- ${FUNCNAME[0]} $@ --"
260 set -x
261 fi
262
263 FILE=$1
264 if [[ ! -f ${FILE} || ! -x ${FILE} ]]; then
265 return 1
266 fi
267
268 return 0
269}
270
271function install_prereqs() {
272 if [[ $DEBUG == y ]]; then
273 echo "-- ${FUNCNAME[0]} $@ --"
274 set -x
275 fi
Jorge Hernandez013b0e72017-08-04 01:13:56 -0500276
277 local CONF_FILE HOME_OWNER
Pamela Dragoshd1728dc2017-02-14 19:57:17 -0500278
279 CONF_FILE=$1
280
281 if ! check_r_file "${CONF_FILE}"; then
282 echo "error: aborting ${COMPONENT_TYPE} installation: ${CONF_FILE} is not accessible"
283 exit 1
284 fi
285
286 if ! process_configuration "${CONF_FILE}"; then
287 echo "error: aborting ${COMPONENT_TYPE} installation: cannot process configuration ${CONF_FILE}"
288 exit 1
289 fi
290
291 if ! check_java "1.8"; then
292 echo "error: aborting ${COMPONENT_TYPE} installation: invalid java version"
293 exit 1
294 fi
295
296
297 if [[ -z ${POLICY_HOME} ]]; then
298 echo "error: aborting ${COMPONENT_TYPE} installation: ${POLICY_HOME} is not set"
299 exit 1
300 fi
301
302 HOME_OWNER=$(ls -ld "${POLICY_HOME}" | awk '{print $3}')
303 if [[ ${HOME_OWNER} != ${POLICY_USER} ]]; then
304 echo "error: aborting ${COMPONENT_TYPE} installation: ${POLICY_USER} does not own ${POLICY_HOME} directory"
305 exit 1
306 fi
307
308 echo -n "Starting ${OPERATION} of ${COMPONENT_TYPE} under ${POLICY_USER}:${POLICY_GROUP} "
309 echo "ownership with umask $(umask)."
310}
311
312function configure_base() {
313 if [[ $DEBUG == y ]]; then
314 echo "-- ${FUNCNAME[0]} $@ --"
315 set -x
316 fi
Jorge Hernandez013b0e72017-08-04 01:13:56 -0500317
318 local BASH_PROFILE_LINE PROFILE_LINE
Pamela Dragoshd1728dc2017-02-14 19:57:17 -0500319
320 # check if fqdn is set in base.conf and use that value if set
321 if [[ -z ${INSTALL_FQDN} ]]
322 then
323 echo "FQDN not set in config...using the default FQDN ${FQDN}"
324 else
325 echo "Using FQDN ${INSTALL_FQDN} from config"
326 FQDN=${INSTALL_FQDN}
327 fi
328
329 configure_component "${BASE_CONF}" "${POLICY_HOME}"
330
331 configure_settings
332
333 BASH_PROFILE_LINE=". ${POLICY_HOME}/etc/profile.d/env.sh"
334 PROFILE_LINE="ps -p \$\$ | grep -q bash || . ${POLICY_HOME}/etc/profile.d/env.sh"
335
336 # Note: adding to .bashrc instead of .bash_profile
337 if ! fgrep -x "${BASH_PROFILE_LINE}" "${HOME}/.bashrc" >/dev/null 2>&1; then
338 echo "${BASH_PROFILE_LINE}" >> "${HOME}/.bashrc"
339 fi
340
341 if ! fgrep -x "${PROFILE_LINE}" "${HOME}/.profile" >/dev/null 2>&1; then
342 echo "${PROFILE_LINE}" >> "${HOME}/.profile"
343 fi
344
Pamela Dragoshd1728dc2017-02-14 19:57:17 -0500345 . "${POLICY_HOME}/etc/profile.d/env.sh"
346
347 cat "${POLICY_HOME}"/etc/cron.d/* | crontab
348}
349
350function install_base() {
351 if [[ $DEBUG == y ]]; then
352 echo "-- ${FUNCNAME[0]} $@ --"
353 set -x
354 fi
Jorge Hernandez013b0e72017-08-04 01:13:56 -0500355
356 local POLICY_HOME_CONTENTS BASE_TGZ BASEX_TGZ BASH_PROFILE_LINE
Pamela Dragoshd1728dc2017-02-14 19:57:17 -0500357
358 install_prereqs "${BASE_CONF}"
359
360 # following properties must be set:
361 # POLICY_HOME - installation directory, must exist and be writable
362
363 # test that all required properties are set
364 for var in POLICY_HOME JAVA_HOME
365 do
366 if [[ -z $(eval echo \$$var) ]]; then
367 echo "ERROR: $var must be set in $BASE_CONF"
368 exit 1
369 fi
370 done
371
372 if [[ ! ( -d "$POLICY_HOME" && -w "$POLICY_HOME" ) ]]; then
373 echo "ERROR: Installation directory $POLICY_HOME does not exist or not writable"
374 exit 1
375 fi
376
377 if ! /bin/rm -fr "${POLICY_HOME}"/* > /dev/null 2>&1; then
378 echo "error: aborting base installation: cannot delete the underlying ${POLICY_HOME} files"
379 exit 1
380 fi
381
382 POLICY_HOME_CONTENTS=$(ls -A "${POLICY_HOME}" 2> /dev/null)
383 if [[ -n ${POLICY_HOME_CONTENTS} ]]; then
384 echo "error: aborting base installation: ${POLICY_HOME} directory is not empty"
385 exit 1
386 fi
387
388 if ! /bin/mkdir -p "${POLICY_HOME}/logs/" > /dev/null 2>&1; then
389 echo "error: aborting base installation: cannot create ${POLICY_HOME}/logs/"
390 exit 1
391 fi
392
393 BASE_TGZ=$(ls base-*.tar.gz)
394 if [ ! -r ${BASE_TGZ} ]; then
395 echo "error: aborting: base package is not accessible"
396 exit 1
397 fi
398
399 tar -tzf ${BASE_TGZ} > /dev/null 2>&1
400 if [[ $? != 0 ]]; then
401 echo >&2 "error: aborting installation: invalid base package file: ${BASE_TGZ}"
402 exit 1
403 fi
404
Jorge Hernandez0c07ac42017-08-28 18:25:23 -0500405 BASEX_TGZ=$(ls basex-*.tar.gz 2> /dev/null)
406 if [ -z ${BASEX_TGZ} ]; then
407 echo "warning: no basex application package present"
Pamela Dragoshd1728dc2017-02-14 19:57:17 -0500408 BASEX_TGZ=
409 else
410 tar -tzf ${BASEX_TGZ} > /dev/null 2>&1
411 if [[ $? != 0 ]]; then
Jorge Hernandez0c07ac42017-08-28 18:25:23 -0500412 echo >&2 "warning: invalid basex application package tar file: ${BASEX_TGZ}"
Pamela Dragoshd1728dc2017-02-14 19:57:17 -0500413 BASEX_TGZ=
414 fi
415 fi
Pamela Dragoshd1728dc2017-02-14 19:57:17 -0500416
417 # Undo any changes in the $HOME directory if any
418
419 BASH_PROFILE_LINE=". ${POLICY_HOME}/etc/profile.d/env.sh"
420# PROFILE_LINE="ps -p \$\$ | grep -q bash || . ${POLICY_HOME}/etc/profile.d/env.sh"
421
422 # Note: using .bashrc instead of .bash_profile
423 if [[ -f ${HOME}/.bashrc ]]; then
424 /bin/sed -i.bak "\:${BASH_PROFILE_LINE}:d" "${HOME}/.bashrc"
425 fi
426
427# if [[ -f ${HOME}/.profile ]]; then
428# /bin/sed -i.bak "\:${PROFILE_LINE}:d" "${HOME}/.profile"
429# fi
430
431 tar -C ${POLICY_HOME} -xf ${BASE_TGZ} --no-same-owner
432 if [[ $? != 0 ]]; then
433 # this should not happened
434 echo "error: aborting base installation: base package cannot be unpacked: ${BASE_TGZ}"
435 exit 1
436 fi
437
438 if [ ! -z ${BASEX_TGZ} ]; then
439 tar -C ${POLICY_HOME} -xf ${BASEX_TGZ} --no-same-owner
440 if [[ $? != 0 ]]; then
441 # this should not happened
442 echo "warning: basex package cannot be unpacked: ${BASEX_TGZ}"
443 fi
444 fi
445
446# /bin/mkdir -p ${POLICY_HOME}/etc/ssl > /dev/null 2>&1
447# /bin/mkdir -p ${POLICY_HOME}/etc/init.d > /dev/null 2>&1
448# /bin/mkdir -p ${POLICY_HOME}/nagios/tmp > /dev/null 2>&1
449# /bin/mkdir -p ${POLICY_HOME}/tmp > /dev/null 2>&1
450# /bin/mkdir -p ${POLICY_HOME}/var > /dev/null 2>&1
451
452# chmod -R 755 ${POLICY_HOME}/nagios > /dev/null 2>&1
453
Pamela Dragoshd1728dc2017-02-14 19:57:17 -0500454 if [[ -d $HOME_M2 ]]; then
455 echo "Renaming existing $HOME_M2 to $HOME/m2.$TIMESTAMP"
456 mv $HOME_M2 $HOME/m2.$TIMESTAMP
457 if [[ $? != 0 ]]; then
458 echo "WARNING: Failed to rename $HOME_M2 directory; will use old directory"
459 fi
460 fi
461 if [[ ! -d $HOME_M2 ]]; then
462 echo "Moving m2 directory to $HOME_M2"
463 mv $POLICY_HOME/m2 $HOME_M2
464 if [[ $? != 0 ]]; then
465 echo "ERROR: Error in moving m2 directory"
466 exit 1
467 fi
468 fi
469
470 configure_base
471
Jorge Hernandez5ed2f952017-09-06 19:42:47 -0500472 # save ${BASE_CONF} in PDP-D installation
Jorge Hernandez735ece52017-07-18 01:26:12 -0500473 cp "${BASE_CONF}" "${POLICY_HOME}"/etc/profile.d
474
Pamela Dragoshd1728dc2017-02-14 19:57:17 -0500475# if ! create_keystore; then
476# echo "error: aborting base installation: creating keystore"
477# exit 1
478# fi
479
480# list_unexpanded_files ${POLICY_HOME}
481
482}
483
484function install_controller()
485{
486 if [[ $DEBUG == y ]]; then
487 echo "-- ${FUNCNAME[0]} $@ --"
488 set -x
489 fi
490
491 if [[ $DEBUG == y ]]; then
492 echo "-- ${FUNCNAME[0]} $@ --"
493 set -x
494 fi
495
496 if [[ -f "${HOME}/.bashrc" ]]; then
497 source "${HOME}/.bashrc"
498 fi
499
500 if [[ -z ${POLICY_HOME} ]]; then
501 echo "error: aborting installation: POLICY_HOME environment variable is not set."
502 exit 1
503 fi
504
505 if ! check_r_file ${POLICY_HOME}/etc/profile.d/env.sh; then
506 echo "error: aborting installation: ${POLICY_HOME}/etc/profile.d/env.sh is not accessible"
507 exit 1
508 fi
Jorge Hernandez013b0e72017-08-04 01:13:56 -0500509
510 local CONTROLLER_CONF CONTROLLER_ZIP RULES_JAR SOURCE_DIR CONTROLLER_DIR AAAA BBBB PORT UTOPIC ARTIFACT_VERSION
Pamela Dragoshd1728dc2017-02-14 19:57:17 -0500511
512 CONTROLLER_CONF=$COMPONENT_TYPE.conf
513 install_prereqs "${CONTROLLER_CONF}"
514
515 # following properties must be set in conf file:
516 # CONTROLLER_ARTIFACT_ID - Maven artifactId for controller
517 # CONTROLLER_NAME - directory name for the controller; controller will be installed to
518 # $POLICY_HOME/controllers/$CONTROLLER_NAME
519 # CONTROLLER_PORT - port number for the controller REST interface
520 # RULES_ARTIFACT - rules artifact specifier: groupId:artifactId:version
521
522 # test that all required properties are set
523 for var in CONTROLLER_ARTIFACT_ID CONTROLLER_NAME CONTROLLER_PORT RULES_ARTIFACT UEB_TOPIC
524 do
525 if [[ -z $(eval echo \$$var) ]]; then
526 echo "ERROR: $var must be set in $CONTROLLER_CONF"
527 exit 1
528 fi
529 done
530
531 CONTROLLER_ZIP=$(ls $CONTROLLER_ARTIFACT_ID*.zip 2>&-)
532 if [[ -z $CONTROLLER_ZIP ]]; then
533 echo "ERROR: Cannot find controller zip file ($CONTROLLER_ARTIFACT_ID*.zip)"
534 exit 1
535 fi
536
537 if [[ ! "$CONTROLLER_NAME" =~ ^[A-Za-z0-9_-]+$ ]]; then
538 echo "ERROR: CONTROLLER_NAME may only contain alphanumeric, underscore, and dash characters"
539 exit 1
540 fi
541
542 if [[ ! "$CONTROLLER_PORT" =~ ^[0-9]+$ ]]; then
543 echo "ERROR: CONTROLLER_PORT is not a valid integer"
544 exit 1
545 fi
546
547 # split artifact string into parts
548 IFS=: read RULES_GROUPID RULES_ARTIFACTID RULES_VERSION <<<$RULES_ARTIFACT
549 if [[ -z $RULES_GROUPID || -z $RULES_ARTIFACTID || -z $RULES_VERSION ]]; then
550 echo "ERROR: Invalid setting for RULES_ARTIFACT property"
551 exit 1
552 fi
553
554 #RULES_JAR=$RULES_ARTIFACTID-$RULES_VERSION.jar
555 RULES_JAR=$(echo ${RULES_ARTIFACTID}-*.jar)
556 if ! check_r_file $RULES_JAR; then
557 echo "WARNING: Rules jar file $RULES_JAR not found in installer package, must be installed manually"
558 RULES_JAR=
559 fi
560
561
562 SOURCE_DIR=$PWD
563 CONTROLLER_DIR=$POLICY_HOME
564
565 cd $CONTROLLER_DIR
566
567 echo "Unpacking controller zip file"
568 # use jar command in case unzip not present on system
569 jar xf $SOURCE_DIR/$CONTROLLER_ZIP
570 if [[ $? != 0 ]]; then
571 echo "ERROR: unpack of controller zip file failed, install aborted"
572 exit 1
573 fi
574
575 chmod +x bin/*
576
577 # Perform base variable replacement in controller config file
578 configure_component "${SOURCE_DIR}/${BASE_CONF}" "${CONTROLLER_DIR}"
579
580 # Perform variable replacements in config files.
581 # config files may contain the following strings that need to be replaced with
582 # real values:
583 # AAAA - artifactId
584 # BBBB - Substring of AAAA after first dash (stripping initial "ncomp-" or "policy-")
585 # PORT - Port number for REST server
586
587 echo "Performing variable replacement in config files"
588 AAAA=$CONTROLLER_ARTIFACT_ID
589 BBBB=${AAAA#[a-z]*-}
590 PORT=$CONTROLLER_PORT
591 UTOPIC=${UEB_TOPIC}
592
593 for file in config/*
594 do
595 sed -i -e "s/AAAA/$AAAA/" -e "s/BBBB/$BBBB/" -e "s/PORT/$PORT/" -e "s!\${{UEB_TOPIC}}!${UTOPIC}!" $file
596 if [[ $? != 0 ]]; then
597 echo "ERROR: variable replacement failed for file $file, install aborted"
598 exit 1
599 fi
600 done
601
Pamela Dragoshd1728dc2017-02-14 19:57:17 -0500602 # append properties for rules artifact to server properties
603 cat >>config/server.properties <<EOF
604
605rules.groupId=$RULES_GROUPID
606rules.artifactId=$RULES_ARTIFACTID
607rules.version=$RULES_VERSION
608EOF
609
610 # TODO: run pw.sh script to set passwords
611
612 # return to directory where we started
613 cd $SOURCE_DIR
614
615 # install rules jar into repository if present
616 if [[ -n $RULES_JAR ]]; then
617 # can't use RULES_VERSION because may be set to "LATEST",
618 # so extract version from the jar filename
619 ARTIFACT_VERSION=$(sed -e "s/${RULES_ARTIFACTID}-//" -e "s/\.jar//" <<<${RULES_JAR})
620 if [[ -n $repositoryUrl ]]; then
621 echo "Deploying rules artifact to Policy Repository"
622 mvn deploy:deploy-file -Dfile=$RULES_JAR \
623 -DgroupId=$RULES_GROUPID -DartifactId=$RULES_ARTIFACTID -Dversion=$ARTIFACT_VERSION \
624 -DrepositoryId=${repositoryID} -Durl=${repositoryUrl} \
625 -DgeneratePom=true -DupdateReleaseInfo=true
626 else
627 echo "Installing rules artifact into local .m2 repository"
628 mvn --offline org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file \
629 -Dfile=$RULES_JAR -DgeneratePom=true -DupdateReleaseInfo=true
630 fi
631 fi
632
633 update_monitor $CONTROLLER_NAME
Jorge Hernandez5ed2f952017-09-06 19:42:47 -0500634
635 # save install configuration as an environment file
636 ln -s -f "${POLICY_HOME}/etc/profile.d/${BASE_CONF}" "${POLICY_HOME}/config/${BASE_CONF}.environment"
Pamela Dragoshd1728dc2017-02-14 19:57:17 -0500637}
638
639
640function update_monitor() {
641 if [[ $DEBUG == y ]]; then
642 echo "-- ${FUNCNAME[0]} $@ --"
643 set -x
644 fi
Jorge Hernandez013b0e72017-08-04 01:13:56 -0500645
646 local NAME lastline
Pamela Dragoshd1728dc2017-02-14 19:57:17 -0500647
648 NAME=$1
649
650 if [[ -f ${POLICY_HOME}/etc/monitor/monitor.cfg ]]; then
651 if grep -q "^${NAME}=" ${POLICY_HOME}/etc/monitor/monitor.cfg; then
652 echo "OK: updating monitoring entry for ${NAME}"
653 /bin/sed -i.bak \
654 -e "s/^${NAME}=.*/${NAME}=off/g" \
655 ${POLICY_HOME}/etc/monitor/monitor.cfg
656 else
657 # make sure file ends with newline
658 lastline=$(tail -n 1 ${POLICY_HOME}/etc/monitor/monitor.cfg; echo x)
659 lastline=${lastline%x}
660 if [ "${lastline: -1}" = $'\n' ]; then
661 echo "OK: adding an entry for ${NAME} in ${POLICY_HOME}/etc/monitor/monitor.cfg"
662 else
663 echo "OK: adding an entry for ${NAME} in ${POLICY_HOME}/etc/monitor/monitor.cfg (with newline)"
664 echo "" >> ${POLICY_HOME}/etc/monitor/monitor.cfg
665 fi
666
667
668 echo "${NAME}=off" >> ${POLICY_HOME}/etc/monitor/monitor.cfg
669 fi
670 else
671 echo "WARNING: ${POLICY_HOME}/etc/monitor/monitor.cfg does not exist. No monitoring enabled."
672 fi
673}
674
675# Usage: getPomAttributes <pom-file> <attribute> ...
676#
677# This function performs simplistic parsing of a 'pom.xml' file, extracting
678# the specified attributes (e.g. 'groupId', 'artifactId', 'version'). The
679# attributes are returned as environment variables with the associated name.
680
681function getPomAttributes
682{
683 local tab=$'\t'
684 local rval=0
685 local file="$1"
686 local attr
687 local value
688 shift
689 for attr in "$@" ; do
690 # Try to fetch the parameter associated with the 'pom.xml' file.
691 # Initially, the 'parent' element is excluded. If the desired
692 # parameter is not found, the 'parent' element is included in the
693 # second attempt.
694 value=$(sed -n \
695 -e '/<parent>/,/<\/parent>/d' \
696 -e '/<dependencies>/,/<\/dependencies>/d' \
697 -e '/<build>/,/<\/build>/d' \
698 -e "/^[ ${tab}]*<${attr}>\([^<]*\)<\/${attr}>.*/{s//\1/p;}" \
699 <"${file}")
700
701 if [[ "${value}" == "" ]] ; then
702 # need to check parent for parameter
703 value=$(sed -n \
704 -e '/<dependencies>/,/<\/dependencies>/d' \
705 -e '/<build>/,/<\/build>/d' \
706 -e "/^[ ${tab}]*<${attr}>\([^<]*\)<\/${attr}>.*/{s//\1/p;}" \
707 <"${file}")
708 if [[ "${value}" == "" ]] ; then
709 echo "${file}: Can't determine ${attr}" >&2
710 rval=1
711 fi
712 fi
713 # the following sets an environment variable with the name referred
714 # to by ${attr}
715 read ${attr} <<<"${value}"
716 done
717 return ${rval}
718}
719
720
721# Usage: installPom <pom-file>
722#
723# This function installs a 'pom.xml' file in the local repository
724
725function installPom
726{
727 # need to extract attributes from POM file
728 if getPomAttributes "${1}" artifactId groupId version ; then
729 local repoID repoUrl
730 if [[ "${version}" =~ SNAPSHOT ]] ; then
731 repoID=${snapshotRepoID}
732 repoUrl=${snapshotRepoUrl}
733 else
734 repoID=${releaseRepoID}
735 repoUrl=${releaseRepoUrl}
736 fi
737 echo "${1}: Deploying POM artifact to remote repository"
738 mvn deploy:deploy-file -Dfile="$1" \
739 -Dpackaging=pom -DgeneratePom=false \
740 -DgroupId=${groupId} \
741 -DartifactId=${artifactId} \
742 -Dversion=${version} \
743 -DrepositoryId=${repoID} -Durl=${repoUrl} \
744 -DupdateReleaseInfo=true
745 else
746 echo "${1}: Can't install pom due to missing attributes" >&2
747 return 1
748 fi
749}
750
751# Usage: installJar <jar-file>
752#
753# This function installs a JAR file in the local repository, as well as
754# the 'pom.xml' member it contains.
755
756function installJar
757{
758 local dir=$(mktemp -d)
759 local jar="${1##*/}"
760 cp -p "${1}" "${dir}/${jar}"
761
762 (
763 local rval=0
764 cd "${dir}"
765 # determine name of 'pom' file within JAR
766 local pom=$(jar tf ${jar} META-INF | grep '/pom\.xml$' | head -1)
767 if [[ "${pom}" ]] ; then
768 # extract pom file
769 jar xf ${jar} "${pom}"
770
771 # determine version from pom file
772 if getPomAttributes "${pom}" version ; then
773 local repoID repoUrl
774 if [[ "${version}" =~ SNAPSHOT ]] ; then
775 repoID=${snapshotRepoID}
776 repoUrl=${snapshotRepoUrl}
777 else
778 repoID=${releaseRepoID}
779 repoUrl=${releaseRepoUrl}
780 fi
781 echo "${1}: Deploying JAR artifact to remote repository"
782 mvn deploy:deploy-file \
783 -Dfile=${jar} \
784 -Dversion=${version} \
785 -Dpackaging=jar -DgeneratePom=false -DpomFile=${pom} \
786 -DrepositoryId=${repoID} -Durl=${repoUrl} \
787 -DupdateReleaseInfo=true
788 else
789 echo "${1}: Can't determine version from 'pom.xml'" >&2
790 rval=1
791 fi
792 else
793 echo "${1}: Can't find 'pom.xml'" >&2
794 rval=1
795 fi
796 rm -rf ${dir}
797 return ${rval}
798 )
799}
800
801# Unzip the 'artifacts-*.zip' file, and install all of the associated
802# artifacts into the local repository.
803
804function installArtifacts
805{
806 local file
807 if [[ -f $(echo artifacts-*.zip) ]] ; then
808 # use jar command in case unzip not present on system
809 jar xf artifacts-*.zip
810 for file in artifacts/* ; do
811 case "${file}" in
812 *pom.xml|*.pom) installPom "${file}";;
813 *.jar) installJar "${file}";;
814 *) echo "${file}: Don't know how to install artifact" >&2;;
815 esac
816 done
817 fi
818}
819
Jorge Hernandez735ece52017-07-18 01:26:12 -0500820function installFeatures
821{
822 if [[ $DEBUG == y ]]; then
823 echo "-- ${FUNCNAME[0]} $@ --"
824 set -x
825 fi
Jorge Hernandez013b0e72017-08-04 01:13:56 -0500826
827 local name featureConf
Krzysztof Kwieciene572b592017-08-08 11:44:36 +0200828 export FEATURES_HOME="${POLICY_HOME}/${FEATURES_DIR}"
829 echo "FEATURES_HOME is ${FEATURES_HOME}"
Jorge Hernandez735ece52017-07-18 01:26:12 -0500830
Jorge Hernandez735ece52017-07-18 01:26:12 -0500831 mkdir -p "${FEATURES_HOME}" > /dev/null 2>&1
832 if [[ -d "${FEATURES_HOME}" && -x "${FEATURES_HOME}" ]]; then
833 SOURCE_DIR=$PWD
834 for feature in feature-*.zip ; do
835 name="${feature#feature-}"
836 name="${name%-[0-9]*\.zip}"
837 mkdir -p "${FEATURES_HOME}/${name}" > /dev/null 2>&1
838 (cd "${FEATURES_HOME}/${name}"; jar xf ${SOURCE_DIR}/${feature})
Jorge Hernandez9eb6ad52017-08-02 09:58:13 -0500839 featureConf="feature-${name}.conf"
840 if [[ -r "${featureConf}" ]]; then
841 configure_component "${featureConf}" "${FEATURES_HOME}"
842 cp "${featureConf}" "${POLICY_HOME}"/etc/profile.d
843 echo "feature ${name} has been installed (configuration present)"
844 else
845 echo "feature ${name} has been installed (no configuration present)"
846 fi
Jorge Hernandez735ece52017-07-18 01:26:12 -0500847 done
848
Jorge Hernandez9eb6ad52017-08-02 09:58:13 -0500849 echo "applying base configuration to features"
Jorge Hernandez735ece52017-07-18 01:26:12 -0500850 configure_component "${BASE_CONF}" "${FEATURES_HOME}"
851 else
852 echo "error: aborting ${FEATURES_HOME} is not accessible"
853 exit 1
854 fi
855}
856
Pamela Dragoshd1728dc2017-02-14 19:57:17 -0500857function do_install()
858{
859 if [[ $DEBUG == y ]]; then
860 echo "-- ${FUNCNAME[0]} $@ --"
861 set -x
862 fi
863
864 echo "Starting installation at $(date)"
865 echo
866
867 COMPONENT_TYPE=base
868 BASE_CONF=base.conf
869 install_base
870 component_preinstall
871
872 COMPONENT_TYPE=policy-management
873 install_controller
874
Jorge Hernandez735ece52017-07-18 01:26:12 -0500875 installFeatures
Pamela Dragoshd1728dc2017-02-14 19:57:17 -0500876 installArtifacts
Jorge Hernandezad540782017-09-19 07:41:39 -0500877
878
879 if [[ -f apps-installer ]]; then
880 # if exists, any customizations to the
881 # base drools installation from the drools apps
882 # is executed here
883
Jorge Hernandezad540782017-09-19 07:41:39 -0500884 ./apps-installer
885 fi
Jorge Hernandez735ece52017-07-18 01:26:12 -0500886
Pamela Dragoshd1728dc2017-02-14 19:57:17 -0500887 echo
888 echo "Installation complete"
889 echo "Please logoff and login again to update shell environment"
890
891}
892
Pamela Dragoshd1728dc2017-02-14 19:57:17 -0500893export POLICY_USER=$(/usr/bin/id -un)
894export POLICY_GROUP=$POLICY_USER
895
896FQDN=$(hostname -f 2> /dev/null)
897if [[ $? != 0 || -z ${FQDN} ]]; then
898 echo "error: cannot determine the FQDN for this host $(hostname)."
899 exit 1
900fi
901
902TIMESTAMP=$(date "+%Y%m%d-%H%M%S")
903LOGFILE=$PWD/install.log.$TIMESTAMP
904
905OPERATION=install
906BASE_CONF=base.conf
Jorge Hernandez013b0e72017-08-04 01:13:56 -0500907HOME_M2=$HOME/.m2
Krzysztof Kwieciene572b592017-08-08 11:44:36 +0200908FEATURES_DIR="features"
Pamela Dragoshd1728dc2017-02-14 19:57:17 -0500909
910do_install 2>&1 | tee $LOGFILE