Esempio n. 1
0
 def create_from_volume_id(cls, region_name, volume_id, name):
     vol = None
     ec2 = fcu_boto.ec2.connect_to_region(region_name)
     rs = ec2.get_all_volumes([volume_id])
     if len(rs) == 1:
         v = rs[0]
         vol = cls()
         vol.volume_id = v.id
         vol.name = name
         vol.region_name = v.region.name
         vol.zone_name = v.zone
         vol.put()
     return vol
Esempio n. 2
0
 def create_from_snapshot(self, name, snapshot, size=None):
     if size < self.size:
         size = self.size
     ec2 = self.get_ec2_connection()
     if self.zone_name is None or self.zone_name == '':
         # deal with the migration case where the zone is not set in the logical volume:
         current_volume = ec2.get_all_volumes([self.volume_id])[0]
         self.zone_name = current_volume.zone
     ebs_volume = ec2.create_volume(size, self.zone_name, snapshot)
     v = Volume()
     v.ec2 = self.ec2
     v.volume_id = ebs_volume.id
     v.name = name
     v.mount_point = self.mount_point
     v.device = self.device
     v.region_name = self.region_name
     v.zone_name = self.zone_name
     v.put()
     return v
Esempio n. 3
0
 def _size(self):
     if not hasattr(self, '__size'):
         ec2 = self.get_ec2_connection()
         rs = ec2.get_all_volumes([self.volume_id])
         self.__size = rs[0].size
     return self.__size
Esempio n. 4
0
 def _attachment_state(self):
     ec2 = self.get_ec2_connection()
     rs = ec2.get_all_volumes([self.volume_id])
     return rs[0].attachment_state()
Esempio n. 5
0
def create_snapshot(module, ec2, state=None, description=None, wait=None,
                    wait_timeout=None, volume_id=None, instance_id=None,
                    snapshot_id=None, device_name=None, snapshot_tags=None,
                    last_snapshot_min_age=None):
    snapshot = None
    changed = False

    required = [volume_id, snapshot_id, instance_id]
    if required.count(None) != len(required) - 1: # only 1 must be set
        module.fail_json(msg='One and only one of volume_id or instance_id or snapshot_id must be specified')
    if instance_id and not device_name or device_name and not instance_id:
        module.fail_json(msg='Instance ID and device name must both be specified')

    if instance_id:
        try:
            volumes = ec2.get_all_volumes(filters={'attachment.instance-id': instance_id, 'attachment.device': device_name})
        except fcu_boto.exception.BotoServerError as e:
            module.fail_json(msg = "%s: %s" % (e.error_code, e.error_message))

        if not volumes:
            module.fail_json(msg="Could not find volume with name %s attached to instance %s" % (device_name, instance_id))

        volume_id = volumes[0].id

    if state == 'absent':
        if not snapshot_id:
            module.fail_json(msg = 'snapshot_id must be set when state is absent')
        try:
            ec2.delete_snapshot(snapshot_id)
        except fcu_boto.exception.BotoServerError as e:
            # exception is raised if snapshot does not exist
            if e.error_code == 'InvalidSnapshot.NotFound':
                module.exit_json(changed=False)
            else:
                module.fail_json(msg = "%s: %s" % (e.error_code, e.error_message))

        # successful delete
        module.exit_json(changed=True)

    if last_snapshot_min_age > 0:
        try:
            current_snapshots = ec2.get_all_snapshots(filters={'volume_id': volume_id})
        except fcu_boto.exception.BotoServerError as e:
            module.fail_json(msg="%s: %s" % (e.error_code, e.error_message))

        last_snapshot_min_age = last_snapshot_min_age * 60 # Convert to seconds
        snapshot = _get_most_recent_snapshot(current_snapshots,
                                             max_snapshot_age_secs=last_snapshot_min_age)
    try:
        # Create a new snapshot if we didn't find an existing one to use
        if snapshot is None:
            snapshot = ec2.create_snapshot(volume_id, description=description)
            changed = True
        if wait:
            if not _create_with_wait(snapshot, wait_timeout):
                module.fail_json(msg='Timed out while creating snapshot.')
        if snapshot_tags:
            for k, v in snapshot_tags.items():
                snapshot.add_tag(k, v)
    except fcu_boto.exception.BotoServerError as e:
        module.fail_json(msg="%s: %s" % (e.error_code, e.error_message))

    module.exit_json(changed=changed,
                     snapshot_id=snapshot.id,
                     volume_id=snapshot.volume_id,
                     volume_size=snapshot.volume_size,
                     tags=snapshot.tags.copy())