blob: 4f6ed92bf3d4ca92a36a266abb18161c3a3be725 [file] [log] [blame]
Lusheng Jid6d409f2018-03-22 23:25:45 -04001#!/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
18set -e
19
20CDAP_HOST='localhost'
21CDAP_PORT='11015'
22TCA_NAMESPACE='cdap_tca_hi_lo'
23TCA_APPNAME='dcae-tca'
24
25TCA_ARTIFACT='dcae-analytics-cdap-tca'
Lusheng Jid6d409f2018-03-22 23:25:45 -040026TCA_FILE_PATH='/opt/tca'
Lusheng Ji1dd7a992018-05-10 23:54:18 -040027TCA_JAR="$(ls -1r ${TCA_FILE_PATH}/${TCA_ARTIFACT}*.jar | head -1)"
28TCA_ARTIFACT_VERSION=$(echo "$TCA_JAR" |rev |cut -f 2-4 -d '.' |rev)
Lusheng Jid6d409f2018-03-22 23:25:45 -040029TCA_APP_CONF="${TCA_FILE_PATH}/tca_app_config.json"
30TCA_CONF="${TCA_FILE_PATH}/tca_config.json"
31TCA_PREF="${TCA_FILE_PATH}/tca_app_preferences.json"
32TCA_CONF_TEMP='/tmp/tca_config.json'
33TCA_APP_CONF_TEMP='/tmp/tca_app_config.json'
34TCA_PREF_TEMP='/tmp/tca_preferences.json'
35
36TCA_PATH_APP="${CDAP_HOST}:${CDAP_PORT}/v3/namespaces/${TCA_NAMESPACE}/apps/${TCA_APPNAME}"
37TCA_PATH_ARTIFACT="${CDAP_HOST}:${CDAP_PORT}/v3/namespaces/${TCA_NAMESPACE}/artifacts"
38
Lusheng Jid6d409f2018-03-22 23:25:45 -040039
Lusheng Ji1dd7a992018-05-10 23:54:18 -040040CONSUL_HOST=${CONSUL_HOST:-consul}
41CONSUL_PORT=${CONSUL_PORT:-8500}
Lusheng Ji3622f7a2018-04-04 21:51:34 -040042CONFIG_BINDING_SERVICE=${CONFIG_BINDING_SERVICE:-config_binding_service}
43
44CBS_SERVICE_NAME=${CONFIG_BINDING_SERVICE}
45
vagrant61c202d2018-04-05 20:34:40 +000046#Changing to HOSTNAME parameter for consistency with k8s deploy
47MY_NAME=${HOSTNAME:-tca}
Lusheng Ji3622f7a2018-04-04 21:51:34 -040048
Lusheng Ji3622f7a2018-04-04 21:51:34 -040049
50echo "Generting preference file"
51sed -i 's/{{DMAAPHOST}}/'"${DMAAPHOST}"'/g' ${TCA_PREF}
52sed -i 's/{{DMAAPPORT}}/'"${DMAAPPORT}"'/g' ${TCA_PREF}
53sed -i 's/{{DMAAPPUBTOPIC}}/'"${DMAAPPUBTOPIC}"'/g' ${TCA_PREF}
54sed -i 's/{{DMAAPSUBTOPIC}}/'"${DMAAPSUBTOPIC}"'/g' ${TCA_PREF}
55sed -i 's/{{DMAAPSUBGROUP}}/OpenDCAEc12/g' ${TCA_PREF}
56sed -i 's/{{DMAAPSUBID}}/c12/g' ${TCA_PREF}
57sed -i 's/{{AAIHOST}}/'"${AAIHOST}"'/g' ${TCA_PREF}
58sed -i 's/{{AAIPORT}}/'"${AAIPORT}"'/g' ${TCA_PREF}
Lusheng Ji1dd7a992018-05-10 23:54:18 -040059if [ -z "$REDISHOSTPORT" ]; then
Lusheng Ji3622f7a2018-04-04 21:51:34 -040060 sed -i 's/{{REDISHOSTPORT}}/NONE/g' ${TCA_PREF}
61 sed -i 's/{{REDISCACHING}}/false/g' ${TCA_PREF}
62else
63 sed -i 's/{{REDISHOSTPORT}}/'"${REDISHOSTPORT}"'/g' ${TCA_PREF}
64 sed -i 's/{{REDISCACHING}}/true/g' ${TCA_PREF}
65fi
Lusheng Jid6d409f2018-03-22 23:25:45 -040066
67function tca_stop {
68 # stop programs
69 echo
70 echo "Stopping TCADMaaPMRPublisherWorker, TCADMaaPMRSubscriberWorker, and TCAVESCollectorFlow ..."
71 echo
72 curl -s -X POST "http://${TCA_PATH_APP}/workers/TCADMaaPMRPublisherWorker/stop"
73 curl -s -X POST "http://${TCA_PATH_APP}/workers/TCADMaaPMRSubscriberWorker/stop"
74 curl -s -X POST "http://${TCA_PATH_APP}/flows/TCAVESCollectorFlow/stop"
75 echo "done"
76 echo
77}
78
79function tca_load_artifact {
80 echo
Lusheng Ji3622f7a2018-04-04 21:51:34 -040081 echo "Loading artifact ${TCA_JAR} to http://${TCA_PATH_ARTIFACT}/${TCA_ARTIFACT}..."
Lusheng Jid6d409f2018-03-22 23:25:45 -040082 curl -s -X POST --data-binary @"${TCA_JAR}" "http://${TCA_PATH_ARTIFACT}/${TCA_ARTIFACT}"
83 echo
84}
85
86function tca_load_conf {
87 echo
Lusheng Ji3622f7a2018-04-04 21:51:34 -040088 echo "Loading configuration ${TCA_APP_CONF} to http://${TCA_PATH_APP}"
Lusheng Jid6d409f2018-03-22 23:25:45 -040089 curl -s -X PUT -d @${TCA_APP_CONF} http://${TCA_PATH_APP}
90 echo
91
92 # load preferences
93 echo
Lusheng Ji3622f7a2018-04-04 21:51:34 -040094 echo "Loading preferences ${TCA_PREF} to http://${TCA_PATH_APP}/preferences"
Lusheng Jid6d409f2018-03-22 23:25:45 -040095 curl -s -X PUT -d @${TCA_PREF} http://${TCA_PATH_APP}/preferences
96 echo
97}
98
99
100function tca_delete {
101 echo
Lusheng Ji3622f7a2018-04-04 21:51:34 -0400102 echo "Deleting application dcae-tca http://${TCA_PATH_APP}"
Lusheng Jid6d409f2018-03-22 23:25:45 -0400103 curl -s -X DELETE http://${TCA_PATH_APP}
104 echo
105
106 # delete artifact
107 echo
Lusheng Ji3622f7a2018-04-04 21:51:34 -0400108 echo "Deleting artifact http://${TCA_PATH_ARTIFACT}/${TCA_ARTIFACT}/versions/${TCA_ARTIFACT_VERSION} ..."
Lusheng Jid6d409f2018-03-22 23:25:45 -0400109 curl -s -X DELETE "http://${TCA_PATH_ARTIFACT}/${TCA_ARTIFACT}/versions/${TCA_ARTIFACT_VERSION}"
110 echo
111}
112
113function tca_start {
114 echo
115 echo "Starting TCADMaaPMRPublisherWorker, TCADMaaPMRSubscriberWorker, and TCAVESCollectorFlow ..."
116 curl -s -X POST "http://${TCA_PATH_APP}/workers/TCADMaaPMRPublisherWorker/start"
117 curl -s -X POST "http://${TCA_PATH_APP}/workers/TCADMaaPMRSubscriberWorker/start"
118 curl -s -X POST "http://${TCA_PATH_APP}/flows/TCAVESCollectorFlow/start"
119 echo
120}
121
122
123function tca_status {
124 echo
125 echo "TCADMaaPMRPublisherWorker status: "
126 curl -s "http://${TCA_PATH_APP}/workers/TCADMaaPMRPublisherWorker/status"
127 echo
128 echo "TCADMaaPMRSubscriberWorker status: "
129 curl -s "http://${TCA_PATH_APP}/workers/TCADMaaPMRSubscriberWorker/status"
130 echo
131 echo "TCAVESCollectorFlow status"
132 curl -s "http://${TCA_PATH_APP}/flows/TCAVESCollectorFlow/status"
133 echo; echo
134}
135
136
137function tca_poll_policy {
Lusheng Jie134c822018-04-23 23:16:32 -0400138 URL0="${CBS_HOST}:${CBS_PORT}/service_component_all/${MY_NAME}"
Lusheng Ji1dd7a992018-05-10 23:54:18 -0400139 echo "tca_poll_policy: Retrieving all-in-one config at ${URL0}"
Lusheng Jie134c822018-04-23 23:16:32 -0400140 HTTP_RESPONSE=$(curl --silent --write-out "HTTPSTATUS:%{http_code}" "$URL0")
Lusheng Ji1dd7a992018-05-10 23:54:18 -0400141 HTTP_BODY=$(echo "$HTTP_RESPONSE" | sed -e 's/HTTPSTATUS\:.*//g')
142 HTTP_STATUS=$(echo "$HTTP_RESPONSE" | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
Lusheng Jiac864ce2018-04-05 12:02:52 -0400143
Lusheng Jiac864ce2018-04-05 12:02:52 -0400144 if [ "$HTTP_STATUS" != "200" ]; then
Lusheng Ji1dd7a992018-05-10 23:54:18 -0400145 echo "tca_poll_policy: Retrieving all-in-one config failed with status $HTTP_STATUS"
Lusheng Jie134c822018-04-23 23:16:32 -0400146 URL1="${CBS_HOST}:${CBS_PORT}/service_component/${MY_NAME}"
Lusheng Ji1dd7a992018-05-10 23:54:18 -0400147 echo "tca_poll_policy: Retrieving app config only at ${URL1}"
Lusheng Jie134c822018-04-23 23:16:32 -0400148 HTTP_RESPONSE1=$(curl --silent --write-out "HTTPSTATUS:%{http_code}" "$URL1")
Lusheng Ji1dd7a992018-05-10 23:54:18 -0400149 HTTP_BODY1=$(echo "$HTTP_RESPONSE1" | sed -e 's/HTTPSTATUS\:.*//g')
150 HTTP_STATUS1=$(echo "$HTTP_RESPONSE1" | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
Lusheng Jie134c822018-04-23 23:16:32 -0400151 if [ "$HTTP_STATUS1" != "200" ]; then
Lusheng Ji1dd7a992018-05-10 23:54:18 -0400152 echo "tca_poll_policy: Retrieving app config only failed with status $HTTP_STATUS1"
Lusheng Jie134c822018-04-23 23:16:32 -0400153 return
154 fi
155
156 URL2="$URL1:preferences"
Lusheng Ji1dd7a992018-05-10 23:54:18 -0400157 echo "tca_poll_policy: Retrieving app preferences only at ${URL2}"
Lusheng Jie134c822018-04-23 23:16:32 -0400158 HTTP_RESPONSE2=$(curl --silent --write-out "HTTPSTATUS:%{http_code}" "$URL2")
Lusheng Ji1dd7a992018-05-10 23:54:18 -0400159 HTTP_BODY2=$(echo "$HTTP_RESPONSE2" | sed -e 's/HTTPSTATUS\:.*//g')
160 HTTP_STATUS2=$(echo "$HTTP_RESPONSE2" | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
Lusheng Jie134c822018-04-23 23:16:32 -0400161 if [ "$HTTP_STATUS2" != "200" ]; then
Lusheng Ji1dd7a992018-05-10 23:54:18 -0400162 echo "tca_poll_policy: Retrieving app preferences only failed with status $HTTP_STATUS2"
163 return
164 fi
165
166 if [[ "$CONFIG" == "null" || "$PREF" == "null" ]]; then
167 echo "tca_poll_policy: either app config or app preferences being empty, config not applicable"
Lusheng Jie134c822018-04-23 23:16:32 -0400168 return
169 fi
170
Lusheng Ji1dd7a992018-05-10 23:54:18 -0400171 echo "$HTTP_BODY1" | jq . --sort-keys > "${TCA_CONF_TEMP}"
172 echo "$HTTP_BODY2" | jq . --sort-keys > "${TCA_PREF_TEMP}"
Lusheng Jie134c822018-04-23 23:16:32 -0400173 else
Lusheng Ji1dd7a992018-05-10 23:54:18 -0400174 CONFIG=$(echo "$HTTP_BODY" | jq .config.app_config)
175 PREF=$(echo "$HTTP_BODY" | jq .config.app_preferences)
176 POLICY=$(echo "$HTTP_BODY" | jq .policies.items[0].config.content.tca_policy)
vagrantb51fd792018-05-01 08:21:11 +0000177
vagrantb51fd792018-05-01 08:21:11 +0000178
Lusheng Ji1dd7a992018-05-10 23:54:18 -0400179 if [[ "$CONFIG" == "null" || "$PREF" == "null" ]]; then
180 echo "tca_poll_policy: CONFIG received is parsed to be empty, trying to parse using R1 format"
181 CONFIG=$(echo "$HTTP_BODY" | jq .config)
182 NEWPREF=$(echo "$HTTP_BODY" | jq .preferences)
183
184 #echo "CONFIG is [$CONFIG]"
185 #echo "NEWPREF is [$NEWPREF]"
vagrantb51fd792018-05-01 08:21:11 +0000186 else
Lusheng Ji1dd7a992018-05-10 23:54:18 -0400187 echo "tca_poll_policy: CONFIG is [${CONFIG}], PREF is [${PREF}], POLICY is [${POLICY}]"
188 ## Check if policy content under tca_policy is returned null
189 ## null indicates no active policy flow; hence use configuration loaded
190 ## from blueprint
191 if [ "$POLICY" == "null" ]; then
192 # tca_policy through blueprint
193 NEWPREF=${PREF}
194 else
195 # tca_policy through active policy flow through PH
196 NEWPREF=$(echo "$PREF" | jq --arg tca_policy "$POLICY" '. + {$tca_policy}')
197 fi
198 NEWPREF=$(echo "$NEWPREF" | sed 's/\\n//g')
vagrantb51fd792018-05-01 08:21:11 +0000199 fi
Lusheng Ji1dd7a992018-05-10 23:54:18 -0400200
201 if [[ "$CONFIG" == "null" || "$NEWPREF" == "null" ]]; then
202 echo "tca_poll_policy: either app config or app preferences being empty, config not applicable"
203 return
204 fi
205
206 echo "$CONFIG" | jq . --sort-keys > "${TCA_CONF_TEMP}"
207 echo "$NEWPREF" | jq . --sort-keys > "${TCA_PREF_TEMP}"
Lusheng Jiac864ce2018-04-05 12:02:52 -0400208 fi
Lusheng Jid6d409f2018-03-22 23:25:45 -0400209
210 if [ ! -e "${TCA_CONF_TEMP}" ] || [ "$(ls -sh ${TCA_CONF_TEMP} |cut -f1 -d' ' |sed -e 's/[^0-9]//g')" -lt "1" ]; then
211 echo "Fail to receive configuration"
212 return
213 fi
214 if [ ! -e "${TCA_PREF_TEMP}" ] || [ "$(ls -sh ${TCA_PREF_TEMP} |cut -f1 -d' ' |sed -e 's/[^0-9]//g')" -lt "1" ]; then
215 echo "Fail to receive preferences"
216 return
217 fi
218
219 CONF_CHANGED=""
220 # extract only the config section from APP CONF (which has both artifact and config sections)
221 jq .config --sort-keys ${TCA_APP_CONF} > ${TCA_CONF}
222 if ! diff ${TCA_CONF} ${TCA_CONF_TEMP} ; then
223 echo "TCA config changed"
224 # generating the new app conf using current app conf's artifact section and the new downloaded config
225 jq --argfile CONFVALUE ${TCA_CONF_TEMP} '.config = $CONFVALUE' <${TCA_APP_CONF} > ${TCA_APP_CONF_TEMP}
226
227 mv ${TCA_APP_CONF_TEMP} ${TCA_APP_CONF}
228 CONF_CHANGED=1
229 fi
230
231 PERF_CHANGED=""
232 # update the subscriber ConsumerID, if not already unique,
233 # so replicas appear as different consumers in the consumer group
234 HOSTID=$(head -1 /etc/hostname | rev |cut -f1-2 -d'-' |rev)
235 CONSUMERID=$(jq .subscriberConsumerId ${TCA_PREF_TEMP} |sed -e 's/\"//g')
236 if ! (echo "$CONSUMERID" |grep "$HOSTID"); then
237 CONSUMERID="${CONSUMERID}-${HOSTID}"
Lusheng Ji1dd7a992018-05-10 23:54:18 -0400238 jq --arg CID "${CONSUMERID}" '.subscriberConsumerId = $CID' < "${TCA_PREF_TEMP}" > "${TCA_PREF_TEMP}2"
Lusheng Jid6d409f2018-03-22 23:25:45 -0400239 mv "${TCA_PREF_TEMP}2" "${TCA_PREF_TEMP}"
240 fi
241 if ! diff ${TCA_PREF} ${TCA_PREF_TEMP} ; then
242 echo "TCA preference updated"
243 mv ${TCA_PREF_TEMP} ${TCA_PREF}
244 PERF_CHANGED=1
245 fi
246
Lusheng Ji1dd7a992018-05-10 23:54:18 -0400247 if [[ "$PERF_CHANGED" == "1" || "$CONF_CHANGED" == "1" ]]; then
248 echo "Newly received configuration/preference differ from the running instance's. reload confg"
Lusheng Jid6d409f2018-03-22 23:25:45 -0400249 tca_stop
250 tca_delete
251 tca_load_artifact
252 tca_load_conf
253 tca_start
254 tca_status
255 fi
256}
257
258
259export PATH=${PATH}:/opt/cdap/sdk/bin
260
Lusheng Ji1dd7a992018-05-10 23:54:18 -0400261
262echo "Starting TCA-CDAP in standalone mode"
263
Lusheng Jid6d409f2018-03-22 23:25:45 -0400264# starting CDAP SDK in background
265cdap sdk start
266
Lusheng Ji1dd7a992018-05-10 23:54:18 -0400267echo "Started, waiting CDAP ready on port 11015 ..."
Lusheng Jid6d409f2018-03-22 23:25:45 -0400268while ! nc -z ${CDAP_HOST} ${CDAP_PORT}; do
269 sleep 0.1 # wait for 1/10 of the second before check again
270done
Lusheng Jid6d409f2018-03-22 23:25:45 -0400271
272echo "Creating namespace cdap_tca_hi_lo ..."
273curl -s -X PUT "http://${CDAP_HOST}:${CDAP_PORT}/v3/namespaces/cdap_tca_hi_lo"
274
Lusheng Jid6d409f2018-03-22 23:25:45 -0400275# stop programs
276tca_stop
277
Lusheng Jid6d409f2018-03-22 23:25:45 -0400278# delete application
279tca_delete
280
Lusheng Jid6d409f2018-03-22 23:25:45 -0400281# load artifact
282tca_load_artifact
283tca_load_conf
284
Lusheng Jid6d409f2018-03-22 23:25:45 -0400285# start programs
286tca_start
287
Lusheng Jid6d409f2018-03-22 23:25:45 -0400288# get status of programs
289tca_status
290
Lusheng Ji1dd7a992018-05-10 23:54:18 -0400291echo "TCA-CDAP standalone mode initialization completed"
Lusheng Jid6d409f2018-03-22 23:25:45 -0400292
293
Lusheng Ji1dd7a992018-05-10 23:54:18 -0400294
295#Changing to HOSTNAME parameter for consistency with k8s deploy
296MY_NAME=${HOSTNAME:-tca}
297
298unset CBS_HOST
299unset CBS_PORT
300echo "TCA environment: I am ${MY_NAME}, consul at ${CONSUL_HOST}:${CONSUL_PORT}, CBS service name ${CBS_SERVICE_NAME}"
301
302while echo
Lusheng Jid6d409f2018-03-22 23:25:45 -0400303do
Lusheng Ji1dd7a992018-05-10 23:54:18 -0400304 echo "$(date): ======================================================"
305 if [[ -z "$CBS_HOST" || -z "$CBS_PORT" ]]; then
306 echo "Retrieving host and port for ${CBS_SERVICE_NAME} from ${CONSUL_HOST}:${CONSUL_PORT}"
307 sleep 2
308 CBS_HOST=$(curl -s "${CONSUL_HOST}:${CONSUL_PORT}/v1/catalog/service/${CBS_SERVICE_NAME}" |jq .[0].ServiceAddress |sed -e 's/\"//g')
309 CBS_PORT=$(curl -s "${CONSUL_HOST}:${CONSUL_PORT}/v1/catalog/service/${CBS_SERVICE_NAME}" |jq .[0].ServicePort |sed -e 's/\"//g')
310 echo "CBS discovered to be at ${CBS_HOST}:${CBS_PORT}"
311 fi
312
313 if [ ! -z "$CBS_HOST" ] && [ ! -z "$CBS_PORT" ]; then
314 tca_poll_policy
315 fi
Lusheng Jid6d409f2018-03-22 23:25:45 -0400316 sleep 30
317done