An alternative method is to make an API call to HQ to create a new environment.
create_new_env.sh
#!/bin/bash
# Variables for URLs and endpoints
URL_LOGIN="http://hq:9991/api/v1/login"
URL_DATA="http://hq:9991/api/v1/environments"
# Check if the file exists and is not empty
if grep -q '^agent' "$FILE"; then
echo "[INFO] Environment has already been created -> Proceeding with the Agent creation"
exit 0
else
# Step 1: Perform the first request to get the session cookie
# Replace the login payload and headers as per your requirements
response=$(curl -s -D - \
-X POST \
-H 'Accept: application/json' \
-H "Content-Type: application/json" \
-d '{"username":"admin", "password":"admin"}' \
$URL_LOGIN)
# Step 2: Extract the session cookie from the response headers
# Assuming the session cookie is called "session_id" in the response
cookie=$(echo "$response" | grep -i 'Set-Cookie' | grep -o 'session_id=[^;]*')
if [ -z "$cookie" ]; then
echo "[ERROR] Failed to retrieve session cookie"
exit 1
fi
echo "[INFO] Session Cookie: $cookie"
# Step 3: Use the session cookie to make another request
response_body=$(curl -s \
-X POST \
-H "Cookie: $cookie" \
-H "Content-Type: application/json" \
-d '{ "name": "demo-environment", "tier": "development" }' \
$URL_DATA)