def apply_scope_to_multiple_nodes(self, *args, **kwargs): self._load_kwargs(kwargs) banner("PCC.Apply scope to multiple nodes") conn = BuiltIn().get_variable_value("${PCC_CONN}") print("Kwargs are: {}".format(kwargs)) try: response_code_list = [] for name in ast.literal_eval(self.node_names): print("Name in for loop: {}".format(name)) node_id = Nodes().get_node_id(conn, Name=name) print("Node id is : {}".format(node_id)) response = Nodes().update_node(conn, Id=node_id, Name=name, scopeId=self.scopeId) print("Response from update node is: {}".format(response)) response_code_list.append(response['StatusCode']) print("Response code list: {}".format(response_code_list)) result = len(response_code_list) > 0 and all( elem == 200 for elem in response_code_list) if result: return "OK" else: return "Error: while applying scope to multiple nodes: Check response code list: {}".format( response_code_list) except Exception as e: return "Error while applying scope to multiple nodes: {}".format(e)
def check_roles_assignment_on_node(self, **kwargs): banner("PCC.Check roles assignment on node") self._load_kwargs(kwargs) print("Kwargs are: {}".format(kwargs)) conn = BuiltIn().get_variable_value("${PCC_CONN}") node_id = Nodes().get_node_id(conn, Name=self.node_name) print("Node id is: {}".format(node_id)) get_node_response = Nodes().get_node(conn, Id=str(node_id))['Result']['Data'] roles_from_node = get_node_response['roles'] print("roles from node is: {}".format(roles_from_node)) if self.roles_id: flag = 0 user_role_id = ast.literal_eval(self.roles_id) print("role id by user is : {}".format(user_role_id)) print("role id from PCC is : {}".format(roles_from_node)) if (set(user_role_id).issubset(set(roles_from_node))): flag = 1 if (flag): return "OK" else: return "Error: Role Ids {} are not assigned on node {}".format( self.roles_id, self.scope_name) else: return "Error in else: Role Ids {} are not assigned on node {}".format( self.roles_id, self.scope_name)
def check_scope_hierarchy_on_node(self, **kwargs): banner("PCC.Check scope hierarchy on node") self._load_kwargs(kwargs) logger.console("Kwargs are: {}".format(kwargs)) conn = BuiltIn().get_variable_value("${PCC_CONN}") node_id = Nodes().get_node_id(conn, Name=self.node_name) scope_tree = Nodes().get_node( conn, Id=str(node_id))['Result']['Data']['scope'] scope_ids = [] while type(scope_tree['parent']) == dict: scope_ids.append(scope_tree['id']) scope_tree = scope_tree['parent'] if scope_tree['parentID'] == None: scope_ids.append(scope_tree['id']) break scope_ids.sort() scope_from_user = ast.literal_eval(self.scope_from_user) scope_from_user.sort() print("Scope from PCC: {}".format(scope_ids)) print("Scope from User: {}".format(scope_from_user)) if scope_ids == scope_from_user: return "OK" else: return "Error: Scope hierarchy doesn't match, scope from user: {} and scope from PCC: {}".format( scope_from_user, scope_ids)
def get_OS_version_by_node_name(self, *args, **kwargs): """ Get OS version by Node Name [Args] (dict) conn: Connection dictionary obtained after logging in (str) Name: Name of the node [Returns] (string) OS_Version: Version of OS of the matchining node, or None: if no match found, or (dict) Error response: If Exception occured """ banner("PCC.Get OS version by node name") self._load_kwargs(kwargs) conn = BuiltIn().get_variable_value("${PCC_CONN}") try: Id = Nodes().get_node_id(conn, Name=self.Name) logger.console("Node Id is: {}".format(Id)) OS_version = Nodes().get_node( conn, Id=str(Id))['Result']['Data']['systemData']['osVersion'] return OS_version except Exception as e: return {"Error": str(e)}
def get_roles_assigned_to_node(self, **kwargs): banner("PCC.Get roles assigned to node") self._load_kwargs(kwargs) print("Kwargs are: {}".format(kwargs)) conn = BuiltIn().get_variable_value("${PCC_CONN}") node_id = Nodes().get_node_id(conn, Name=self.node_name) get_node_response = Nodes().get_node(conn, Id=str(node_id))['Result']['Data'] roles_from_node = get_node_response['roles'] if roles_from_node: return roles_from_node else: return "No roles assigned to node"
def check_policy_assignment_on_node(self, **kwargs): banner("PCC.Check policy assignment on node") self._load_kwargs(kwargs) print("Kwargs are: {}".format(kwargs)) conn = BuiltIn().get_variable_value("${PCC_CONN}") node_id = Nodes().get_node_id(conn, Name=self.node_name) get_node_response = Nodes().get_node(conn, Id=str(node_id))['Result']['Data'] policy_ids_from_node = get_node_response['scope']['policies'] self.policyIDs.sort() policy_ids_from_node.sort() if self.policyIDs == policy_ids_from_node: return "OK" return "Error: Policy Ids {} are not assigned on node {}".format( self.policyIDs, self.node_name)
def get_Host_IP(self, *args, **kwargs): """ Get Host IP used by Container Registry [Args] (dict) conn: Connection dictionary obtained after logging in (str) Name: Name of the Container Registry who's Host IP is required (name=<Name>) [Returns] (str) Host IP: Host IP used by the Container Registry, or None: if no match found, or (dict) Error response: If Exception occured """ banner("PCC.Get Host IP") self._load_kwargs(kwargs) try: server_id = self.get_CR_server_id(**kwargs) node_ids_response = Nodes().get_nodes(**kwargs) print("node_ids_response: {}".format(node_ids_response)) for node in get_response_data(node_ids_response): if server_id == node['Id']: return node['Host'] except Exception as e: logger.console("Error in get_Host_IP: {}".format(e))
def check_scope_assignment_on_node(self, **kwargs): banner("PCC.Check scope assignment on node") self._load_kwargs(kwargs) logger.console("Kwargs are: {}".format(kwargs)) conn = BuiltIn().get_variable_value("${PCC_CONN}") node_id = Nodes().get_node_id(conn, Name=self.node_name) get_node_response = Nodes().get_node(conn, Id=str(node_id))['Result']['Data'] if (str(self.scopeId) == str( get_node_response['scope']['id'])) and (str( self.parentID) == str( get_node_response['scope']['parentID'])) and (str( self.scope_name) == str( get_node_response['scope']['name'])): return "OK" return "Error: Scope Id {} is not assigned on node {}".format( self.scopeId, self.node_name)
def install_s3cmd(self, *args, **kwargs): self._load_kwargs(kwargs) conn = BuiltIn().get_variable_value("${PCC_CONN}") print("Kwargs are: {}".format(kwargs)) try: host_ips = [] status = [] get_nodes_response = Nodes().get_nodes(**kwargs) host_ips = [ str(node['Host']) for node in get_response_data(get_nodes_response) ] print("host_ips_list : {}".format(host_ips)) for ip in host_ips: OS_type = Cli().get_OS_version(host_ip=ip, linux_user=self.username, linux_password=self.password) if re.search("Debian", str(OS_type)) or re.search( "Ubuntu", str(OS_type)): cmd = "sudo apt-get --assume-yes install s3cmd" if re.search("Red Hat Enterprise", str(OS_type)) or re.search( "CentOS", str(OS_type)): cmd = "sudo yum -y install s3cmd" s3cmd_install = cli_run(cmd=cmd, host_ip=ip, linux_user=self.username, linux_password=self.password) print("Cmd: {} executed successfully. Output is : {}".format( cmd, str(s3cmd_install))) trace("Cmd: {} executed successfully. Output is : {}".format( cmd, str(s3cmd_install))) return "OK" except Exception as e: print("s3cmd installation unsuccessful: {}".format(e)) return ("s3cmd installation unsuccessful: {}".format(e))
def install_nettools(self, *args, **kwargs): self._load_kwargs(kwargs) conn = BuiltIn().get_variable_value("${PCC_CONN}") print("Kwargs are: {}".format(kwargs)) try: host_ips = [] status = [] get_nodes_response = Nodes().get_nodes(**kwargs) host_ips = [ str(node['Host']) for node in get_response_data(get_nodes_response) ] print("host_ips_list : {}".format(host_ips)) for ip in host_ips: cmd = "sudo cat /etc/os-release|grep PRETTY_NAME" cmd_output = cli_run(cmd=cmd, host_ip=ip, linux_user=self.username, linux_password=self.password) serialised_status = self._serialize_response( time.time(), cmd_output) serialised_cmd_output = str( serialised_status['Result']['stdout']).replace('\n', '').strip() if "Ubuntu" in serialised_cmd_output: cmd = "sudo apt-get install net-tools" cmd_output = cli_run(cmd=cmd, host_ip=ip, linux_user=self.username, linux_password=self.password) serialised_status = self._serialize_response( time.time(), cmd_output) serialised_cmd_output = str( serialised_status['Result']['stdout']).replace( '\n', '').strip() if "0 newly installed" or "1 newly installed" in serialised_cmd_output: status.append("OK") else: logger.console( "Error in installing net-tools on Ubuntu: {}". format(serialised_cmd_output)) status.append( "Error in installing net-tools on Ubuntu") elif "CentOS" in serialised_cmd_output: cmd = "sudo yum -y install net-tools" cmd_output = cli_run(cmd=cmd, host_ip=ip, linux_user=self.username, linux_password=self.password) serialised_status = self._serialize_response( time.time(), cmd_output) serialised_cmd_output = str( serialised_status['Result']['stdout']).replace( '\n', '').strip() if "Complete!" or "Nothing to do" in serialised_cmd_output: status.append("OK") else: logger.console( "Error in installing net-tools on CentOS: {}". format(serialised_cmd_output)) status.append( "Error in installing net-tools on CentOS") elif "Debian" in serialised_cmd_output: cmd = "sudo apt-get install net-tools" cmd_output = cli_run(cmd=cmd, host_ip=ip, linux_user=self.username, linux_password=self.password) serialised_status = self._serialize_response( time.time(), cmd_output) serialised_cmd_output = str( serialised_status['Result']['stdout']).replace( '\n', '').strip() if "0 upgraded" or "0 newly installed" or "1 newly installed" in serialised_cmd_output: status.append("OK") else: logger.console( "Error in installing net-tools on Debian: {}". format(serialised_cmd_output)) status.append( "Error in installing net-tools on Debian") else: return "Error: OS version not supported in code" print("Status: {}".format(status)) result = len(status) > 0 and all(elem == "OK" for elem in status) if result: return "OK" else: return "Error: Installation of net-tools failed" except Exception as e: return "Error in installing net-tools: {}".format(e)