-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbootstrap.sh
executable file
·154 lines (133 loc) · 3.64 KB
/
bootstrap.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
#!/bin/bash -eu
set -o pipefail
IFS=$'\t\n\r'
readonly COMPOSE_DIR=$( cd $(dirname $0); pwd )
main() {
create-droplet
create-droplet-floating-ip
create-fqdn
create-wildcard-cname
}
create-environment() {
source $COMPOSE_DIR/.env
[ -n "${DROPLET:-}" ] || { echo ".env missing DROPLET" ; FATAL=1; }
[ -n "${TOKEN_FILE:-}" ] || { echo ".env missing TOKEN_FILE" ; FATAL=1; }
[ -n "${REGION:-}" ] || { echo ".env missing REGION" ; FATAL=1; }
[ -z "${FATAL:-}" ] || exit 1
}
create-droplet() {
droplet-exists || {
docker-machine \
create --driver digitalocean\
--digitalocean-access-token=$(token) \
--digitalocean-region=$REGION \
$DROPLET
}
}
create-droplet-floating-ip() {
local IP=$(droplet-floating-ip)
[ -n "$IP" ] || {
local ID=$(droplet-list | awk "/$DROPLET\$/ {print \$1}")
do-api -sS -X POST \
-d "{\"droplet_id\":$ID}" \
"https://api.digitalocean.com/v2/floating_ips"
}
}
create-fqdn() {
fqdn-exists || {
do-api -X POST \
-d "{\"name\":\"${DROPLET}\",\"ip_address\":$(droplet-floating-ip)}" \
"https://api.digitalocean.com/v2/domains"
}
}
create-wildcard-cname() {
wildcard-cname-exists || {
do-api -X POST \
-d "{\"type\":\"CNAME\",\"name\":\"*\",\"data\":\"@\",\"priority\":null,\"port\":null,\"weight\":null}" \
"https://api.digitalocean.com/v2/domains/${DROPLET}/records"
}
}
droplet-list-json() {
do-api -sS -X GET "https://api.digitalocean.com/v2/droplets?page=1&per_page=10"
}
droplet-list() {
# memoize to self-regulate DO rate-limits
if [ -z "${DROPLET_LIST:-}" ]; then
local JSON="$(droplet-list-json)"
echo $JSON
<<<"$JSON" jq -r '.droplets[]'
readonly DROPLET_LIST="$(<<<"$JSON" jq -r '.droplets[] | .id, .name' \
| paste - -)"
fi
echo -e $DROPLET_LIST
}
droplet-exists() {
droplet-list | grep -q "$DROPLET\$"
}
droplet-floating-ip() {
do-api -sS -X GET "https://api.digitalocean.com/v2/floating_ips?page=1&per_page=20" \
| jq ".floating_ips[] | select(.droplet.name == \"$DROPLET\") | .ip"
}
fqdn-exists() {
# not_found is in the error payload if the domain doesn't exist
# so this code is a double-negative: true if we don't find "not_found"
do-api -sS -X GET "https://api.digitalocean.com/v2/domains/${DROPLET}" \
| grep -qv not_found
}
wildcard-cname-exists() {
list-cnames \
| grep -q '^*'
}
list-cnames() {
do-api -sS -X GET \
"https://api.digitalocean.com/v2/domains/${DROPLET}/records" \
| jq -r '.domain_records[] | select(.type == "CNAME") | .name, .data' \
| paste - -
}
create-cname() {
local SUBDOMAIN="${1:-*}"
cname-exists $SUBDOMAIN || {
local URL="https://api.digitalocean.com/v2/domains/${DROPLET}/records"
do-api -X POST -d@- $URL <<EOF
{
"type" : "CNAME",
"name" : "$SUBDOMAIN",
"data" : "@",
"priority" : null,
"port" : null,
"weight" : null
}
EOF
}
}
cname-exists() {
local SUBDOMAIN="${1:-UNSPECIFIED}"
list-cnames | grep -q "^$SUBDOMAIN"
}
do-api() {
curl \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $(token)" \
$@
}
token() {
cat $TOKEN_FILE
}
case ${1:-} in
list-cnames \
| create-cname \
| create-droplet \
| create-droplet-floating-ip \
| create-fqdn \
| create-wildcard-cname \
| wildcard-cname-exists \
)
readonly CMD=${1}
shift
;;
*)
readonly CMD=main
;;
esac
create-environment
$CMD $@