summaryrefslogtreecommitdiff
path: root/tests/cloud_tests/platforms/ec2/snapshot.py
blob: 2c48cb54ead450611b466d86662eeb4bc0c740c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# This file is part of cloud-init. See LICENSE file for license information.

"""Base EC2 snapshot."""

from ..snapshots import Snapshot
from tests.cloud_tests import LOG


class EC2Snapshot(Snapshot):
    """EC2 image copy backed snapshot."""

    platform_name = 'ec2'

    def __init__(self, platform, properties, config, features, image_ami,
                 delete_on_destroy=True):
        """Set up snapshot.

        @param platform: platform object
        @param properties: image properties
        @param config: image config
        @param features: supported feature flags
        @param image_ami: string of image ami ID
        @param delete_on_destroy: boolean to delete on destroy
        """
        super(EC2Snapshot, self).__init__(
            platform, properties, config, features)

        self.image_ami = image_ami
        self.delete_on_destroy = delete_on_destroy

    def destroy(self):
        """Deregister the backing AMI."""
        if self.delete_on_destroy:
            image = self.platform.ec2_resource.Image(self.image_ami)
            snapshot_id = image.block_device_mappings[0]['Ebs']['SnapshotId']

            LOG.debug('removing custom ami %s', self.image_ami)
            self.platform.ec2_client.deregister_image(ImageId=self.image_ami)

            LOG.debug('removing custom snapshot %s', snapshot_id)
            self.platform.ec2_client.delete_snapshot(SnapshotId=snapshot_id)

    def launch(self, user_data, meta_data=None, block=True, start=True,
               use_desc=None):
        """Launch instance.

        @param user_data: user-data for the instance
        @param meta_data: meta_data for the instance
        @param block: wait until instance is created
        @param start: start instance and wait until fully started
        @param use_desc: string of test name
        @return_value: an Instance
        """
        if meta_data is not None:
            raise ValueError("metadata not supported on Ec2")

        instance = self.platform.create_instance(
            self.properties, self.config, self.features,
            self.image_ami, user_data)

        if start:
            instance.start()

        return instance

# vi: ts=4 expandtab