Skip to content

Commit ce5ea46

Browse files
Fix cost over usage resource file path and Jenkins exit handling. (#1008)
Write the user resource JSON to /tmp before S3 upload and fail the weekly Jenkins job when podman runs return a non-zero exit code. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 6c4e56b commit ce5ea46

2 files changed

Lines changed: 23 additions & 17 deletions

File tree

cloud_governance/policy/aws/cost_over_usage.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ def aggregate_user_sum(self, data: list):
7474
"""
7575
df = pandas.DataFrame.from_records(data).fillna({})
7676
if 'Instances' in data[0]:
77-
df = df.groupby('User').agg({'Cost': sum, 'Instances': sum}).reset_index()
77+
df = df.groupby('User').agg({'Cost': 'sum', 'Instances': sum}).reset_index()
7878
else:
79-
df = df.groupby('User').agg({'Cost': sum}).reset_index()
79+
df = df.groupby('User').agg({'Cost': 'sum'}).reset_index()
8080
return self.filter_sort_list(user_resources=df.to_dict('records'))
8181

8282
def aws_user_usage(self, days: int, cost_usage: int):
@@ -87,25 +87,25 @@ def aws_user_usage(self, days: int, cost_usage: int):
8787
@return:
8888
"""
8989
users = []
90-
cc = []
9190
user_data = self._elastic_upload.elastic_search_operations.get_index_hits(days=days,
9291
index=self._elastic_upload.es_index)
9392
user_data = self.aggregate_user_sum(user_data)
9493
for user_usage in user_data:
9594
user = user_usage['User']
9695
if user_usage['Cost'] > cost_usage:
97-
file_name = ""
98-
if user_usage.get('Instances'):
99-
used_instances = self.get_user_used_instances(user_used_list=user_usage.get('Instances'))
100-
with open(file_name, 'w') as file:
101-
json.dump(used_instances, file, indent=4)
10296
ignore_user_mails = self._elastic_upload.literal_eval(self.__ignore_mails)
10397
if user not in ignore_user_mails:
98+
cc = []
10499
special_user_mails = self._elastic_upload.literal_eval(self._elastic_upload.special_user_mails)
105100
to = user if user not in special_user_mails else special_user_mails[user]
106101
ldap_data = self.__ldap.get_user_details(user_name=to)
107102
name = to
108-
file_name = os.path.join('/tmp', f'{to}_resource.json')
103+
file_name = None
104+
if user_usage.get('Instances'):
105+
used_instances = self.get_user_used_instances(user_used_list=user_usage.get('Instances'))
106+
file_name = os.path.join('/tmp', f'{to}_resource.json')
107+
with open(file_name, 'w') as file:
108+
json.dump(used_instances, file, indent=4)
109109

110110
if ldap_data:
111111
cc.append(f'{ldap_data.get("managerId")}@redhat.com')
@@ -114,8 +114,10 @@ def aws_user_usage(self, days: int, cost_usage: int):
114114
user_usage=user_usage[
115115
'Cost'], name=name,
116116
usage_cost=self.COST_USAGE_DOLLAR)
117-
self._elastic_upload.postfix_mail.send_email_postfix(subject=subject, content=body, to=to, cc=cc,
118-
filename=file_name)
117+
email_kwargs = {'subject': subject, 'content': body, 'to': to, 'cc': cc}
118+
if file_name:
119+
email_kwargs['filename'] = file_name
120+
self._elastic_upload.postfix_mail.send_email_postfix(**email_kwargs)
119121
users.append(to)
120122
return users
121123

jenkins/clouds/aws/weekly/cost_over_usage/run_upload_es.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import sys
23

34
AWS_ACCESS_KEY_ID_DELETE_PERF = os.environ['AWS_ACCESS_KEY_ID_DELETE_PERF']
45
AWS_SECRET_ACCESS_KEY_DELETE_PERF = os.environ['AWS_SECRET_ACCESS_KEY_DELETE_PERF']
@@ -22,9 +23,12 @@
2223
es_index_psap = 'cloud-governance-cost-explorer-psap'
2324
es_index_perf_scale = 'cloud-governance-cost-explorer-perf-scale'
2425

25-
os.system(
26-
f"""podman run --rm --name cloud-governance --net="host" -e account="perf-dept" -e policy="cost_over_usage" -e AWS_ACCESS_KEY_ID="{AWS_ACCESS_KEY_ID_DELETE_PERF}" -e AWS_SECRET_ACCESS_KEY="{AWS_SECRET_ACCESS_KEY_DELETE_PERF}" -e es_host="{ES_HOST}" -e es_port="{ES_PORT}" -e es_user="{ES_USER}" -e es_password="{ES_PASSWORD}" -e es_index="{es_index_perf}" -e LDAP_HOST_NAME="{LDAP_HOST_NAME}" -e special_user_mails="{special_user_mails}" -e IGNORE_MAILS="{IGNORE_MAILS}" -e log_level="INFO" -e policy_output="{BUCKET_PERF}" {QUAY_CLOUD_GOVERNANCE_REPOSITORY}""")
27-
os.system(
28-
f"""podman run --rm --name cloud-governance --net="host" -e account="psap" -e policy="cost_over_usage" -e AWS_ACCESS_KEY_ID="{AWS_ACCESS_KEY_ID_DELETE_PSAP}" -e AWS_SECRET_ACCESS_KEY="{AWS_SECRET_ACCESS_KEY_DELETE_PSAP}" -e es_host="{ES_HOST}" -e es_port="{ES_PORT}" -e es_user="{ES_USER}" -e es_password="{ES_PASSWORD}" -e es_index="{es_index_psap}" -e LDAP_HOST_NAME="{LDAP_HOST_NAME}" -e special_user_mails="{special_user_mails}" -e IGNORE_MAILS="{IGNORE_MAILS}" -e log_level="INFO" -e policy_output="{BUCKET_PSAP}" {QUAY_CLOUD_GOVERNANCE_REPOSITORY}""")
29-
os.system(
30-
f"""podman run --rm --name cloud-governance --net="host" -e account="perf-scale" -e policy="cost_over_usage" -e AWS_ACCESS_KEY_ID="{AWS_ACCESS_KEY_ID_DELETE_PERF_SCALE}" -e AWS_SECRET_ACCESS_KEY="{AWS_SECRET_ACCESS_KEY_DELETE_PERF_SCALE}" -e es_host="{ES_HOST}" -e es_port="{ES_PORT}" -e es_user="{ES_USER}" -e es_password="{ES_PASSWORD}" -e es_index="{es_index_perf_scale}" -e LDAP_HOST_NAME="{LDAP_HOST_NAME}" -e special_user_mails="{special_user_mails}" -e IGNORE_MAILS="{IGNORE_MAILS}" -e policy_output="{BUCKET_PERF_SCALE}" -e log_level="INFO" {QUAY_CLOUD_GOVERNANCE_REPOSITORY}""")
26+
commands = [
27+
f"""podman run --rm --name cloud-governance --net="host" -e account="perf-dept" -e policy="cost_over_usage" -e AWS_ACCESS_KEY_ID="{AWS_ACCESS_KEY_ID_DELETE_PERF}" -e AWS_SECRET_ACCESS_KEY="{AWS_SECRET_ACCESS_KEY_DELETE_PERF}" -e es_host="{ES_HOST}" -e es_port="{ES_PORT}" -e es_user="{ES_USER}" -e es_password="{ES_PASSWORD}" -e es_index="{es_index_perf}" -e LDAP_HOST_NAME="{LDAP_HOST_NAME}" -e special_user_mails="{special_user_mails}" -e IGNORE_MAILS="{IGNORE_MAILS}" -e log_level="INFO" -e policy_output="{BUCKET_PERF}" {QUAY_CLOUD_GOVERNANCE_REPOSITORY}""",
28+
f"""podman run --rm --name cloud-governance --net="host" -e account="psap" -e policy="cost_over_usage" -e AWS_ACCESS_KEY_ID="{AWS_ACCESS_KEY_ID_DELETE_PSAP}" -e AWS_SECRET_ACCESS_KEY="{AWS_SECRET_ACCESS_KEY_DELETE_PSAP}" -e es_host="{ES_HOST}" -e es_port="{ES_PORT}" -e es_user="{ES_USER}" -e es_password="{ES_PASSWORD}" -e es_index="{es_index_psap}" -e LDAP_HOST_NAME="{LDAP_HOST_NAME}" -e special_user_mails="{special_user_mails}" -e IGNORE_MAILS="{IGNORE_MAILS}" -e log_level="INFO" -e policy_output="{BUCKET_PSAP}" {QUAY_CLOUD_GOVERNANCE_REPOSITORY}""",
29+
f"""podman run --rm --name cloud-governance --net="host" -e account="perf-scale" -e policy="cost_over_usage" -e AWS_ACCESS_KEY_ID="{AWS_ACCESS_KEY_ID_DELETE_PERF_SCALE}" -e AWS_SECRET_ACCESS_KEY="{AWS_SECRET_ACCESS_KEY_DELETE_PERF_SCALE}" -e es_host="{ES_HOST}" -e es_port="{ES_PORT}" -e es_user="{ES_USER}" -e es_password="{ES_PASSWORD}" -e es_index="{es_index_perf_scale}" -e LDAP_HOST_NAME="{LDAP_HOST_NAME}" -e special_user_mails="{special_user_mails}" -e IGNORE_MAILS="{IGNORE_MAILS}" -e policy_output="{BUCKET_PERF_SCALE}" -e log_level="INFO" {QUAY_CLOUD_GOVERNANCE_REPOSITORY}""",
30+
]
31+
32+
for command in commands:
33+
if os.system(command) != 0:
34+
sys.exit(1)

0 commit comments

Comments
 (0)