HEX
Server: Apache
System: Linux server2.voipitup.com.au 4.18.0-553.109.1.lve.el8.x86_64 #1 SMP Thu Mar 5 20:23:46 UTC 2026 x86_64
User: posscale (1027)
PHP: 8.2.30
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/__pycache__/lxd.cpython-310.pyc
o

�N�g
g�
@s�dZddlZddlZddlmZddlZddlZddlmZm	Z	ddl
mZzddlZdZ
ddlZe�ejj�WneyDdZ
Ynwe�e�ZdZd	Zd
ddd
dddddd�	ZdZdZiZdd�Zejjj� d�dd��Z!dd�Z"ejjj� d�							d�dd��Z#ejjj� d�ejjj� d�d d!���Z$ejjj� d�ejjj� d�d"d#���Z%d�d$d%�Z&d&d'�Z'd�d(d)�Z(	d�d*d+�Z)				,							d�d-d.�Z*	d�d/d0�Z+d�d1d2�Z,	d�d3d4�Z-d�d5d6�Z.d�d7d8�Z/	9					d�d:d;�Z0d�d<d=�Z1d�d>d?�Z2d�d@dA�Z3									d�dBdC�Z4	d�dDdE�Z5				d�dFdG�Z6	d�dHdI�Z7	d�dJdK�Z8	L				d�dMdN�Z9	d�dOdP�Z:						Q				d�dRdS�Z;								d�dTdU�Z<	d�dVdW�Z=	d�dXdY�Z>							d�dZd[�Z?	d�d\d]�Z@d�d^d_�ZA	d�d`da�ZB				d�dbdc�ZC	d�ddde�ZD	d�dfdg�ZE	L				d�dhdi�ZF	d�djdk�ZG	d�dldm�ZH	d�dndo�ZI	d�dpdq�ZJd�drds�ZK								d�dtdu�ZL								d�dvdw�ZM							Q	d�dxdy�ZN					d�dzd{�ZO	|				d�d}d~�ZP	d�dd��ZQd�d�d��ZR	d�d�d��ZS	d�d�d��ZT	d�d�d��ZUd�d��ZVd�d�d��ZWd�d��ZXd�d��ZYd�d��ZZd�d�d��Z[d�d��Z\e
�ryddl]Ze^ejd���sUGd�d��d�ejj_�Z`e`ej_`zdd�lambZbWne�ymdd�lcmbZbYnwd�d�d��Zdedebje_ddSdS)�uU
Module for managing the LXD daemon and its containers.

.. versionadded:: 2019.2.0

`LXD(1)`_ is a container "hypervisor". This execution module provides
several functions to help manage it and its containers.

.. note::

    - `pylxd(2)`_ version >=2.2.5 is required to let this work,
      currently only available via pip.

        To install on Ubuntu:

        $ apt-get install libssl-dev python-pip
        $ pip install -U pylxd

    - you need lxd installed on the minion
      for the init() and version() methods.

    - for the config_get() and config_get() methods
      you need to have lxd-client installed.

.. _LXD(1): https://linuxcontainers.org/lxd/
.. _pylxd(2): https://github.com/lxc/pylxd/blob/master/doc/source/installation.rst

:maintainer: René Jochum <rene@jochums.at>
:maturity: new
:depends: python-pylxd
:platform: Linux
�N)�datetime)�CommandExecutionError�SaltInvocationError)�VersionTFzrestructuredtext enz2.2.5�0�1�2�3�4�5�6�7�8)	�unknownZi686�x86_64Zarmv7l�aarch64�ppcZppc64Zppc64leZs390x�gZlxdcCs0trtt��tt�krdd�t�t�fStSdS)NFzdThe lxd execution module cannot be loaded: pylxd "{}" is not supported, you need at least pylxd "{}")FzTThe lxd execution module cannot be loaded: the pylxd python module is not available.)�	HAS_PYLXDr�
pylxd_version�_pylxd_minimal_version�format�__virtualname__�rr�D/opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/lxd.py�__virtual__Rs��rcCstdd�S)zt
    Returns the actual lxd version.

    CLI Example:

    .. code-block:: bash

        salt '*' lxd.version

    �cmd.runz
lxd --version)�__salt__rrrr�versionisrcCstjS)z|
    Returns the actual pylxd version.

    CLI Example:

    .. code-block:: bash

        salt '*' lxd.pylxd_version

    )�pylxd�__version__rrrrrxsr�dirc

Csd|�d�}|dur|d|�d�}|dur|d|�d�}|dur*|d|�d�}|dur6|d|�d�}|durB|d|�d�}|durN|d	|�d�}ztd
|�}Wntyn}	ztd|�dt|	�����d}	~	wwd
|vr�t||�d
�dd���|S)a
    Calls lxd init --auto -- opts

    storage_backend :
        Storage backend to use (zfs or dir, default: dir)

    trust_password :
        Password required to add new clients

    network_address : None
        Address to bind LXD to (default: none)

    network_port : None
        Port to bind LXD to (Default: 8443)

    storage_create_device : None
        Setup device based storage using this DEVICE

    storage_create_loop : None
        Setup loop based storage with this SIZE in GB

    storage_pool : None
        Storage pool to use or create

    CLI Examples:

    To listen on all IPv4/IPv6 Addresses:

    .. code-block:: bash

        salt '*' lxd.init dir PaSsW0rD [::]

    To not listen on Network:

    .. code-block:: bash

        salt '*' lxd.init
    z#lxd init --auto --storage-backend="�"Nz --trust-password="z --network-address="z --network-port="z --storage-create-device="z --storage-create-loop="z --storage-pool="rzFailed to call: 'z', error was: �error:�)r�
ValueErrorr�str�index)
Zstorage_backendZtrust_password�network_addressZnetwork_portZstorage_create_deviceZstorage_create_loopZstorage_pool�cmd�output�errr�init�s41����r,ZlxccCsHd�||�}td|�}d|vrt||�d�dd���d|�d�fS)ag
    Set an LXD daemon config option

    CLI Examples:

    To listen on IPv4 and IPv6 port 8443,
    you can omit the :8443 its the default:

    .. code-block:: bash

        salt '*' lxd.config_set core.https_address [::]:8443

    To set the server trust password:

    .. code-block:: bash

        salt '*' lxd.config_set core.trust_password blah

    zlxc config set "{}" "{}"rr#r$NzConfig value "z" successfully set.)rrrr')�key�valuer)r*rrr�
config_set�s��r/cCs>d|�d�}td|�}d|vrt||�d�dd���|S)z�
    Get an LXD daemon config option

    key :
        The key of the config value to retrieve

    CLI Examples:

    .. code-block:: bash

        salt '*' lxd.config_get core.https_address
    zlxc config get "r"rr#r$N)rrr')r-r)r*rrr�
config_get�s�r0c
Cs`d�t|�t|�t|�t|�f�}|tvrt�d|�t|Szd|dus(|dkr2t�d�t��}nP|�d�r=t�|�}nE|dusE|durItd��t	j
�|�}t	j
�|�}t	j
�|�sbtd�
|���t	j
�|�sotd	�
|���t�d
||||�tj|||f|d�}Wn&tjjy�td|�d
���ty�}z
td�
|t|����d}~ww|t|<|S)a^
    Get an pyxld client, this is not meant to be run over the CLI.

    remote_addr :
        An URL to a remote Server, you also have to give cert and key if you
        provide remote_addr and its a TCP Address!

        Examples:
            https://myserver.lan:8443
            /var/lib/mysocket.sock

    cert :
        PEM Formatted SSL Certificate.

        Examples:
            ~/.config/lxc/client.crt

    key :
        PEM Formatted SSL Key.

        Examples:
            ~/.config/lxc/client.key

    verify_cert : True
        Wherever to verify the cert, this is by default True
        but in the most cases you want to set it off as LXD
        normally uses self-signed certificates.

    See the `requests-docs`_ for the SSL stuff.

    .. _requests-docs: http://docs.python-requests.org/en/master/user/advanced/#ssl-cert-verification

    �|z2Returning the client "%s" from our connection poolNz/var/lib/lxd/unix.socketz*Trying to connect to the local unix socket�/z:You have to give a Cert and Key file for remote endpoints.zTYou have given an invalid cert path: "{}", the file does not exist or is not a file.zTYou have given an invalid key path: "{}", the file does not exists or is not a file.zGTrying to connect to "%s" with cert "%s", key "%s" and verify_cert "%s")Zendpoint�certZverifyzFailed to connect to '�'zPFailed to connect to "{}", looks like the SSL verification failed, error was: {})�joinr&�_connection_pool�log�debugrZClient�
startswithr�os�path�
expanduser�isfiler�
exceptionsZClientConnectionFailedr�	TypeError)�remote_addrr3r-�verify_certZpool_key�clientr+rrr�pylxd_client_getst#��	


����������rCc
Cs<z	|jdd�WdStjjy}ztt|���d}~ww)z�Saves an object (profile/image/container) and
        translate its execpetion on failure

    obj :
        The object to save

    This is an internal method, no CLI Example.
    T��waitN)�saverr>�LXDAPIExceptionrr&)�objr+rrr�pylxd_save_object�s	���rIc
CsTt||||�}|jrdSz	|�|�W|jStjjy)}ztt|���d}~ww)a�
    Authenticate with a remote LXDaemon.

    remote_addr :
        An URL to a remote Server, you also have to give cert and key if you
        provide remote_addr and its a TCP Address!

        Examples:
            https://myserver.lan:8443

    password :
        The password of the remote.

    cert :
        PEM Formatted SSL Certificate.

        Examples:
            ~/.config/lxc/client.crt

    key :
        PEM Formatted SSL Key.

        Examples:
            ~/.config/lxc/client.key

    verify_cert : True
        Wherever to verify the cert, this is by default True
        but in the most cases you want to set it off as LXD
        normally uses self-signed certificates.

    CLI Example:

    .. code-block:: bash

        salt '*' lxd.authenticate https://srv01:8443 <yourpass> ~/.config/lxc/client.crt ~/.config/lxc/client.key false

    See the `requests-docs`_ for the SSL stuff.

    .. _requests-docs: http://docs.python-requests.org/en/master/user/advanced/#ssl-cert-verification

    TN)rCZtrusted�authenticaterr>rGrr&)r@�passwordr3r-rArBr+rrrrJ�s*���rJcC�4t||||�}|j��}|rdd�|D�Stt|�S)a[
    Lists containers

    list_names : False
        Only return a list of names when True

    remote_addr :
        An URL to a remote Server, you also have to give cert and key if
        you provide remote_addr and its a TCP Address!

        Examples:
            https://myserver.lan:8443
            /var/lib/mysocket.sock

    cert :
        PEM Formatted SSL Certificate.

        Examples:
            ~/.config/lxc/client.crt

    key :
        PEM Formatted SSL Key.

        Examples:
            ~/.config/lxc/client.key

    verify_cert : True
        Wherever to verify the cert, this is by default True
        but in the most cases you want to set it off as LXD
        normally uses self-signed certificates.

    CLI Examples:

    Full dict with all available information:

    .. code-block:: bash

        salt '*' lxd.container_list

    For a list of names:

    .. code-block:: bash

        salt '*' lxd.container_list true

    See also `container-attributes`_.

    .. _container-attributes: https://github.com/lxc/pylxd/blob/master/doc/source/containers.rst#container-attributes

    cS�g|]}|j�qSr��name��.0�crrr�
<listcomp>�z"container_list.<locals>.<listcomp>)rC�
containers�all�map�_pylxd_model_to_dict)�
list_namesr@r3r-rArBrUrrr�container_list�s
6

rZrc

Cs|durdg}|dur
i}|duri}t||	|
|�}
t|tttf�s&td��|tvr2td�||���t|t�r<d|d�}t	||�\}}z|
j
j|t|||||d�|d�}Wntj
jyj}ztt|���d}~ww|ss|��d	S|r�|��D]
\}}t||fi|��qy|r�|St|�S)
a	
    Create a container

    name :
        The name of the container

    source :
        Can be either a string containing an image alias:
             "xenial/amd64"

        or an dict with type "image" with alias:
            {"type": "image",
             "alias": "xenial/amd64"}

        or image with "fingerprint":
            {"type": "image",
             "fingerprint": "SHA-256"}

        or image with "properties":
            {"type": "image",
             "properties": {
                "os": "ubuntu",
                "release": "14.04",
                "architecture": "x86_64"}}

        or none:
            {"type": "none"}

        or copy:
            {"type": "copy",
             "source": "my-old-container"}

    profiles : ['default']
        List of profiles to apply on this container

    config :
        A config dict or None (None = unset).

        Can also be a list:
            [{'key': 'boot.autostart', 'value': 1},
             {'key': 'security.privileged', 'value': '1'}]

    devices :
        A device dict or None (None = unset).

    architecture : 'x86_64'
        Can be one of the following:
            * unknown
            * i686
            * x86_64
            * armv7l
            * aarch64
            * ppc
            * ppc64
            * ppc64le
            * s390x

    ephemeral : False
        Destroy this container after stop?

    remote_addr :
        An URL to a remote Server, you also have to give cert and key if
        you provide remote_addr and its a TCP Address!

        Examples:
            https://myserver.lan:8443
            /var/lib/mysocket.sock

    cert :
        PEM Formatted SSL Certificate.

        Examples:
            ~/.config/lxc/client.crt

    key :
        PEM Formatted SSL Key.

        Examples:
            ~/.config/lxc/client.key

    verify_cert : True
        Wherever to verify the cert, this is by default True
        but in the most cases you want to set it off as LXD
        normally uses self-signed certificates.

    _raw : False
        Return the raw pyxld object or a dict?

    CLI Examples:

    .. code-block:: bash

        salt '*' lxd.container_create test xenial/amd64

    See also the `rest-api-docs`_.

    .. _rest-api-docs: https://github.com/lxc/lxd/blob/master/doc/rest-api.md#post-1

    N�defaultz/'profiles' must be formatted as list/tuple/set.z6Unknown architecture '{}' given for the container '{}'�image)�type�alias)rO�architecture�profiles�source�config�	ephemeralrDZ	operation)rC�
isinstance�list�tuple�setr�_architecturesrr&�normalize_input_valuesrU�createrr>rGr�json�items�container_device_addrX)rOrar`rb�devicesr_rcrEr@r3r-rA�_rawrB�	containerr+�dnZdargsrrr�container_createsRr��

�
���rrc
Cs�t||||�}|dur|j��}|r|Sn#g}z	|j�|�g}Wntjjy1td|�d���w|r8|dSg}|D]}	|�t	|	j
t|	�fg��q<|S)a)Gets a container from the LXD

    name :
        The name of the container to get.

    remote_addr :
        An URL to a remote Server, you also have to give cert and key if
        you provide remote_addr and its a TCP Address!

        Examples:
            https://myserver.lan:8443
            /var/lib/mysocket.sock

    cert :
        PEM Formatted SSL Certificate.

        Examples:
            ~/.config/lxc/client.crt

    key :
        PEM Formatted SSL Key.

        Examples:
            ~/.config/lxc/client.key

    verify_cert : True
        Wherever to verify the cert, this is by default True
        but in the most cases you want to set it off as LXD
        normally uses self-signed certificates.

    _raw :
        Return the pylxd object, this is internal and by states in use.
    N�Container '�' not foundr)rCrUrV�getrr>rGr�append�dictrOrX)
rOr@r3r-rArorBrU�infosrprrr�
container_get�s$$
��rycCs$t|||||dd�}|jdd�dS)a�
    Delete a container

    name :
        Name of the container to delete

    remote_addr :
        An URL to a remote Server, you also have to give cert and key if
        you provide remote_addr and its a TCP Address!

        Examples:
            https://myserver.lan:8443
            /var/lib/mysocket.sock

    cert :
        PEM Formatted SSL Certificate.

        Examples:
            ~/.config/lxc/client.crt

    key :
        PEM Formatted SSL Key.

        Examples:
            ~/.config/lxc/client.key

    verify_cert : True
        Wherever to verify the cert, this is by default True
        but in the most cases you want to set it off as LXD
        normally uses self-signed certificates.
    T�rorD)ry�delete�rOr@r3r-rArprrr�container_delete�s r}cCsDt|||||dd�}|jtkrtd|�d���|j|dd�t|�S)a
    Rename a container

    name :
        Name of the container to Rename

    newname :
        The new name of the container

    remote_addr :
        An URL to a remote Server, you also have to give cert and key if
        you provide remote_addr and its a TCP Address!

        Examples:
            https://myserver.lan:8443
            /var/lib/mysocket.sock

    cert :
        PEM Formatted SSL Certificate.

        Examples:
            ~/.config/lxc/client.crt

    key :
        PEM Formatted SSL Key.

        Examples:
            ~/.config/lxc/client.key

    verify_cert : True
        Wherever to verify the cert, this is by default True
        but in the most cases you want to set it off as LXD
        normally uses self-signed certificates.
    Trzz$Can't rename the running container 'z'.rD)ry�status_code�CONTAINER_STATUS_RUNNINGr�renamerX)rOZnewnamer@r3r-rArprrr�container_renames
%
r�c	s�t||||�}|dur|j��}nz	|j�|�g}Wntjjy+td|�d���wg}|D]}i�|���|�	t
|j�fdd�t��D�fg��q0|S)a�
    Get container state

    remote_addr :
        An URL to a remote Server, you also have to give cert and key if
        you provide remote_addr and its a TCP Address!

        Examples:
            https://myserver.lan:8443
            /var/lib/mysocket.sock

    cert :
        PEM Formatted SSL Certificate.

        Examples:
            ~/.config/lxc/client.crt

    key :
        PEM Formatted SSL Key.

        Examples:
            ~/.config/lxc/client.key

    verify_cert : True
        Wherever to verify the cert, this is by default True
        but in the most cases you want to set it off as LXD
        normally uses self-signed certificates.
    Nrsrtcs"i|]
}|�d�s|t�|��qS)�_)r9�getattr)rQ�k��staterr�
<dictcomp>ws�
�z#container_state.<locals>.<dictcomp>)
rCrUrVrurr>rGrr�rvrwrOr!)	rOr@r3r-rArBrUZstatesrprr�r�container_stateFs.�
�����r�cC�(t|||||dd�}|jdd�t|�S)a�
    Start a container

    name :
        Name of the container to start

    remote_addr :
        An URL to a remote Server, you also have to give cert and key if
        you provide remote_addr and its a TCP Address!

        Examples:
            https://myserver.lan:8443
            /var/lib/mysocket.sock

    cert :
        PEM Formatted SSL Certificate.

        Examples:
            ~/.config/lxc/client.crt

    key :
        PEM Formatted SSL Key.

        Examples:
            ~/.config/lxc/client.key

    verify_cert : True
        Wherever to verify the cert, this is by default True
        but in the most cases you want to set it off as LXD
        normally uses self-signed certificates.
    TrzrD)ry�startrXr|rrr�container_start�� r��cCs,t|||||dd�}|j||dd�t|�S)a�
    Stop a container

    name :
        Name of the container to stop

    remote_addr :
        An URL to a remote Server, you also have to give cert and key if
        you provide remote_addr and its a TCP Address!

        Examples:
            https://myserver.lan:8443
            /var/lib/mysocket.sock

    cert :
        PEM Formatted SSL Certificate.

        Examples:
            ~/.config/lxc/client.crt

    key :
        PEM Formatted SSL Key.

        Examples:
            ~/.config/lxc/client.key

    verify_cert : True
        Wherever to verify the cert, this is by default True
        but in the most cases you want to set it off as LXD
        normally uses self-signed certificates.
    TrzrD)ry�stoprX)rO�timeout�forcer@r3r-rArprrr�container_stop�s(r�cCr�)a�
    Restart a container

    name :
        Name of the container to restart

    remote_addr :
        An URL to a remote Server, you also have to give cert and key if
        you provide remote_addr and its a TCP Address!

        Examples:
            https://myserver.lan:8443
            /var/lib/mysocket.sock

    cert :
        PEM Formatted SSL Certificate.

        Examples:
            ~/.config/lxc/client.crt

    key :
        PEM Formatted SSL Key.

        Examples:
            ~/.config/lxc/client.key

    verify_cert : True
        Wherever to verify the cert, this is by default True
        but in the most cases you want to set it off as LXD
        normally uses self-signed certificates.
    TrzrD)ryZrestartrXr|rrr�container_restart�r�r�cCr�)a�
    Freeze a container

    name :
        Name of the container to freeze

    remote_addr :
        An URL to a remote Server, you also have to give cert and key if
        you provide remote_addr and its a TCP Address!

        Examples:
            https://myserver.lan:8443
            /var/lib/mysocket.sock

    cert :
        PEM Formatted SSL Certificate.

        Examples:
            ~/.config/lxc/client.crt

    key :
        PEM Formatted SSL Key.

        Examples:
            ~/.config/lxc/client.key

    verify_cert : True
        Wherever to verify the cert, this is by default True
        but in the most cases you want to set it off as LXD
        normally uses self-signed certificates.
    TrzrD)ryZfreezerXr|rrr�container_freeze�r�r�cCr�)a�
    Unfreeze a container

    name :
        Name of the container to unfreeze

    remote_addr :
        An URL to a remote Server, you also have to give cert and key if
        you provide remote_addr and its a TCP Address!

        Examples:
            https://myserver.lan:8443
            /var/lib/mysocket.sock

    cert :
        PEM Formatted SSL Certificate.

        Examples:
            ~/.config/lxc/client.crt

    key :
        PEM Formatted SSL Key.

        Examples:
            ~/.config/lxc/client.key

    verify_cert : True
        Wherever to verify the cert, this is by default True
        but in the most cases you want to set it off as LXD
        normally uses self-signed certificates.
    TrzrD)ryZunfreezerXr|rrr�container_unfreezer�r�c

Cs|dur|}|dur|}|	dur|}	t|||||	dd�}
t||||�}|
jD]}z|j�|�Wq&tjjy=td��w|
jt	k}
|rM|
rM|
j
dd�z|
j|dd�}|
j|_|��Wntjjyr}zt
t|���d}~ww|
jdd�|r�|
r�|jdd�t|�S)a�Migrate a container.

    If the container is running, it either must be shut down
    first (use stop_and_start=True) or criu must be installed
    on the source and destination machines.

    For this operation both certs need to be authenticated,
    use :mod:`lxd.authenticate <salt.modules.lxd.authenticate`
    to authenticate your cert(s).

    name :
        Name of the container to migrate

    stop_and_start :
        Stop the container on the source and start it on dest

    remote_addr :
        An URL to a remote Server, you also have to give cert and key if
        you provide remote_addr and its a TCP Address!

        Examples:
            https://myserver.lan:8443
            /var/lib/mysocket.sock

    cert :
        PEM Formatted SSL Certificate.

        Examples:
            ~/.config/lxc/client.crt

    key :
        PEM Formatted SSL Key.

        Examples:
            ~/.config/lxc/client.key

    verify_cert : True
        Wherever to verify the cert, this is by default True
        but in the most cases you want to set it off as LXD
        normally uses self-signed certificates.

    CLI Example:

    .. code-block:: bash

        # Authorize
        salt '*' lxd.authenticate https://srv01:8443 <yourpass> ~/.config/lxc/client.crt ~/.config/lxc/client.key false
        salt '*' lxd.authenticate https://srv02:8443 <yourpass> ~/.config/lxc/client.crt ~/.config/lxc/client.key false

        # Migrate phpmyadmin from srv01 to srv02
        salt '*' lxd.container_migrate phpmyadmin stop_and_start=true remote_addr=https://srv02:8443 cert=~/.config/lxc/client.crt key=~/.config/lxc/client.key verify_cert=False src_remote_addr=https://srv01:8443
    NTrzz8not all the profiles from the source exist on the targetrD)ryrCr`rurr>rGrr~rr�ZmigraterFrr&r{r�rX)rOZstop_and_startr@r3r-rA�src_remote_addr�src_cert�src_key�src_verify_certrp�dest_clientZpnameZwas_runningZdest_containerr+rrr�container_migrateDsB@�
��
��r�cC� t|||||dd�}t|d|�S)a
    Get a container config value

    name :
        Name of the container

    config_key :
        The config key to retrieve

    remote_addr :
        An URL to a remote Server, you also have to give cert and key if
        you provide remote_addr and its a TCP Address!

        Examples:
            https://myserver.lan:8443
            /var/lib/mysocket.sock

    cert :
        PEM Formatted SSL Certificate.

        Examples:
            ~/.config/lxc/client.crt

    key :
        PEM Formatted SSL Key.

        Examples:
            ~/.config/lxc/client.key

    verify_cert : True
        Wherever to verify the cert, this is by default True
        but in the most cases you want to set it off as LXD
        normally uses self-signed certificates.
    Trzrb�ry�_get_property_dict_item�rO�
config_keyr@r3r-rArprrr�container_config_get�s%r�cC�"t|||||dd�}t|d||�S)a1
    Set a container config value

    name :
        Name of the container

    config_key :
        The config key to set

    config_value :
        The config value to set

    remote_addr :
        An URL to a remote Server, you also have to give cert and key if
        you provide remote_addr and its a TCP Address!

        Examples:
            https://myserver.lan:8443
            /var/lib/mysocket.sock

    cert :
        PEM Formatted SSL Certificate.

        Examples:
            ~/.config/lxc/client.crt

    key :
        PEM Formatted SSL Key.

        Examples:
            ~/.config/lxc/client.key

    verify_cert : True
        Wherever to verify the cert, this is by default True
        but in the most cases you want to set it off as LXD
        normally uses self-signed certificates.
    Trzrb�ry�_set_property_dict_item)rOr��config_valuer@r3r-rArprrr�container_config_set�s.r�cCr�)a
    Delete a container config value

    name :
        Name of the container

    config_key :
        The config key to delete

    remote_addr :
        An URL to a remote Server, you also have to give cert and key if
        you provide remote_addr and its a TCP Address!

        Examples:
            https://myserver.lan:8443
            /var/lib/mysocket.sock

    cert :
        PEM Formatted SSL Certificate.

        Examples:
            ~/.config/lxc/client.crt

    key :
        PEM Formatted SSL Key.

        Examples:
            ~/.config/lxc/client.key

    verify_cert : True
        Wherever to verify the cert, this is by default True
        but in the most cases you want to set it off as LXD
        normally uses self-signed certificates.
    Trzrb�ry�_delete_property_dict_itemr�rrr�container_config_delete�%r�cCr�)a�
    Get a container device

    name :
        Name of the container

    device_name :
        The device name to retrieve

    remote_addr :
        An URL to a remote Server, you also have to give cert and key if
        you provide remote_addr and its a TCP Address!

        Examples:
            https://myserver.lan:8443
            /var/lib/mysocket.sock

    cert :
        PEM Formatted SSL Certificate.

        Examples:
            ~/.config/lxc/client.crt

    key :
        PEM Formatted SSL Key.

        Examples:
            ~/.config/lxc/client.key

    verify_cert : True
        Wherever to verify the cert, this is by default True
        but in the most cases you want to set it off as LXD
        normally uses self-signed certificates.
    Trzrnr��rO�device_namer@r3r-rArprrr�container_device_get5r�r��diskc	Ks*t|||||dd�}||d<t|d||�S)aW
    Add a container device

    name :
        Name of the container

    device_name :
        The device name to add

    device_type :
        Type of the device

    ** kwargs :
        Additional device args

    remote_addr :
        An URL to a remote Server, you also have to give cert and key if
        you provide remote_addr and its a TCP Address!

        Examples:
            https://myserver.lan:8443
            /var/lib/mysocket.sock

    cert :
        PEM Formatted SSL Certificate.

        Examples:
            ~/.config/lxc/client.crt

    key :
        PEM Formatted SSL Key.

        Examples:
            ~/.config/lxc/client.key

    verify_cert : True
        Wherever to verify the cert, this is by default True
        but in the most cases you want to set it off as LXD
        normally uses self-signed certificates.
    Trzr]rnr�)	rOr��device_typer@r3r-rA�kwargsrprrrrm_s2rmcCr�)a�
    Delete a container device

    name :
        Name of the container

    device_name :
        The device name to delete

    remote_addr :
        An URL to a remote Server, you also have to give cert and key if
        you provide remote_addr and its a TCP Address!

        Examples:
            https://myserver.lan:8443
            /var/lib/mysocket.sock

    cert :
        PEM Formatted SSL Certificate.

        Examples:
            ~/.config/lxc/client.crt

    key :
        PEM Formatted SSL Key.

        Examples:
            ~/.config/lxc/client.key

    verify_cert : True
        Wherever to verify the cert, this is by default True
        but in the most cases you want to set it off as LXD
        normally uses self-signed certificates.
    Trzrnr�r�rrr�container_device_delete�r�r��basec

Cst|�}|�d�sd|��}t||	|
||dd�}
tj�|�}tj�|�sI|�d�dkrItd||d�}|s=t	d|�d	���tj�|�sGt	d
��|}|�
tjj�}|sVtjj}tj�|�sdt
d|�d���tj�|�rp|spt	d
��zd}|
j�tj�|d��Wn)tjjy�Yntjjy�}zt|��d�dkr�d}WYd}~nd}~wwtj�|��r[|�rtj�|tj�|��}|�sd}z|
j�tj�|��Wn-tjjy�d}Yn!tjjy�}zt|��d�dkr�d}n�WYd}~nd}~ww|�rt	d��|du�s|du�s|du�r2t�|�}|du�r"t|j�}|du�r*|j}|du�r2|j}tjj�|d��}|
jj ||�!�|||d�Wd�dS1�sTwYdStj�|��sft	d��|�"tj��rvt#tj�$|��}nL|�r~t#|�}nDz|
j�tj�tj�$|�d��Wn2tjj�y�Yn'tjj�y�}zt|��d�dk�r�d}t#|�}d}WYd}~nd}~ww|�s�t	d��g}t�%|�D]\}}}tj�|||d��&tjj��}|�'|��q�|
�(ddg|�|}|}|}t�%|�D]~\}}}tj�|||d��&tjj��}|D]e}tj�||�}tj�||�}|du�s6|du�s6|du�rUt�|�}|du�rEt|j�}|du�rM|j}|du�rU|j}tjj�|d��}|
jj ||�!�|||d�Wd�n	1�svwY�q�qdS)a�
    Put a file into a container

    name :
        Name of the container

    src :
        The source file or directory

    dst :
        The destination file or directory

    recursive :
        Decent into src directory

    overwrite :
        Replace destination if it exists

    mode :
        Set file mode to octal number

    uid :
        Set file uid (owner)

    gid :
        Set file gid (group)

    remote_addr :
        An URL to a remote Server, you also have to give cert and key if
        you provide remote_addr and its a TCP Address!

        Examples:
            https://myserver.lan:8443
            /var/lib/mysocket.sock

    cert :
        PEM Formatted SSL Certificate.

        Examples:
            ~/.config/lxc/client.crt

    key :
        PEM Formatted SSL Key.

        Examples:
            ~/.config/lxc/client.key

    verify_cert : True
        Wherever to verify the cert, this is by default True
        but in the most cases you want to set it off as LXD
        normally uses self-signed certificates.

    CLI Example:

    .. code-block:: bash

        salt '*' lxd.container_file_put <container name> /var/tmp/foo /var/tmp/

    rTrzz://r�
cp.cache_file��saltenvzFile 'rt�File path must be absolute.�No such file or directory 'r4zGCannot copy overwriting a directory without recursive flag set to true!F�.zIs a directoryNz	not foundz)Destination exists and overwrite is false�rb)�mode�uid�gidz$Source is neither file nor directory�mkdirz-p))r&r9ryr:r;r<�isabs�findrr�rstrip�sep�existsr�isdir�filesrur5rr>�NotFoundrGr=�basename�stat�oct�st_mode�st_uid�st_gid�salt�utils�fopen�put�read�endswith�len�dirname�walk�lstriprv�execute)rO�src�dst�	recursive�	overwriter�r�r�r�r@r3r-rArp�cached_fileZdst_is_directory�why�foundr�Zsrc_fp�idxZdstdirsr;r�r�ZdstdirZset_modeZset_uidZset_gidZsrc_nameZdst_namerrr�container_file_put�s�X

�������





��
"��
  




����r�cCs�|durt|�}|�d�sd|��}t||||	|
dd�}tj�|�}tj�|�s,td��tj�|�r>tj�	|tj�
|��}ntj�tj�|��sLtd��tj�|�rc|sXtd��tj�
|�sbtd��ntj�|�}tj�|�swtd	|�d
���tjj�|d��}
|
�|j�|��Wd�n1s�wY|r�t�||�|s�|dkr�t|�}nd}|s�|dkr�t|�}nd}|dks�|dkr�t�|||�dS)
a�
    Get a file from a container

    name :
        Name of the container

    src :
        The source file or directory

    dst :
        The destination file or directory

    mode :
        Set file mode to octal number

    uid :
        Set file uid (owner)

    gid :
        Set file gid (group)

    remote_addr :
        An URL to a remote Server, you also have to give cert and key if
        you provide remote_addr and its a TCP Address!

        Examples:
            https://myserver.lan:8443
            /var/lib/mysocket.sock

    cert :
        PEM Formatted SSL Certificate.

        Examples:
            ~/.config/lxc/client.crt

    key :
        PEM Formatted SSL Key.

        Examples:
            ~/.config/lxc/client.key

    verify_cert : True
        Wherever to verify the cert, this is by default True
        but in the most cases you want to set it off as LXD
        normally uses self-signed certificates.

    NrTrzr�z/Parent directory for destination doesn't exist.z*Destination exists and overwrite is false.z%Destination exists but is not a file.r�r4�wb���)r&r9ryr:r;r<r�rr�r5r�r�r�r=rr�r�r�r��writeru�chmod�int�chown)rOr�r�r�r�r�r�r@r3r-rArpZdst_pathZdfrrr�container_file_get�sFA

��

r�c

Cs�t|||||dd�}z$|�|�}i}t|d�s#td|d|dd�}n
t|j|j|jd�}Wntjj	yK}	ztddt
|	�d�}WYd}	~	nd}	~	wwt|d�dkrZd	|d
<|Sd|d
<|S)a�
    Execute a command list on a container.

    name :
        Name of the container

    cmd :
        Command to be executed (as a list)

        Example :
            '["ls", "-l"]'

    remote_addr :
        An URL to a remote Server, you also have to give cert and key if
        you provide remote_addr and its a TCP Address!

        Examples:
            https://myserver.lan:8443
            /var/lib/mysocket.sock

    cert :
        PEM Formatted SSL Certificate.

        Examples:
            ~/.config/lxc/client.crt

    key :
        PEM Formatted SSL Key.

        Examples:
            ~/.config/lxc/client.key

    verify_cert : True
        Wherever to verify the cert, this is by default True
        but in the most cases you want to set it off as LXD
        normally uses self-signed certificates.

    CLI Example:

    .. code-block:: bash

        salt '*' lxd.container_execute <container name> '["ls", "-l"]'

    Trz�	exit_coder�)r��stdout�stderr�NF�result)ryr��hasattrrwr�r�r�rr>r�r&r�)
rOr)r@r3r-rArpr�Z
saltresultr+rrr�container_executes2/

������r�cCrL)a~Lists all profiles from the LXD.

    list_names :

        Return a list of names instead of full blown dicts.

    remote_addr :
        An URL to a remote Server, you also have to give cert and key if
        you provide remote_addr and its a TCP Address!

        Examples:
            https://myserver.lan:8443
            /var/lib/mysocket.sock

    cert :
        PEM Formatted SSL Certificate.

        Examples:
            ~/.config/lxc/client.crt

    key :
        PEM Formatted SSL Key.

        Examples:
            ~/.config/lxc/client.key

    verify_cert : True
        Wherever to verify the cert, this is by default True
        but in the most cases you want to set it off as LXD
        normally uses self-signed certificates.

    CLI Examples:

    .. code-block:: bash

        salt '*' lxd.profile_list true --out=json
        salt '*' lxd.profile_list --out=json
    cSrMrrN)rQ�prrrrS�rTz profile_list.<locals>.<listcomp>)rCr`rVrWrX)rYr@r3r-rArBr`rrr�profile_listb�
*

r�c
Csxt||||�}t||�\}}z
|j�|||�}	Wntjjy,}
ztt|
���d}
~
ww|dur8||	_	t
|	�t|	�S)a�Creates a profile.

    name :
        The name of the profile to get.

    config :
        A config dict or None (None = unset).

        Can also be a list:
            [{'key': 'boot.autostart', 'value': 1},
             {'key': 'security.privileged', 'value': '1'}]

    devices :
        A device dict or None (None = unset).

    description :
        A description string or None (None = unset).

    remote_addr :
        An URL to a remote Server, you also have to give cert and key if
        you provide remote_addr and its a TCP Address!

        Examples:
            https://myserver.lan:8443
            /var/lib/mysocket.sock

    cert :
        PEM Formatted SSL Certificate.

        Examples:
            ~/.config/lxc/client.crt

    key :
        PEM Formatted SSL Key.

        Examples:
            ~/.config/lxc/client.key

    verify_cert : True
        Wherever to verify the cert, this is by default True
        but in the most cases you want to set it off as LXD
        normally uses self-signed certificates.

    CLI Examples:

    .. code-block:: bash

        salt '*' lxd.profile_create autostart config="{boot.autostart: 1, boot.autostart.delay: 2, boot.autostart.priority: 1}"
        salt '*' lxd.profile_create shared_mounts devices="{shared_mount: {type: 'disk', source: '/home/shared', path: '/home/shared'}}"

    See the `lxd-docs`_ for the details about the config and devices dicts.

    .. _lxd-docs: https://github.com/lxc/lxd/blob/master/doc/rest-api.md#post-10
    N)rCrir`rjrr>rGrr&�descriptionrIrX)rOrbrnr�r@r3r-rArB�profiler+rrr�profile_create�s@��r�cC�Vt||||�}d}z|j�|�}Wntjjy"td|�d���w|r'|St|�S)a~Gets a profile from the LXD

    name :
        The name of the profile to get.

    remote_addr :
        An URL to a remote Server, you also have to give cert and key if
        you provide remote_addr and its a TCP Address!

        Examples:
            https://myserver.lan:8443
            /var/lib/mysocket.sock

    cert :
        PEM Formatted SSL Certificate.

        Examples:
            ~/.config/lxc/client.crt

    key :
        PEM Formatted SSL Key.

        Examples:
            ~/.config/lxc/client.key

    verify_cert : True
        Wherever to verify the cert, this is by default True
        but in the most cases you want to set it off as LXD
        normally uses self-signed certificates.

    _raw :
        Return the pylxd object, this is internal and by states in use.

    CLI Examples:

    .. code-block:: bash

        salt '*' lxd.profile_get autostart
    Nz	Profile 'rt)rCr`rurr>rGrrX)rOr@r3r-rArorBr�rrr�profile_get��*�r�cCs t|||||dd�}|��dS)a*Deletes a profile.

    name :
        The name of the profile to delete.

    remote_addr :
        An URL to a remote Server, you also have to give cert and key if
        you provide remote_addr and its a TCP Address!

        Examples:
            https://myserver.lan:8443
            /var/lib/mysocket.sock

    cert :
        PEM Formatted SSL Certificate.

        Examples:
            ~/.config/lxc/client.crt

    key :
        PEM Formatted SSL Key.

        Examples:
            ~/.config/lxc/client.key

    verify_cert : True
        Wherever to verify the cert, this is by default True
        but in the most cases you want to set it off as LXD
        normally uses self-signed certificates.

    CLI Example:

    .. code-block:: bash

        salt '*' lxd.profile_delete shared_mounts
    Trz)r�r{)rOr@r3r-rAr�rrr�profile_deletes%r�cCr�)a�Get a profile config item.

    name :
        The name of the profile to get the config item from.

    config_key :
        The key for the item to retrieve.

    remote_addr :
        An URL to a remote Server, you also have to give cert and key if
        you provide remote_addr and its a TCP Address!

        Examples:
            https://myserver.lan:8443
            /var/lib/mysocket.sock

    cert :
        PEM Formatted SSL Certificate.

        Examples:
            ~/.config/lxc/client.crt

    key :
        PEM Formatted SSL Key.

        Examples:
            ~/.config/lxc/client.key

    verify_cert : True
        Wherever to verify the cert, this is by default True
        but in the most cases you want to set it off as LXD
        normally uses self-signed certificates.

    CLI Example:

    .. code-block:: bash

        salt '*' lxd.profile_config_get autostart boot.autostart
    Trzrb�r�r��rOr�r@r3r-rAr�rrr�profile_config_getH�*r�cCr�)a�Set a profile config item.

    name :
        The name of the profile to set the config item to.

    config_key :
        The items key.

    config_value :
        Its items value.

    remote_addr :
        An URL to a remote Server, you also have to give cert and key if
        you provide remote_addr and its a TCP Address!

        Examples:
            https://myserver.lan:8443
            /var/lib/mysocket.sock

    cert :
        PEM Formatted SSL Certificate.

        Examples:
            ~/.config/lxc/client.crt

    key :
        PEM Formatted SSL Key.

        Examples:
            ~/.config/lxc/client.key

    verify_cert : True
        Wherever to verify the cert, this is by default True
        but in the most cases you want to set it off as LXD
        normally uses self-signed certificates.

    CLI Example:

    .. code-block:: bash

        salt '*' lxd.profile_config_set autostart boot.autostart 0
    Trzrb)r�r�)rOr�r�r@r3r-rAr�rrr�profile_config_setws3r�cCr�)a�Delete a profile config item.

    name :
        The name of the profile to delete the config item.

    config_key :
        The config key for the value to retrieve.

    remote_addr :
        An URL to a remote Server, you also have to give cert and key if
        you provide remote_addr and its a TCP Address!

        Examples:
            https://myserver.lan:8443
            /var/lib/mysocket.sock

    cert :
        PEM Formatted SSL Certificate.

        Examples:
            ~/.config/lxc/client.crt

    key :
        PEM Formatted SSL Key.

        Examples:
            ~/.config/lxc/client.key

    verify_cert : True
        Wherever to verify the cert, this is by default True
        but in the most cases you want to set it off as LXD
        normally uses self-signed certificates.

    CLI Example:

    .. code-block:: bash

        salt '*' lxd.profile_config_delete autostart boot.autostart.delay
    Trzrb�r�r�r�rrr�profile_config_delete�r�r�cCr�)a|Get a profile device.

    name :
        The name of the profile to get the device from.

    device_name :
        The name of the device to retrieve.

    remote_addr :
        An URL to a remote Server, you also have to give cert and key if
        you provide remote_addr and its a TCP Address!

        Examples:
            https://myserver.lan:8443
            /var/lib/mysocket.sock

    cert :
        PEM Formatted SSL Certificate.

        Examples:
            ~/.config/lxc/client.crt

    key :
        PEM Formatted SSL Key.

        Examples:
            ~/.config/lxc/client.key

    verify_cert : True
        Wherever to verify the cert, this is by default True
        but in the most cases you want to set it off as LXD
        normally uses self-signed certificates.

    CLI Example:

    .. code-block:: bash

        salt '*' lxd.profile_device_get default eth0
    Trzrnr��rOr�r@r3r-rAr�rrr�profile_device_get�r�rcKsHt|||||dd�}||d<|��D]
\}	}
t|
�||	<qt|d||�S)a�Set a profile device.

    name :
        The name of the profile to set the device to.

    device_name :
        The name of the device to set.

    remote_addr :
        An URL to a remote Server, you also have to give cert and key if
        you provide remote_addr and its a TCP Address!

        Examples:
            https://myserver.lan:8443
            /var/lib/mysocket.sock

    cert :
        PEM Formatted SSL Certificate.

        Examples:
            ~/.config/lxc/client.crt

    key :
        PEM Formatted SSL Key.

        Examples:
            ~/.config/lxc/client.key

    verify_cert : True
        Wherever to verify the cert, this is by default True
        but in the most cases you want to set it off as LXD
        normally uses self-signed certificates.

    CLI Example:

    .. code-block:: bash

        salt '*' lxd.profile_device_set autostart eth1 nic nictype=bridged parent=lxdbr0
    Trzr]rn)r�rlr&r�)rOr�r�r@r3r-rAr�r�r��vrrr�profile_device_set
	s
1rcCr�)a�Delete a profile device.

    name :
        The name of the profile to delete the device.

    device_name :
        The name of the device to delete.

    remote_addr :
        An URL to a remote Server, you also have to give cert and key if
        you provide remote_addr and its a TCP Address!

        Examples:
            https://myserver.lan:8443
            /var/lib/mysocket.sock

    cert :
        PEM Formatted SSL Certificate.

        Examples:
            ~/.config/lxc/client.crt

    key :
        PEM Formatted SSL Key.

        Examples:
            ~/.config/lxc/client.key

    verify_cert : True
        Wherever to verify the cert, this is by default True
        but in the most cases you want to set it off as LXD
        normally uses self-signed certificates.

    CLI Example:

    .. code-block:: bash

        salt '*' lxd.profile_device_delete autostart eth1

    Trzrnr�r�rrr�profile_device_deleteH	s+rcCrL)a�Lists all images from the LXD.

    list_aliases :

        Return a dict with the fingerprint as key and
        a list of aliases as value instead.

    remote_addr :
        An URL to a remote Server, you also have to give cert and key if
        you provide remote_addr and its a TCP Address!

        Examples:
            https://myserver.lan:8443
            /var/lib/mysocket.sock

    cert :
        PEM Formatted SSL Certificate.

        Examples:
            ~/.config/lxc/client.crt

    key :
        PEM Formatted SSL Key.

        Examples:
            ~/.config/lxc/client.key

    verify_cert : True
        Wherever to verify the cert, this is by default True
        but in the most cases you want to set it off as LXD
        normally uses self-signed certificates.

    CLI Examples:

    .. code-block:: bash

        salt '*' lxd.image_list true --out=json
        salt '*' lxd.image_list --out=json
    cSs i|]}|jdd�|jD��qS)cSsg|]}|d�qSrNr)rQ�arrrrS�	sz)image_list.<locals>.<dictcomp>.<listcomp>)�fingerprint�aliases)rQ�irrrr��	s zimage_list.<locals>.<dictcomp>)rC�imagesrVrWrX)Zlist_aliasesr@r3r-rArBrrrr�
image_list{	r�r	cCr�)a�Get an image by its fingerprint

    fingerprint :
        The fingerprint of the image to retrieve

    remote_addr :
        An URL to a remote Server, you also have to give cert and key if
        you provide remote_addr and its a TCP Address!

        Examples:
            https://myserver.lan:8443
            /var/lib/mysocket.sock

    cert :
        PEM Formatted SSL Certificate.

        Examples:
            ~/.config/lxc/client.crt

    key :
        PEM Formatted SSL Key.

        Examples:
            ~/.config/lxc/client.key

    verify_cert : True
        Wherever to verify the cert, this is by default True
        but in the most cases you want to set it off as LXD
        normally uses self-signed certificates.

    _raw : False
        Return the raw pylxd object or a dict of it?

    CLI Examples:

    .. code-block:: bash

        salt '*' lxd.image_get <fingerprint>
    NzImage with fingerprint 'rt)rCrrurr>rGrrX)rr@r3r-rArorBr\rrr�	image_get�	r�r
cCr�)a|Get an image by an alias

    alias :
        The alias of the image to retrieve

    remote_addr :
        An URL to a remote Server, you also have to give cert and key if
        you provide remote_addr and its a TCP Address!

        Examples:
            https://myserver.lan:8443
            /var/lib/mysocket.sock

    cert :
        PEM Formatted SSL Certificate.

        Examples:
            ~/.config/lxc/client.crt

    key :
        PEM Formatted SSL Key.

        Examples:
            ~/.config/lxc/client.key

    verify_cert : True
        Wherever to verify the cert, this is by default True
        but in the most cases you want to set it off as LXD
        normally uses self-signed certificates.

    _raw : False
        Return the raw pylxd object or a dict of it?

    CLI Examples:

    .. code-block:: bash

        salt '*' lxd.image_get_by_alias xenial/amd64
    NzImage with alias 'rt)rCrZget_by_aliasrr>rGrrX)r^r@r3r-rArorBr\rrr�image_get_by_alias�	r�rcCst|||||�}|��dS)asDelete an image by an alias or fingerprint

    name :
        The alias or fingerprint of the image to delete,
        can be a obj for the states.

    remote_addr :
        An URL to a remote Server, you also have to give cert and key if
        you provide remote_addr and its a TCP Address!

        Examples:
            https://myserver.lan:8443
            /var/lib/mysocket.sock

    cert :
        PEM Formatted SSL Certificate.

        Examples:
            ~/.config/lxc/client.crt

    key :
        PEM Formatted SSL Key.

        Examples:
            ~/.config/lxc/client.key

    verify_cert : True
        Wherever to verify the cert, this is by default True
        but in the most cases you want to set it off as LXD
        normally uses self-signed certificates.

    CLI Examples:

    .. code-block:: bash

        salt '*' lxd.image_delete xenial/amd64
    T)�
_verify_imager{)r\r@r3r-rArrr�image_delete
s'r
c


Cs�|durg}t||||�}
z|
jj||||d�}Wntjjy-}ztt|���d}~ww|D]}t||�q0|	r<|St	|�S)a�Create an image from simplestreams

    server :
        Simplestreams server URI

    alias :
        The alias of the image to retrieve

    remote_addr :
        An URL to a remote Server, you also have to give cert and key if
        you provide remote_addr and its a TCP Address!

        Examples:
            https://myserver.lan:8443
            /var/lib/mysocket.sock

    cert :
        PEM Formatted SSL Certificate.

        Examples:
            ~/.config/lxc/client.crt

    key :
        PEM Formatted SSL Key.

        Examples:
            ~/.config/lxc/client.key

    verify_cert : True
        Wherever to verify the cert, this is by default True
        but in the most cases you want to set it off as LXD
        normally uses self-signed certificates.

    aliases : []
        List of aliases to append to the copied image

    public : False
        Make this image public available

    auto_update : False
        Should LXD auto update that image?

    _raw : False
        Return the raw pylxd object or a dict of the image?

    CLI Examples:

    .. code-block:: bash

        salt '*' lxd.image_from_simplestreams "https://cloud-images.ubuntu.com/releases" "trusty/amd64" aliases='["t", "trusty/amd64"]' auto_update=True
    N��public�auto_update)
rCrZcreate_from_simplestreamsrr>rGrr&�image_alias_addrX)
Zserverr^r@r3r-rArrrrorBr\r+rrr�image_from_simplestreamsK
s ?
���rc	

Cs~|durg}t||||�}	z|	jj|||d�}
Wntjjy,}ztt|���d}~ww|D]}t|
|�q/|r;|
St	|
�S)azCreate an image from an url

    url :
        The URL from where to download the image

    remote_addr :
        An URL to a remote Server, you also have to give cert and key if
        you provide remote_addr and its a TCP Address!

        Examples:
            https://myserver.lan:8443
            /var/lib/mysocket.sock

    cert :
        PEM Formatted SSL Certificate.

        Examples:
            ~/.config/lxc/client.crt

    key :
        PEM Formatted SSL Key.

        Examples:
            ~/.config/lxc/client.key

    verify_cert : True
        Wherever to verify the cert, this is by default True
        but in the most cases you want to set it off as LXD
        normally uses self-signed certificates.

    aliases : []
        List of aliases to append to the copied image

    public : False
        Make this image public available

    auto_update : False
        Should LXD auto update that image?

    _raw : False
        Return the raw pylxd object or a dict of the image?

    CLI Examples:

    .. code-block:: bash

        salt '*' lxd.image_from_url https://dl.stgraber.org/lxd aliases='["busybox-amd64"]'
    Nr)
rCrZcreate_from_urlrr>rGrr&rrX)
�urlr@r3r-rArrrrorBr\r+r^rrr�image_from_url�
s ;
���rc	
Cs�|durg}td||d�}	d}
tjj�|	d��}|��}
Wd�n1s'wYt||||�}z|jj|
|dd�}
Wnt	j
jyR}ztt
|���d}~ww|D]}t|
|�qU|ra|
St|
�S)arCreate an image from a file

    filename :
        The filename of the rootfs

    remote_addr :
        An URL to a remote Server, you also have to give cert and key if
        you provide remote_addr and its a TCP Address!

        Examples:
            https://myserver.lan:8443
            /var/lib/mysocket.sock

    cert :
        PEM Formatted SSL Certificate.

        Examples:
            ~/.config/lxc/client.crt

    key :
        PEM Formatted SSL Key.

        Examples:
            ~/.config/lxc/client.key

    verify_cert : True
        Wherever to verify the cert, this is by default True
        but in the most cases you want to set it off as LXD
        normally uses self-signed certificates.

    aliases : []
        List of aliases to append to the copied image

    public : False
        Make this image public available

    saltenv : base
        The saltenv to use for salt:// copies

    _raw : False
        Return the raw pylxd object or a dict of the image?

    CLI Examples:

    .. code-block:: bash

        salt '*' lxd.image_from_file salt://lxd/files/busybox.tar.xz aliases=["busybox-amd64"]
    Nr�r��zr+bT)rrE)rr�r�r�r�r�rCrrjrr>rGrr&rrX)�filenamer@r3r-rArrr�ror��data�fprBr\r+r^rrr�image_from_file�
s&;
���rc
Cs�|	durg}	t�d|||�d}
zt|||||dd�}
Wnty/t|||||dd�}
Ynwt||||�}|
j||
|dd�}|	D]}t||�qB|rN|St|�S)a7Copy an image from another LXD instance

    source :
        An alias or a fingerprint of the source.

    src_remote_addr :
        An URL to the source remote daemon

        Examples:
            https://mysourceserver.lan:8443

    src_cert :
        PEM Formatted SSL Certificate for the source

        Examples:
            ~/.config/lxc/client.crt

    src_key :
        PEM Formatted SSL Key for the source

        Examples:
            ~/.config/lxc/client.key

    src_verify_cert : True
        Wherever to verify the cert, this is by default True
        but in the most cases you want to set it off as LXD
        normally uses self-signed certificates.

    remote_addr :
        Address of the destination daemon

        Examples:
            https://mydestserver.lan:8443

    cert :
        PEM Formatted SSL Certificate for the destination

        Examples:
            ~/.config/lxc/client.crt

    key :
        PEM Formatted SSL Key for the destination

        Examples:
            ~/.config/lxc/client.key

    verify_cert : True
        Wherever to verify the cert, this is by default True
        but in the most cases you want to set it off as LXD
        normally uses self-signed certificates.

    aliases : []
        List of aliases to append to the copied image

    public : None
        Make this image public available, None = copy source

    auto_update : None
        Wherever to auto-update from the original source, None = copy source

    _raw : False
        Return the raw pylxd object or a dict of the destination image?

    CLI Examples:

    .. code-block:: bash

        salt '*' lxd.image_copy_lxd xenial/amd64 https://srv01:8443 ~/.config/lxc/client.crt ~/.config/lxc/client.key false https://srv02:8443 ~/.config/lxc/client.crt ~/.config/lxc/client.key false aliases="['xenial/amd64']"
    Nz/Trying to copy the image "%s" from "%s" to "%s"Trz)rrrE)	r7r8rrr
rC�copyrrX)rar�r�r�r�r@r3r-rArrrroZ	src_imager�Z
dest_imager^rrr�image_copy_lxdEs6T�

�
���rr�cCs>t|||||�}|jD]}|d|krdSq|�||�dS)a�Create an alias on the given image

    image :
        An image alias, a fingerprint or a image object

    alias :
        The alias to add

    description :
        Description of the alias

    remote_addr :
        An URL to a remote Server, you also have to give cert and key if
        you provide remote_addr and its a TCP Address!

        Examples:
            https://myserver.lan:8443
            /var/lib/mysocket.sock

    cert :
        PEM Formatted SSL Certificate.

        Examples:
            ~/.config/lxc/client.crt

    key :
        PEM Formatted SSL Key.

        Examples:
            ~/.config/lxc/client.key

    verify_cert : True
        Wherever to verify the cert, this is by default True
        but in the most cases you want to set it off as LXD
        normally uses self-signed certificates.

    CLI Examples:

    .. code-block:: bash

        salt '*' lxd.image_alias_add xenial/amd64 x "Short version of xenial/amd64"
    rOT)rr�	add_alias)r\r^r�r@r3r-rAZ
alias_inforrrr�s3
�rcCs:t|||||�}z|�|�WdStjjyYdSw)a�Delete an alias (this is currently not restricted to the image)

    image :
        An image alias, a fingerprint or a image object

    alias :
        The alias to delete

    remote_addr :
        An URL to a remote Server, you also have to give cert and key if
        you provide remote_addr and its a TCP Address!

        Examples:
            https://myserver.lan:8443
            /var/lib/mysocket.sock

    cert :
        PEM Formatted SSL Certificate.

        Examples:
            ~/.config/lxc/client.crt

    key :
        PEM Formatted SSL Key.

        Examples:
            ~/.config/lxc/client.key

    verify_cert : True
        Wherever to verify the cert, this is by default True
        but in the most cases you want to set it off as LXD
        normally uses self-signed certificates.

    CLI Examples:

    .. code-block:: bash

        salt '*' lxd.image_alias_add xenial/amd64 x "Short version of xenial/amd64"
    FT)rZdelete_aliasrr>rG)r\r^r@r3r-rArrr�image_alias_delete�s*��rcCsPt|||||dd�}|r|g}i}|D]}|�|jdd�|j��D�i�q|S)a�
    Get all snapshots for a container

    container :
        The name of the container to get.

    remote_addr :
        An URL to a remote server. The 'cert' and 'key' fields must also be
        provided if 'remote_addr' is defined.

        Examples:
            https://myserver.lan:8443
            /var/lib/mysocket.sock

    cert :
        PEM Formatted SSL Certificate.

        Examples:
            ~/.config/lxc/client.crt

    key :
        PEM Formatted SSL Key.

        Examples:
            ~/.config/lxc/client.key

    verify_cert : True
        Verify the ssl certificate.  Default: True

    CLI Examples:

    .. code-block:: bash

        salt '*' lxd.snapshots_all test-container
    TrzcSsg|]}d|ji�qSrNrNrPrrrrSbsz!snapshots_all.<locals>.<listcomp>)ry�updaterO�	snapshotsrV)rpr@r3r-rArU�ret�contrrr�
snapshots_all7s$�"r"cCsht|||||dd�}|st���d�}|j�|�t|��|�D]}|�d�|kr/d|iSq ddiS)a
    Create a snapshot for a container

    container :
        The name of the container to get.

    name :
        The name of the snapshot.

    remote_addr :
        An URL to a remote server. The 'cert' and 'key' fields must also be
        provided if 'remote_addr' is defined.

        Examples:
            https://myserver.lan:8443
            /var/lib/mysocket.sock

    cert :
        PEM Formatted SSL Certificate.

        Examples:
            ~/.config/lxc/client.crt

    key :
        PEM Formatted SSL Key.

        Examples:
            ~/.config/lxc/client.key

    verify_cert : True
        Verify the ssl certificate.  Default: True

    CLI Examples:

    .. code-block:: bash

        salt '*' lxd.snapshots_create test-container test-snapshot
    Trzz%Y%m%d%H%M%SrOF)ryr�now�strftimerrjr"ru)rprOr@r3r-rAr!rRrrr�snapshots_creategs)�r%cCs^t|||||dd�}z|j��D]}|j|kr|��WdSqWdStjjy.YdSw)a
    Delete a snapshot for a container

    container :
        The name of the container to get.

    name :
        The name of the snapshot.

    remote_addr :
        An URL to a remote server. The 'cert' and 'key' fields must also be
        provided if 'remote_addr' is defined.

        Examples:
            https://myserver.lan:8443
            /var/lib/mysocket.sock

    cert :
        PEM Formatted SSL Certificate.

        Examples:
            ~/.config/lxc/client.crt

    key :
        PEM Formatted SSL Key.

        Examples:
            ~/.config/lxc/client.key

    verify_cert : True
        Verify the ssl certificate.  Default: True

    CLI Examples:

    .. code-block:: bash

        salt '*' lxd.snapshots_delete test-container test-snapshot
    TrzF)ryrrVrOr{rr>rG)rprOr@r3r-rAr!�srrr�snapshots_delete�s)
����r'cCs t|||||dd�}|j�|�S)a
    Get information about snapshot for a container

    container :
        The name of the container to get.

    name :
        The name of the snapshot.

    remote_addr :
        An URL to a remote server. The 'cert' and 'key' fields must also be
        provided if 'remote_addr' is defined.

        Examples:
            https://myserver.lan:8443
            /var/lib/mysocket.sock

    cert :
        PEM Formatted SSL Certificate.

        Examples:
            ~/.config/lxc/client.crt

    key :
        PEM Formatted SSL Key.

        Examples:
            ~/.config/lxc/client.key

    verify_cert : True
        Verify the ssl certificate.  Default: True

    CLI Examples:

    .. code-block:: bash

        salt '*' lxd.snapshots_get test-container test-snapshot
    Trz)ryrru)rprOr@r3r-rArrr�
snapshots_get�s)r(cCs�t|t�r|rd|dvrd|dvrdd�|D�}ni}t|t�r&td��t|t�r/td��|durB|��D]
\}}t|�||<q7|dur\|D]}||��D]
\}}||||<qPqH||fS)	a
    normalize config input so returns can be put into mongodb, which doesn't like `.`

    This is not meant to be used on the commandline.

    CLI Examples:

    .. code-block:: bash

        salt '*' lxd.normalize_input_values config={} devices={}
    r-rr.cSsi|]	}|d|d�qS)r-r.r)rQ�drrrr�
sz*normalize_input_values.<locals>.<dictcomp>z3config can't be a string, validate your YAML input.z4devices can't be a string, validate your YAML input.N)rdrer&rrl)rbrnr�rrqrrrri
s*


���ric
Cs6i}|duri}ttttt|���tt|������}t|���}ttttt|j���tt|j������|_t|j���}i}|�	|�D]*}|�
d�sQ|�
d�rRqE|sdd�||j|�||<|j|=qEd�||j|�||<qE|�|�D]<}|�
d�s�|�
d�r�qu|||j|kr�|s�d�||||j|�||<|||j|<qud�||||j|�||<qu|�	|�D].}|�
d�s�|�
d�r�q�|s�d|�d	||�d
�||<|||j|<q�d�|||�||<q�|r�||d<|dur�i}t|j
���}	t|���}
i}|	�	|
�D]#}|d
k�r�q|�s d|�d
�||<|j
|=�qd|�d
�||<�q|j
��D]6\}}|d
k�r:�q/||v�rA�q/|||k�rd|�s\d|�d
�||<|||j
|<�q/d|�d
�||<�q/|
�	|	�D]&}|d
k�rt�qk|�s�d|�d
�||<|||j
|<�qkd|�d
�||<�qk|�r�||d<|S)a�Syncs the given config and devices with the object
    (a profile or a container)
    returns a changes dict with all changes made.

    obj :
        The object to sync with / or just test with.

    newconfig:
        The new config to check with the obj.

    newdevices:
        The new devices to check with the obj.

    test:
        Wherever to not change anything and give "Would change" message.
    Nz	volatile.zimage.z+Removed config key "{}", its value was "{}"z+Would remove config key "{} with value "{}"z3Changed config key "{}" to "{}", its value was "{}"z?Would change config key "{}" to "{}", its current value is "{}"zAdded config key "z" = "r"z Would add config key "{}" = "{}"rb�rootzRemoved device "zWould remove device "zChanged device "zWould change device "zAdded device "zWould add device "rn)rwre�ziprWr&�keys�valuesrgrb�
differencer9r�intersectionrnrl)
rHZ	newconfigZ
newdevices�testZchangesZcckZockZconfig_changesr�ZdkZndkZdevices_changesrrrr�sync_config_devices.
s� �$�
�


������
�


�
r1cCs�t||�}|dkr;|d}|dkr'd|vrtd��|ddkr'd|vr'td��|��D]
}|�d	�r5||=q+|||<nt|�||<t|�t|�S)
z�Sets the dict item key of the attr from obj.

    Basicaly it does getattr(obj, prop)[key] = value.


    For the disk device we added some checks to make
    device changes on the CLI saver.
    rnr]r�r;zpath must be given as parameterr2raz!source must be given as parameter�__)r�rr,r9r&rIrX)rH�propr-r.�attrr�r�rrrr��
s 
	
�
r�cCs*t||�}||vrtd|�d���||S)Nr4�' doesn't exists)r�r�rHr3r-r4rrrr��
s
r�cCs4t||�}||vrtd|�d���||=t|�dS)Nr4r5T)r�rrIr6rrrr��
s
r�cCstt|t�r+|}d}z
t|||||dd�}W|Sty*t|||||dd�}Y|Swt|d�s8td|�d���|S)NTrzrzInvalid image 'r4)rdr&rrr
r�)r\r@r3r-rArOrrrr�
s
���
rcCs0i}|j��D]}t||�rt||�||<q|S)z)Translates a plyxd model object to a dict)Z__attributes__r,r�r�)rHZ
marshalledr-rrrrXs
�rXr�c@seZdZdZdS)r�z0An exception raised when an object is not found.N)�__name__�
__module__�__qualname__�__doc__rrrrr�s)�	ContainercCs�i}|durt|t�rt|�}n
|�d�sd|��}||d<|dur(t|�|d<|dur2t|�|d<|jjj|jj	j
jd|i||d�}|jdkS)Nrz
X-LXD-modez	X-LXD-uidz	X-LXD-gidr;)�paramsr�headers��)
rdr�r�r9r&Z_clientZapirU�
_containerrOr�Zpostr~)�self�filepathrr�r�r�r=�responserrrr�(s




�
r�)r!NNNNNN)NNNT)T)FNNNT)NNNrFTNNNTF)NNNNTF)NNNNT)r�TNNNT)	FNNNTNNNN)r�NNNT)
FFNNNr�NNNT)FNNNNNNT)NNNNNNT)NNNTF)NNNTNFFF)NNNTNFr�F)TNNNF)r�NNNT)F)NNN)fr:�loggingr:rZsalt.utils.decorators.pathr�Zsalt.utils.filesZsalt.exceptionsrrZsalt.utils.versionsrrrZurllib3Zdisable_warningsr>ZInsecureRequestWarning�ImportError�	getLoggerr7r7Z
__docformat__rrhrrr6rr�Z
decoratorsr;�whichrrr,r/r0rCrIrJrZrrryr}r�r�r�r�r�r�r�r�r�r�r�r�rmr�r�r�r�r�r�r�r�r�r�r�rrrr	r
rr
rrrrrrr"r%r'r(rir1r�r�r�rrXZpylxd.exceptionsr�rGr�Zpylxd.containerr;Zpylxd.models.containerr�ZFilesManagerrrrr�<module>s�!�
�

�S"
k
<
�A
�+
�
9&
�
.
='
�
-
%
%'
�l
�-
�4
�+
�-
�9
�.
�g
�p
�Q
�5
�Q
�
8,
�3
�9
�0
�2
�<
�4
�4
�9
�
80
�W
�S
�]
�
�>
�
91
�7
�7
�2
)#

�
�