def create_method_response(self, restid, resourceid, method, statuscode, further_ops): """ This function creates a method response :param method: the method that is requested :type method: basestring :param restid: the id of the rest api object :type restid: basestring :param resourceid: id of a single resource object :type resourceid: basestring :param statuscode: the status code :type statuscode: basestring :param further_opts: This opt passes in json_data fur not mandatory options :type further_opts: dict :return: the created method response object """ if self.dryrun: logger.info("Dryrun requested no changes will be done") return None opts = {'restApiId': restid, 'resourceId': resourceid, 'httpMethod': method, 'statusCode': statuscode} if 'responseParameters' in further_ops: opts['responseParameters'] = further_ops['responseParameters'] if 'responseModels' in further_ops: opts['responseModels'] = further_ops['responseModels'] logger.debug("The opts sent to create method response %s" % opts) resp = self.apigateway_client.put_method_response(**opts) super(Apigateway, self).query_information(query=resp) return resp
def create_integration(self, restid, resourceid, method, integration_type, further_opts=None): """ This function creates an integration object :param method: the method that is requested :type method: basestring :param restid: the id of the rest api object :type restid: basestring :param resourceid: id of a single resource object :type resourceid: basestring :param integration_type: an enum of the integration type :type integration_type: basestring :param further_opts: This opt passes in json_data fur not mandatory options :type further_opts: dict :return: object of the created integration """ if self.dryrun: logger.info("Dryrun requested no changes will be done") return None opts = {'restApiId': restid, 'resourceId': resourceid, 'httpMethod': method, 'type': integration_type, 'integrationHttpMethod': method} # There is aws cli bug and integrationHttpMethod also needs to be added. may change later # opts = {'restApiId': restid, 'resourceId': resourceid, 'httpMethod': method, 'type': integration_type} for element in ['integrationHttpMethod', 'uri', 'credentials', 'requestParameters', 'requestTemplates', 'cacheNamespace', 'cacheNamespace']: if element in further_opts: opts[element] = further_opts[element] logger.debug("The opts for integration object creation: %s" % opts) resp = self.apigateway_client.put_integration(**opts) super(Apigateway, self).query_information(query=resp) return resp
def create_method(self, restid, resourceid, method, authorizationtype, apikeyreq=False, further_opts=None): """ This function creates a method object :param method: the method that is requested :type method: basestring :param restid: the id of the rest api object :type restid: basestring :param resourceid: id of a single resource object :type resourceid: basestring :param authorizationtype: :type authorizationtype: basestring :param apikeyreq: should apikey be required :type apikeyreq: bool :param further_opts: This opt passes in json_data fur not mandatory options :type further_opts: dict :return: the created method object """ if self.dryrun: logger.info("Dryrun requested no changes will be done") return None if isinstance(apikeyreq, bool) is False: logger.debug("apikey is not boolean, converting") apikeyreq = Misc.str2bool(apikeyreq) opts = {'restApiId': restid, 'resourceId': resourceid, 'httpMethod': method, 'authorizationType': authorizationtype, 'apiKeyRequired': apikeyreq} if 'requestParameters' in further_opts: opts['requestParameters'] = further_opts['requestParameters'] if 'requestModels' in further_opts: opts['requestModels'] = further_opts['requestModels'] logger.debug("The opts sent to create method %s" % opts) resp = self.apigateway_client.put_method(**opts) super(Apigateway, self).query_information(query=resp) return resp
def get_subnet_with_algorithym(self, puppet_role, subnets, num, fillup, xively_service): """ This function returns subnets in order they should be used to fill up accordng to requested algorithym :param puppet_role: the puppet role of the requested instances :param subnets: all the subnets that are avaiable :param num: The number of subnets we should return :param fillup: Should fillup or round robin algorithym be used :param xively_service: the xively service of the requested instance :return: a list of instances in order they should be used """ ordered = {} for subnet in subnets: instances = self.get_ec2_instances(filters=[{'Name': 'tag:Puppet_role', 'Values': [puppet_role]}, {'Name': 'tag:Xively_service', 'Values': [xively_service]}, {'Name': 'subnet-id', 'Values': [subnet.get('SubnetId')]}]) ordered[subnet.get('SubnetId')] = len(instances) ordered = sorted(ordered.items(), key=operator.itemgetter(1)) logger.debug("The ordered subnet list is: %s" % (str(ordered),)) ret = [] for i in range(0, num): if fillup: cur = ordered.pop(0) ret.append(cur[0]) tmp = {} for item in ordered: tmp[item[0]] = item[1] tmp[cur[0]] = cur[1] + 1 ordered = sorted(tmp.items(), key=operator.itemgetter(1)) else: mod = i % len(ordered) ret.append(ordered[mod][0]) return ret
def get_method_response(self, restid, resourceid, method, statuscode): """ This function returns a method response object :param method: the method that is requested :type method: basestring :param restid: the id of the rest api object :type restid: basestring :param resourceid: id of a single resource object :type resourceid: basestring :param statuscode: the statuscode requested :type statuscode: basestring :return: None if not found, else the object """ try: ret = self.apigateway_client.get_method_response(restApiId=restid, resourceId=resourceid, httpMethod=method, statusCode=statuscode) super(Apigateway, self).query_information(query=ret) logger.debug("We found the method response") except Exception as e: # https://github.com/aws/aws-cli/issues/1620 if e.response['Error']['Code'] == "NotFoundException": logger.warning("Method response %s for resource %s does not exist" % (statuscode, resourceid)) else: logger.error("%s" % e, ) ret = None return ret
def send_msg(self, msg=None, url=None, attribs=None): logger.debug("Sending msg %s to %s" % (msg, url)) if attribs: resp = self.boto3.send_message(QueueUrl=url, MessageBody=msg, attribs=attribs) else: resp = self.boto3.send_message(QueueUrl=url, MessageBody=msg) logger.debug("Message details: Md5: %s, MsgID: %s" % (resp['MD5OfMessageBody'], resp['MessageId']))
def remove_older_policy_versions(self, arn, dryrun): default_version = self.get_policy(arn=arn)['DefaultVersionId'] versions = self.get_policy_versions(arn=arn) versions = [i for i in versions if i['VersionId'] != default_version] for v in versions: logger.debug("Going to delete policy version %s" % (v['VersionId'],)) self.delete_policy_version(arn=arn, version=v['VersionId'], dryrun=dryrun)
def recieve_msg(self, url=None, filter=None): logger.debug("Recieving messages for url : %s" % (url,)) if filter: resp = self.boto3.recieve_message(QueueUrl=url, MessageAttributeNames=filter) else: resp = self.boto3.recieve_message(QueueUrl=url) return resp['Messages']
def route53_info(self, env=None): envs = super(awsrequests, self).get_needed_envs(env=env) v = Vpc() r = Route53() res = [] for environment in envs: logger.debug("Working in env: %s" % environment) vpc = v.get_vpc_from_env(env=environment) domain = Misc.get_value_from_array_hash(dictlist=vpc.get('Tags'), key="Domain") zoneid = r.get_zoneid_from_domain(domain=domain) records = r.list_zone_records(zoneid=zoneid) for record in records: rec = record.pop('ResourceRecords', []) values = [] for rr in rec: values.append(rr['Value']) record['Values'] = values if 'AliasTarget' in record: aliastarget = record.pop('AliasTarget') record['TTL'] = 'alias' record['Values'] = [aliastarget['DNSName']] record['Env'] = environment res.append(record) logger.debug("Processed record is: %s" % record, ) return res
def query_information(self, query): ''' This function is used to print debug information about a query, to see if it was succesful or not :param query: The boto3 query :return: Query with removed metadata ''' if query['ResponseMetadata']['HTTPStatusCode'] == 201: logger.debug("Resource was succesfully created") logger.info("Query RequestID: %s, HTTPStatusCode: %s" % ( query['ResponseMetadata']['RequestId'], query['ResponseMetadata']['HTTPStatusCode'])) elif query['ResponseMetadata']['HTTPStatusCode'] == 202: logger.debug('Request accepted but processing later.') logger.info("Query RequestID: %s, HTTPStatusCode: %s" % ( query['ResponseMetadata']['RequestId'], query['ResponseMetadata']['HTTPStatusCode'])) elif query['ResponseMetadata']['HTTPStatusCode'] == 204: logger.debug('Request done but no content returned.') logger.info("Query RequestID: %s, HTTPStatusCode: %s" % ( query['ResponseMetadata']['RequestId'], query['ResponseMetadata']['HTTPStatusCode'])) elif query['ResponseMetadata']['HTTPStatusCode'] != 200: logger.warning('There was an issue with request.') logger.warning("Query RequestID: %s, HTTPStatusCode: %s" % ( query['ResponseMetadata']['RequestId'], query['ResponseMetadata']['HTTPStatusCode'])) else: logger.debug("Request had no issues") logger.debug("Query RequestID: %s, HTTPStatusCode: %s" % ( query['ResponseMetadata']['RequestId'], query['ResponseMetadata']['HTTPStatusCode'])) query.pop('ResponseMetadata') if 'NextToken' in query: logger.error("Token is present. Paging needs to be implemented") return query
def server_certificate_upload(self, cert_name=None, pub_key=None, priv_key=None, cert_chain=None): i = Iam() if pub_key and os.path.isfile(pub_key): with open(pub_key, "r") as pub_key_fh: pub_key = pub_key_fh.read() logger.info("Read pub_key to internal variable: %s" % pub_key, ) if priv_key and os.path.isfile(priv_key): with open(priv_key, "r") as priv_key_fh: priv_key = priv_key_fh.read() logger.info("Read priv_key to internal variable: %s" % priv_key, ) if cert_chain and os.path.isfile(cert_chain): with open(cert_chain, "r") as cert_chain_fh: cert_chain = cert_chain_fh.read() logger.debug( "Read cert_chain to internal variable: %s" % cert_chain, ) out = i.upload_server_cert(cert_name=cert_name, pub_key=pub_key, priv_key=priv_key, cert_chain=cert_chain) print "ServerCertificateId: %s" % out['ServerCertificateMetadata'][ 'ServerCertificateId']
def upload_apigateway(self, json, dryrun): """ This function uploads an apigateway json to aws, and creates needed changes :param json: the json object :type json: json object :param dryrun: a boolean object if changes need to be made :type dryrun: bool :return: None """ from wrapper.apigateway import Apigateway apigateway = Apigateway(session=self.session, dryrun=dryrun) logger.debug("Testing if rest api exists") if apigateway.apigateway_exists(name=json['name']): logger.debug("Need to test if description needs to be updated") rest_api = apigateway.get_rest_api_by_name(name=json['name']) rest_api_id = rest_api['id'] if rest_api['description'] != json['description']: logger.info("Need to update the description") resp = apigateway.update_rest_api(restid=rest_api_id, operation=[{ 'op': 'replace', 'path': '/description', 'value': json['description'] }]) else: rest_api_create_resp = apigateway.create_rest_api( name=json['name'], desc=json['description']) rest_api_id = rest_api_create_resp['id'] if dryrun: logger.warning( "Whole resource needs to be created, no point in continue") return None logger.info("The rest api id we are going to work on: %s" % rest_api_id) resource_hash = apigateway.generate_resourcehash(restid=rest_api_id) root_id = resource_hash['/'] for resource in json['resources']: if resource['path'] not in resource_hash: logger.info("Need to create path in apigateway") resource_data = apigateway.create_resource( restid=rest_api_id, parentid=root_id, pathpart=resource['pathPart']) resource_hash[resource['path']] = resource_data['id'] for method in resource['resourceMethods']: apigateway.compare_method( restid=rest_api_id, resourceid=resource_hash[resource['path']], method=method, json_data=resource['resourceMethods'][method]) resource_hash.pop(resource['path']) for remaining_resource in resource_hash: logger.warning( "Need to delete following resources since not defined") apigateway.delete_resource( restid=rest_api_id, resourceid=resource_hash[remaining_resource])
def query_information(self, query): ''' This function is used to print debug information about a query, to see if it was succesful or not :param query: The boto3 query :return: Query with removed metadata ''' if query['ResponseMetadata']['HTTPStatusCode'] == 201: logger.debug("Resource was succesfully created") logger.info("Query RequestID: %s, HTTPStatusCode: %s" % (query['ResponseMetadata']['RequestId'], query['ResponseMetadata']['HTTPStatusCode'])) elif query['ResponseMetadata']['HTTPStatusCode'] == 202: logger.debug('Request accepted but processing later.') logger.info("Query RequestID: %s, HTTPStatusCode: %s" % (query['ResponseMetadata']['RequestId'], query['ResponseMetadata']['HTTPStatusCode'])) elif query['ResponseMetadata']['HTTPStatusCode'] == 204: logger.debug('Request done but no content returned.') logger.info("Query RequestID: %s, HTTPStatusCode: %s" % (query['ResponseMetadata']['RequestId'], query['ResponseMetadata']['HTTPStatusCode'])) elif query['ResponseMetadata']['HTTPStatusCode'] != 200: logger.warning('There was an issue with request.') logger.warning("Query RequestID: %s, HTTPStatusCode: %s" % (query['ResponseMetadata']['RequestId'], query['ResponseMetadata']['HTTPStatusCode'])) else: logger.debug("Request had no issues") logger.debug("Query RequestID: %s, HTTPStatusCode: %s" % (query['ResponseMetadata']['RequestId'], query['ResponseMetadata']['HTTPStatusCode'])) query.pop('ResponseMetadata') if 'NextToken' in query: logger.error("Token is present. Paging needs to be implemented") return query
def gather_information_for_cloudofrmation_parameters(stack_data, vpc, ami): parameters = [] env = Misc.get_value_from_array_hash(dictlist=vpc.get('Tags'), key="Environment") if 'cloudformation_parameters' in stack_data: for parameter in stack_data['cloudformation_parameters']: if parameter["ParameterKey"] == "Environment": parameters.append({"ParameterKey": "Environment", "ParameterValue": env, "UsePreviousValue": False}) elif parameter["ParameterKey"] == "InstanceType": instance = None if 'instance_type' in stack_data and env in stack_data['instance_type']: instance = stack_data["instance_type"][env] else: instance = Misc.get_value_from_array_hash(dictlist=ami.get('Tags'), key="Instancetype") parameters.append( {"ParameterKey": "InstanceType", "ParameterValue": instance, "UsePreviousValue": False}) elif parameter["ParameterKey"] == "Puppetrole": parameters.append({"ParameterKey": "Puppetrole", "ParameterValue": stack_data['puppet_role'], "UsePreviousValue": False}) elif parameter["ParameterKey"] == "XivelyService": parameters.append({"ParameterKey": "XivelyService", "ParameterValue": stack_data['xively_service'], "UsePreviousValue": False}) elif parameter["ParameterKey"] == "Ami": parameters.append( {"ParameterKey": "Ami", "ParameterValue": stack_data['ami'], "UsePreviousValue": False}) elif parameter["ParameterKey"] == "KeyName": key = Misc.get_value_from_array_hash(dictlist=vpc.get('Tags'), key="Keypair") parameters.append({"ParameterKey": "KeyName", "ParameterValue": key, "UsePreviousValue": False}) else: parameter["UsePreviousValue"] = False parameters.append(parameter) else: logger.warning(msg="No cloudformation parameter object in json") logger.debug(msg="Cloudformation parameters is: %s" % (parameters,)) return parameters
def create_integration_response(self, restid, resourceid, method, statuscode, further_opts=None): """ This function creates an integration response object :param method: the method that is requested :type method: basestring :param restid: the id of the rest api object :type restid: basestring :param resourceid: id of a single resource object :type resourceid: basestring :param statuscode: thestatus code to attach integration response :type statuscode: basestring :param further_opts: This opt passes in json_data fur not mandatory options :type further_opts: dict :return: """ if self.dryrun: logger.info("Dryrun requested no changes will be done") return None opts = {'restApiId': restid, 'resourceId': resourceid, 'httpMethod': method, 'statusCode': statuscode} for element in ['selectionPattern', 'responseParameters', 'responseTemplates']: if element in further_opts: if further_opts[element] == "None": opts[element] = None else: opts[element] = further_opts[element] logger.debug("The opts sent to create integration response %s" % opts) resp = self.apigateway_client.put_integration_response(**opts) super(Apigateway, self).query_information(query=resp) return resp
def elb_listener_cert(listeners=None): res = [] for listener in listeners: if 'Listener' in listener and 'SSLCertificateId' in listener['Listener']: res.append(listener['Listener']['SSLCertificateId']) logger.debug("Returning certs: %s" % res, ) return res
def __init__(self, session): """ This function creates the initial client and resource objects :param session: a boto3 session object for connecting to aws :return: a wrapper.Elb object for running wrapper commands """ logger.debug("Starting Elb wrapper") self.elb_client = session.client(service_name="elb")
def check_auto_scaling_group_exists(self, auto_scaling_group_name=None): asg = self.get_auto_scaling_groups(auto_scaling_group_name) for g in asg: if auto_scaling_group_name[0] == g['AutoScalingGroupName']: logger.debug("Launch Configuration exists: %s" % auto_scaling_group_name) return True return False
def recieve_msg(self, url=None, filter=None): logger.debug("Recieving messages for url : %s" % (url, )) if filter: resp = self.boto3.recieve_message(QueueUrl=url, MessageAttributeNames=filter) else: resp = self.boto3.recieve_message(QueueUrl=url) return resp['Messages']
def __init__(self): logger.debug("Starting Class for Cloudwatch") try: config_file = open("%s/etc/aws.conf" % (os.environ['KERRIGAN_ROOT'],), 'r') self.yaml = yaml.load(config_file) except IOError as e: logger.error("aws.conf I/O error({0}): {1}".format(e.errno, e.strerror)) self.cloudwatch = boto3.client('cloudwatch', region_name='us-east-1')
def __init__(self, session): """ This function creates the initial client and resource objects :param session: a boto3 session object for connecting to aws :return: a wrapper.Route53 object for running wrapper commands """ logger.debug("Starting route53 wrapper") self.route53_client = session.client(service_name="route53") self.route53_resource = session.resource(service_name="route53")
def __init__(self, arguments): """ This function creates the initial client and resource objects :param arguments: Arguments for a boto3 session object :type arguments: dict :return: a boto3.session object for wrapper commands """ logger.debug("Starting Session object") self.session = boto3.session.Session(**arguments)
def __init__(self, session): ''' This function creates the initial client and resource objects :param session: a boto3 session object for connecting to aws :return: a wrapper.S3 object for running wrapper commands ''' logger.debug("Starting iam wrapper") self.s3_client = session.client(service_name="s3") self.s3_resource = session.resource(service_name="s3")
def ssl_ports_in_elb(elb=None): ports = [] for listener in elb['ListenerDescriptions']: if 'SSLCertificateId' in listener['Listener']: logger.debug("Found SSL port in elb") ports.append(listener['Listener']['LoadBalancerPort']) else: logger.debug("This is not an SSL listener") return ports
def translate_security_group_ip_address_in_cloudformation(cloudformation_json, env_cidr): for resource_name in cloudformation_json['Resources']: logger.debug(msg="Iterating over %s for sg translation" % (resource_name,)) if 'Type' not in cloudformation_json['Resources'][resource_name] or 'AWS::EC2::SecurityGroup' != \ cloudformation_json['Resources'][resource_name]['Type']: continue cloudformation_json['Resources'][resource_name] = translate_security_group( security_group=cloudformation_json['Resources'][resource_name], env_cidr=env_cidr) return cloudformation_json
def validate_kerrigan_json(stack_data, env): # do some tests logger.debug(msg="Validating if stack can be deployed to env") if 'envs' in stack_data and env in stack_data['envs']: logger.info(msg="Stack can be deployed to environment %s" % (env,)) else: logger.error(msg="Stack should not be deployed to Environment %s" % (env,)) exit(20) return stack_data
def __init__(self, session): """ This function creates the initial client and resource objects :param session: a boto3 session object for connecting to aws :return: a wrapper.Autoscale object for running wrapper commands """ logger.debug("Starting Autoscale wrapper") self.autoscale_client = session.client(service_name="autoscale") self.autoscale_resource = session.resource(service_name="autoscale")
def get_account_information(self): ''' This def returns the session object for boto3, cli arguments provided and used and logger_arguments :rtype: Dict :return: Session object for wrapper and core class ''' [arguments, logger_arguments] = cli_argument_parse() logger.debug("Arguments provided: %s" % arguments, ) s = Session(arguments=arguments).session return {'session': s, 'logger_arguments': logger_arguments, 'cli_arguments': arguments}
def change_record_for_zoneid(self, zoneid=None, changebatch=None, dryrun=None): if dryrun: logger.debug("Dryrun requested") logger.warning("Not running changebatch: %s" % changebatch, ) return True ret = self.boto3.change_resource_record_sets(HostedZoneId=zoneid, ChangeBatch=changebatch) if ret['ResponseMetadata']['HTTPStatusCode'] == 200: logger.info("Change was succesful") else: logger.error("There was an issue with the change")
def __init__(self): logger.debug("Starting awsrequests") try: config_file = open( "%s/etc/aws.conf" % (os.environ['KERRIGAN_ROOT'], ), 'r') self.yaml = yaml.load(config_file) except IOError as e: logger.error("aws.conf I/O error({0}): {1}".format( e.errno, e.strerror)) self.owner = self.yaml['owner']
def get_elb_from_env(self, env=None): v = Vpc() vpc = v.get_vpc_from_env(env=env) elbs = self.get_all_elbs() ret = [] for elb in elbs: if elb['VPCId'] == vpc.get('VpcId'): ret.append(elb) logger.debug("Elbs in env %s : %s" % (env, ret)) return ret
def __init__(self): logger.debug("Starting Class for Cloudwatch") try: config_file = open( "%s/etc/aws.conf" % (os.environ['KERRIGAN_ROOT'], ), 'r') self.yaml = yaml.load(config_file) except IOError as e: logger.error("aws.conf I/O error({0}): {1}".format( e.errno, e.strerror)) self.cloudwatch = boto3.client('cloudwatch', region_name='us-east-1')
def __init__(self, session, dryrun=False): ''' This function creates the initial client and resource objects :param session: a boto3 session object for connecting to aws :return: a wrapper.Dynamodb object for running wrapper commands ''' logger.debug("Starting apigateway wrapper") self.apigateway_client = session.client(service_name="apigateway") self.dryrun = dryrun logger.debug("Dryrun value is %s" % self.dryrun)
def get_sgs_for_elb(self, env=None, name=None): ec2 = Ec2() vpc = Vpc() v = vpc.get_vpc_from_env(env=env) sgs = ec2.get_security_groups( filters=[{'Name': 'tag:ELB', 'Values': [name]}, {'Name': 'vpc-id', 'Values': [v.get('VpcId')]}]) res = [] for sg in sgs: res.append(sg.get('GroupId')) logger.debug("Sgs for the elb are %s" % res, ) return res
def listener_has_cert(listeners=None, cert=None): for listener in listeners: if 'Listener' in listener and 'SSLCertificateId' in listener['Listener']: logger.info("listener has certificate ID, inspecting") if listener['Listener']['SSLCertificateId'] == cert: logger.info("Certificate is used for elb") return True else: logger.debug("ELB listener has different cert") else: logger.debug("ELB has no cert or listener") return False
def tag_resource(self, tags, instanceid): """ This function tags a ec2 resource :param tags: A dict of key:value tags :type tags: dict :param id: The id of the instance to tag :return: """ for key in tags.keys(): logger.debug("Adding tag to resource %s" % (key,)) ret = self.ec2_client.create_tags(Resources=[instanceid], Tags=[{'Key': key, 'Value': tags[key]}]) super(Ec2, self).query_information(query=ret)
def get_auto_scaling_groups(self, auto_scaling_groups=None): if auto_scaling_groups: resp = self.autoscale.describe_auto_scaling_groups( AutoScalingGroupNames=auto_scaling_groups) else: resp = self.autoscale.describe_auto_scaling_groups() result = [] for asg in resp['AutoScalingGroups']: logger.debug("Gathering info on %s" % (asg['AutoScalingGroupName'])) result.append(asg) logger.debug("Launch Configuration information: %s" % (asg)) return result
def get_launch_configs(self, launch_configs=None): if launch_configs: resp = self.autoscale.describe_launch_configurations( LaunchConfigurationNames=launch_configs) else: resp = self.autoscale.describe_launch_configurations() result = [] for lc in resp['LaunchConfigurations']: logger.debug("Gathering info on %s" % (lc['LaunchConfigurationName'])) result.append(lc) logger.debug("Launch Configuration information: %s" % (lc)) return result
def check_launch_config_exists(self, env=None, xively_service=None, stack=None): launch_config_name = self.generate_launch_config_name( env=env, stack=stack, xively_service=xively_service) lcs = self.get_launch_configs() for l in lcs: if launch_config_name == l['LaunchConfigurationName']: logger.debug("Launch Configuration exists: %s" % launch_config_name) return True return False
def ec2_instance_name_exist(self, name): """ This function is used to determine if an ec2 instance name is unique or already exists :param name: the name that should be tested :return: If none is returned the name already existed, if unique the name is returned """ reservations = self.get_ec2_instances(filters=[{'Name': 'tag:Name', 'Values': [name]}]) instances = [i for r in reservations for i in r.instances] ret = None if not instances: logger.debug("Generated name is '%s'" % (name,)) ret = name return ret
def get_userdata_for_os(ostype=None): # FIXME not tested """ This function retrieves the OS userdata that is provided by devops :param ostype: the ostype that is requested :type ostype: basestring :return: A string containing the userdata :rtype: basestring """ with open("%s/etc/%s_userdata" % (os.environ['KERRIGAN_ROOT'], ostype), 'r') as user_data_file: userdata = user_data_file.read() logger.debug("Userdata is : %s" % (userdata,)) return userdata
def translate_security_group_ip_address_in_cloudformation( cloudformation_json, env_cidr): for resource_name in cloudformation_json['Resources']: logger.debug(msg="Iterating over %s for sg translation" % (resource_name, )) if 'Type' not in cloudformation_json['Resources'][resource_name] or 'AWS::EC2::SecurityGroup' != \ cloudformation_json['Resources'][resource_name]['Type']: continue cloudformation_json['Resources'][ resource_name] = translate_security_group( security_group=cloudformation_json['Resources'][resource_name], env_cidr=env_cidr) return cloudformation_json
def cli_argument_parse(): # FIXME test this, need to mock sysargv somehow ''' This function parses and removes cli arguments that the argparse should not handle :return: an array with logger option and aws account options :rtype: array ''' logger.info("Parsing CLI arguments for global options") ret_logger = {'table': True, 'csv': False} ret = {} i = 0 while i < len(sys.argv): if i >= len(sys.argv): break if sys.argv[i] == '--aws_access_key': ret['aws_access_key_id'] = sys.argv[i + 1] sys.argv.pop(i + 1) sys.argv.pop(i) i -= 1 elif sys.argv[i] == '--aws_secret_key': ret['aws_secret_access_key'] = sys.argv[i + 1] sys.argv.pop(i + 1) sys.argv.pop(i) i -= 1 elif sys.argv[i] == '--aws_region': ret['region_name'] = sys.argv[i + 1] sys.argv.pop(i + 1) sys.argv.pop(i) i -= 1 elif sys.argv[i] == '--aws_account': ret['profile_name'] = sys.argv[i + 1] sys.argv.pop(i + 1) sys.argv.pop(i) i -= 1 if sys.argv[i] == '--table': logger.info("Table output is being used") ret_logger['table'] = True ret_logger['csv'] = False sys.argv.pop(i) i -= 1 elif sys.argv[i] == '--csv': logger.info("Csv output is being used") ret_logger['table'] = False ret_logger['csv'] = True sys.argv.pop(i) i -= 1 else: i += 1 logger.debug("Cli opts parsed: %s" % (ret,)) return [ret, ret_logger]
def get_all_elbs(self, elbs=None, with_tag=True): if elbs: resp = self.elb.describe_load_balancers(LoadBalancerNames=elbs) else: resp = self.elb.describe_load_balancers() result = [] for lb in resp['LoadBalancerDescriptions']: logger.debug("Gathering info on %s" % (lb['LoadBalancerName'],)) if with_tag: tags = self.get_elb_tags(name=lb['LoadBalancerName']) lb['Tags'] = tags result.append(lb) logger.debug("ELb information: %s" % (lb,)) return result
def get_userdata_for_os(ostype=None): # FIXME not tested """ This function retrieves the OS userdata that is provided by devops :param ostype: the ostype that is requested :type ostype: basestring :return: A string containing the userdata :rtype: basestring """ with open("%s/etc/%s_userdata" % (os.environ['KERRIGAN_ROOT'], ostype), 'r') as user_data_file: userdata = user_data_file.read() logger.debug("Userdata is : %s" % (userdata, )) return userdata
def get_zoneid_from_domain(self, domain=None): zones = self.list_hosted_zones() logger.debug("Searching for domain %s" % (domain,)) concat = domain + '.' logger.debug("Concated domain name is %s" % (concat,)) for zone in zones: if zone['Name'] == concat: if zone['Id'].startswith('/hostedzone/'): tmp = zone['Id'].replace('/hostedzone/', '') return tmp else: return zone['Id'] logger.error("Could not find zone with domain name") return None
def apigateway_exists(self, name): """ function checks if the apigateway exists or not :param name: name of the apigateway to test :type name: basestring :return: True if found, false if not :rtype: bool """ gw_id = self.get_rest_api_by_name(name=name) if gw_id: ret = True else: ret = False logger.debug("The apigateway %s status: %s" % (name, ret)) return ret
def get_rest_api_by_name(self, name): """ This function returns a rest api by name :param name: the name of the reste api to return :type name: basestring :return: a rest api top level object :rtype: object """ gws = self.get_rest_api() ret = None logger.debug("Searcing for rest api by name") for gw in gws: if gw['name'] == name: logger.info("Found the gw by name") ret = gw return ret