blob: bc15cac2b7c53f46db1f931d85cdaa450998a210 [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 Jid6d409f2018-03-22 23:25:45 -0400150 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
202export PATH=${PATH}:/opt/cdap/sdk/bin
203
204# starting CDAP SDK in background
205cdap sdk start
206
207
208
209echo "Waiting CDAP ready on port 11015 ..."
210while ! nc -z ${CDAP_HOST} ${CDAP_PORT}; do
211 sleep 0.1 # wait for 1/10 of the second before check again
212done
213echo "CDAP has started"
214
215
216echo "Creating namespace cdap_tca_hi_lo ..."
217curl -s -X PUT "http://${CDAP_HOST}:${CDAP_PORT}/v3/namespaces/cdap_tca_hi_lo"
218
219
220# stop programs
221tca_stop
222
223
224# delete application
225tca_delete
226
227
228# load artifact
229tca_load_artifact
230tca_load_conf
231
232
233# start programs
234tca_start
235
236
237# get status of programs
238tca_status
239
240
241
242while echo -n
243do
244 echo "======================================================"
245 date
246 tca_poll_policy
247 sleep 30
248done