#!/bin/bash
echo "Starting"
export hostip=$(hostname -i)
# Wait until Consul can be contacted
until curl -s ${CONSUL_HTTP_ADDR}/v1/status/leader | grep 8300; do
echo "Waiting for Consul to start"
sleep 1
done
# register any config from individual files
if [ ! -z "$CONFIG_FILE" ]; then
IFS=';' read -r -a configs <<< ${CONFIG_FILE}
for file in "${configs[@]}"; do
echo "Writing central config $file"
consul config write $file
exit_status=$?
if [ $exit_status -ne 0 ]; then
echo "### Error writing central config: $file ###"
cat $file
echo ""
exit 1
fi
done
fi
# register any central config from a folder
if [ ! -z "$CENTRAL_CONFIG_DIR" ]; then
for file in `ls -v $CENTRAL_CONFIG_DIR/*`; do
echo "Writing central config $file"
consul config write $file
echo ""
exit_status=$?
if [ $exit_status -ne 0 ]; then
echo "### Error writing central config: $file ###"
cat $file
echo ""
exit 1
fi
done
fi
# If we do not need to register a service just run the command
if [ ! -z "$SERVICE_NAME" ]; then
# register the service with consul
echo "Registering service with consul $SERVICE_NAME"
consul connect envoy -gateway=ingress -register -service ${SERVICE_NAME} -address ${hostip}:${SERVICE_BIND}
exit_status=$?
if [ $exit_status -ne 0 ]; then
echo "### Error writing service config: $file ###"
cat $file
echo ""
exit 1
fi
# make sure the service deregisters when exit
export service_id=$(curl http://${CONSUL_HTTP_ADDR}/v1/catalog/service/${SERVICE_NAME}| jq --raw-output '.[0].ServiceID')
trap "consul services deregister -id=${service_id}" SIGINT SIGTERM EXIT
fi
I would then have the following nomad job, to deploy the ingress gateway: