cc_lxd.py 5.89 KB
Newer Older
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
# 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>
33 34 35 36 37 38 39 40 41 42 43 44 45
    bridge:
      mode: <new, existing or none>
      name: <name>
      ipv4_address: <ip addr>
      ipv4_netmask: <cidr>
      ipv4_dhcp_first: <ip addr>
      ipv4_dhcp_last: <ip addr>
      ipv4_dhcp_leases: <size>
      ipv4_nat: <bool>
      ipv6_address: <ip addr>
      ipv6_netmask: <cidr>
      ipv6_nat: <bool>
      domain: <domain>
46 47 48 49 50 51
"""

from cloudinit import util


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

62
    # Grab the configuration
63 64 65
    init_cfg = lxd_cfg.get('init')
    if not isinstance(init_cfg, dict):
        log.warn("lxd/init config must be a dictionary. found a '%s'",
66
                 type(init_cfg))
67 68
        init_cfg = {}

69 70 71 72 73
    bridge_cfg = lxd_cfg.get('bridge')
    if not isinstance(bridge_cfg, dict):
        log.warn("lxd/bridge config must be a dictionary. found a '%s'",
                 type(bridge_cfg))
        bridge_cfg = {}
74

75
    # Install the needed packages
76
    packages = []
77
    if not util.which("lxd"):
78
        packages.append('lxd')
79

80
    if init_cfg.get("storage_backend") == "zfs" and not util.which('zfs'):
81 82
        packages.append('zfs')

83
    if len(packages):
84
        try:
85
            cloud.distro.install_packages(packages)
86 87
        except util.ProcessExecutionError as exc:
            log.warn("failed to install packages %s: %s", packages, exc)
88 89 90
            return

    # Set up lxd if init config is given
91 92 93 94 95 96 97 98 99 100 101 102 103
    if init_cfg:
        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)

    # Set up lxd-bridge if bridge config is given
Scott Moser's avatar
Scott Moser committed
104 105
    dconf_comm = "debconf-communicate"
    if bridge_cfg and util.which(dconf_comm):
106
        debconf = bridge_to_debconf(bridge_cfg)
107 108 109

        # Update debconf database
        try:
Scott Moser's avatar
Scott Moser committed
110
            log.debug("Setting lxd debconf via " + dconf_comm)
111
            data = "\n".join(["set %s %s" % (k, v)
Scott Moser's avatar
Scott Moser committed
112
                              for k, v in debconf.items()]) + "\n"
113
            util.subp(['debconf-communicate'], data)
114
        except Exception:
Scott Moser's avatar
Scott Moser committed
115
            util.logexc(log, "Failed to run '%s' for lxd with" % dconf_comm)
116 117

        # Remove the existing configuration file (forces re-generation)
Scott Moser's avatar
Scott Moser committed
118
        util.del_file("/etc/default/lxd-bridge")
119 120

        # Run reconfigure
121 122 123
        log.debug("Running dpkg-reconfigure for lxd")
        util.subp(['dpkg-reconfigure', 'lxd',
                   '--frontend=noninteractive'])
Scott Moser's avatar
Scott Moser committed
124 125 126
    elif bridge_cfg:
        raise RuntimeError(
            "Unable to configure lxd bridge without %s." + dconf_comm)
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142


def bridge_to_debconf(bridge_cfg):
    debconf = {}

    if bridge_cfg.get("mode") == "none":
        debconf["lxd/setup-bridge"] = "false"
        debconf["lxd/bridge-name"] = ""

    elif bridge_cfg.get("mode") == "existing":
        debconf["lxd/setup-bridge"] = "false"
        debconf["lxd/use-existing-bridge"] = "true"
        debconf["lxd/bridge-name"] = bridge_cfg.get("name")

    elif bridge_cfg.get("mode") == "new":
        debconf["lxd/setup-bridge"] = "true"
143 144 145
        if bridge_cfg.get("name"):
            debconf["lxd/bridge-name"] = bridge_cfg.get("name")

146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
        if bridge_cfg.get("ipv4_address"):
            debconf["lxd/bridge-ipv4"] = "true"
            debconf["lxd/bridge-ipv4-address"] = \
                bridge_cfg.get("ipv4_address")
            debconf["lxd/bridge-ipv4-netmask"] = \
                bridge_cfg.get("ipv4_netmask")
            debconf["lxd/bridge-ipv4-dhcp-first"] = \
                bridge_cfg.get("ipv4_dhcp_first")
            debconf["lxd/bridge-ipv4-dhcp-last"] = \
                bridge_cfg.get("ipv4_dhcp_last")
            debconf["lxd/bridge-ipv4-dhcp-leases"] = \
                bridge_cfg.get("ipv4_dhcp_leases")
            debconf["lxd/bridge-ipv4-nat"] = \
                bridge_cfg.get("ipv4_nat", "true")

        if bridge_cfg.get("ipv6_address"):
            debconf["lxd/bridge-ipv6"] = "true"
            debconf["lxd/bridge-ipv6-address"] = \
                bridge_cfg.get("ipv6_address")
            debconf["lxd/bridge-ipv6-netmask"] = \
                bridge_cfg.get("ipv6_netmask")
            debconf["lxd/bridge-ipv6-nat"] = \
                bridge_cfg.get("ipv6_nat", "false")

170 171 172
        if bridge_cfg.get("domain"):
            debconf["lxd/bridge-domain"] = bridge_cfg.get("domain")

173 174 175 176
    else:
        raise Exception("invalid bridge mode \"%s\"" % bridge_cfg.get("mode"))

    return debconf