def _create_layer(self) -> str: # Create tmp folders tmp_path = FileUtils.create_tmp_dir() layer_code_path = FileUtils.create_tmp_dir() # Extract 'extra' and 'faassupervisor' from supervisor_zip_path with zipfile.ZipFile(self.supervisor_zip_path) as thezip: for file in thezip.namelist(): # Remove the parent folder path parent_folder, file_name = file.split('/', 1) if file_name.startswith('extra') or file_name.startswith( 'faassupervisor'): thezip.extract(file, tmp_path.name) # Extract content of 'extra' files in layer_code_path extra_folder_path = FileUtils.join_paths(tmp_path.name, parent_folder, 'extra') files = FileUtils.get_all_files_in_directory(extra_folder_path) for file_path in files: FileUtils.unzip_folder(file_path, layer_code_path.name) # Copy 'faassupervisor' to layer_code_path supervisor_folder_path = FileUtils.join_paths(tmp_path.name, parent_folder, 'faassupervisor') shutil.move( supervisor_folder_path, FileUtils.join_paths(layer_code_path.name, 'python', 'faassupervisor')) # Create layer zip with content of layer_code_path layer_zip_path = FileUtils.join_paths(tmp_path.name, f'{self.layer_name}.zip') FileUtils.zip_folder(layer_zip_path, layer_code_path.name) # Register the layer props = self._get_supervisor_layer_props(layer_zip_path) response = self.layer.create(**props) return response['LayerVersionArn']
def create_function(self): # Create tmp folders zip_payload_path = None supervisor_zip_path = None if self.function.get('runtime') == "image": # Create docker image in ECR self.function['container']['image'] = ContainerImage.create_ecr_image(self.resources_info, self.supervisor_version) else: # Check if supervisor's source is already cached cached, supervisor_zip_path = SupervisorUtils.is_supervisor_cached(self.supervisor_version) if not cached: # Download supervisor supervisor_zip_path = SupervisorUtils.download_supervisor(self.supervisor_version) # Manage supervisor layer self._manage_supervisor_layer(supervisor_zip_path) # Create function tmp_folder = FileUtils.create_tmp_dir() zip_payload_path = FileUtils.join_paths(tmp_folder.name, 'function.zip') self._set_image_id() self._set_fdl() creation_args = self._get_creations_args(zip_payload_path, supervisor_zip_path) response = self.client.create_function(**creation_args) if response and "FunctionArn" in response: self.function['arn'] = response.get('FunctionArn', "") return response
def __init__(self, aws_properties, supervisor_version): self.aws = aws_properties self.supervisor_version = supervisor_version self.scar_tmp_function_folder = FileUtils.create_tmp_dir() self.scar_tmp_function_folder_path = self.scar_tmp_function_folder.name self._supervisor_zip_path = FileUtils.join_paths(self.aws.lambdaf.tmp_folder_path, 'faas.zip') self.package_args = {}
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 _initialize_properties(self, aws_properties): self.aws.lambdaf.environment = {'Variables': {}} self.aws.lambdaf.invocation_type = "RequestResponse" self.aws.lambdaf.log_type = "Tail" self.aws.lambdaf.layers = [] self.aws.lambdaf.tmp_folder = FileUtils.create_tmp_dir() self.aws.lambdaf.tmp_folder_path = self.aws.lambdaf.tmp_folder.name self.aws.lambdaf.zip_file_path = FileUtils.join_paths(self.aws.lambdaf.tmp_folder_path, 'function.zip') if hasattr(self.aws.lambdaf, "name"): self.aws.lambdaf.handler = "{0}.lambda_handler".format(self.aws.lambdaf.name) if not hasattr(self.aws.lambdaf, "asynchronous"): self.aws.lambdaf.asynchronous = False self._set_default_call_parameters()
def __init__(self, resources_info: Dict, supervisor_zip_path: str): self.resources_info = resources_info self.supervisor_zip_path = supervisor_zip_path # Temporal folder to store the supervisor and udocker files self.tmp_payload_folder = FileUtils.create_tmp_dir()
def _create_tmp_folders() -> None: tmp_zip_folder = FileUtils.create_tmp_dir() layer_code_folder = FileUtils.create_tmp_dir() return (tmp_zip_folder.name, layer_code_folder.name)