HEX
Server: Apache
System: Linux server2.voipitup.com.au 4.18.0-553.104.1.lve.el8.x86_64 #1 SMP Tue Feb 10 20:07:30 UTC 2026 x86_64
User: posscale (1027)
PHP: 8.2.29
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/__pycache__/nspawn.cpython-310.pyc
o

�N�g���@sdZddlZddlZddlZddlZddlZddlZddlZddlZddl	Z
ddlZ
ddlZ
ddl
Z
ddlZ
ddlmZmZe�e�ZddiZdZdZdZd	Zd
d�Zdd
�Zdd�Zdidd�Zdd�Zdd�Zdd�Z dd�Z!dd�Z"dd�Z#d d!�Z$d"d#�Z%d$d%�Z&djd'd(�Z'e				)		&			dkd*d+��Z(ed,d-��Z)		)		)	&			dld.d/�Z*		)		)	&			dld0d1�Z+		)		)	&			dld2d3�Z,		)		)	&			dld4d5�Z-		)		)	&			dld6d7�Z.dmd8d9�Z/d:d;�Z0		)	)								dnd<d=�Z1d>d?�Z2d@dA�Z3e
j4j�5e3d�Z6dBdC�Z7dDdE�Z8edFdG��Z9dHdI�Z:edJdK��Z;edLdM��Z<edNdO��Z=edodPdQ��Z>dRdS�Z?dTdU�Z@dVdW�ZAedodXdY��ZBedodZd[��ZCe
j4j�5eCd\�ZDedpd]d^��ZEe
j4j�5eEd_�ZFd`da�ZGdodbdc�ZHdoddde�ZIdfdg�ZJe
j4j�5eJdh�ZKdS)qau
Manage nspawn containers

.. versionadded:: 2015.8.0

`systemd-nspawn(1)`__ is a tool used to manage lightweight namespace
containers. This execution module provides several functions to help manage
these containers.

.. __: http://www.freedesktop.org/software/systemd/man/systemd-nspawn.html

Minions running systemd >= 219 will place new containers in
``/var/lib/machines``, while those running systemd < 219 will place them in
``/var/lib/container``.

.. note:

    ``nsenter(1)`` is required to run commands within containers. It should
    already be present on any systemd host, as part of the **util-linux**
    package.
�N)�CommandExecutionError�SaltInvocationError�list_�listZnspawnz/nspawn.initial_seedzF/etc/systemd/system/multi-user.target.wants/systemd-nspawn@{0}.serviceZnsentercCs@tddkrtjj�t�rtjj��durt�d�dSt	SdS)zA
    Only work on systems that have been booted with systemd
    Zkernel�LinuxNz+nspawn: Unable to determine systemd version)FzdThe nspawn execution module failed to load: only work on systems that have been booted with systemd.)
�
__grains__�salt�utils�systemdZbooted�__context__�version�log�error�__virtualname__�rr�G/opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/nspawn.py�__virtual__2s
�rcCstjj�t�S)z�
    Returns __context__.get('systemd.version', 0), avoiding duplication of the
    call to dict.get and making it easier to change how we handle this context
    var in the future
    )rr	r
rrrrrr�_sd_versionBsrcst����fdd��}|S)z>
    Decorator to ensure that the named container exists.
    cs>t|�std|�d����|g|�Ritjjjdi|����S)N�Container 'z' does not existr)�existsrrr	�args�clean_kwargs)�namer�kwargs��wrappedrr�check_existsPs&z$_ensure_exists.<locals>.check_exists)�	functools�wraps)rrrrr�_ensure_existsKsr�FcsJt�dkr|r�fdd�dD�Stj�d��Stj�d��}|r#|gS|S)zs
    Return the container root directory. Starting with systemd 219, new
    images go into /var/lib/machines.
    ��csg|]	}tj�|���qSr)�os�path�join��.0�x�rrr�
<listcomp>`s��z_root.<locals>.<listcomp>)�/var/lib/machines�/var/lib/containerr*r+)rr"r#r$)rZ	all_roots�retrr(r�_rootYs

�r-c
Cspt|�}tj�|�rtjjjtd<t	d|�d���zt�
|�|WSty7}z
t	d|�d|����d}~ww)z+
    Make the container root directory
    �retcode�
Container z already existsz(Unable to make container root directory z: N)r-r"r#rr�defaults�	exitcodes�SALT_BUILD_FAILrr�makedirs�OSError)rr#�excrrr�_make_container_rootns
���r6c
Cslztjjjtd<t�|�Wnty-}z|jtj	kr#t
d|����WYd}~nd}~wwt
d|�d���)Nr.z%Unable to cleanup container root dir r/z failed to build)rr0r1r2r�shutil�rmtreer4�errno�ENOENTr)�dstrr5rrr�
_build_failed�s���r<cKsTtjj�d�std��t|�}d|�d�}td|dd�}|dd	kr(t||�|S)
z+
    Bootstrap an Arch Linux container
    ZpacstrapzBpacstrap not found, is the arch-install-scripts package installed?zpacstrap -c -d z base�cmd.run_allF��python_shellr.r)rr	r#�whichrr6�__salt__r<)rrr;�cmdr,rrr�_bootstrap_arch�s�
rCcKs�|�dd�}|std��dkrtd}nd}gd�}||vr&td�|���t|�}d	|�d
|��}td|dd�}|d
dkrEt||�|S)z,
    Bootstrap a Debian Linux container
    rFr"Zdebian�	osreleaseZstable)	ZhammZslinkZpotatoZwoodyZsargeZetchZlennyZsqueezeZwheezyzRUnsupported Debian version "{}". Only "stable" or "jessie" and newer are supported�debootstrap --arch=amd64 � r=r>r.r)�getr�lowerr�formatr6rAr<)rrrZrelease_blacklistr;rBr,rrr�_bootstrap_debian�s"
�
rJcKsnt|�}|�dd�std��dkrtd}nd}nd}d�||�}td|dd	�}|d
dkr5t||�|S)z&
    Bootstrap a Fedora container
    rFr"ZfedorarDZ21z�yum -y --releasever={} --nogpg --installroot={} --disablerepo="*" --enablerepo=fedora install systemd passwd yum fedora-release vim-minimalr=r>r.r)r6rGrrHrIrAr<)rrr;rrBr,rrr�_bootstrap_fedora�s
�
rKcKsp|�dd�}|std��dkrtd}nd}t|�}d|�d|��}td	|dd
�}|ddkr6t||�|S)
z,
    Bootstrap a Ubuntu Linux container
    rFr"ZubuntuZ
oscodenameZxenialrErFr=r>r.r)rGrrHr6rAr<)rrrr;rBr,rrr�_bootstrap_ubuntu�s

rLcCs0dd�tD�D]}t�d|�t�|d�qdS)z4
    Clear any lxc variables set in __context__
    cSsg|]	}|�d�r|�qS)znspawn.)�
startswithr%rrrr)�sz"_clear_context.<locals>.<listcomp>zClearing __context__['%s']N)rr
Ztrace�pop)�varrrr�_clear_context�s�rPcCst|�dkrdSt|�S)z<
    Raise an exception if the container does not exist
    �runningT)�state�startr(rrr�_ensure_running�srTc	Cszzt|�}Wntytd|�d���wzt�}t�d|�Wn
ttfy.td��w||kr;td�||���dS)z`
    Raises an exception if the systemd version is not greater than the
    passed version.
    zInvalid version '�'znspawn: detected systemd %sz%nspawn: Unable to get systemd versionz<This function requires systemd >= {} (Detected version: {}).N)�int�
ValueErrorrrr
�debug�
IndexErrorrI)rZ	installedrrr�_ensure_systemd�s$�����rZrXcCs"d}td|�d|��|||d�S)z+
    Helper function to run machinectl
    z!machinectl --no-legend --no-pagerr=rF)�output_loglevel�ignore_retcode�use_vt)rA)rBr[r\r]�prefixrrr�_machinectls�r_TcCs�t|�}d}z$td||tt|||||||	|
d�}
W|r*|dkr*t|�dkr*t|�n|r<|dkr=t|�dkr>t|�wwww|dvrE|
S|
|S)z/
    Common logic for nspawn.run functions
    Nzcontainer_resource.run)
�container_type�exec_driver�output�no_start�stdinr?r[r\r]�keep_env�stopped)N�all)rRrAr�EXEC_DRIVER�stop)rrBrbrcrdr?�preserve_stater[r\r]re�
orig_stater5r,rrr�_runs4���
�rlc
CsFz
tt|��d��WSttfy"}z
td|�d|����d}~ww)z�
    Returns the PID of a container

    name
        Container name

    CLI Example:

    .. code-block:: bash

        salt myminion nspawn.pid arch1
    �PIDz!Unable to get PID for container '�': N)rV�inforG�	TypeErrorrWr�rr5rrr�pidMs��rrc


C�t||d||||||||	d�S)aL
    Run :mod:`cmd.run <salt.modules.cmdmod.run>` within a container

    name
        Name of the container in which to run the command

    cmd
        Command to run

    no_start : False
        If the container is not running, don't start it

    preserve_state : True
        After running the command, return the container to its previous state

    stdin : None
        Standard input to be used for the command

    output_loglevel : debug
        Level at which to log the output from the command. Set to ``quiet`` to
        suppress logging.

    use_vt : False
        Use SaltStack's utils.vt to stream output to console.

    keep_env : None
        If not passed, only a sane default PATH environment variable will be
        set. If ``True``, all environment variables from the container's host
        will be kept. Otherwise, a comma-separated list (or Python list) of
        environment variable names can be passed, and those environment
        variables will be kept.

    CLI Example:

    .. code-block:: bash

        salt myminion nspawn.run mycontainer 'ifconfig -a'
    N�	rbrcrjrdr?r[r]r\re�rl�
rrBrcrjrdr?r[r]r\rerrr�runas2�rwc


Crs)a�
    Run :mod:`cmd.run_stdout <salt.modules.cmdmod.run_stdout>` within a container

    name
        Name of the container in which to run the command

    cmd
        Command to run

    no_start : False
        If the container is not running, don't start it

    preserve_state : True
        After running the command, return the container to its previous state

    stdin : None
        Standard input to be used for the command

    output_loglevel : debug
        Level at which to log the output from the command. Set to ``quiet`` to
        suppress logging.

    use_vt : False
        Use SaltStack's utils.vt to stream output to console. Assumes
        ``output=all``.

    keep_env : None
        If not passed, only a sane default PATH environment variable will be
        set. If ``True``, all environment variables from the container's host
        will be kept. Otherwise, a comma-separated list (or Python list) of
        environment variable names can be passed, and those environment
        variables will be kept.

    CLI Example:

    .. code-block:: bash

        salt myminion nspawn.run_stdout mycontainer 'ifconfig -a'
    �stdoutrtrurvrrr�
run_stdout��3�ryc


Crs)a�
    Run :mod:`cmd.run_stderr <salt.modules.cmdmod.run_stderr>` within a container

    name
        Name of the container in which to run the command

    cmd
        Command to run

    no_start : False
        If the container is not running, don't start it

    preserve_state : True
        After running the command, return the container to its previous state

    stdin : None
        Standard input to be used for the command

    output_loglevel : debug
        Level at which to log the output from the command. Set to ``quiet`` to
        suppress logging.

    use_vt : False
        Use SaltStack's utils.vt to stream output to console. Assumes
        ``output=all``.

    keep_env : None
        If not passed, only a sane default PATH environment variable will be
        set. If ``True``, all environment variables from the container's host
        will be kept. Otherwise, a comma-separated list (or Python list) of
        environment variable names can be passed, and those environment
        variables will be kept.

    CLI Example:

    .. code-block:: bash

        salt myminion nspawn.run_stderr mycontainer 'ip addr show'
    �stderrrtrurvrrr�
run_stderr�rzr|c


Crs)ay
    Run :mod:`cmd.retcode <salt.modules.cmdmod.retcode>` within a container

    name
        Name of the container in which to run the command

    cmd
        Command to run

    no_start : False
        If the container is not running, don't start it

    preserve_state : True
        After running the command, return the container to its previous state

    stdin : None
        Standard input to be used for the command

    output_loglevel : debug
        Level at which to log the output from the command. Set to ``quiet`` to
        suppress logging.

    use_vt : False
        Use SaltStack's utils.vt to stream output to console. Assumes
        ``output=all``.

    keep_env : None
        If not passed, only a sane default PATH environment variable will be
        set. If ``True``, all environment variables from the container's host
        will be kept. Otherwise, a comma-separated list (or Python list) of
        environment variable names can be passed, and those environment
        variables will be kept.

    CLI Example:

    .. code-block:: bash

        salt myminion nspawn.retcode mycontainer 'ip addr show'
    r.rtrurvrrrr.&rzr.c


Crs)a?
    Run :mod:`cmd.run_all <salt.modules.cmdmod.run_all>` within a container

    .. note::

        While the command is run within the container, it is initiated from the
        host. Therefore, the PID in the return dict is from the host, not from
        the container.

    name
        Name of the container in which to run the command

    cmd
        Command to run

    no_start : False
        If the container is not running, don't start it

    preserve_state : True
        After running the command, return the container to its previous state

    stdin : None
        Standard input to be used for the command

    output_loglevel : debug
        Level at which to log the output from the command. Set to ``quiet`` to
        suppress logging.

    use_vt : False
        Use SaltStack's utils.vt to stream output to console. Assumes
        ``output=all``.

    keep_env : None
        If not passed, only a sane default PATH environment variable will be
        set. If ``True``, all environment variables from the container's host
        will be kept. Otherwise, a comma-separated list (or Python list) of
        environment variable names can be passed, and those environment
        variables will be kept.

    CLI Example:

    .. code-block:: bash

        salt myminion nspawn.run_all mycontainer 'ip addr show'
    rgrtrurvrrr�run_allhs9�r}cCsV|std��}t�d|�z
t�d|��||d�WSty*td|�d���w)a3
    Bootstrap a container from package servers, if dist is None the os the
    minion is running as will be created, otherwise the needed bootstrapping
    tools will need to be available on the host.

    CLI Example:

    .. code-block:: bash

        salt myminion nspawn.bootstrap_container <name>
    r"z6nspawn.bootstrap: no dist provided, defaulting to '%s'Z_bootstrap_)rzUnsupported distribution "�")rrHr
rX�globals�KeyErrorr)r�distrrrr�bootstrap_container�s�r�cCsBd}t|d�}|rt|d�}d|vrd}|St|d�|Sd}|S)Nrzcommand -v salt-minionzps auxzsalt-minion�z*salt-call --local service.stop salt-minion)r.ry)rr,Z
has_minionZ	processesrrr�_needs_install�s


��r�cCs�|	durzt�|	�Wntyt�d�Ynwt|�}|s"dS|
r-d|
vr,|
d7}
nd}
|s3d}t|�}
|
s;|
S|sBt|�}nd}t|dt�d	��d
k}t�	�}|r[|s[d}|Sd}t
d||||||d
�}|sp|sp|�r|�rt
d�}d|��}t|d|��dd�t
d|d�}tj
�d|�}d|��d|��fD]}t||�r�t�d||�dSq�t|||�d�dd�t||dtj
�|d��t||dtj
�|d��t||dtj
�|d��|
�|�}
d �||
�d	d!�|�}t�d"||�t||d#dd$�d
k}n:d}n7tj�|d�}|d%}t||dd&�t||dtj
�|d��t||dtj
�|d��t|d'dd�d}t�|�|
d(k�rMt|�|�r[t|d)t�d	�dd�|S)*a(
    Bootstrap a container from package servers, if dist is None the os the
    minion is running as will be created, otherwise the needed bootstrapping
    tools will need to be available on the host.

    CLI Example:

    .. code-block:: bash

        salt '*' nspawn.bootstrap_salt arch1
    N�z{0}z -c {0}z-c {0}�shTz	test -e 'rUrFz
seed.mkconfig)�tmpZid_�approve_key�pub_key�priv_keyztest.random_hashz/tmp/.c_zinstall -m 0700 -d r>zconfig.gather_bootstrap_script)�	bootstrapz/tmpz	mkdir -p z
chmod 700 ztmpdir %s creation failed (%s)z
/bootstrap.sh)r3�configZminionZprivkeyz
minion.pemZpubkeyz
minion.pubz{0} {2}/bootstrap.sh {1}z''z Running %s in LXC container '%s'ro)r[r]�pki_dirz/etc/salt/minionz,salt-call --local service.enable salt-minionrfztouch ')�time�sleeprprorTr�r.�SEED_MARKER�tempfileZmkdtemprArwr"r#r$ryr
r�copy_torI�replacerr��
minion_configr7r8ri)rr�r��installr�r�Z
bootstrap_urlZ
force_installZunconditional_installZbootstrap_delayZbootstrap_argsZbootstrap_shell�c_inforkZ
needs_installZseededr�r,Z	cfg_filesZrstrZ	configdirZbs_Zdest_dirrBr�r�rrr�bootstrap_salt�s���
=��

��
���
��

r�c	Cs�g}t�dkr(td�d��D]}z|�|��d�Wqty%Yqw|St�}zt�|�D]}tj	�
tj	�||��rC|�|�q1W|StyPY|Sw)zx
    Lists all nspawn containers

    CLI Example:

    .. code-block:: bash

        salt myminion nspawn.list_all
    r!zlist-imagesrxr)
rr_�
splitlines�append�splitrYr-r"�listdirr#�isdirr$r4)r,�line�rootdir�dirnamerrr�list_allSs*

�
�
����r�c	CsJg}td�d��D]}z|�|��d�Wq
ty Yq
wt|�S)z�
    Lists running nspawn containers

    .. note::

        ``nspawn.list`` also works to list running containers

    CLI Example:

    .. code-block:: bash

        salt myminion nspawn.list_running
        salt myminion nspawn.list
    rrxr)r_r�r�r�rY�sorted)r,r�rrr�list_runningos�r�cCsttt��tt���S)z�
    Lists stopped nspawn containers

    CLI Example:

    .. code-block:: bash

        salt myminion nspawn.list_stopped
    )r��setr�r�rrrr�list_stopped�s
r�cCs0d|��}|tvr
t|S|t�vt|<t|S)z�
    Returns true if the named container exists

    CLI Example:

    .. code-block:: bash

        salt myminion nspawn.exists <name>
    znspawn.exists.)rr�)rZ
contextkeyrrrr�s


rcCs>zd|�d�}t|dd�d�d�dWStyYdSw)	z�
    Return state of container (running or stopped)

    CLI Example:

    .. code-block:: bash

        salt myminion nspawn.state <name>
    zshow z --property=StateT)r\rx�=���rf)r_r�rY�rrBrrrrR�s�rRc	Ks�tjjjdi|��}|�dd�}|rtjj�|�|s t|�n	|t�vr)t|�t	d|���}|ddkr>t
d|�d���dd	dd
d�}i}t�d�}t�d
�}|d�
�}d}	d}
t|�D]h\}}|�|�}
|
r�|
��\}}|�||�}|dur{q_|d	kr�z|��d}Wn	ty�Ynw|}
|	r�d}	|||<q_|
dur�q_|�||�r�|S|	r�||
�||���q_||||��g||
<d}	q_|S)a*
    Return info about a container

    .. note::

        The container must be running for ``machinectl`` to gather information
        about it. If the container is stopped, then this function will start
        it.

    start : False
        If ``True``, then the container will be started to retrieve the info. A
        ``Started`` key will be in the return data if the container was
        started.

    CLI Example:

    .. code-block:: bash

        salt myminion nspawn.info arch1
        salt myminion nspawn.info arch1 force_start=False
    rSFzstatus r.rz"Unable to get info for container 'rUzNetwork Interfacermz
Running Since)ZIfaceZLeaderZServiceZSincez^\s+([A-Za-z]+): (.+)$z[|`]rxNTr)rr	rrrN�invalid_kwargsrTr�rSr_r�re�compiler��	enumerate�match�groupsrGr�rY�searchr��strip)rrZstart_r�Zkey_name_mapr,Zkv_pairZtree�linesZ	multilineZcur_key�idxr�r��key�valrrrro�sb

�


�
�rocC�4d|��}td|dd�dkrtjjjtd<dSdS)z�
    Set the named container to be launched at boot

    CLI Example:

    .. code-block:: bash

        salt myminion nspawn.enable <name>
    z systemctl enable systemd-nspawn@�cmd.retcodeFr>rr.T�rArr0r1�EX_UNAVAILABLErr�rrr�enable
�

r�cCr�)z�
    Set the named container to *not* be launched at boot

    CLI Example:

    .. code-block:: bash

        salt myminion nspawn.enable <name>
    z!systemctl disable systemd-nspawn@r�Fr>rr.Tr�r�rrr�disabler�r�cCsVt�dkr
td|���}n
d|��}td|dd�}|ddkr)tjjjtd<dSd	S)
zz
    Start the named container

    CLI Example:

    .. code-block:: bash

        salt myminion nspawn.start <name>
    r!zstart �systemctl start systemd-nspawn@r=Fr>r.rT�rr_rArr0r1r�r)rr,rBrrrrS1s

rScCsht�dkr|r
d}nd}t|�d|���}n
d|��}td|dd�}|d	d
kr2tjjjtd	<dSdS)zq
    This is a compatibility function which provides the logic for
    nspawn.poweroff and nspawn.terminate.
    r!�	terminate�poweroffrF�systemctl stop systemd-nspawn@r=Fr>r.rTr�)r�kill�actionr,rBrrrriIs

ricC�t|dd�S)aQ
    Issue a clean shutdown to the container.  Equivalent to running
    ``machinectl poweroff`` on the named container.

    For convenience, running ``nspawn.stop``(as shown in the CLI examples
    below) is equivalent to running ``nspawn.poweroff``.

    .. note::

        ``machinectl poweroff`` is only supported in systemd >= 219. On earlier
        systemd versions, running this function will simply issue a clean
        shutdown via ``systemctl``.

    CLI Examples:

    .. code-block:: bash

        salt myminion nspawn.poweroff arch1
        salt myminion nspawn.stop arch1
    F�r��rir(rrrr�_sr�cCr�)a�
    Kill all processes in the container without issuing a clean shutdown.
    Equivalent to running ``machinectl terminate`` on the named container.

    For convenience, running ``nspawn.stop`` and passing ``kill=True`` (as
    shown in the CLI examples below) is equivalent to running
    ``nspawn.terminate``.

    .. note::

        ``machinectl terminate`` is only supported in systemd >= 219. On
        earlier systemd versions, running this function will simply issue a
        clean shutdown via ``systemctl``.

    CLI Examples:

    .. code-block:: bash

        salt myminion nspawn.terminate arch1
        salt myminion nspawn.stop arch1 kill=True
    Tr�r�r(rrrr�wsr�cCst|�S)zL
    This is a compatibility function which simply calls nspawn.reboot.
    )�rebootr(rrr�restart�sr�cCs�t�dkrt|�dkrtd|���}n-t|�Sd|��}td|dd�}|dd	kr3tjjjt	d<dSd
|��}td|dd�}|dd	krOtjjjt	d<dSdS)ad
    Reboot the container by sending a SIGINT to its init process. Equivalent
    to running ``machinectl reboot`` on the named container.

    For convenience, running ``nspawn.restart`` (as shown in the CLI examples
    below) is equivalent to running ``nspawn.reboot``.

    .. note::

        ``machinectl reboot`` is only supported in systemd >= 219. On earlier
        systemd versions, running this function will instead restart the
        container via ``systemctl``.

    CLI Examples:

    .. code-block:: bash

        salt myminion nspawn.reboot arch1
        salt myminion nspawn.restart arch1
    r!rQzreboot r�r=Fr>r.rr�T)
rrRr_rSrArr0r1r�r)rr�r,rBrrrr��s


r�c
Cs�|st|�dkrtd|�d���dd�}t�dkr6td|���}|dd	kr4tjjjtd<|||d
�dSzt	�
tj�
t�|��WdSty\}z|||�WYd}~dSd}~ww)
a�
    Remove the named container

    .. warning::

        This function will remove all data associated with the container. It
        will not, however, remove the btrfs subvolumes created by pulling
        container images (:mod:`nspawn.pull_raw
        <salt.modules.nspawn.pull_raw>`, :mod:`nspawn.pull_tar
        <salt.modules.nspawn.pull_tar>`, :mod:`nspawn.pull_dkr
        <salt.modules.nspawn.pull_dkr>`).

    stop : False
        If ``True``, the container will be destroyed even if it is
        running/frozen.

    CLI Examples:

    .. code-block:: bash

        salt '*' nspawn.remove foo
        salt '*' nspawn.remove foo stop=True
    rfrz' is not stoppedcSstd|�d|����)NzUnable to remove container 'rn)rrqrrr�_failed_remove�szremove.<locals>._failed_remover!zremove r.rr{NT)rRrrr_rr0r1r�rr7r8r"r#r$r-r4)rrir�r,r5rrr�remove�s"
����r��destroyc	Csx|}z|�d�rtd|�}|std|����|}Wnty(td|����wt�dkr/	td|||tt||d�S)a^
    Copy a file from the host into a container

    name
        Container name

    source
        File to be copied to the container

    dest
        Destination on the container. Must be an absolute path.

    overwrite : False
        Unless this option is set to ``True``, then if a file exists at the
        location specified by the ``dest`` argument, an error will be raised.

    makedirs : False

        Create the parent directory on the container if it does not already
        exist.

    CLI Example:

    .. code-block:: bash

        salt 'minion' nspawn.copy_to /tmp/foo /root/foo
    zsalt://z
cp.cache_filezUnable to cache zInvalid source file r!zcontainer_resource.copy_to)r`ra�	overwriter3)rMrAr�AttributeErrorrrrrh)r�source�destr�r3r#Z
cached_sourcerrrr��s,
��
�r��cpcs�td�t|�rtd|�d���|dvrd�n|dkrd�ntd|�d	���tjjjdi|��}�fd
d�tjjjdi|����D�}|rLtjj�|�g}|dvr�|�	dd
�}|s`|�
d�n9dd�}z|��}Wntyv|�Yn#w|dvr~|�|�
d|���n|dkr�d|vr�|�
d�
|d��d�
|d�|�||�}t|dd�}	|	ddkr�d}
|	dr�|
d�
|	d�7}
t|
��dS) z5
    Common logic for machinectl pull-* commands
    r!rz' already exists)�raw�tar��verify�dkr��indexzUnsupported image type 'rUcsi|]\}}|�vr||�qSrr)r&r'�y�Zvalid_kwargsrr�
<dictcomp>Es
�z_pull_image.<locals>.<dictcomp>r�Fz--verify=nocSstd��)Nz:'verify' must be one of the following: signature, checksum)rrrrr�_bad_verifyVs�z _pull_image.<locals>._bad_verify)Z	signatureZchecksumz	--verify=r�z--dkr-index-url={}zpull-{} {} {} {}rFT)r]r.rzMError occurred pulling image. Stderr from the pull command (if any) follows: r{z

{}Nr)rZrrrr	rr�itemsr�rGr�rHr�rIr$r_r)Z	pull_typeZimagerrZ
bad_kwargsZ	pull_optsr�r�rB�result�msgrr�r�_pull_image6sP
�
��r�cC�td|||d�S)a�
    Execute a ``machinectl pull-raw`` to download a .qcow2 or raw disk image,
    and add it to /var/lib/machines as a new container.

    .. note::

        **Requires systemd >= 219**

    url
        URL from which to download the container

    name
        Name for the new container

    verify : False
        Perform signature or checksum verification on the container. See the
        ``machinectl(1)`` man page (section titled "Image Transfer Commands")
        for more information on requirements for image verification. To perform
        signature verification, use ``verify=signature``. For checksum
        verification, use ``verify=checksum``. By default, no verification will
        be performed.

    CLI Examples:

    .. code-block:: bash

        salt myminion nspawn.pull_raw http://ftp.halifax.rwth-aachen.de/fedora/linux/releases/21/Cloud/Images/x86_64/Fedora-Cloud-Base-20141203-21.x86_64.raw.xz fedora21
    r�r��r���urlrr�rrr�pull_raww�r�cCr�)ae
    Execute a ``machinectl pull-raw`` to download a .tar container image,
    and add it to /var/lib/machines as a new container.

    .. note::

        **Requires systemd >= 219**

    url
        URL from which to download the container

    name
        Name for the new container

    verify : False
        Perform signature or checksum verification on the container. See the
        ``machinectl(1)`` man page (section titled "Image Transfer Commands")
        for more information on requirements for image verification. To perform
        signature verification, use ``verify=signature``. For checksum
        verification, use ``verify=checksum``. By default, no verification will
        be performed.

    CLI Examples:

    .. code-block:: bash

        salt myminion nspawn.pull_tar http://foo.domain.tld/containers/archlinux-2015.02.01.tar.gz arch2
    r�r�r�r�rrr�pull_tar�r�r�cCr�)au
    Execute a ``machinectl pull-dkr`` to download a docker image and add it to
    /var/lib/machines as a new container.

    .. note::

        **Requires systemd >= 219**

    url
        URL from which to download the container

    name
        Name for the new container

    index
        URL of the Docker index server from which to pull (must be an
        ``http://`` or ``https://`` URL).

    CLI Examples:

    .. code-block:: bash

        salt myminion nspawn.pull_dkr centos/centos6 cent6 index=https://get.docker.com
        salt myminion nspawn.pull_docker centos/centos6 cent6 index=https://get.docker.com
    r�r�r�)r�rr�rrr�pull_dkr�sr��pull_docker)r F)rXFF)	NFNTFrXFFN)FTNTrXFFN)NN)NTTNNNFFNNN)F)FF)L�__doc__r9r�loggingr"r�r7r�r�Zsalt.defaults.exitcodesrZsalt.utils.argsZsalt.utils.functoolsZsalt.utils.pathZsalt.utils.systemdZsalt.exceptionsrr�	getLogger�__name__r
Z__func_alias__rr�ZWANTrhrrrr-r6r<rCrJrKrLrPrTrZr_rlrrrwryr|r.r}r�r�r�r�r�r	Zalias_functionrr�rrRror�r�rSrir�r�r�r�r�r�r�r�r�r�r�r�r�rrrr�<module>s
�	

$	


�-

�D
�E
�E
�E
�
H
�|

Q


2,4
A