def change_icon_theme(self): """ Changes icon theme with help of gsd-xsettings """ icon_dirs = [] icons_path = pathlib.Path('~/.local/share/icons').expanduser() for icon in icons_path.glob('*'): if icon: icon_dirs += [pathlib.Path(icon).name] menu_params = self.menu_params(len(icon_dirs), 'icon theme') selection = '' if not icon_dirs: return if len(icon_dirs) == 1: selection = icon_dirs[0] else: try: selection = subprocess.run( self.menu.args(menu_params), stdout=subprocess.PIPE, input=bytes('\n'.join(icon_dirs), 'UTF-8'), check=True ).stdout except subprocess.CalledProcessError as proc_err: Misc.print_run_exception_info(proc_err) if selection: self.apply_settings(selection, '-i')
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 info(config: dict, tag: str, attr: str, fill: str) -> str: """ Create rule in i3 commands format Args: config (dict): extension config. tag (str): target tag. attr (str): tag attrubutes. fill (str): attribute to fill. """ conv_dict_attr = { 'class': 'class', 'instance': 'instance', 'name': 'window_name', 'role': 'window_role' } cmd = '' if fill in attr: if not attr.endswith('_r'): win_attr = conv_dict_attr[attr] else: win_attr = conv_dict_attr[attr[:-2]] start = f'{win_attr}="' + Misc.ch(config[tag][attr], '^') attrlist = [] attrlist = config[tag][attr] if config[tag].get(attr + '_r', ''): attrlist += config[tag][attr + '_r'] if not attr.endswith('_r'): cmd = start + Misc.parse_attr(attrlist, end='') if cmd: cmd += '"' return cmd
def change_gtk_theme(self): """ Changes gtk theme with help of gsd-xsettings """ theme_dirs = [] gtk_theme_path = pathlib.Path('~/.local/share/themes').expanduser() for theme in gtk_theme_path.glob('*/*/gtk.css'): if theme: theme_dirs += [pathlib.PurePath(theme).parent.parent.name] menu_params = self.menu_params(len(theme_dirs), 'gtk theme') selection = '' if not theme_dirs: return if len(theme_dirs) == 1: selection = theme_dirs[0] else: try: selection = subprocess.run( self.menu.args(menu_params), stdout=subprocess.PIPE, input=bytes('\n'.join(theme_dirs), 'UTF-8'), check=True ).stdout except subprocess.CalledProcessError as proc_err: Misc.print_run_exception_info(proc_err) if selection: self.apply_settings(selection, '-a')
class Radiance(dict, _V): """ The Radiance class Radiance object is Read-Only and contains minimum information """ def __init__(self): dict.__init__(self) for key in radiance_list: self[key] = None self[key + '_ATTRIBUTE'] = {'COMMENT': 'none', 'UNITS': 'n/a'} self.misc = None def loadh5(self, h5): self.loadh5liste(h5, radiance_list) def read(self, filename): f = h5py.File(filename, 'r') # get the Dataset h5 = f['/RADIANCE/'] self.loadh5(h5) # Load self.loadh5(h5) h5 = f['/MISC/'] self.misc = Misc() self.misc.loadh5(h5) f.close()
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 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 test_parse_service_columns_with_services_cloudformation(self): columns = "" keys = Misc.get_supported_columns(service="cloudformation").keys() self.assertListEqual( keys, Misc.parse_service_columns(service="cloudformation", columns=columns))
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 __init__(self, filename): Disassembler.__init__(self, filename) PAPA_ROP.__init__(self, filename) Format_String.__init__(self, filename) Process.__init__(self, filename) Misc.__init__(self, filename) context.log_level = 'INFO' context.delete_corefiles = True
def invoke_checklocaladminaccess(target_computername, domain, user, password=str(), lmhash=str(), nthash=str()): misc = Misc(target_computername, domain, user, password, lmhash, nthash) return misc.invoke_checklocaladminaccess()
def main(): killer = Killer() args = _parse_argv() logger = get_logger("DNSWatch", log_level=args.loglevel, log_file=args.logfile) logger.info("Starting dnswatch v.{}.".format(__version__)) misc = Misc(logger) try: exit_code = 2 if not get_lock("dnswatch", timeout=5): misc.die("Lock exists") c = Config() action = None while True: config = c.read(args.config) dw = DNSWatch(config) if action == 'reload': dw.reload_config() else: dw.initial_config() try: action = dw.watch(pause=config["watch"]["pause"]) except: action = dw.watch() if action == "kill": # Do DNS cleanup and exit loop dw.cleanup() break elif action == "softkill": # Exit loop without DNS cleanup break elif action == "reload": # Do nothing pass else: misc.die("Unknown action requested: {}".format(action)) logger.info("Finished successfully.") exit_code = 0 except SystemExit: exit_code = sys.exc_info()[1] except: logger.error("Exiting with errors.") if args.trace: print(sys.exc_info()) trace = sys.exc_info()[2] traceback.print_tb(trace) exit_code = 1 finally: sys.exit(exit_code)
def dump_cfg(self) -> None: i3_cfg, test_cfg = 'config', '.config' generated_cfg = '\n'.join(self.generate()) with open(Misc.i3path() + test_cfg, 'w', encoding='utf8') as fp: fp.write(generated_cfg) if checker.check_i3_config(verbose=False, cfg=test_cfg): with open(Misc.i3path() + i3_cfg, 'w', encoding='utf8') as fp: fp.write(generated_cfg) os.remove(Misc.i3path() + test_cfg)
def apply_settings(self, selection, *cmd_opts): """ Apply selected gnome settings """ ret = "" if selection is not None: ret = selection.decode('UTF-8').strip() if ret is not None and ret != '': try: subprocess.call([self.gsettings_script, *cmd_opts, ret]) except subprocess.CalledProcessError as proc_err: Misc.print_run_exception_info(proc_err)
def __init__(self): self.profile = Profile() self.option = Option() self.kmatrix = Profile() self.chanprof = Chanprof() self.emissivity = Emissivity() self.reflectance = Reflectance() self.misc = Misc() self.scaled = False self.scalecoef = 1.0 self.scaledfirst = True
def trim_msa(self, options): """ Trim an untrimmed archaea or bacterial MSA file.""" if options.reference_mask in ['bac', 'arc']: mask_type = "reference" mask_id = options.reference_mask else: mask_type = "file" mask_id = options.mask_file misc = Misc() misc.trim_msa(options.untrimmed_msa, mask_type, mask_id, options.output) self.logger.info('Done.')
def auto_save_geom(self, save: bool = True, with_notification: bool = False) -> None: """ Set geometry autosave option with optional notification. Args: save(bool): predicate that shows that want to enable/disable autosave mode. with_notification(bool): to create notify-osd-based notification or not. """ self.geom_auto_save = save if with_notification: Misc.notify_msg(f"geometry autosave={save}")
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 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 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 read(self, filename): f = h5py.File(filename, 'r') # get the Dataset h5 = f['/RADIANCE/'] self.loadh5(h5) # Load self.loadh5(h5) h5 = f['/MISC/'] self.misc = Misc() self.misc.loadh5(h5) f.close()
def change_jmx(self, modifiers): test_plan_elems = { 'iterations': "${__P(iterations," + Misc.none_safe(modifiers.get('iterations')) + ")}", 'threads': "${__P(threads," + Misc.none_safe(modifiers.get('threads')) + ")}" } modify_data = {} for mod, data in modifiers.items(): if mod in test_plan_elems and data: modify_data[mod] = test_plan_elems[mod] return self.edit_jmx(modify_data)
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 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 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 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_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 __init__(self): print "Starting account creation and buildup" self.step = 0 from misc import Misc from core.base import base base = base() if Misc.confirm(prompt="Are you sure you want to create an account infrastructure?", resp=False): self.account = base.get_account_information() if 'profile_name' in self.account['cli_arguments']: print "Aws account has been provided" else: logger.error("Aws account not provided") exit(1) if 'region_name' in self.account['cli_arguments']: print "Aws region has been provided" else: logger.error("Aws region not provided") exit(1) if 'session' in self.account and self.account['session'] is not (None or ""): logger.info("Session object created succesfully") else: logger.error("Aws Session not created successfuly") exit(1) self.run_workflow() else: print "You are not prepared - Illidian"
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 create_policy_version(self, arn, statement, dryrun, setasdefault=True): if dryrun: logger.warning("Dryrun requested for creating policy version %s" % (arn,)) return None document = {'Version': '2012-10-17', 'Statement': statement} document = Misc.convert_to_json(document) resp = self.iam.create_policy_version(PolicyArn=arn, PolicyDocument=document, SetAsDefault=setasdefault) return resp['PolicyVersion']
def service_supported_columns(self, service): ''' This function returns the current supported columns for printing and parsing :return: An array with currently supported column attributes for printing :rtype: array ''' from misc import Misc return Misc.get_supported_columns(service=service)
def info_all(self, filter=None): e = Ec2() instances = e.get_all_instances(filters=filter) output = [] for i in instances: model = Misc.parse_instance(i) output.append(model) return output
def compare_elb_tags(elb_tags=None, tags=None): for tag in tags: elb_tag_value = Misc.get_value_from_array_hash(dictlist=elb_tags, key=tag) if elb_tag_value == tags[tag]: continue else: return False return True
def create_policy(self, name, statement, description, dryrun, path="/"): if dryrun: logger.warning("Dryrun requested, not creating %s resource: %s" % (name, str(statement))) return None document = {'Version': '2012-10-17', 'Statement': statement} document = Misc.convert_to_json(document) print document resp = self.iam.create_policy(PolicyName=name, Path=path, PolicyDocument=document, Description=description) return resp['Policy']
def __init__(self, config, sync=True): self.logger = logging.getLogger("DNSWatch.Route53") self.misc = Misc(self.logger) self.config = config self.sync = sync self.unchecked_requests = list() self.client = boto3.client( "route53", aws_access_key_id=config["update_key"]["name"], aws_secret_access_key=config["update_key"]["key"])
def test_format_boto3_filter_multiple_values(self): filters = "tag-value:test,tag-test:test2" should_be = [{ 'Name': 'tag-value', 'Values': ['test'] }, { 'Name': 'tag-test', 'Values': ['test2'] }] self.assertEqual(should_be, Misc.format_boto3_filter(filters=filters))
def server_certficate_update(self, domain=None, intermediate=False): i = Iam() e = Elb() old_name = "old." + domain i.update_server_cert(cert_name=old_name, new_name=domain) logger.debug("Rename certificate") i.update_server_cert(cert_name=domain, new_name=old_name) logger.debug("Gathering certificates informations") # We should query old certs, to test if they have been moved, and it is not an incorrect call being made old_cert = Misc.get_cert_body(name=old_name) old_key = Misc.get_cert_body(name=old_name, type="key") new_cert = Misc.get_cert_body(name=domain) new_key = Misc.get_cert_body(name=domain, type="key") logger.debug("Uploading new certificate for domain") if intermediate: inter_body = Misc.get_cert_body(domain="intermediate") i.upload_server_cert(cert_name=domain, pub_key=new_cert, priv_key=new_key, cert_chain=inter_body) else: i.upload_server_cert(cert_name=domain, pub_key=new_cert, priv_key=new_key) elbs = e.get_elbs_from_ssl_cert() old_object = i.get_server_cert(name=old_name) new_object = i.get_server_cert(name=domain) if old_object['ServerCertificateMetadata']['Arn'] in elbs: move_elbs = elbs[old_object['ServerCertificateMetadata']['Arn']] else: move_elbs = [] for elb in move_elbs: logger.info("Migrating ELB %s" % elb.get('LoadBalancerName')) ports = ssl_ports_in_elb(elb=elb) for port in ports: logger.debug('Migrating port %s' % port, ) e.set_elb_ssl_cert( elb_name=elb.get('LoadBalancerName'), port=port, cert=new_object['ServerCertificateMetadata']['Arn']) logger.info("Deleting old cert") i.delete_server_certs(cert_name=old_name) logger.echo("Updated certificate to new one.")
def create_elb(self, name=None, listeners=None, scheme=None, tags=None, env=None, sg_name=None): subnets = self.get_subnets_for_elb(scheme=scheme, env=env) yaml_tags = Misc.get_yaml_tags_for_sub(sub="elb") lb_name = self.generate_elb_name(stack=name, facing=scheme, env=env) for y in yaml_tags: logger.debug("Checking if tag exists %s" % y, ) if y == "Environment": tags.append({'Key': y, 'Value': env}) continue if y == "Name": tags.append({'Key': y, 'Value': lb_name}) continue t = Misc.get_value_from_array_hash(dictlist=tags, key=y) if t is None: tags.append({'Key': y, 'Value': ""}) sgs = self.get_sgs_for_elb(env=env, name=sg_name) self.elb.create_load_balancer(LoadBalancerName=lb_name, Scheme=scheme, Tags=tags, SecurityGroups=sgs, Subnets=subnets, Listeners=listeners) return lb_name
def get_all_subnets(self, filters=None, subnetids=None): """ This function returns all subnets, or filters them as requested :param filters: A dict list with the boto3 filters :param subnetids: A list of subnetids that should only be returned :return: A list of subnets that were requested """ if subnetids: response = self.vpc_client.describe_subnets(SubnetIds=subnetids) elif filters: response = self.vpc_client.describe_subnets(Filters=filters) else: response = self.vpc_client.describe_subnets() result = [] for s in response['Subnets']: allowed = Misc.get_value_from_array_hash(dictlist=s.get('Tags'), key="Allowed") if Misc.str2bool(allowed): result.append(s) logger.debug("Allowed az subnets are: %s" % (result,)) return 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 __init__(self, config): self.logger = logging.getLogger("DNSWatch.BindProvider") self.misc = Misc(self.logger) self.dhcl = DHClient() self.dnso = DNSOps(config["dnsupdate"]) self.zone = config["dnsupdate"]["zone"] self.fqdn = config["host"]["fqdn"] self.private_ip = config["host"]["private_ip"] self.public_ip = config["host"]["public_ip"] self.alias_dict = config["dnsupdate"]["alias"] self.aliases = None
def test_str2bool_false(self): self.assertFalse(Misc.str2bool("false")) self.assertFalse(Misc.str2bool("False")) self.assertFalse(Misc.str2bool("f")) self.assertFalse(Misc.str2bool("0")) self.assertFalse(Misc.str2bool("no")) self.assertFalse(Misc.str2bool("ILIKECARS"))
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 get_account_id(self): """ This functions extracts the AWS account id from the user object :return: the AWs account id :rtype: int """ user_data = self.get_user() if 'Arn' in user_data: arn = user_data['Arn'] ret = Misc.parse_arn(arn=arn)['account-id'] else: logger.error("Can not determine the Account ID, User has no ARN") raise ValueError return ret
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 info_all(self): elbs = self.get_all_elbs() result = [] yaml = Misc.get_aws_yaml(yaml_file="elb") V = Vpc() for lb in elbs: tmp = lb['LoadBalancerName'].split('-') if len(tmp) >= 3: elb_env = tmp.pop(0) short_env = tmp.pop(0) elb_stack = "-".join(tmp) elb_facing = lb['Scheme'] if elb_stack in yaml and elb_facing in yaml[elb_stack]: yaml_info = yaml[elb_stack][elb_facing] v = V.get_vpc_from_env(env=elb_env) domain = Misc.get_value_from_array_hash(dictlist=v.get('Tags'), key="Domain") if elb_facing == "internet-facing": elb_dns_name = yaml_info['dns'] + "." + Misc.change_domain_to_local(domain=domain) elif elb_facing == "internal": elb_dns_name = yaml_info['dns'] + "." + Misc.change_domain_to_local(domain=domain) else: elb_dns_name = None info = {} if elb_dns_name is not None: info['DNS cname'] = elb_dns_name else: info['DNS cname'] = "This elb is not in automatisation framework. Will be decomissioned" info['Xively_service'] = Misc.get_value_from_array_hash(dictlist=lb.get('Tags'), key="Xively_service") info['Puppet_role'] = Misc.get_value_from_array_hash(dictlist=lb.get('Tags'), key="Puppet_role") info['Env'] = Misc.get_value_from_array_hash(dictlist=lb.get('Tags'), key="Environment") info['Real endpoint'] = lb['DNSName'] info['Vpcid'] = lb['VPCId'] info['Name'] = lb['LoadBalancerName'] info['CreateTime'] = lb['CreatedTime'].strftime("%Y-%m-%d %H:%M") info['Facing'] = elb_facing info['Availability Zones'] = lb['AvailabilityZones'] info['Securitygroups'] = lb['SecurityGroups'] instance = [] for i in lb['Instances']: instance.append(i['InstanceId']) info['InstanceIds'] = instance listen = [] for listener in lb['ListenerDescriptions']: listener = listener['Listener'] listen.append( "%s-%s-%s" % (listener['LoadBalancerPort'], listener['InstancePort'], listener['Protocol'])) info['From-To-Protocol'] = listen result.append(info) return result
def get_subnets_for_elb(self, scheme=None, env=None): vpc = Vpc() v = vpc.get_vpc_from_env(env=env) azs = Misc.get_azs_from_yaml(region=self.region) if scheme == "internal": avail = "private" else: avail = "public" res = [] sub = vpc.get_all_subnets( filters=[{'Name': 'tag:Availability', 'Values': [avail]}, {'Name': 'availabilityZone', 'Values': azs}, {'Name': 'vpc-id', 'Values': [v.get('VpcId')]}]) for s in sub: logger.debug("Adding sub: %s" % sub, ) res.append(s.get('SubnetId')) logger.debug("Subnets for elb are: %s" % res, ) return res
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 get_ami_stacks(self, account_id): ''' This function returns all active ami Puppet_roles :param account_id: The account id that is being used. IAM wrapper returns this number :type account_id: int :return: Array of strings of valid puppet_roles :rtype: array ''' images = self.get_images(account_id=account_id, filters=[{'Name': "tag-key", 'Values': ['Puppet_role']}]) stacks = {} for i in images: v = Misc.get_value_from_array_hash(dictlist=i['Tags'], key='Puppet_role') if v is not "" and v is not None: stacks[v] = 1 stacks = stacks.keys() logger.debug("Active stacks: " + str(stacks)) return stacks
def get_db_instances(self, filters=None): resp = self.boto3.describe_db_instances() r = [] for rds in resp['DBInstances']: arn = Misc.generate_arn(service="rds", resourcetype="db", name=rds.get('DBInstanceIdentifier')) rds['ARN'] = arn rds['Tags'] = self.get_tags_for_rds(name=arn) r.append(rds) ret = [] if filters: # FIXME currently filters are not implemented # resp = self.boto3.describe_db_instances(Filters=filters) for rds in r: if rds_instance_filters(rds=rds, filters=filters): ret.append(rds) else: ret = r return ret
def translate_security_group(security_group, env_cidr): ipranges = Misc.get_yaml(landscape_path="ipranges.yaml") ret = [] for rule in security_group['Properties']['SecurityGroupIngress']: if 'CidrIp' in rule: logger.debug("We have a CidrIp in the security group") if rule['CidrIp'] == "vpc": ret.append(change_sg_cidr(rule=rule, iprange=env_cidr)) elif rule['CidrIp'] == "self": print "self" elif rule['CidrIp'] in ipranges: for iprange in ipranges[rule['CidrIp']]: ret.append(change_sg_cidr(rule=rule, iprange=iprange)) else: ret.append(rule) security_group['Properties']['SecurityGroupIngress'] = ret logger.debug(msg="Translated security group: %s" % (ret,)) return security_group
def rds_instance_filters(rds=None, filters=None): for f in filters: logger.debug("Filter investigation %s" % f, ) if f['Name'] == "VpcId": if f['Values'][0] == rds.get('DBSubnetGroup').get('VpcId'): logger.info("This is the VPC we need for rds %s" % rds.get('DBSubnetGroup').get('VpcId'), ) else: logger.debug("RDS is in wrong VPC") return False if f['Name'] == "tag:Name": if 'Tags' in rds: logger.debug("RDS instance has tags") tag_name = Misc.get_value_from_array_hash(dictlist=rds['Tags'], key='Name') if f['Values'][0] == tag_name: logger.info("Tag name is same") continue return False return True
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 get_active_envs(self, env=None): """ This function returns an array with the active environemnts in the account :param env: a comma seperated list of environments that should be validated :type env: basestring :return: An array with active environments :rtype: list """ vpcs = self.get_all_vpcs() envs = [] for vpc in vpcs: cur = Misc.get_value_from_array_hash(dictlist=vpc['Tags'], key='Environment') if cur != "": envs.append(cur) else: logger.warning("Vpc has no Environment tag: %s" % (vpc.id)) if env: envs = [env] logger.debug("Current envs: " + str(envs)) return envs
def generate_ec2_unique_name(self, env, puppet_role, num): """ This function generates x number of unique ec2 instance names :param env: Which environment should the names be generated to :param puppet_role: What is the puppet_role being used :param num: How many name should be generated :return: An array of of names generated """ ret = [] for i in range(num): name = None logger.debug("Arguments are: env: %s, puppet_role: %s" % (env, puppet_role)) while name is None: name = env + '-' + puppet_role + '-' + Misc.random3digit() name = self.ec2_instance_name_exist(name=name) logger.info("Generated instance name is %s" % (name,)) if name and name not in ret: ret.append(name) else: name = None return ret
def get_all_image_instances(self, imageid): if imageid: instances = self.ami_client.describe_instances(Filters=[{'Name': 'image-id', 'Values': [imageid]}]) else: instances = self.ami_client.describe_instances() super(Ami, self).query_information(query=instances) temp = {} for reservation in instances['Reservations']: for instance in reservation['Instances']: if 'Tags' in instance: value = Misc.get_value_from_array_hash(dictlist=instance.get('Tags'), key='Name') else: value = instance.get('InstanceId') if instance['ImageId'] not in temp: temp[instance['ImageId']] = [value] else: temp[instance['ImageId']].append(value) ret = [] for imageid in temp: img = {'ImageId': imageid} img['InstanceCount'] = len(temp[imageid]) img['Instances'] = temp[imageid] ret.append(img) return ret