def upload_local_image_artifacts(resource_id, resource_dict, property_name, parent_dir, uploader): """ Upload local artifacts referenced by the property at given resource and return ECR URL of the uploaded object. It is the responsibility of callers to ensure property value is a valid image. If path is already a path to S3 object, this method does nothing. :param resource_id: Id of the CloudFormation resource :param resource_dict: Dictionary containing resource definition :param property_name: Property name of CloudFormation resource where this local path is present :param parent_dir: Resolve all relative paths with respect to this directory :param uploader: Method to upload files to ECR :return: ECR URL of the uploaded object """ image_path = jmespath.search(property_name, resource_dict) if not image_path: raise ImageNotFoundError(property_name=property_name, resource_id=resource_id) if is_ecr_url(image_path): LOG.debug("Property %s of %s is already an ECR URL", property_name, resource_id) return image_path return uploader.upload(image_path, resource_id)
def convert(self, value, param, ctx): key_value_pair = value.split("=") if len(key_value_pair) != 2: raise click.BadParameter( f"{param.opts[0]} is not a valid format, it needs to be of the form function_logical_id=ECR_URI" ) key = key_value_pair[0] _value = key_value_pair[1] if not is_ecr_url(_value): raise click.BadParameter(f"{param.opts[0]} needs to have valid ECR URI as value") return {key: _value}
def prompt_image_repository(self, stacks: List[Stack]): """ Prompt for the image repository to push the images. For each image function found in build artifacts, it will prompt for an image repository. Parameters ---------- stacks : List[Stack] List of stacks to look for image functions. Returns ------- Dict A dictionary contains image function logical ID as key, image repository as value. """ image_repositories = {} artifacts_format = get_template_artifacts_format( template_file=self.template_file) if IMAGE in artifacts_format: self.function_provider = SamFunctionProvider( stacks, ignore_code_extraction_warnings=True) function_resources = get_template_function_resource_ids( template_file=self.template_file, artifact=IMAGE) for resource_id in function_resources: image_repositories[resource_id] = prompt( f"\t{self.start_bold}Image Repository for {resource_id}{self.end_bold}", default=self.image_repositories.get(resource_id, "") if isinstance(self.image_repositories, dict) else "" or self.image_repository, ) if not is_ecr_url(image_repositories.get(resource_id)): raise GuidedDeployFailedError( f"Invalid Image Repository ECR URI: {image_repositories.get(resource_id)}" ) for resource_id, function_prop in self.function_provider.functions.items( ): if function_prop.packagetype == IMAGE: image = function_prop.imageuri try: tag = tag_translation(image) except NonLocalImageException: pass except NoImageFoundException as ex: raise GuidedDeployFailedError( "No images found to deploy, try running sam build" ) from ex else: click.secho( f"\t {image} to be pushed to {image_repositories.get(resource_id)}:{tag}" ) click.secho(nl=True) return image_repositories
def prompt_image_repository(self, parameter_overrides): image_repositories = {} artifacts_format = get_template_artifacts_format( template_file=self.template_file) if IMAGE in artifacts_format: self.transformed_resources = transform_template( parameter_overrides=parameter_overrides, template_dict=get_template_data( template_file=self.template_file), ) function_resources = get_template_function_resource_ids( template_file=self.template_file, artifact=IMAGE) for resource_id in function_resources: image_repositories[resource_id] = prompt( f"\t{self.start_bold}Image Repository for {resource_id}{self.end_bold}", default=self.image_repositories.get(resource_id, "") if isinstance(self.image_repositories, dict) else "" or self.image_repository, ) if not is_ecr_url(image_repositories.get(resource_id)): raise GuidedDeployFailedError( f"Invalid Image Repository ECR URI: {image_repositories.get(resource_id)}" ) for resource_id, function_prop in self.transformed_resources.functions.items( ): if function_prop.packagetype == IMAGE: image = function_prop.imageuri try: tag = tag_translation(image) except NonLocalImageException: pass except NoImageFoundException as ex: raise GuidedDeployFailedError( "No images found to deploy, try running sam build" ) from ex else: click.secho( f"\t {image} to be pushed to {image_repositories.get(resource_id)}:{tag}" ) click.secho(nl=True) return image_repositories