Lusheng Ji | d6d409f | 2018-03-22 23:25:45 -0400 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # ================================================================================ |
| 3 | # Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. |
| 4 | # ================================================================================ |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | # ============LICENSE_END========================================================= |
| 17 | |
| 18 | set -e |
| 19 | |
| 20 | CDAP_HOST='localhost' |
| 21 | CDAP_PORT='11015' |
| 22 | TCA_NAMESPACE='cdap_tca_hi_lo' |
| 23 | TCA_APPNAME='dcae-tca' |
| 24 | |
| 25 | TCA_ARTIFACT='dcae-analytics-cdap-tca' |
Lusheng Ji | 3622f7a | 2018-04-04 21:51:34 -0400 | [diff] [blame^] | 26 | TCA_ARTIFACT_VERSION='2.2.0-SNAPSHOT' |
Lusheng Ji | d6d409f | 2018-03-22 23:25:45 -0400 | [diff] [blame] | 27 | TCA_FILE_PATH='/opt/tca' |
Lusheng Ji | 3622f7a | 2018-04-04 21:51:34 -0400 | [diff] [blame^] | 28 | TCA_JAR="${TCA_FILE_PATH}/${TCA_ARTIFACT}.${TCA_ARTIFACT_VERSION}.jar" |
Lusheng Ji | d6d409f | 2018-03-22 23:25:45 -0400 | [diff] [blame] | 29 | TCA_APP_CONF="${TCA_FILE_PATH}/tca_app_config.json" |
| 30 | TCA_CONF="${TCA_FILE_PATH}/tca_config.json" |
| 31 | TCA_PREF="${TCA_FILE_PATH}/tca_app_preferences.json" |
| 32 | TCA_CONF_TEMP='/tmp/tca_config.json' |
| 33 | TCA_APP_CONF_TEMP='/tmp/tca_app_config.json' |
| 34 | TCA_PREF_TEMP='/tmp/tca_preferences.json' |
| 35 | |
| 36 | TCA_PATH_APP="${CDAP_HOST}:${CDAP_PORT}/v3/namespaces/${TCA_NAMESPACE}/apps/${TCA_APPNAME}" |
| 37 | TCA_PATH_ARTIFACT="${CDAP_HOST}:${CDAP_PORT}/v3/namespaces/${TCA_NAMESPACE}/artifacts" |
| 38 | |
Lusheng Ji | d6d409f | 2018-03-22 23:25:45 -0400 | [diff] [blame] | 39 | |
Lusheng Ji | 3622f7a | 2018-04-04 21:51:34 -0400 | [diff] [blame^] | 40 | CONSUL_HOST=${CONSU_HOST:-consul} |
| 41 | CONSUL_PORT=${CONSU_PORT:-8500} |
| 42 | CONFIG_BINDING_SERVICE=${CONFIG_BINDING_SERVICE:-config_binding_service} |
| 43 | |
| 44 | CBS_SERVICE_NAME=${CONFIG_BINDING_SERVICE} |
| 45 | |
| 46 | CBS_HOST=$(curl -s "${CONSUL_HOST}:${CONSUL_PORT}/v1/catalog/service/${CBS_SERVICE_NAME}" |jq .[0].ServiceAddress |sed -e 's/\"//g') |
| 47 | CBS_PORT=$(curl -s "${CONSUL_HOST}:${CONSUL_PORT}/v1/catalog/service/${CBS_SERVICE_NAME}" |jq .[0].ServicePort |sed -e 's/\"//g') |
| 48 | CBS_HOST=${CBS_HOST:-config_binding_service} |
| 49 | CBS_PORT=${CBS_PORT:-10000} |
| 50 | |
| 51 | MY_NAME=${SERVICE_NAME:-tca} |
| 52 | |
| 53 | echo "TCA environment: I am ${MY_NAME}, consul at ${CONSUL_HOST}:${CONSUL_PORT}, CBS at ${CBS_HOST}:${CBS_PORT}, service name ${CBS_SERVICE_NAME}" |
| 54 | |
| 55 | |
| 56 | echo "Generting preference file" |
| 57 | sed -i 's/{{DMAAPHOST}}/'"${DMAAPHOST}"'/g' ${TCA_PREF} |
| 58 | sed -i 's/{{DMAAPPORT}}/'"${DMAAPPORT}"'/g' ${TCA_PREF} |
| 59 | sed -i 's/{{DMAAPPUBTOPIC}}/'"${DMAAPPUBTOPIC}"'/g' ${TCA_PREF} |
| 60 | sed -i 's/{{DMAAPSUBTOPIC}}/'"${DMAAPSUBTOPIC}"'/g' ${TCA_PREF} |
| 61 | sed -i 's/{{DMAAPSUBGROUP}}/OpenDCAEc12/g' ${TCA_PREF} |
| 62 | sed -i 's/{{DMAAPSUBID}}/c12/g' ${TCA_PREF} |
| 63 | sed -i 's/{{AAIHOST}}/'"${AAIHOST}"'/g' ${TCA_PREF} |
| 64 | sed -i 's/{{AAIPORT}}/'"${AAIPORT}"'/g' ${TCA_PREF} |
| 65 | if [ -z $REDISHOSTPORT ]; then |
| 66 | sed -i 's/{{REDISHOSTPORT}}/NONE/g' ${TCA_PREF} |
| 67 | sed -i 's/{{REDISCACHING}}/false/g' ${TCA_PREF} |
| 68 | else |
| 69 | sed -i 's/{{REDISHOSTPORT}}/'"${REDISHOSTPORT}"'/g' ${TCA_PREF} |
| 70 | sed -i 's/{{REDISCACHING}}/true/g' ${TCA_PREF} |
| 71 | fi |
Lusheng Ji | d6d409f | 2018-03-22 23:25:45 -0400 | [diff] [blame] | 72 | |
| 73 | function tca_stop { |
| 74 | # stop programs |
| 75 | echo |
| 76 | echo "Stopping TCADMaaPMRPublisherWorker, TCADMaaPMRSubscriberWorker, and TCAVESCollectorFlow ..." |
| 77 | echo |
| 78 | curl -s -X POST "http://${TCA_PATH_APP}/workers/TCADMaaPMRPublisherWorker/stop" |
| 79 | curl -s -X POST "http://${TCA_PATH_APP}/workers/TCADMaaPMRSubscriberWorker/stop" |
| 80 | curl -s -X POST "http://${TCA_PATH_APP}/flows/TCAVESCollectorFlow/stop" |
| 81 | echo "done" |
| 82 | echo |
| 83 | } |
| 84 | |
| 85 | function tca_load_artifact { |
| 86 | echo |
Lusheng Ji | 3622f7a | 2018-04-04 21:51:34 -0400 | [diff] [blame^] | 87 | echo "Loading artifact ${TCA_JAR} to http://${TCA_PATH_ARTIFACT}/${TCA_ARTIFACT}..." |
Lusheng Ji | d6d409f | 2018-03-22 23:25:45 -0400 | [diff] [blame] | 88 | curl -s -X POST --data-binary @"${TCA_JAR}" "http://${TCA_PATH_ARTIFACT}/${TCA_ARTIFACT}" |
| 89 | echo |
| 90 | } |
| 91 | |
| 92 | function tca_load_conf { |
| 93 | echo |
Lusheng Ji | 3622f7a | 2018-04-04 21:51:34 -0400 | [diff] [blame^] | 94 | echo "Loading configuration ${TCA_APP_CONF} to http://${TCA_PATH_APP}" |
Lusheng Ji | d6d409f | 2018-03-22 23:25:45 -0400 | [diff] [blame] | 95 | curl -s -X PUT -d @${TCA_APP_CONF} http://${TCA_PATH_APP} |
| 96 | echo |
| 97 | |
| 98 | # load preferences |
| 99 | echo |
Lusheng Ji | 3622f7a | 2018-04-04 21:51:34 -0400 | [diff] [blame^] | 100 | echo "Loading preferences ${TCA_PREF} to http://${TCA_PATH_APP}/preferences" |
Lusheng Ji | d6d409f | 2018-03-22 23:25:45 -0400 | [diff] [blame] | 101 | curl -s -X PUT -d @${TCA_PREF} http://${TCA_PATH_APP}/preferences |
| 102 | echo |
| 103 | } |
| 104 | |
| 105 | |
| 106 | function tca_delete { |
| 107 | echo |
Lusheng Ji | 3622f7a | 2018-04-04 21:51:34 -0400 | [diff] [blame^] | 108 | echo "Deleting application dcae-tca http://${TCA_PATH_APP}" |
Lusheng Ji | d6d409f | 2018-03-22 23:25:45 -0400 | [diff] [blame] | 109 | curl -s -X DELETE http://${TCA_PATH_APP} |
| 110 | echo |
| 111 | |
| 112 | # delete artifact |
| 113 | echo |
Lusheng Ji | 3622f7a | 2018-04-04 21:51:34 -0400 | [diff] [blame^] | 114 | echo "Deleting artifact http://${TCA_PATH_ARTIFACT}/${TCA_ARTIFACT}/versions/${TCA_ARTIFACT_VERSION} ..." |
Lusheng Ji | d6d409f | 2018-03-22 23:25:45 -0400 | [diff] [blame] | 115 | curl -s -X DELETE "http://${TCA_PATH_ARTIFACT}/${TCA_ARTIFACT}/versions/${TCA_ARTIFACT_VERSION}" |
| 116 | echo |
| 117 | } |
| 118 | |
| 119 | function tca_start { |
| 120 | echo |
| 121 | echo "Starting TCADMaaPMRPublisherWorker, TCADMaaPMRSubscriberWorker, and TCAVESCollectorFlow ..." |
| 122 | curl -s -X POST "http://${TCA_PATH_APP}/workers/TCADMaaPMRPublisherWorker/start" |
| 123 | curl -s -X POST "http://${TCA_PATH_APP}/workers/TCADMaaPMRSubscriberWorker/start" |
| 124 | curl -s -X POST "http://${TCA_PATH_APP}/flows/TCAVESCollectorFlow/start" |
| 125 | echo |
| 126 | } |
| 127 | |
| 128 | |
| 129 | function tca_status { |
| 130 | echo |
| 131 | echo "TCADMaaPMRPublisherWorker status: " |
| 132 | curl -s "http://${TCA_PATH_APP}/workers/TCADMaaPMRPublisherWorker/status" |
| 133 | echo |
| 134 | echo "TCADMaaPMRSubscriberWorker status: " |
| 135 | curl -s "http://${TCA_PATH_APP}/workers/TCADMaaPMRSubscriberWorker/status" |
| 136 | echo |
| 137 | echo "TCAVESCollectorFlow status" |
| 138 | curl -s "http://${TCA_PATH_APP}/flows/TCAVESCollectorFlow/status" |
| 139 | echo; echo |
| 140 | } |
| 141 | |
| 142 | |
| 143 | function tca_poll_policy { |
Lusheng Ji | d6d409f | 2018-03-22 23:25:45 -0400 | [diff] [blame] | 144 | MY_NAME=${SERVICE_NAME:-tca} |
Lusheng Ji | d6d409f | 2018-03-22 23:25:45 -0400 | [diff] [blame] | 145 | |
| 146 | URL1="${CBS_HOST}:${CBS_PORT}/service_component/${MY_NAME}" |
| 147 | URL2="$URL1:preferences" |
| 148 | |
Lusheng Ji | 3622f7a | 2018-04-04 21:51:34 -0400 | [diff] [blame^] | 149 | echo "tca_poll_policy: Retrieving configuration file at ${URL1}" |
Lusheng Ji | d6d409f | 2018-03-22 23:25:45 -0400 | [diff] [blame] | 150 | curl -s "$URL1" | jq . --sort-keys > "${TCA_CONF_TEMP}" |
| 151 | echo "Retrieving preferences file at ${URL1}" |
| 152 | curl -s "$URL2" | jq . --sort-keys > "${TCA_PREF_TEMP}" |
| 153 | |
| 154 | if [ ! -e "${TCA_CONF_TEMP}" ] || [ "$(ls -sh ${TCA_CONF_TEMP} |cut -f1 -d' ' |sed -e 's/[^0-9]//g')" -lt "1" ]; then |
| 155 | echo "Fail to receive configuration" |
| 156 | return |
| 157 | fi |
| 158 | if [ ! -e "${TCA_PREF_TEMP}" ] || [ "$(ls -sh ${TCA_PREF_TEMP} |cut -f1 -d' ' |sed -e 's/[^0-9]//g')" -lt "1" ]; then |
| 159 | echo "Fail to receive preferences" |
| 160 | return |
| 161 | fi |
| 162 | |
| 163 | CONF_CHANGED="" |
| 164 | # extract only the config section from APP CONF (which has both artifact and config sections) |
| 165 | jq .config --sort-keys ${TCA_APP_CONF} > ${TCA_CONF} |
| 166 | if ! diff ${TCA_CONF} ${TCA_CONF_TEMP} ; then |
| 167 | echo "TCA config changed" |
| 168 | # generating the new app conf using current app conf's artifact section and the new downloaded config |
| 169 | jq --argfile CONFVALUE ${TCA_CONF_TEMP} '.config = $CONFVALUE' <${TCA_APP_CONF} > ${TCA_APP_CONF_TEMP} |
| 170 | |
| 171 | mv ${TCA_APP_CONF_TEMP} ${TCA_APP_CONF} |
| 172 | CONF_CHANGED=1 |
| 173 | fi |
| 174 | |
| 175 | PERF_CHANGED="" |
| 176 | # update the subscriber ConsumerID, if not already unique, |
| 177 | # so replicas appear as different consumers in the consumer group |
| 178 | HOSTID=$(head -1 /etc/hostname | rev |cut -f1-2 -d'-' |rev) |
| 179 | CONSUMERID=$(jq .subscriberConsumerId ${TCA_PREF_TEMP} |sed -e 's/\"//g') |
| 180 | if ! (echo "$CONSUMERID" |grep "$HOSTID"); then |
| 181 | CONSUMERID="${CONSUMERID}-${HOSTID}" |
| 182 | jq --arg CID ${CONSUMERID} '.subscriberConsumerId = $CID' < "${TCA_PREF_TEMP}" > "${TCA_PREF_TEMP}2" |
| 183 | mv "${TCA_PREF_TEMP}2" "${TCA_PREF_TEMP}" |
| 184 | fi |
| 185 | if ! diff ${TCA_PREF} ${TCA_PREF_TEMP} ; then |
| 186 | echo "TCA preference updated" |
| 187 | mv ${TCA_PREF_TEMP} ${TCA_PREF} |
| 188 | PERF_CHANGED=1 |
| 189 | fi |
| 190 | |
| 191 | if [[ "$PERF_CHANGED" == "1" || "$CONF_CHANGED" == "1" ]]; then |
| 192 | tca_stop |
| 193 | tca_delete |
| 194 | tca_load_artifact |
| 195 | tca_load_conf |
| 196 | tca_start |
| 197 | tca_status |
| 198 | fi |
| 199 | } |
| 200 | |
| 201 | |
| 202 | export PATH=${PATH}:/opt/cdap/sdk/bin |
| 203 | |
| 204 | # starting CDAP SDK in background |
| 205 | cdap sdk start |
| 206 | |
| 207 | |
| 208 | |
| 209 | echo "Waiting CDAP ready on port 11015 ..." |
| 210 | while ! nc -z ${CDAP_HOST} ${CDAP_PORT}; do |
| 211 | sleep 0.1 # wait for 1/10 of the second before check again |
| 212 | done |
| 213 | echo "CDAP has started" |
| 214 | |
| 215 | |
| 216 | echo "Creating namespace cdap_tca_hi_lo ..." |
| 217 | curl -s -X PUT "http://${CDAP_HOST}:${CDAP_PORT}/v3/namespaces/cdap_tca_hi_lo" |
| 218 | |
| 219 | |
| 220 | # stop programs |
| 221 | tca_stop |
| 222 | |
| 223 | |
| 224 | # delete application |
| 225 | tca_delete |
| 226 | |
| 227 | |
| 228 | # load artifact |
| 229 | tca_load_artifact |
| 230 | tca_load_conf |
| 231 | |
| 232 | |
| 233 | # start programs |
| 234 | tca_start |
| 235 | |
| 236 | |
| 237 | # get status of programs |
| 238 | tca_status |
| 239 | |
| 240 | |
| 241 | |
| 242 | while echo -n |
| 243 | do |
| 244 | echo "======================================================" |
| 245 | date |
| 246 | tca_poll_policy |
| 247 | sleep 30 |
| 248 | done |