summaryrefslogtreecommitdiff
path: root/cloudinit/config/cc_lxd.py
blob: 63b8fb63ae5daf939668702f46aa5c2860519710 (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
# vi: ts=4 expandtab
#
#    Copyright (C) 2016 Canonical Ltd.
#
#    Author: Wesley Wiedenmeier <wesley.wiedenmeier@canonical.com>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License version 3, as
#    published by the Free Software Foundation.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""
This module initializes lxd using 'lxd init'

Example config:
  #cloud-config
  lxd:
    init:
      network_address: <ip addr>
      network_port: <port>
      storage_backend: <zfs/dir>
      storage_create_device: <dev>
      storage_create_loop: <size>
      storage_pool: <name>
      trust_password: <password>
"""

from cloudinit import util


def handle(name, cfg, cloud, log, args):
    # Get config
    lxd_cfg = cfg.get('lxd')
    if not lxd_cfg:
        log.debug("Skipping module named %s, not present or disabled by cfg")
        return
    if not isinstance(lxd_cfg, dict):
        log.warn("lxd config must be a dictionary. found a '%s'",
                 type(lxd_cfg))
        return

    init_cfg = lxd_cfg.get('init')
    if not isinstance(init_cfg, dict):
        log.warn("lxd/init config must be a dictionary. found a '%s'",
                 type(init_cfg))
        init_cfg = {}

    if not init_cfg:
        log.debug("no lxd/init config. disabled.")
        return

    packages = []
    # Ensure lxd is installed
    if not util.which("lxd"):
        packages.append('lxd')

    # if using zfs, get the utils
    if init_cfg.get("storage_backend") == "zfs" and not util.which('zfs'):
        packages.append('zfs')

    if len(packages):
        try:
            cloud.distro.install_packages(packages)
        except util.ProcessExecutionError as exc:
            log.warn("failed to install packages %s: %s", packages, exc)
            return

    # Set up lxd if init config is given
    init_keys = (
        'network_address', 'network_port', 'storage_backend',
        'storage_create_device', 'storage_create_loop',
        'storage_pool', 'trust_password')
    cmd = ['lxd', 'init', '--auto']
    for k in init_keys:
        if init_cfg.get(k):
            cmd.extend(["--%s=%s" %
                        (k.replace('_', '-'), str(init_cfg[k]))])
    util.subp(cmd)