Repository navigation
Expand file tree
/
Copy pathcall-http-triggers.sh
More file actions
executable file
·315 lines (266 loc) · 13.4 KB
/
Copy pathcall-http-triggers.sh
File metadata and controls
executable file
·315 lines (266 loc) · 13.4 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/bin/bash
# Usage: call-http-triggers.sh [function-app-name]
#
# The function app to call is the one named by the first argument, else by FUNCTION_APP_NAME, else the only function app in the
# subscription. Name it when several are deployed.
get_docker_container_name_by_prefix() {
local app_prefix="$1"
local container_name
# Check if Docker is running
if ! docker info >/dev/null 2>&1; then
echo "Error: Docker is not running" >&2
return 1
fi
echo "Looking for containers with names starting with [$app_prefix]..." >&2
# Find the container using grep
container_name=$(docker ps --format "{{.Names}}" | grep "^${app_prefix}" | head -1)
if [ -z "$container_name" ]; then
echo "Error: No running container found with name starting with [$app_prefix]" >&2
return 1
fi
echo "Found matching container [$container_name]" >&2
echo "$container_name"
}
get_docker_container_ip_address_by_name() {
local container_name="$1"
local ip_address
if [ -z "$container_name" ]; then
echo "Error: Container name is required" >&2
return 1
fi
# Get IP address
ip_address=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "$container_name")
if [ -z "$ip_address" ]; then
echo "Error: Container [$container_name] has no IP address assigned" >&2
return 1
fi
echo "$ip_address"
}
get_docker_container_port_mapping() {
local container_name="$1"
local container_port="$2"
local host_port
if [ -z "$container_name" ] || [ -z "$container_port" ]; then
echo "Error: Container name and container port are required" >&2
return 1
fi
# Get host port mapping
host_port=$(docker inspect -f "{{(index (index .NetworkSettings.Ports \"${container_port}/tcp\") 0).HostPort}}" "$container_name")
if [ -z "$host_port" ]; then
echo "Error: No host port mapping found for container [$container_name] port [$container_port]" >&2
return 1
fi
echo "$host_port"
}
get_docker_container_app_port() {
local container_name="$1"
local container_port
if [ -z "$container_name" ]; then
echo "Error: Container name is required" >&2
return 1
fi
# The emulator publishes the port the app listens on in its container, which is not necessarily 80:
# an app listens on the port its WEBSITES_PORT app setting names. Read it from the container.
container_port=$(docker inspect -f '{{range $port, $bindings := .NetworkSettings.Ports}}{{if $bindings}}{{println $port}}{{end}}{{end}}' "$container_name" | grep '/tcp$' | head -n 1)
container_port="${container_port%/tcp}"
if [ -z "$container_port" ]; then
echo "Error: Container [$container_name] publishes no TCP port" >&2
return 1
fi
echo "$container_port"
}
call_http_trigger_functions() {
# The function app to call: the one the caller named, else the only one in the sample's resource group or the
# subscription
function_app_name="$1"
if [ -z "$function_app_name" ]; then
if [ -n "$PREFIX" ]; then
# The samples runner gives every sample its own PREFIX, while the resource group of the sample before
# can still be being deleted: look only in this sample's resource group
echo "Getting the name of the only function app in the [${PREFIX}-rg] resource group..."
function_app_count=$(az functionapp list --resource-group "${PREFIX}-rg" --query "length(@)" --output tsv)
function_app_name=$(az functionapp list --resource-group "${PREFIX}-rg" --query "[0].name" --output tsv)
else
echo "Getting the name of the only function app in the subscription..."
function_app_count=$(az functionapp list --query "length(@)" --output tsv)
function_app_name=$(az functionapp list --query "[0].name" --output tsv)
fi
if [ "$function_app_count" != "1" ]; then
echo "Error: Found [${function_app_count:-0}] function apps; name the one to call: $0 <function-app-name>"
exit 1
fi
fi
if [ -n "$function_app_name" ]; then
echo "Function app [$function_app_name] successfully retrieved."
else
echo "Error: No function app found"
exit 1
fi
# Get the resource group name
echo "Getting resource group name for function app [$function_app_name]..."
resource_group_name=$(az functionapp list --query "[?name=='${function_app_name}'] | [0].resourceGroup" --output tsv)
if [ -n "$resource_group_name" ]; then
echo "Resource group [$resource_group_name] successfully retrieved."
else
echo "Error: No resource group found for function app [$function_app_name]"
exit 1
fi
# Get the the default host name of the function app
echo "Getting the default host name of the function app [$function_app_name]..."
function_host_name=$(az functionapp show \
--name "$function_app_name" \
--resource-group "$resource_group_name" \
--query 'defaultHostName' \
--output tsv)
if [ -n "$function_host_name" ]; then
echo "Function app default host name [$function_host_name] successfully retrieved."
else
echo "Error: No function app default host name found"
exit 1
fi
player_name='Leo'
game_session='1'
# Only the LocalStack emulator runs the function app in a Docker container on this machine. On Azure, call the
# functions on the default host name over HTTPS with the default function key, which their Function authorization
# level requires.
cloud_name=$(az cloud show --query name --output tsv | tr -d '\r')
if [[ $cloud_name != "LocalStack" ]]; then
# Azure reads the keys from the Functions host, which can answer with a timeout while it starts after a
# deployment: ask again until it returns the default key
local attempt function_key=""
for attempt in $(seq 1 "${RETRY_COUNT:-30}"); do
function_key=$(az functionapp keys list \
--name "$function_app_name" \
--resource-group "$resource_group_name" \
--query 'functionKeys.default' \
--output tsv 2>/dev/null | tr -d '\r')
[ -n "$function_key" ] && break
if [ "$attempt" -lt "${RETRY_COUNT:-30}" ]; then
echo "Attempt $attempt of ${RETRY_COUNT:-30}: no default function key yet for function app [$function_app_name]. Waiting [${SLEEP:-10}] seconds before retry..."
sleep "${SLEEP:-10}"
fi
done
if [ -z "$function_key" ]; then
echo "Error: No default function key found for function app [$function_app_name]"
exit 1
fi
# The games live in the memory of the function app, and its timer function creates the first one when the host
# starts, so wait until the game exists
echo "Waiting for game session [$game_session] to exist in the [$function_app_name] function app on [$cloud_name]..."
local status_code body
for attempt in $(seq 1 "${RETRY_COUNT:-30}"); do
status_code=$(curl --max-time 30 -s -o /dev/null -w "%{http_code}" -H "x-functions-key: $function_key" "https://$function_host_name/api/player/$game_session/$player_name/status")
if [[ $status_code == 200 ]]; then
echo "Game session [$game_session] exists in the [$function_app_name] function app"
break
fi
if [ "$attempt" -lt "${RETRY_COUNT:-30}" ]; then
echo "Attempt $attempt of ${RETRY_COUNT:-30}: the function app answered [$status_code]. Waiting [${SLEEP:-10}] seconds before retry..."
sleep "${SLEEP:-10}"
else
echo "Error: game session [$game_session] still doesn't exist in the [$function_app_name] function app after ${RETRY_COUNT:-30} attempts"
exit 1
fi
done
# Call the GET HTTP trigger function that returns a player status in a specified game session
echo "Calling HTTP trigger function to retrieve player [$player_name] status in game session [$game_session] via function hostname [$function_host_name]..."
if ! body=$(curl --max-time 30 -fsS -H "x-functions-key: $function_key" "https://$function_host_name/api/player/$game_session/$player_name/status"); then
echo "Error: the player status call failed"
exit 1
fi
jq . <<<"$body"
# Call the POST HTTP trigger function that returns the game session details
echo "Calling HTTP trigger function to retrieve game session [$game_session] details via function hostname [$function_host_name]..."
if ! body=$(curl --max-time 30 -fsS -X POST -H "Content-Type: application/json" -H "x-functions-key: $function_key" -d "{\"gameId\": $game_session}" "https://$function_host_name/api/game/session"); then
echo "Error: the game session call failed"
exit 1
fi
jq . <<<"$body"
return 0
fi
# Get the Docker container name
echo "Finding container name with prefix [ls-$function_app_name]..."
container_name=$(get_docker_container_name_by_prefix "ls-$function_app_name")
if [ $? -eq 0 ] && [ -n "$container_name" ]; then
echo "Container [$container_name] found successfully"
else
echo "Failed to get container name"
exit 1
fi
# Get the container IP address
echo "Getting IP address for container [$container_name]..."
container_ip=$(get_docker_container_ip_address_by_name "$container_name")
if [ $? -eq 0 ] && [ -n "$container_ip" ]; then
echo "IP address [$container_ip] retrieved successfully for container [$container_name]"
else
echo "Failed to get container IP address"
exit 1
fi
# Get the port the function app listens on in its container
echo "Getting the port the function app listens on in container [$container_name]..."
container_port=$(get_docker_container_app_port "$container_name")
if [ $? -eq 0 ] && [ -n "$container_port" ]; then
echo "Function app listens on port [$container_port] in container [$container_name]"
else
echo "Failed to get the port the function app listens on in container [$container_name]"
fi
# Get the host port mapped to it
echo "Getting the host port mapped to port [$container_port] in container [$container_name]..."
host_port=$(get_docker_container_port_mapping "$container_name" "$container_port")
if [ $? -eq 0 ] && [ -n "$host_port" ]; then
echo "Mapped host port [$host_port] retrieved successfully for container [$container_name]"
else
echo "Failed to get mapped host port for container [$container_name]"
exit 1
fi
# The games live in the memory of the function app, and its timer function creates the first one when the host
# starts. After a restart, such as the one a second deployment causes, the host waits for the lock lease of the
# previous instance to expire and starts the timer up to a minute later, so wait until the game exists.
echo "Waiting for game session [$game_session] to exist in the [$function_app_name] function app..."
local attempt status_code
for attempt in $(seq 1 "${RETRY_COUNT:-18}"); do
status_code=$(curl --max-time 10 -s -o /dev/null -w "%{http_code}" "http://$function_host_name/api/player/$game_session/$player_name/status")
if [[ $status_code == 200 ]]; then
echo "Game session [$game_session] exists in the [$function_app_name] function app"
break
fi
if [ "$attempt" -lt "${RETRY_COUNT:-18}" ]; then
echo "Attempt $attempt of ${RETRY_COUNT:-18}: the function app answered [$status_code]. Waiting [${SLEEP:-10}] seconds before retry..."
sleep "${SLEEP:-10}"
else
echo "Error: game session [$game_session] still doesn't exist in the [$function_app_name] function app after ${RETRY_COUNT:-18} attempts"
exit 1
fi
done
if [ -n "$function_host_name" ]; then
# Call the GET HTTP trigger function that returns a player status in a specified game session via the function hostname
echo "Calling HTTP trigger function to retrieve player [$player_name] status in game session [$game_session] via function hostname [$function_host_name]..."
curl --max-time 10 -s "http://$function_host_name/api/player/$game_session/$player_name/status" | jq
# Call the POST HTTP trigger function that returns the game session details via the function hostname
echo "Calling HTTP trigger function to retrieve game session [$game_session] details via function hostname [$function_host_name]..."
curl --max-time 10 -s -X POST -H "Content-Type: application/json" -d "{\"gameId\": $game_session}" "http://$function_host_name/api/game/session" | jq
else
echo "Failed to retrieve function hostname"
fi
if [ -n "$container_ip" ] && [ -n "$container_port" ]; then
# Call the GET HTTP trigger function that returns a player status in a specified game session via the container IP address
echo "Calling HTTP trigger function to retrieve player [$player_name] status in game session [$game_session] via container IP address [$container_ip]..."
curl --max-time 10 -s "http://$container_ip:$container_port/api/player/$game_session/$player_name/status" | jq
# Call the POST HTTP trigger function that returns the game session details via the container IP address
echo "Calling HTTP trigger function to retrieve game session [$game_session] details via container IP address [$container_ip]..."
curl --max-time 10 -s -X POST -H "Content-Type: application/json" -d "{\"gameId\": $game_session}" "http://$container_ip:$container_port/api/game/session" | jq
else
echo "Failed to retrieve container IP address"
fi
if [ -n "$host_port" ]; then
# Call the GET HTTP trigger function that returns a player status in a specified game session via the host port
echo "Calling HTTP trigger function to retrieve player [$player_name] status in game session [$game_session] via host port [$host_port]..."
curl --max-time 10 -s "http://localhost:$host_port/api/player/$game_session/$player_name/status" | jq
# Call the POST HTTP trigger function that returns the game session details via the host port
echo "Calling HTTP trigger function to retrieve game session [$game_session] details via host port [$host_port]..."
curl --max-time 10 -s -X POST -H "Content-Type: application/json" -d "{\"gameId\": $game_session}" "http://localhost:$host_port/api/game/session" | jq
else
echo "Failed to retrieve host port"
fi
}
call_http_trigger_functions "${1:-${FUNCTION_APP_NAME:-}}"