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/cloud/__pycache__/__init__.cpython-310.pyc
o

�N�g�_�@s|dZddlZddlZddlZddlZddlZddlZddlZddlZddl	m
Z
ddlZddl
ZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlmZmZmZmZddlm Z e�!e"�Z#dd�Z$							ddd	�Z%Gd
d�d�Z&Gdd
�d
�Z'Gdd�de'�Z(dd�Z)ddd�Z*ddd�Z+ddd�Z,dd�Z-dd�Z.dd�Z/dS) z`
The top level interface used to translate configuration data back to the
correct cloud modules
�N)�groupby)�SaltCloudConfigError�SaltCloudException�SaltCloudNotFound�SaltCloudSystemExit)�compile_templatecs�fdd�}|S)z(Warning, this is a picklable decorator !c
s0||d<d}z�|i|��}|�d�W|StyA}z t��}|�d�|�d�|�|�d|�d��WYd}~|Sd}~wtyl}z t��}|�d�|�d�|�|�d|�d��WYd}~|Sd}~wty�}z t��}|�d�|�d	�|�|�d|�d��WYd}~|Sd}~ww)
z7called with [queue, args, kwargs] as first optional arg�queueN�END�KEYBOARDINTzKeyboard interrupt�
�ERROR�	ExceptionzSystem exit)�put�KeyboardInterrupt�	traceback�
format_excr
�
SystemExit)r�args�kwargs�ret�ex�trace��func��G/opt/saltstack/salt/lib/python3.10/site-packages/salt/cloud/__init__.py�_call,s8�

 ��

 ��

 ��zcommunicator.<locals>._callr)rrrrr�communicator)srcs:�si�|sd}|st�|�}�st��}|���|dur0|s0d�|�}	t�|	�td|	����|durF��fdd�|D�}
|j||
d�}n	|�	|�|�g�}	��
�}|d	vr���
�}
��
�}d
|
�d�}	|	d|�d
�7}	t�|	�|��|��td|	����|dvs�|r�||�r�|�
�|��	|St�d�qP)a'
    Manage a multiprocessing pool

    - If the queue does not output anything, the pool runs indefinitely

    - If the queue returns KEYBOARDINT or ERROR, this will kill the pool
      totally calling terminate & join and ands with a SaltCloudSystemExit
      exception notifying callers from the abnormal termination

    - If the queue returns END or callback is defined and returns True,
      it just join the process and return the data.

    target
        the function you want to execute in multiprocessing
    pool
        pool object can be None if you want a default pool, but you ll
        have then to define pool_size instead
    pool_size
        pool size if you did not provide yourself a pool
    callback
        a boolean taking a string in argument which returns True to
        signal that 'target' is finished and we need to join
        the pool
    queue
        A custom multiprocessing queue in case you want to do
        extra stuff and need it later in your program
    args
        positional arguments to call the function with
        if you don't want to use pool.map

    mapped_args
        a list of one or more arguments combinations to call the function with
        e.g. (foo, [[1], [2]]) will call::

                foo([1])
                foo([2])

    kwargs
        kwargs to give to the function in case of process

    Attention, the function must have the following signature:

            target(queue, *args, **kw)

    You may use the 'communicator' decorator to generate such a function
    (see end of this file)
    �NzdWe are called to asynchronously execute {} but we do no have anything to execute, weird, we bail outzException caught
csg|]}�|g�g�qSrr)�.0�arg�rrrr�
<listcomp>�sz"enter_mainloop.<locals>.<listcomp>)r�iterableT)rr
zCaught z, terminating workers
zTRACE: r)r	g�?)�multiprocessing�Pool�Manager�Queue�format�log�errorr�mapZapply�get�	terminate�join�close�time�sleep)�targetZmapped_argsrr�pool�	pool_size�callbackr�manager�msgr#r�testZtype_rrr!r�enter_mainloopGsJ9
�


��r9c@s�eZdZdZd'dd�Zdd�Zdd�Zd(d	d
�Zd(dd�Zd(d
d�Z	d)dd�Z
d*dd�Zd+dd�Zd,dd�Z
d(dd�Zd(dd�Zdd �Zd!d"�Zd#d$�Z						d-d%d&�ZdS).�CloudClientz5
    The client class to wrap cloud interactions
    NcCsh|r||_ntj�|�|_|jdg}tjj�|tjj���|r�|�	di��
�D]\}}|d}i|d<|jd�|||ii�q)|�	di��
�D]A\}}	|	d�d�d}t
t|jd|����}|�d|��|	d<||	d<|jd�||	i�|jd||d�||	i�qJ|�	d	i��
�D]\}}
d	|jvr�i|jd	<|
|jd	|<q�|j�|�dSdS)
NZcachedir�	providers�driver�profiles�provider�:r�profile�maps)�opts�salt�configZcloud_config�utilsZverifyZ
verify_env�userZget_user�pop�items�update�split�next�iter�keys)�self�pathrBZ
config_dirZpillarsZv_dirs�namer>r<r@Zmap_dctrrr�__init__�s2�

�zCloudClient.__init__c	Ks�tjj��}|�|j���d|d<d|d<d|d<d|d<d|d<d|d	<d
|vr1|�|d
�|�|�|�dd�}|rz|�d
i���}dd�|D�D]}||ksX|�|�qMdd�|��D�}|rz|�di�}|��D]}||vry|�|�qn|S)zi
        Set the opts dict to defaults and allow for opts to be overridden in
        the kwargs
        F�parallelZkeep_tmpT�deployZupdate_bootstrap�show_deploy_args�Zscript_argsrr@Nr=cSsg|]}|�qSrr�r�arrrr"�sz.CloudClient._opts_defaults.<locals>.<listcomp>cSs.g|]}|�dd�r|�dd��d�d�qS)r>rUr?r)r,rJrVrrrr"�s
��r;)	rCrDZDEFAULT_CLOUD_OPTS�copyrIrBr,rG�values)	rNrrBr@Ztmp_profilesZ_profiler;Z
_providersr>rrr�_opts_defaults�s8

��
�zCloudClient._opts_defaultscCs8t||�}tjj�||�}||�dd�i|�di���S)zG
        Pass the cloud function and low data structure to run
        rrr)�getattrrCrErZformat_callr,)rN�fun�lowZl_funZf_callrrrr]�s
zCloudClient.lowcC�$tj�|���}tjj�|�|��S)zF
        List all available sizes in configured cloud systems
        )rC�cloud�MaprZrE�data�simple_types_filter�	size_list�rNr>�mapperrrr�
list_sizes�zCloudClient.list_sizescCr^)zG
        List all available images in configured cloud systems
        )rCr_r`rZrErarb�
image_listrdrrr�list_imagesrgzCloudClient.list_imagescCr^)zJ
        List all available locations in configured cloud systems
        )rCr_r`rZrErarb�
location_listrdrrr�list_locationsrgzCloudClient.list_locations�
list_nodescC�$tj�|���}d|jd<|�|�S)z2
        Query basic instance information
        rl�selected_query_option�rCr_r`rZrB�map_providers_parallel�rNZ
query_typererrr�query�

zCloudClient.query�list_nodes_fullcCrm)z0
        Query all instance information
        rtrnrorqrrr�
full_queryrszCloudClient.full_query�list_nodes_selectcCrm)�3
        Query select instance information
        rvrnrorqrrr�select_query%rszCloudClient.select_query�list_nodes_mincCrm)rwryrnrorqrrr�	min_query-rszCloudClient.min_querycKsV|si}||d<tj�|jdi|���}t|t�r|�d�}tjj�	|j
|||d��S)aY
        Pass in a profile to create, names is a list of vm names to allocate

            vm_overrides is a special dict that will be per node options
            overrides

        Example:

        .. code-block:: python

            >>> client= salt.cloud.CloudClient(path='/etc/salt/cloud')
            >>> client.profile('do_512_git', names=['minion01',])
            {'minion01': {'backups_active': 'False',
                    'created_at': '2014-09-04T18:10:15Z',
                    'droplet': {'event_id': 31000502,
                                 'id': 2530006,
                                 'image_id': 5140006,
                                 'name': 'minion01',
                                 'size_id': 66},
                    'id': '2530006',
                    'image_id': '5140006',
                    'ip_address': '107.XXX.XXX.XXX',
                    'locked': 'True',
                    'name': 'minion01',
                    'private_ip_address': None,
                    'region_id': '4',
                    'size_id': '66',
                    'status': 'new'}}


        r@�,)�vm_overridesNr)rCr_r`rZ�
isinstance�strrJrErarb�run_profile)rNr@�namesr|rrerrrr@5s 

�zCloudClient.profilecKsNi}|r||d<|�|�tj�|jdi|���}|��}tjj�|�	|��S)z"
        To execute a map
        r+Nr)
rIrCr_r`rZ�map_datarErarb�run_map)rNrOr�kwargre�dmaprrr�map_run_s
zCloudClient.map_runcCs<tj�|jdd��}t|t�r|�d�}tjj�	|�
|��S)�'
        Destroy the named VMs
        T)�destroyr{)rCr_r`rZr}r~rJrErarbr�)rNr�rerrrr�ks

zCloudClient.destroyc	Ks�tj�|���}|jd}||vr!|dtt||������7}ndSt|t	�r-|�
d�}i}|D]"}|��}||d<||d<d|d<||d	<tjj
�|�|��||<q1|S)
aM
        Create the named VMs, without using a profile

        Example:

        .. code-block:: python

            client.create(provider='my-ec2-config', names=['myinstance'],
                image='ami-1624987f', size='t1.micro', ssh_username='ec2-user',
                securitygroup='default', delvol_on_destroy=True)
        r;r?Fr{rPr<Nr@r>)rCr_r`rZrBrKrLrMr}r~rJrXrErarb�create)	rNr>r�rrer;rrP�vm_rrrr�ts 
 

zCloudClient.createc
Ks�tj�|���}|��}||vr |dtt||������7}ndSt|t	�r,|�
d�}i}|D]"}|��}	||	d<||	d<d|	d<||	d<tjj
�|�|	��||<q0|S)	a�
        Perform actions with block storage devices

        Example:

        .. code-block:: python

            client.extra_action(names=['myblock'], action='volume_create',
                provider='my-nova', kwargs={'voltype': 'SSD', 'size': 1000}
            )
            client.extra_action(names=['salt-net'], action='network_create',
                provider='my-nova', kwargs={'cidr': '192.168.100.0/24'}
            )
        r?Fr{rPr>Nr@�action)rCr_r`rZrprKrLrMr}r~rJrXrErarb�extras)
rNr�r>r�rrer;rrP�extra_rrr�extra_action�s  

zCloudClient.extra_actioncCs||duri}tj�|jd||d�|���}|r |rtd��|g}|r/|s/||jd<|�||�S|r:|s:|�|||�Std��)aD
        Execute a single action via the cloud plugin backend

        Examples:

        .. code-block:: python

            client.action(fun='show_instance', names=['myinstance'])
            client.action(fun='show_image', provider='my-ec2-config',
                kwargs={'image': 'ami-10314d79'}
            )
        N)r�r�zMPlease specify either a list of 'names' or a single 'instance', but not both.r�zTEither an instance (or list of names) or a provider must be specified, but not both.r)rCr_r`rZrrB�	do_action�do_function)rNr\Zcloudmapr�r>�instancerrerrrr��s"�
�zCloudClient.action)NNNN�N)rl)rt)rv)ry)NNNNNN)�__name__�
__module__�__qualname__�__doc__rQrZr]rfrirkrrrurxrzr@r�r�r�r�r�rrrrr:�s0
*








*	&$�r:c@s�eZdZdZdd�Zdd�Zdd�Zdd	�Zd6dd
�Zd6dd�Z		d7dd�Z
dd�Zd8dd�Zd8dd�Z
d8dd�Zd8dd�Zd8dd�Zd d!�Zd9d"d#�Zd$d%�Zd:d(d)�Zed*d+��Zd,d-�Zd;d.d/�Zd0d1�Zd2d3�Zd4d5�ZdS)<�Cloudz/
    An object for the creation of new VMs
    cCs(||_tj�|j�|_|��i|_dSr�)rBrC�loader�clouds�$_Cloud__filter_non_working_providers�_Cloud__cached_provider_queries�rNrBrrrrQ�s
zCloud.__init__cCsXt�}|jd��D]\}}t|�dkr$|D]}|�|�d|���qq
|�|�q
|S)z1
        Return the configured providers
        r;rr?)�setrBrH�len�add)rNr;�alias�driversr<rrr�get_configured_providers�szCloud.get_configured_providerscCs|durd}|dkr-t�}|jd��D]\}}|D]	}|�||f�qq|s+td��|Sd|vrU|�d�\}}||jdvsH||jd|vrUtd�|d�|������t�}|jd��D]\}}|D]}|||fvrt|�||f�qeq_|s�td�|d�|������|S)	z@
        Get a dict describing the configured providers
        N�allr;z(There are no cloud providers configured.r?z.No cloud providers matched '{}'. Available: {}�, z9No cloud providers matched '{}'. Available selections: {})	r�rBrHr�rrJr(r.r�)rN�lookupr;r�r�r<rrr�lookup_providerssD�������zCloud.lookup_providersc
Cs�|durd}|durd}|dkrct�}t�}|jd��D]7\}}|�d�}|rT|�d�d}|�d�d}	||krB|�||f�n|	|krM|�||	f�|�||f�q|s[td��|dkra|S|SdS)	zH
        Return a dictionary describing the configured profiles
        Nr�r=r>r?rrz'There are no cloud profiles configured.)r�rBrHr,rJr�r)
rNr>r�r=Zprovider_profilesr��infor;Zgiven_prov_nameZsalt_prov_namerrr�lookup_profiles1s0
��zCloud.lookup_profilesrlFc
Cs,|dur||jvr|j|Si}|jd��D]w\}}|��D]n\}}|�d|��}||jvr6t�d|�q||vr>i||<z-tjjj	|j|d�
||g�d��|j|�|||<Wd�n1sewYWqty�}	ztjd||	t
jd	�g|||<WYd}	~	qd}	~	wwq||j|<|S)
z�
        Return a mapping of what named VMs are running on what VM providers
        based on what providers are defined in the configuration and VMs
        Tr;�.�)Public cloud provider %s is not availabler?�Z__active_provider_name__N�=Failed to execute '%s()' while querying for running nodes: %s�Zexc_info_on_loglevel)r�rBrHr�r)r*rCrE�context�func_globals_injectr.r
�debug�logging�DEBUG)
rNrr�cached�pmapr�r�r<�detailsr\�errrrr�
map_providersQs@

�������
zCloud.map_providersc
Cs@|dur||jvr|j|S|j��}g}|�|d�|d<|d��D]>\}}|}|��D]3\}}	|�d�durC|�d�|jvrCd}|�d|��}
|
|jvrVt�d|�q.|�	|
||||d	��q.q$i}|si|St
|�}t�|d
kru|pvd
t
�}
tt||
d�}|D]\}}}	|	s�q�||vr�i||<|	|||<q�||j|<|S)z�
        Return a mapping of what named VMs are running on what VM providers
        based on what providers are defined in the configuration and VMs

        Same as map_providers but query in parallel.
        Tr;rnNz.list_nodes_minryr�r�)r\rBrrr�r<�
)r3)r�rBrX�_optimize_providersrHr,r�r)r*�appendr�r$r%�init_pool_workerr9�!_run_parallel_map_providers_query)rNrrr�rBZmultiprocessing_datar�r�Z
this_queryr<r�r\�outputZ
data_countr3Z
parallel_pmaprrrrpvsT


�����
zCloud.map_providers_parallelNcCsDt|t�r|g}i}i}|j||d�}|��D]�\}}	|	��D]\}
}|
|vr+|||
<|r>||jd|d�d�dvr>q|��D][\}}
||vrKqB|
dkrfd|vrfd||dvrf|||ddvrfqB|
dkr�d|vr�d||dvr�|||ddvr�qB||vr�i||<|
||vr�i|||
<|
|||
|<qBqq|S)N�r�r=r>r?rZec2Zaws)r}r~rprHrBrJ)rNr�rrr�r@�matchesZhandled_drivers�mapped_providersr�r�r<�vms�vm_namer�rrr�get_running_by_names�sH

����-zCloud.get_running_by_namescCs�i}i}|��D]\}}|��D]\}}||vri||<||||<qq|��D]L\}}|�d�}	|	|jvrTt�d|�|��D]\}}
||vrLi||<|
|||<q@q(|j|	|�}|rt|��D]\}}
||vrmi||<|
|||<qaq(|S)zD
        Return an optimized mapping of available providers
        z.optimize_providersz0The '%s' cloud driver is unable to be optimized.)rHr�r)r�)rNr;Z
new_providersZprovider_by_driverr�r<rPraZproviders_datar\Z	prov_dataZnew_datarrrr��s2�

�zCloud._optimize_providersr�cC��i}|�|�}|s|S|D]g\}}|�d�}||jvr#t�d||�q
||vr+i||<z-tjjj|j|d�||g�d��|j|�|||<Wd�n1sRwYWq
t	yt}ztj
d||tjd�WYd}~q
d}~ww|S)zO
        Return a mapping of all location data for available providers
        z.avail_locationszbThe '%s' cloud driver defined under '%s' provider alias is unable to get the locations informationr?r�N�&Failed to get the output of '%s()': %sr��
r�r�r)r�rCrEr�r�r.r
r*r�r��rNr�ra�lookupsr�r<r\r�rrrrjs@


�������zCloud.location_listcCr�)�L
        Return a mapping of all image data for available providers
        z
.avail_imagesz_The '%s' cloud driver defined under '%s' provider alias is unable to get the images informationr?r�Nr�r�r�r�rrrrh:�@


�������zCloud.image_listcCr�)r�z.avail_sizesz^The '%s' cloud driver defined under '%s' provider alias is unable to get the sizes informationr?r�Nr�r�r�r�rrrrcbr�zCloud.size_listcCsPi}|�|�}|s|S|D]\}}||vri||<|||vr%i|||<q
|S)r�)r�)rNr�rar�r�r<rrr�
provider_list�s
�zCloud.provider_listcCsRi}|�||�}|s|S|D]\}}||vri||<|||vr&i|||<q|S)z=
        Return a mapping of all configured profiles
        )r�)rNr>r�rar�r�r<rrr�profile_list�s�zCloud.profile_listcCs4g}|jd��D]\}}|�||�|�i�q	|S)z6
        Create/Verify the VMs in the VM data
        r=)rBrHr�r�)rNrr��
vm_detailsrrr�
create_all�szCloud.create_allc
Cs�i}t|�}|j||d�}t�}g}|��D].\}}|��D]%\}	}
|
D]}||vrB|�||	|f�|jdrB|�|j|||	d��q$qq|jdr�|r�d|jvrW|jd}nt|�}t�d|�t	t
||d�}
i}|
D]}|�|�ql|D]5}|d}|d}	|d	}||vr�i||<|	||vr�i|||	<|||||	|<||vr�|�|�qvn`t�d
�|D]X\}}	}|	�d�}t
jjj|j|d�||	g�d
��|j||�}Wd�n1s�wY||vr�i||<|	||vr�i|||	<||||	|<||v�r|�|�q�|D�]*\}}	}|||	|}|�s"�q|dd�||	g�|	d�}t
jjd||jid�}tj�|jdd|�d|��}t�|�d��}tj�|��st|�stt|t��rrd|v�rrt
jj�|jd|d��qtj�|��r�|�s�t
jj�|jdtj�|���qtj�|��s�|�r�t|�dk�r�t
jj�|jdtj�|d���qtd� |��	t!|�D]\}}td|�dtj�|�����q�t"d�}zt#|�}Wnt$�y�td|�d��Ynwztj�|�%|��}Wnt&�yY�q�wt"d |�d!��}|d"k�s|�'��(d#��r0t
jj�|jd|�td$|�d��n	td%|�d��	�q|�rK|�sKt)d&� d'�|����|�rW|�rW||d(<|S|�s^t)d)��|S)*r�r�rR)rBrPr�r<r4z0Destroying in parallel mode; Cloud pool size: %s�r4r�r<rPz Destroying in non-parallel mode.z.destroyr?r�N)rPr@r>r<�minion��default�pki_dirZminions�idz.*ZnewnamerrzjThere are several minion keys who's name starts with '{}'. We need to ask you which one should be deleted:T� z: z,Which minion key should be deleted(number)? �'z' is not a valid selection.zDelete 'z
'? [Y/n]? rU�yz	Deleted 'zDid not delete 'z%The following VM's were not found: {}r��	Not FoundzNo machines were destroyed!)*r�r�rHr�rBr�r�r)r�r9�_destroy_multiprocessingrI�removerCrEr�r�r�r.rD�get_cloud_config_value�osrOr,�glob�isfiler}�dictr_Z
remove_key�basename�printr(�	enumerate�input�int�
ValueErrorrGr
�lower�
startswithr)rNr�r�Z	processed�matchingZvms_to_destroy�
parallel_datar�r�r<r�rPr4�
output_multipZ
ret_multip�objr\rr��minion_dict�key_fileZglobbed_key_file�idx�filenameZ	selection�deleterrrr��s
�����
�
��

��

��
���
���� �����z
Cloud.destroycCs�g}|��}i}|��D]\}}g||<|D]
}||vr#||�|�qq|��D]\}}|�d�}	|D]}
|�|
|j|	|
�i�q4q)|S)z&
        Reboot the named VMs
        z.reboot)rprHr�r�)rNr�rr�Zacts�prov�nodes�nodeZnames_r\rPrrr�reboot`s ��
�zCloud.rebootT�c
Cs�i}tjjd||jid�}|d�d�\}}|�d�}||jvr.t�d|d|d|�dStj�d	||j�}	tj�d
||j�}
|	ry|
sPd|vrPt�d|d�d
|vrxd|vrxt�	d|d�tj
j�tj�d||j��\}}||d
<||d<nd|d
<d|d<|�
d|d�}
|�
d�}|�
d�r�|r�||d<d|vr�d�|
|dg�}
|
dur�d|vr�d|vr�t�	d|d�tj
j�tj�d||j��\}}||d<||d<|dur�|	dur�tj
j�|jd|d
|
�tj�d||j�|d<z
tj�d||j�|d<Wn
t�y
Ynwz�|d�d�\}}|�d�}tj
jj|j|d�||g�d��|j||�}Wd�n	1�s>wY|du�r�d |jv�r�|jd �rf|jd d!v�rft�d"�d#|jd <t�|�t��d$}t��|k�r�t�tjj�}d%�|jd&�d%�dd'��}|�tj�tj�|d���tjj|d(��,}|j|dd)�|jd �|jd*d+�}|�r�t� d,|�	Wd�nWd�n	1�s�wYt��|k�sxWnt�y�}zt�!d-|d|�WYd}~nd}~wwz|jd.}Wnt�yd}Ynw|jd/�r[|jd0�r[|�s[t� d1|jd0|d�tjj|jd(��}|j|d|jd0|jd*d$d+�}Wd�n	1�sRwY||d2<|S)3z$
        Create a single VM
        r�r�r>r?z.create�TCreating '%s' using '%s' as the provider cannot complete since '%s' is not availablerPNrS�make_master�masterz2There's no master defined on the '%s' VM settings.�pub_key�priv_key�Generating minion keys for '%s'�keysizer��domainZuse_fqdnZ
append_domainr�T�
master_pub�
master_pemz#Generating the master keys for '%s'r�Zscriptr�Z
inline_scriptr�FZsync_after_install)r�Zbeaconsr�ZenginesZ	executors�grainsr)Zmatchers�modulesr�ZpillarZproxymodulesZ	renderersZ	returnersZsdbZserializersZstatesZthoriumrEz6Bad option for sync_after_install. Defaulting to 'all'r��<�/�	conf_file���)Zmoptszsaltutil.sync_{}�timeout)rz.Synchronized the following dynamic modules: %sz>Failed to create VM %s. Configuration value %s needs to be setr+rR�start_action�Running %s on %sr)"rCrDr�rBrJr�r)r*�warningr�rEr_�gen_keysr,r.Z
accept_key�KeyErrorr�r�r0r1rX�deepcopyZDEFAULT_MASTER_OPTSrIZ
master_configr�rO�client�get_local_client�cmdr(r��	exception)rNr��local_masterZ
sync_sleepr�r�r�r<r\rSr��priv�pubZkey_idr�Zmaster_privr�rZexpiration_timeZmopts_Z	conf_pathrr�excZopt_mapZ
action_outrrrr�ss�
�

����
���
�����

�������������zCloud.createcCs>|��}tjj�||�}tjj�||�}|�|�||d<|S)a
        Create vm config.

        :param str name: The name of the vm
        :param dict main: The main cloud config
        :param dict provider: The provider config
        :param dict profile: The profile config
        :param dict overrides: The vm's config overrides
        rP)rXrCrE�
dictupdaterI)rP�mainr>r@�	overridesZvmrrr�	vm_configs
zCloud.vm_configc
Cs�i}|d�d�\}}d�||d�}||jvr&t�d|d|d|�dSz-tjjj|j||dd��|j|d
i|��}Wd�W|S1sLwYW|St	yu}zt�
d	|d|d|d|�WYd}~|Sd}~ww)z
        Extra actions
        r>r?�{}.{}r�r�rPNr�zEFailed to perform %s.%s on %s. Configuration value %s needs to be setr)rJr(r�r)r*rCrEr�r�r
r)rNr�r�r�r<r\rrrrr�!s>
��������zCloud.extrascCs�||jdvrd|�d�}t�|�d|iSi}|si}z(tjj�|jdd��}tjj�|�}Wd�n1s:wY|sCi}Wnt	yOi}Ynt
yYi}Ynw|dur`i}|��}|jd|}	i}
|��D])\}}t
t|��}
|||
D]}|||
||
|<||
|d<|
|
|d	<q�qq|	d�d
�\}}|jd||��}|d=|D]�}||
vr�|
|d}|
|d	}|�d|�d
|��}t�|�d|i||<q�|�||||	|�}|jd
�rtj|j|fd�}|��dd�|j�i||<q�z;|�|�||<||�s#ddi||<t|�dk�r!td��Wq�|j�dd�du�r=t||t��r=||�dd�Wq�ttf�yc}zt|�dk�rQ�dt|�i||<WYd}~q�d}~ww|S)zl
        Parse over the options passed on the command line and determine how to
        handle them
        r=zProfile z is not defined�Errorr�rNr>r<r?r;z already exists under rR)r2rZProvisioningz)VM being provisioned in parallel. PID: {}zFailed to deploy VMrrTF�
deploy_kwargs) rBr)r*rCrE�files�fopenZyamlZ	safe_loadr
�OSErrorrprHrKrLrJrXrr$ZProcessr��startr(�pidr�rr,r}r�rGrr~)rNr@r�r|r7rZmccZmain_cloud_configr��profile_detailsr�r��valZ	prov_namer�r�r<�provider_detailsrPZdrivr��processrrrrrBs�
����
���
���zCloud.run_profilecCsji}i}t|�}|����D]�\}}|sn�|��D]�\}}|s"n�d}	d�||jd�}
|
|jvr:t�d|
�d}	|��D]�\}}|sFn�||vrit|t	�sQi}d|vr`|d|vr`|d}n	t�
d|||�q>|	dur�|�|
�dur{|�|
gi�||
�
|�q>tjjj|j|
d	�||g�d
��M||vr�i||<|||vr�i|||<tjjjdi|��}|r�|j|
||dd�||||<n|j|
|dd�||||<|�|�Wd�n1s�wYq>qqt�}
|�r||d<t�}|��D]\}}|�t|��}q�|�|�}
|
�rt|
�|d
<t|�|d<|�s"|S|
�r'|St|�|d<t|�|d
<|S)zX
        Perform an action on a VM which may be specific to this cloud provider
        Trr�z)'%s()' is not available. Not actioning...Fr�z-vm:%s in provider:%s is not in name list:'%s'Nr?r���callzInvalid Actionsr�zNot Actioned/Not Runningr)r�rprHr(rBr�r)r�r}r�r�r,rIr�rCrEr�r�r.rZclean_kwargsr��union�
difference�list)rNr�rrZinvalid_functionsr�r�r<r�Zvalid_functionr\r�r�Zmissing_vmsZinvalid_func_vms�keyr"rrrr��s�


������
zCloud.do_actioncCs|�|�}t|�dkrtd�|d�dd�|D�����|��\}}|�d|��}||jvr6td�|||���t�d||�t	j
jj|j|d	�||g�d
��*|re|||j|d|d�iiWd
�S|||j|dd�iiWd
�S1s{wYd
S)z=
        Perform a function against a cloud provider
        rz=More than one results matched '{}'. Please specify one of: {}r�cSsg|]\}}|�d|���qS)r?r)rr�r<rrrr"sz%Cloud.do_function.<locals>.<listcomp>r�zUThe '{}' cloud provider alias, for the '{}' driver, does not define the function '{}'z4Trying to execute '%s' with the following kwargs: %sr?r��function)r&rNr%)
r�r�rr(r.rGr�r)r�rCrEr�r�)rNr�rrr�r�r<r\rrrr��s2
��

����$�zCloud.do_functionc
Cs<|jd����D]�\}}|��D]r}|�d�}||jvrIt�d|||||�|jd|�|�||jdvr9q|jd|sH|jd�|�qtjj	j
|j|d�||g�d��!|j|�durtt�d||�|jd|�|�Wd�n1s~wYq||jdvr�q	|jd|s�|jd�|�q	dS)	zV
        Remove any mis-configured cloud providers from the available listing
        r;z.get_configured_provideraNThe cloud driver, '%s', configured under the '%s' cloud provider alias, could not be loaded. Please check your provider configuration files and ensure all required dependencies are installed for the '%s' driver.
In rare cases, this could indicate the '%s()' function could not be found.
Removing '%s' from the available providers listr?r�Fz�The cloud driver, '%s', configured under the '%s' cloud provider alias is not properly configured. Removing it from the available providers list.N)rBrXrHr�r)rrGrCrEr�r�r.)rNr�r�r<r\rrrZ__filter_non_working_providerssJ

��������z$Cloud.__filter_non_working_providers�rlF)rlFN)r��F)Tr�r�)r�r�r�r�rQr�r�r�r�rpr�r�rjrhrcr�r�r�r�r�r��staticmethodrr�rr�r�r�rrrrr��s:
+
 
%A
�9
"
)
(
(

+


!V` r�c@s`eZdZdZdd�Zddd�Zdd	d
�Zdd�Zd
d�Zddd�Z	dd�Z
ddd�Zdd�ZdS)r`z3
    Create a VM stateful map execution object
    cCst�||�|��|_dSr�)r�rQ�read�rendered_mapr�rrrrQSszMap.__init__rlFcCsz|����}i}|��D]�\}}t|�}||jdvr8d|vr#i|d<d�|d�|��}t�|�||d|<q|�	|||�}	|	��D]B\}
}|��D]9\}}
|
��D]0\}}|
|vr_i||
<|||
vrki||
|<|||
||<z|�
|�WqSty�YqSwqKqC|s�q|jd|}|d�d�\}
}|D]}|
|vr�i||
<|||
vr�i||
|<d||
||<q�q|S)Nr=�ErrorszBNo provider for the mapped '{}' profile was found. Skipped VMS: {}r�r>r?�Absent)
r/rXrHr�rBr(r.r)r�r�r�r
rJ)rNrrr�r0�interpolated_mapr@Z
mapped_vmsr�r7r�r�r�r<r�r�r�r!rrrr3WsR�
���
�zMap.interpolated_mapNc	Cs�|j|d�}|����D]B\}}|dkrq|����D])\}}|����D]\}}|dkr6|||�|�q%|||sD||�|�q||sN|�|�q|S)N)rrr1r2)r3rXrHrG)	rNrr�	query_mapr�r�r<r�r�r�rrr�
delete_map�s ��
�zMap.delete_mapcCs�|�d�}dgddgddgd�}g}|��D]>\}}|��D]5\}}|��D],\}	}
z||}Wnty@t�d|�t��w|
dkrR|
d��|vrR|�|	�q&qq|S)	Nrl�stoppedZrunningZactive)r�stopr�zxThe use of '%s' as an action is not supported in this context. Only 'start', 'stop', and 'reboot' are supported options.r2�state)r3rHr
r)r*rr�r�)rNr�r4Zmatching_statesZvm_namesr�r�r<r�r�r�Zstate_actionrrr�get_vmnames_by_action�s2
���	
���zMap.get_vmnames_by_actioncCsd}|j�dd�durH|j�dd�durB|j�dd�durn�|j�d�|j�d�vr6t�d|jd�t��|jd|j�d�}n�|jd}n�t�|j�}d|d<tj�	|�|_t
j�|jd�sv|jd�
d	�svt�d
|jd�t��|jd�
d	�r�|jjd|jd�}n|jd}z%|j�dd
�}tj�|ji�}|j�d�}|j�d�}t|||||�}Wn!ty�}ztjd|jd|tjd�iWYd}~Sd}~wwd|vr�tjj||jddd�}|s�iS|����D]�\}	}
t|
ttf��rNi}|
D]H}t|t��r|di}|��D]6\}
}|du�st|t��r!i}z|�d|
�Wnt�y@t�d�iYSw|||
<�q�q|||	<q�t|
t��roi}|
��D]\}
}|�d|
�|||
<�qZ|||	<q�t|
t��rx|
g}
i||	<|
D]}
d|
i||	|
<�q~q�|S)zH
        Read in the specified map and return the map structure
        Nr+r�Z
map_pillarrAz8The specified map not found in pillar at 'cloud:maps:%s'�localZfile_clientzsalt://z+The specified map file does not exist: '%s'z
cp.cache_file�rendererz
jinja|yamlZrenderer_blacklistZrenderer_whitelistz)Rendering map %s failed, render error:
%sr�ZincludeF)�verboserPz�Cannot use 'name' as a minion id in a cloud map as it is a reserved word. Please change 'name' to a different minion id reference.) rBr,r)r*rrXrrCr�ZMasterMinionr�rOr�r�Z	functionsr�Zrenderrr
r�r�rDZinclude_configrHr}r)�tupler~�bool�
setdefault�AttributeErrorr�)rN�map_Zlocal_minion_optsZ
cached_mapr;ZrendZ	blacklistZ	whitelistrr@Zmapped�entries�mappingrPrrrrr/�s��
�

����	���	�zMap.readc	Cs�|dur6|d��D])}g}z|d}Wntyg}Ynw|D]}|j|t|�|d�r2dSq"q
dS||vr<dS|�|�z
|d|d}WntyVg}Ynw|D]}|j|t|�|d�rhdSqYdS)Nr��requires)�seenr"TF)rYr
�	_has_loopr)r�)rNr�rEr"rYZmachines�machinerrrrF&s6����
��z
Map._has_loopc	Cs�z|d}Wnty|YSwg}|D]:}z|d|}Wn$tyDz|d|}WntyAd}t�|�t|��wYnw|�|�||||��qt|�d}|S)NrDr��existingzMissing dependency in cloud mapr)r
r)r*rr��_calcdep�max)	rNr�rGra�levelZdeplistZlevelsrPr7rrrrI@s,�
���zMap._calcdepc	sdii}|j|d��t�}t�}t�|j�}|��D]�\}}||jdvrGd�|d�dd�|D���}t	�
|�d|vr@i|d<||d|<q|jd�|�}	|��D]�\}
}d	|vrn|d	|	d	krn|�d	��d
�\}}
n
|	�d	��d
�\}}
t�|jd||
�}|d=t
jj�||	�t�|�}dD]}d
|��}||vr�t	�d||
|�|�|�||<q�d|vr�d|vr�d|dvr�d|dvr�|dd�|dd�|dd=|ds�|d=t
jj�||�}||d|
<|d	�d
�\}}
|�||
|
f�qSq�fdd�}���D]\\}}|��D]R\}
}|��D]H\}}|�||
|f�||dv�r.�q||�}|�s7�q|D]&}||dv�rDnt	�d|�d|v�rSi|d<|d�|�|d|<�q9�q�q�q	d|jv�r�|jd�r�|jddu�r~td��|�|�|d<|S)z9
        Create a data map of what to execute on
        r�r�r=zjThe required profile, '{}', defined in the map does not exist. The defined nodes, {}, will not be created.r�css�|]	}d|�d�VqdS)r�Nr)rr�rrr�	<genexpr>cs�zMap.map_data.<locals>.<genexpr>�errorsr>r?r;)r�r�r�ZvolumesrDrAz�The use of '%s' on the '%s' mapping has been deprecated. The preferred way now is to just define '%s'. For now, salt-cloud will do the proper thing and convert the deprecated mapping into the preferred one.r�r�csZi}���D]$\}}|��D]\}}|��D]\}}||kr(||vr(|d||<qqq|S)Nr8)rH)rPr�r�r�r<r�r�r��r�rr�get_matching_by_name�s���z*Map.map_data.<locals>.get_matching_by_namez0%r already exists, removing from the create map.rHZhardZenable_hard_mapsFz�The --hard map can be extremely dangerous to use, and therefore must explicitly be enabled in the main configuration file, by setting 'enable_hard_maps' to Truer�)rpr�rXrr0rHrBr(r.r)r*r,rJrCrErrIrrGr�rr()rNr�rZexistZdefinedr0Zprofile_namer�r7Zprofile_data�nodenamerr�r<r#ZnodedataZsettingZ
deprecatedrOr�r�rPr�r��itemrrNrr�Ts���


�
�
�8	�
����zMap.map_datac$Cs||�|�rd}t�|�t|��|d��D]#\}}t�d|�d}|�||||�}t�d||�||d|d<qz|d��}Wn
tyOi��}Ynw|D]#\}}t�d|�d}|�||||�}t�d||�||d|d<qRt	|d��dd	�d
�}i}|j
dr�g}	d}
d}d}d}
�z^td
d�|D��\}
}|
}t�d|
�tj
�d||j
�dur�td��t�d|d�tjj�tj
�d||j
��\}}||d<||d<tjj��}tjj�|d��
}|�|�Wd�n1s�wYtjjj||j
dd�}
t�|�|�dd�du�r=|�di�d|dv�r'|dd}d|dv�r=d |dd<|
du�r=|
|d!<|D]F\}}tj
jd||j
dd"�}|du�rU�q?t�d#|d�tjj�tj
�d||j
��\}}||d$<||d%<|�d&i�|d&�||i��q?d}|d�d'd��r�|d�dd�du�r�d}|j||d(�}t|t ��s�t�d)|�nd*|v�r�td+�!|d*���|j
�d,d�du�r�|�d-i��p�|�"d-i�}|�d.|�d/d��}|du�r�td0�!|
���|||
<Wn,t#�yt�d1�tj$�%|j
d2d3�}tj$�&|��rtjjj||j
dd�}
Ynw|j
�'�}|j
d�r3t�d4�d|d5<|
du}|D]�\}}||
|fv�rF�q9d|v�ra|d�d'd��ra|d�dd�du�rad}|
du�ro|du�ro|
|d!<|du�r�|�di�|d�d|�|j
d�r�|	�(||||d6���q9z+|j||d(�||<|j
�d,d�du�r�d-|v�r�t||t ��r�||�"d-d�W�q9t�y�}ztjd7||t)j*d8�d9t+|�i||<WYd}~�q9d}~ww|�d:d;�D]
}|�,|�||<�q�|j
d�r�|	�r�d<|j
v�r|j
d<}nt-|	�}t�d=|�t.t/|	|d>�}|j
d?�r�g}d@}t0|d�1�dAd	��D]\}}|�(g�|dB7}|D]} ||�(| d��qA�q2i}|D]=}!t�dC|j
d?dD�%|!��tj2�3��}"|�|"j4dE�%|!�|j
d?|j
dFdGdHdI��Wd�n	1�s�wY�qT|D]}#|tt5|#�6���tt5|#�1���dJ<|�|#��q�|S|D]}#|�|#��q�|S)Kz4
        Execute the contents of the VM map
        z,Uh-oh, that cloud map has a dependency loop!r�zCalculating dependencies for %srzGot execution order %s for %srKrHcSs|ddS)NrrKr��xrrr�<lambda>�szMap.run_map.<locals>.<lambda>)r*rRNcss,�|]\}}|�dd�dur||fVqdS)r�FTN)r,)rrPr@rrrrL�s���zMap.run_map.<locals>.<genexpr>zCreating new master '%s'rSFzTCannot proceed with 'make_master' when salt deployment is disabled(ex: --no-deploy).zGenerating master keys for '%s'rPr�r�r��wZ	hash_type)Zsum_type�make_minionTr�r�r�z	127.0.0.1�
master_fingerr�r�r�r�Zpreseed_minion_keysr�rz/Master creation details is not a dictionary: %sr1z?An error occurred while creating the master, not continuing: {}rTrZ	salt_host�hostz2Host for new master {} was not found, aborting mapzNo make_master found in mapr�z
master.pubziSince parallel deployment is in use, ssh console output is disabled. All ssh output will be logged thoughZdisplay_ssh_output)rBrPr@r� Failed to deploy '%s'. Error: %sr�rr�rr4zCloud pool size: %sr�rrcSs|dS)NrKrrRrrrrT�srrr�r{rrr))rZtgt_typer)7rFr)r*rrHr�rIr�r
�sortedrBrKrCrDr�rrEr_r	rZmkstempr�writeZcryptZ
pem_fingerr��unlinkr,r?rIr�r}r�r(rG�
StopIterationrOr.r�rXr�r�r�r~r�r�r9�_create_multiprocessingrrYrr
rrLrM)$rNr�r7r*r"rKZ
existing_listZcreate_listr�r�Zmaster_nameZmaster_minion_nameZmaster_hostrWZmaster_profilerrZmaster_temp_pubZmtprPr@rVr�outrr�rBrr4r�Z
actionlist�grprQ�grouprr�rrrr��s�

�
������
�


�
��

�
�
��
��

���
�
�
��
�����	�
������	$�zMap.run_mapr,r�)NNr-)
r�r�r�r�rQr3r5r9r/rFrIr�r�rrrrr`Ns

.
s
r`cCst�tjtj�dS)zh
    Make every worker ignore KeyboarInterrup's since it will be handled by the
    parent process.
    N)�signal�SIGINT�SIG_IGNrrrrr��sr�c
Cs�d|dd<t|d�}z
|j|d|dd�}Wn(tyA}ztjd|d|tjd	�|dd
t|�iiWYd}~Sd}~ww|d�dd
�d
urWt	|t
�rW|�dd�|dtj
j�|�iS)z�
    This function will be called from another process when running a map in
    parallel mode. The result from the create is always a json object.
    �jsonrBr�r@rrXrZrPr�rNrTFr)r�r�rr)r*r�r�r~r,r}r�rGrCrErarb)r�rr_r�rrrr�create_multiprocessing�s*
�� ��	�rgc
Cs�d|dd<tj�|d�}z3|d�|d�}tjjj|d�|d|dg�d��||d	�}Wd
�n1s;wYWn(tyi}zt	j
d|d	|tjd�|d	d
t
|�iiWYd
}~Sd
}~ww|d	tjj�|�iS)z�
    This function will be called from another process when running a map in
    parallel mode. The result from the destroy is always a json object.
    rfrBr�z
{}.destroyr<r?r�r�rPNzFailed to destroy %s. Error: %sr�r)rCr�r�r(rEr�r�r.rr)r*r�r�r~rarb)r�rr�r\r�rrrr�destroy_multiprocessing	s0����� ��	rhc
Cs�t|d�}z?tjjj|j|dd�|d|dg�d��|d|dtjj�|j|d��fWd�WS1s>wYWdSt	yl}zt
jd|d|tj
d	�|d|dd
fWYd}~Sd}~ww)z`
    This function will be called from another process when building the
    providers map.
    rBr\r?r�r<r�Nr�r�r)r�rCrEr�r�r�r.rarbr
r)r�r�r�)rarr_r�rrr� run_parallel_map_providers_query 	s*��(�	���ricO�tt�|di|��S�Nr)rri�r�kwrrrr�<	�r�cOrjrk)rrhrlrrrr�@	rnr�cOrjrk)rrgrlrrrr_D	rnr_)NNNNNNNr�)0r�rXr�r�r$r�rcr0r�	itertoolsrZsalt.clientrCZsalt.configZsalt.loaderZ
salt.syspathsZsalt.utils.argsZsalt.utils.cloudZsalt.utils.contextZsalt.utils.cryptZsalt.utils.dataZsalt.utils.dictupdateZsalt.utils.filesZsalt.utils.userZsalt.utils.verifyZsalt.utils.yamlZsalt.exceptionsrrrrZ
salt.templater�	getLoggerr�r)rr9r:r�r`r�rgrhrir�r�r_rrrr�<module>sz
 
�dEh