コード例 #1
0
 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']
コード例 #2
0
ファイル: lambdalayers.py プロジェクト: secobau/scar
def _copy_extra_files(parent_folder: str, tmp_zip_path: str,
                      layer_code_path: str) -> None:
    extra_folder_path = FileUtils.join_paths(tmp_zip_path, 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)
コード例 #3
0
    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())