Last active
June 17, 2018 14:45
-
-
Save knabben/ea48eebc549dc726393eff7f7d6d3e23 to your computer and use it in GitHub Desktop.
AWS Helper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # DNS CONSUL POPULATOR | |
| # -*- coding: utf-8 -*- | |
| import boto3 | |
| import requests | |
| from tabulate import tabulate | |
| def start_dns(args): | |
| """ Start Consul objects """ | |
| try: | |
| dns = DNS(args.url) | |
| instances = dns.fetch_ec2_instances() | |
| dns.create_dns_register(args.url, instances) | |
| except requests.exceptions.ConnectionError: | |
| raise Exception("Set a working Consul host") | |
| class DNS(object): | |
| def __init__(self, host_consul=None): | |
| assert host_consul | |
| self.host_consul = host_consul | |
| self.ec2 = boto3.client('ec2') | |
| def put_dns_register(self, consul, name, ip): | |
| """ Put DNS on Consul via API """ | |
| data = {"Datacenter": "dc1", "Address": ip, "Node": name} | |
| requests.put("http://{}/v1/catalog/register".format(consul), json=data) | |
| return data | |
| def create_dns_register(self, consul, instances): | |
| """ Insert instances on Consul """ | |
| for name, ip in instances.iteritems(): | |
| print(self.put_dns_register(consul, name, ip)) | |
| def get_dns_host(self, instances): | |
| """ Get DNS list with PublicIpAddress """ | |
| print(tabulate({"IP": instances.values(), "Name": instances.keys()})) | |
| def fetch_ec2_instances(self): | |
| """ Fetch EC2 instances data """ | |
| instances = {} | |
| # Instance should be a key/value of EC2 Name vs. PublicIP | |
| for instance in self.ec2.describe_instances()["Reservations"]: | |
| try: | |
| inst_obj = instance.get("Instances")[0] | |
| public_ip = inst_obj.get('PublicIpAddress') | |
| name_tags = filter( | |
| lambda item: item.get('Key') == 'Name', | |
| inst_obj.get('Tags') | |
| ) | |
| name = ''.join(map(lambda item: item.get('Value'), name_tags)) | |
| instances[name] = public_ip | |
| except IndexError: | |
| continue | |
| return instances | |
| def start_es(args): | |
| if not args.ip or not args.domain: | |
| raise Exception("Wrong parameters") | |
| es = ES(args.ip, args.domain) | |
| config, ips = es.append_ip_acl(es.fetch_config()) | |
| print(es.update_access_list(config)) | |
| # UPDATING ES IP ACL | |
| class ES(object): | |
| def __init__(self, ip, domain): | |
| self.domain = domain | |
| self.ip = ip | |
| self.es = boto3.client('es') | |
| def fetch_config(self): | |
| return self.es.describe_elasticsearch_domain(DomainName=self.domain) | |
| def append_ip_acl(self, data): | |
| """ Append the IP as last item of ACL """ | |
| try: | |
| options = data.get('DomainStatus').get('AccessPolicies') | |
| policy = json.loads(options) | |
| except: | |
| return {}, [] | |
| ips = policy['Statement'][0]['Condition']['IpAddress']['aws:SourceIp'] | |
| ips.append(self.ip) | |
| return policy, ips | |
| def update_access_list(self, access_list): | |
| if access_list: | |
| return self.es.update_elasticsearch_domain_config( | |
| DomainName=self.domain, | |
| AccessPolicies=json.dumps(access_list)) | |
| def start_sqs(args): | |
| queue = Queue(args.queues) | |
| output = queue.convert_to_tabulate(queue.get_sqs_list()) | |
| print(output) | |
| # QUEUE LIST | |
| class Queue(object): | |
| def __init__(self, queues): | |
| self.sqs = boto3.resource('sqs') | |
| self.queues = queues | |
| def get_sqs_list(self): | |
| """ Get a list of queues with messages numbers """ | |
| data = defaultdict(list) | |
| for arg_queue in self.queues: | |
| sqs_queue = self.sqs.get_queue_by_name(QueueName=arg_queue) | |
| data['queue'].append(arg_queue) | |
| data['messages'].append( | |
| sqs_queue.attributes['ApproximateNumberOfMessages'] | |
| ) | |
| data['notvisible'].append( | |
| sqs_queue.attributes['ApproximateNumberOfMessagesNotVisible'] | |
| ) | |
| return data | |
| def convert_to_tabulate(self, data): | |
| """ Print queue list on tabulate """ | |
| return tabulate( | |
| {"Queue": data.get('queue'), "Messages": data.get('messages'), | |
| "NotVisible": data.get('notvisible')}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment