-
Notifications
You must be signed in to change notification settings - Fork 0
/
kubernetes_deploy.sh
executable file
·105 lines (79 loc) · 3.54 KB
/
kubernetes_deploy.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
#! /bin/bash
set -e
usage="Usage: './kubernetes_deploy.sh image-name selector namespace context rc' e.g. './kubernetes_deploy.sh myImageName app=myApp myNamespace . ./kubernetes/rc.json ./kubernetes/service.json'"
if [[ $# -lt 6 ]]; then
echo "Incorrect number of arguments, minimum of 6 required";
echo $usage;
exit 1;
fi
IMAGE=$1;
SELECTOR=$2;
NAMESPACE=$3
CONTEXT=$4
RC_FILE=$5
SVC_FILE=$6
ADDITIONAL_TAG=$7
export NAMESPACE=$NAMESPACE
export VERSION=${CIRCLE_SHA1:0:7}-ci${CIRCLE_BUILD_NUM}
export QUALIFIED_IMAGE_NAME=${GCLOUD_REGISTRY_PREFIX}gcr.io/${CLOUDSDK_CORE_PROJECT}/${IMAGE}:${VERSION}
export CLOUDSDK_CORE_DISABLE_PROMPTS=1
export CLOUDSDK_PYTHON_SITEPACKAGES=1
export DEPLOYMENT_ID=$CIRCLE_BUILD_NUM
echo "Installing json command line tool"
npm install -g json
echo "Building image ${QUALIFIED_IMAGE_NAME} with context ${CONTEXT}"
docker build -t ${QUALIFIED_IMAGE_NAME} ${CONTEXT}
source $(dirname "$0")/authenticate.sh
echo "Authenticating against cluster"
~/google-cloud-sdk/bin/gcloud container clusters get-credentials $GCLOUD_CLUSTER
echo "Pushing image to registry"
~/google-cloud-sdk/bin/gcloud docker push ${QUALIFIED_IMAGE_NAME} > /dev/null
if [[ -z $ADDITIONAL_TAG ]]; then
ADDITIONAL_IMAGE=${GCLOUD_REGISTRY_PREFIX}gcr.io/${CLOUDSDK_CORE_PROJECT}/${IMAGE}:${ADDITIONAL_TAG}
echo "Pushing additional ${ADDITIONAL_IMAGE} image to registry"
docker tag ${QUALIFIED_IMAGE_NAME} ${ADDITIONAL_IMAGE}
~/google-cloud-sdk/bin/gcloud docker push ${ADDITIONAL_IMAGE} > /dev/null
fi
echo "Expanding variables in service config file"
cat ${SVC_FILE} | perl -pe 's/\{\{(\w+)\}\}/$ENV{$1}/eg' > svc.txt
echo "Checking for existing svc"
SVC_NAME=$(cat svc.txt | json metadata.name)
SVC_EXISTS=$(~/google-cloud-sdk/bin/kubectl get svc $SVC_NAME --namespace=${NAMESPACE} || true)
if [[ -z $SVC_EXISTS ]]; then
echo "Creating svc $SVC_NAME"
cat svc.txt | ~/google-cloud-sdk/bin/kubectl create --namespace=${NAMESPACE} -f -
fi
if [[ -n $SVC_EXISTS ]]; then
echo "svc $SVC_NAME is already deployed"
fi
echo "Checking for existing rc"
RC_QUERY_RESULT=$(~/google-cloud-sdk/bin/kubectl get rc -l ${SELECTOR} --namespace=${NAMESPACE} -o template --template="{{.items}}")
if [[ $RC_QUERY_RESULT == "[]" ]]; then
echo "Deploying new rc"
export REPLICAS=1
cat ${RC_FILE} | perl -pe 's/\{\{(\w+)\}\}/$ENV{$1}/eg' > rc.txt
echo Checking all required secrets exist
SECRETS=$(cat rc.txt | json spec.template.spec.volumes | json -a secret.secretName)
for s in $(echo $SECRETS | tr " " "\n")
do
SECRET_EXISTS=$(~/google-cloud-sdk/bin/kubectl get secret $s --namespace=${NAMESPACE} || true)
if [[ -z $SECRET_EXISTS ]]; then
echo "Secret $s does not exist in namespace $NAMESPACE"
exit 1
fi
unset SECRET_EXISTS
done
echo "Creating rc"
cat rc.txt | ~/google-cloud-sdk/bin/kubectl create --namespace=${NAMESPACE} -f -
fi
if [[ $RC_QUERY_RESULT != "[]" ]]; then
echo "Performing rc rolling update"
OLD_RC_NAME=$(~/google-cloud-sdk/bin/kubectl get rc -l ${SELECTOR} --namespace=${NAMESPACE} -o template --template="{{(index .items 0).metadata.name}}")
echo "Old replication controller name: ${OLD_RC_NAME}"
export REPLICAS=$(~/google-cloud-sdk/bin/kubectl get rc ${OLD_RC_NAME} --namespace=${NAMESPACE} -o template --template="{{.spec.replicas}}")
echo "Current replicas: ${REPLICAS}"
echo "Expanding variables in rc config file"
cat ${RC_FILE} | perl -pe 's/\{\{(\w+)\}\}/$ENV{$1}/eg' > rc.txt
echo "Updating rc"
cat rc.txt | ~/google-cloud-sdk/bin/kubectl rolling-update ${OLD_RC_NAME} --namespace=${NAMESPACE} -f -
fi