-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtime.py
69 lines (54 loc) · 2.01 KB
/
runtime.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import os
import requests
CLIENT_ID = os.getenv("CLIENT_ID")
CLIENT_KEY = os.getenv("CLIENT_KEY")
CLIENT_REALM = os.getenv("CLIENT_REALM")
RUN_ID = os.getenv("RUN_ID")
FORCE = os.getenv("FORCE_CANCEL")
inputs_list = [CLIENT_ID, CLIENT_KEY, CLIENT_REALM]
if None in inputs_list:
print("- Some mandatory authentication input is empty. Please, check the input list.")
exit(1)
if RUN_ID == "":
print("- RUN_ID was not provided.")
print(" No need to cancel it.")
exit(0)
# Getting access token
print("Authenticating..")
iam_url = f"https://auth.stackspot.com/{CLIENT_REALM}/oidc/oauth/token"
iam_headers = {'Content-Type': 'application/x-www-form-urlencoded'}
iam_data = { "client_id":f"{CLIENT_ID}", "grant_type":"client_credentials", "client_secret":f"{CLIENT_KEY}" }
login_req = requests.post(
url=iam_url,
headers=iam_headers,
data=iam_data
)
if login_req.status_code != 200:
print("- Error during iam authentication")
print("- Status:", login_req.status_code)
print("- Error:", login_req.reason)
print("- Response:", login_req.text)
exit(1)
d1 = login_req.json()
access_token = d1["access_token"]
# Calling Cancel Action
print("Cancelling Run...")
cancel_headers = {"Authorization": f"Bearer {access_token}", "Content-Type": "application/json"}
cancel_run_url=f"https://runtime-manager.v1.stackspot.com/v1/run/cancel/{RUN_ID}?force={FORCE}"
print(f"Requesting Run {RUN_ID} to be cancelled")
cancel_request = requests.post(
url=cancel_run_url,
headers=cancel_headers
)
if cancel_request.status_code == 202:
print(f"- RUN {RUN_ID} cancelled successfully!")
elif cancel_request.status_code == 404:
print(f"- RUN {RUN_ID} not found.")
elif cancel_request.status_code == 422:
print(f"- RUN {RUN_ID} not currently running. Unable to Abort.")
else:
print("- Error cancelling run")
print("- Status:", cancel_request.status_code)
print("- Error:", cancel_request.reason)
print("- Response:", cancel_request.text)
exit(1)