Your IP : 216.73.217.13


Current Path : /lib/python3/dist-packages/cloudinit/config/__pycache__/
Upload File :
Current File : //lib/python3/dist-packages/cloudinit/config/__pycache__/cc_lxd.cpython-38.pyc

U

�Ad�G�	@sBUdZddlZddlmZddlmZddlmZmZddl	m
Zddl	mZmZm
Z
ddlmZdd	lmZdd
lmZmZddlmZe�e�ZdZd
ZdgZdddeeed�ed�ed�gedgd�Zeed<ee�Zeee d�dd�Z!e eeee"dd�dd�Z#dd�Z$d d!�Z%d"d#�Z&d+d&d'�Z'ee ee d(�d)d*�Z(dS),z>LXD: configure lxd with ``lxd init`` and optionally lxd-bridge�N)�Logger)�dedent)�List�Tuple)�log)�safeyaml�subp�util)�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://linuxcontainers.org/lxd/docs/master/preseed/ 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�titleZdescription�distrosZexamplesZ	frequencyZactivate_by_schema_keys�meta)�init_cfg�
bridge_cfg�preseed_strcCs�g}t|t�s&|�dt|�j�d��t|t�sH|�dt|�j�d��t|t�sj|�dt|�j�d��|r�|sv|r�|r�dgng}||r�dgng7}|�dd�|����|r�td	�|���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 'zlxd.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)rrr�errorsZincompat_cfg�r"�9/usr/lib/python3/dist-packages/cloudinit/config/cc_lxd.py�supplemental_schema_validation�s*
�
�
��r$)r�cfg�cloudr�args�returnc
Cs�|�d�}|s|�d|�dSt|t�s>tdt|�j�d���|�di�}|�dd�}|�di�}t|||�t||�}	t	|	�r�z|j
�|	�Wn8tj
k
r�}
z|�d	|	|
�WY�dSd}
~
XYnXt�dd
dg�|r�tjdddg|d
�dS|�r�d}t��dd}|ddk�rftj�d|�d���sf|�d�t�ddddddg�tdd�|D��}dddg}
|D]6}|�|��rt|
�d|�dd �t||�fg��qtt�|
�d!}|�r�|�d"t�}tj�d#��rxt�|��rxt|�}z<|�d$|�d%�d&d'�|��D��d%}t�d!g|�Wn&tk
�rPt�|d(|�YnXt�d#�|�d)�t�d*dd+g�nlt |�\}}t!|t"|�t"|�t"|�d,�|�r�|�d-d.�|��t#|�|�r�|�d/d.�|��t#|�n|�r�t$d0|��dS)1Nrz8Skipping module named %s, not present or disabled by cfgz*lxd config must be a dictionary. found a 'rZinitZpreseed�Zbridgez!failed to install packages %s: %sZ	waitreadyz
--timeout=300z	--preseed)�data)Znetwork_addressZnetwork_port�storage_backendZstorage_create_deviceZstorage_create_loop�storage_poolZtrust_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.�lxcZstorage�create�defaultzlvm.use_thinpool=falsecss|]}|dkr|VqdS)r+Nr")�.0�keyr"r"r#�	<genexpr>szhandle.<locals>.<genexpr>z--autoz--%s=%s�_�-zdebconf-communicaterz/etc/default/lxd-bridgezSetting lxd debconf via �
cSsg|]\}}d||f�qS)z	set %s %sr")r3�k�vr"r"r#�
<listcomp>,szhandle.<locals>.<listcomp>zFailed to run '%s' for lxd withz Running dpkg-reconfigure for lxdzdpkg-reconfigurez--frontend=noninteractive)�net_name�did_initr1�attachzCreating lxd bridge: %s� z!Setting up default lxd bridge: %sz*Unable to configure lxd bridge without %s.)%�get�debugrrr rrr$�get_required_packages�lenZdistroZinstall_packagesr�ProcessExecutionError�warningr	Zsystem_info�os�path�exists�tuple�extend�replacer�_DEFAULT_NETWORK_NAME�which�bridge_to_debconfr�items�	ExceptionZlogexcZdel_file�
bridge_to_cmd�maybe_cleanup_default�bool�_lxc�RuntimeError)rr%r&rr'Zlxd_cfgrrr�packages�excZ	init_keysZkernel�cmdr9Z
dconf_commr<�debconfr*�
cmd_create�
cmd_attachr"r"r#�handle�s�
�
�

�����
�
�����


��
�r\cCs`i}|�d�dkr&d|d<d|d<�n6|�d�dkrVd|d<d|d	<|�d
�|d<�n|�d�dk�rJd|d<|�d
�r�|�d
�|d<|�d�r�d|d
<|�d�|d<|�d�|d<|�d�|d<|�d�|d<|�d�|d<|�dd�|d<|�d��r.d|d<|�d�|d<|�d�|d<|�dd�|d<|�d ��r\|�d �|d!<ntd"|�d���|S)#N�mode�none�falsezlxd/setup-bridger)zlxd/bridge-name�existing�truezlxd/use-existing-bridger�new�ipv4_addresszlxd/bridge-ipv4zlxd/bridge-ipv4-address�ipv4_netmaskzlxd/bridge-ipv4-netmask�ipv4_dhcp_firstzlxd/bridge-ipv4-dhcp-first�ipv4_dhcp_lastzlxd/bridge-ipv4-dhcp-lastZipv4_dhcp_leaseszlxd/bridge-ipv4-dhcp-leases�ipv4_natzlxd/bridge-ipv4-nat�ipv6_addresszlxd/bridge-ipv6zlxd/bridge-ipv6-address�ipv6_netmaskzlxd/bridge-ipv6-netmask�ipv6_natzlxd/bridge-ipv6-nat�domainzlxd/bridge-domain�invalid bridge mode "%s")r@rP)rrYr"r"r#rNUsL

����rNcCs�|�d�dkrdS|�dt�}g}dd|ddg}|�d�d	krFd|fS|�d�d
krftd|�d���dd|g}|�d
�r�|�d�r�|�d|�d
�|�d�f�|�dd�dkr�|�d�|�d�r�|�d�r�d|�d�|�d�f}|�d|�n
|�d�|�d��rV|�d��rV|�d|�d�|�d�f�|�dd�dk�r`|�d�n
|�d�|�d��r�|�d |�d��|�d!d"�}|d"k�r�|�d#|���||fS)$Nr]r^)NNr�networkzattach-profiler2�eth0r`rbrlr1rcrdzipv4.address=%s/%srgraz
ipv4.nat=truererfz%s-%szipv4.dhcp.ranges=%szipv4.address=nonerhrizipv6.address=%s/%srjr_z
ipv6.nat=truezipv6.address=nonerkz
dns.domain=%s�mtu���zbridge.mtu=)r@rLrPr)rZbridge_namerZr[Z
dhcp_rangeror"r"r#rQ�sT
��
��
��

rQcCsBdtj�dd�tj�dd�d�}tjdgt|�dg|d	�dS)
N�C�HOMEz/root�USER�root)�LC_ALLrrrsr0z
--force-local)Z
update_env)rF�environr@r�list)rX�envr"r"r#rT�s
�rTr2rnc

Cs"|tks|sdSd}d}|r�d}z$tdd||g�t�||||�WnBtjk
r�}	z"|	jdkrf|	�t�||||�W5d}	~	XYn"Xd}tdd	|g�t�|||�|�rd
}z&tddd
||g�t�||||�WnDtjk
�r}	z"|	jdkr�|	�t�||||�W5d}	~	XYnXdS)z�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/lxc/lxd/issues/4649Nz"failed. Assuming it did not exist.z
succeeded.z/Detach of lxd network '%s' from profile '%s' %srmzdetach-profile�zDeletion of lxd network '%s' %s�deletez+Removal of device '%s' from profile '%s' %s�profileZdevice�remove)rLrT�LOGrArrDZ	exit_code)
r<r=r1r>r{Znic_nameZfail_assume_enoentZ	succeeded�msg�er"r"r#rR�s2

"
rR)rrr(c
Cs�g}t�d�s|�d�g}i}d|kr6|�|d�|rxd|krxzt�|�}Wn&tjttfk
rvt�	d�YnX|�
dg�D]}|�
d�r�|�|d�q�d|kr�t�d�s�|�d�d|kr�t�d	�s�|�d
�d|kr�t�d�s�|�d
�|S)z&identify required packages for installrr+Z
storage_poolsz|lxd.preseed string value is not YAML.  Unable to determine required storage driver packages to support storage_pools config.ZdriverZzfszzfsutils-linuxr/ZlvcreateZlvm2Zbtrfsz
mkfs.btrfszbtrfs-progs)rrMrr�loadZ	YAMLError�	TypeErrorr r}rEr@)rrrVZstorage_driversZpreseed_cfgr,r"r"r#rB�s0

�




rB)r2rn))�__doc__rFZloggingr�textwrapr�typingrrZ	cloudinitrrrr	Zcloudinit.cloudr
Zcloudinit.configrZcloudinit.config.schemarr
Zcloudinit.settingsrZ	getLoggerrr}rLZMODULE_DESCRIPTIONrr�__annotations__rrr$rwr\rNrQrTrRrBr"r"r"r#�<module>sh
����r�|�&�1<
�
(