def info_all(self): # FIXME implement possbility to scope to env logger.info("Gathering all elbs") parser = argparse.ArgumentParser( description='ec2 tool for devops', usage='''kerrigan.py elb info_all [<args>]] ''' + self.global_options) args = parser.parse_args(sys.argv[3:]) e = awsrequests() res = e.elb_info_all() for r in res: if self.cli['csv']: r['Availability Zones'] = Misc.join_list_to_string( list=r['Availability Zones']) r['Securitygroups'] = Misc.join_list_to_string( list=r['Securitygroups']) r['InstanceIds'] = Misc.join_list_to_string( list=r['InstanceIds']) r['From-To-Protocol'] = Misc.join_list_to_string( list=r['From-To-Protocol']) elif self.cli['table']: r['Availability Zones'] = Misc.list_to_multiline_string( r['Availability Zones']) r['InstanceIds'] = Misc.list_to_multiline_string( r['InstanceIds']) r['Securitygroups'] = Misc.list_to_multiline_string( r['Securitygroups']) r['From-To-Protocol'] = Misc.list_to_multiline_string( r['From-To-Protocol']) logger.output(data=res, csvvar=self.cli['csv'], tablevar=self.cli['table'])
def info(self): logger.info("Starting to gather information") a = awsrequests() parser = argparse.ArgumentParser( description='ec2 tool for devops', usage='''kerrigan.py route53 info [<args>]] ''' + self.global_options) parser.add_argument('--env', action='store', help="Environment to gather information about") args = parser.parse_args(sys.argv[3:]) res = a.route53_info(env=args.env) for r in res: if self.cli['csv']: r['Values'] = Misc.join_list_to_string(list=r['Values']) elif self.cli['table']: r['Values'] = Misc.list_to_multiline_string(r['Values']) else: logger.error( 'There is an unhandled printing. Need to investigate') # Add information if not present: if 'Weight' not in r: r['Weight'] = '-' r['SetIdentifier'] = '-' logger.output(data=res, csvvar=self.cli['csv'], tablevar=self.cli['table'])
def upload_apigateway(self): ''' This function is a wrapper for parsing arguments and uploading apigateway :return: ''' logger.info("Started upload command") a = awsrequests(session=self.account_information['session']) parser = argparse.ArgumentParser( description='upload apigateway', usage='''kerrigan.py apigateway upload_apigateway [<args>]] ''' + self.global_options, formatter_class=argparse.ArgumentDefaultsHelpFormatter, prog="kerrigan") parser.add_argument('--json', metavar='FILE', required=True, type=lambda x: Misc.is_valid_file(parser, x), help="Which file to upload") parser.add_argument('--dryrun', action="store_true", default=False, help="No changes should be done") args = parser.parse_args(sys.argv[3:]) result = a.upload_apigateway(json=Misc.parse_file_to_json(args.json), dryrun=args.dryrun)
def instance_status(self): logger.info("Started instance status command") a = awsrequests(session=self.account_information['session']) parser = argparse.ArgumentParser( description='instance status about ami', usage='''kerrigan.py ami instance_Status [<args>]] ''' + self.global_options, formatter_class=argparse.ArgumentDefaultsHelpFormatter, prog="kerrigan") parser.add_argument('--imageid', action='store', default=None, help="Only imageIds that should be queried") args = parser.parse_args(sys.argv[3:]) result = a.image_instance_status(imageid=args.imageid) for res in result: if self.account_information['logger_arguments']['table']: res['Instances'] = Misc.list_to_multiline_string( list=res['Instances']) else: res['Instances'] = Misc.join_list_to_string( list=res['Instances']) logger.output( data=result, csvvar=self.account_information['logger_arguments']['csv'], tablevar=self.account_information['logger_arguments']['table'])
def report_elb(self): ''' This function is a wrapper for parsing arguments and printing for elb attribute reports Tested :return: ''' logger.info("Started report generation command") a = awsrequests(session=self.account_information['session']) parser = argparse.ArgumentParser(description='report generation about elbs', usage='''kerrigan.py elb report_elb [<args>]] ''' + self.global_options, formatter_class=argparse.ArgumentDefaultsHelpFormatter, prog="kerrigan") parser.add_argument('--columns', action='store', default=a.service_supported_columns(service="elb"), help="Which columns to display") parser.add_argument('--filters', action='store', default=None, help="The filters that should be used, example: key1:value1,key2:value2") args = parser.parse_args(sys.argv[3:]) columns = Misc.parse_service_columns(columns=args.columns, service="elb") if args.filters: filters = Misc.format_boto3_filter(filters=args.filters) else: filters = None result = a.information_elbs(columns=columns, filters=filters) for res in result: if self.account_information['logger_arguments']['table']: res['AvailabilityZones'] = Misc.list_to_multiline_string(list=res['AvailabilityZones']) res['SecurityGroups'] = Misc.list_to_multiline_string(list=res['SecurityGroups']) res['Instances'] = Misc.list_to_multiline_string(list=res['Instances']) else: res['AvailabilityZones'] = Misc.join_list_to_string(list=res['AvailabilityZones']) res['SecurityGroups'] = Misc.join_list_to_string(list=res['SecurityGroups']) res['Instances'] = Misc.join_list_to_string(list=res['Instances']) logger.output(data=result, csvvar=self.account_information['logger_arguments']['csv'], tablevar=self.account_information['logger_arguments']['table'])
def report_s3(self): ''' This function is a wrapper for parsing arguments and printing for s3 attribute reports :return: ''' logger.info("Started report generation command") a = awsrequests(session=self.account_information['session']) parser = argparse.ArgumentParser( description='report generation about s3', usage='''kerrigan.py s3 report_s3 [<args>]] ''' + self.global_options, formatter_class=argparse.ArgumentDefaultsHelpFormatter, prog="kerrigan") parser.add_argument( '--columns', action='store', default=a.service_supported_columns(service="s3").keys(), help="Which columns to display") parser.add_argument( '--filters', action='store', default=None, help= "The filters that should be used, example: key1:value1,key2:value2" ) args = parser.parse_args(sys.argv[3:])
def report_kinesis(self): ''' This function is a wrapper for parsing arguments and printing for kinesis attribute reports :return: ''' logger.info("Started report generation command") a = awsrequests(session=self.account_information['session']) parser = argparse.ArgumentParser( description='report generation about kinesis', usage='''kerrigan.py kinesis report_kinesis [<args>]] ''' + self.global_options, formatter_class=argparse.ArgumentDefaultsHelpFormatter, prog="kerrigan") parser.add_argument( '--columns', action='store', default=a.service_supported_columns(service="kinesis").keys(), help="Which columns to display") args = parser.parse_args(sys.argv[3:]) columns = Misc.parse_service_columns(service="kinesis", columns=args.columns) result = a.information_kinesis(columns=columns) logger.output( data=result, csvvar=self.account_information['logger_arguments']['csv'], tablevar=self.account_information['logger_arguments']['table'])
def delete_server_cert(self): logger.info("Going to delete cert") parser = argparse.ArgumentParser(description='ec2 tool for devops', usage='''kerrigan.py iam delete_server_cert [<args>]] ''' + self.global_options) parser.add_argument('--cert_name', action='store', help="Name of certificate to delete") args = parser.parse_args(sys.argv[3:]) a = awsrequests() a.server_certificate_delete(cert_name=args.cert_name)
def list_users(self): logger.info("Going to list users") parser = argparse.ArgumentParser(description='ec2 tool for devops', usage='''kerrigan.py iam list_users [<args>]] ''' + self.global_options) args = parser.parse_args(sys.argv[3:]) a = awsrequests() res = a.list_iam_users() logger.output(data=res, csvvar=self.cli['csv'], tablevar=self.cli['table'])
def list_buckets(self): logger.info("Starting to gather information") a = awsrequests() parser = argparse.ArgumentParser(description='ec2 tool for devops', usage='''kerrigan.py s3 list_buckets [<args>]] ''' + self.global_options) parser.add_argument('--extended', action='store_true', default=False, help="Gather extended info about Buckets") args = parser.parse_args(sys.argv[3:]) res = a.list_s3_buckets(extended=args.extended) logger.output(data=res, csvvar=self.cli['csv'], tablevar=self.cli['table'])
def compare(self): logger.info("Going to compare security groups") a = awsrequests() parser = argparse.ArgumentParser(description='ec2 tool for devops', usage='''kerrigan.py instance compare_securitygroups [<args>]] ''' + self.global_options) parser.add_argument('--env', action='store', help="Environment to check") parser.add_argument('--dryrun', action='store_true', default=False, help="No actions should be taken") args = parser.parse_args(sys.argv[3:]) a = awschecks() a.compare_securitygroups(env=args.env, dryrun=args.dryrun)
def create_user_credentials(self): logger.info("Going to create user credentials") parser = argparse.ArgumentParser(description='ec2 tool for devops', usage='''kerrigan.py iam create_user_credentials [<args>]] ''' + self.global_options) parser.add_argument('--username', action='store', help="Username to create credentials for", required=True) parser.add_argument('--dryrun', action='store_true', default=False, help="No changes should be done") args = parser.parse_args(sys.argv[3:]) a = awsrequests() resp = a.create_user_credentials(username=args.username,dryrun=args.dryrun) logger.output(data=resp, csvvar=self.cli['csv'], tablevar=self.cli['table'])
def overflow(self): logger.info("Started stack provision command") a = awsrequests(session=self.account_information['session']) parser = argparse.ArgumentParser(description='stack provision', usage='''kerrigan.py stack overflow [<args>]] ''' + self.global_options, formatter_class=argparse.ArgumentDefaultsHelpFormatter, prog="kerrigan") parser.add_argument('--json', action='store', required=True, help="Stack json to provision") parser.add_argument('--env', action='store', required=True, help="Which env to deploy to") parser.add_argument('--dryrun', action='store_true', default=False, help="No changes should be done") args = parser.parse_args(sys.argv[3:]) a.deploy_stack_to_env(env=args.env, file=args.json, dryrun=args.dryrun)
def update(self): logger.info("Going to update env certs") parser = argparse.ArgumentParser(description='ec2 tool for devops', usage='''kerrigan.py iam update [<args>]] ''' + self.global_options) parser.add_argument('--domain', action='store', required=True, help="Which domain cert to update: example: star.dev.xively.com") parser.add_argument('--intermediate', action='store_true', default=False, help="Should intermediate certificate be uploaded") args = parser.parse_args(sys.argv[3:]) a = awsrequests() a.server_certficate_update(domain=args.domain, intermediate=args.intermediate)
def info_bucket(self): logger.info("Starting to gather information") a = awsrequests() parser = argparse.ArgumentParser(description='ec2 tool for devops', usage='''kerrigan.py s3 info_bucket [<args>]] ''' + self.global_options) parser.add_argument('--name', action='store', help="Name of bucket", required=True) parser.add_argument('--level', action='store', choices=['acl', 'lifecycle', 'region', 'logging', 'policy', 'replication','tagging'], help="What information to return") args = parser.parse_args(sys.argv[3:]) res = a.info_s3_bucket(name=args.name, choice=args.level) logger.output(data=res, csvvar=self.cli['csv'], tablevar=self.cli['table'])
def report_elb(self): ''' This function is a wrapper for parsing arguments and printing for elb attribute reports Tested :return: ''' logger.info("Started report generation command") a = awsrequests(session=self.account_information['session']) parser = argparse.ArgumentParser( description='report generation about elbs', usage='''kerrigan.py elb report_elb [<args>]] ''' + self.global_options, formatter_class=argparse.ArgumentDefaultsHelpFormatter, prog="kerrigan") parser.add_argument('--columns', action='store', default=a.service_supported_columns(service="elb"), help="Which columns to display") parser.add_argument( '--filters', action='store', default=None, help= "The filters that should be used, example: key1:value1,key2:value2" ) args = parser.parse_args(sys.argv[3:]) columns = Misc.parse_service_columns(columns=args.columns, service="elb") if args.filters: filters = Misc.format_boto3_filter(filters=args.filters) else: filters = None result = a.information_elbs(columns=columns, filters=filters) for res in result: if self.account_information['logger_arguments']['table']: res['AvailabilityZones'] = Misc.list_to_multiline_string( list=res['AvailabilityZones']) res['SecurityGroups'] = Misc.list_to_multiline_string( list=res['SecurityGroups']) res['Instances'] = Misc.list_to_multiline_string( list=res['Instances']) else: res['AvailabilityZones'] = Misc.join_list_to_string( list=res['AvailabilityZones']) res['SecurityGroups'] = Misc.join_list_to_string( list=res['SecurityGroups']) res['Instances'] = Misc.join_list_to_string( list=res['Instances']) logger.output( data=result, csvvar=self.account_information['logger_arguments']['csv'], tablevar=self.account_information['logger_arguments']['table'])
def upload_server_cert(self): logger.info("Going to upload a cert") parser = argparse.ArgumentParser(description='ec2 tool for devops', usage='''kerrigan.py iam upload_server_cert [<args>]] ''' + self.global_options) parser.add_argument('--cert_name', action='store', required=True, help="Name of certificate to upload, should be CN of domain") parser.add_argument('--pub_key', action='store', help="File containing public key") parser.add_argument('--priv_key', action='store', help="File containing private key") parser.add_argument('--cert_chain', action='store', help="File containing certificate chain") args = parser.parse_args(sys.argv[3:]) a = awsrequests() a.server_certificate_upload(cert_name=args.cert_name, pub_key=args.pub_key, priv_key=args.priv_key, cert_chain=args.cert_chain)
def dump_apigateway(self): ''' This function is a wrapper for parsing arguments and printing for apigateway attribute reports :return: ''' logger.info("Started report generation command") a = awsrequests(session=self.account_information['session']) parser = argparse.ArgumentParser(description='report generation about apigateway', usage='''kerrigan.py apigateway report_apigateway [<args>]] ''' + self.global_options, formatter_class=argparse.ArgumentDefaultsHelpFormatter, prog="kerrigan") parser.add_argument('--name', required=True, action='store', help="Name of apigateway to dump") args = parser.parse_args(sys.argv[3:]) result = a.dump_apigateway(name=args.name) print result
def deploy(self): logger.info("Started deployment command") a = awsrequests(session=self.account_information['session']) parser = argparse.ArgumentParser( description='deployment for stack in env', usage='''kerrigan.py stack deploy [<args>]] ''' + self.global_options, formatter_class=argparse.ArgumentDefaultsHelpFormatter, prog="kerrigan") parser.add_argument('--env', action='store', required=True, help="Which env to deploy to") parser.add_argument('--puppet_role', action='store', required=True, help="Puppet_role to deploy") parser.add_argument('--dry_run', action='store_true', default=False, help="No changes should be done") parser.add_argument('--xively_service', action='store', default=None, help="Xively_service to deploy") parser.add_argument('--num', action='store', default=1, help="Number of instances to spin up") parser.add_argument('--instance_type', action='store', help="Requested instance size") parser.add_argument('--base_ami', action='store', default="", choices=a.get_ami_stacks(), help="Base Ami to use") parser.add_argument('--iam', action='store', help="Custom Iam role to use") parser.add_argument('--requester', action='store', default="", help="The person requesting the machine") parser.add_argument('--customer', action='store', default="", help="Customer associated with the gateway") args = parser.parse_args(sys.argv[3:]) a.prepare_deployment(**vars(args))
def report_route53(self): ''' This function is a wrapper for parsing arguments and printing for route53 attribute reports :return: ''' logger.info("Started report generation command") a = awsrequests(session=self.account_information['session']) parser = argparse.ArgumentParser(description='report generation about route53', usage='''kerrigan.py route53 report_route53 [<args>]] ''' + self.global_options, formatter_class=argparse.ArgumentDefaultsHelpFormatter, prog="kerrigan") parser.add_argument('--columns', action='store', default=a.service_supported_columns(service="route53").keys(), help="Which columns to display") parser.add_argument('--filters', action='store', default=None, help="The filters that should be used, example: key1:value1,key2:value2") args = parser.parse_args(sys.argv[3:])
def upload_apigateway(self): ''' This function is a wrapper for parsing arguments and uploading apigateway :return: ''' logger.info("Started upload command") a = awsrequests(session=self.account_information['session']) parser = argparse.ArgumentParser(description='upload apigateway', usage='''kerrigan.py apigateway upload_apigateway [<args>]] ''' + self.global_options, formatter_class=argparse.ArgumentDefaultsHelpFormatter, prog="kerrigan") parser.add_argument('--json', metavar='FILE', required=True, type=lambda x: Misc.is_valid_file(parser, x), help="Which file to upload") parser.add_argument('--dryrun', action="store_true",default=False,help="No changes should be done") args = parser.parse_args(sys.argv[3:]) result = a.upload_apigateway(json=Misc.parse_file_to_json(args.json),dryrun=args.dryrun)
def snappy(self): logger.info("Started snappy command") a = awsrequests(session=self.account_information['session']) parser = argparse.ArgumentParser(description='deployment for snappy', usage='''kerrigan.py stack snappy [<args>]] ''' + self.global_options, formatter_class=argparse.ArgumentDefaultsHelpFormatter, prog="kerrigan") parser.add_argument('--num', action='store', default=1, help="Number of instances to spin up") parser.add_argument('--env', action='store', required=True, help="Which env to deploy to") parser.add_argument('--dryrun', action='store_true', default=False, help="No changes should be done") parser.add_argument('--accountid', action='store', required=True, help="The account id for snappy") parser.add_argument('--channelname', action='store', required=True, help="The channelname for snappy") parser.add_argument('--newrelic', action='store', required=True, help="The newrelic environment to use") parser.add_argument('--devicestring', action='store', required=True, help="The deviec string like deploy/nots") parser.add_argument('--branch', action='store', default="None", help="The branch to checkout from") args = parser.parse_args(sys.argv[3:]) a.deploy_snappy(**vars(args))
def info_certs(self): logger.info("Going to list all certs") parser = argparse.ArgumentParser(description='ec2 tool for devops', usage='''kerrigan.py iam info_all [<args>]] ''' + self.global_options) args = parser.parse_args(sys.argv[3:]) a = awsrequests() res = a.server_certificates_info_all() for r in res: if self.cli['csv']: r['ELB'] = Misc.join_list_to_string(list=r['ELB']) elif self.cli['table']: r['ELB'] = Misc.list_to_multiline_string(r['ELB']) else: logger.error('There is an unhandled printing. Need to investigate') logger.output(data=res, csvvar=self.cli['csv'], tablevar=self.cli['table'])
def report_apigateway(self): ''' This function is a wrapper for parsing arguments and printing for apigateway attribute reports :return: ''' logger.info("Started report generation command") a = awsrequests(session=self.account_information['session']) parser = argparse.ArgumentParser(description='report generation about apigateway', usage='''kerrigan.py apigateway report_apigateway [<args>]] ''' + self.global_options, formatter_class=argparse.ArgumentDefaultsHelpFormatter, prog="kerrigan") parser.add_argument('--columns', action='store', default=a.service_supported_columns(service="sg").keys(), help="Which columns to display") args = parser.parse_args(sys.argv[3:]) columns = Misc.parse_service_columns(service="apigateway", columns=args.columns) result = a.information_apigateway(columns=columns) logger.output(data=result, csvvar=self.account_information['logger_arguments']['csv'], tablevar=self.account_information['logger_arguments']['table'])
def list_buckets(self): logger.info("Starting to gather information") a = awsrequests() parser = argparse.ArgumentParser( description='ec2 tool for devops', usage='''kerrigan.py s3 list_buckets [<args>]] ''' + self.global_options) parser.add_argument('--extended', action='store_true', default=False, help="Gather extended info about Buckets") args = parser.parse_args(sys.argv[3:]) res = a.list_s3_buckets(extended=args.extended) logger.output(data=res, csvvar=self.cli['csv'], tablevar=self.cli['table'])
def instance_status(self): logger.info("Started instance status command") a = awsrequests(session=self.account_information['session']) parser = argparse.ArgumentParser(description='instance status about ami', usage='''kerrigan.py ami instance_Status [<args>]] ''' + self.global_options, formatter_class=argparse.ArgumentDefaultsHelpFormatter, prog="kerrigan") parser.add_argument('--imageid', action='store', default=None, help="Only imageIds that should be queried") args = parser.parse_args(sys.argv[3:]) result = a.image_instance_status(imageid=args.imageid) for res in result: if self.account_information['logger_arguments']['table']: res['Instances'] = Misc.list_to_multiline_string(list=res['Instances']) else: res['Instances'] = Misc.join_list_to_string(list=res['Instances']) logger.output(data=result, csvvar=self.account_information['logger_arguments']['csv'], tablevar=self.account_information['logger_arguments']['table'])
def list_user_groups(self): logger.info("Going to list groups") parser = argparse.ArgumentParser(description='ec2 tool for devops', usage='''kerrigan.py iam list_user_groups [<args>]] ''' + self.global_options) parser.add_argument('--username', action='store', default=None, help="Should intermediate certificate be uploaded") args = parser.parse_args(sys.argv[3:]) a = awsrequests() res = a.list_user_groups(username=args.username) out = [] for r in res: if self.cli['csv']: res[r] = Misc.join_list_to_string(list=res[r]) elif self.cli['table']: res[r] = Misc.list_to_multiline_string(res[r]) out.append({'Username': r, 'Groups': res[r]}) logger.output(data=out, csvvar=self.cli['csv'], tablevar=self.cli['table'])
def compare(self): logger.info("Going to compare security groups") a = awsrequests() parser = argparse.ArgumentParser( description='ec2 tool for devops', usage='''kerrigan.py instance compare_securitygroups [<args>]] ''' + self.global_options) parser.add_argument('--env', action='store', help="Environment to check") parser.add_argument('--dryrun', action='store_true', default=False, help="No actions should be taken") args = parser.parse_args(sys.argv[3:]) a = awschecks() a.compare_securitygroups(env=args.env, dryrun=args.dryrun)
def terminate_ec2(self): """ This function is a wrapper for parsing arguments and printing for ec2 instance termination Tested :return: """ a = awsrequests(session=self.account_information['session']) parser = argparse.ArgumentParser(description='creation of ec2 instance', usage='''kerrigan.py ec2 create_ec2 [<args>]] ''' + self.global_options, formatter_class=argparse.ArgumentDefaultsHelpFormatter, prog="kerrigan") parser.add_argument('--dry_run', action='store_true', default=False, help="For testing purpose only") parser.add_argument('--instanceids', action='store', required=True, help="A comma seperated list of instance id-s") args = parser.parse_args(sys.argv[3:]) instanceids = Misc.string_to_array(string=args.instanceids, split_char=",") result = a.terminate_instance(dryrun=args.dry_run, instanceids=instanceids) logger.output(data=result, csvvar=self.account_information['logger_arguments']['csv'], tablevar=self.account_information['logger_arguments']['table'])
def deploy(self): logger.info("Started deployment command") a = awsrequests(session=self.account_information['session']) parser = argparse.ArgumentParser(description='deployment for stack in env', usage='''kerrigan.py stack deploy [<args>]] ''' + self.global_options, formatter_class=argparse.ArgumentDefaultsHelpFormatter, prog="kerrigan") parser.add_argument('--env', action='store', required=True, help="Which env to deploy to") parser.add_argument('--puppet_role', action='store', required=True, help="Puppet_role to deploy") parser.add_argument('--dry_run', action='store_true', default=False, help="No changes should be done") parser.add_argument('--xively_service', action='store', default=None, help="Xively_service to deploy") parser.add_argument('--num', action='store', default=1, help="Number of instances to spin up") parser.add_argument('--instance_type', action='store', help="Requested instance size") parser.add_argument('--base_ami', action='store', default="", choices=a.get_ami_stacks(), help="Base Ami to use") parser.add_argument('--iam', action='store', help="Custom Iam role to use") parser.add_argument('--requester', action='store', default="", help="The person requesting the machine") parser.add_argument('--customer', action='store', default="", help="Customer associated with the gateway") args = parser.parse_args(sys.argv[3:]) a.prepare_deployment(**vars(args))
def snappy(self): logger.info("Started snappy command") a = awsrequests(session=self.account_information['session']) parser = argparse.ArgumentParser( description='deployment for snappy', usage='''kerrigan.py stack snappy [<args>]] ''' + self.global_options, formatter_class=argparse.ArgumentDefaultsHelpFormatter, prog="kerrigan") parser.add_argument('--num', action='store', default=1, help="Number of instances to spin up") parser.add_argument('--env', action='store', required=True, help="Which env to deploy to") parser.add_argument('--dryrun', action='store_true', default=False, help="No changes should be done") parser.add_argument('--accountid', action='store', required=True, help="The account id for snappy") parser.add_argument('--channelname', action='store', required=True, help="The channelname for snappy") parser.add_argument('--newrelic', action='store', required=True, help="The newrelic environment to use") parser.add_argument('--devicestring', action='store', required=True, help="The deviec string like deploy/nots") parser.add_argument('--branch', action='store', default="None", help="The branch to checkout from") args = parser.parse_args(sys.argv[3:]) a.deploy_snappy(**vars(args))
def info_all(self): # FIXME implement possbility to scope to env logger.info("Gathering all elbs") parser = argparse.ArgumentParser(description='ec2 tool for devops', usage='''kerrigan.py elb info_all [<args>]] ''' + self.global_options) args = parser.parse_args(sys.argv[3:]) e = awsrequests() res = e.elb_info_all() for r in res: if self.cli['csv']: r['Availability Zones'] = Misc.join_list_to_string(list=r['Availability Zones']) r['Securitygroups'] = Misc.join_list_to_string(list=r['Securitygroups']) r['InstanceIds'] = Misc.join_list_to_string(list=r['InstanceIds']) r['From-To-Protocol'] = Misc.join_list_to_string(list=r['From-To-Protocol']) elif self.cli['table']: r['Availability Zones'] = Misc.list_to_multiline_string(r['Availability Zones']) r['InstanceIds'] = Misc.list_to_multiline_string(r['InstanceIds']) r['Securitygroups'] = Misc.list_to_multiline_string(r['Securitygroups']) r['From-To-Protocol'] = Misc.list_to_multiline_string(r['From-To-Protocol']) logger.output(data=res, csvvar=self.cli['csv'], tablevar=self.cli['table'])
def dump_apigateway(self): ''' This function is a wrapper for parsing arguments and printing for apigateway attribute reports :return: ''' logger.info("Started report generation command") a = awsrequests(session=self.account_information['session']) parser = argparse.ArgumentParser( description='report generation about apigateway', usage='''kerrigan.py apigateway report_apigateway [<args>]] ''' + self.global_options, formatter_class=argparse.ArgumentDefaultsHelpFormatter, prog="kerrigan") parser.add_argument('--name', required=True, action='store', help="Name of apigateway to dump") args = parser.parse_args(sys.argv[3:]) result = a.dump_apigateway(name=args.name) print result
def info(self): logger.info("Starting to gather information") a = awsrequests() parser = argparse.ArgumentParser(description='ec2 tool for devops', usage='''kerrigan.py route53 info [<args>]] ''' + self.global_options) parser.add_argument('--env', action='store', help="Environment to gather information about") args = parser.parse_args(sys.argv[3:]) res = a.route53_info(env=args.env) for r in res: if self.cli['csv']: r['Values'] = Misc.join_list_to_string(list=r['Values']) elif self.cli['table']: r['Values'] = Misc.list_to_multiline_string(r['Values']) else: logger.error('There is an unhandled printing. Need to investigate') # Add information if not present: if 'Weight' not in r: r['Weight'] = '-' r['SetIdentifier'] = '-' logger.output(data=res, csvvar=self.cli['csv'], tablevar=self.cli['table'])
def create_ec2(self): """ This function is a wrapper for parsing aguments and printing for ec2 create instance Tested :return: """ logger.info("Started create ec2 command") a = awsrequests(session=self.account_information['session']) parser = argparse.ArgumentParser(description='creation of ec2 instance', usage='''kerrigan.py ec2 create_ec2 [<args>]] ''' + self.global_options, formatter_class=argparse.ArgumentDefaultsHelpFormatter, prog="kerrigan") parser.add_argument('--puppet_role', action='store', default=None, help="Puppet_role to create", required=True) parser.add_argument('--env', action='store', default=None, choices=a.get_active_envs(), help="The environment the machine should provision to", required=True) parser.add_argument('--num', action='store', type=int, default=1, help="Number of instances to create") parser.add_argument('--requester', action='store', default="", help="The person requesting the machine") parser.add_argument('--customer', action='store', default="", help="Customer associated with the gateway") parser.add_argument('--xively_service', action='store', default="", help="The docker image running on the machine / Xively_service") parser.add_argument('--base_ami', action='store', default="", choices=a.get_ami_stacks(), help="Base Ami to use") parser.add_argument('--iam', action='store', help="Custom Iam role to use") parser.add_argument('--instance_type', action='store', help="Requested instance size") parser.add_argument('--dry_run', action='store_true', default=False, help="For testing purpose only") parser.add_argument('--shutdown', action='store', choices=["stop", "terminate"], default="stop", help="Shutdown action") parser.add_argument('--monitoring', action='store_true', default=False, help="Monitoring enabled -- Do not use") parser.add_argument('--keypair', action='store', default=None, help="Which keypair to use") parser.add_argument('--availability', action='store', default=None, help="Public/private availability, default is the AMI provided availability, valid values: public, private") parser.add_argument('--fillup', action='store_true', default=False, help="Instead of round robin, fillup algorithm should be used") args = parser.parse_args(sys.argv[3:]) result = a.create_ec2_instance(**vars(args)) for r in result: print "Instance Id: %s" % (r.get('InstanceId'),)
def report_ami(self): ''' This function is a wrapper for parsing arguments and printing for ami attribute reports :return: ''' logger.info("Started report generation command") a = awsrequests(session=self.account_information['session']) parser = argparse.ArgumentParser(description='report generation about ami', usage='''kerrigan.py ami report_ami [<args>]] ''' + self.global_options, formatter_class=argparse.ArgumentDefaultsHelpFormatter, prog="kerrigan") parser.add_argument('--columns', action='store', default=a.service_supported_columns(service="ami").keys(), help="Which columns to display") parser.add_argument('--filters', action='store', default=None, help="The filters that should be used, example: key1:value1,key2:value2") args = parser.parse_args(sys.argv[3:]) columns = Misc.parse_service_columns(service="ami", columns=args.columns) if args.filters: filters = Misc.format_boto3_filter(filters=args.filters) else: filters = None result = a.information_ami(columns=columns, filters=filters) logger.output(data=result, csvvar=self.account_information['logger_arguments']['csv'], tablevar=self.account_information['logger_arguments']['table'])
def overflow(self): logger.info("Started stack provision command") a = awsrequests(session=self.account_information['session']) parser = argparse.ArgumentParser( description='stack provision', usage='''kerrigan.py stack overflow [<args>]] ''' + self.global_options, formatter_class=argparse.ArgumentDefaultsHelpFormatter, prog="kerrigan") parser.add_argument('--json', action='store', required=True, help="Stack json to provision") parser.add_argument('--env', action='store', required=True, help="Which env to deploy to") parser.add_argument('--dryrun', action='store_true', default=False, help="No changes should be done") args = parser.parse_args(sys.argv[3:]) a.deploy_stack_to_env(env=args.env, file=args.json, dryrun=args.dryrun)
def info_bucket(self): logger.info("Starting to gather information") a = awsrequests() parser = argparse.ArgumentParser( description='ec2 tool for devops', usage='''kerrigan.py s3 info_bucket [<args>]] ''' + self.global_options) parser.add_argument('--name', action='store', help="Name of bucket", required=True) parser.add_argument('--level', action='store', choices=[ 'acl', 'lifecycle', 'region', 'logging', 'policy', 'replication', 'tagging' ], help="What information to return") args = parser.parse_args(sys.argv[3:]) res = a.info_s3_bucket(name=args.name, choice=args.level) logger.output(data=res, csvvar=self.cli['csv'], tablevar=self.cli['table'])
def report_ec2_instances(self): ''' This function is a wrapper for parsing arguments and printing for ec2 instance attribute reports Tested :return: ''' logger.info("Started report generation command") a = awsrequests(session=self.account_information['session']) parser = argparse.ArgumentParser(description='report generation about ec2 instances', usage='''kerrigan.py ec2 report_ec2_instances [<args>]] ''' + self.global_options, formatter_class=argparse.ArgumentDefaultsHelpFormatter, prog="kerrigan") parser.add_argument('--columns', action='store', default=a.service_supported_columns(service="ec2").keys(), help="Which columns to display") parser.add_argument('--filters', action='store', default=None, help="The filters that should be used, example: key1:value1,key2:value2") args = parser.parse_args(sys.argv[3:]) columns = Misc.parse_service_columns(service="ec2", columns=args.columns) if args.filters: filters = Misc.format_boto3_filter(filters=args.filters) else: filters = None result = a.information_ec2_instances(columns=columns, filters=filters) logger.output(data=result, csvvar=self.account_information['logger_arguments']['csv'], tablevar=self.account_information['logger_arguments']['table'])
def create(self): logger.info("Started create command") a = awsrequests() parser = argparse.ArgumentParser(description='autoscaling tool for devops', usage='''kerrigan.py autoscaling create [<args>]] ''' + self.global_options) parser.add_argument('--stack', action='store', choices=a.get_image_stacks(), default=None, help="Stack to create", required=True) parser.add_argument('--min', action='store', type=int, default=1, help="Minimum number of instances to create default:1", required=True) parser.add_argument('--max', action='store', type=int, default=1, help="Maximum number of instances to create default:1", required=True) parser.add_argument('--env', action='store', default=None, choices=a.get_envs(), help="The environment the auto scaling group should provision to", required=True) parser.add_argument('--requester', action='store', default="", help="The person requesting the machine") parser.add_argument('--customer', action='store', default="", help="The customer assigned to the resource") parser.add_argument('--elb', action='store', default="", help="The elastic load balancers to use for the auto scaling group, separated by comma", required=True) parser.add_argument('--health_check', action='store', default="ELB", help="Service to use for health check; options: ELB or EC2", required=True) parser.add_argument('--health_check_grace_period', action='store', type=int, default=60, help="Restrict health check for x number of seconds after launch", required=True) parser.add_argument('--xively_service', action='store', default="", help="The docker image running on the machine") parser.add_argument('--availability', action='store', default="public", help="Public or private subnets from the given environment; options: public or private", required=True) args = parser.parse_args(sys.argv[3:]) logger.info("Starting Launch process " + time.asctime(time.localtime(time.time()))) instance = a.launch_auto_scaling_group(env=args.env, stack=args.stack, min_size=args.min, max_size=args.max, xively_service=args.xively_service, requester=args.requester, load_balancer_name=args.elb.split(','), health_check=args.health_check, health_check_grace_period=args.health_check_grace_period, availability=args.availability, customer=args.customer)
def run_workflow(self): from misc import Misc from core.awsrequests import awsrequests AWSreq = awsrequests(session=self.account['session']) region = self.account['cli_arguments']['region_name'] print "" print "Step %s: Creating ec2 keys" % (self.increment_step(),) if not Misc.confirm("Has the keys for all envs been created with syntax default-env syntax?", resp=True): exit(1) print "" print "Step %s: Creating bucket for cloudformation" % (self.increment_step(),) if Misc.confirm("Should we create the s3 bucket for cloudformation?", resp=True): bucket_name = raw_input("What should the bucket name be (ex: xively-devops-templates-dr ): ") AWSreq.create_s3_bucket(name=bucket_name, location=region) else: print "Assuming bucket is already created" bucket_name = raw_input("What is the bucket name?") print "" print "Step %s: Upload xively_cloudformation repo to the s3 bucket" % (self.increment_step(),) while not Misc.confirm("Is the upload finished?", resp=False): print "Finish upload before continue" print "" print "Step %s: Run cloudformation template for infrastructure?" % (self.increment_step(),) if Misc.confirm("Should we run the cloudformation template", resp=False): # FIXME test if works cloudformation_template_name = raw_input( "What is the name of template to run (ex. VPC_dr_account.template ): ") url = "https://s3.amazonaws.com/" + bucket_name + "/" + cloudformation_template_name awsrequests.create_cloudformation_stack(stackname="vpc-infrastructure", templateurl=url) else: print "Assuming the stack has already been run." print "" print "Step %s: Run cloudformation template for users base" % (self.increment_step(),) if Misc.confirm("Should we run the cloudformation template?", resp=False): # FIXME test if works cloudformation_template_name = raw_input( "What is the name of template to run (ex. prod_account_iam.template ): ") url = "https://s3.amazonaws.com/" + bucket_name + "/" + cloudformation_template_name awsrequests.create_cloudformation_stack(stackname="devops-users", templateurl=url) else: print "Assuming the stack has already been run." devops_groupname = raw_input("Name of the generated devops group? ") print "" print "Step %s: Start amon.py to deploy to environement" % (self.increment_step(),) while not Misc.confirm("Is the packer generation started", resp=False): print "Start before we continue" print "" print "Step %s: Creating devops users" % (self.increment_step(),) devops_yaml = Misc.get_yaml(yamlfile="devops_users.yaml") for user in devops_yaml['users']: print "Checking user %s" % (user,) create_password = True if not AWSreq.iam_user_exists(username=user): print "Creating user %s" % (user,) AWSreq.create_iam_user(username=user, dryrun=False, path="/") else: login_profile = AWSreq.get_login_profile(username=user) if login_profile is not None: create_password = False if create_password: user_password = Misc.generate_password(size=10) AWSreq.create_iam_login_profile(username=user, password=user_password) print "Username: %s generated password is: %s" % (user, user_password) user_groups = AWSreq.iam_user_groups(username=user) if devops_groupname not in user_groups[user]: print "Need to add to group" AWSreq.add_iam_user_to_group(username=user, groupname=devops_groupname)
def create(self): logger.info("Started create command") a = awsrequests() parser = argparse.ArgumentParser( description='autoscaling tool for devops', usage='''kerrigan.py autoscaling create [<args>]] ''' + self.global_options) parser.add_argument('--stack', action='store', choices=a.get_image_stacks(), default=None, help="Stack to create", required=True) parser.add_argument( '--min', action='store', type=int, default=1, help="Minimum number of instances to create default:1", required=True) parser.add_argument( '--max', action='store', type=int, default=1, help="Maximum number of instances to create default:1", required=True) parser.add_argument( '--env', action='store', default=None, choices=a.get_envs(), help="The environment the auto scaling group should provision to", required=True) parser.add_argument('--requester', action='store', default="", help="The person requesting the machine") parser.add_argument('--customer', action='store', default="", help="The customer assigned to the resource") parser.add_argument( '--elb', action='store', default="", help= "The elastic load balancers to use for the auto scaling group, separated by comma", required=True) parser.add_argument( '--health_check', action='store', default="ELB", help="Service to use for health check; options: ELB or EC2", required=True) parser.add_argument( '--health_check_grace_period', action='store', type=int, default=60, help="Restrict health check for x number of seconds after launch", required=True) parser.add_argument('--xively_service', action='store', default="", help="The docker image running on the machine") parser.add_argument( '--availability', action='store', default="public", help= "Public or private subnets from the given environment; options: public or private", required=True) args = parser.parse_args(sys.argv[3:]) logger.info("Starting Launch process " + time.asctime(time.localtime(time.time()))) instance = a.launch_auto_scaling_group( env=args.env, stack=args.stack, min_size=args.min, max_size=args.max, xively_service=args.xively_service, requester=args.requester, load_balancer_name=args.elb.split(','), health_check=args.health_check, health_check_grace_period=args.health_check_grace_period, availability=args.availability, customer=args.customer)