#!/usr/bin/env bash # Copyright (c) HashiCorp, Inc. # SPDX-License-Identifier: BUSL-1.1 set -eu help() { cat < /dev/null run_tests() { run bash script.sh $@ } run_shell() { run bash $@ } run() { local tty_args='' [ -t 1 ] && tty_args='-it' docker run $tty_args --rm \ -v $(pwd):/src \ -w /src \ -e NOMAD_ADDR=$NOMAD_ADDR \ -e NOMAD_TOKEN=$NOMAD_TOKEN \ --ipc=host \ --net=host \ "$IMAGE" \ $@ } run_proxy() { # sending these outputs to stderr so that 'export NOMAD_ADDR=' is the # only stdout line, for users to eval, or this script to write then source. nomad namespace apply proxy 1>&2 nomad job run ./input/proxy.nomad 1>&2 set +e IP="$(_get_aws_ip)" [ -n "$IP" ] || { >&2 echo 'falling back to service IP' IP="$(_get_svc_ip)" } set -e [ -n "$IP" ] || { >&2 echo 'unable to get an IP for nomad proxy...' exit 1 # bad form to exit from a function, but this is essential (and eval'd) } echo "export NOMAD_ADDR=https://$IP:6464" } _get_aws_ip(){ aws_metadata_url="http://169.254.169.254/latest/meta-data" nomad exec -namespace=proxy -job nomad-proxy \ curl -s "$aws_metadata_url/public-ipv4" } _get_svc_ip() { nomad service info -namespace=proxy \ -t '{{ range . }}{{ .Address }}{{ end }}' \ nomad-proxy } stop_proxy() { # make sure addr isn't still pointed at the proxy export NOMAD_ADDR="${NOMAD_ADDR/6464/4646}" nomad job stop -purge -namespace=proxy nomad-proxy nomad namespace delete proxy } run_ci() { set -x run_proxy > /tmp/proxy_addr.env source /tmp/proxy_addr.env run_tests rc=$? stop_proxy exit $rc } opt="$1" case $opt in help|--help|-h) help ;; proxy|--proxy) run_proxy ;; test|--test) shift ; run_tests "$@" ;; stop|--stop) stop_proxy ;; ci|--ci) run_ci ;; shell) shift ; run_shell "$@" ;; *) run_tests "$@" ;; esac