-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcall-http-trigger.sh
More file actions
executable file
·168 lines (135 loc) · 5.35 KB
/
call-http-trigger.sh
File metadata and controls
executable file
·168 lines (135 loc) · 5.35 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
#!/bin/bash
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"
}
call_http_trigger_function() {
# Get the function app name
echo "Getting function app name..."
function_app_name=$(az functionapp list --query '[0].name' --output tsv)
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 '[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
# 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")
greeting_count=10
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 mapped host port for function app HTTP trigger (internal port 80)
echo "Getting the host port mapped to internal port 80 in container [$container_name]..."
host_port=$(get_docker_container_port_mapping "$container_name" "80")
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
# Retrieve LocalStack proxy port
proxy_port=$(curl http://localhost:4566/_localstack/proxy -s | jq '.proxy_port')
if [ -n "$proxy_port" ]; then
# Call the GetGreetings HTTP trigger function to retrieve the last greetings via emulator
echo "Calling HTTP trigger function to retrieve the last [$greeting_count] greetings via emulator..."
curl --proxy "http://localhost:$proxy_port/" -s "http://$function_host_name/api/greetings?count=$greeting_count" | jq
else
echo "Failed to retrieve LocalStack proxy port"
fi
if [ -n "$container_ip" ]; then
# Call the GetGreetings HTTP trigger function to retrieve the last greetings via the container IP address
echo "Calling HTTP trigger function to retrieve the last [$greeting_count] greetings via container IP address [$container_ip]..."
curl -s "http://$container_ip/api/greetings?count=$greeting_count" | jq
else
echo "Failed to retrieve container IP address"
fi
if [ -n "$host_port" ]; then
# Call the GetGreetings HTTP trigger function to retrieve the last greetings via the host port
echo "Calling HTTP trigger function to retrieve the last [$greeting_count] greetings via host port [$host_port]..."
curl -s "http://localhost:$host_port/api/greetings?count=$greeting_count" | jq
else
echo "Failed to retrieve host port"
fi
}
call_http_trigger_function