Skip to content

Commit

Permalink
Merge pull request #14 from wnkz/ft-copy-permissions
Browse files Browse the repository at this point in the history
implement copy-permissions option
  • Loading branch information
wnkz committed Apr 18, 2017
2 parents 9cd98e3 + fa65ec8 commit 9cc3934
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions shipami/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,34 @@ def __copy_image(self, src_image, name=None, name_suffix=None, description=None,
self.__set_managed(dst_image)
self.__set_tag(dst_image, 'shipami:copied_from', '{}:{}'.format(src_region, src_image.id))

# TODO: implement copy_permissions
if copy_permissions:
try:
self.__wait_for_image(dst_image)
for permission in self.__get_image_permissions(src_image):
account_id = permission.get('UserId')
logger.debug('adding launchPermission permission for {} on image {}'.format(account_id, dst_image.id))
self.__share_modify_attribute(dst_image, 'launchPermission', 'add', account_id)

src_block_devices = self.__get_image_block_devices(src_image)
for dst_block_device in self.__get_image_block_devices(dst_image):
dst_snapshot = dst_block_device.get('Snapshot')
self.__wait_for_snapshot(dst_snapshot)
for src_block_device in src_block_devices:
if src_block_device.get('DeviceName') == dst_block_device.get('DeviceName'):
src_snapshot = src_block_device.get('Snapshot')
logger.debug('found matching DeviceName for {} and {}'.format(src_snapshot.id, dst_snapshot.id))
for permission in self.__get_snapshot_permissions(src_snapshot):
account_id = permission.get('UserId')
if account_id == 'aws-marketplace':
account_id = self.MARKETPLACE_ACCOUNT_ID
logger.debug('adding createVolumePermission permission for {} on snapshot {}'.format(account_id, dst_snapshot.id))
self.__share_modify_attribute(dst_snapshot, 'createVolumePermission', 'add', account_id)
except botocore.exceptions.ClientError as e:
message = e.response['Error']['Message']
logger.error(message)
raise RuntimeError(message)

if wait:
if wait and not copy_permissions:
self.__wait_for_image(dst_image)

return dst_image
Expand Down Expand Up @@ -280,6 +305,18 @@ def __get_image_snapshots(self, image):
snapshots.append(ec2.Snapshot(block_device_mapping['Ebs']['SnapshotId']))
return snapshots

def __get_image_block_devices(self, image):
region_name = self.__get_image_region(image)
ec2 = self.__get_session(region_name).resource('ec2')
block_devices = []

# We must wait for the image to be avaiale in order to get the SnapshotIds
self.__wait_for_image(image)
for block_device_mapping in image.block_device_mappings:
if block_device_mapping.get('Ebs'):
block_devices.append({'DeviceName': block_device_mapping.get('DeviceName'), 'Snapshot': ec2.Snapshot(block_device_mapping['Ebs']['SnapshotId'])})
return block_devices

def __set_managed(self, image):
self.__set_tag(image, 'shipami:managed', 'True')

Expand Down

0 comments on commit 9cc3934

Please sign in to comment.