def get_interfaces(interfaces): """ @type interfaces: list """ ret = list() for interface in interfaces: ret.append("{0}\n".format(interface.get('NetworkInterfaceId'))) ret.append(" Attachment:\n") for akey, avalue in iteritems(interface.get('Attachment')): ret.append(" {0}:{1}\n".format(str(akey), str(avalue))) ret.append(" Private IP Addresses:\n") if interface.get('PrivateIpAddresses'): for private_ip_address in interface.get('PrivateIpAddresses'): for pkey, pvalue in iteritems(private_ip_address): if pkey == "Association": ret.append(" Association:\n") for qqkey, qqvalue in iteritems(pvalue): ret.append(" {0}:{1}\n".format(qqkey, qqvalue)) else: ret.append(" {0}:{1}\n".format( str(pkey), str(pvalue))) ret.append(" Security Groups:\n") if interface.get('Groups'): for group in interface.get('Groups'): for gkey, gvalue in iteritems(group): ret.append(" {0}:{1}\n".format(str(gkey), str(gvalue))) for key, value in iteritems(interface): if str(key) not in ("Attachment", "NetworkInterfaceId", "PrivateIpAddresses", "Groups", "Association", "PrivateIpAddress"): ret.append(" {0}:{1}\n".format(str(key), str(value))) return ("".join(ret)).rstrip()
def get_interfaces(interfaces): """ @type interfaces: list """ ret = list() for interface in interfaces: ret.append("{0}\n".format(interface.get('NetworkInterfaceId'))) ret.append(" Attachment:\n") for akey, avalue in iteritems(interface.get('Attachment')): ret.append(" {0}:{1}\n".format(str(akey), str(avalue))) ret.append(" Private IP Addresses:\n") if interface.get('PrivateIpAddresses'): for private_ip_address in interface.get('PrivateIpAddresses'): for pkey, pvalue in iteritems(private_ip_address): if pkey == "Association": ret.append(" Association:\n") for qqkey, qqvalue in iteritems(pvalue): ret.append(" {0}:{1}\n".format(qqkey, qqvalue)) else: ret.append(" {0}:{1}\n".format(str(pkey), str(pvalue))) ret.append(" Security Groups:\n") if interface.get('Groups'): for group in interface.get('Groups'): for gkey, gvalue in iteritems(group): ret.append(" {0}:{1}\n".format(str(gkey), str(gvalue))) for key, value in iteritems(interface): if str(key) not in ("Attachment", "NetworkInterfaceId", "PrivateIpAddresses", "Groups", "Association", "PrivateIpAddress"): ret.append(" {0}:{1}\n".format(str(key), str(value))) return ("".join(ret)).rstrip()
def output_dict(dict_=None): """ @type dict_: dict """ output = list() for k, v in iteritems(dict_): output.append('{0}: {1}\n'.format(k, str(v))) return ''.join(output).rstrip()
def get_placement_details(placement): """ @type placement: dict """ ret = [] for key, value in iteritems(placement): ret.append("{0}:{1}".format(key, value)) if ret: return "\n".join(ret)
def execute_plan(plan=None): """Create, Modify or Delete, depending on plan item.""" execution_result = list() for task in plan: action = task['action'] if action == 'delete': command = generate_delete_user_command( username=task.get('username'), manage_home=task['manage_home']) command_output = execute_command(command) execution_result.append( dict(task=task, command_output=command_output)) remove_sudoers_entry(username=task.get('username')) elif action == 'add': command = generate_add_user_command( proposed_user=task.get('proposed_user'), manage_home=task['manage_home']) command_output = execute_command(command) if task['proposed_user'].public_keys and task[ 'manage_home'] and task['manage_keys']: write_authorized_keys(task['proposed_user']) if task['proposed_user'].sudoers_entry: write_sudoers_entry( username=task['proposed_user'].name, sudoers_entry=task['proposed_user'].sudoers_entry) execution_result.append( dict(task=task, command_output=command_output)) elif action == 'update': result = task['user_comparison'].get('result') # Don't modify user if only keys have changed action_count = 0 for k, _ in iteritems(result): if '_action' in k: action_count += 1 command_output = None if task['manage_home'] and task[ 'manage_keys'] and action_count == 1 and 'public_keys_action' in result: write_authorized_keys(task['proposed_user']) elif action_count == 1 and 'sudoers_entry_action' in result: write_sudoers_entry(username=task['proposed_user'].name, sudoers_entry=task['user_comparison'] ['result']['replacement_sudoers_entry']) else: command = generate_modify_user_command(task=task) command_output = execute_command(command) if task['manage_home'] and task['manage_keys'] and result.get( 'public_keys_action'): write_authorized_keys(task['proposed_user']) if result.get('sudoers_entry_action'): write_sudoers_entry( username=task['proposed_user'].name, sudoers_entry=task['user_comparison']['result'] ['replacement_sudoers_entry']) execution_result.append( dict(task=task, command_output=command_output))
def execute_plan(plan=None): """Create, Modify or Delete, depending on plan item.""" execution_result = list() for task in plan: action = task['action'] if action == 'delete': command = generate_delete_user_command(username=task.get('username'), manage_home=task['manage_home']) command_output = execute_command(command) execution_result.append(dict(task=task, command_output=command_output)) remove_sudoers_entry(username=task.get('username')) elif action == 'add': command = generate_add_user_command(proposed_user=task.get('proposed_user'), manage_home=task['manage_home']) command_output = execute_command(command) if task['proposed_user'].public_keys and task['manage_home'] and task['manage_keys']: write_authorized_keys(task['proposed_user']) if task['proposed_user'].sudoers_entry: write_sudoers_entry(username=task['proposed_user'].name, sudoers_entry=task['proposed_user'].sudoers_entry) execution_result.append(dict(task=task, command_output=command_output)) elif action == 'update': result = task['user_comparison'].get('result') # Don't modify user if only keys have changed action_count = 0 for k, _ in iteritems(result): if '_action' in k: action_count += 1 command_output = None if task['manage_home'] and task['manage_keys'] and action_count == 1 and 'public_keys_action' in result: write_authorized_keys(task['proposed_user']) elif action_count == 1 and 'sudoers_entry_action' in result: write_sudoers_entry(username=task['proposed_user'].name, sudoers_entry=task['user_comparison']['result']['replacement_sudoers_entry']) else: command = generate_modify_user_command(task=task) command_output = execute_command(command) if task['manage_home'] and task['manage_keys'] and result.get('public_keys_action'): write_authorized_keys(task['proposed_user']) if result.get('sudoers_entry_action'): write_sudoers_entry(username=task['proposed_user'].name, sudoers_entry=task['user_comparison']['result']['replacement_sudoers_entry']) execution_result.append(dict(task=task, command_output=command_output))
def get_healthcheck(hc=None): if hc: out = str() for key, value in iteritems(hc): out += "{0}: {1}\n".format(key, value) return out.rstrip()