3 \meH @s,UdZddlZddlZddlmZddlmZmZddlm Z m Z m Z ddl m Z ddlmZddlmZmZdd lmZejeZd Zd Zd gZd ddeeedededgedgdZeeeZeeedddZeee eddddZ ddZ!ddZ"dd Z#d(d#d$Z$eeeed%d&d'Z%dS))z>LXD: configure lxd with ``lxd init`` and optionally lxd-bridgeN)dedent)ListTuple)safeyamlsubputil)Cloud)Config) MetaSchema get_meta_doc) PER_INSTANCEZlxdbr0aeThis module configures lxd with user specified options using ``lxd init``. If lxd is not present on the system but lxd configuration is provided, then lxd will be installed. If the selected storage backend userspace utility is not installed, it will be installed. If network bridge configuration is provided, then lxd-bridge will be configured accordingly. ZubuntuZcc_lxdZLXDz9Configure LXD with ``lxd init`` and optionally lxd-bridgez # Simplest working directory backed LXD configuration lxd: init: storage_backend: dir a # LXD init showcasing cloud-init's LXD config options lxd: init: network_address: 0.0.0.0 network_port: 8443 storage_backend: zfs storage_pool: datapool storage_create_loop: 10 bridge: mode: new mtu: 1500 name: lxdbr0 ipv4_address: 10.0.8.1 ipv4_netmask: 24 ipv4_dhcp_first: 10.0.8.2 ipv4_dhcp_last: 10.0.8.3 ipv4_dhcp_leases: 250 ipv4_nat: true ipv6_address: fd98:9e0:3744::1 ipv6_netmask: 64 ipv6_nat: true domain: lxd a # For more complex non-iteractive LXD configuration of networks, # storage_pools, profiles, projects, clusters and core config, # `lxd:preseed` config will be passed as stdin to the command: # lxd init --preseed # See https://documentation.ubuntu.com/lxd/en/latest/howto/initialize/#non-interactive-configuration or # run: lxd init --dump to see viable preseed YAML allowed. # # Preseed settings configuring the LXD daemon for HTTPS connections # on 192.168.1.1 port 9999, a nested profile which allows for # LXD nesting on containers and a limited project allowing for # RBAC approach when defining behavior for sub projects. lxd: preseed: | config: core.https_address: 192.168.1.1:9999 networks: - config: ipv4.address: 10.42.42.1/24 ipv4.nat: true ipv6.address: fd42:4242:4242:4242::1/64 ipv6.nat: true description: "" name: lxdbr0 type: bridge project: default storage_pools: - config: size: 5GiB source: /var/snap/lxd/common/lxd/disks/default.img description: "" name: default driver: zfs profiles: - config: {} description: Default LXD profile devices: eth0: name: eth0 network: lxdbr0 type: nic root: path: / pool: default type: disk name: default - config: {} security.nesting: true devices: eth0: name: eth0 network: lxdbr0 type: nic root: path: / pool: default type: disk name: nested projects: - config: features.images: true features.networks: true features.profiles: true features.storage.volumes: true description: Default LXD project name: default - config: features.images: false features.networks: true features.profiles: false features.storage.volumes: false description: Limited Access LXD project name: limited lxd)idnametitle descriptiondistrosZexamplesZ frequencyZactivate_by_schema_keys)init_cfg bridge_cfg preseed_strcCsg}t|ts&|jdt|jdt|tsH|jdt|jdt|tsj|jdt|jd|r|sv|r|rdgng}||rdgng7}|jddj||rtd j|d S) zValidate user-provided lxd network and bridge config option values. @raises: ValueError describing invalid values provided. z/lxd.init config must be a dictionary. found a ''z1lxd.bridge config must be a dictionary. found a 'z.lxd.preseed config must be a string. found a 'zlxd.initz lxd.bridgezMUnable to configure LXD. lxd.preseed config can not be provided with key(s): z, z. N) isinstancedictappendtype__name__strjoin ValueError)rrrerrorsZ incompat_cfgr /usr/lib/python3.6/cc_lxd.pysupplemental_schema_validations"    r")rcfgcloudargsreturnc(CsN|jd}|stjd|dSt|ts>tdt|jd|jdi}|jdd}|jdi}t|||t j dsyt j d d dgWn2t j k r}zt d ||WYdd}~XnXt ||} t| ry|jj| Wn2t j k r} ztjd | | dSd} ~ XnXt j dd dg|rFt j dddg|ddS|r d9} tjdd} |ddkrtjjd| d rtjdt j ddd d!dd"gtd#d$| D} ddd%g} x>| D]6}|j|r| jd&|jd'd(t||fgqWt j | d)}|r8|jd*t}tjjd+rt j |rt|}yszhandle..z--autoz--%s=%s_-zdebconf-communicaterz/etc/default/lxd-bridgezSetting lxd debconf via %s cSsg|]\}}d||fqS)z set %s %sr )r6kvr r r! /szhandle..zFailed to run '%s' for lxd withz Running dpkg-reconfigure for lxdzdpkg-reconfigurez--frontend=noninteractive)net_namedid_initr4attachzCreating lxd bridge: %s z!Setting up default lxd bridge: %sz*Unable to configure lxd bridge without %s.)r)r*r+r,r-r.r/)&getLOGdebugrrrrrr"rwhichProcessExecutionError RuntimeErrorget_required_packageslenZdistroZinstall_packageswarningrZ system_infoospathexiststupleextendreplacer_DEFAULT_NETWORK_NAMEbridge_to_debconfritems ExceptionZlogexcZdel_file bridge_to_cmdmaybe_cleanup_defaultbool_lxc)rr#r$r%Zlxd_cfgrrrepackagesexcZ init_keysZkernelcmdr<Z dconf_commr?debconfr( cmd_create cmd_attachr r r!handles            &        racCs`i}|jddkr&d|d<d|d<n6|jddkrVd|d<d|d <|jd |d<n|jdd krJd|d<|jd r|jd |d<|jd rd|d <|jd |d<|jd|d<|jd|d<|jd|d<|jd|d<|jdd|d<|jdr.d|d<|jd|d<|jd|d<|jdd|d<|jd r\|jd |d!<ntd"|jd|S)#Nmodenonefalsezlxd/setup-bridger'zlxd/bridge-nameexistingtruezlxd/use-existing-bridgernew ipv4_addresszlxd/bridge-ipv4zlxd/bridge-ipv4-address ipv4_netmaskzlxd/bridge-ipv4-netmaskipv4_dhcp_firstzlxd/bridge-ipv4-dhcp-firstipv4_dhcp_lastzlxd/bridge-ipv4-dhcp-lastZipv4_dhcp_leaseszlxd/bridge-ipv4-dhcp-leasesipv4_natzlxd/bridge-ipv4-nat ipv6_addresszlxd/bridge-ipv6zlxd/bridge-ipv6-address ipv6_netmaskzlxd/bridge-ipv6-netmaskipv6_natzlxd/bridge-ipv6-natdomainzlxd/bridge-domainzinvalid bridge mode "%s")rCrH)rr^r r r!rSXsB         rScCs|jddkrd#S|jdt}g}dd|ddg}|jddkrFd|fS|jdd krftd |jddd |g}|jd r|jd r|jd|jd |jd f|jdddkr|jd|jdr|jdrd|jd|jdf}|jd|n |jd|jdrV|jdrV|jd|jd|jdf|jdddkr`|jdn |jd|jdr|jd|jd|jd d$}|d%kr|jd"|||fS)&Nrbrcrnetworkzattach-profiler5eth0rergzinvalid bridge mode "%s"r4rhrizipv4.address=%s/%srlrfz ipv4.nat=truerjrkz%s-%szipv4.dhcp.ranges=%szipv4.address=nonermrnzipv6.address=%s/%srordz ipv6.nat=truezipv6.address=nonerpz dns.domain=%smtuz bridge.mtu=)NNru)rCrRrHr)rZ bridge_namer_r`Z dhcp_rangersr r r!rVsH         rVcCsBdtjjddtjjddd}tjdgt|dg|d dS) NCHOMEz/rootUSERroot)LC_ALLrwrxr3z --force-local)Z update_env)rLenvironrCrlist)r]envr r r!rYs rYr5rrc Cs$|tks| rdSd}d}|rd}y$tdd||gtj||||WnBtjk r} z$| jdkrh| tj||||WYdd} ~ Xn"Xd}tdd |gtj||||r d }y&td d d ||gtj||||WnDtjk r} z$| jdkr| tj||||WYdd} ~ XnXdS)aNewer versions of lxc (3.0.1+) create a lxdbr0 network when 'lxd init --auto' is run. Older versions did not. By removing any that lxd-init created, we simply leave the add/attach code intact. https://github.com/canonical/lxd/issues/4649Nz"failed. Assuming it did not exist.z succeeded.z/Detach of lxd network '%s' from profile '%s' %srqzdetach-profilertzDeletion of lxd network '%s' %sdeletez+Removal of device '%s' from profile '%s' %sprofileZdeviceremove)rRrYrDrErrGZ exit_code) r?r@r4rArZnic_nameZfail_assume_enoentZ succeededmsgrZr r r!rWs2  " rW)rrr&c Csg}g}i}d|kr"|j|d|rdd|krdytj|}Wn&tjttfk rbtjdYnXx,|jdgD]}|jdrr|j|dqrWd|krt j d r|jdd|krt j d r|jd d |krt j d  r|jd |S) z&identify required packages for installr+Z storage_poolsz|lxd.preseed string value is not YAML. Unable to determine required storage driver packages to support storage_pools config.ZdriverZzfszzfsutils-linuxr2ZlvcreateZlvm2Zbtrfsz mkfs.btrfsz btrfs-progs) rrloadZ YAMLError TypeErrorrrDrKrCrrF)rrr[Zstorage_driversZ preseed_cfgr.r r r!rIs*      rI)r5rr)&__doc__ZloggingrLtextwraprZtypingrrZ cloudinitrrrZcloudinit.cloudrZcloudinit.configr Zcloudinit.config.schemar r Zcloudinit.settingsr Z getLoggerrrDrRZMODULE_DESCRIPTIONrmetarrr"r|rarSrVrYrWrIr r r r!sF      L$1< '