Skip to content

Commit

Permalink
Add scripts to sandbox-admin image
Browse files Browse the repository at this point in the history
  • Loading branch information
fridim committed Nov 12, 2024
1 parent 9d8c2c8 commit 23f68ae
Show file tree
Hide file tree
Showing 9 changed files with 374 additions and 0 deletions.
83 changes: 83 additions & 0 deletions tools/sandbox_api_close_and_delete_AwsSandbox.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/usr/bin/env python3

import os
import sys
import requests
import json
import logging
from pathlib import Path

def main():
# route comes from environment variable 'route'
route = os.getenv('route')
if route is None:
print("route environment variable not set")
sys.exit(1)

# admintoken comes from environment variable 'admintoken'
admintoken = os.getenv('admintoken')
if admintoken is None:
print("admintoken environment variable not set")
sys.exit(1)

if len(sys.argv) != 2:
print(f"Usage: {sys.argv[0]} <sandbox>")
sys.exit(1)

sandbox = sys.argv[1]

answer = input(f"Are you sure you want to permanently delete the sandbox {sandbox}? [y/N] ")
if answer.lower() != 'y':
print("Aborted.")
sys.exit(1)

logs_dir = Path.home() / 'logs'
logs_dir.mkdir(parents=True, exist_ok=True)
log_file = logs_dir / 'sandbox_close_and_delete.log'
logging.basicConfig(filename=log_file, level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')

# First HTTP request to get access_token
login_url = f"{route}/api/v1/login"
headers = {
'Authorization': f'Bearer {admintoken}',
}

response = requests.get(login_url, headers=headers)
with open(log_file, 'a') as f:
logging.info(f"GET {login_url}")
logging.info(f"Response status: {response.status_code}")

if response.status_code != 200:
print(f"Failed to get access token. Status code: {response.status_code}")
print(response.text)
sys.exit(1)

response_json = response.json()
access_token = response_json.get('access_token')
if not access_token:
print("access_token not found in response")
sys.exit(1)

# Second HTTP request to delete the sandbox
account_type = 'AwsSandbox'
delete_url = f"{route}/api/v1/accounts/{account_type}/{sandbox}"
headers = {
'Authorization': f'Bearer {access_token}',
}

response = requests.delete(delete_url, headers=headers)
with open(log_file, 'a') as f:
logging.info(f"DELETE {delete_url}")
logging.info(f"Response status: {response.status_code}")
logging.info(f"Response body: {response.text}\n")

if response.status_code not in [200, 204]:
print(f"Failed to delete sandbox. Status code: {response.status_code}")
print(response.text)
sys.exit(1)
else:
print("Sandbox deleted successfully.")
print(response.text)

if __name__ == "__main__":
main()
83 changes: 83 additions & 0 deletions tools/sandbox_api_delete_placement.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/usr/bin/env python3

import os
import sys
import requests
import json
import logging
from pathlib import Path

def main():
# route comes from environment variable 'route'
route = os.getenv('route')
if route is None:
print("route environment variable not set")
sys.exit(1)

# admintoken comes from environment variable 'admintoken'
admintoken = os.getenv('admintoken')
if admintoken is None:
print("admintoken environment variable not set")
sys.exit(1)

if len(sys.argv) != 2:
print(f"Usage: {sys.argv[0]} <uuid>")
sys.exit(1)

uuid = sys.argv[1]

answer = input(f"Are you sure you want to delete the placement {uuid}? [y/N] ")
if answer.lower() != 'y':
print("Aborted.")
sys.exit(1)

logs_dir = Path.home() / 'logs'
logs_dir.mkdir(parents=True, exist_ok=True)
log_file = logs_dir / 'sandbox_delete_placement.log'

logging.basicConfig(filename=log_file, level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')

# First HTTP request to get access_token
login_url = f"{route}/api/v1/login"
headers = {
'Authorization': f'Bearer {admintoken}',
}

response = requests.get(login_url, headers=headers)
with open(log_file, 'a') as f:
logging.info(f"GET {login_url}")
logging.info(f"Response status: {response.status_code}")

if response.status_code != 200:
print(f"Failed to get access token. Status code: {response.status_code}")
print(response.text)
sys.exit(1)

response_json = response.json()
access_token = response_json.get('access_token')
if not access_token:
print("access_token not found in response")
sys.exit(1)

# Second HTTP request to delete the sandbox
delete_url = f"{route}/api/v1/placements/{uuid}"
headers = {
'Authorization': f'Bearer {access_token}',
}

response = requests.delete(delete_url, headers=headers)
with open(log_file, 'a') as f:
logging.info(f"DELETE {delete_url}")
logging.info(f"Response status: {response.status_code}")
logging.info(f"Response body: {response.text}\n")

if response.status_code not in [200, 202, 204]:
print(f"Failed to delete placement. Status code: {response.status_code}")
print(response.text)
sys.exit(1)
else:
print("Placement successfully deleted.")
print(response.text)

if __name__ == "__main__":
main()
20 changes: 20 additions & 0 deletions tools/sandbox_api_disable_shared_cluster.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

cluster=$1

if [ -z "${cluster}" ]; then
echo "$0 CLUSTERNAME"
exit 2
fi

if [ -z "${route}" ]; then
echo "route is not set. Did you source the rc file?"
exit 2
fi

. ~/.sandbox-api-admin-token.rc

hurl --variable login_token_admin=$admintoken \
--variable host=$route \
--variable cluster=${cluster} \
~/sandbox/tools/ocp_shared_cluster_configuration_disable.hurl
15 changes: 15 additions & 0 deletions tools/sandbox_api_disable_shared_cluster_dev.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

cluster=$1

if [ -z "${cluster}" ]; then
echo "$0 CLUSTERNAME"
exit 2
fi

. ~/.sandbox-api-admin-token.rc

hurl --variable login_token_admin=$admintokendev \
--variable host=$routedev \
--variable cluster=${cluster} \
~/sandbox/tools/ocp_shared_cluster_configuration_disable.hurl
15 changes: 15 additions & 0 deletions tools/sandbox_api_enable_shared_cluster.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

cluster=$1

if [ -z "${cluster}" ]; then
echo "$0 CLUSTERNAME"
exit 2
fi

. ~/.sandbox-api-admin-token.rc

hurl --variable login_token_admin=$admintoken \
--variable host=$route \
--variable cluster=${cluster} \
~/sandbox/tools/ocp_shared_cluster_configuration_enable.hurl
15 changes: 15 additions & 0 deletions tools/sandbox_api_enable_shared_cluster_dev.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

cluster=$1

if [ -z "${cluster}" ]; then
echo "$0 CLUSTERNAME"
exit 2
fi

. ~/.sandbox-api-admin-token.rc

hurl --variable login_token_admin=$admintokendev \
--variable host=$routedev \
--variable cluster=${cluster} \
~/sandbox/tools/ocp_shared_cluster_configuration_enable.hurl
83 changes: 83 additions & 0 deletions tools/sandbox_api_mark_AwsSandbox_for_cleanup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/usr/bin/env python3

import os
import sys
import requests
import json
import logging
from pathlib import Path

def main():
# route comes from environment variable 'route'
route = os.getenv('route')
if route is None:
print("route environment variable not set")
sys.exit(1)

# admintoken comes from environment variable 'admintoken'
admintoken = os.getenv('admintoken')
if admintoken is None:
print("admintoken environment variable not set")
sys.exit(1)

if len(sys.argv) != 2:
print(f"Usage: {sys.argv[0]} <sandbox>")
sys.exit(1)

sandbox = sys.argv[1]

answer = input(f"Are you sure you want to mark the sandbox {sandbox} for cleanup? [y/N] ")
if answer.lower() != 'y':
print("Aborted.")
sys.exit(1)

logs_dir = Path.home() / 'logs'
logs_dir.mkdir(parents=True, exist_ok=True)
log_file = logs_dir / 'sandbox_mark_for_cleanup.log'
logging.basicConfig(filename=log_file, level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')

# First HTTP request to get access_token
login_url = f"{route}/api/v1/login"
headers = {
'Authorization': f'Bearer {admintoken}',
}

response = requests.get(login_url, headers=headers)
with open(log_file, 'a') as f:
logging.info(f"GET {login_url}")
logging.info(f"Response status: {response.status_code}")

if response.status_code != 200:
print(f"Failed to get access token. Status code: {response.status_code}")
print(response.text)
sys.exit(1)

response_json = response.json()
access_token = response_json.get('access_token')
if not access_token:
print("access_token not found in response")
sys.exit(1)

# Second HTTP request to delete the sandbox
account_type = 'AwsSandbox'
cleanup_url = f"{route}/api/v1/accounts/{account_type}/{sandbox}/cleanup"
headers = {
'Authorization': f'Bearer {access_token}',
}

response = requests.put(cleanup_url, headers=headers)
with open(log_file, 'a') as f:
logging.info(f"PUT {cleanup_url}")
logging.info(f"Response status: {response.status_code}")
logging.info(f"Response body: {response.text}\n")

if response.status_code not in [200, 204]:
print(f"Failed to delete sandbox. Status code: {response.status_code}")
print(response.text)
sys.exit(1)
else:
print("Sandbox successfully marked for cleanup.")
print(response.text)

if __name__ == "__main__":
main()
30 changes: 30 additions & 0 deletions tools/sandbox_api_update_shared_cluster.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash

cluster=$1

ORIG="$(cd "$(dirname "$0")" || exit; pwd)"

if [ -z "${cluster}" ]; then
echo "$0 CLUSTERNAME [JSON PAYLOAD file]"
exit 2
fi

[ -e ~/.sandbox-api-admin-token.rc ] && source ~/.sandbox-api-admin-token.rc

payload=$(mktemp)

if [ -n "${2}" ]; then
cat "${2}" > ${payload}
else
echo "please provide the payload content, end with <CTRL-D>"
cat > ${payload}
fi

hurl --variable login_token_admin=$admintoken \
--file-root /tmp \
--variable host=$route \
--variable cluster=${cluster} \
--variable payload=${payload} \
~/sandbox/tools/ocp_shared_cluster_configuration_update.hurl

rm -f ${payload}
30 changes: 30 additions & 0 deletions tools/sandbox_api_update_shared_cluster_dev.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash

cluster=$1

ORIG="$(cd "$(dirname "$0")" || exit; pwd)"

if [ -z "${cluster}" ]; then
echo "$0 CLUSTERNAME [JSON PAYLOAD file]"
exit 2
fi

[ -e ~/.sandbox-api-admin-token.rc ] && source ~/.sandbox-api-admin-token.rc

payload=$(mktemp)

if [ -n "${2}" ]; then
cat "${2}" > ${payload}
else
echo "please provide the payload content, end with <CTRL-D>"
cat > ${payload}
fi

hurl --variable login_token_admin=$admintokendev \
--file-root /tmp \
--variable host=$routedev \
--variable cluster=${cluster} \
--variable payload=${payload} \
~/sandbox/tools/ocp_shared_cluster_configuration_update.hurl

rm -f ${payload}

0 comments on commit 23f68ae

Please sign in to comment.