-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.sh
executable file
·270 lines (205 loc) · 6.5 KB
/
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#!/bin/bash -e
set -e
set -o noglob
set -o pipefail
# Default Values
BUILD_ID=""
NAMESPACE=""
CONTAINER_REGISTRY="" ## Name of container registry including hostname
REPO="" ## name of repo for registry, ie $CONTAINER_REGISTRY.azurecr.io/$REPO:$BUILD_ID
SUBSCRIPTION_ID="" ## Used for Azure to set subscription
REGION=""
REPLICA_COUNT="3" ## Set amount of replicas for the k8 deployment\
RESOURCE_GROUP="" ## Used for Azure for resource group
# optional with defaults
MAINTENANCE_WINDOW=1 ## deploy using a maintenance window. Command will use drush on the current site to enable maintenance mode, run deploy, then disable maintenance when done
DELETE_CRON=1
GOVCLOUD=0
debug_level=0
# script set (ie, not set from arguments)
CURRENT_DIRECTORY=$(echo "${PWD}")
IMAGE_ID="" ## result of $CONTAINER_REGISTRY/$REPO:$BUILD_ID but only set after those variables are set
DRUPAL_POD=""
AZURE=0
AWS=0
# --- helper functions for logs ---
debug() {
if [ $debug_level -ge 1 ]; then
echo '[DEBUG] ' "$@"
fi
}
info() {
if [ $debug_level -ge 0 ]; then
echo '[INFO] ' "$@"
fi
}
warn() {
echo '[WARN] ' "$@" >&2
}
fatal() {
echo '[ERROR] ' "$@" >&2
exit 1
}
azure_govcloud() {
az cloud set --name AzureUSGovernment
}
azure_authenticate() {
az account set --subscription $SUBSCRIPTION_ID
}
azure_cr_authenticate() {
if [ $GOVCLOUD -eq 1 ]; then
azure_govcloud
fi
REGISTRY_NAME=$(echo "$CONTAINER_REGISTRY" | cut -d '.' -f 1)
az acr login -n $REGISTRY_NAME
}
azure_generate_kubeconfig() {
az aks get-credentials --resource-group $RESOURCE_GROUP --name aks-$NAMESPACE --public-fqdn
}
# --- build image ----
# build() {
# }
# --- set IMAGE_ID variable ----
generate_image_tag() {
IMAGE_ID=$CONTAINER_REGISTRY/$REPO:$BUILD_ID
info "image_id: ${IMAGE_ID}"
}
# --- Push built image to container registry ----
push() {
info "tagging image: drupal-${BUILD_ID} as ${IMAGE_ID}"
# retag prebuilt image
docker image tag drupal-$BUILD_ID $IMAGE_ID
# push to repo
info "pushing image: ${IMAGE_ID}"
docker push $IMAGE_ID
}
get_drupal_pod() {
DRUPAL_POD=$(kubectl get pods -l app=drupal -n $NAMESPACE --sort-by=.metadata.creationTimestamp -o jsonpath='{.items[-1:].metadata.name}')
}
deploy() {
if [ $AZURE -eq 1 ]; then
azure_authenticate
azure_generate_kubeconfig
fi
generate_image_tag
# deploy configmap
kubectl apply -f $CURRENT_DIRECTORY/.kubernetes/configmap-$NAMESPACE.yaml
if [ $DELETE_CRON -eq 1 ]; then
kubectl delete --ignore-not-found cronjob drupal-cron -n $NAMESPACE
fi
# put site into maintenance mode for length of deployment
if [ $MAINTENANCE_WINDOW -eq 1 ]; then
get_drupal_pod
kubectl exec "$DRUPAL_POD" -n $NAMESPACE -- bash -c "drush state:set system.maintenance_mode 1 --input-format=integer"
fi
# Deploy Drupal
export NAMESPACE=$NAMESPACE
export IMAGE_ID=$IMAGE_ID
export REPLICA_COUNT=$REPLICA_COUNT
envsubst '$NAMESPACE,$IMAGE_ID,$REPLICA_COUNT' < $CURRENT_DIRECTORY/.kubernetes/deployment.yaml | kubectl apply -f -
# Wait 5 to ensure the new replica set has been deployed out
sleep 5
kubectl rollout status deployments/drupal -n $NAMESPACE
# set DRUPAL_POD to one of the newest pods so we can run post deploy steps against it
get_drupal_pod
# run post deploy scripts
kubectl exec "$DRUPAL_POD" -n $NAMESPACE -- bash -c "drush deploy"
# lift maintenance mode
if [ $MAINTENANCE_WINDOW -eq 1 ]; then
get_drupal_pod
kubectl exec "$DRUPAL_POD" -n $NAMESPACE -- bash -c "vendor/bin/drush state:set system.maintenance_mode 0 --input-format=integer"
fi
# if we removed cron, we need to add it back in
if [ $DELETE_CRON -eq 1 ]; then
envsubst '$NAMESPACE,$IMAGE_ID' < $CURRENT_DIRECTORY/.kubernetes/crons.yaml | kubectl apply -f -
fi
unset NAMESPACE IMAGE_ID REPLICA_COUNT
}
# --- helper function that combines build and push into one ----
push_image() {
if [ $AZURE -eq 1 ]; then
azure_authenticate
azure_cr_authenticate
fi
generate_image_tag
push
}
# --- exec ----
entrypoint() {
# Array to store missing arguments
missing_args=()
# Extract the command
command="$1"
shift
# Process command-line arguments
for arg in "$@"; do
case $arg in
--build-id=*) BUILD_ID="${arg#*=}" ;;
--namespace=*) NAMESPACE="${arg#*=}" ;;
--container-registry=*) CONTAINER_REGISTRY="${arg#*=}" ;;
--repo=*) REPO="${arg#*=}" ;;
--subscription-id=*) SUBSCRIPTION_ID="${arg#*=}" ;;
--resource-group=*) RESOURCE_GROUP="${arg#*=}" ;;
--region=*) REGION="${arg#*=}" ;;
--maintenance-window=*) MAINTENANCE_WINDOW="${arg#*=}" ;;
--delete-cron=*) DELETE_CRON="${arg#*=}" ;;
--replica-count=*) REPLICA_COUNT="${arg#*=}" ;;
--govcloud=*) GOVCLOUD="${arg#*=}" ;;
-v|--v)
debug_level=$((debug_level + 1))
shift
;;
*) echo "Invalid argument: $arg" ;;
esac
done
# output all commands if debug level is great enough
if [ $debug_level -ge 1 ]; then
set -o xtrace
fi
# Output arguments for debugging purposes
for arg in "$@"; do
info "Argument: $arg"
done
# set appropriate cloud environment based on arguments provided
if [ -n "$SUBSCRIPTION_ID" ] && [ "$SUBSCRIPTION_ID" != "" ]; then
AZURE=1
fi
if [ -n "$REGION" ] && [ "$REGION" != "" ]; then
AWS=1
fi
# Check for required arguments based on the command
case $command in
push)
# Check for required arguments
if [ -z "$BUILD_ID" ]; then missing_args+=("--build-id"); fi
if [ -z "$NAMESPACE" ]; then missing_args+=("--namespace"); fi
if [ -z "$CONTAINER_REGISTRY" ]; then missing_args+=("--container-registry"); fi
if [ -z "$REPO" ]; then missing_args+=("--repo"); fi
if [ -z "$SUBSCRIPTION_ID" ]; then missing_args+=("--subscription-id"); fi
# Execute function if missing_args is empty
if [ ${#missing_args[@]} -eq 0 ]; then push_image; fi
;;
deploy)
# Check for required arguments
if [ -z "$BUILD_ID" ]; then missing_args+=("--build-id"); fi
if [ -z "$NAMESPACE" ]; then missing_args+=("--namespace"); fi
if [ -z "$CONTAINER_REGISTRY" ]; then missing_args+=("--container-registry"); fi
if [ -z "$REPO" ]; then missing_args+=("--repo"); fi
if [ -z "$SUBSCRIPTION_ID" ]; then missing_args+=("--subscription-id"); fi
if [ -z "$RESOURCE_GROUP" ]; then missing_args+=("--resource-group"); fi
# Execute function if missing_args is empty
if [ ${#missing_args[@]} -eq 0 ]; then deploy; fi
;;
*)
echo "Invalid command: $command"
exit 1
;;
esac
# Check if any missing arguments exist
if [ ${#missing_args[@]} -gt 0 ]; then
fatal "Missing required argument(s) for command '$command': ${missing_args[*]}"
fi
}
{
entrypoint "$@"
}