Ejemplo n.º 1
0
 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!')
Ejemplo n.º 2
0
 def execute_function(self):
     script_path = self._get_script_path()
     if 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)
             else:
                 SysUtils.delete_env_var('LD_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!')
Ejemplo n.º 3
0
def parse_event(event):
    """Parses the received event and
    returns the appropriate event class."""
    # Make sure the event is always stored
    parsed_event = None
    if not isinstance(event, dict):
        try:
            event = json.loads(event)
        except ValueError:
            return UnknownEvent(event)
    # Applies the event identification flow
    if _is_api_gateway_event(event):
        get_logger().info("API Gateway event found.")
        parsed_event = ApiGatewayEvent(event)
        # Update event info with API request event body
        # to be further processed (if needed)
        if parsed_event.has_json_body():
            event = parsed_event.body
            if not isinstance(parsed_event.body, dict):
                event = json.loads(parsed_event.body)
    if _is_storage_event(event):
        get_logger().info("Storage event found.")
        parsed_event = _parse_storage_event(event)
        # Store 'object_key' in environment variable
        SysUtils.set_env_var("STORAGE_OBJECT_KEY", parsed_event.object_key)
    return parsed_event if parsed_event else UnknownEvent(event)
Ejemplo n.º 4
0
 def execute_function(self):
     script_path = self._get_script_path()
     if 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)
             else:
                 SysUtils.delete_env_var('LD_LIBRARY_PATH')
             proc = subprocess.Popen(['/bin/bash', script_path],
                                     stdout=subprocess.PIPE,
                                     stderr=subprocess.STDOUT,
                                     encoding='utf-8',
                                     errors='ignore')
             SysUtils.set_env_var('LD_LIBRARY_PATH',
                                  pyinstaller_library_path)
             get_logger().debug("CONTAINER OUTPUT:\n %s", self.output)
             for line in proc.stdout:
                 get_logger().debug(line.strip())
                 self.output = self.output + line
         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(encoding='utf-8', errors='ignore'))
             sys.exit(cpe.returncode)
     else:
         get_logger().error('No user script found!')
Ejemplo n.º 5
0
    def _create_tmp_dirs(self):
        """Creates the temporal directories where the
        input/output data is going to be stored.

        The folders are deleted automatically
        when the execution finishes.
        """
        self.input_tmp_dir = FileUtils.create_tmp_dir()
        self.output_tmp_dir = FileUtils.create_tmp_dir()
        SysUtils.set_env_var("TMP_INPUT_DIR", self.input_tmp_dir.name)
        SysUtils.set_env_var("TMP_OUTPUT_DIR", self.output_tmp_dir.name)
Ejemplo n.º 6
0
    def _parse_input(self):
        """Download input data from storage provider
        or save data from POST request.

        A function can have information from several storage providers
        but one event always represents only one file (so far), so only
        one provider is going to be used for each event received.
        """
        input_file_path = self.stg_config.download_input(self.parsed_event,
                                                         self.input_tmp_dir.name)
        if input_file_path and FileUtils.is_file(input_file_path):
            SysUtils.set_env_var('INPUT_FILE_PATH', input_file_path)
            get_logger().info('INPUT_FILE_PATH variable set to \'%s\'', input_file_path)
Ejemplo n.º 7
0
 def run(self):
     """Generic method to launch the supervisor execution."""
     try:
         get_logger().info('Executing function')
         SysUtils.set_env_var('INPUT_DATA', str(self.inputs))
         self.supervisor.execute_function()
         # if is_batch_execution() and SysUtils.is_lambda_environment():
         #     # Only delegate to batch
         #     self.supervisor.execute_function()
         # else:
         #     self._parse_input()
         #     self.supervisor.execute_function()
         #     self._parse_output()
         get_logger().info('Creating response')
         return self.supervisor.create_response()
     except FaasSupervisorError as fse:
         get_logger().exception(fse)
         get_logger().error('Creating error response')
         return self.supervisor.create_error_response()
Ejemplo n.º 8
0
    def _parse_input(self):
        """Download input data from storage provider
        or save data from POST request.

        A function can have information from several storage providers
        but one event always represents only one file (so far), so only
        one provider is going to be used for each event received.
        """
        stg_prov = self._get_input_provider()
        get_logger().info("Found '%s' input provider", stg_prov.get_type())
        if stg_prov:
            get_logger().info("Downloading input file using '%s' event",
                              self.parsed_event.get_type())
            input_file_path = storage.download_input(
                stg_prov, self.parsed_event,
                SysUtils.get_env_var("TMP_INPUT_DIR"))
            if input_file_path and FileUtils.is_file(input_file_path):
                SysUtils.set_env_var("INPUT_FILE_PATH", input_file_path)
                get_logger().info("INPUT_FILE_PATH variable set to '%s'",
                                  input_file_path)
Ejemplo n.º 9
0
 def _set_lambda_env_vars(self):
     SysUtils.set_env_var('AWS_LAMBDA_REQUEST_ID', self.get_request_id())
Ejemplo n.º 10
0
 def _save_request_parameters(self):
     # Add passed HTTP parameters to container variables
     for key, value in self.event["queryStringParameters"].items():
         SysUtils.set_env_var(f"CONT_VAR_{key}", value)
Ejemplo n.º 11
0
 def test_set_env_var(self):
     with mock.patch.dict('os.environ', {}, clear=True):
         SysUtils.set_env_var('TEST_VAR', 'TEST_VAL')
         self.assertTrue('TEST_VAR' in os.environ)
         self.assertEqual(os.environ['TEST_VAR'], 'TEST_VAL')