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__/vagrant.cpython-310.pyc
o

�N�g�P�@s&dZddlZddlZddlZddlZddlZddlmZddl	m
Z
mZe�e
�ZdZdZdd�Zd	d
�Zdd�Zd
d�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zd5dd�Zd5dd�Zd6d d!�Z				"		d7d#d$�Zd%d&�Zd'd(�Z d)d*�Z!d+d,�Z"d-d.�Z#d8d/d0�Z$d1d2�Z%d9d3d4�Z&dS):ab
Work with virtual machines managed by Vagrant.

.. versionadded:: 2018.3.0

Mapping between a Salt node id and the Vagrant machine name
(and the path to the Vagrantfile where it is defined)
is stored in a Salt sdb database on the Vagrant host (minion) machine.
In order to use this module, sdb must be configured. An SQLite
database is the recommended storage method.  The URI used for
the sdb lookup is "sdb://vagrant_sdb_data".

requirements:
   - the VM host machine must have salt-minion, Vagrant and a vm provider installed.
   - the VM host must have a valid definition for `sdb://vagrant_sdb_data`

    Configuration example:

    .. code-block:: yaml

        # file /etc/salt/minion.d/vagrant_sdb.conf
        vagrant_sdb_data:
          driver: sqlite3
          database: /var/cache/salt/vagrant.sqlite
          table: sdb
          create_table: True

�N)�	ipaddress)�CommandExecutionError�SaltInvocationError�vagrantzsdb://vagrant_sdb_data/cCstjj�d�durdStS)z*
    run Vagrant commands if possible
    rN)FzAThe vagrant module could not be loaded: vagrant command not found)�salt�utils�path�which�__virtualname__�rr�H/opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/vagrant.py�__virtual__.sr
cCst�|��S)z�
    returns string used to fetch data for "key" from the sdb store.

    Salt node id's are used as the key for vm_ dicts.

    )�VAGRANT_SDB_URL)�keyrrr�_build_sdb_uri:srcCs|�dtj�|���}t|�S)z�
    returns string used to fetch id names from the sdb store.

    the cwd and machine name are concatenated with '?' which should
    never collide with a Salt node id -- which is important since we
    will be storing both in the same table.
    �?)�osr�abspathr)�machine�cwdrrrr�_build_machine_uriDsrcCsFtdt|�|t�|dr!tdt|d|�dd��|t�dSdS)z'store the vm_ information keyed by name�sdb.sdb_setrr�.N)�	__utils__r�__opts__r�get��name�vm_rrr�_update_vm_infoPs��rcCsPztdt|�t�}Wntytd��w|dusd|vr&td|����|S)z�
    get the information for a VM.

    :param name: salt_id name
    :return: dictionary of {'machine': x, 'cwd': y, ...}.
    �sdb.sdb_getz8Probable sdb driver not found. Check your configuration.Nr�'No Vagrant machine defined for Salt_id )rrr�KeyErrorrrrrr�get_vm_info[s��r#cCstdt||�t�}|S)z�
    returns the salt_id name of the Vagrant VM

    :param machine: the Vagrant machine name
    :param cwd: the path to Vagrantfile
    :return: salt_id name
    r )rrr)rrrrrr�get_machine_idmsr$cCs�z0t|�}|dr/t|d|�dd��}z	td|t�Wnty.td|dt�YnwWn	ty9Ynwt|�}z
td|t�WdStyZtd|it�YdStycYdSw)z�
    erase the information for a VM the we are destroying.

    some sdb drivers (such as the SQLite driver we expect to use)
    do not have a `delete` method, so if the delete fails, we have
    to replace the with a blank entry.
    rrrzsdb.sdb_deleterN)r#rrrrr"�	Exceptionr)rrrZurirrr�_erase_vm_infoys*����r&cCs�|d}t�d|p
d�d|��}td||�d�|�d�dd	�}i}|�d
�D]}|����}t|�dkr>|d||d
<q(t�dt|��|S)z�
    get the information for ssh communication from the new VM

    :param vm_: the VM's info as we have it now
    :return: dictionary of ssh stuff
    rz'requesting vagrant ssh-config for VM %s�	(default)zvagrant ssh-config �	cmd.shell�runasrT)r)rZignore_retcode�
��rz
ssh_config=%s)	�log�info�__salt__r�split�strip�len�debug�repr)rr�cmd�reply�
ssh_config�line�tokensrrr�_vagrant_ssh_config�s
��r:cCsd}td|�S)z�
    Return the version of Vagrant on the minion

    CLI Example:

    .. code-block:: bash

        salt '*' vagrant.version
    z
vagrant -vr()r/)r5rrr�version�s
r;c	
Cs�g}d}td|�}t�d|�|�d�D]1}|����}z	t|dd�}Wnttfy1Yqw|d}|d}t||�}|rF|�	|�q|S)	a�
    Return a list of the salt_id names of all available Vagrant VMs on
    this host without regard to the path where they are defined.

    CLI Example:

    .. code-block:: bash

        salt '*' vagrant.list_domains --log-level=info

    The log shows information about all known Vagrant environments
    on this machine. This data is cached and may not be completely
    up-to-date.
    zvagrant global-statusr(�--->
%sr*r�r,���)
r/r-r3r0r1�int�
ValueError�
IndexErrorr$�append)	�vmsr5r6r8r9�_rrrrrr�list_domains�s$�

�rEcCsjg}d}td||d�}t�d|�|�d�D]}|����}t|�dkr2|ddkr2|�|d�q|S)	z�
    Return a list of machine names for active virtual machine on the host,
    which are defined in the Vagrantfile at the indicated path.

    CLI Example:

    .. code-block:: bash

        salt '*' vagrant.list_active_vms  cwd=/projects/project_1
    �vagrant statusr(�rr<r*r,�runningr)r/r-r.r0r1r2rB�rrCr5r6r8r9rrr�list_active_vms�s�rJcCsxg}d}td||d�}t�d|�|�d�D]"}|����}t|�dkr9|d�d�r9|dd	kr9|�|d
�q|S)z�
    Return a list of machine names for inactive virtual machine on the host,
    which are defined in the Vagrantfile at the indicated path.

    CLI Example:

    .. code-block:: bash

        salt '*' virt.list_inactive_vms cwd=/projects/project_1
    rFr(rGr<r*r,r>�)rHr)r/r-r.r0r1r2�endswithrBrIrrr�list_inactive_vms�s�rM�c
	Cs�|rt|�}|d}|dp|}n|std|����d}g}d|��}td||�}t�d|�|�d�D]C}|����}t|�d	krx|d
�d�rxz$|dd
�	|d	d
��|d
�
d��d�t|d|�d�}	|�
|	�Wq5tywYq5wq5|S)a
    Return list of information for all the vms indicating their state.

    If you pass a VM name in as an argument then it will return info
    for just the named VM, otherwise it will return all VMs defined by
    the Vagrantfile in the `cwd` directory.

    CLI Example:

    .. code-block:: bash

        salt '*' vagrant.vm_state <name>  cwd=/projects/project_1

    returns a list of dictionaries with machine name, state, provider,
    and salt_id name.

    .. code-block:: python

        datum = {'machine': _, # Vagrant machine name,
                 'state': _, # string indicating machine state, like 'running'
                 'provider': _, # the Vagrant VM provider
                 'name': _} # salt_id name

    Known bug: if there are multiple machines in your Vagrantfile, and you request
    the status of the ``primary`` machine, which you defined by leaving the ``machine``
    parameter blank, then you may receive the status of all of them.
    Please specify the actual machine name for each VM if there are more than one.

    rrz,Path to Vagranfile must be defined, but cwd=rNzvagrant status r(r<r*r,r>rKr� �()r�stateZproviderr)r#rr/r-r.r0r1r2rL�join�lstrip�rstripr$rBrA)
rrrrr.r5r6r8r9Zdatumrrr�vm_states8�
���rUFc	Cs�|durin|��}||d<|p|�d�|d<|dstd��|p&|�d|�|d<|p0|�d|�|d<|p:|�dd�|d<t||�|rQt�d	|�t||�}|Sd
�||dpYd�}|S)a`
    Initialize a new Vagrant VM.

    This inputs all the information needed to start a Vagrant VM.  These settings are stored in
    a Salt sdb database on the Vagrant host minion and used to start, control, and query the
    guest VMs. The salt_id assigned here is the key field for that database and must be unique.

    :param name: The salt_id name you will use to control this VM
    :param cwd: The path to the directory where the Vagrantfile is located
    :param machine: The machine name in the Vagrantfile. If blank, the primary machine will be used.
    :param runas: The username on the host who owns the Vagrant work files.
    :param start: (default: False) Start the virtual machine now.
    :param vagrant_provider: The name of a Vagrant VM provider (if not the default).
    :param vm: Optionally, all the above information may be supplied in this dictionary.
    :return: A string indicating success, or False.

    CLI Example:

    .. code-block:: bash

        salt <host> vagrant.init <salt_id> /path/to/Vagrantfile
        salt my_laptop vagrant.init x1 /projects/bevy_master machine=quail1
    Nrrz5Path to Vagrantfile must be defined by "cwd" argumentrr)�vagrant_providerrNzStarting VM %szName {} defined using VM {}r')�copyrrrr-r3�_start�format)	rrrr)�startrVZvmr�retrrr�initJs" �

�r\cCst|�}t||�S)z�
    Start (vagrant up) a virtual machine defined by salt_id name.
    The machine must have been previously defined using "vagrant.init".

    CLI Example:

    .. code-block:: bash

        salt <host> vagrant.start <salt_id>
    )r#rXrrrrrZs
rZcCs�z|d}Wntytd|����w|�dd�}|r"d|��nd}d|�d|��}td||�d	�|�d
�dd�}|dkrc|d
�d�D]}|�d�rb|��d�d�}||d<t||�nqG|ddkrrd|�d|�d�SdS)Nrr!rVrNz--provider=zvagrant up rO�cmd.run_allr)rr.�r)rZoutput_loglevel�stdoutr*z==>r,�:�retcoderz	Started "z" using Vagrant machine "z".F)r"rrr/r0�
startswithrTr)rrrrVZ	provider_r5r[r8rrrrX�s,��

�rXcCst|�S)a=
    Send a soft shutdown (vagrant halt) signal to the named vm.

    This does the same thing as vagrant.stop. Other-VM control
    modules use "stop" and "shutdown" to differentiate between
    hard and soft shutdowns.

    CLI Example:

    .. code-block:: bash

        salt <host> vagrant.shutdown <salt_id>
    )�stop)rrrr�shutdown�srdcC�@t|�}|d}d|��}td||�d�|�d�d�}|dkS)z�
    Hard shutdown the virtual machine. (vagrant halt)

    CLI Example:

    .. code-block:: bash

        salt <host> vagrant.stop <salt_id>
    rz
vagrant halt �cmd.retcoder)r�r)rr�r#r/r�rrrr5r[rrrrc��


rccCre)z�
    Pause (vagrant suspend) the named VM.

    CLI Example:

    .. code-block:: bash

        salt <host> vagrant.pause <salt_id>
    rzvagrant suspend rfr)rrgrrhrirrr�pause�rjrkcCsRt|�}|d}|rdnd}d|�d|��}td||�d�|�d�d	�}|d
kS)a
    Reboot a VM. (vagrant reload)

    CLI Example:

    .. code-block:: bash

        salt <host> vagrant.reboot <salt_id> provision=True

    :param name: The salt_id name you will use to control this VM
    :param provision: (False) also re-run the Vagrant provisioning scripts.
    rz--provisionrNzvagrant reload rOrfr)rrgrrh)rZ	provisionrrZprovr5r[rrr�reboot�s
rlcCs\t|�}|d}d|��}td||�d�|�d�dd�}|dd	kr,t|�d
|��SdS)z�
    Destroy and delete a virtual machine. (vagrant destroy -f)

    This also removes the salt_id name defined by vagrant.init.

    CLI Example:

    .. code-block:: bash

        salt <host> vagrant.destroy <salt_id>
    rzvagrant destroy -f r]r)rr.r^rarz
Destroyed VM F)r#r/rr&rirrr�destroy�s
�
rmcCs�t|�}t|�}z|d|d|d|dd�}Wnty+td�|�dd����w|r�d	jdi|��}t�d
|�td|�}t�d|�t	j
|d
d�}|�d�D]R}	zD|	�ddd���}
d}d|
vru|
�
d�d}t	�|
|�}nd|
vr�|
�
d�d}t	�|
|�d�d�}||vr�t|�|d<WnWqTtttfy�YqTwt�d||�dd��|r�z)tjj�|d��}
tjj�|
���|d<Wd�W|S1s�wYW|Sty�}ztd|����d}~ww|S)a�
    Retrieve hints of how you might connect to a Vagrant VM.

    :param name: the salt_id of the machine
    :param network_mask: a CIDR mask to search for the VM's address
    :param get_private_key: (default: False) return the key used for ssh login
    :return: a dict of ssh login information for the VM

    CLI Example:

    .. code-block:: bash

        salt <host> vagrant.get_ssh_config <salt_id>
        salt my_laptop vagrant.get_ssh_config quail1 network_mask=10.0.0.0/8 get_private_key=True

    The returned dictionary contains:

    - key_filename:  the name of the private key file on the VM host computer
    - ssh_username:  the username to be used to log in to the VM
    - ssh_host:  the IP address used to log in to the VM.  (This will usually be `127.0.0.1`)
    - ssh_port:  the TCP port used to log in to the VM.  (This will often be `2222`)
    - \[ip_address:\]  (if `network_mask` is defined. see below)
    - \[private_key:\]  (if `get_private_key` is True) the private key for ssh_username

    About `network_mask`:

    Vagrant usually uses a redirected TCP port on its host computer to log in to a VM using ssh.
    This redirected port and its IP address are "ssh_port" and "ssh_host".  The ssh_host is
    usually the localhost (127.0.0.1).
    This makes it impossible for a third machine (such as a salt-cloud master) to contact the VM
    unless the VM has another network interface defined.  You will usually want a bridged network
    defined by having a `config.vm.network "public_network"` statement in your `Vagrantfile`.

    The IP address of the bridged adapter will typically be assigned by DHCP and unknown to you,
    but you should be able to determine what IP network the address will be chosen from.
    If you enter a CIDR network mask, Salt will attempt to find the VM's address for you.
    The host machine will send an "ifconfig" command to the VM (using ssh to `ssh_host`:`ssh_port`)
    and return the IP address of the first interface it can find which matches your mask.
    ZIdentityFileZUserZHostNameZPort)Zkey_filenameZssh_usernameZssh_hostZssh_portz=Insufficient SSH information to contact VM {}. Is it running?rr'z�ssh -i {IdentityFile} -p {Port} -oStrictHostKeyChecking={StrictHostKeyChecking} -oUserKnownHostsFile={UserKnownHostsFile} -oControlPath=none {User}@{HostName} ifconfigz5Trying ssh -p %(Port)s %(User)s@%(HostName)s ifconfigr(r<F)�strictr*zaddr:rNr,NZinetZinet6�/r�
ip_addressz(Network IP address in %s detected as: %sz(not found)Zprivate_keyz+Error processing Vagrant private key file: r)r#r:r"rrYrr-r.r/r�
ip_networkr0�replace�indexrp�strrA�AttributeError�	TypeErrorrr�filesZfopenZstringutilsZ
to_unicode�read�OSError)rZnetwork_maskZget_private_keyrr7ZansZcommandr6Ztarget_network_ranger8r9Z
found_addressZnxtZpks�errr�get_ssh_configs�(
�
����������
�������r{)N)rNN)NrNNFrNN)F)rNF)'�__doc__�loggingrZsalt.utils.filesrZsalt.utils.pathZsalt.utils.stringutilsZsalt._compatrZsalt.exceptionsrr�	getLogger�__name__r-r
rr
rrrr#r$r&r:r;rErJrMrUr\rZrXrdrcrkrlrmr{rrrr�<module>sN

 
!

@
�5