diff options
Diffstat (limited to 'src/main/swm/common')
-rw-r--r-- | src/main/swm/common/common.env | 19 | ||||
-rw-r--r-- | src/main/swm/common/deinstall.env | 15 | ||||
-rw-r--r-- | src/main/swm/common/deinstall_postproc.sh | 26 | ||||
-rw-r--r-- | src/main/swm/common/deinstall_preproc.sh | 45 | ||||
-rw-r--r-- | src/main/swm/common/install.env | 37 | ||||
-rw-r--r-- | src/main/swm/common/install_postproc.sh | 191 | ||||
-rw-r--r-- | src/main/swm/common/install_preproc.sh | 46 | ||||
-rw-r--r-- | src/main/swm/common/utils/findreplace.sh | 94 |
8 files changed, 473 insertions, 0 deletions
diff --git a/src/main/swm/common/common.env b/src/main/swm/common/common.env new file mode 100644 index 0000000..b6ae68a --- /dev/null +++ b/src/main/swm/common/common.env @@ -0,0 +1,19 @@ + +# This file is used to source variables from the generated archetype +# Because the *proc.sh scripts may contain variables that conflict with +# Maven variables, we exclude those scripts and only replace values here +ROOT_DIR=${INSTALL_ROOT}${distFilesRoot}; export ROOT_DIR +LRMCLI=${INSTALL_ROOT}/opt/app/aft/scldlrm/bin/lrmcli +PATH=$PATH:`dirname $0`/utils; export PATH + + +# Fail - used to quickly exit with a rc and error message +fail() { + rc=$1 + shift; + echo "PROC_USER_MSG: [$rc]: $@" >&2 + exit ${rc} +} + + + diff --git a/src/main/swm/common/deinstall.env b/src/main/swm/common/deinstall.env new file mode 100644 index 0000000..9a54c67 --- /dev/null +++ b/src/main/swm/common/deinstall.env @@ -0,0 +1,15 @@ + +# This file is used to set the environment which the install_*.sh files +# will use when executing. Only set variables that must be derived at +# installation time here. For variables that should be set by the installer +# in SWM, add VariableDescriptor elements to the descriptor.xml. Place +# logical steps in the install_preproc.sh or install_postproc.sh. + +. `dirname $0`/common.env + +# CHECK FOR ABSOLUTELY REQUIRED VARIABLES HERE +#derive version components for lrm.xml +MAJOR_VERSION=`echo ${AFTSWM_ACTION_NEW_VERSION} | awk -F'.' '{print $1}'`; export MAJOR_VERSION +MINOR_VERSION=`echo ${AFTSWM_ACTION_NEW_VERSION} | awk -F'.' '{print $2}'`; export MINOR_VERSION +PATCH_VERSION=`echo ${AFTSWM_ACTION_NEW_VERSION} | awk -F'.' '{print $3}'`; export PATCH_VERSION + diff --git a/src/main/swm/common/deinstall_postproc.sh b/src/main/swm/common/deinstall_postproc.sh new file mode 100644 index 0000000..b761056 --- /dev/null +++ b/src/main/swm/common/deinstall_postproc.sh @@ -0,0 +1,26 @@ +#!/bin/sh +#******************************************************************************* +# ============LICENSE_START======================================================= +# org.onap.dmaap +# ================================================================================ +# Copyright © 2017 AT&T Intellectual Property. All rights reserved. +# ================================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============LICENSE_END========================================================= +# +# ECOMP is a trademark and service mark of AT&T Intellectual Property. +# +#******************************************************************************* + +. `dirname $0`/deinstall.env +rm -rf ${ROOT_DIR} +exit 0 diff --git a/src/main/swm/common/deinstall_preproc.sh b/src/main/swm/common/deinstall_preproc.sh new file mode 100644 index 0000000..9ee8c93 --- /dev/null +++ b/src/main/swm/common/deinstall_preproc.sh @@ -0,0 +1,45 @@ +#!/bin/sh +#******************************************************************************* +# ============LICENSE_START======================================================= +# org.onap.dmaap +# ================================================================================ +# Copyright © 2017 AT&T Intellectual Property. All rights reserved. +# ================================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============LICENSE_END========================================================= +# +# ECOMP is a trademark and service mark of AT&T Intellectual Property. +# +#******************************************************************************* + +. `dirname $0`/deinstall.env + +LRMCLI=${INSTALL_ROOT}/opt/app/aft/scldlrm/bin/lrmcli +PATH=$PATH:`dirname $0`/utils; export PATH + +runningCount=`${LRMCLI} -running | grep -w ${SOA_CLOUD_NAMESPACE}.${AFTSWM_ACTION_ARTIFACT_NAME} | wc -l` || fail 300 "Unable to determine how many instances are running prior to notifying LRM of the upgrade" + +if [ "${runningCount}" -eq 0 ]; then + +${LRMCLI} -delete -name ${SOA_CLOUD_NAMESPACE}.${AFTSWM_ACTION_ARTIFACT_NAME} -version ${AFTSWM_ACTION_NEW_VERSION} -routeoffer ${AFT_SERVICE_ENV} || exit 101 + + else + ${LRMCLI} -shutdown -name ${SOA_CLOUD_NAMESPACE}.${AFTSWM_ACTION_ARTIFACT_NAME} -version ${AFTSWM_ACTION_NEW_VERSION} -routeoffer ${AFT_SERVICE_ENV} -ttw ${RESOURCE_MANAGER_WAIT_TIME_IN_SECONDS} || exit 100 + ${LRMCLI} -delete -name ${SOA_CLOUD_NAMESPACE}.${AFTSWM_ACTION_ARTIFACT_NAME} -version ${AFTSWM_ACTION_NEW_VERSION} -routeoffer ${AFT_SERVICE_ENV} || exit 101 + +fi + +rm -rf ${INSTALL_ROOT}/${ROOT_DIR}/logs || { + echo "WARNING: Unable to purge logs directory during deinstall" +} + +exit 0 diff --git a/src/main/swm/common/install.env b/src/main/swm/common/install.env new file mode 100644 index 0000000..c98a29a --- /dev/null +++ b/src/main/swm/common/install.env @@ -0,0 +1,37 @@ + +# This file is used to set the environment which the install_*.sh files +# will use when executing. Only set variables that must be derived at +# installation time here. For variables that should be set by the installer +# in SWM, add VariableDescriptor elements to the descriptor.xml. Place +# logical steps in the install_preproc.sh or install_postproc.sh. + +. `dirname $0`/common.env + +# CHECK FOR ABSOLUTELY REQUIRED VARIABLES HERE +test -z "${SCLD_ENV}" && fail 1 "SCLD_ENV required" +test -z "${LATITUDE}" && fail 2 "LATITUDE required" +test -z "${LONGITUDE}" && fail 3 "LONGITUDE required" +test -z "${AFT_ENVIRONMENT}" && fail 4 "AFT_ENVIRONMENT required" + +#derive version components for lrm.xml +MAJOR_VERSION=`echo ${AFTSWM_ACTION_NEW_VERSION} | awk -F'.' '{print $1}'`; export MAJOR_VERSION +MINOR_VERSION=`echo ${AFTSWM_ACTION_NEW_VERSION} | awk -F'.' '{print $2}'`; export MINOR_VERSION +PATCH_VERSION=`echo ${AFTSWM_ACTION_NEW_VERSION} | awk -F'.' '{print $3}'`; export PATCH_VERSION + +# special handling for introscope... +if [ ! -z "${INTROSCOPE_LIB}" ]; then + if [ -z "${INTROSCOPE_AGENTPROFILE}" ]; then + fail 100 "INTROSCOPE_AGENTPROFILE must be set" + fi + + if [ -f ${INTROSCOPE_LIB}/Agent.jar ] && [ -f ${INTROSCOPE_AGENTPROFILE} ]; then + if [ -f ${TEMPLATE_RSRC_XML} ]; then + INTROSCOPE_VARS="-javaagent:${INTROSCOPE_LIB}/Agent.jar -noverify -Dcom.wily.introscope.agentProfile=${INTROSCOPE_AGENTPROFILE} -Dintroscope.agent.agentName=${AFTSWM_ACTION_ARTIFACT_NAME}" + export INTROSCOPE_VARS + fi + else + INTROSCOPE_VARS=""; export INTROSCOPE_VARS + fi +else + INTROSCOPE_VARS=""; export INTROSCOPE_VARS +fi diff --git a/src/main/swm/common/install_postproc.sh b/src/main/swm/common/install_postproc.sh new file mode 100644 index 0000000..cdad299 --- /dev/null +++ b/src/main/swm/common/install_postproc.sh @@ -0,0 +1,191 @@ +#!/bin/sh +#******************************************************************************* +# ============LICENSE_START======================================================= +# org.onap.dmaap +# ================================================================================ +# Copyright © 2017 AT&T Intellectual Property. All rights reserved. +# ================================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============LICENSE_END========================================================= +# +# ECOMP is a trademark and service mark of AT&T Intellectual Property. +# +#******************************************************************************* + +if [ -f "/tmp/exitdmaapMRpostproc" ]; then +echo "file /tmp/exitdmaapMRpostproc found .exiting..........." +exit 0; +fi +. `dirname $0`/install.env + +mkdir -p ${ROOT_DIR}/conf || fail 100 "Error on creating the conf directory." +mkdir -p ${ROOT_DIR}/docs || fail 100 "Error on creating the docs directory." +mkdir -p ${ROOT_DIR}/lib || fail 100 "Error on creating the lib directory." +mkdir -p ${ROOT_DIR}/log || fail 100 "Error on creating the logs directory." + +############################################################################## +# REMOVING THE DATA DIRECTORY +# The following if statement is checking to see if a new version is being installed +# on top of another version. If a new version is installed on top of the current +# version WITHOUT a proper deinstall, this will remove the data directory which +# is necessary to cleanup old AJSC route metadata. If CSTEM chooses to re-run +# the install_postproc.sh to update swm node variables, this if statement will NOT +# remove the data directory which is necessary for the SAME version to utilize the +# correct data directory route metadata. +############################################################################## +if [ "${AFTSWM_ACTION_NEW_VERSION}" != "${AFTSWM_ACTION_PREVIOUS_VERSION}" ] +then +rm -rf ${ROOT_DIR}/data +fi + +# Cleaning the jetty directory which contains the AJSC exploded war as well as +# any other apps running under jetty directory +rm -rf ${ROOT_DIR}/jetty + +# A simple override for the SOA Cloud platform value. Normally this is not +# needed outside of SOA Cloud development sandboxes +# this is used in the template.lrm.xml file during startup of the service +if [ ! -z "${SCLD_PLATFORM}" ]; then + SCLD_OPTIONAL_PLATFORM_FLAG="-Dplatform=${SCLD_PLATFORM}"; export SCLD_OPTIONAL_PLATFORM_FLAG +fi + +############################################################################## +# PROCESS TEMPLATE FILES FROM ENVIRONMENT +# pattern: looks for all files starting with "template.", processes them using the +# current environment, then renames them by removing the "template." in the same +# directory +############################################################################## +utilpath=`dirname $0`/utils +for tfile in `ls ${ROOT_DIR}/bundleconfig/etc/sysprops/template.* ${ROOT_DIR}/bundleconfig/etc/appprops/template.* ${ROOT_DIR}/bin/template.* ${ROOT_DIR}/etc/template.* 2>/dev/null`; do + dfile=`echo ${tfile} | sed -e 's@/template\.@/@g'` + sh ${utilpath}/findreplace.sh ${tfile} ${dfile} || exit 200 +done + +runningCount=`${LRMCLI} -running | grep -w ${SOA_CLOUD_NAMESPACE}.${AFTSWM_ACTION_ARTIFACT_NAME} | wc -l` || fail 300 "Unable to determine how many instances are running prior to notifying LRM of the upgrade" + +############################################################################## +# DEPLOY CONTAINER TO LRM +############################################################################## + +if [ -z "${RESOURCE_MANAGER_WAIT_TIME_IN_SECONDS}" ] +then + RESOURCE_MANAGER_WAIT_TIME_IN_SECONDS=180 + export RESOURCE_MANAGER_WAIT_TIME_IN_SECONDS +fi + + + +DTE_TME_STAMP=`date +%Y%m%d_%H%M%S` + +LRM_ADD_LOG=/tmp/${SOA_CLOUD_NAMESPACE}.${AFTSWM_ACTION_ARTIFACT_NAME}_ADD_${DTE_TME_STAMP}.out + echo "Adding resource to lrm" + echo "${LRMCLI} -add -file ${ROOT_DIR}/etc/lrm.xml -ttw ${RESOURCE_MANAGER_WAIT_TIME_IN_SECONDS}" + ${LRMCLI} -add -file ${ROOT_DIR}/etc/lrm.xml -ttw ${RESOURCE_MANAGER_WAIT_TIME_IN_SECONDS} > ${LRM_ADD_LOG} + LRM_ADD_RC=$? + echo "LRMCLI ADD RC : ${LRM_ADD_RC}" + if [ "${LRM_ADD_RC}" -ne "0" ]; then + + RSRC_EXIST=`cat ${LRM_ADD_LOG} | grep SCLD-LRM-1024` # resource is already added + if [ "${RSRC_EXIST:-}x" = "x" ]; then + echo "LRM add for Resource ${SOA_CLOUD_NAMESPACE}.${AFTSWM_ACTION_ARTIFACT_NAME} failed..." + cat ${LRM_ADD_LOG} + rm -f ${LRM_ADD_LOG} + exit 1 + fi + echo "LRM Resource for ${SOA_CLOUD_NAMESPACE}.${AFTSWM_ACTION_ARTIFACT_NAME} already exists. Proceeding with either addOrUpgrade or modify" + echo "Get the number of configured ${SOA_CLOUD_NAMESPACE}.${AFTSWM_ACTION_ARTIFACT_NAME} instance" + versionCtr=`${LRMCLI} -configured | grep ${SOA_CLOUD_NAMESPACE}.${AFTSWM_ACTION_ARTIFACT_NAME} | awk {'print $3'} | wc -l` + if [ ${versionCtr} -eq 1 ]; then + echo "Updating lrm resource" + echo "${LRMCLI} -addOrUpgrade -file ${ROOT_DIR}/etc/lrm.xml -ttw ${RESOURCE_MANAGER_WAIT_TIME_IN_SECONDS}" + ${LRMCLI} -addOrUpgrade -file ${ROOT_DIR}/etc/lrm.xml -ttw ${RESOURCE_MANAGER_WAIT_TIME_IN_SECONDS} || abort "lrmcli addOrUpgrade failed" + else + echo "Modifying lrm resource" + echo "${LRMCLI} -modify -file ${ROOT_DIR}/etc/lrm.xml" + ${LRMCLI} -modify -file ${ROOT_DIR}/etc/lrm.xml || abort "lrmcli modify failed" + fi + fi + + echo "LRMCLI execution on ${SOA_CLOUD_NAMESPACE}.${AFTSWM_ACTION_ARTIFACT_NAME} executed succesfully!" + +ls ${ROOT_DIR}/bundleconfig/etc/appprops/MsgRtrApi.properties + +if [ ! -z $CONFIG_ZK_SERVERS ]; then +sed -i '/config.zk.servers=/c\config.zk.servers='$CONFIG_ZK_SERVERS ${ROOT_DIR}/bundleconfig/etc/appprops/MsgRtrApi.properties +fi + +if [ ! -z $TRANSID_REQD ]; then +sed -i '/transidUEBtopicreqd=/c\transidUEBtopicreqd='$TRANSID_REQD ${ROOT_DIR}/bundleconfig/etc/appprops/MsgRtrApi.properties +fi + +if [ ! -z $MR_TOPICFACTOTRYCLASS ]; then +sed -i '/msgRtr.topicfactory.aaf=/c\msgRtr.topicfactory.aaf='$MR_TOPICFACTOTRYCLASS ${ROOT_DIR}/bundleconfig/etc/appprops/MsgRtrApi.properties +fi + +if [ ! -z $MR_NAMESPACE ]; then +sed -i '/msgRtr.namespace.aaf=/c\msgRtr.namespace.aaf='$MR_NAMESPACE ${ROOT_DIR}/bundleconfig/etc/appprops/MsgRtrApi.properties +fi + + +if [ ! -z $CADI_KEYFILE ]; then +sed -i '/cadi_keyfile=/c\cadi_keyfile='$CADI_KEYFILE ${ROOT_DIR}/etc/cadi.properties +fi + +if [ ! -z $AAF_URL ]; then +sed -i '/aaf_url=/c\aaf_url='$AAF_URL ${ROOT_DIR}/etc/cadi.properties +fi + +if [ ! -z $AAF_ID ]; then +sed -i '/aaf_id=/c\aaf_id='$AAF_ID ${ROOT_DIR}/etc/cadi.properties +fi + +if [ ! -z $AAF_PWD ]; then +sed -i '/aaf_password=/c\aaf_password='$AAF_PWD ${ROOT_DIR}/etc/cadi.properties +fi + +if [ ! -z $MR_LOGLOC ]; then +sed -i '/<property name="logDirectory" value=/c\<property name="logDirectory" value="'$MR_LOGLOC'"/>' ${ROOT_DIR}/bundleconfig/etc/logback.xml +fi + +if [ ! -z $MR_KSPATH ]; then +sed -i '/<Set name="KeyStorePath">/c\<Set name="KeyStorePath">'$MR_KSPATH'</Set>' ${ROOT_DIR}/etc/ajsc-jetty.xml +fi + +if [ ! -z $MR_KSPWD ]; then +sed -i '/<Set name="KeyStorePassword">/c\<Set name="KeyStorePassword">'$MR_KSPWD'</Set>' ${ROOT_DIR}/etc/ajsc-jetty.xml +fi + + +if [ ! -z $MR_KMPWD ]; then +sed -i '/<Set name="KeyManagerPassword">/c\<Set name="KeyManagerPassword">'$MR_KMPWD'</Set>' ${ROOT_DIR}/etc/ajsc-jetty.xml +fi + + + +if [ "${runningCount}" -eq 0 ]; then + if [ "${LRM_START_SVC}" = "true" ]; then + echo "${LRMCLI} -start -name ${SOA_CLOUD_NAMESPACE}.${AFTSWM_ACTION_ARTIFACT_NAME} -version ${AFTSWM_ACTION_NEW_VERSION} -routeoffer ${AFT_SERVICE_ENV} | egrep SUCCESS\|SCLD-LRM-1041" + ${LRMCLI} -start -name ${SOA_CLOUD_NAMESPACE}.${AFTSWM_ACTION_ARTIFACT_NAME} -version ${AFTSWM_ACTION_NEW_VERSION} -routeoffer ${AFT_SERVICE_ENV} | egrep SUCCESS\|SCLD-LRM-1041 + if [ $? -ne 0 ]; then + fail 500 "Start of ${SOA_CLOUD_NAMESPACE}.${AFTSWM_ACTION_ARTIFACT_NAME} with routeOffer ${AFT_SERVICE_ENV} failed" + fi + echo "${LRMCLI} -running | grep -w ${SOA_CLOUD_NAMESPACE}.${AFTSWM_ACTION_ARTIFACT_NAME}" + ${LRMCLI} -running | grep -w ${SOA_CLOUD_NAMESPACE}.${AFTSWM_ACTION_ARTIFACT_NAME} + else + echo "PROC_USER_MSG: LRM_START_SVC is set to false and no running instances were found prior to upgrading so ending install with no running service instances." + fi +else + ${LRMCLI} -running | grep -w ${SOA_CLOUD_NAMESPACE}.${AFTSWM_ACTION_ARTIFACT_NAME} +fi + + +exit 0 diff --git a/src/main/swm/common/install_preproc.sh b/src/main/swm/common/install_preproc.sh new file mode 100644 index 0000000..461c74d --- /dev/null +++ b/src/main/swm/common/install_preproc.sh @@ -0,0 +1,46 @@ +#!/bin/sh +#******************************************************************************* +# ============LICENSE_START======================================================= +# org.onap.dmaap +# ================================================================================ +# Copyright © 2017 AT&T Intellectual Property. All rights reserved. +# ================================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============LICENSE_END========================================================= +# +# ECOMP is a trademark and service mark of AT&T Intellectual Property. +# +#******************************************************************************* + +. `dirname $0`/deinstall.env + +LRMCLI=${INSTALL_ROOT}/opt/app/aft/scldlrm/bin/lrmcli +PATH=$PATH:`dirname $0`/utils; export PATH +if [ -d $LRMCLI ]; then +runningCount=`${LRMCLI} -running | grep -w ${SOA_CLOUD_NAMESPACE}.${AFTSWM_ACTION_PREVIOUS_VERSION} | wc -l` || fail 300 "Unable to determine how many instances are running prior to notifying LRM of the upgrade" + +if [ "${runningCount}" -eq 0 ]; then + +${LRMCLI} -delete -name ${SOA_CLOUD_NAMESPACE}.${AFTSWM_ACTION_ARTIFACT_NAME} -version ${AFTSWM_ACTION_PREVIOUS_VERSION} -routeoffer ${AFT_SERVICE_ENV} || exit 101 + + else + ${LRMCLI} -shutdown -name ${SOA_CLOUD_NAMESPACE}.${AFTSWM_ACTION_ARTIFACT_NAME} -version ${AFTSWM_ACTION_PREVIOUS_VERSION} -routeoffer ${AFT_SERVICE_ENV} -ttw ${RESOURCE_MANAGER_WAIT_TIME_IN_SECONDS} || exit 100 + ${LRMCLI} -delete -name ${SOA_CLOUD_NAMESPACE}.${AFTSWM_ACTION_ARTIFACT_NAME} -version ${AFTSWM_ACTION_PREVIOUS_VERSION} -routeoffer ${AFT_SERVICE_ENV} || exit 101 + +fi + +rm -rf ${INSTALL_ROOT}/${ROOT_DIR}/logs || { + echo "WARNING: Unable to purge logs directory during deinstall" +} +fi + +exit 0 diff --git a/src/main/swm/common/utils/findreplace.sh b/src/main/swm/common/utils/findreplace.sh new file mode 100644 index 0000000..efd8a77 --- /dev/null +++ b/src/main/swm/common/utils/findreplace.sh @@ -0,0 +1,94 @@ +#!/bin/sh +#******************************************************************************* +# ============LICENSE_START======================================================= +# org.onap.dmaap +# ================================================================================ +# Copyright © 2017 AT&T Intellectual Property. All rights reserved. +# ================================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============LICENSE_END========================================================= +# +# ECOMP is a trademark and service mark of AT&T Intellectual Property. +# +#******************************************************************************* +# Copyright 2011 AT&T Intellectual Properties +############################################################################## +# findreplace.sh <template> <destination> +# +# This script searches a provided file for templatized variable names +# in the format __varname__ and, if found in the current environment +# replaces those. Once complete, it will move the final copy of the file +# to <destination>. +# +############################################################################## +TEMPLATE=${1:?"Template file path required"} +DESTINATION=${2:?"Destination file path required"} + +if [ ! -f "${TEMPLATE}" ]; then + echo "ERROR: Specified template file does not exist: ${TEMPLATE}" + exit 100 +fi + +DIRECTORY=`dirname ${DESTINATION}` +if [ ! -d "${DIRECTORY}" ]; then + echo "ERROR: Destination directory does not exist: ${DIRECTORY}" + exit 200 +fi + +SED_SCR=/tmp/sed.$$ +echo "{" > ${SED_SCR} + +# create a sed script for replacing variables from current environment +for i in `env | awk -F= '{ print $1}'`; do + if [ "$i" = "IFS" ] ; then + continue; + fi + + VALUE=`eval echo '$'${i}` || { + echo 'WARNING: Unable to format '${i}' for sed replacement' + continue; + } + + for x in '@' '^' '&' '?' '#' '~' '%' '|' '+' '/'; do + echo ${VALUE} | grep "$x" 2>/dev/null 1>/dev/null + if [ $? != 0 ]; then + CCHAR="$x" + break + fi + done + + if [ -z "${CCHAR}" ]; then + echo "WARNING: Unable to find a suitable sed replacement character for ${VALUE}, will ignore setting ${KEY} in templates" + continue; + fi + + echo " s${CCHAR}__${i}__${CCHAR}${VALUE}${CCHAR}g" >> ${SED_SCR} +done + +sed -e 's/\\\@/\\\\@/g' ${SED_SCR} > ${SED_SCR}.1 || exit 300 + +if [ -f ${DESTINATION} ]; then + TIMESTAMP=`date +%Y%m%d%H%M%S` + o_dir=`dirname ${DESTINATION}` + o_file=`basename ${DESTINATION}` + mv ${DESTINATION} ${o_dir}/bu.${o_file}.${TIMESTAMP} +fi + +mv -f ${SED_SCR}.1 ${SED_SCR} || exit 400 + +echo "}" >> ${SED_SCR} || exit 500 + +sed -f ${SED_SCR} ${TEMPLATE} > ${DESTINATION} || exit 600 + +rm -f $SED_SCR + +exit 0
\ No newline at end of file |