def get_agentids(os_code=None, customer_name=None, tag_id=None): agentids=[] agent_ids_for_tag_id= get_agent_ids_from_tag(tag_id=tag_id) agent_ids_for_os_customer=get_all_agent_ids(agent_os=os_code,customer_name=customer_name) agentids=list(set(agent_ids_for_tag_id + agent_ids_for_os_customer)) return agentids
def get_agentids(os_code=None, customer_name=None, tag_id=None): agentids=[] agent_ids_for_tag_id= get_agent_ids_from_tag(tag_id=tag_id) agent_ids_for_os_customer=get_all_agent_ids(agent_os=os_code,customer_name=customer_name) agentids=list(set(agent_ids_for_tag_id + agent_ids_for_os_customer)) return agentids
def install_apps(self, oper_type, appids, cpu_throttle=CPUThrottleValues.NORMAL, net_throttle=0, restart=RebootValues.NONE, agentids=None, tag_id=None): """This method creates the operation and stores it into the agent queue. Args: oper_type (str): The operation type, etc.. install_os_apps, uninstall appids (list): List of the application ids, that you want to install. Kwargs: cpu_throttle (str): Throttle how much cpu to use while installing the applications. default = normal net_throttle (int): Throttle how much bandwidth is being used, while the agent is downloading the applications. default = 0 (unlimited) restart (str): Choose if you want to restart the system after the application is installed. Examples (none, needed, forced) default = 'none' (do not reboot) agentids (str): List of agent ids. default = None tag_id (str): 36 character UUID of the agent. default = None """ oper_plugin = vFensePlugins.RV_PLUGIN results = { ApiResultKeys.DATA: [], ApiResultKeys.USERNAME: self.username, ApiResultKeys.URI: self.uri, ApiResultKeys.HTTP_METHOD: self.method } performed_on = vFenseObjects.AGENT if tag_id: performed_on = vFenseObjects.TAG if not agentids: agentids = get_agent_ids_from_tag(tag_id) else: agentids += get_agent_ids_from_tag(tag_id) operation = (PatchingOperation( self.username, self.customer_name, )) operation_id = (operation.create_operation(oper_type, oper_plugin, agentids, tag_id, cpu_throttle, net_throttle, restart, performed_on=performed_on)) if operation_id: msg = 'operation created' status_code = GenericCodes.ObjectCreated vfense_status_code = AgentOperationCodes.Created results[ApiResultKeys.GENERATED_IDS] = [operation_id] results[ApiResultKeys.GENERIC_STATUS_CODE] = status_code results[ApiResultKeys.VFENSE_STATUS_CODE] = vfense_status_code results[ApiResultKeys.MESSAGE] = msg for agent_id in agentids: valid_appids = (return_valid_appids_for_agent( appids, agent_id, collection=self.CurrentAppsPerAgentCollection)) pkg_data = [] for app_id in valid_appids: update_app_status_by_agentid_and_appid( agent_id, app_id, CommonAppKeys.PENDING, self.CurrentAppsPerAgentCollection) pkg_data.append(self._get_apps_data(app_id, agent_id)) operation_data = { AgentOperationKey.Operation: oper_type, AgentOperationKey.OperationId: operation_id, AgentOperationKey.Plugin: oper_plugin, AgentOperationKey.Restart: restart, CommonFileKeys.PKG_FILEDATA: pkg_data, OperationPerAgentKey.AgentId: agent_id, AgentOperationKey.CpuThrottle: cpu_throttle, AgentOperationKey.NetThrottle: net_throttle, } self._store_in_agent_queue(operation_data) operation.add_agent_to_install_operation( agent_id, operation_id, pkg_data) else: msg = 'operation failed to create' status_code = GenericFailureCodes.FailedToCreateObject vfense_status_code = ( AgentOperationFailureCodes.FailedToCreateOperation) results[ApiResultKeys.GENERATED_IDS] = [operation_id], results[ApiResultKeys.GENERIC_STATUS_CODE] = status_code results[ApiResultKeys.VFENSE_STATUS_CODE] = vfense_status_code results[ApiResultKeys.MESSAGE] = msg return results
def generic_operation( self, action, plugin, agentids=None, tag_id=None ): """This will do all the necessary work, to add the operation into the agent_queue. This method is to be used for operations that do not need any extra manipulation, in order to be added into the queue. Args: action (str): This is the action that will be performed, on the agent. Examples.... reboot, shutdown, install_os_apps, etc.. plugin (str): The plugin that this operation is for. Examples... core, rv, ra, vuln agentids (list): List of agent ids, this operation will be performed on. tag_id (str): The tag id that this operation will be performed on. Basic Usage: >>> from vFense.operations.store_agent_operation import StoreAgentOperation >>> username = '******' >>> customer_name = 'default' >>> uri = '/api/v1/agent/33ba8521-b2e5-47dc-9bdc-0f1e3384049d' >>> method = 'POST' >>> store = StoreAgentOperation(username, customer_name, uri, method) >>> store.generic_operation( 'reboot', 'core', agentids=['33ba8521-b2e5-47dc-9bdc-0f1e3384049d'] ) Returns: Dictionary { "rv_status_code": 6000, "http_method": "POST", "http_status": 200, "unchanged_ids": [], "generated_ids": [ "d5fb023c-82a0-4552-adc1-b3f83de7ae8a" ], "message": "operation created", "data": [ { "operation_id": "d5fb023c-82a0-4552-adc1-b3f83de7ae8a", "operation": "reboot", "agent_id": "456404f1-b185-4f4f-8fb7-bfb21b3a5d53", "plugin": "core" } ], "uri": "/api/v1/456404f1-b185-4f4f-8fb7-bfb21b3a5d53/" } """ data = [] performed_on = vFenseObjects.AGENT if tag_id: performed_on = vFenseObjects.TAG if not agentids: agentids = get_agent_ids_from_tag(tag_id) elif agentids: agentids += get_agent_ids_from_tag(tag_id) results = { ApiResultKeys.DATA: [], ApiResultKeys.USERNAME: self.username, ApiResultKeys.URI: self.uri, ApiResultKeys.HTTP_METHOD: self.method } operation = ( AgentOperation( self.username, self.customer_name, ) ) operation_id = ( operation.create_operation( action, plugin, agentids, tag_id, performed_on=performed_on ) ) if operation_id: msg = 'operation created' status_code = GenericCodes.ObjectCreated vfense_status_code = AgentOperationCodes.Created results[ApiResultKeys.GENERATED_IDS] = [operation_id] results[ApiResultKeys.GENERIC_STATUS_CODE] = status_code results[ApiResultKeys.VFENSE_STATUS_CODE] = vfense_status_code results[ApiResultKeys.MESSAGE] = msg for agent_id in agentids: operation_data = { AgentOperationKey.Operation: action, AgentOperationKey.OperationId: operation_id, AgentOperationKey.Plugin: plugin, OperationPerAgentKey.AgentId: agent_id, } agent_data = deepcopy(operation_data) data.append(agent_data) self._store_in_agent_queue(operation_data) operation.add_agent_to_operation(agent_id, operation_id) results[ApiResultKeys.DATA] = data else: msg = 'operation failed to create' status_code = GenericFailureCodes.FailedToCreateObject vfense_status_code = ( AgentOperationFailureCodes.FailedToCreateOperation ) results[ApiResultKeys.GENERATED_IDS] = [operation_id] results[ApiResultKeys.GENERIC_STATUS_CODE] = status_code results[ApiResultKeys.VFENSE_STATUS_CODE] = vfense_status_code results[ApiResultKeys.MESSAGE] = msg results[ApiResultKeys.DATA] = data return results
def install_apps( self, oper_type, appids, cpu_throttle=CPUThrottleValues.NORMAL, net_throttle=0, restart=RebootValues.NONE, agentids=None, tag_id=None ): """This method creates the operation and stores it into the agent queue. Args: oper_type (str): The operation type, etc.. install_os_apps, uninstall appids (list): List of the application ids, that you want to install. Kwargs: cpu_throttle (str): Throttle how much cpu to use while installing the applications. default = normal net_throttle (int): Throttle how much bandwidth is being used, while the agent is downloading the applications. default = 0 (unlimited) restart (str): Choose if you want to restart the system after the application is installed. Examples (none, needed, forced) default = 'none' (do not reboot) agentids (str): List of agent ids. default = None tag_id (str): 36 character UUID of the agent. default = None """ oper_plugin = vFensePlugins.RV_PLUGIN results = { ApiResultKeys.DATA: [], ApiResultKeys.USERNAME: self.username, ApiResultKeys.URI: self.uri, ApiResultKeys.HTTP_METHOD: self.method } performed_on = vFenseObjects.AGENT if tag_id: performed_on = vFenseObjects.TAG if not agentids: agentids = get_agent_ids_from_tag(tag_id) else: agentids += get_agent_ids_from_tag(tag_id) operation = ( PatchingOperation( self.username, self.customer_name, ) ) operation_id = ( operation.create_operation( oper_type, oper_plugin, agentids, tag_id, cpu_throttle, net_throttle, restart, performed_on=performed_on ) ) if operation_id: msg = 'operation created' status_code = GenericCodes.ObjectCreated vfense_status_code = AgentOperationCodes.Created results[ApiResultKeys.GENERATED_IDS] = [operation_id] results[ApiResultKeys.GENERIC_STATUS_CODE] = status_code results[ApiResultKeys.VFENSE_STATUS_CODE] = vfense_status_code results[ApiResultKeys.MESSAGE] = msg for agent_id in agentids: valid_appids = ( return_valid_appids_for_agent( appids, agent_id, collection=self.CurrentAppsPerAgentCollection ) ) pkg_data = [] for app_id in valid_appids: update_app_status_by_agentid_and_appid( agent_id, app_id, CommonAppKeys.PENDING, self.CurrentAppsPerAgentCollection ) pkg_data.append( self._get_apps_data(app_id, agent_id) ) operation_data = { AgentOperationKey.Operation: oper_type, AgentOperationKey.OperationId: operation_id, AgentOperationKey.Plugin: oper_plugin, AgentOperationKey.Restart: restart, CommonFileKeys.PKG_FILEDATA: pkg_data, OperationPerAgentKey.AgentId: agent_id, AgentOperationKey.CpuThrottle: cpu_throttle, AgentOperationKey.NetThrottle: net_throttle, } self._store_in_agent_queue(operation_data) operation.add_agent_to_install_operation( agent_id, operation_id, pkg_data ) else: msg = 'operation failed to create' status_code = GenericFailureCodes.FailedToCreateObject vfense_status_code = ( AgentOperationFailureCodes.FailedToCreateOperation ) results[ApiResultKeys.GENERATED_IDS] = [operation_id], results[ApiResultKeys.GENERIC_STATUS_CODE] = status_code results[ApiResultKeys.VFENSE_STATUS_CODE] = vfense_status_code results[ApiResultKeys.MESSAGE] = msg return results
def generic_operation(self, action, plugin, agentids=None, tag_id=None): """This will do all the necessary work, to add the operation into the agent_queue. This method is to be used for operations that do not need any extra manipulation, in order to be added into the queue. Args: action (str): This is the action that will be performed, on the agent. Examples.... reboot, shutdown, install_os_apps, etc.. plugin (str): The plugin that this operation is for. Examples... core, rv, ra, vuln agentids (list): List of agent ids, this operation will be performed on. tag_id (str): The tag id that this operation will be performed on. Basic Usage: >>> from vFense.operations.store_agent_operation import StoreAgentOperation >>> username = '******' >>> customer_name = 'default' >>> uri = '/api/v1/agent/33ba8521-b2e5-47dc-9bdc-0f1e3384049d' >>> method = 'POST' >>> store = StoreAgentOperation(username, customer_name, uri, method) >>> store.generic_operation( 'reboot', 'core', agentids=['33ba8521-b2e5-47dc-9bdc-0f1e3384049d'] ) Returns: Dictionary { "rv_status_code": 6000, "http_method": "POST", "http_status": 200, "unchanged_ids": [], "generated_ids": [ "d5fb023c-82a0-4552-adc1-b3f83de7ae8a" ], "message": "operation created", "data": [ { "operation_id": "d5fb023c-82a0-4552-adc1-b3f83de7ae8a", "operation": "reboot", "agent_id": "456404f1-b185-4f4f-8fb7-bfb21b3a5d53", "plugin": "core" } ], "uri": "/api/v1/456404f1-b185-4f4f-8fb7-bfb21b3a5d53/" } """ data = [] performed_on = vFenseObjects.AGENT if tag_id: performed_on = vFenseObjects.TAG if not agentids: agentids = get_agent_ids_from_tag(tag_id) elif agentids: agentids += get_agent_ids_from_tag(tag_id) results = { ApiResultKeys.DATA: [], ApiResultKeys.USERNAME: self.username, ApiResultKeys.URI: self.uri, ApiResultKeys.HTTP_METHOD: self.method } operation = (AgentOperation( self.username, self.customer_name, )) operation_id = (operation.create_operation(action, plugin, agentids, tag_id, performed_on=performed_on)) if operation_id: msg = 'operation created' status_code = GenericCodes.ObjectCreated vfense_status_code = AgentOperationCodes.Created results[ApiResultKeys.GENERATED_IDS] = [operation_id] results[ApiResultKeys.GENERIC_STATUS_CODE] = status_code results[ApiResultKeys.VFENSE_STATUS_CODE] = vfense_status_code results[ApiResultKeys.MESSAGE] = msg for agent_id in agentids: operation_data = { AgentOperationKey.Operation: action, AgentOperationKey.OperationId: operation_id, AgentOperationKey.Plugin: plugin, OperationPerAgentKey.AgentId: agent_id, } agent_data = deepcopy(operation_data) data.append(agent_data) self._store_in_agent_queue(operation_data) operation.add_agent_to_operation(agent_id, operation_id) results[ApiResultKeys.DATA] = data else: msg = 'operation failed to create' status_code = GenericFailureCodes.FailedToCreateObject vfense_status_code = ( AgentOperationFailureCodes.FailedToCreateOperation) results[ApiResultKeys.GENERATED_IDS] = [operation_id] results[ApiResultKeys.GENERIC_STATUS_CODE] = status_code results[ApiResultKeys.VFENSE_STATUS_CODE] = vfense_status_code results[ApiResultKeys.MESSAGE] = msg results[ApiResultKeys.DATA] = data return results