def execute_function(self): if SysUtils.is_var_in_env('SCRIPT'): script_path = SysUtils.join_paths( SysUtils.get_env_var("TMP_INPUT_DIR"), self._SCRIPT_FILE_NAME) script_content = StrUtils.base64_to_str( SysUtils.get_env_var('SCRIPT')) FileUtils.create_file_with_content(script_path, script_content) get_logger().info("Script file created in '%s'", script_path) FileUtils.set_file_execution_rights(script_path) get_logger().info("Executing user defined script: '%s'", script_path) try: pyinstaller_library_path = SysUtils.get_env_var( 'LD_LIBRARY_PATH') orig_library_path = SysUtils.get_env_var( 'LD_LIBRARY_PATH_ORIG') if orig_library_path: SysUtils.set_env_var('LD_LIBRARY_PATH', orig_library_path) self.output = subprocess.check_output( ['/bin/sh', script_path], stderr=subprocess.STDOUT).decode("latin-1") SysUtils.set_env_var('LD_LIBRARY_PATH', pyinstaller_library_path) get_logger().debug("CONTAINER OUTPUT:\n %s", self.output) except subprocess.CalledProcessError as cpe: # Exit with user script return code if an # error occurs (Kubernetes handles the error) get_logger().error(cpe.output.decode('latin-1')) sys.exit(cpe.returncode) else: get_logger().error('No user script found!')
def save_event(self, input_dir_path): file_path = SysUtils.join_paths(input_dir_path, self._FILE_NAME) if self.has_json_body(): FileUtils.create_file_with_content(file_path, self.body) else: FileUtils.create_file_with_content(file_path, base64.b64decode(self.body), mode='wb') return file_path
def test_create_file_with_json_content(self): mopen = mock.mock_open() with mock.patch('builtins.open', mopen, create=True): FileUtils.create_file_with_content('/tmp/file', { "k1": "v1", "k2": "v2" }) mopen.assert_called_once_with('/tmp/file', 'w') mopen().write.assert_called_once_with('{"k1": "v1", "k2": "v2"}')
def _get_script_path(self): script_path = None if SysUtils.is_var_in_env('SCRIPT'): script_path = SysUtils.join_paths( SysUtils.get_env_var("TMP_INPUT_DIR"), self._SCRIPT_FILE_NAME) script_content = StrUtils.base64_to_str( SysUtils.get_env_var('SCRIPT')) FileUtils.create_file_with_content(script_path, script_content) get_logger().info("Script file created in '%s'", script_path) elif FileUtils.is_file(self._OSCAR_SCRIPT_PATH): script_path = self._OSCAR_SCRIPT_PATH return script_path
def _parse_exec_script_and_commands(self): # Check for script in function event if 'script' in self.raw_event: self.script_path = f"{self.input_folder}/script.sh" script_content = StrUtils.base64_to_str(self.raw_event['script']) FileUtils.create_file_with_content(self.script_path, script_content) # Container invoked with arguments elif 'cmd_args' in self.raw_event: # Add args self.cmd_args = json.loads(self.raw_event['cmd_args']) # Script to be executed every time (if defined) elif ConfigUtils.read_cfg_var('init_script') is not '': # Add init script self.init_script_path = f"{self.input_folder}/init_script.sh" FileUtils.cp_file(ConfigUtils.read_cfg_var('init_script'), self.init_script_path)
def _parse_exec_script_and_commands(self): # Check for script in function event if 'script' in self.raw_event: self.script_path = f"{self.input_folder}/script.sh" script_content = StrUtils.base64_to_str(self.raw_event['script']) FileUtils.create_file_with_content(self.script_path, script_content) # Container invoked with arguments elif 'cmd_args' in self.raw_event: # Add args self.cmd_args = json.loads(self.raw_event['cmd_args']) # Script to be executed every time (if defined) elif SysUtils.is_var_in_env('INIT_SCRIPT_PATH'): # Add init script self.init_script_path = f"{self.input_folder}/init_script.sh" FileUtils.cp_file(SysUtils.get_env_var("INIT_SCRIPT_PATH"), self.init_script_path)
def download_file(self, parsed_event, input_dir_path): """Downloads the file from the space of Onedata and returns the path were the download is placed. """ file_download_path = "" url = f'https://{self.oneprovider_host}{self._CDMI_PATH}{parsed_event.object_key}' get_logger().info('Downloading item from host \'%s\' with key \'%s\'', self.oneprovider_host, parsed_event.object_key) response = requests.get(url, headers=self.headers) if response.status_code == 200: file_download_path = SysUtils.join_paths(input_dir_path, parsed_event.file_name) FileUtils.create_file_with_content(file_download_path, response.content, mode='wb') get_logger().info( 'Successful download of file \'%s\' with key \'%s\' in path \'%s\'', parsed_event.file_name, parsed_event.object_key, file_download_path) else: raise OnedataDownloadError(file_name=parsed_event.object_key, status_code=response.status_code) return file_download_path
def download_file(self, parsed_event, input_dir_path): """ Downloads the file from the space of Onedata and returns the path were the download is placed. """ file_download_path = "" url = f"https://{self.oneprovider_host}/{self._CDMI_PATH}{parsed_event.object_key}" get_logger().info("Downloading item from host '%s' with key '%s'", self.oneprovider_host, parsed_event.object_key) response = requests.get(url, headers=self.headers) if response.status_code == 200: file_download_path = SysUtils.join_paths(input_dir_path, parsed_event.file_name) FileUtils.create_file_with_content(file_download_path, response.content, mode='wb') get_logger().info( "Successful download of file '%s' with key '%s' in path '%s'", parsed_event.file_name, parsed_event.object_key, file_download_path) else: get_logger().error( "File '%s' download from Onedata host '%s' failed!", parsed_event.file_name, self.oneprovider_host) return file_download_path
def save_event(self, input_dir_path): """Stores the unknown event and returns the file path where the file is stored.""" file_path = SysUtils.join_paths(input_dir_path, self._file_name) try: json.loads(self.event) except ValueError: FileUtils.create_file_with_content(file_path, base64.b64decode(self.event), mode='wb') except TypeError: FileUtils.create_file_with_content(file_path, self.event) else: FileUtils.create_file_with_content(file_path, self.event) return file_path
def test_create_file_with_content(self): mopen = mock.mock_open() with mock.patch('builtins.open', mopen, create=True): FileUtils.create_file_with_content('/tmp/file', 'fifayfofum') mopen.assert_called_once_with('/tmp/file', 'w') mopen().write.assert_called_once_with('fifayfofum')
def save_event(self, input_dir_path): """Stores the unknown event and returns the file path where the file is stored.""" file_path = SysUtils.join_paths(input_dir_path, self._FILE_NAME) FileUtils.create_file_with_content(file_path, self.event) return file_path