-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustodian
executable file
·193 lines (158 loc) · 4.82 KB
/
custodian
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env bash
################################################################################
# Sanity
################################################################################
set -o errexit
set -o nounset
set -o pipefail
################################################################################
# DEBUG
################################################################################
if [[ "${SCRIPT_DBG:-0}" == "1" ]]; then
set -x
fi
################################################################################
# Globals
################################################################################
# shellcheck disable=SC2155
declare -r SCRIPT_NAME="$(basename "$0")"
declare WATCH_TCP="${WATCH_TCP:-}"
declare WATCH_IDLE="${WATCH_IDLE:-}"
declare DNS_ZONE_ID="${DNS_ZONE_ID:-}"
declare DNS_RECORD="${DNS_RECORD:-}"
declare SNS_TOPIC_ARN="${SNS_TOPIC_ARN:-}"
declare FINISHED=false
################################################################################
# Helpers
################################################################################
function io::print_help() {
printf '%s\n' "Ben's Terraform AWS Fargate on Demand Custodian"
printf 'Usage: %s [-h|--help] [options]\n' "$(basename "$0")"
printf '\t%s\n' "-h, --help: Prints help"
printf '\t%s\n' ""
printf '\n%s\n' "Watch Options"
printf '\t%s\n' "--watch-tcp (WATCH_TCP) TCP Port to watch using a iptables rule"
printf '\t%s\n' "--watch-idle (WATCH_IDLE) Number of seconds to remain idle"
printf '\t%s\n' ""
printf '\n%s\n' "Required Options"
printf '\t%s\n' "--dns-zone-id (DNS_ZONE_ID) Route 53 Zone ID containing the record to update"
printf '\t%s\n' "--dns-record (DNS_RECORD) Route 53 record to update"
printf '\t%s\n' ""
printf '\n%s\n' "Options"
printf '\t%s\n' "--topic (SNS_TOPIC_ARN) SNS Topic to send events, if not provided no events will be emitted"
printf '\t%s\n' ""
}
function io::info() {
echo "[${SCRIPT_NAME}] INFO $*"
}
function io::die() {
local msg="${1}"
local ret="${2:-1}"
local print_help="${3:-}"
if [[ ${print_help} == "print help" ]]; then
io::print_help >&2
fi
echo "[${SCRIPT_NAME}] ERROR ${msg}" >&2
exit "${ret}"
}
function io::die_missing_value() {
local key="${1}"
io::die "missing value for argument '${key}'"
}
################################################################################
# Trap
################################################################################
function sigterm() {
rv=$?
io::info "Terminating..."
if [[ "${FINISHED}" = false ]]; then
io::info "Stopping task..."
./task-reaper
if [[ -n "${SNS_TOPIC_ARN}" ]]; then
io::info "Emitting stop event..."
./event-emitter --type 'stop' --topic "${SNS_TOPIC_ARN}"
fi
fi
io::info "... done"
exit "${rv}"
}
trap sigterm SIGTERM
################################################################################
# Main
################################################################################
function main() {
while test $# -gt 0; do
key="${1}"
shift
case "${key}" in
--help | -h)
io::print_help
exit 0
;;
--watch-tcp)
if [[ $# -lt 1 ]]; then
io::die_missing_value "${key}"
fi
WATCH_TCP="${1}"
shift
;;
--watch-idle)
if [[ $# -lt 1 ]]; then
io::die_missing_value "${key}"
fi
WATCH_IDLE="${1}"
shift
;;
--dns-zone-id)
if [[ $# -lt 1 ]]; then
io::die_missing_value "${key}"
fi
DNS_ZONE_ID="${1}"
shift
;;
--dns-record)
if [[ $# -lt 1 ]]; then
io::die_missing_value "${key}"
fi
DNS_RECORD="${1}"
shift
;;
--SNS_TOPIC_ARN)
if [[ $# -lt 1 ]]; then
io::die_missing_value "${key}"
fi
SNS_TOPIC_ARN="${1}"
shift
;;
*)
io::info "unsupported option: ${key}"
;;
esac
done
io::info "Updating DNS record..."
./dns-updater --dns-zone-id "${DNS_ZONE_ID}" --dns-record "${DNS_RECORD}"
if [[ -n "${SNS_TOPIC_ARN}" ]]; then
io::info "Emitting start event..."
./event-emitter --type 'start' --topic "${SNS_TOPIC_ARN}"
fi
if [[ -n "${WATCH_TCP}" ]]; then
io::info "Starting TCP Port Watcher..."
./watcher-tcp \
--port "${WATCH_TCP}" \
--events-topic "${SNS_TOPIC_ARN}" \
--timeout "${WATCH_IDLE}"
fi
io::info "Stopping task..."
./task-reaper
if [[ -n "${SNS_TOPIC_ARN}" ]]; then
io::info "Emitting stop event..."
./event-emitter --type 'stop' --topic "${SNS_TOPIC_ARN}"
fi
# Prevent double "stop" in trap function
FINISHED=true
io::info "... done"
}
################################################################################
# Entry Point
################################################################################
main "$@"