def close_tasks(self, instance, project, region, request_id=None, task_id=None, user=None, requester=None): """Close Turbinia Tasks based on Request ID. Args: instance (string): The Turbinia instance name (by default the same as the INSTANCE_ID in the config). project (string): The name of the project. region (string): The name of the zone to execute in. request_id (string): The Id of the request we want tasks for. task_id (string): The Id of the request we want task for. user (string): The user of the request we want tasks for. requester (string): The user making the request to close tasks. Returns: String of closed Task IDs. """ cloud_function = gcp_function.GoogleCloudFunction(project) func_args = { 'instance': instance, 'kind': 'TurbiniaTask', 'request_id': request_id, 'task_id': task_id, 'user': user, 'requester': requester } response = cloud_function.ExecuteFunction('closetasks', region, func_args) return 'Closed Task IDs: %s' % response.get('result')
def function(self) -> function_module.GoogleCloudFunction: """Get a GoogleCloudFunction object for the project. Returns: GoogleCloudFunction: Object that represents Google Cloud Function. """ if self._function: return self._function self._function = function_module.GoogleCloudFunction(self.project_id) return self._function
def get_task_data(self, instance, project, region, days=0, task_id=None, request_id=None, user=None, function_name='gettasks'): """Gets task data from Google Cloud Functions. Args: instance (string): The Turbinia instance name (by default the same as the INSTANCE_ID in the config). project (string): The name of the project. region (string): The name of the region to execute in. days (int): The number of days we want history for. task_id (string): The Id of the task. request_id (string): The Id of the request we want tasks for. user (string): The user of the request we want tasks for. function_name (string): The GCF function we want to call Returns: List of Task dict objects. """ cloud_function = gcp_function.GoogleCloudFunction(project) func_args = {'instance': instance, 'kind': 'TurbiniaTask'} if days: start_time = datetime.now() - timedelta(days=days) # Format this like '1990-01-01T00:00:00z' so we can cast it directly to a # javascript Date() object in the cloud function. start_string = start_time.strftime(DATETIME_FORMAT) func_args.update({'start_time': start_string}) elif task_id: func_args.update({'task_id': task_id}) elif request_id: func_args.update({'request_id': request_id}) if user: func_args.update({'user': user}) response = None retry_count = 0 credential_error_count = 0 while response is None and retry_count < MAX_RETRIES: try: response = cloud_function.ExecuteFunction( function_name, region, func_args) except auth.exceptions.RefreshError as exception: if credential_error_count == 0: log.info( 'GCP Credentials need to be refreshed, please refresh in another ' 'terminal and this process will resume. Error: {0!s}'. format(exception)) else: log.debug( 'GCP Credentials need to be refreshed, please refresh in another ' 'terminal and this process will resume. Attempt {0:d}. Error: ' '{1!s}'.format(credential_error_count + 1, exception)) # Note, we are intentially not incrementing the retry_count here because # we will retry indefinitely while we wait for the user to reauth. credential_error_count += 1 except httplib2.ServerNotFoundError as exception: log.info( 'Error connecting to server, will retry [{0:d} of {1:d} retries]: ' '{2!s}'.format(retry_count, MAX_RETRIES, exception)) retry_count += 1 if response is None: time.sleep(RETRY_SLEEP) if 'result' not in response: log.error('No results found') if response.get('error', '{}') != '{}': msg = 'Error executing Cloud Function: [{0!s}].'.format( response.get('error')) log.error(msg) log.debug('GCF response: {0!s}'.format(response)) raise TurbiniaException( 'Cloud Function {0:s} returned no results.'.format( function_name)) try: results = json.loads(response['result']) except (TypeError, ValueError) as e: raise TurbiniaException( 'Could not deserialize result [{0!s}] from GCF: [{1!s}]'. format(response.get('result'), e)) # Convert run_time/last_update back into datetime objects task_data = results[0] for task in task_data: if task.get('run_time'): task['run_time'] = timedelta(seconds=task['run_time']) if task.get('last_update'): task['last_update'] = datetime.strptime( task['last_update'], DATETIME_FORMAT) return task_data
def get_task_data(self, instance, project, region, days=0, task_id=None, request_id=None, user=None, function_name='gettasks'): """Gets task data from Google Cloud Functions. Args: instance (string): The Turbinia instance name (by default the same as the INSTANCE_ID in the config). project (string): The name of the project. region (string): The name of the region to execute in. days (int): The number of days we want history for. task_id (string): The Id of the task. request_id (string): The Id of the request we want tasks for. user (string): The user of the request we want tasks for. function_name (string): The GCF function we want to call Returns: List of Task dict objects. """ cloud_function = gcp_function.GoogleCloudFunction(project) func_args = {'instance': instance, 'kind': 'TurbiniaTask'} if days: start_time = datetime.now() - timedelta(days=days) # Format this like '1990-01-01T00:00:00z' so we can cast it directly to a # javascript Date() object in the cloud function. start_string = start_time.strftime(DATETIME_FORMAT) func_args.update({'start_time': start_string}) elif task_id: func_args.update({'task_id': task_id}) elif request_id: func_args.update({'request_id': request_id}) if user: func_args.update({'user': user}) response = cloud_function.ExecuteFunction(function_name, region, func_args) if 'result' not in response: log.error('No results found') if response.get('error', '{}') != '{}': msg = 'Error executing Cloud Function: [{0!s}].'.format( response.get('error')) log.error(msg) log.debug('GCF response: {0!s}'.format(response)) raise TurbiniaException( 'Cloud Function {0:s} returned no results.'.format( function_name)) try: results = json.loads(response['result']) except (TypeError, ValueError) as e: raise TurbiniaException( 'Could not deserialize result [{0!s}] from GCF: [{1!s}]'. format(response.get('result'), e)) # Convert run_time/last_update back into datetime objects task_data = results[0] for task in task_data: if task.get('run_time'): task['run_time'] = timedelta(seconds=task['run_time']) if task.get('last_update'): task['last_update'] = datetime.strptime( task['last_update'], DATETIME_FORMAT) return task_data