def _set_ebs_volume(self): """Set ebs root volume in imagebuilder cfn template.""" if (self.config.image is None or self.config.image.root_volume is None or self.config.image.root_volume.size is None): ami_id = imagebuilder_utils.get_ami_id( self.config.build.parent_image) ami_info = AWSApi.instance().ec2.describe_image(ami_id) default_root_volume_size = ami_info.volume_size + PCLUSTER_RESERVED_VOLUME_SIZE if self.config.image is None or self.config.image.root_volume is None: default_root_volume = Volume(size=default_root_volume_size) else: default_root_volume = copy.deepcopy( self.config.image.root_volume) default_root_volume.size = default_root_volume_size ebs = imagebuilder.CfnImageRecipe.EbsInstanceBlockDeviceSpecificationProperty( volume_size=default_root_volume.size, volume_type=ROOT_VOLUME_TYPE, encrypted=default_root_volume.encrypted, kms_key_id=default_root_volume.kms_key_id, ) else: ebs = imagebuilder.CfnImageRecipe.EbsInstanceBlockDeviceSpecificationProperty( volume_size=self.config.image.root_volume.size, volume_type=ROOT_VOLUME_TYPE, encrypted=self.config.image.root_volume.encrypted, kms_key_id=self.config.image.root_volume.kms_key_id, ) return ebs
def _validate_base_ami(self, image: str): try: ami_id = imagebuilder_utils.get_ami_id(image) image_info = AWSApi.instance().ec2.describe_image(ami_id=ami_id) return image_info except AWSClientError: self._add_failure(f"Invalid image '{image}'.", FailureLevel.ERROR) return None
def _validate(self, volume_size: int, image: str): """Validate the volume size is larger than base ami volume size.""" ami_id = imagebuilder_utils.get_ami_id(image) ami_info = AWSApi.instance().ec2.describe_image(ami_id) min_volume_size = ami_info.volume_size if volume_size < min_volume_size: self._add_failure( "Root volume size {0} GB is less than the minimum required size {1} GB that equals parent ami " "volume size.".format(volume_size, min_volume_size), FailureLevel.ERROR, )
def _get_root_device_name(self): ami_id = imagebuilder_utils.get_ami_id(self.config.build.parent_image) ami_info = AWSApi.instance().ec2.describe_image(ami_id) return ami_info.device_name
def test_evaluate_ami_id(mocker, parent_image, response, ami_id): mock_aws_api(mocker) mocker.patch("pcluster.aws.imagebuilder.ImageBuilderClient.get_image_id", return_value=response) assert_that( imagebuilder_utils.get_ami_id(parent_image)).is_equal_to(ami_id)