diff --git a/templates/update_tags.sh.tpl b/templates/update_tags.sh.tpl index 8fd4c01..ddcf2df 100644 --- a/templates/update_tags.sh.tpl +++ b/templates/update_tags.sh.tpl @@ -24,21 +24,27 @@ fi CURL_OPTS="--connect-timeout 5 --max-time 10 --retry 5 --retry-delay 1 --retry-max-time 40 --retry-connrefused --silent" # Update tags on the main service -SERVICE=$(curl ${CURL_OPTS} -H "X-Consul-Token: ${CONSUL_HTTP_TOKEN}" http://{{ sockaddr "GetInterfaceIP \"nomad\"" }}:8500/v1/catalog/service/[[ .instance ]]) -if [ "$(echo ${SERVICE} | jq ".[] | .ServiceTags | any(.==\"${NEW_ROLE}\")")" = "false" ]; then +SERVICE_HAS_TAG=$(curl ${CURL_OPTS} \ + -H "X-Consul-Token: ${CONSUL_HTTP_TOKEN}" \ + http://{{ sockaddr "GetInterfaceIP \"nomad\"" }}:8500/v1/catalog/service/[[ .instance ]] |\ + jq ".[] | select( .ServiceTags as \$tags | \"postgres-{{ env "NOMAD_ALLOC_INDEX" }}\" | IN(\$tags[]) ) | .ServiceTags | any(.==\"master\")") +if [ "${SERVICE_HAS_TAG}" = "false" ]; then echo "Updating tags for the main service" - echo "${SERVICE}" |\ + curl ${CURL_OPTS} -H "X-Consul-Token: ${CONSUL_HTTP_TOKEN}" http://{{ sockaddr "GetInterfaceIP \"nomad\"" }}:8500/v1/catalog/service/[[ .instance ]] |\ jq --from-file /local/serviceformat.jq --arg role "${NEW_ROLE}" --arg mytag postgres-{{ env "NOMAD_ALLOC_INDEX" }} |\ curl ${CORL_OPTS} -H "X-Consul-Token: ${CONSUL_HTTP_TOKEN}" -X PUT -d @- http://{{ sockaddr "GetInterfaceIP \"nomad\"" }}:8500/v1/txn > /dev/null else - echo "Main service already has th expected ${NEW_ROLE} tag" + echo "Main service already has the expected ${NEW_ROLE} tag" fi # Update tags on the sidecar service (connect-proxy) -SIDECAR=$(curl ${CURL_OPTS} -H "X-Consul-Token: ${CONSUL_HTTP_TOKEN}" http://{{ sockaddr "GetInterfaceIP \"nomad\"" }}:8500/v1/catalog/service/[[ .instance ]]-sidecar-proxy) -if [ "$(echo ${SIDECAR} | jq ".[] | .ServiceTags | any(.==\"${NEW_ROLE}\")")" = "false" ]; then +SIDECAR_HAS_TAG=$(curl ${CURL_OPTS} \ + -H "X-Consul-Token: ${CONSUL_HTTP_TOKEN}" \ + http://{{ sockaddr "GetInterfaceIP \"nomad\"" }}:8500/v1/catalog/service/[[ .instance ]]-sidecar-proxy |\ + jq ".[] | select( .ServiceTags as \$tags | \"postgres-{{ env "NOMAD_ALLOC_INDEX" }}\" | IN(\$tags[]) ) | .ServiceTags | any(.==\"master\")") +if [ "${SIDECAR_HAS_TAG}" = "false" ]; then echo "Updating tags for the sidecar" - echo "${SIDECAR}" |\ + curl ${CURL_OPTS} -H "X-Consul-Token: ${CONSUL_HTTP_TOKEN}" http://{{ sockaddr "GetInterfaceIP \"nomad\"" }}:8500/v1/catalog/service/[[ .instance ]]-sidecar-proxy |\ jq --from-file /local/serviceformat.jq --arg role "${NEW_ROLE}" --arg mytag postgres-{{ env "NOMAD_ALLOC_INDEX" }} |\ curl ${CURL_OPTS} -H "X-Consul-Token: ${CONSUL_HTTP_TOKEN}" -X PUT -d @- http://{{ sockaddr "GetInterfaceIP \"nomad\"" }}:8500/v1/txn > /dev/null else