summaryrefslogtreecommitdiff
path: root/tests/cloud_tests/images/lxd.py
blob: 7a416141639563008366833cf1af14b1ef1d994d (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# This file is part of cloud-init. See LICENSE file for license information.

from tests.cloud_tests.images import base
from tests.cloud_tests.snapshots import lxd as lxd_snapshot


class LXDImage(base.Image):
    """
    LXD backed image
    """
    platform_name = "lxd"

    def __init__(self, name, config, platform, pylxd_image):
        """
        setup
        """
        self.platform = platform
        self._pylxd_image = pylxd_image
        self._instance = None
        super(LXDImage, self).__init__(name, config, platform)

    @property
    def pylxd_image(self):
        self._pylxd_image.sync()
        return self._pylxd_image

    @property
    def instance(self):
        if not self._instance:
            self._instance = self.platform.launch_container(
                image=self.pylxd_image.fingerprint,
                image_desc=str(self), use_desc='image-modification')
        self._instance.start(wait=True, wait_time=self.config.get('timeout'))
        return self._instance

    @property
    def properties(self):
        """
        {} containing: 'arch', 'os', 'version', 'release'
        """
        properties = self.pylxd_image.properties
        return {
            'arch': properties.get('architecture'),
            'os': properties.get('os'),
            'version': properties.get('version'),
            'release': properties.get('release'),
        }

    def execute(self, *args, **kwargs):
        """
        execute command in image, modifying image
        """
        return self.instance.execute(*args, **kwargs)

    def push_file(self, local_path, remote_path):
        """
        copy file at 'local_path' to instance at 'remote_path', modifying image
        """
        return self.instance.push_file(local_path, remote_path)

    def run_script(self, script):
        """
        run script in image, modifying image
        return_value: script output
        """
        return self.instance.run_script(script)

    def snapshot(self):
        """
        create snapshot of image, block until done
        """
        # clone current instance, start and freeze clone
        instance = self.platform.launch_container(
            container=self.instance.name, image_desc=str(self),
            use_desc='snapshot')
        instance.start(wait=True, wait_time=self.config.get('timeout'))
        if self.config.get('boot_clean_script'):
            instance.run_script(self.config.get('boot_clean_script'))
        instance.freeze()
        return lxd_snapshot.LXDSnapshot(
            self.properties, self.config, self.platform, instance)

    def destroy(self):
        """
        clean up data associated with image
        """
        if self._instance:
            self._instance.destroy()
        self.pylxd_image.delete(wait=True)
        super(LXDImage, self).destroy()

# vi: ts=4 expandtab