Lusheng Ji | d6d409f | 2018-03-22 23:25:45 -0400 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # ================================================================================ |
Vijay Venkatesh Kumar | 290fe20 | 2019-04-24 12:49:48 +0000 | [diff] [blame] | 3 | # Copyright (c) 2018-2019 AT&T Intellectual Property. All rights reserved. |
Lusheng Ji | d6d409f | 2018-03-22 23:25:45 -0400 | [diff] [blame] | 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 | d6d409f | 2018-03-22 23:25:45 -0400 | [diff] [blame] | 26 | TCA_FILE_PATH='/opt/tca' |
Lusheng Ji | 1dd7a99 | 2018-05-10 23:54:18 -0400 | [diff] [blame] | 27 | TCA_JAR="$(ls -1r ${TCA_FILE_PATH}/${TCA_ARTIFACT}*.jar | head -1)" |
| 28 | TCA_ARTIFACT_VERSION=$(echo "$TCA_JAR" |rev |cut -f 2-4 -d '.' |rev) |
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 | e380f6b | 2018-05-14 22:45:56 -0400 | [diff] [blame] | 39 | MR_WATCHDOG_PATH="${TCA_FILE_PATH}/mr-watchdog.sh" |
| 40 | |
| 41 | |
| 42 | WORKER_COUNT='0' |
Lusheng Ji | d6d409f | 2018-03-22 23:25:45 -0400 | [diff] [blame] | 43 | |
Lusheng Ji | 1dd7a99 | 2018-05-10 23:54:18 -0400 | [diff] [blame] | 44 | CONSUL_HOST=${CONSUL_HOST:-consul} |
| 45 | CONSUL_PORT=${CONSUL_PORT:-8500} |
Lusheng Ji | 3622f7a | 2018-04-04 21:51:34 -0400 | [diff] [blame] | 46 | CONFIG_BINDING_SERVICE=${CONFIG_BINDING_SERVICE:-config_binding_service} |
| 47 | |
| 48 | CBS_SERVICE_NAME=${CONFIG_BINDING_SERVICE} |
| 49 | |
vagrant | 61c202d | 2018-04-05 20:34:40 +0000 | [diff] [blame] | 50 | #Changing to HOSTNAME parameter for consistency with k8s deploy |
| 51 | MY_NAME=${HOSTNAME:-tca} |
Lusheng Ji | 3622f7a | 2018-04-04 21:51:34 -0400 | [diff] [blame] | 52 | |
Lusheng Ji | 3622f7a | 2018-04-04 21:51:34 -0400 | [diff] [blame] | 53 | |
| 54 | echo "Generting preference file" |
Lusheng Ji | e380f6b | 2018-05-14 22:45:56 -0400 | [diff] [blame] | 55 | DMAAPSUBGROUP=${DMAAPSUBGROUP:-OpenDCAEc12} |
| 56 | DMAAPSUBID=${DMAAPSUBID:=c12} |
Lusheng Ji | 3622f7a | 2018-04-04 21:51:34 -0400 | [diff] [blame] | 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} |
Lusheng Ji | e380f6b | 2018-05-14 22:45:56 -0400 | [diff] [blame] | 61 | sed -i 's/{{DMAAPSUBGROUP}}/'"${DMAAPSUBGROUP}"'/g' ${TCA_PREF} |
| 62 | sed -i 's/{{DMAAPSUBID}}/'"${DMAAPSUBID}"'/g' ${TCA_PREF} |
Lusheng Ji | 3622f7a | 2018-04-04 21:51:34 -0400 | [diff] [blame] | 63 | sed -i 's/{{AAIHOST}}/'"${AAIHOST}"'/g' ${TCA_PREF} |
| 64 | sed -i 's/{{AAIPORT}}/'"${AAIPORT}"'/g' ${TCA_PREF} |
Lusheng Ji | 1dd7a99 | 2018-05-10 23:54:18 -0400 | [diff] [blame] | 65 | if [ -z "$REDISHOSTPORT" ]; then |
Lusheng Ji | 3622f7a | 2018-04-04 21:51:34 -0400 | [diff] [blame] | 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 { |
Lusheng Ji | e380f6b | 2018-05-14 22:45:56 -0400 | [diff] [blame] | 130 | WORKER_COUNT='0' |
Lusheng Ji | d6d409f | 2018-03-22 23:25:45 -0400 | [diff] [blame] | 131 | echo |
Lusheng Ji | e380f6b | 2018-05-14 22:45:56 -0400 | [diff] [blame] | 132 | STATUS=$(curl -s "http://${TCA_PATH_APP}/workers/TCADMaaPMRPublisherWorker/status") |
| 133 | echo "TCADMaaPMRPublisherWorker status: $STATUS" |
| 134 | INC=$(echo "$STATUS" | jq . |grep RUNNING |wc -l) |
| 135 | WORKER_COUNT=$((WORKER_COUNT+INC)) |
| 136 | |
| 137 | STATUS=$(curl -s "http://${TCA_PATH_APP}/workers/TCADMaaPMRSubscriberWorker/status") |
| 138 | echo "TCADMaaPMRSubscriberWorker status: $STATUS" |
| 139 | INC=$(echo "$STATUS" | jq . |grep RUNNING |wc -l) |
| 140 | WORKER_COUNT=$((WORKER_COUNT+INC)) |
| 141 | |
| 142 | STATUS=$(curl -s "http://${TCA_PATH_APP}/flows/TCAVESCollectorFlow/status") |
| 143 | echo "TCAVESCollectorFlow status: $STATUS" |
| 144 | INC=$(echo "$STATUS" | jq . |grep RUNNING |wc -l) |
| 145 | WORKER_COUNT=$((WORKER_COUNT+INC)) |
Lusheng Ji | d6d409f | 2018-03-22 23:25:45 -0400 | [diff] [blame] | 146 | echo |
Lusheng Ji | d6d409f | 2018-03-22 23:25:45 -0400 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | |
Lusheng Ji | e380f6b | 2018-05-14 22:45:56 -0400 | [diff] [blame] | 150 | function tca_restart { |
| 151 | MR_HOST=$(jq .subscriberHostName ${TCA_PREF} |sed -e 's/\"//g') |
| 152 | MR_PORT=$(jq .subscriberHostPort ${TCA_PREF} |sed -e 's/\"//g') |
| 153 | MR_TOPIC=$(jq .subscriberTopicName ${TCA_PREF} |sed -e 's/\"//g') |
| 154 | echo "Verifying DMaaP topic: ${MR_TOPIC}@${MR_HOST}:${MR_PORT} (will block until topic ready)" |
| 155 | "${MR_WATCHDOG_PATH}" "${MR_HOST}" "${MR_PORT}" "${MR_TOPIC}" |
| 156 | tca_stop |
| 157 | tca_delete |
| 158 | tca_load_artifact |
| 159 | tca_load_conf |
| 160 | tca_start |
| 161 | sleep 5 |
| 162 | tca_status |
| 163 | } |
| 164 | |
Lusheng Ji | d6d409f | 2018-03-22 23:25:45 -0400 | [diff] [blame] | 165 | function tca_poll_policy { |
Lusheng Ji | e134c82 | 2018-04-23 23:16:32 -0400 | [diff] [blame] | 166 | URL0="${CBS_HOST}:${CBS_PORT}/service_component_all/${MY_NAME}" |
Lusheng Ji | 1dd7a99 | 2018-05-10 23:54:18 -0400 | [diff] [blame] | 167 | echo "tca_poll_policy: Retrieving all-in-one config at ${URL0}" |
Lusheng Ji | e134c82 | 2018-04-23 23:16:32 -0400 | [diff] [blame] | 168 | HTTP_RESPONSE=$(curl --silent --write-out "HTTPSTATUS:%{http_code}" "$URL0") |
Lusheng Ji | 1dd7a99 | 2018-05-10 23:54:18 -0400 | [diff] [blame] | 169 | HTTP_BODY=$(echo "$HTTP_RESPONSE" | sed -e 's/HTTPSTATUS\:.*//g') |
| 170 | HTTP_STATUS=$(echo "$HTTP_RESPONSE" | tr -d '\n' | sed -e 's/.*HTTPSTATUS://') |
Lusheng Ji | ac864ce | 2018-04-05 12:02:52 -0400 | [diff] [blame] | 171 | |
Lusheng Ji | ac864ce | 2018-04-05 12:02:52 -0400 | [diff] [blame] | 172 | if [ "$HTTP_STATUS" != "200" ]; then |
Lusheng Ji | 1dd7a99 | 2018-05-10 23:54:18 -0400 | [diff] [blame] | 173 | echo "tca_poll_policy: Retrieving all-in-one config failed with status $HTTP_STATUS" |
Lusheng Ji | e134c82 | 2018-04-23 23:16:32 -0400 | [diff] [blame] | 174 | URL1="${CBS_HOST}:${CBS_PORT}/service_component/${MY_NAME}" |
Lusheng Ji | 1dd7a99 | 2018-05-10 23:54:18 -0400 | [diff] [blame] | 175 | echo "tca_poll_policy: Retrieving app config only at ${URL1}" |
Lusheng Ji | e134c82 | 2018-04-23 23:16:32 -0400 | [diff] [blame] | 176 | HTTP_RESPONSE1=$(curl --silent --write-out "HTTPSTATUS:%{http_code}" "$URL1") |
Lusheng Ji | 1dd7a99 | 2018-05-10 23:54:18 -0400 | [diff] [blame] | 177 | HTTP_BODY1=$(echo "$HTTP_RESPONSE1" | sed -e 's/HTTPSTATUS\:.*//g') |
| 178 | HTTP_STATUS1=$(echo "$HTTP_RESPONSE1" | tr -d '\n' | sed -e 's/.*HTTPSTATUS://') |
Lusheng Ji | e134c82 | 2018-04-23 23:16:32 -0400 | [diff] [blame] | 179 | if [ "$HTTP_STATUS1" != "200" ]; then |
Lusheng Ji | 1dd7a99 | 2018-05-10 23:54:18 -0400 | [diff] [blame] | 180 | echo "tca_poll_policy: Retrieving app config only failed with status $HTTP_STATUS1" |
Lusheng Ji | e134c82 | 2018-04-23 23:16:32 -0400 | [diff] [blame] | 181 | return |
| 182 | fi |
| 183 | |
| 184 | URL2="$URL1:preferences" |
Lusheng Ji | 1dd7a99 | 2018-05-10 23:54:18 -0400 | [diff] [blame] | 185 | echo "tca_poll_policy: Retrieving app preferences only at ${URL2}" |
Lusheng Ji | e134c82 | 2018-04-23 23:16:32 -0400 | [diff] [blame] | 186 | HTTP_RESPONSE2=$(curl --silent --write-out "HTTPSTATUS:%{http_code}" "$URL2") |
Lusheng Ji | 1dd7a99 | 2018-05-10 23:54:18 -0400 | [diff] [blame] | 187 | HTTP_BODY2=$(echo "$HTTP_RESPONSE2" | sed -e 's/HTTPSTATUS\:.*//g') |
| 188 | HTTP_STATUS2=$(echo "$HTTP_RESPONSE2" | tr -d '\n' | sed -e 's/.*HTTPSTATUS://') |
Lusheng Ji | e134c82 | 2018-04-23 23:16:32 -0400 | [diff] [blame] | 189 | if [ "$HTTP_STATUS2" != "200" ]; then |
Lusheng Ji | 1dd7a99 | 2018-05-10 23:54:18 -0400 | [diff] [blame] | 190 | echo "tca_poll_policy: Retrieving app preferences only failed with status $HTTP_STATUS2" |
| 191 | return |
| 192 | fi |
| 193 | |
| 194 | if [[ "$CONFIG" == "null" || "$PREF" == "null" ]]; then |
| 195 | echo "tca_poll_policy: either app config or app preferences being empty, config not applicable" |
Lusheng Ji | e134c82 | 2018-04-23 23:16:32 -0400 | [diff] [blame] | 196 | return |
| 197 | fi |
| 198 | |
Lusheng Ji | 1dd7a99 | 2018-05-10 23:54:18 -0400 | [diff] [blame] | 199 | echo "$HTTP_BODY1" | jq . --sort-keys > "${TCA_CONF_TEMP}" |
| 200 | echo "$HTTP_BODY2" | jq . --sort-keys > "${TCA_PREF_TEMP}" |
Lusheng Ji | e134c82 | 2018-04-23 23:16:32 -0400 | [diff] [blame] | 201 | else |
Lusheng Ji | 1dd7a99 | 2018-05-10 23:54:18 -0400 | [diff] [blame] | 202 | CONFIG=$(echo "$HTTP_BODY" | jq .config.app_config) |
| 203 | PREF=$(echo "$HTTP_BODY" | jq .config.app_preferences) |
Vijay Venkatesh Kumar | 290fe20 | 2019-04-24 12:49:48 +0000 | [diff] [blame] | 204 | POLICY=$(echo "$HTTP_BODY" | jq .policies.items[0].config.tca_policy) |
vagrant | b51fd79 | 2018-05-01 08:21:11 +0000 | [diff] [blame] | 205 | |
vagrant | b51fd79 | 2018-05-01 08:21:11 +0000 | [diff] [blame] | 206 | |
Lusheng Ji | 1dd7a99 | 2018-05-10 23:54:18 -0400 | [diff] [blame] | 207 | if [[ "$CONFIG" == "null" || "$PREF" == "null" ]]; then |
| 208 | echo "tca_poll_policy: CONFIG received is parsed to be empty, trying to parse using R1 format" |
| 209 | CONFIG=$(echo "$HTTP_BODY" | jq .config) |
| 210 | NEWPREF=$(echo "$HTTP_BODY" | jq .preferences) |
| 211 | |
| 212 | #echo "CONFIG is [$CONFIG]" |
| 213 | #echo "NEWPREF is [$NEWPREF]" |
vagrant | b51fd79 | 2018-05-01 08:21:11 +0000 | [diff] [blame] | 214 | else |
Lusheng Ji | 1dd7a99 | 2018-05-10 23:54:18 -0400 | [diff] [blame] | 215 | echo "tca_poll_policy: CONFIG is [${CONFIG}], PREF is [${PREF}], POLICY is [${POLICY}]" |
| 216 | ## Check if policy content under tca_policy is returned null |
| 217 | ## null indicates no active policy flow; hence use configuration loaded |
| 218 | ## from blueprint |
| 219 | if [ "$POLICY" == "null" ]; then |
| 220 | # tca_policy through blueprint |
| 221 | NEWPREF=${PREF} |
| 222 | else |
| 223 | # tca_policy through active policy flow through PH |
| 224 | NEWPREF=$(echo "$PREF" | jq --arg tca_policy "$POLICY" '. + {$tca_policy}') |
| 225 | fi |
| 226 | NEWPREF=$(echo "$NEWPREF" | sed 's/\\n//g') |
vagrant | b51fd79 | 2018-05-01 08:21:11 +0000 | [diff] [blame] | 227 | fi |
Lusheng Ji | 1dd7a99 | 2018-05-10 23:54:18 -0400 | [diff] [blame] | 228 | |
| 229 | if [[ "$CONFIG" == "null" || "$NEWPREF" == "null" ]]; then |
| 230 | echo "tca_poll_policy: either app config or app preferences being empty, config not applicable" |
| 231 | return |
| 232 | fi |
| 233 | |
| 234 | echo "$CONFIG" | jq . --sort-keys > "${TCA_CONF_TEMP}" |
| 235 | echo "$NEWPREF" | jq . --sort-keys > "${TCA_PREF_TEMP}" |
Lusheng Ji | ac864ce | 2018-04-05 12:02:52 -0400 | [diff] [blame] | 236 | fi |
Lusheng Ji | d6d409f | 2018-03-22 23:25:45 -0400 | [diff] [blame] | 237 | |
| 238 | if [ ! -e "${TCA_CONF_TEMP}" ] || [ "$(ls -sh ${TCA_CONF_TEMP} |cut -f1 -d' ' |sed -e 's/[^0-9]//g')" -lt "1" ]; then |
| 239 | echo "Fail to receive configuration" |
| 240 | return |
| 241 | fi |
| 242 | if [ ! -e "${TCA_PREF_TEMP}" ] || [ "$(ls -sh ${TCA_PREF_TEMP} |cut -f1 -d' ' |sed -e 's/[^0-9]//g')" -lt "1" ]; then |
| 243 | echo "Fail to receive preferences" |
| 244 | return |
| 245 | fi |
| 246 | |
| 247 | CONF_CHANGED="" |
| 248 | # extract only the config section from APP CONF (which has both artifact and config sections) |
| 249 | jq .config --sort-keys ${TCA_APP_CONF} > ${TCA_CONF} |
| 250 | if ! diff ${TCA_CONF} ${TCA_CONF_TEMP} ; then |
| 251 | echo "TCA config changed" |
| 252 | # generating the new app conf using current app conf's artifact section and the new downloaded config |
| 253 | jq --argfile CONFVALUE ${TCA_CONF_TEMP} '.config = $CONFVALUE' <${TCA_APP_CONF} > ${TCA_APP_CONF_TEMP} |
| 254 | |
| 255 | mv ${TCA_APP_CONF_TEMP} ${TCA_APP_CONF} |
| 256 | CONF_CHANGED=1 |
| 257 | fi |
| 258 | |
| 259 | PERF_CHANGED="" |
| 260 | # update the subscriber ConsumerID, if not already unique, |
| 261 | # so replicas appear as different consumers in the consumer group |
| 262 | HOSTID=$(head -1 /etc/hostname | rev |cut -f1-2 -d'-' |rev) |
| 263 | CONSUMERID=$(jq .subscriberConsumerId ${TCA_PREF_TEMP} |sed -e 's/\"//g') |
| 264 | if ! (echo "$CONSUMERID" |grep "$HOSTID"); then |
| 265 | CONSUMERID="${CONSUMERID}-${HOSTID}" |
Lusheng Ji | 1dd7a99 | 2018-05-10 23:54:18 -0400 | [diff] [blame] | 266 | jq --arg CID "${CONSUMERID}" '.subscriberConsumerId = $CID' < "${TCA_PREF_TEMP}" > "${TCA_PREF_TEMP}2" |
Lusheng Ji | d6d409f | 2018-03-22 23:25:45 -0400 | [diff] [blame] | 267 | mv "${TCA_PREF_TEMP}2" "${TCA_PREF_TEMP}" |
| 268 | fi |
| 269 | if ! diff ${TCA_PREF} ${TCA_PREF_TEMP} ; then |
| 270 | echo "TCA preference updated" |
| 271 | mv ${TCA_PREF_TEMP} ${TCA_PREF} |
| 272 | PERF_CHANGED=1 |
| 273 | fi |
| 274 | |
Lusheng Ji | 1dd7a99 | 2018-05-10 23:54:18 -0400 | [diff] [blame] | 275 | if [[ "$PERF_CHANGED" == "1" || "$CONF_CHANGED" == "1" ]]; then |
| 276 | echo "Newly received configuration/preference differ from the running instance's. reload confg" |
Lusheng Ji | e380f6b | 2018-05-14 22:45:56 -0400 | [diff] [blame] | 277 | tca_restart |
| 278 | else |
| 279 | echo "Newly received configuration/preference identical from the running instance's" |
Lusheng Ji | d6d409f | 2018-03-22 23:25:45 -0400 | [diff] [blame] | 280 | fi |
| 281 | } |
| 282 | |
| 283 | |
| 284 | export PATH=${PATH}:/opt/cdap/sdk/bin |
| 285 | |
Lusheng Ji | 1dd7a99 | 2018-05-10 23:54:18 -0400 | [diff] [blame] | 286 | |
| 287 | echo "Starting TCA-CDAP in standalone mode" |
| 288 | |
Lusheng Ji | d6d409f | 2018-03-22 23:25:45 -0400 | [diff] [blame] | 289 | # starting CDAP SDK in background |
| 290 | cdap sdk start |
| 291 | |
Lusheng Ji | e380f6b | 2018-05-14 22:45:56 -0400 | [diff] [blame] | 292 | echo "CDAP Started, waiting CDAP ready on ${CDAP_HOST}:${CDAP_PORT} ..." |
Lusheng Ji | d6d409f | 2018-03-22 23:25:45 -0400 | [diff] [blame] | 293 | while ! nc -z ${CDAP_HOST} ${CDAP_PORT}; do |
Lusheng Ji | e380f6b | 2018-05-14 22:45:56 -0400 | [diff] [blame] | 294 | sleep 1 # wait for 1 second before check again |
Lusheng Ji | d6d409f | 2018-03-22 23:25:45 -0400 | [diff] [blame] | 295 | done |
Lusheng Ji | d6d409f | 2018-03-22 23:25:45 -0400 | [diff] [blame] | 296 | |
| 297 | echo "Creating namespace cdap_tca_hi_lo ..." |
| 298 | curl -s -X PUT "http://${CDAP_HOST}:${CDAP_PORT}/v3/namespaces/cdap_tca_hi_lo" |
| 299 | |
Lusheng Ji | d6d409f | 2018-03-22 23:25:45 -0400 | [diff] [blame] | 300 | # stop programs |
| 301 | tca_stop |
Lusheng Ji | d6d409f | 2018-03-22 23:25:45 -0400 | [diff] [blame] | 302 | # delete application |
| 303 | tca_delete |
Lusheng Ji | d6d409f | 2018-03-22 23:25:45 -0400 | [diff] [blame] | 304 | # load artifact |
| 305 | tca_load_artifact |
| 306 | tca_load_conf |
Lusheng Ji | d6d409f | 2018-03-22 23:25:45 -0400 | [diff] [blame] | 307 | # start programs |
| 308 | tca_start |
| 309 | |
Lusheng Ji | d6d409f | 2018-03-22 23:25:45 -0400 | [diff] [blame] | 310 | # get status of programs |
| 311 | tca_status |
| 312 | |
Lusheng Ji | e380f6b | 2018-05-14 22:45:56 -0400 | [diff] [blame] | 313 | echo "TCA-CDAP standalone mode initialization completed, with $WORKER_COUNT / 3 up" |
Lusheng Ji | d6d409f | 2018-03-22 23:25:45 -0400 | [diff] [blame] | 314 | |
| 315 | |
Lusheng Ji | 1dd7a99 | 2018-05-10 23:54:18 -0400 | [diff] [blame] | 316 | |
| 317 | #Changing to HOSTNAME parameter for consistency with k8s deploy |
| 318 | MY_NAME=${HOSTNAME:-tca} |
| 319 | |
| 320 | unset CBS_HOST |
| 321 | unset CBS_PORT |
| 322 | echo "TCA environment: I am ${MY_NAME}, consul at ${CONSUL_HOST}:${CONSUL_PORT}, CBS service name ${CBS_SERVICE_NAME}" |
| 323 | |
| 324 | while echo |
Lusheng Ji | d6d409f | 2018-03-22 23:25:45 -0400 | [diff] [blame] | 325 | do |
Lusheng Ji | e380f6b | 2018-05-14 22:45:56 -0400 | [diff] [blame] | 326 | echo "======================================================> $(date)" |
| 327 | tca_status |
| 328 | |
| 329 | while [ "$WORKER_COUNT" != "3" ]; do |
| 330 | echo "Status checking: worker count is $WORKER_COUNT, needs a reset" |
| 331 | sleep 5 |
| 332 | |
| 333 | tca_restart |
| 334 | echo "TCA restarted" |
| 335 | done |
| 336 | |
| 337 | |
Lusheng Ji | 1dd7a99 | 2018-05-10 23:54:18 -0400 | [diff] [blame] | 338 | if [[ -z "$CBS_HOST" || -z "$CBS_PORT" ]]; then |
| 339 | echo "Retrieving host and port for ${CBS_SERVICE_NAME} from ${CONSUL_HOST}:${CONSUL_PORT}" |
| 340 | sleep 2 |
| 341 | CBS_HOST=$(curl -s "${CONSUL_HOST}:${CONSUL_PORT}/v1/catalog/service/${CBS_SERVICE_NAME}" |jq .[0].ServiceAddress |sed -e 's/\"//g') |
| 342 | CBS_PORT=$(curl -s "${CONSUL_HOST}:${CONSUL_PORT}/v1/catalog/service/${CBS_SERVICE_NAME}" |jq .[0].ServicePort |sed -e 's/\"//g') |
| 343 | echo "CBS discovered to be at ${CBS_HOST}:${CBS_PORT}" |
| 344 | fi |
| 345 | |
| 346 | if [ ! -z "$CBS_HOST" ] && [ ! -z "$CBS_PORT" ]; then |
| 347 | tca_poll_policy |
| 348 | fi |
Lusheng Ji | d6d409f | 2018-03-22 23:25:45 -0400 | [diff] [blame] | 349 | sleep 30 |
| 350 | done |