-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path05-deploy-app.sh
More file actions
executable file
·90 lines (75 loc) · 3.17 KB
/
Copy path05-deploy-app.sh
File metadata and controls
executable file
·90 lines (75 loc) · 3.17 KB
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
#!/bin/bash
# Variables
source ./00-variables.sh
# Retrieve the PostgreSQL server FQDN
PG_FQDN_FULL=$(az postgres flexible-server show \
--name "$PG_SERVER_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--query "fullyQualifiedDomainName" \
--output tsv)
if [ -z "$PG_FQDN_FULL" ]; then
echo "Failed to retrieve PostgreSQL server FQDN. Run 01-deploy-resources.sh first."
exit 1
fi
# Split host:port (LocalStack emulator embeds the dynamic TCP-proxy port in fullyQualifiedDomainName;
# real Azure returns just the bare host).
PG_FQDN="${PG_FQDN_FULL%%:*}"
if [[ "$PG_FQDN_FULL" == *:* ]]; then
PG_PORT="${PG_FQDN_FULL##*:}"
fi
# Generate a stable Flask SECRET_KEY (sessions survive pod restarts)
# Reuse the key already stored in the Secret, when there is one. A new key on every run would leave the
# running pods signing with the old one, so their sessions, flash messages and antiforgery tokens break
# across replicas until every pod has restarted.
FLASK_SECRET_KEY=$(kubectl get secret vacation-planner-postgres-secrets --namespace $NAMESPACE --output jsonpath='{.data.SECRET_KEY}' 2>/dev/null | base64 --decode 2>/dev/null)
if [[ -z $FLASK_SECRET_KEY ]]; then
FLASK_SECRET_KEY=$(openssl rand -hex 32)
fi
# Get the login server for the Azure Container Registry
echo "Getting login server for Azure Container Registry [$ACR_NAME]..."
ACR_LOGIN_SERVER=$(az acr show \
--name "$ACR_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--query "loginServer" \
--output tsv \
--only-show-errors)
if [ -n "$ACR_LOGIN_SERVER" ]; then
echo "Login server retrieved successfully: $ACR_LOGIN_SERVER"
else
echo "Failed to retrieve login server for Azure Container Registry [$ACR_NAME]."
exit 1
fi
FULL_IMAGE="${ACR_LOGIN_SERVER}/${IMAGE_NAME}:${IMAGE_TAG}"
# Create namespace
cat namespace.yml |
yq "(.metadata.name)|="\""$NAMESPACE"\" |
kubectl apply -f -
# Create secret with the PostgreSQL password and the Flask secret key
cat secret.yml |
yq "(.metadata.namespace)|="\""$NAMESPACE"\" |
yq "(.data.PG_PASSWORD)|="\""$(echo -n $PG_USER_PASSWORD | base64 -w0)"\" |
yq "(.data.SECRET_KEY)|="\""$(echo -n $FLASK_SECRET_KEY | base64 -w0)"\" |
kubectl apply -f -
# Create configmap with environment variables
cat configmap.yml |
yq "(.metadata.namespace)|="\""$NAMESPACE"\" |
yq "(.data.PG_HOST)|="\""$PG_FQDN"\" |
yq "(.data.PG_PORT)|="\""$PG_PORT"\" |
yq "(.data.PG_DATABASE)|="\""$PG_DATABASE_NAME"\" |
yq "(.data.PG_USER)|="\""$PG_USER_NAME"\" |
yq "(.data.LOGIN_NAME)|="\""$LOGIN_NAME"\" |
kubectl apply -f -
# Create deployment
cat deployment.yml |
yq "(.metadata.namespace)|="\""$NAMESPACE"\" |
yq "(.spec.template.spec.containers[0].image)|="\""$FULL_IMAGE"\" |
yq "(.spec.template.spec.containers[0].imagePullPolicy)|="\""$IMAGE_PULL_POLICY"\" |
yq "(.spec.template.spec.containers[0].ports[0].containerPort)|=$PORT" |
kubectl apply -f -
# Create service
cat service.yml |
yq "(.metadata.namespace)|="\""$NAMESPACE"\" |
kubectl apply -f -
# Roll the pods so a re-push of the same image tag actually takes effect: the pod template is unchanged,
# so kubectl apply reports no change and leaves the running pods on the image they started with.
kubectl rollout restart deployment/$DEPLOYMENT_NAME --namespace $NAMESPACE