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