Example #1
0
 def save_tmp_udocker_env(self):
     # Avoid override global variables
     if SysUtils.is_variable_in_environment("UDOCKER_DIR"):
         self.udocker_dir_orig = SysUtils.get_environment_variable(
             "UDOCKER_DIR")
     # Set temporal global vars
     SysUtils.set_environment_variable("UDOCKER_DIR", self.udocker_dir)
Example #2
0
 def download_udocker_image(self):
     self.save_tmp_udocker_env()
     SysUtils.execute_command_with_msg(
         self.udocker_exec + ["pull", self.aws.lambdaf.image],
         cli_msg="Downloading container image")
     self._create_udocker_container()
     self._set_udocker_local_registry()
     self.restore_udocker_env()
Example #3
0
 def _update_config_file(self):
     logger.info(("SCAR configuration file deprecated.\n"
                  "Updating your SCAR configuration file."))
     FileUtils.copy_file(self.config_file_path, self.backup_file_path)
     logger.info(f"Old configuration file saved in '{self.backup_file_path}'.")
     self._create_new_config_file()
     logger.info((f"New configuration file saved in '{self.config_file_path}'.\n"
                  "Please fill your new configuration file with your account information."))
     SysUtils.finish_scar_execution()
Example #4
0
    def _validate_container_size(self, max_payload_size):
        if FileUtils.get_tree_size(self.udocker_dir) < (max_payload_size / 2):
            ucmd = self.udocker_exec + [
                "create", "--name=lambda_cont", self.aws.lambdaf.image
            ]
            SysUtils.execute_command_with_msg(
                ucmd, cli_msg="Creating container structure")

        elif FileUtils.get_tree_size(self.udocker_dir) > max_payload_size:
            FileUtils.delete_folder(
                FileUtils.join_paths(self.udocker_dir, "containers"))

        else:
            self.aws.lambdaf.environment['Variables']['UDOCKER_LAYERS'] = \
                                         '/var/task/udocker/containers/'
Example #5
0
 def prepare_udocker_image(self):
     self._save_tmp_udocker_env()
     cmd_out = SysUtils.execute_command_with_msg(self._udocker_exec + ["load", "-i",
                                                                       self.resources_info.get('lambda').get('container').get('image_file')],
                                                 cli_msg="Loading image file")
     # Get the image name from the command output
     self.resources_info['lambda']['container']['image'] = cmd_out.split('\n')[1]
     self._set_udocker_local_registry()
     self._restore_udocker_env()
Example #6
0
class ConfigFileParser():
    """Class to manage the SCAR configuration file creation, update and load."""

    _CONFIG_FOLDER_PATH = ".scar"
    _CONFIG_FILE_PATH = "scar.cfg"
    _CONFIG_FILE_NAME_BCK = "scar.cfg_old"
    config_file_folder = FileUtils.join_paths(SysUtils.get_user_home_path(), _CONFIG_FOLDER_PATH)
    config_file_path = FileUtils.join_paths(config_file_folder, _CONFIG_FILE_PATH)
    backup_file_path = FileUtils.join_paths(config_file_folder, _CONFIG_FILE_NAME_BCK)

    @exception(logger)
    def __init__(self):
        # Check if the config file exists
        if FileUtils.is_file(self.config_file_path):
            with open(self.config_file_path) as cfg_file:
                self.cfg_data = json.load(cfg_file)
            if not self._is_config_file_updated():
                self._update_config_file()
        else:
            self._create_scar_config_folder_and_file()

    def _is_config_file_updated(self):
        if 'config_version' not in self.cfg_data['scar']:
            return False
        return StrUtils.compare_versions(self.cfg_data.get('scar', {}).get("config_version", ""),
                                         _DEFAULT_CFG['scar']["config_version"]) >= 0

    def get_properties(self):
        """Returns the configuration data of the configuration file."""
        return self.cfg_data

    def get_udocker_zip_url(self):
        """Returns the url where the udocker zip is stored."""
        return self.cfg_data['scar']['udocker_info']['zip_url']

    def _create_scar_config_folder_and_file(self):
        FileUtils.create_folder(self.config_file_folder)
        self._create_new_config_file()
        raise ScarConfigFileError(file_path=self.config_file_path)

    def _create_new_config_file(self):
        FileUtils.create_file_with_content(self.config_file_path,
                                           json.dumps(_DEFAULT_CFG, indent=2))

    def _update_config_file(self):
        logger.info(("SCAR configuration file deprecated.\n"
                     "Updating your SCAR configuration file."))
        FileUtils.copy_file(self.config_file_path, self.backup_file_path)
        logger.info(f"Old configuration file saved in '{self.backup_file_path}'.")
        self._create_new_config_file()
        logger.info((f"New configuration file saved in '{self.config_file_path}'.\n"
                     "Please fill your new configuration file with your account information."))
        SysUtils.finish_scar_execution()
Example #7
0
 def prepare_udocker_image(self):
     self.save_tmp_udocker_env()
     image_path = FileUtils.join_paths(FileUtils.get_tmp_dir(),
                                       "udocker_image.tar.gz")
     FileUtils.copy_file(self.aws.lambdaf.image_file, image_path)
     cmd_out = SysUtils.execute_command_with_msg(
         self.udocker_exec + ["load", "-i", image_path],
         cli_msg="Loading image file")
     # Get the image name from the command output
     self.aws.lambdaf.image = cmd_out.split('\n')[1]
     self._create_udocker_container()
     self.aws.lambdaf.environment['Variables'][
         'IMAGE_ID'] = self.aws.lambdaf.image
     self._set_udocker_local_registry()
     self.restore_udocker_env()
Example #8
0
 def restore_udocker_env(self):
     if self.udocker_dir_orig:
         SysUtils.set_environment_variable("UDOCKER_DIR",
                                           self.udocker_dir_orig)
     else:
         SysUtils.delete_environment_variable("UDOCKER_DIR")