| 1 | #!/bin/sh
|
| 2 | log() { printf '\n\033[m\033[38;5;11m~~~ %s %s ~~~\033[m\n' $1 $2; }
|
| 3 |
|
| 4 | log "deploy environment"
|
| 5 |
|
| 6 | # obvious stuff
|
| 7 | export PATH=${PATH}:/usr/local/bin
|
| 8 | export SHELL=/bin/sh
|
| 9 | # a righteous secure umask
|
| 10 | umask 027
|
| 11 |
|
| 12 | log "deploy ssh_agent"
|
| 13 |
|
| 14 | # ensure we have a usable running ssh agent to inject keys into
|
| 15 | # reuse an existing agent if there is one
|
| 16 | SSH_AUTH_SOCK=$(find /tmp/ssh-* -type s -name agent.\* -print | head -1 )
|
| 17 |
|
| 18 | # else, clean up and acquire a new one
|
| 19 | if test ! -S "${SSH_AUTH_SOCK:-}" ; then
|
| 20 | echo no existing ssh-agent found, spawning a new one
|
| 21 | /bin/pkill -U $USER ssh-agent
|
| 22 | eval $(/usr/bin/ssh-agent)
|
| 23 | fi
|
| 24 |
|
| 25 | echo ssh-agent: $SSH_AUTH_SOCK
|
| 26 | export SSH_AUTH_SOCK
|
| 27 |
|
| 28 | log "vault authn"
|
| 29 |
|
| 30 | # obtain a vault token for use during this session
|
| 31 | # ensure we have address and auth key for vault
|
| 32 | if test -z "${VAULT_ADDR:-}" ; then
|
| 33 | echo vault: missing VAULT_ADDR url
|
| 34 | exit 1
|
| 35 | fi
|
| 36 |
|
| 37 | if test -z "${VAULT_SECRET:-}" ; then
|
| 38 | echo vault: missing VAULT_SECRET token
|
| 39 | exit 1
|
| 40 | fi
|
| 41 |
|
| 42 | # from here on error handling can be enabled safely
|
| 43 | set -o pipefail
|
| 44 | set -e
|
| 45 |
|
| 46 | # acquire a valid token
|
| 47 | export VAULT_TOKEN=$(/usr/local/bin/vault login -token-only -method=github token=${VAULT_SECRET})
|
| 48 | /usr/local/bin/vault read secret/test > /dev/null 2>&1
|
| 49 | log "vault tokens_valid"
|
| 50 |
|
| 51 | # acquire ssh keys
|
| 52 | # first, the deploy key as github only checks the first presented key
|
| 53 | /usr/local/bin/vault read -field=ssh_private_key secret/enso-bot | ssh-add -
|
| 54 | # then, the ansible key, as ssh daemon on servers will check both
|
| 55 | /usr/local/bin/vault read -field=ssh_private_key secret/ansible | ssh-add -
|
| 56 | log "ssh_agent keys_loaded"
|
| 57 | ssh-add -L
|
| 58 |
|
| 59 |
|
| 60 | log "cleaning environment"
|
| 61 |
|
| 62 | export VAULT_SECRET=""
|
| 63 | test "$PHASE" == "webhook" && export BUILDKITE_REPO=''
|
| 64 | test "$PHASE" != "webhook" && export CABAL_HMAC_SECRET=''
|
| 65 |
|
| 66 | log "setup tmpdir"
|
| 67 | # set a per-job custom TMPDIR
|
| 68 | export TMPDIR=$(mktemp -d -t buildkite)
|
| 69 | echo tmpdir: $TMPDIR
|
| 70 | echo home: $HOME
|
| 71 | echo pwd: $(pwd -P)
|
| 72 |
|
| 73 | ############ end common section ############
|
| 74 |
|
| 75 | log "buildkite env ready"
|