summaryrefslogtreecommitdiff
path: root/contrib/imgts/lib_entrypoint.sh
blob: 7b76c823feea99ac1e84ecc5fecc0783bc89b938 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/bin/bash

set -e

RED="\e[1;36;41m"
YEL="\e[1;33;44m"
NOR="\e[0m"
SENTINEL="__unknown__"  # default set in dockerfile
# Disable all input prompts
# https://cloud.google.com/sdk/docs/scripting-gcloud
GCLOUD="gcloud --quiet"

die() {
    EXIT=$1
    PFX=$2
    shift 2
    MSG="$@"
    echo -e "${RED}${PFX}:${NOR} ${YEL}$MSG${NOR}"
    [[ "$EXIT" -eq "0" ]] || exit "$EXIT"
}

# Pass in a list of one or more envariable names; exit non-zero with
# helpful error message if any value is empty
req_env_var() {
    for i; do
        if [[ -z "${!i}" ]]
        then
            die 1 FATAL entrypoint.sh requires \$$i to be non-empty.
        elif [[ "${!i}" == "$SENTINEL" ]]
        then
            die 2 FATAL entrypoint.sh requires \$$i to be explicitly set.
        fi
    done
}

gcloud_init() {
    set +xe
    TMPF=$(mktemp -p '' .$(uuidgen)XXXX)
    trap "rm -f $TMPF" EXIT
    echo "$GCPJSON" > $TMPF  && \
    $GCLOUD auth activate-service-account --project "$GCPPROJECT" --key-file=$TMPF || \
        die 5 FATAL auth
    rm -f $TMPF
}