Skip to content

Commit

Permalink
fix(aws): ec2_ami_get_root_device_name to look into multiple accounts
Browse files Browse the repository at this point in the history
since we have images availble in more then one account,
and images are not always set as public, and we can see their metadata
we should look them up in all of the account possible
  • Loading branch information
fruch committed Aug 7, 2024
1 parent b83b3ad commit 23ccb24
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
2 changes: 1 addition & 1 deletion sdcm/sct_provision/aws/instance_parameters_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def UserData(self) -> Optional[str]: # pylint: disable=invalid-name

@cached_property
def _root_device_name(self):
return ec2_ami_get_root_device_name(image_id=self.ImageId, region=self._region_name)
return ec2_ami_get_root_device_name(image_id=self.ImageId, region_name=self._region_name)

@property
def _root_device_size(self):
Expand Down
2 changes: 1 addition & 1 deletion sdcm/sct_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ def _create_instance(self,
[{"Key": "Name", "Value": instance_name}],
}],
BlockDeviceMappings=[{
"DeviceName": ec2_ami_get_root_device_name(image_id=base_image, region=aws_region.region_name),
"DeviceName": ec2_ami_get_root_device_name(image_id=base_image, region_name=aws_region.region_name),
"Ebs": {
"VolumeSize": root_disk_size_gb or self.instance_root_disk_size(test_duration),
"VolumeType": "gp3"
Expand Down
2 changes: 1 addition & 1 deletion sdcm/tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -1379,7 +1379,7 @@ def get_cluster_aws(self, loader_info, db_info, monitor_info):
if loader_info['disk_size']:
loader_info['device_mappings'] = [{
"DeviceName": ec2_ami_get_root_device_name(image_id=self.params.get('ami_id_loader').split()[0],
region=regions[0]),
region_name=regions[0]),
"Ebs": {
"VolumeSize": loader_info['disk_size'],
"VolumeType": "gp3"
Expand Down
24 changes: 13 additions & 11 deletions sdcm/utils/aws_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def init_monitoring_info_from_params(monitor_info: dict, params: dict, regions:
if monitor_info['disk_size']:
monitor_info['device_mappings'] = [{
"DeviceName": ec2_ami_get_root_device_name(image_id=params.get('ami_id_monitor').split()[0],
region=regions[0]),
region_name=regions[0]),
"Ebs": {
"VolumeSize": monitor_info['disk_size'],
"VolumeType": "gp3"
Expand Down Expand Up @@ -268,7 +268,7 @@ def init_db_info_from_params(db_info: dict, params: dict, regions: List, root_de
if db_info['disk_size']:
root_device = root_device if root_device else ec2_ami_get_root_device_name(
image_id=params.get('ami_id_db_scylla').split()[0],
region=regions[0])
region_name=regions[0])
db_info['device_mappings'] = [{
"DeviceName": root_device,
"Ebs": {
Expand Down Expand Up @@ -430,15 +430,17 @@ def ec2_instance_wait_public_ip(instance):
LOGGER.debug("[%s] Got public ip: %s", instance, instance.public_ip_address)


def ec2_ami_get_root_device_name(image_id, region):
ec2 = boto3.resource('ec2', region)
image = ec2.Image(image_id)
try:
if image.root_device_name:
return image.root_device_name
except (TypeError, ClientError) as exc:
raise AssertionError(f"Image '{image_id}' details not found in '{region}'") from exc
return None
def ec2_ami_get_root_device_name(image_id, region_name):
ec2_resource = boto3.resource('ec2', region_name)

for client in (ec2_resource, get_scylla_images_ec2_resource(region_name=region_name)):
try:
image = client.Image(image_id)
if image.root_device_name:
return image.root_device_name
except (TypeError, AttributeError, ClientError):
pass
raise AssertionError(f"Image '{image_id}' details not found in '{region_name}'")


@functools.cache
Expand Down

0 comments on commit 23ccb24

Please sign in to comment.