Skip to content

Commit cefd522

Browse files
push before europa reload
1 parent 54de413 commit cefd522

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

images/ce-fn-webhook.png

-81.8 KB
Binary file not shown.

jobs/vpc-auto-stop-start/app.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515

1616
import os
1717
import json
18-
import click
18+
import sys
1919
import time
2020
import logging
21+
import httpx
22+
import click
2123
import ibm_vpc
2224
from ibm_vpc import VpcV1
2325
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
@@ -50,6 +52,45 @@ def vpc_client():
5052

5153
ignore_group = ['tethys', 'jbmh-locate']
5254

55+
def get_iam_token():
56+
try:
57+
hdrs = { 'Accept' : 'application/json', 'Content-Type' : 'application/x-www-form-urlencoded' }
58+
params = { 'grant_type' : 'urn:ibm:params:oauth:grant-type:apikey', 'apikey' : ibmcloud_api_key }
59+
resp = httpx.post('https://iam.cloud.ibm.com/identity/token', data = params, headers = hdrs)
60+
resp.raise_for_status()
61+
response_json = resp.json()
62+
iam_token = response_json['access_token']
63+
return iam_token
64+
65+
except httpx.HTTPError as e:
66+
logging.error("HTTP exception {}.".format(str(e)))
67+
sys.exit(1)
68+
69+
def send_log_to_ibm_cloud_logs(application_name, subsystem_name, computer_name, message):
70+
iam_token = get_iam_token()
71+
if not iam_token:
72+
raise ValueError("IAM_TOKEN environment variable not found")
73+
74+
log_data = [{
75+
"applicationName": application_name,
76+
"subsystemName": subsystem_name,
77+
"computerName": computer_name,
78+
"text": {"message": message},
79+
"category": "cat-1",
80+
"className": "class-1",
81+
"methodName": "method-1",
82+
"threadId": "thread-1"
83+
}]
84+
85+
logging_url = os.environ.get('LOGGING_URL')
86+
hdrs = { 'Content-Type' : 'application/json', "Authorization": f"{iam_token}" }
87+
resp = httpx.post(logging_url, data=json.dumps(log_data), headers = hdrs)
88+
resp.raise_for_status()
89+
print(resp.text)
90+
91+
92+
93+
5394
@click.group()
5495
def cli():
5596
"""Group to hold our commands"""
@@ -71,6 +112,7 @@ def stop_vpc_instances():
71112
if instance_name not in ignore_group:
72113
print(f"Stopping instance {instance_name}")
73114
response = client.create_instance_action(instance_id=instance_id, type='stop').get_result()
115+
send_log_to_ibm_cloud_logs("ce-start-stop-script", "stop-action", f"{instance_name}", f"Stopping instance {instance_name}")
74116
logging.info(response)
75117
except ApiException as e:
76118
print("Failed to stop instances: %s\n" % e)
@@ -90,6 +132,7 @@ def start_vpc_instances():
90132
instance_name = instance['name']
91133

92134
print(f"Starting instance {instance_name}")
135+
send_log_to_ibm_cloud_logs("ce-start-stop-script", "start-action", f"{instance_name}", f"Starting instance {instance_name}")
93136
response = client.create_instance_action(instance_id=instance_id, type='start').get_result()
94137
logging.info(response)
95138

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1+
anyio==4.4.0
12
certifi==2024.2.2
23
charset-normalizer==3.3.2
34
click==8.1.7
5+
h11==0.14.0
6+
httpcore==1.0.5
7+
httpx==0.27.0
48
ibm-cloud-sdk-core==3.19.2
59
ibm-vpc==0.21.0
610
idna==3.6
711
PyJWT==2.8.0
812
python-dateutil==2.9.0.post0
913
requests==2.31.0
1014
six==1.16.0
15+
sniffio==1.3.1
1116
urllib3==2.2.1

0 commit comments

Comments
 (0)