3
# Copyright (C) 2016 Canonical Ltd.
5
# Author: Wesley Wiedenmeier <wesley.wiedenmeier@canonical.com>
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.
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.
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/>.
20
This module initializes lxd using 'lxd init'
26
network_address: <ip addr>
28
storage_backend: <zfs/dir>
29
storage_create_device: <dev>
30
storage_create_loop: <size>
32
trust_password: <password>
34
mode: <new, existing or none>
36
ipv4_address: <ip addr>
38
ipv4_dhcp_first: <ip addr>
39
ipv4_dhcp_last: <ip addr>
40
ipv4_dhcp_leases: <size>
42
ipv6_address: <ip addr>
48
from cloudinit import util
51
def handle(name, cfg, cloud, log, args):
53
lxd_cfg = cfg.get('lxd')
55
log.debug("Skipping module named %s, not present or disabled by cfg",
58
if not isinstance(lxd_cfg, dict):
59
log.warn("lxd config must be a dictionary. found a '%s'",
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'",
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'",
76
# Install the needed packages
78
if not util.which("lxd"):
79
packages.append('lxd')
81
if init_cfg.get("storage_backend") == "zfs" and not util.which('zfs'):
82
packages.append('zfs')
86
cloud.distro.install_packages(packages)
87
except util.ProcessExecutionError as exc:
88
log.warn("failed to install packages %s: %s", packages, exc)
91
# Set up lxd if init config is given
94
'network_address', 'network_port', 'storage_backend',
95
'storage_create_device', 'storage_create_loop',
96
'storage_pool', 'trust_password')
97
cmd = ['lxd', 'init', '--auto']
100
cmd.extend(["--%s=%s" %
101
(k.replace('_', '-'), str(init_cfg[k]))])
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)
109
# Update debconf database
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)
116
util.logexc(log, "Failed to run '%s' for lxd with" % dconf_comm)
118
# Remove the existing configuration file (forces re-generation)
119
util.del_file("/etc/default/lxd-bridge")
122
log.debug("Running dpkg-reconfigure for lxd")
123
util.subp(['dpkg-reconfigure', 'lxd',
124
'--frontend=noninteractive'])
127
"Unable to configure lxd bridge without %s." + dconf_comm)
130
def bridge_to_debconf(bridge_cfg):
133
if bridge_cfg.get("mode") == "none":
134
debconf["lxd/setup-bridge"] = "false"
135
debconf["lxd/bridge-name"] = ""
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")
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")
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")
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")
171
if bridge_cfg.get("domain"):
172
debconf["lxd/bridge-domain"] = bridge_cfg.get("domain")
175
raise Exception("invalid bridge mode \"%s\"" % bridge_cfg.get("mode"))