Skip to content

Commit ee6bd4c

Browse files
fix: Update license_usage_offline.sh (#1014)
* Update license_usage_offline.sh updated license script * fix: latest script * fix: Update static/scripts/license_usage_offline.sh Co-authored-by: Travis Martin <[email protected]> --------- Co-authored-by: Travis Martin <[email protected]>
1 parent ca19998 commit ee6bd4c

File tree

1 file changed

+50
-49
lines changed

1 file changed

+50
-49
lines changed

static/scripts/license_usage_offline.sh

Lines changed: 50 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,26 @@
44
set -euo pipefail
55
IFS=$'\n\t'
66

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+
727
# Function to display usage
828
usage() {
929
echo "Usage: $0 -j <JWT file> -i <NIM IP> -u <username> -p <password> -s <initial|telemetry>"
@@ -44,7 +64,6 @@ if [ ! -d "/tmp" ]; then
4464
mkdir -p /tmp || { echo "Failed to create /tmp directory. Exiting."; exit 1; }
4565
fi
4666

47-
4867
# Read JWT contents
4968
if [ ! -f "$JWT_FILE" ]; then
5069
echo -e "JWT file '$JWT_FILE' not found.$" >&2
@@ -84,25 +103,29 @@ is_ipv4() {
84103
return 1
85104
fi
86105
}
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)
87113

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"
90117
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
98122

99123
# NGINX Instance Manager Version check
100124
VERSION_JSON=$(curl -sk -X GET "https://$NIM_IP/api/platform/v1/modules/versions" \
101125
--header "Content-Type: application/json" \
102126
--header "Authorization: Basic $AUTH_HEADER")
103127
NIM_VER=$(echo "$VERSION_JSON" | sed -E 's/.*"nim"[ \t]*:[ \t]*"([0-9]+\.[0-9]+)(\.[0-9]+)?".*/\1/')
104128
echo "Current version of NGINX Instance Manager is $NIM_VER"
105-
JWT_CONTENT=$(<"$JWT_FILE")
106129

107130
# Construct JSON payload
108131
JSON_PAYLOAD=$(cat <<EOF
@@ -145,16 +168,9 @@ fi
145168
ORIGIN="https://$NIM_IP"
146169
REFERER="$ORIGIN/ui/settings/license"
147170

148-
149-
150171
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')
157172
echo "Applying JWT license"
173+
sleep 5
158174
RESPONSE=$(curl -sS -k --max-time 10 -w "\n%{http_code}" -X POST "https://$NIM_IP/api/platform/v1/license?telemetry=true" \
159175
-H "Origin: $ORIGIN" \
160176
-H "Referer: $REFERER" \
@@ -165,12 +181,9 @@ if [[ "$USE_CASE" == "initial" ]]; then
165181
HTTP_BODY=$(echo "$RESPONSE" | sed '$d')
166182
HTTP_STATUS=$(echo "$RESPONSE" | tail -n1)
167183
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
170185
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."
174187
fi
175188
exit 1
176189
fi
@@ -179,12 +192,11 @@ fi
179192
if [[ "$NIM_VER" < "2.18" ]]; then
180193
echo "NGINX Instance Manager version $NIM_VER is not supported by this script. Please use NGINX Instance Manager 2.18 or later"
181194
exit 1
182-
183195
elif [[ "$NIM_VER" == "2.18" ]] || [[ "$NIM_VER" == "2.19" ]]; then
184196
echo "NGINX Instance Manager version $NIM_VER detected."
185197
ORIGIN="https://$NIM_IP"
186198

187-
# Send the PUT request and separate body and status code
199+
# Send the PUT request and separate body and status code
188200
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" \
189201
--header "Origin: $ORIGIN" \
190202
--header "Referer: https://$NIM_IP/ui/settings/license" \
@@ -223,7 +235,6 @@ elif [[ "$NIM_VER" == "2.18" ]] || [[ "$NIM_VER" == "2.19" ]]; then
223235
fi
224236

225237
if [[ "$USE_CASE" != "telemetry" ]]; then
226-
227238
RESPONSE=$(curl -sS -k --max-time 10 -w "\n%{http_code}" -X POST "https://$NIM_IP/api/platform/v1/license?telemetry=true" \
228239
-H "Origin: $ORIGIN" \
229240
-H "Referer: $REFERER" \
@@ -237,8 +248,7 @@ if [[ "$USE_CASE" != "telemetry" ]]; then
237248
echo -e "License applied successfully in DISCONNECTED mode."
238249
fi
239250

240-
241-
251+
sleep 5
242252
# Continue with further steps for version >= 2.20...
243253
echo "Executing telemetry tasks "
244254
# Step 2: Download the usage report
@@ -263,27 +273,25 @@ if [[ "$NIM_VER" == "2.18" ]] || [[ "$NIM_VER" == "2.19" ]]; then
263273
--header 'accept: */*' \
264274
--header 'authorization: Basic $AUTH_HEADER' \
265275
--output \"$report_save_path\""
276+
266277
if [ "$USE_CASE" == "telemetry" ]; then
267278
echo "Running telemetry stage: "
268-
269279
# Run the saved command and store the response
270280
response=$(eval $prepare_usage_command)
271-
272281
sleep 2
273282
# 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
275284
echo -e "Success: Report generation is in progress."
276285
else
277286
echo -e "Failure: Report generation not in progress or unexpected response."
278287
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
287295
fi
288296
else
289297
# Perform the request and capture the status code and output
@@ -296,24 +304,19 @@ else
296304
# Extract the HTTP status code (last line)
297305
HTTP_STATUS=$(echo "$HTTP_RESPONSE" | tail -n1)
298306

299-
# Check the status code
307+
# Check the status code
300308
if [ "$HTTP_STATUS" -ne 200 ]; then
301309
echo -e "Failed to download usage report from NGINX Instance Manager. HTTP Status Code: $HTTP_STATUS" >&2
302310
echo "Please verify that NGINX Instance Manager is reachable and the credentials are correct." >&2
303311
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
306312
exit 1
307313
fi
308314
fi
309315

310316
echo -e "Usage report downloaded successfully as '/tmp/response.zip'."
311-
312317
# Step 3: Upload the usage report to F5 server
313318
echo "Uploading the usage report to F5 Licensing server"
314-
315319
TEEM_UPLOAD_URL="https://product.apis.f5.com/ee/v1/entitlements/telemetry/bulk"
316-
317320
# Capture both response body and status code
318321
UPLOAD_RESULT=$(curl -sS -w "\n%{http_code}" --location "$TEEM_UPLOAD_URL" \
319322
--header "Authorization: Bearer $JWT_CONTENT" \
@@ -338,7 +341,6 @@ fi
338341

339342
# Extract the statusLink
340343
STATUS_LINK=$(echo "$UPLOAD_BODY" | jq -r '.statusLink // empty')
341-
342344
if [ -z "$STATUS_LINK" ]; then
343345
echo -e "Failed to extract statusLink from the upload response. Response: $UPLOAD_BODY$" >&2
344346
exit 1
@@ -425,7 +427,6 @@ echo -e "Report validated successfully. All conditions met."
425427
# Step 5: Download the report from F5
426428

427429
echo "Downloading report from F5 License server..."
428-
429430
DOWNLOAD_URL="https://product.apis.f5.com/ee/v1/entitlements/telemetry/bulk/download/$STATUS_ID"
430431
DOWNLOAD_RESPONSE=$(curl -sS -w "%{http_code}" --location "$DOWNLOAD_URL" \
431432
--header "Authorization: Bearer $JWT_CONTENT" \
@@ -442,7 +443,6 @@ echo -e "Report downloaded successfully from F5 as '/tmp/response_teem.zip'."
442443

443444
# Step 6: Upload the acknowledgement report to NGINX Instance Manager
444445
echo "Uploading the license acknowledgement to NGINX Instance Manager..."
445-
446446
UPLOAD_URL="https://$NIM_IP/api/platform/v1/report/upload"
447447
UPLOAD_RESPONSE=$(curl -k -sS --location "$UPLOAD_URL" \
448448
--header "Authorization: Basic $AUTH_HEADER" \
@@ -468,3 +468,4 @@ if [ "$UPLOAD_MESSAGE" != "Report uploaded successfully." ] || [ "$HTTP_STATUS"
468468
fi
469469
echo -e "Acknowledgement uploaded successfully to NGINX Instance Manager."
470470

471+

0 commit comments

Comments
 (0)