diff --git a/example/postgres.nomad.hcl b/example/postgres.nomad.hcl index 0882d27..fe56a2c 100644 --- a/example/postgres.nomad.hcl +++ b/example/postgres.nomad.hcl @@ -201,9 +201,8 @@ def create_svc_txn: | .ServiceTags -= ["replica"] | .ServiceTags -= ["uninitialized"] | .ServiceTags -= ["traefik.enable=true"] - | .ServiceTags -= ["traefik.enable=false"] | .ServiceTags += [$role] - | if $role == "master" then .ServiceTags += ["traefik.enable=true"] else .ServiceTags += ["traefik.enable=false"] end + | if $role == "master" then .ServiceTags += ["traefik.enable=true"] else .ServiceTags -= ["traefik.enable=true"] end # Rename 'ServiceName' to 'ServiceService' # Will be transformed to 'Service.Service later' @@ -261,21 +260,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/postgres) -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/postgres |\ + jq ".[] | select( .ServiceTags as \$tags | \"postgres-{{ env "NOMAD_ALLOC_INDEX" }}\" | IN(\$tags[]) ) | .ServiceTags | any(.==\"${NEW_ROLE}\")") +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/postgres |\ 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/postgres-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/postgres-sidecar-proxy |\ + jq ".[] | select( .ServiceTags as \$tags | \"postgres-{{ env "NOMAD_ALLOC_INDEX" }}\" | IN(\$tags[]) ) | .ServiceTags | any(.==\"${NEW_ROLE}\")") +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/postgres-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