blob: 95a41d2a1c0f5f6d69313c648d9782e9a6bb84fd [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 Ji3622f7a2018-04-04 21:51:34 -040026TCA_ARTIFACT_VERSION='2.2.0-SNAPSHOT'
Lusheng Jid6d409f2018-03-22 23:25:45 -040027TCA_FILE_PATH='/opt/tca'
Lusheng Ji3622f7a2018-04-04 21:51:34 -040028TCA_JAR="${TCA_FILE_PATH}/${TCA_ARTIFACT}.${TCA_ARTIFACT_VERSION}.jar"
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 Ji3622f7a2018-04-04 21:51:34 -040040CONSUL_HOST=${CONSU_HOST:-consul}
41CONSUL_PORT=${CONSU_PORT:-8500}
42CONFIG_BINDING_SERVICE=${CONFIG_BINDING_SERVICE:-config_binding_service}
43
44CBS_SERVICE_NAME=${CONFIG_BINDING_SERVICE}
45
46CBS_HOST=$(curl -s "${CONSUL_HOST}:${CONSUL_PORT}/v1/catalog/service/${CBS_SERVICE_NAME}" |jq .[0].ServiceAddress |sed -e 's/\"//g')
47CBS_PORT=$(curl -s "${CONSUL_HOST}:${CONSUL_PORT}/v1/catalog/service/${CBS_SERVICE_NAME}" |jq .[0].ServicePort |sed -e 's/\"//g')
48CBS_HOST=${CBS_HOST:-config_binding_service}
49CBS_PORT=${CBS_PORT:-10000}
50
51MY_NAME=${SERVICE_NAME:-tca}
52
53echo "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
56echo "Generting preference file"
57sed -i 's/{{DMAAPHOST}}/'"${DMAAPHOST}"'/g' ${TCA_PREF}
58sed -i 's/{{DMAAPPORT}}/'"${DMAAPPORT}"'/g' ${TCA_PREF}
59sed -i 's/{{DMAAPPUBTOPIC}}/'"${DMAAPPUBTOPIC}"'/g' ${TCA_PREF}
60sed -i 's/{{DMAAPSUBTOPIC}}/'"${DMAAPSUBTOPIC}"'/g' ${TCA_PREF}
61sed -i 's/{{DMAAPSUBGROUP}}/OpenDCAEc12/g' ${TCA_PREF}
62sed -i 's/{{DMAAPSUBID}}/c12/g' ${TCA_PREF}
63sed -i 's/{{AAIHOST}}/'"${AAIHOST}"'/g' ${TCA_PREF}
64sed -i 's/{{AAIPORT}}/'"${AAIPORT}"'/g' ${TCA_PREF}
65if [ -z $REDISHOSTPORT ]; then
66 sed -i 's/{{REDISHOSTPORT}}/NONE/g' ${TCA_PREF}
67 sed -i 's/{{REDISCACHING}}/false/g' ${TCA_PREF}
68else
69 sed -i 's/{{REDISHOSTPORT}}/'"${REDISHOSTPORT}"'/g' ${TCA_PREF}
70 sed -i 's/{{REDISCACHING}}/true/g' ${TCA_PREF}
71fi
Lusheng Jid6d409f2018-03-22 23:25:45 -040072
73function 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
85function tca_load_artifact {
86 echo
Lusheng Ji3622f7a2018-04-04 21:51:34 -040087 echo "Loading artifact ${TCA_JAR} to http://${TCA_PATH_ARTIFACT}/${TCA_ARTIFACT}..."
Lusheng Jid6d409f2018-03-22 23:25:45 -040088 curl -s -X POST --data-binary @"${TCA_JAR}" "http://${TCA_PATH_ARTIFACT}/${TCA_ARTIFACT}"
89 echo
90}
91
92function tca_load_conf {
93 echo
Lusheng Ji3622f7a2018-04-04 21:51:34 -040094 echo "Loading configuration ${TCA_APP_CONF} to http://${TCA_PATH_APP}"
Lusheng Jid6d409f2018-03-22 23:25:45 -040095 curl -s -X PUT -d @${TCA_APP_CONF} http://${TCA_PATH_APP}
96 echo
97
98 # load preferences
99 echo
Lusheng Ji3622f7a2018-04-04 21:51:34 -0400100 echo "Loading preferences ${TCA_PREF} to http://${TCA_PATH_APP}/preferences"
Lusheng Jid6d409f2018-03-22 23:25:45 -0400101 curl -s -X PUT -d @${TCA_PREF} http://${TCA_PATH_APP}/preferences
102 echo
103}
104
105
106function tca_delete {
107 echo
Lusheng Ji3622f7a2018-04-04 21:51:34 -0400108 echo "Deleting application dcae-tca http://${TCA_PATH_APP}"
Lusheng Jid6d409f2018-03-22 23:25:45 -0400109 curl -s -X DELETE http://${TCA_PATH_APP}
110 echo
111
112 # delete artifact
113 echo
Lusheng Ji3622f7a2018-04-04 21:51:34 -0400114 echo "Deleting artifact http://${TCA_PATH_ARTIFACT}/${TCA_ARTIFACT}/versions/${TCA_ARTIFACT_VERSION} ..."
Lusheng Jid6d409f2018-03-22 23:25:45 -0400115 curl -s -X DELETE "http://${TCA_PATH_ARTIFACT}/${TCA_ARTIFACT}/versions/${TCA_ARTIFACT_VERSION}"
116 echo
117}
118
119function 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
129function 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
143function tca_poll_policy {
Lusheng Jid6d409f2018-03-22 23:25:45 -0400144 MY_NAME=${SERVICE_NAME:-tca}
Lusheng Jid6d409f2018-03-22 23:25:45 -0400145
146 URL1="${CBS_HOST}:${CBS_PORT}/service_component/${MY_NAME}"
147 URL2="$URL1:preferences"
148
Lusheng Ji3622f7a2018-04-04 21:51:34 -0400149 echo "tca_poll_policy: Retrieving configuration file at ${URL1}"
Lusheng Jiac864ce2018-04-05 12:02:52 -0400150 HTTP_RESPONSE=$(curl --silent --write-out "HTTPSTATUS:%{http_code}" "$URL1")
151 HTTP_BODY=$(echo $HTTP_RESPONSE | sed -e 's/HTTPSTATUS\:.*//g')
152 HTTP_STATUS=$(echo $HTTP_RESPONSE | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
153 if [ "$HTTP_STATUS" != "200" ]; then
154 echo "receiving $HTTP_RESPONSE from CBS"
155 return
156 fi
157 echo $HTTP_BODY | jq . --sort-keys > "${TCA_CONF_TEMP}"
158
159 echo "tca_poll_policy: Retrieving preferences file at ${URL1}"
160 HTTP_RESPONSE=$(curl --silent --write-out "HTTPSTATUS:%{http_code}" "$URL2")
161 HTTP_BODY=$(echo $HTTP_RESPONSE | sed -e 's/HTTPSTATUS\:.*//g')
162 HTTP_STATUS=$(echo $HTTP_RESPONSE | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
163 if [ "$HTTP_STATUS" != "200" ]; then
164 echo "receiving $HTTP_RESPONSE from CBS"
165 return
166 fi
167 echo $HTTP_BODY | jq . --sort-keys > "${TCA_PREF_TEMP}"
Lusheng Jid6d409f2018-03-22 23:25:45 -0400168
169 if [ ! -e "${TCA_CONF_TEMP}" ] || [ "$(ls -sh ${TCA_CONF_TEMP} |cut -f1 -d' ' |sed -e 's/[^0-9]//g')" -lt "1" ]; then
170 echo "Fail to receive configuration"
171 return
172 fi
173 if [ ! -e "${TCA_PREF_TEMP}" ] || [ "$(ls -sh ${TCA_PREF_TEMP} |cut -f1 -d' ' |sed -e 's/[^0-9]//g')" -lt "1" ]; then
174 echo "Fail to receive preferences"
175 return
176 fi
177
178 CONF_CHANGED=""
179 # extract only the config section from APP CONF (which has both artifact and config sections)
180 jq .config --sort-keys ${TCA_APP_CONF} > ${TCA_CONF}
181 if ! diff ${TCA_CONF} ${TCA_CONF_TEMP} ; then
182 echo "TCA config changed"
183 # generating the new app conf using current app conf's artifact section and the new downloaded config
184 jq --argfile CONFVALUE ${TCA_CONF_TEMP} '.config = $CONFVALUE' <${TCA_APP_CONF} > ${TCA_APP_CONF_TEMP}
185
186 mv ${TCA_APP_CONF_TEMP} ${TCA_APP_CONF}
187 CONF_CHANGED=1
188 fi
189
190 PERF_CHANGED=""
191 # update the subscriber ConsumerID, if not already unique,
192 # so replicas appear as different consumers in the consumer group
193 HOSTID=$(head -1 /etc/hostname | rev |cut -f1-2 -d'-' |rev)
194 CONSUMERID=$(jq .subscriberConsumerId ${TCA_PREF_TEMP} |sed -e 's/\"//g')
195 if ! (echo "$CONSUMERID" |grep "$HOSTID"); then
196 CONSUMERID="${CONSUMERID}-${HOSTID}"
197 jq --arg CID ${CONSUMERID} '.subscriberConsumerId = $CID' < "${TCA_PREF_TEMP}" > "${TCA_PREF_TEMP}2"
198 mv "${TCA_PREF_TEMP}2" "${TCA_PREF_TEMP}"
199 fi
200 if ! diff ${TCA_PREF} ${TCA_PREF_TEMP} ; then
201 echo "TCA preference updated"
202 mv ${TCA_PREF_TEMP} ${TCA_PREF}
203 PERF_CHANGED=1
204 fi
205
206 if [[ "$PERF_CHANGED" == "1" || "$CONF_CHANGED" == "1" ]]; then
207 tca_stop
208 tca_delete
209 tca_load_artifact
210 tca_load_conf
211 tca_start
212 tca_status
213 fi
214}
215
216
217export PATH=${PATH}:/opt/cdap/sdk/bin
218
219# starting CDAP SDK in background
220cdap sdk start
221
222
223
224echo "Waiting CDAP ready on port 11015 ..."
225while ! nc -z ${CDAP_HOST} ${CDAP_PORT}; do
226 sleep 0.1 # wait for 1/10 of the second before check again
227done
228echo "CDAP has started"
229
230
231echo "Creating namespace cdap_tca_hi_lo ..."
232curl -s -X PUT "http://${CDAP_HOST}:${CDAP_PORT}/v3/namespaces/cdap_tca_hi_lo"
233
234
235# stop programs
236tca_stop
237
238
239# delete application
240tca_delete
241
242
243# load artifact
244tca_load_artifact
245tca_load_conf
246
247
248# start programs
249tca_start
250
251
252# get status of programs
253tca_status
254
255
256
257while echo -n
258do
259 echo "======================================================"
260 date
261 tca_poll_policy
262 sleep 30
263done