-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy_ecs_prod.sh
86 lines (72 loc) · 2.11 KB
/
deploy_ecs_prod.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
#!/bin/bash
set -euo pipefail
TASK_OVERRIDE_JSON_COLLECT_STATIC='{
"containerOverrides": [
{
"name": "django-be",
"command": ["python", "manage.py", "collectstatic", "--no-input", "--clear"]
}
]
}'
TASK_OVERRIDE_JSON_MIGRATE_DB='{
"containerOverrides": [
{
"name": "django-be",
"command": ["python", "manage.py", "migrate"]
}
]
}'
# run ECS task with custom command
runTask() {
if [ "$#" -lt 1 ]
then
return 1
fi
local SUBNET_LIST
SUBNET_LIST=$(printf "%s" "$ECS_SUBNETS" | sed 's/\([0-9a-zA-Z-]\+\)/"\1"/g')
local SECGROUP_LIST
SECGROUP_LIST=$(printf "%s" "$ECS_SECURITY_GROUPS" | sed 's/\([0-9a-zA-Z-]\+\)/"\1"/g')
local TASK_NETWORK_JSON='{
"awsvpcConfiguration": {
"subnets": [ '"$SUBNET_LIST"' ],
"securityGroups": [ '"$SECGROUP_LIST"' ],
"assignPublicIp": "DISABLED"
}
}'
aws --region "$ECS_REGION" \
ecs run-task \
--task-definition "$ECS_TASK" \
--cluster "$ECS_CLUSTER" \
--launch-type "FARGATE" \
--network-configuration "$TASK_NETWORK_JSON" \
--overrides "$1" \
--propagate-tags "TASK_DEFINITION"
}
# Backup DB
SNAPSHOT_EXTRA_ID="$(date -u +%Y%m%d-%H%M%S)"
SNAPSHOT_ID="$RDS_SNAPSHOT_PREFIX-$SNAPSHOT_EXTRA_ID"
aws --region "$RDS_REGION" \
rds create-db-cluster-snapshot \
--db-cluster-identifier "$RDS_CLUSTER_ID" \
--db-cluster-snapshot-identifier "$SNAPSHOT_ID"
# Check backup completion
#TODO
sleep 10
aws --region "$RDS_REGION" \
rds describe-db-cluster-snapshots \
--db-cluster-snapshot-identifier "$SNAPSHOT_ID" \
| jq '.DBClusterSnapshots[0].Status'
# Migrate DB
runTask "$TASK_OVERRIDE_JSON_MIGRATE_DB"
# Check migration completion
#TODO
sleep 5
# Collect static files
runTask "$TASK_OVERRIDE_JSON_COLLECT_STATIC"
# Check collect static completion
#TODO
sleep 5
# Redeploy ECS service
aws --region "$ECS_REGION" ecs update-service --cluster "$ECS_CLUSTER" --service "$ECS_SERVICE" --force-new-deployment
# Check service deployment completion
#TODO