This repository has been archived by the owner on Oct 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
build_cluster.sh
executable file
·105 lines (89 loc) · 2.76 KB
/
build_cluster.sh
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/bin/bash
set -e
set -x
usage() {
echo "Usage: KEY_FILE=<path> $0 -m staging|production|sandbox -d 1|0 -t 1|0" $1 1>&2; exit 1;
}
source /etc/profile
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
PUSH=0
TRAVIS=0
GIT_BRANCH=$(git branch | tail -n 1 | awk '{print $2}')
GIT_COMMIT=$(git log -n 1 | head -n 1 | awk '{print $2}')
while getopts ":m:d:t:" opt; do
case $opt in
m)
echo "${OPTARG} environment"
if [[ "${OPTARG}" == production ]]; then
source $DIR/environments/production.sh
elif [[ "${OPTARG}" == staging ]]; then
source $DIR/environments/staging.sh
elif [[ "${OPTARG}" == sandbox ]]; then
source $DIR/environments/sandbox.sh
else
echo "BAD ARGUMENT TO $0: ${OPTARG}"
exit 1
fi
;;
d)
echo "${OPTARG} deploy"
if [[ "${OPTARG}" == "1" ]]; then
PUSH=1
fi
;;
t)
if [[ "${OPTARG}" == 1 ]]; then
echo "Travis"
cd $TRAVIS_BUILD_DIR
TRAVIS=1
GIT_COMMIT=${TRAVIS_COMMIT}
GIT_BRANCH=${TRAVIS_BRANCH}
fi
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
echo "PROJECT ${PROJECT}"
CONTAINER_TAG=${GIT_BRANCH}-${GIT_COMMIT}
CONTAINER_NAME="gcr.io/${PROJECT}/vis-pipeline:${CONTAINER_TAG}"
# -- DOCKER
# Build container
echo "Building Docker image"
if [ $TRAVIS == 1 ]; then
docker build -t pipeline -f Dockerfile .
else
docker build -t ${PROJECT}/vis-pipeline -f Dockerfile .
fi
if [ $PUSH == 1 ] && [ -n "${KEY_FILE}" ]; then
gcloud config set project ${PROJECT}
gcloud auth activate-service-account --key-file=${KEY_FILE}
echo "Pushing docker image '${CONTAINER_NAME}' to container registry"
# the _json_key is required per instructions here:
# https://cloud.google.com/container-registry/docs/advanced-authentication
docker login -u _json_key -p "$(cat $KEY_FILE)" https://gcr.io
docker tag ${PROJECT}/vis-pipeline ${CONTAINER_NAME}
docker push ${CONTAINER_NAME}
# update the k8s files. most templates are updated at deploy time
# except these, because we need to know the specific container name.
mkdir -p deploy-build/k8s/
cp templates/k8s/configmap.yaml deploy-build/k8s/
cp templates/k8s/deployment_bq.yaml deploy-build/k8s/
cp templates/k8s/deployment_bt.yaml deploy-build/k8s/
./travis/substitute_values.sh deploy-build/k8s/ \
CONTAINER_NAME ${CONTAINER_NAME} \
BIGTABLE_INSTANCE ${BIGTABLE_INSTANCE} \
PROJECT ${PROJECT} \
API_MODE ${API_MODE} \
BIGTABLE_POOL_SIZE ${BIGTABLE_POOL_SIZE} \
STAGING_LOCATION ${STAGING_LOCATION} \
K8_CLUSTER ${K8_CLUSTER} \
GIT_COMMIT ${GIT_COMMIT} \
GIT_BRANCH ${GIT_BRANCH}
fi