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