File: //opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/__pycache__/vagrant.cpython-310.pyc
o
�N�g�P � @ s& d Z ddlZddlZddlZddlZddlZddlmZ ddl m
Z
mZ e�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/c C s t jj�d�du rdS tS )z*
run Vagrant commands if possible
r N)FzAThe vagrant module could not be loaded: vagrant command not found)�salt�utils�path�which�__virtualname__� r r �H/opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/vagrant.py�__virtual__. s r
c C s 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)�keyr r r �_build_sdb_uri: s r c C s | � dt j�|�� �}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�cwdr r r r �_build_machine_uriD s r c C sF t d t| �|t� |d r!t d t|d |�dd��| t� dS dS )z'store the vm_ information keyed by name�sdb.sdb_setr r �.N)� __utils__r �__opts__r �get��name�vm_r r r �_update_vm_infoP s ��r c C sP zt d t| �t�}W n ty td��w |du sd|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 )r r r �KeyErrorr r r r r �get_vm_info[ s ��r# c C s t d t| |�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 )r r r )r r r r r r �get_machine_idm s r$ c C s� z0t | �}|d r/t|d |�dd��}z td |t� W n ty. td |dt� Y nw W n ty9 Y nw t| �}z
td |t� W dS tyZ td |i t� Y dS tyc Y dS w )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.
r r r zsdb.sdb_deleter N)r# r r r r r"