o
    wÚ7e¦H  ã                
   @   sF  U d Z ddlZddlmZ ddlmZmZ ddlmZ	 ddlm
Z
mZmZ ddlmZ ddlmZ dd	lmZmZ dd
lmZ e	 e¡ZdZdZdgZdddeeedƒedƒedƒgedgdœZeed< eeƒZ dededefdd„Z dededede!d df
d!d"„Z"d#d$„ Z#d%d&„ Z$d'd(„ Z%	*d/d+d,„Z&deded ee fd-d.„Z'dS )0z>LXD: configure lxd with ``lxd init`` and optionally lxd-bridgeé    N)Údedent)ÚListÚTuple)Úlog)ÚsafeyamlÚsubpÚutil)ÚCloud)ÚConfig)Ú
MetaSchemaÚget_meta_doc)ÚPER_INSTANCEÚlxdbr0ae  This 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.
ÚubuntuÚcc_lxdÚLXDz9Configure 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)ÚidÚnameÚtitleÚdescriptionÚdistrosÚexamplesÚ	frequencyÚactivate_by_schema_keysÚmetaÚinit_cfgÚ
bridge_cfgÚpreseed_strc                 C   sÂ   g }t | tƒs| dt| ƒj› d¡ t |tƒs$| dt|ƒj› d¡ t |tƒs5| dt|ƒj› d¡ |rV| s;|rV| r@dgng }||rHdgng 7 }| dd |¡› ¡ |r_td	 |¡ƒ‚d
S )z…Validate 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 'zlxd.initz
lxd.bridgezMUnable to configure LXD. lxd.preseed config can not be provided with key(s): z, z. N)Ú
isinstanceÚdictÚappendÚtypeÚ__name__ÚstrÚjoinÚ
ValueError)r   r   r   ÚerrorsÚincompat_cfg© r*   ú9/usr/lib/python3/dist-packages/cloudinit/config/cc_lxd.pyÚsupplemental_schema_validation¢   s<   
ÿÿ
ÿÿ
ÿÿÿÿÿr,   r   ÚcfgÚcloudÚargsÚreturnc              
   C   s&  |  d¡}|st d| ¡ d S t|tƒstdt|ƒj› dƒ‚|  di ¡}|  dd¡}|  di ¡}t|||ƒ t	 
d¡sZz	t	 	g d	¢¡ W n t	jyY } ztd
| ƒ|‚d }~ww t||ƒ}	t|	ƒr‡z|j |	¡ W n t	jy† }
 zt d|	|
¡ W Y d }
~
d S d }
~
ww t	 	g d¢¡ |r›t	j	g d¢|d d S |rñd}t ¡ d d }|d dkrÌtj d|› d¡sÌt d¡ t	 	g d¢¡ tdd„ |D ƒƒ}g d¢}|D ]}|  |¡rë| d| dd¡t|| ƒf g¡ qÒt	 	|¡ d}|rˆ|  dt¡}tj d ¡rPt	 
|¡rPt|ƒ}zt d!|¡ d" d#d$„ | ¡ D ƒ¡d" }t	 	dg|¡ W n ty<   t  td%| ¡ Y nw t !d ¡ t d&¡ t	 	g d'¢¡ d S t"|ƒ\}}t#|t$|ƒt$|ƒt$|ƒd( |rtt d)d* |¡¡ t%|ƒ |r†t d+d* |¡¡ t%|ƒ d S d S |r‘td,| ƒ‚d S )-Nr   z8Skipping module named %s, not present or disabled by cfgz*lxd config must be a dictionary. found a 'r   ÚinitÚpreseedÚ Úbridge)ÚsnapÚinstallr   z#Failed to install lxd from snap: %sz!failed to install packages %s: %s)r   Ú	waitreadyz--timeout=300)r   r1   z	--preseed)Údata)Únetwork_addressÚnetwork_portÚstorage_backendÚstorage_create_deviceÚstorage_create_loopÚstorage_poolÚtrust_passwordÚunameé   r;   Úlvmz/lib/modules/z"/kernel/drivers/md/dm-thin-pool.kozqcloud-init doesn't use thinpool by default on Ubuntu due to LP #1982780. This behavior will change in the future.)ÚlxcÚstorageÚcreateÚdefaultrB   zlvm.use_thinpool=falsec                 s   s    | ]	}|d kr|V  qdS )r;   Nr*   )Ú.0Úkeyr*   r*   r+   Ú	<genexpr>  s   € ÿzhandle.<locals>.<genexpr>)r   r1   z--autoz--%s=%sÚ_ú-zdebconf-communicater   z/etc/default/lxd-bridgezSetting lxd debconf via %sÚ
c                 S   s   g | ]
\}}d ||f ‘qS )z	set %s %sr*   )rG   ÚkÚvr*   r*   r+   Ú
<listcomp>0  s    zhandle.<locals>.<listcomp>zFailed to run '%s' for lxd withz Running dpkg-reconfigure for lxd)zdpkg-reconfigurer   z--frontend=noninteractive)Únet_nameÚdid_initrE   ÚattachzCreating lxd bridge: %sú z!Setting up default lxd bridge: %sz*Unable to configure lxd bridge without %s.)&ÚgetÚLOGÚdebugr    r!   r'   r#   r$   r,   r   ÚwhichÚProcessExecutionErrorÚRuntimeErrorÚget_required_packagesÚlenÚdistroÚinstall_packagesÚwarningr   Úsystem_infoÚosÚpathÚexistsÚtupleÚextendÚreplacer%   Ú_DEFAULT_NETWORK_NAMEÚbridge_to_debconfr&   ÚitemsÚ	ExceptionÚlogexcÚdel_fileÚbridge_to_cmdÚmaybe_cleanup_defaultÚboolÚ_lxc)r   r-   r.   r/   Úlxd_cfgr   r   r   ÚeÚpackagesÚexcÚ	init_keysÚkernelÚcmdrM   Ú
dconf_commrP   Údebconfr8   Ú
cmd_createÚ
cmd_attachr*   r*   r+   ÚhandleÇ   sØ   
ÿ
ÿÿ
ÿþ€ÿ
€þ
ÿÿÿÿ
ÿ€
ÿÿýÿÿÿ

ü
ÿüÿÿr{   c                 C   sX  i }|   d¡dkrd|d< d|d< |S |   d¡dkr+d|d< d|d	< |   d
¡|d< |S |   d¡dkr£d|d< |   d
¡rB|   d
¡|d< |   d¡rvd|d< |   d¡|d< |   d¡|d< |   d¡|d< |   d¡|d< |   d¡|d< |   dd¡|d< |   d¡r•d|d< |   d¡|d< |   d¡|d< |   dd¡|d< |   d ¡r¡|   d ¡|d!< |S td"|   d¡ ƒ‚)#NÚmodeÚnoneÚfalsezlxd/setup-bridger3   zlxd/bridge-nameÚexistingÚtruezlxd/use-existing-bridger   ÚnewÚipv4_addresszlxd/bridge-ipv4zlxd/bridge-ipv4-addressÚipv4_netmaskzlxd/bridge-ipv4-netmaskÚipv4_dhcp_firstzlxd/bridge-ipv4-dhcp-firstÚipv4_dhcp_lastzlxd/bridge-ipv4-dhcp-lastÚipv4_dhcp_leaseszlxd/bridge-ipv4-dhcp-leasesÚipv4_natzlxd/bridge-ipv4-natÚipv6_addresszlxd/bridge-ipv6zlxd/bridge-ipv6-addressÚipv6_netmaskzlxd/bridge-ipv6-netmaskÚipv6_natzlxd/bridge-ipv6-natÚdomainzlxd/bridge-domainúinvalid bridge mode "%s")rT   rY   )r   rx   r*   r*   r+   rg   Y  sN   )Ù$Þ

ÿÿÿ
ÿ
þrg   c                 C   s¤  |   d¡dkr	dS |   dt¡}g }dd|ddg}|   d¡d	kr#d |fS |   d¡d
kr3td|   d¡ ƒ‚dd|g}|   d¡r||   d¡r|| d|   d¡|   d¡f ¡ |   dd¡dkr^| d¡ |   d¡r{|   d¡r{d|   d¡|   d¡f }| d| ¡ n| d¡ |   d¡r¨|   d¡r¨| d|   d¡|   d¡f ¡ |   dd¡dkr§| d¡ n| d¡ |   d¡r¼| d |   d¡ ¡ |   d!d"¡}|d"krÎ| d#|› ¡ ||fS )$Nr|   r}   )NNr   Únetworkzattach-profilerF   Úeth0r   r   rŒ   rE   r‚   rƒ   zipv4.address=%s/%sr‡   r€   zipv4.nat=truer„   r…   z%s-%szipv4.dhcp.ranges=%szipv4.address=nonerˆ   r‰   zipv6.address=%s/%srŠ   r~   zipv6.nat=truezipv6.address=noner‹   zdns.domain=%sÚmtuéÿÿÿÿzbridge.mtu=)rT   rf   rY   r"   )r   Úbridge_namery   rz   Ú
dhcp_ranger   r*   r*   r+   rl   Š  sX   
ÿÿ
ÿþ€
ÿÿ
€

rl   c                 C   sB   dt j dd¡t j dd¡dœ}tjdgt| ƒ dg |d	 d S )
NÚCÚHOMEz/rootÚUSERÚroot)ÚLC_ALLr”   r•   rC   z--force-local)Ú
update_env)r`   ÚenvironrT   r   Úlist)rv   Úenvr*   r*   r+   ro   Æ  s
   ý"ro   rF   rŽ   c           
   
   C   s&  | t ks|sdS d}d}|rUd}ztdd| |gƒ t || ||¡ W n" tjyD }	 z|	jdkr2|	‚t || ||¡ W Y d}	~	nd}	~	ww d}tdd	| gƒ t || |¡ |r‘d
}ztddd||gƒ t ||||¡ W dS  tjy }	 z|	jdkr}|	‚t ||||¡ W Y d}	~	dS d}	~	ww dS )a   Newer 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' %sr   zdetach-profileé   zDeletion of lxd network '%s' %sÚdeletez+Removal of device '%s' from profile '%s' %sÚprofileÚdeviceÚremove)rf   ro   rU   rV   r   rX   Ú	exit_code)
rP   rQ   rE   rR   rž   Únic_nameÚfail_assume_enoentÚ	succeededÚmsgrq   r*   r*   r+   rm   Ï  s<   

€ý
€ýûrm   c              
   C   sä   g }g }i }d| v r|  | d ¡ |r1d|v r1zt |¡}W n tjttfy0   t d¡ Y nw | dg ¡D ]}| d¡rE|  |d ¡ q7d|v rTt	 
d¡sT|  d¡ d|v rbt	 
d¡sb|  d	¡ d
|v rpt	 
d¡sp|  d¡ |S )z&identify required packages for installr;   Ústorage_poolsz|lxd.preseed string value is not YAML.  Unable to determine required storage driver packages to support storage_pools config.ÚdriverÚzfszzfsutils-linuxrB   ÚlvcreateÚlvm2Úbtrfsz
mkfs.btrfszbtrfs-progs)r"   r   ÚloadÚ	YAMLErrorÚ	TypeErrorr'   rU   r^   rT   r   rW   )r   r   rr   Ústorage_driversÚpreseed_cfgr>   r*   r*   r+   rZ   ÷  s0   ÿÿ
€


rZ   )rF   rŽ   )(Ú__doc__r`   Útextwrapr   Útypingr   r   Ú	cloudinitr   Úloggingr   r   r   Úcloudinit.cloudr	   Úcloudinit.configr
   Úcloudinit.config.schemar   r   Úcloudinit.settingsr   Ú	getLoggerr$   rU   rf   ÚMODULE_DESCRIPTIONr   r   Ú__annotations__r!   r%   r,   rš   r{   rg   rl   ro   rm   rZ   r*   r*   r*   r+   Ú<module>   s`   
ÿÿÿÝr‡|ÿÿ
ÿ% 1<

ÿ(