WIP

parent 82cc0a39
#!/usr/bin/env python3
import click
from akinaka_libs import helpers
from time import gmtime, strftime
from akinaka_update.commands import update as update_commands
from akinaka_cleanup.commands import cleanup as cleanup_commands
from akinaka_rds.commands import copy as copy_commands
from akinaka_reporting.commands import reporting as reporting_commands
from akinaka_container.commands import container as container_commands
from akinaka_k8s.commands import k8s as k8s_commands
@click.group()
@click.option("--log-level", '-l', default="INFO", type=click.Choice(["INFO", "ERROR", "DEBUG"]), help="How much information to show in logging. Default is INFO")
@click.pass_context
def cli(ctx=None, log_level=None):
ctx.obj = {'log_level': log_level}
cli.add_command(update_commands)
cli.add_command(cleanup_commands)
cli.add_command(copy_commands)
cli.add_command(reporting_commands)
cli.add_command(container_commands)
cli.add_command(k8s_commands)
cli()
......@@ -6,8 +6,8 @@ import datetime
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
from akinaka.client.aws_client import AWS_Client
from akinaka.libs import helpers
import logging
helpers.set_logger()
......@@ -40,7 +40,7 @@ class CleanupAMIs():
def delist_out_of_retention_amis(self, all_amis):
amis_to_delete = set()
for ami in all_amis:
if dateutil.parser.parse(ami['CreationDate']) < self.retention_end:
amis_to_delete.add(ami['ImageId'])
......@@ -53,23 +53,23 @@ class CleanupAMIs():
unused_amis = set()
all_accounts_instances = []
instance_amis = set()
for role in self.role_arns:
ec2_client = aws_client.create_client('ec2', self.region, role)
all_accounts_instances.append(ec2_client.describe_instances()['Reservations'])
all_accounts_instances = [account for accounts in all_accounts_instances for account in accounts]
for account_instance in all_accounts_instances:
instance_amis.add(account_instance['Instances'][0]['ImageId'])
instance_amis.add(account_instance['Instances'][0]['ImageId'])
for ami in amis:
if ami in instance_amis:
continue
unused_amis.add(ami)
return unused_amis
def delist_latest_arbitrary_amis(self, amis):
def delist_latest_arbitrary_amis(self, amis):
for exceptional_ami in self.exceptional_amis:
filter = {
......@@ -77,9 +77,9 @@ class CleanupAMIs():
'Values': [ exceptional_ami ]
}
all_arbitrary_amis = self.list_amis(filter)
all_arbitrary_amis = self.list_amis(filter)
amis.discard(max(all_arbitrary_amis, key=lambda x:x['CreationDate'])['ImageId'])
return amis
def delist_launch_template_finds(self, amis):
......@@ -90,7 +90,7 @@ class CleanupAMIs():
LaunchTemplateNames=[launch_template]
)['LaunchTemplates'][0]['LatestVersionNumber']
str(latest_version)
# It shouldn't be possible to have no value for ImageId, but it is :D
try:
in_use_ami = ec2_client.describe_launch_template_versions(
......@@ -101,7 +101,7 @@ class CleanupAMIs():
in_use_ami = ""
amis.discard(in_use_ami)
return amis
def get_snapshot(self, ami):
......@@ -111,7 +111,7 @@ class CleanupAMIs():
ec2_client = aws_client.create_client('ec2', self.region, self.role_arns[0])
for ami in amis:
snapshot = self.get_snapshot(ami)
snapshot = self.get_snapshot(ami)
ec2_client.deregister_image(ImageId=ami)
ec2_client.delete_snapshot(SnapshotId=snapshot)
......@@ -120,12 +120,9 @@ class CleanupAMIs():
amis_to_delete = self.delist_in_use_amis(amis_to_delete)
amis_to_delete = self.delist_latest_arbitrary_amis(amis_to_delete)
amis_to_delete = self.delist_launch_template_finds(amis_to_delete)
if self.not_dry_run:
logging.info("Deleting the following AMIs and their snapshots: {}".format(amis_to_delete))
self.delete_amis(amis_to_delete)
else:
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
from akinaka.libs import helpers
import logging
helpers.set_logger()
......
......@@ -6,8 +6,8 @@ import datetime
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
from akinaka.client.aws_client import AWS_Client
from akinaka.libs import helpers
import logging
helpers.set_logger()
......
#!/usr/bin/env python3
import boto3
from akinaka_client.aws_client import AWS_Client
from akinaka_libs import helpers
from akinaka.client.aws_client import AWS_Client
from akinaka.libs import helpers
import logging
helpers.set_logger()
......
import boto3
from akinaka_client.aws_client import AWS_Client
from akinaka_libs import helpers
from akinaka.client.aws_client import AWS_Client
from akinaka.libs import helpers
import base64
import logging
......
import click
from akinaka_client.aws_client import AWS_Client
from akinaka_libs import helpers
from akinaka.client.aws_client import AWS_Client
from akinaka.libs import helpers
from time import gmtime, strftime
import logging
......
#!/usr/bin/env python3
from time import sleep
from akinaka_libs import helpers
from akinaka_libs import exceptions
from akinaka.libs import helpers
from akinaka.libs import exceptions
import yaml
import logging
......
import boto3
from akinaka_client.aws_client import AWS_Client
import akinaka_libs.helpers as helpers
from akinaka.client.aws_client import AWS_Client
import akinaka.libs.helpers as helpers
from datetime import datetime, timezone
aws_client = AWS_Client()
......@@ -18,7 +18,7 @@ class CloudWatch():
try:
return self.cloudwatch_client.get_metric_statistics(
Namespace = namespace,
MetricName = name,
MetricName = name,
StartTime = start,
EndTime = end,
Period = granularity,
......@@ -39,9 +39,3 @@ class CloudWatch():
fields = [{ 'Name': 'Currency', 'Value': 'USD' }],
stat_types = ["Maximum"],
)['Datapoints']
import boto3
from akinaka_client.aws_client import AWS_Client
import akinaka_libs.helpers as helpers
from akinaka.client.aws_client import AWS_Client
import akinaka.libs.helpers as helpers
from datetime import datetime, timezone, timedelta
aws_client = AWS_Client()
......@@ -11,7 +11,7 @@ class CostExplorer():
def get_bill_estimates(self, from_days_ago):
days_ago = 0 or int(from_days_ago)
if days_ago > 0:
end = datetime.now().strftime("%Y-%m-%d")
datetime_days_ago = datetime.now() - timedelta(days=days_ago + 1)
......@@ -20,7 +20,7 @@ class CostExplorer():
start = datetime.now().strftime("%Y-%m-%d")
datetime_days_ago = datetime.now() + timedelta(days=1)
end = datetime_days_ago.strftime("%Y-%m-%d")
return self.costexplorer_client.get_cost_and_usage(
TimePeriod={
'Start': start,
......@@ -30,4 +30,4 @@ class CostExplorer():
Metrics=[
'UnblendedCost'
]
)
\ No newline at end of file
)
from akinaka_libs import helpers
from akinaka.libs import helpers
import logging
helpers.set_logger(level="ERROR")
......
#!/usr/bin/env python3
def main():
import click
from time import gmtime, strftime
from akinaka.libs import helpers
from akinaka.update.commands import update as update_commands
from akinaka.cleanup.commands import cleanup as cleanup_commands
from akinaka.rds.commands import copy as copy_commands
from akinaka.reporting.commands import reporting as reporting_commands
from akinaka.container.commands import container as container_commands
from akinaka.k8s.commands import k8s as k8s_commands
@click.group()
@click.option("--log-level", '-l', default="INFO", type=click.Choice(["INFO", "ERROR", "DEBUG"]), help="How much information to show in logging. Default is INFO")
@click.pass_context
def cli(ctx=None, log_level=None):
ctx.obj = {'log_level': log_level}
cli.add_command(update_commands)
cli.add_command(cleanup_commands)
cli.add_command(copy_commands)
cli.add_command(reporting_commands)
cli.add_command(container_commands)
cli.add_command(k8s_commands)
cli()
if __name__ == "__main__":
main()
import click
from akinaka_libs import helpers
from akinaka.libs import helpers
import logging
from akinaka_cleanup.rds import cleanup_snapshots
from akinaka.cleanup.rds import cleanup_snapshots
helpers.set_logger()
@click.group()
......
#!/usr/bin/env python
from akinaka_client.aws_client import AWS_Client
from akinaka.client.aws_client import AWS_Client
import boto3
import time
import datetime
import sys
from akinaka_libs import helpers
from akinaka.libs import helpers
import logging
helpers.set_logger()
......
import akinaka_libs.cloudwatch
import akinaka_libs.costexplorer
import akinaka.libs.cloudwatch
import akinaka.libs.costexplorer
import boto3
import time
import datetime
import sys
import tabulate
from akinaka_libs import helpers
from akinaka.libs import helpers
import logging
helpers.set_logger()
class BillingQueries():
def __init__(self, region, assume_role_arn):
self.costexplorer = akinaka_libs.costexplorer.CostExplorer(region, assume_role_arn)
self.costexplorer = akinaka.libs.costexplorer.CostExplorer(region, assume_role_arn)
def days_estimates(self, from_days_ago):
try:
......@@ -21,7 +21,7 @@ class BillingQueries():
except Exception as e:
logging.error("Billing estimates is not available: {}".format(e))
return e
results = []
if len(data) == 1:
amount = float(data[0]['Total']['UnblendedCost']['Amount'])
......@@ -35,9 +35,9 @@ class BillingQueries():
amount = float(d['Total']['UnblendedCost']['Amount'])
results.append([d['TimePeriod']['End'], "{} {:.2f}".format(unit, amount)])
message = "\nEstimated bill for the past {} days\n".format(str(len(data)))
message += tabulate.tabulate(results, headers=["Date", "Total"], tablefmt='psql')
message += "\n"
logging.info(message)
return
\ No newline at end of file
return
#!/usr/bin/env python3
from time import sleep
from akinaka_libs import helpers
from akinaka_libs import exceptions
from akinaka.libs import helpers
from akinaka.libs import exceptions
from botocore.exceptions import ParamValidationError
from botocore.exceptions import ClientError
from akinaka_client.aws_client import AWS_Client
from akinaka.client.aws_client import AWS_Client
import logging
aws_client = AWS_Client()
......
import click
from akinaka_client.aws_client import AWS_Client
from akinaka_libs import helpers
from akinaka.client.aws_client import AWS_Client
from akinaka.libs import helpers
from time import gmtime, strftime
import logging
......
#!/usr/bin/env python3
from akinaka_client.aws_client import AWS_Client
from akinaka_libs import exceptions
from akinaka_libs import helpers
from akinaka.client.aws_client import AWS_Client
from akinaka.libs import exceptions
from akinaka.libs import helpers
import logging
aws_client = AWS_Client()
......
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import setuptools
with open("README.md", "r") as fh:
......@@ -13,7 +16,10 @@ setuptools.setup(
long_description=long_description,
long_description_content_type="text/markdown",
url="https://gitlab.olindata.com/olindata/akinaka",
keywords=[],
packages=setuptools.find_packages(),
include_package_data=True,
entry_points={"console_scripts": ["akinaka=akinaka.main:main"]},
install_requires=[
'boto3',
'datetime',
......@@ -26,6 +32,5 @@ setuptools.setup(
"License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)",
"Operating System :: OS Independent",
],
scripts=['akinaka.py'],
zip_safe=False
)
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