def _add_init_script(self) -> None: """Copy the init script defined by the user to the payload folder.""" if self.resources_info.get('lambda').get('init_script', False): init_script_path = self.resources_info.get('lambda').get('init_script') FileUtils.copy_file(init_script_path, FileUtils.join_paths(self.tmp_payload_folder.name, FileUtils.get_file_name(init_script_path)))
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()
def _add_init_script(self): if hasattr(self.aws.lambdaf, "init_script"): if hasattr(self.aws, "config_path"): self.aws.lambdaf.init_script = FileUtils.join_paths(self.aws.config_path, self.aws.lambdaf.init_script) FileUtils.copy_file(self.aws.lambdaf.init_script, FileUtils.join_paths(self.scar_tmp_function_folder_path, _INIT_SCRIPT_NAME)) self.aws.lambdaf.environment['Variables']['INIT_SCRIPT_PATH'] = \ f"/var/task/{_INIT_SCRIPT_NAME}"
def _extract_handler_code(self) -> None: function_handler_dest = FileUtils.join_paths(self.scar_tmp_function_folder_path, f"{self.aws.lambdaf.name}.py") file_path = "" with ZipFile(self._supervisor_zip_path) as thezip: for file in thezip.namelist(): if file.endswith("function_handler.py"): file_path = FileUtils.join_paths(self.aws.lambdaf.tmp_folder_path, file) thezip.extract(file, self.aws.lambdaf.tmp_folder_path) break FileUtils.copy_file(file_path, function_handler_dest)
def _add_extra_payload(self) -> None: if self.resources_info.get('lambda').get('extra_payload', False): payload_path = self.resources_info.get('lambda').get('extra_payload') logger.info(f"Adding extra payload '{payload_path}'") if FileUtils.is_file(payload_path): FileUtils.copy_file(self.resources_info.get('lambda').get('extra_payload'), self.tmp_payload_folder.name) else: FileUtils.copy_dir(self.resources_info.get('lambda').get('extra_payload'), self.tmp_payload_folder.name) del(self.resources_info['lambda']['extra_payload'])
def create_ecr_image(resources_info: Dict, supervisor_version: str) -> str: """Creates an ECR image using the user provided image adding the supervisor tools.""" # If the user set an already prepared image return the image name image_name = ContainerImage._ecr_image_name_prepared( resources_info.get('lambda').get('container')) if image_name: return image_name tmp_folder = FileUtils.create_tmp_dir() # Create function config file FileUtils.write_yaml( FileUtils.join_paths(tmp_folder.name, "function_config.yaml"), create_function_config(resources_info)) init_script_path = resources_info.get('lambda').get('init_script') # Copy the init script defined by the user to the payload folder if init_script_path: FileUtils.copy_file( init_script_path, FileUtils.join_paths( tmp_folder.name, FileUtils.get_file_name(init_script_path))) # Get supervisor zip supervisor_zip_path = ContainerImage.get_supervisor_zip( resources_info, supervisor_version) # Unzip the supervisor file to the temp file FileUtils.unzip_folder(supervisor_zip_path, tmp_folder.name) # Create dockerfile to generate the new ECR image FileUtils.create_file_with_content( "%s/Dockerfile" % tmp_folder.name, ContainerImage._create_dockerfile_ecr_image( resources_info.get('lambda'))) # Create the ECR Repo and get the image uri ecr_cli = ECR(resources_info) repo_name = resources_info.get('lambda').get('name') ecr_image = ecr_cli.get_repository_uri(repo_name) if not ecr_image: logger.info('Creating ECR repository: %s' % repo_name) ecr_image = ecr_cli.create_repository(repo_name) # Build and push the image to the ECR repo platform = None arch = resources_info.get('lambda').get('architectures', ['x86_64'])[0] if arch == 'arm64': platform = 'linux/arm64' return ContainerImage._build_push_ecr_image( tmp_folder.name, ecr_image, platform, ecr_cli.get_authorization_token())
def _extract_handler_code(self) -> None: function_handler_dest = FileUtils.join_paths(self.tmp_payload_folder.name, f"{self.resources_info.get('lambda').get('name')}.py") file_path = "" with ZipFile(self.supervisor_zip_path) as thezip: for file in thezip.namelist(): if file.endswith("function_handler.py"): file_path = FileUtils.join_paths(FileUtils.get_tmp_dir(), file) # Extracts the complete folder structure and the file (cannot avoid) thezip.extract(file, FileUtils.get_tmp_dir()) break if file_path: # Copy only the handler to the payload folder FileUtils.copy_file(file_path, function_handler_dest)
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()