All pages
Powered by GitBook
1 of 1

Environment Creation

In this page we will explain how to utilise CLI for environment creation.

Prerequisites:

  • Running HQ;

  • Enough permissions to create a new Service Account;

  • Running Docker daemon.

1

  1. Go to IAM section.

  2. Click the "Service Account" Tab;

  3. Click on "New Service Account";

  4. Fill in the mandatory fields. Make sure you assign a group with enough permissions to create new environments to the service account.

  5. Copy the service account key, which will be used later;

2

Create a new environment

terminal
docker run -it lensting/lenses-cli:6-preview \
 environments create \
 --name dev-env --tier development \ 
 --address [HQ_URL] \
 --token sa_key_*

Output:

terminal
{
  "name": "dev-env",
  "display_name": "dev-env",
  "lrn": "environments:environment:dev-env",
  "id": "env_s9UdVL2uHr7btloc",
  "created_at": "2024-11-27T09:55:23.263588Z",
  "tier": "development",
  "status": {
    "agent_connected": false
  },
  "agent_key": "agent_key_*"
}

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)