This repository has been archived by the owner on Apr 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
deploy
executable file
·92 lines (78 loc) · 4.14 KB
/
deploy
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
#!/usr/bin/env bash
set -e
cd "$(git rev-parse --show-toplevel)"
if [ "$2" = "" ]; then
echo "Usage ./bin/deploy [subdomain] [domain]"
exit 1
fi
SUBDOMAIN="$1"
DOMAIN="$2"
# Create host NAME combining git commit hash+subdomain+domain.
COMMIT=$(git rev-parse --short HEAD)
NAME="${COMMIT}-${SUBDOMAIN}.${DOMAIN}"
echo "[deploy] Attempting to create host ${NAME}"
TAG_PREFIX="${SUBDOMAIN}.${DOMAIN}"
AWS='docker run --rm -e AWS_ACCESS_KEY_ID -e AWS_SECRET_ACCESS_KEY -e AWS_VPC_ID -e AWS_DEFAULT_REGION -e AWS_ZONE -e AWS_SUBNET_ID anigeo/awscli'
# Provision a candidate AWS Docker machine.
docker-machine create "${NAME}" --driver amazonec2
echo "[deploy] Candidate Docker machine created. Waiting a few seconds before getting the IP and ID"
sleep 10
IP=$(docker-machine ip "${NAME}" 2>&1)
INSTANCE_ID=$(docker-machine inspect --format='{{.Driver.InstanceId}}' "${NAME}" 2>&1)
echo "[deploy] Machine created at IP=${IP}, INSTANCE_ID=${INSTANCE_ID}."
echo "[deploy] Setting tag to 'candidate' for ${INSTANCE_ID}."
${AWS} ec2 create-tags --resources "${INSTANCE_ID}" --tags "Key=Status,Value=${TAG_PREFIX}-candidate"
# Open port 80.
${AWS} ec2 revoke-security-group-ingress --group-name docker-machine --protocol tcp --port 80 --cidr 0.0.0.0/0
${AWS} ec2 authorize-security-group-ingress --group-name docker-machine --protocol tcp --port 80 --cidr 0.0.0.0/0
# Deploy specific commit/tag to host.
docker-machine ssh "${NAME}" "sudo git clone https://github.com/CivicActions/agile-california.git ${PWD} && cd ${PWD} && sudo git checkout \"${COMMIT}\""
# Change directory permissions to non-privileged user.
docker-machine ssh "${NAME}" "sudo chown -R \${USER}:\${GROUP} ${PWD}"
# Publish web container port in Docker Compose.
git checkout docker-compose.yml
sed -i'' -e "s/^web:/web:\n ports:\n - 80:80/" docker-compose.yml
# Enable (in subshell) Docker client connection to remote Docker host.
(
eval "$(docker-machine env "${NAME}")"
# Enable Bowline
source bin/activate
# Build site on remote host.
build
composer install
# Initial install (later on this will also need to manage migration between old and new instances).
initial-install
# Run tests.
run-tests
if [ $? -eq 0 ]; then
echo "[deploy] Tests PASS, activating candidate build."
else
echo "[deploy] Tests FAIL, tagging 'candidate' as 'failed' - please review fails, fix and manually remove failed candidate before redeploying."
${AWS} ec2 create-tags --resources "${INSTANCE_ID}" --tags "Key=Status,Value=${TAG_PREFIX}-failed"
echo "[deploy] Aborting build."
exit 1
fi
)
# Clean up - remove instances tagged as "old".
OLD=$(${AWS} ec2 describe-instances --output text --filters "Name=tag:Status,Values=${TAG_PREFIX}-old" | grep -P "TAGS\tName" | awk '{print $3}')
if [ ! -z "${OLD}" ]; then
echo "[deploy] Removing instances tagged as 'old': ${OLD}"
docker-machine rm -y "${OLD}"
fi
# Tag existing "active" instance as "old", but leave running so we can roll back if needed.
INITIAL_ACTIVE=$(${AWS} ec2 describe-instances --output text --filters "Name=tag:Status,Values=${TAG_PREFIX}-active" | grep 'INSTANCES' | awk '{print $7}')
if [ ! -z "${INITIAL_ACTIVE}" ]; then
echo "[deploy] Setting tag from 'active' to 'old' for ${INITIAL_ACTIVE}."
${AWS} ec2 create-tags --resources "${INITIAL_ACTIVE}" --tags "Key=Status,Value=${TAG_PREFIX}-old"
fi
# Tag candidate instance as active.
echo "[deploy] Setting tag from 'candidate' to 'active' for ${INSTANCE_ID}."
${AWS} ec2 create-tags --resources "${INSTANCE_ID}" --tags "Key=Status,Value=${TAG_PREFIX}-active"
# Setup DNS and CDN.
docker run -it --rm -e TOKEN="${CLOUDFLARE_TOKEN}" -e EMAIL="${CLOUDFLARE_EMAIL}" -e DOMAIN="${DOMAIN}" programmerq/cfcli cfcli removerecord "${SUBDOMAIN}"
docker run -it --rm -e TOKEN="${CLOUDFLARE_TOKEN}" -e EMAIL="${CLOUDFLARE_EMAIL}" -e DOMAIN="${DOMAIN}" programmerq/cfcli cfcli -a -t A addrecord "${SUBDOMAIN}" "${IP}"
echo "[deploy] Deployed to https://${SUBDOMAIN}.${DOMAIN}"
# Purge CDN cache
docker run -it --rm -e TOKEN="${CLOUDFLARE_TOKEN}" -e EMAIL="${CLOUDFLARE_EMAIL}" -e DOMAIN="${DOMAIN}" programmerq/cfcli cfcli purgecache
echo "[deploy] Purging CloudFlare cache for ${DOMAIN}."
echo "[deploy] Deploy success."