4
4
set -euo pipefail
5
5
IFS=$' \n\t '
6
6
7
+ # Debug mode
8
+ if [[ " ${DEBUG:- false} " == " true" ]]; then
9
+ set -x # Enable command tracing
10
+ echo " Debug mode enabled"
11
+ echo " Running in directory: $( pwd) "
12
+ echo " Script arguments: $* "
13
+ env | grep -E ' JWT_FILE|NIM_IP|USERNAME|PASSWORD|USE_CASE'
14
+ fi
15
+
16
+ # Set timeouts for operations
17
+ CURL_TIMEOUT=${CURL_TIMEOUT:- 30}
18
+ API_POLL_TIMEOUT=${API_POLL_TIMEOUT:- 60}
19
+
20
+ # Function to log with timestamp
21
+ log () {
22
+ echo " [$( date ' +%Y-%m-%d %H:%M:%S' ) ] $* "
23
+ }
24
+
25
+ log " Script started"
26
+
7
27
# Function to display usage
8
28
usage () {
9
29
echo " Usage: $0 -j <JWT file> -i <NIM IP> -u <username> -p <password> -s <initial|telemetry>"
@@ -44,7 +64,6 @@ if [ ! -d "/tmp" ]; then
44
64
mkdir -p /tmp || { echo " Failed to create /tmp directory. Exiting." ; exit 1; }
45
65
fi
46
66
47
-
48
67
# Read JWT contents
49
68
if [ ! -f " $JWT_FILE " ]; then
50
69
echo -e " JWT file '$JWT_FILE ' not found.$" >&2
@@ -84,25 +103,29 @@ is_ipv4() {
84
103
return 1
85
104
fi
86
105
}
106
+ echo " Checking connectivity to NGINX Instance Manager using Curl ..."
107
+ if ! curl -sk --output /dev/null --silent --fail --max-time $CURL_TIMEOUT " https://$NIM_IP " ; then
108
+ echo -e " The NGINX Instance Manager UI is not reachable on $NIM_IP "
109
+ exit 1
110
+ fi
111
+ echo " Checking connectivity to F5 licensing server..."
112
+ SERVER_RESPONSE=$( curl -v --max-time $CURL_TIMEOUT https://product.apis.f5.com 2>&1 )
87
113
88
- if is_ipv4 " $NIM_IP " ; then
89
- check_ping " $NIM_IP "
114
+ # Check if the server is reachable by verifying connection was established
115
+ if echo " $SERVER_RESPONSE " | grep -q " Connected to product.apis.f5.com" && echo " $SERVER_RESPONSE " | grep -q " server accepted" ; then
116
+ echo -e " The licensing server is reachable on product.apis.f5.com"
90
117
else
91
- echo " Checking connectivity to NGINX Instance Manager using Curl ..."
92
- if ! curl -sk --output /dev/null --silent --fail " https://$NIM_IP " ; then
93
- echo -e " The NGINX Instance Manager UI is not reachable on $NIM_IP "
94
- exit 1
95
- fi
96
- fi
97
- check_ping " product.apis.f5.com"
118
+ echo -e " The licensing server is not reachable on product.apis.f5.com"
119
+ echo -e " Connection details: $SERVER_RESPONSE "
120
+ exit 1
121
+ fi
98
122
99
123
# NGINX Instance Manager Version check
100
124
VERSION_JSON=$( curl -sk -X GET " https://$NIM_IP /api/platform/v1/modules/versions" \
101
125
--header " Content-Type: application/json" \
102
126
--header " Authorization: Basic $AUTH_HEADER " )
103
127
NIM_VER=$( echo " $VERSION_JSON " | sed -E ' s/.*"nim"[ \t]*:[ \t]*"([0-9]+\.[0-9]+)(\.[0-9]+)?".*/\1/' )
104
128
echo " Current version of NGINX Instance Manager is $NIM_VER "
105
- JWT_CONTENT=$( < " $JWT_FILE " )
106
129
107
130
# Construct JSON payload
108
131
JSON_PAYLOAD=$( cat << EOF
145
168
ORIGIN=" https://$NIM_IP "
146
169
REFERER=" $ORIGIN /ui/settings/license"
147
170
148
-
149
-
150
171
if [[ " $USE_CASE " == " initial" ]]; then
151
- # Read JWT content
152
- if [ ! -f " $JWT_FILE " ]; then
153
- echo -e " JWT file '$JWT_FILE ' not found." >&2
154
- exit 1
155
- fi
156
- # Step 1: Apply JWT license (only if use case is 'initial' or 'intial_only')
157
172
echo " Applying JWT license"
173
+ sleep 5
158
174
RESPONSE=$( curl -sS -k --max-time 10 -w " \n%{http_code}" -X POST " https://$NIM_IP /api/platform/v1/license?telemetry=true" \
159
175
-H " Origin: $ORIGIN " \
160
176
-H " Referer: $REFERER " \
@@ -165,12 +181,9 @@ if [[ "$USE_CASE" == "initial" ]]; then
165
181
HTTP_BODY=$( echo " $RESPONSE " | sed ' $d' )
166
182
HTTP_STATUS=$( echo " $RESPONSE " | tail -n1)
167
183
if [ " $HTTP_STATUS " -ne 202 ]; then
168
- echo -e " HTTP request failed with status code $HTTP_STATUS .
169
- Response: $HTTP_BODY $" >&2
184
+ echo -e " HTTP request failed with status code $HTTP_STATUS .\nResponse: $HTTP_BODY $" >&2
170
185
if echo " $HTTP_BODY " | jq -r ' .message' | grep -q " failed to register token. already registered" ; then
171
- echo -e " NGINX Instance Manager already registered and licensed.
172
- If needed, terminate the current license manually in the NGINX Instance Manager UI and re-run the script with the correct license.
173
- https://docs.nginx.com/nginx-instance-manager/disconnected/add-license-disconnected-deployment/"
186
+ echo -e " NGINX Instance Manager is already licensed.\nTo use a different license, remove the current one in the NGINX Instance Manager UI, then re-run the script.\nSee https://docs.nginx.com/nginx-instance-manager/disconnected/add-license-disconnected-deployment/ for details."
174
187
fi
175
188
exit 1
176
189
fi
179
192
if [[ " $NIM_VER " < " 2.18" ]]; then
180
193
echo " NGINX Instance Manager version $NIM_VER is not supported by this script. Please use NGINX Instance Manager 2.18 or later"
181
194
exit 1
182
-
183
195
elif [[ " $NIM_VER " == " 2.18" ]] || [[ " $NIM_VER " == " 2.19" ]]; then
184
196
echo " NGINX Instance Manager version $NIM_VER detected."
185
197
ORIGIN=" https://$NIM_IP "
186
198
187
- # Send the PUT request and separate body and status code
199
+ # Send the PUT request and separate body and status code
188
200
PUT_RESPONSE_CODE=$( curl -k -s -w " %{http_code}" -o /tmp/put_response.json --location --request PUT " https://$NIM_IP /api/platform/v1/license?telemetry=true" \
189
201
--header " Origin: $ORIGIN " \
190
202
--header " Referer: https://$NIM_IP /ui/settings/license" \
@@ -223,7 +235,6 @@ elif [[ "$NIM_VER" == "2.18" ]] || [[ "$NIM_VER" == "2.19" ]]; then
223
235
fi
224
236
225
237
if [[ " $USE_CASE " != " telemetry" ]]; then
226
-
227
238
RESPONSE=$( curl -sS -k --max-time 10 -w " \n%{http_code}" -X POST " https://$NIM_IP /api/platform/v1/license?telemetry=true" \
228
239
-H " Origin: $ORIGIN " \
229
240
-H " Referer: $REFERER " \
@@ -237,8 +248,7 @@ if [[ "$USE_CASE" != "telemetry" ]]; then
237
248
echo -e " License applied successfully in DISCONNECTED mode."
238
249
fi
239
250
240
-
241
-
251
+ sleep 5
242
252
# Continue with further steps for version >= 2.20...
243
253
echo " Executing telemetry tasks "
244
254
# Step 2: Download the usage report
@@ -263,27 +273,25 @@ if [[ "$NIM_VER" == "2.18" ]] || [[ "$NIM_VER" == "2.19" ]]; then
263
273
--header 'accept: */*' \
264
274
--header 'authorization: Basic $AUTH_HEADER ' \
265
275
--output \" $report_save_path \" "
276
+
266
277
if [ " $USE_CASE " == " telemetry" ]; then
267
278
echo " Running telemetry stage: "
268
-
269
279
# Run the saved command and store the response
270
280
response=$( eval $prepare_usage_command )
271
-
272
281
sleep 2
273
282
# Validate if the response contains "Report generation in progress"
274
- if echo " $response " | grep -q ' "telemetry":"Report generation in progress"' ; then
283
+ if echo " $response " | grep -q ' "telemetry":"Report generation in progress"' ; then
275
284
echo -e " Success: Report generation is in progress."
276
285
else
277
286
echo -e " Failure: Report generation not in progress or unexpected response."
278
287
exit 1
279
- fi
280
-
281
- echo " Running command: $download_usage_command "
282
- eval $download_usage_command
283
- else
284
- echo " Running command: $download_usage_command "
285
- eval $download_usage_command
286
- fi
288
+ fi
289
+ echo " Running command: $download_usage_command "
290
+ eval $download_usage_command
291
+ else
292
+ echo " Running command: $download_usage_command "
293
+ eval $download_usage_command
294
+ fi
287
295
fi
288
296
else
289
297
# Perform the request and capture the status code and output
@@ -296,24 +304,19 @@ else
296
304
# Extract the HTTP status code (last line)
297
305
HTTP_STATUS=$( echo " $HTTP_RESPONSE " | tail -n1)
298
306
299
- # Check the status code
307
+ # Check the status code
300
308
if [ " $HTTP_STATUS " -ne 200 ]; then
301
309
echo -e " Failed to download usage report from NGINX Instance Manager. HTTP Status Code: $HTTP_STATUS " >&2
302
310
echo " Please verify that NGINX Instance Manager is reachable and the credentials are correct." >&2
303
311
echo " (or) Verify that NGINX Instance Manager is licensed before using the 'telemetry' flag (run it with 'initial' first)."
304
- # Optionally, remove the partial or corrupt file
305
- rm -f /tmp/response.zip
306
312
exit 1
307
313
fi
308
314
fi
309
315
310
316
echo -e " Usage report downloaded successfully as '/tmp/response.zip'."
311
-
312
317
# Step 3: Upload the usage report to F5 server
313
318
echo " Uploading the usage report to F5 Licensing server"
314
-
315
319
TEEM_UPLOAD_URL=" https://product.apis.f5.com/ee/v1/entitlements/telemetry/bulk"
316
-
317
320
# Capture both response body and status code
318
321
UPLOAD_RESULT=$( curl -sS -w " \n%{http_code}" --location " $TEEM_UPLOAD_URL " \
319
322
--header " Authorization: Bearer $JWT_CONTENT " \
338
341
339
342
# Extract the statusLink
340
343
STATUS_LINK=$( echo " $UPLOAD_BODY " | jq -r ' .statusLink // empty' )
341
-
342
344
if [ -z " $STATUS_LINK " ]; then
343
345
echo -e " Failed to extract statusLink from the upload response. Response: $UPLOAD_BODY $" >&2
344
346
exit 1
@@ -425,7 +427,6 @@ echo -e "Report validated successfully. All conditions met."
425
427
# Step 5: Download the report from F5
426
428
427
429
echo " Downloading report from F5 License server..."
428
-
429
430
DOWNLOAD_URL=" https://product.apis.f5.com/ee/v1/entitlements/telemetry/bulk/download/$STATUS_ID "
430
431
DOWNLOAD_RESPONSE=$( curl -sS -w " %{http_code}" --location " $DOWNLOAD_URL " \
431
432
--header " Authorization: Bearer $JWT_CONTENT " \
@@ -442,7 +443,6 @@ echo -e "Report downloaded successfully from F5 as '/tmp/response_teem.zip'."
442
443
443
444
# Step 6: Upload the acknowledgement report to NGINX Instance Manager
444
445
echo " Uploading the license acknowledgement to NGINX Instance Manager..."
445
-
446
446
UPLOAD_URL=" https://$NIM_IP /api/platform/v1/report/upload"
447
447
UPLOAD_RESPONSE=$( curl -k -sS --location " $UPLOAD_URL " \
448
448
--header " Authorization: Basic $AUTH_HEADER " \
@@ -468,3 +468,4 @@ if [ "$UPLOAD_MESSAGE" != "Report uploaded successfully." ] || [ "$HTTP_STATUS"
468
468
fi
469
469
echo -e " Acknowledgement uploaded successfully to NGINX Instance Manager."
470
470
471
+
0 commit comments