-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.sh
executable file
·155 lines (126 loc) · 3.39 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
#! /bin/bash
set -e
cd "$(dirname "$0")"
check_env() {
file="$inf/vars/$1/config.json"
if [ ! -f "$file" ]; then
echo "Missing config file $file; run init?" 1>&2
exit 1
fi
}
run_ansible() {
env="$1"
task="$2"
shift 2
check_env "$env"
ansible-playbook \
--inventory "$inf/vars/$env/inventory.ini" \
--extra-vars="envname=$env" \
--extra-vars="@inf/vars/$env/config.json" \
"$inf/ansible/$task.yml" \
"$@"
}
run_delete() {
tfs="$inf/vars/$1/state.tfstate"
if [ -f "$tfs" ] && [ $(wc -c < "$tfs") -ge 200 ]; then
echo "Terraform state is not cleaned up; run teardown first." 1>&2
exit 1
fi
rm -r "$inf/vars/$1"
}
run_init() {
env="$1"
shift 1
if [ -d "$inf/vars/$env" ]; then
echo "Environment $env already exists." 1>&2
exit 1
fi
mkdir -p "$inf/vars/$env"
cat <<EOF > "$inf/vars/$env/config.json"
{
"aws_region": "us-west-1",
"aws_instance": "t3.micro",
"aws_access_key": "YOUR AWS ACCESS KEY",
"aws_secret_key": "YOUR AWS SECRET KEY",
"aws_keypair": "YOUR AWS KEYPAIR NAME",
"sql_database": "decompetition",
"sql_username": "deco",
"sql_password": "$(head -c 27 /dev/urandom | base64 | tr +/ xl)",
"app_servers": 2,
"app_secret": "$(head -c 108 /dev/urandom | base64 | tr +/ xl)",
"app_start_time": "YYYY-MM-DD HH:MM",
"app_end_time": "YYYY-MM-DD HH:MM",
"challenge_dir": "/ABSOLUTE/PATH/TO/CHALLENGE/DIR",
"containers": [
"decompetition/builder-2021"
]
}
EOF
echo "Config file created at inf/vars/$env/config.json"
echo "Edit this file and add your secrets!"
}
run_ssh() {
env="$1"
shift 1
ssh -F "$inf/vars/$env/ssh_config" "$@"
}
run_terraform() {
if [ ! -d "$inf/terraform/.terraform" ]; then
echo "Initializing Terraform..."
terraform -chdir="$inf/terraform" init
fi
env="$1"
task="$2"
shift 2
check_env "$env"
terraform -chdir="$inf/terraform" "$task" \
-state="$inf/vars/$env/state.tfstate" \
-var-file="$inf/vars/$env/config.json" \
-var="envname=$env" \
"$@"
}
usage() {
cat <<EOF 1>&2
USAGE: $0 task env [extra-args]
Available tasks (S=shell; T=terraform; A=ansible):
init S Create config files for a new environment.
provision T Create or update the AWS infrastructure.
configure A Configure the AWS machines and deploy the challenges and server.
update A Update all APT packages.
prune A Clean up unused Docker images.
growfs A Resize the root filesystem.
ssh S Connect to a host via SSH.
teardown T Delete the AWS infrastructure.
delete S Delete the config files.
EOF
}
if [ $# -lt 2 ]; then
usage
exit 1
fi
task="$1"
env="$2"
inf="$(cd "$(dirname "$0")"; pwd)/inf"
shift 2
if echo "$env" | grep -qE '^[[:alnum:]]+$'; then
: All good, nothing to do.
else
echo "Invalid environment name: $env" 1>&2
echo "Environment names must be alphanumeric." 1>&2
exit 1
fi
case "$task" in
init) run_init "$env" ;;
delete) run_delete "$env" ;;
ssh) run_ssh "$env" "$@" ;;
provision) run_terraform "$env" apply "$@" ;;
teardown) run_terraform "$env" destroy "$@" ;;
configure) run_ansible "$env" "$task" "$@" ;;
growfs) run_ansible "$env" "$task" "$@" ;;
redeploy) run_ansible "$env" "$task" "$@" ;;
update) run_ansible "$env" "$task" "$@" ;;
*)
usage
exit 1
;;
esac