Commit df47f72d authored by Nazrul Kamaruddin's avatar Nazrul Kamaruddin

Upgraded all scripts with Python logging.

parent 67eee3d7
......@@ -7,7 +7,10 @@ from datetime import timedelta, timezone, datetime
from time import strftime
import dateutil.parser
from akinaka_client.aws_client import AWS_Client
from akinaka_libs import helpers
import logging
helpers.set_logger()
aws_client = AWS_Client()
class CleanupAMIs():
......@@ -43,7 +46,7 @@ class CleanupAMIs():
amis_to_delete.add(ami['ImageId'])
# Same with list comprehension
# print([ami for ami in all_amis if dateutil.parser.parse(ami['CreationDate']) < self.retention_start])
# logging.info([ami for ami in all_amis if dateutil.parser.parse(ami['CreationDate']) < self.retention_start])
return amis_to_delete
def delist_in_use_amis(self, amis):
......@@ -119,10 +122,10 @@ class CleanupAMIs():
amis_to_delete = self.delist_launch_template_finds(amis_to_delete)
if self.not_dry_run:
print("Deleting the following AMIs and their snapshots: {}".format(amis_to_delete))
logging.info("Deleting the following AMIs and their snapshots: {}".format(amis_to_delete))
self.delete_amis(amis_to_delete)
else:
print("These are the AMIs I would have deleted if you gave me --not-dry-run: {}".format(amis_to_delete))
logging.info("These are the AMIs I would have deleted if you gave me --not-dry-run: {}".format(amis_to_delete))
import click
from akinaka_libs import helpers
import logging
helpers.set_logger()
@click.group()
@click.option("--region", required=True, help="Region your resources are located in")
......@@ -37,7 +41,7 @@ def ami(ctx, retention, not_dry_run, exceptional_amis, launch_templates):
amis.cleanup()
exit(0)
except Exception as e:
print(e)
logging.error(e)
exit(1)
@cleanup.command()
......@@ -54,7 +58,7 @@ def ebs(ctx):
volumes.cleanup()
exit(0)
except Exception as e:
print(e)
logging.error(e)
exit(1)
......
......@@ -7,7 +7,10 @@ from datetime import timedelta, timezone, datetime
from time import strftime
import dateutil.parser
from akinaka_client.aws_client import AWS_Client
from akinaka_libs import helpers
import logging
helpers.set_logger()
aws_client = AWS_Client()
class CleanupVolumes():
......@@ -39,16 +42,16 @@ class CleanupVolumes():
def cleanup(self):
for role in self.role_arns:
print("\nProcessing account: {}".format(role))
logging.error("\nProcessing account: {}".format(role))
volumes_to_delete = self.list_available_volumes(role)
if self.not_dry_run:
print("Deleting the following volumes and their snapshots: {}".format(volumes_to_delete))
logging.info("Deleting the following volumes and their snapshots: {}".format(volumes_to_delete))
self.delete_volumes(volumes_to_delete, role)
else:
print("These are the volumes I would have deleted if you gave me --not-dry-run:\n")
logging.info("These are the volumes I would have deleted if you gave me --not-dry-run:\n")
for volume in volumes_to_delete:
print("Volume: {}\n".format(volume['VolumeId']))
logging.info("Volume: {}\n".format(volume['VolumeId']))
from akinaka_libs import helpers
import logging
helpers.set_logger()
class AkinakaGeneralError(Exception):
pass
......@@ -9,5 +14,5 @@ class AkinakaLoggingError(Exception):
class AkinakaCriticalException(Exception):
def __init__(self):
print("Something terrible happened. Exiting")
logging.error("Something terrible happened. Exiting")
exit(1)
\ No newline at end of file
import click
from akinaka_libs import helpers
import logging
helpers.set_logger()
@click.group()
@click.option("--region", envvar='AWS_DEFAULT_REGION', help="Region your resources are located in")
......@@ -30,5 +34,5 @@ def rds(ctx, source_role_arn, target_role_arn, snapshot_style, source_instance_n
rds_copy.copy_instance()
exit(0)
except Exception as e:
print(e)
logging.error(e)
exit(1)
This diff is collapsed.
......@@ -5,6 +5,10 @@ import time
import datetime
import sys
import tabulate
from akinaka_libs import helpers
import logging
helpers.set_logger()
class BillingQueries():
def __init__(self, region, assume_role_arn):
......@@ -15,7 +19,7 @@ class BillingQueries():
response = self.costexplorer.get_bill_estimates(from_days_ago)
data = response['ResultsByTime']
except Exception as e:
print("Billing estimates is not available: {}".format(e))
logging.error("Billing estimates is not available: {}".format(e))
return e
results = []
......@@ -35,5 +39,5 @@ class BillingQueries():
message += tabulate.tabulate(results, headers=["Date", "Total"], tablefmt='psql')
message += "\n"
print(message)
return message
\ No newline at end of file
logging.info(message)
return
\ No newline at end of file
......@@ -57,7 +57,7 @@ class ASG():
# TODO: Support multiple load balancers with multiple target groups.
else:
print("""
logging.error("""
One of these mutually exclusive options need to be passed:
--lb
--asg
......
......@@ -35,7 +35,7 @@ class TargetGroup():
active_asg = "-".join(c)
for asg in asgs['AutoScalingGroups']:
# print("{}, {}".format(asg['AutoScalingGroupName'], asg['TargetGroupARNs']))
# logging.info("{}, {}".format(asg['AutoScalingGroupName'], asg['TargetGroupARNs']))
if asg['AutoScalingGroupName'] == self.new_asg and len(asg['TargetGroupARNs']) < 1:
passive.append(asg)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment