~cloud-init-dev/cloud-init/trunk

« back to all changes in this revision

Viewing changes to cloudinit/config/cc_lxd.py

  • Committer: Scott Moser
  • Date: 2016-08-10 15:06:15 UTC
  • Revision ID: smoser@ubuntu.com-20160810150615-ma2fv107w3suy1ma
README: Mention move of revision control to git.

cloud-init development has moved its revision control to git.
It is available at 
  https://code.launchpad.net/cloud-init

Clone with 
  git clone https://git.launchpad.net/cloud-init
or
  git clone git+ssh://git.launchpad.net/cloud-init

For more information see
  https://git.launchpad.net/cloud-init/tree/HACKING.rst

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# vi: ts=4 expandtab
2
 
#
3
 
#    Copyright (C) 2016 Canonical Ltd.
4
 
#
5
 
#    Author: Wesley Wiedenmeier <wesley.wiedenmeier@canonical.com>
6
 
#
7
 
#    This program is free software: you can redistribute it and/or modify
8
 
#    it under the terms of the GNU General Public License version 3, as
9
 
#    published by the Free Software Foundation.
10
 
#
11
 
#    This program is distributed in the hope that it will be useful,
12
 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
#    GNU General Public License for more details.
15
 
#
16
 
#    You should have received a copy of the GNU General Public License
17
 
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 
 
19
 
"""
20
 
This module initializes lxd using 'lxd init'
21
 
 
22
 
Example config:
23
 
  #cloud-config
24
 
  lxd:
25
 
    init:
26
 
      network_address: <ip addr>
27
 
      network_port: <port>
28
 
      storage_backend: <zfs/dir>
29
 
      storage_create_device: <dev>
30
 
      storage_create_loop: <size>
31
 
      storage_pool: <name>
32
 
      trust_password: <password>
33
 
    bridge:
34
 
      mode: <new, existing or none>
35
 
      name: <name>
36
 
      ipv4_address: <ip addr>
37
 
      ipv4_netmask: <cidr>
38
 
      ipv4_dhcp_first: <ip addr>
39
 
      ipv4_dhcp_last: <ip addr>
40
 
      ipv4_dhcp_leases: <size>
41
 
      ipv4_nat: <bool>
42
 
      ipv6_address: <ip addr>
43
 
      ipv6_netmask: <cidr>
44
 
      ipv6_nat: <bool>
45
 
      domain: <domain>
46
 
"""
47
 
 
48
 
from cloudinit import util
49
 
 
50
 
 
51
 
def handle(name, cfg, cloud, log, args):
52
 
    # Get config
53
 
    lxd_cfg = cfg.get('lxd')
54
 
    if not lxd_cfg:
55
 
        log.debug("Skipping module named %s, not present or disabled by cfg",
56
 
                  name)
57
 
        return
58
 
    if not isinstance(lxd_cfg, dict):
59
 
        log.warn("lxd config must be a dictionary. found a '%s'",
60
 
                 type(lxd_cfg))
61
 
        return
62
 
 
63
 
    # Grab the configuration
64
 
    init_cfg = lxd_cfg.get('init')
65
 
    if not isinstance(init_cfg, dict):
66
 
        log.warn("lxd/init config must be a dictionary. found a '%s'",
67
 
                 type(init_cfg))
68
 
        init_cfg = {}
69
 
 
70
 
    bridge_cfg = lxd_cfg.get('bridge')
71
 
    if not isinstance(bridge_cfg, dict):
72
 
        log.warn("lxd/bridge config must be a dictionary. found a '%s'",
73
 
                 type(bridge_cfg))
74
 
        bridge_cfg = {}
75
 
 
76
 
    # Install the needed packages
77
 
    packages = []
78
 
    if not util.which("lxd"):
79
 
        packages.append('lxd')
80
 
 
81
 
    if init_cfg.get("storage_backend") == "zfs" and not util.which('zfs'):
82
 
        packages.append('zfs')
83
 
 
84
 
    if len(packages):
85
 
        try:
86
 
            cloud.distro.install_packages(packages)
87
 
        except util.ProcessExecutionError as exc:
88
 
            log.warn("failed to install packages %s: %s", packages, exc)
89
 
            return
90
 
 
91
 
    # Set up lxd if init config is given
92
 
    if init_cfg:
93
 
        init_keys = (
94
 
            'network_address', 'network_port', 'storage_backend',
95
 
            'storage_create_device', 'storage_create_loop',
96
 
            'storage_pool', 'trust_password')
97
 
        cmd = ['lxd', 'init', '--auto']
98
 
        for k in init_keys:
99
 
            if init_cfg.get(k):
100
 
                cmd.extend(["--%s=%s" %
101
 
                            (k.replace('_', '-'), str(init_cfg[k]))])
102
 
        util.subp(cmd)
103
 
 
104
 
    # Set up lxd-bridge if bridge config is given
105
 
    dconf_comm = "debconf-communicate"
106
 
    if bridge_cfg and util.which(dconf_comm):
107
 
        debconf = bridge_to_debconf(bridge_cfg)
108
 
 
109
 
        # Update debconf database
110
 
        try:
111
 
            log.debug("Setting lxd debconf via " + dconf_comm)
112
 
            data = "\n".join(["set %s %s" % (k, v)
113
 
                              for k, v in debconf.items()]) + "\n"
114
 
            util.subp(['debconf-communicate'], data)
115
 
        except Exception:
116
 
            util.logexc(log, "Failed to run '%s' for lxd with" % dconf_comm)
117
 
 
118
 
        # Remove the existing configuration file (forces re-generation)
119
 
        util.del_file("/etc/default/lxd-bridge")
120
 
 
121
 
        # Run reconfigure
122
 
        log.debug("Running dpkg-reconfigure for lxd")
123
 
        util.subp(['dpkg-reconfigure', 'lxd',
124
 
                   '--frontend=noninteractive'])
125
 
    elif bridge_cfg:
126
 
        raise RuntimeError(
127
 
            "Unable to configure lxd bridge without %s." + dconf_comm)
128
 
 
129
 
 
130
 
def bridge_to_debconf(bridge_cfg):
131
 
    debconf = {}
132
 
 
133
 
    if bridge_cfg.get("mode") == "none":
134
 
        debconf["lxd/setup-bridge"] = "false"
135
 
        debconf["lxd/bridge-name"] = ""
136
 
 
137
 
    elif bridge_cfg.get("mode") == "existing":
138
 
        debconf["lxd/setup-bridge"] = "false"
139
 
        debconf["lxd/use-existing-bridge"] = "true"
140
 
        debconf["lxd/bridge-name"] = bridge_cfg.get("name")
141
 
 
142
 
    elif bridge_cfg.get("mode") == "new":
143
 
        debconf["lxd/setup-bridge"] = "true"
144
 
        if bridge_cfg.get("name"):
145
 
            debconf["lxd/bridge-name"] = bridge_cfg.get("name")
146
 
 
147
 
        if bridge_cfg.get("ipv4_address"):
148
 
            debconf["lxd/bridge-ipv4"] = "true"
149
 
            debconf["lxd/bridge-ipv4-address"] = \
150
 
                bridge_cfg.get("ipv4_address")
151
 
            debconf["lxd/bridge-ipv4-netmask"] = \
152
 
                bridge_cfg.get("ipv4_netmask")
153
 
            debconf["lxd/bridge-ipv4-dhcp-first"] = \
154
 
                bridge_cfg.get("ipv4_dhcp_first")
155
 
            debconf["lxd/bridge-ipv4-dhcp-last"] = \
156
 
                bridge_cfg.get("ipv4_dhcp_last")
157
 
            debconf["lxd/bridge-ipv4-dhcp-leases"] = \
158
 
                bridge_cfg.get("ipv4_dhcp_leases")
159
 
            debconf["lxd/bridge-ipv4-nat"] = \
160
 
                bridge_cfg.get("ipv4_nat", "true")
161
 
 
162
 
        if bridge_cfg.get("ipv6_address"):
163
 
            debconf["lxd/bridge-ipv6"] = "true"
164
 
            debconf["lxd/bridge-ipv6-address"] = \
165
 
                bridge_cfg.get("ipv6_address")
166
 
            debconf["lxd/bridge-ipv6-netmask"] = \
167
 
                bridge_cfg.get("ipv6_netmask")
168
 
            debconf["lxd/bridge-ipv6-nat"] = \
169
 
                bridge_cfg.get("ipv6_nat", "false")
170
 
 
171
 
        if bridge_cfg.get("domain"):
172
 
            debconf["lxd/bridge-domain"] = bridge_cfg.get("domain")
173
 
 
174
 
    else:
175
 
        raise Exception("invalid bridge mode \"%s\"" % bridge_cfg.get("mode"))
176
 
 
177
 
    return debconf