Esempio n. 1
0
    def _create_template_ebs_volume(self, operation):
        """
        Helper function to create template EBS volume to work on.

        :param NamedConstant operation: Intended use of created template.
            A value from ``VolumeOperations``.

        :returns: Suitable volume in the right start state for input operation.
        :rtype: boto.ec2.volume.Volume
        """
        volume = EbsVolume()

        # Irrelevant volume attributes.
        volume.id = u'vol-9c48a689'
        volume.create_time = u'2015-07-14T22:46:00.447Z'
        volume.size = 1
        volume.snapshot_id = ''
        volume.zone = u'us-west-2b'
        volume.type = u'standard'

        volume_state_table = VolumeStateTable()
        state_flow = volume_state_table.table[operation]
        start_state = state_flow.start_state.value

        # Interesting volume attribute.
        volume.status = start_state

        return volume
Esempio n. 2
0
    def test_delete_orphaned_volumes(self):
        """ Test that we clean up instance volumes that are orphaned by AWS.
        """
        aws_svc, encryptor_image, guest_image = build_aws_service()

        # Simulate a tagged orphaned volume.
        volume = Volume()
        volume.id = test_aws_service.new_id()
        aws_svc.volumes[volume.id] = volume
        aws_svc.tagged_volumes.append(volume)

        # Verify that lookup succeeds before encrypt().
        self.assertEqual(volume, aws_svc.get_volume(volume.id))
        self.assertEqual([volume],
                         aws_svc.get_volumes(
                             tag_key=encrypt_ami.TAG_ENCRYPTOR_SESSION_ID,
                             tag_value='123'))

        encrypt_ami.encrypt(aws_svc=aws_svc,
                            enc_svc_cls=DummyEncryptorService,
                            image_id=guest_image.id,
                            encryptor_ami=encryptor_image.id)

        # Verify that the volume was deleted.
        self.assertIsNone(aws_svc.volumes.get(volume.id, None))
Esempio n. 3
0
    def test_delete_orphaned_volumes(self):
        """ Test that we clean up instance volumes that are orphaned by AWS.
        """
        aws_svc, encryptor_image, guest_image = build_aws_service()

        # Simulate a tagged orphaned volume.
        volume = Volume()
        volume.id = test_aws_service.new_id()
        aws_svc.volumes[volume.id] = volume
        aws_svc.tagged_volumes.append(volume)

        # Verify that lookup succeeds before encrypt().
        self.assertEqual(volume, aws_svc.get_volume(volume.id))
        self.assertEqual(
            [volume],
            aws_svc.get_volumes(
                tag_key=encrypt_ami.TAG_ENCRYPTOR_SESSION_ID, tag_value='123')
        )

        encrypt_ami.encrypt(
            aws_svc=aws_svc,
            enc_svc_cls=DummyEncryptorService,
            image_id=guest_image.id,
            encryptor_ami=encryptor_image.id
        )

        # Verify that the volume was deleted.
        self.assertIsNone(aws_svc.volumes.get(volume.id, None))
Esempio n. 4
0
    def run_instance(self,
                     image_id,
                     security_group_ids=None,
                     instance_type='m3.medium',
                     block_device_map=None):
        instance = Instance()
        instance.id = _new_id()
        instance.root_device_name = '/dev/sda1'
        instance._state.code = 0
        instance._state.name = 'pending'

        # Create volumes based on block device data from the image.
        image = self.get_image(image_id)
        instance_bdm = BlockDeviceMapping()
        for device_name, bdm in image.block_device_mapping.iteritems():
            # Create a new volume and attach it to the instance.
            volume = Volume()
            volume.size = 8
            volume.id = _new_id()
            self.volumes[volume.id] = volume

            bdt = BlockDeviceType(volume_id=volume.id, size=8)
            instance_bdm[device_name] = bdt

        instance.block_device_mapping = instance_bdm
        self.instances[instance.id] = instance

        return instance
Esempio n. 5
0
    def _create_template_ebs_volume(self, operation):
        """
        Helper function to create template EBS volume to work on.

        :param NamedConstant operation: Intended use of created template.
            A value from ``VolumeOperations``.

        :returns: Suitable volume in the right start state for input operation.
        :rtype: boto.ec2.volume.Volume
        """
        volume = EbsVolume()

        # Irrelevant volume attributes.
        volume.id = u'vol-9c48a689'
        volume.create_time = u'2015-07-14T22:46:00.447Z'
        volume.size = 1
        volume.snapshot_id = ''
        volume.zone = u'us-west-2b'
        volume.type = u'standard'

        volume_state_table = VolumeStateTable()
        state_flow = volume_state_table.table[operation]
        start_state = state_flow.start_state.value

        # Interesting volume attribute.
        volume.status = start_state

        return volume
Esempio n. 6
0
 def create_volume(self, size, zone, **kwargs):
     volume = Volume()
     volume.id = 'vol-' + new_id()
     volume.size = size
     volume.zone = zone
     volume.status = 'available'
     self.volumes[volume.id] = volume
     return volume
Esempio n. 7
0
def detach_volume(ec2_conn, volume_id, force=False, logger=None, timeout=DEFAULT_TIMEOUT):
    logger = logger or logging.getLogger(__name__)
    if isinstance(volume_id, basestring):
        vol = Volume(ec2_conn)
        vol.id = volume_id
    else:
        vol = volume_id

    logger.debug('Detaching volume %s', vol.id)
    try:
        vol.detach(force)
    except BotoServerError, e:
        if e.code != 'IncorrectState':
            raise
Esempio n. 8
0
    def run_instance(self,
                     image_id,
                     security_group_ids=None,
                     instance_type='c3.xlarge',
                     placement=None,
                     block_device_map=None,
                     subnet_id=None,
                     user_data=None,
                     ebs_optimized=True,
                     instance_profile_name=None):
        instance = Instance()
        instance.id = new_id()
        instance.image_id = image_id
        instance.root_device_name = '/dev/sda1'
        instance._state.code = 0
        instance._state.name = 'pending'

        # Create volumes based on block device data from the image.
        image = self.get_image(image_id)
        instance_bdm = BlockDeviceMapping()
        for device_name, bdm in image.block_device_mapping.iteritems():
            # Create a new volume and attach it to the instance.
            volume = Volume()
            volume.size = 8
            volume.id = new_id()
            self.volumes[volume.id] = volume

            bdt = BlockDeviceType(volume_id=volume.id, size=8)
            instance_bdm[device_name] = bdt

        instance.block_device_mapping = instance_bdm
        self.instances[instance.id] = instance

        if self.run_instance_callback:
            args = RunInstanceArgs()
            args.image_id = image_id
            args.instance_type = instance_type
            args.ebs_optimized = ebs_optimized
            args.security_group_ids = security_group_ids
            args.subnet_id = subnet_id
            args.user_data = user_data
            args.instance = instance
            self.run_instance_callback(args)

        return instance
Esempio n. 9
0
def attach_volume(ec2_conn, volume_id, instance_id, devname, to_me=False, logger=None, timeout=DEFAULT_TIMEOUT):
    logger = logger or logging.getLogger(__name__)
    if isinstance(volume_id, basestring):
        vol = Volume(ec2_conn)
        vol.id = volume_id
    else:
        vol = volume_id

    def attach():
        try:
            vol.attach(instance_id, devname)
        except BotoServerError, e:
            if e.status == 400 and not e.code:
                # RHEL here can raise Null body error
                return
            else:
                raise
        return 1
Esempio n. 10
0
    def test_wait_for_volume(self):
        aws_svc, encryptor_image, guest_image = build_aws_service()

        # Create a dummy volume.
        volume = Volume()
        volume.size = 8
        volume.id = new_id()
        volume.status = 'detaching'
        aws_svc.volumes[volume.id] = volume

        def transition_to_available(callback_volume):
            self.num_calls += 1
            self.assertEqual(volume, callback_volume)
            self.assertFalse(self.num_calls > 5)

            if self.num_calls == 5:
                volume.status = 'available'

        aws_svc.get_volume_callback = transition_to_available
        result = aws_service.wait_for_volume(aws_svc, volume.id)
        self.assertEqual(volume, result)