%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /usr/lib/python2.7/site-packages/salt/cloud/clouds/
Upload File :
Create Path :
Current File : //usr/lib/python2.7/site-packages/salt/cloud/clouds/libvirt.pyo

�
���^c@@sdZddlmZmZmZddlZddlZddlZddlm	Z	ddl
mZy&ddlZddlm
Z
eZWnek
r�eZnXddljZddlZddlmZmZmZmZidd6dd	6dd
6dd6d
d6d
d6dd6ZdZdZeje�Z d�Z!er]ej"de!dd�nd�Z$d�Z%d�Z&dd�Z'dd�Z(dd�Z)d�Z*d�Z+ed�Z,d �Z-d!�Z.dd"�Z/d#�Z0d$�Z1d%�Z2d&�Z3d'�Z4d(�Z5dS()u
Libvirt Cloud Module
====================

Example provider:

.. code-block:: yaml

    # A provider maps to a libvirt instance
    my-libvirt-config:
      driver: libvirt
      # url: "qemu+ssh://user@remotekvm/system?socket=/var/run/libvirt/libvirt-sock"
      url: qemu:///system

Example profile:

.. code-block:: yaml

    base-itest:
      # points back at provider configuration e.g. the libvirt daemon to talk to
      provider: my-libvirt-config
      base_domain: base-image
      # ip_source = [ ip-learning | qemu-agent ]
      ip_source: ip-learning
      # clone_strategy = [ quick | full ]
      clone_strategy: quick
      ssh_username: vagrant
      # has_ssh_agent: True
      password: vagrant
      # if /tmp is mounted noexec do workaround
      deploy_command: sh /tmp/.saltcloud/deploy.sh
      # -F makes the bootstrap script overwrite existing config
      # which make reprovisioning a box work
      script_args: -F
      grains:
        sushi: more tasty
      # point at the another master at another port
      minion:
        master: 192.168.16.1
        master_port: 5506

Tested on:
- Fedora 26 (libvirt 3.2.1, qemu 2.9.1)
- Fedora 25 (libvirt 1.3.3.2, qemu 2.6.1)
- Fedora 23 (libvirt 1.2.18, qemu 2.4.1)
- Centos 7 (libvirt 1.2.17, qemu 1.5.3)

i(tabsolute_importtprint_functiontunicode_literalsN(tElementTree(tsix(tlibvirtError(tSaltCloudConfigErrortSaltCloudExecutionFailuretSaltCloudNotFoundtSaltCloudSystemExiturunningiiupausediushutdowniiucrashediun<filterref filter='clean-traffic'>
        <parameter name='CTRL_IP_LEARNING' value='any'/>
      </filterref>ulibvirtcC@stjd|�dS(u>
    Redirect stderr prints from libvirt to salt logging.
    ulibvirt error %sN(tlogtdebug(tctxterror((s=/usr/lib/python2.7/site-packages/salt/cloud/clouds/libvirt.pytlibvirt_error_handlergstfRcC@s-tstdfSt�tkr)tdfStS(u�
    This function determines whether or not
    to make this cloud module available upon execution.
    Most often, it uses get_configured_provider() to determine
     if the necessary configuration has been set up.
    It may also check for necessary imports decide whether to load the module.
    In most cases, it will return a True or False value.
    If the name of the driver used does not match the filename,
     then that name should be returned instead of True.

    @return True|False|str
    u2Unable to locate or import python libvirt library.u)The 'libvirt' provider is not configured.(tHAS_LIBVIRTtFalsetget_configured_providert__virtualname__(((s=/usr/lib/python2.7/site-packages/salt/cloud/clouds/libvirt.pyt__virtual__rs



cC@stjttptd�S(u/
    Return the first configured instance.
    uurl(uurl(tconfigtis_provider_configuredt__opts__t__active_provider_name__R(((s=/usr/lib/python2.7/site-packages/salt/cloud/clouds/libvirt.pyR�s	cC@sGytj|�}Wn-tk
rBtdjtd|���nX|S(NuHSorry, {0} failed to open a connection to the hypervisor software at {1}ufqdn(tlibvirttopent	ExceptionRtformatt
__grains__(turltconn((s=/usr/lib/python2.7/site-packages/salt/cloud/clouds/libvirt.pyt
__get_conn�s
cC@s|dkrtd��ntjdi�}i}ggtj|�D]}|jd�^qFD]}|r_|^q_}x�|D]�}t|d�}|j�}xn|D]f}	i|	j�d6dd6dd	6t|	j	�d
d6gd6t
|	tj�d
6}
|
||	j
�<q�Wq~W|S(u�
    Return a list of the VMs

    id (str)
    image (str)
    size (str)
    state (str)
    private_ips (list)
    public_ips (list)

    uactionu=The list_nodes function must be called with -f or --function.u	providersulibvirtuurluiduuimageusizeiustateuprivate_ipsu
public_ips(R	RtgetRt
itervaluesR tlistAllDomainst
UUIDStringtVIRT_STATE_NAME_MAPtstatetget_domain_ipsRt(VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_LEASEtname(tcallt	providerstrettcfgt_ftproviders_to_checktproviderRtdomainstdomaintdata((s=/usr/lib/python2.7/site-packages/salt/cloud/clouds/libvirt.pyt
list_nodes�s&D


cC@s%|dkrtd��nt|�S(ui
    Because this module is not specific to any cloud providers, there will be
    no nodes to list.
    uactionuBThe list_nodes_full function must be called with -f or --function.(R	R4(R*((s=/usr/lib/python2.7/site-packages/salt/cloud/clouds/libvirt.pytlist_nodes_full�scC@s[|dkrtd��ntjd�}|s?td��ntjjjt�||�S(uO
    Return a list of the VMs that are on the provider, with select fields
    uactionuDThe list_nodes_select function must be called with -f or --function.uquery.selectionu,query.selection not found in /etc/salt/cloud(R	RR!tsalttutilstcloudtlist_nodes_selectR5(R*t	selection((s=/usr/lib/python2.7/site-packages/salt/cloud/clouds/libvirt.pyR9�scC@s*|tjkrdS|tjkr&dSdS(Nuipv4uipv6(RtVIR_IP_ADDR_TYPE_IPV4tVIR_IP_ADDR_TYPE_IPV6(t	addr_type((s=/usr/lib/python2.7/site-packages/salt/cloud/clouds/libvirt.pytto_ip_addr_type�sc
C@s�g}|jd�}|dtjkr,|Sy|j|d�}Wn'tjk
rk}tjd|�|SXxtj|�D]n\}}|dr|xU|dD]F}t	|d�}	tjd|�|	dkr�|j
|d�q�q�Wq|q|W|S(NiuException polling address %suaddrsutypeuFound address %suipv4uaddr(R&RtVIR_DOMAIN_RUNNINGtinterfaceAddressesRR
tinfoRt	iteritemsR>tappend(
R2t	ip_sourcetipsR&t	addressesR
R)tvaltaddrttp((s=/usr/lib/python2.7/site-packages/salt/cloud/clouds/libvirt.pyR'�s"
cC@set||�}|r@g|D]}|jd�s|^q}n|sYt|�|kr]dS||S(Nu127.(R't
startswithtlentNone(R2tidxRDt
skip_loopbackREtip((s=/usr/lib/python2.7/site-packages/salt/cloud/clouds/libvirt.pyt
get_domain_ip
s+c!C@s�|jd�pd}|dWkr9tdj|���n|jd�pKd}|dXkrrtdj|���n|jd	�dVk	r�|jd	�nt}tjd
|d||�y7|dr�tjt	t
p�d
|d�tkr�tSWntk
rnX|d}t
ddddj|�dt
dd|ddddg�dt	ddt	d�tjd|t	dtddV�}|dVk	r�tjj|�r�tdj|���n||d<||d<g}y�|d }t|d!�}y|j|�}	Wn�tk
r�}
|j|�}|jd"�}i|d6|d 6}
t
ddd#d$j|�dit
dd%|
t|
��d&6dt	ddt	d�tjd'|�tj|�}||jd(�_|jd)�dVkrtjd*�}|jd"|�n|jd)�}d+j|�|_|j|jd,��xg|j d-�D]V}|j|jd.��|dkrG|jd/�dVkrG|j!tjt"��qGqGWx�|j d0�D]�}|jd1�dVk	r�|jd2�}|rDd3|j#krD|j#d3}|j$d4j|�d4j|��}tjd5|�||j#d3<qDq�q�Wx�|j d6�D]�}|jd7�}|dVkr�t%d8��n|j#jd9�}tjd:|j#�|d;kr`|jd<�j#d=}t&||�\}}|dkr|j't(|�d"�}n|j)t*|�|d"�}|j!id>d?6|d@6�|j�|jd<�j#d=<qX|dAkr�|jd<�j#d=}t&||�\}}|j)t*|�|d"�}|j!id>d?6|d@6�|j�|jd<�j#d=<qXt%dBj|���qXWt+j,j-j.tj/|��}tjdC|�|rBt0j1nd"}|j2||�}	|j!idDd?6|	d@6�|	j3t0j4�nXtjdE|�|dkr�t0j5}n|dkr�t0j6}nt+j,j7j8t9dF|	d"|fdGtjdH|t	ddY�dKtjdL|t	ddI�dMtjdN|t	ddO��}tjdP|�||dQ<t
dR|t	�} t
dddSdTj|�dt
ddU|ddddg�dt	ddt	d�| SWn#t:k
r�}
t;|�|
�nXdVS(Zu$
    Provision a single machine
    uclone_strategyufulluquicku8'clone_strategy' must be one of quick or full. Got '{0}'u	ip_sourceuip-learningu
qemu-agentu?'ip_source' must be one of qemu-agent or ip-learning. Got '{0}'uvalidate_xmlu1Cloning '%s' with strategy '%s' validate_xml='%s'unameuprofileulibvirtucloud.fire_eventueventustarting createusalt/cloud/{0}/creatingtargsucloud.filter_eventucreatinguproviderudrivertsock_dirusock_dirt	transportu	transportuprivate_keyt
search_globaltdefaultu-The defined key_filename '{0}' does not existukey_filenameubase_domainuurliurequesting instanceusalt/cloud/{0}/requestingu
requestingukwargsuSource machine XML '%s'u./nameu
./descriptionudescriptionuCloned from {0}u./uuidu./devices/interfaceu./macu/./filterref/parameter[@name='CTRL_IP_LEARNING']u./devices/channel[@type='unix']u8./target[@type='virtio'][@name='org.qemu.guest_agent.0']u./source[@mode='bind']upathu/domain-{0}/u!Rewriting agent socket path to %su,./devices/disk[@device='disk'][@type='file']u./driver[@name='qemu']u-Non qemu driver disk encountered bailing out.utypeudisk attributes %suqcow2u./sourceufileuvolumeuwhatuitemurawuDisk type '{0}' not supporteduClone XML '%s'udomainuVM '%s'tupdate_argsttimeoutuwait_for_ip_timeouti
i<tintervaluwait_for_ip_intervaltinterval_multiplieruwait_for_ip_interval_multiplieriuAddress = %sussh_hostucloud.bootstrapucreated instanceusalt/cloud/{0}/createducreatedN(uquickufull(uip-learningu
qemu-agentiX(<R!R	RRLtTrueR
RARtis_profile_configuredRRRtAttributeErrort	__utils__tget_cloud_config_valuetostpathtisfileRR tlookupByNameRtXMLDesctlistRRt
fromstringtfindttexttElementtinserttremovetfindallRCtIP_LEARNING_XMLtattribtreplaceRtfind_pool_and_volumet	createXMLt$create_volume_with_backing_store_xmlt
createXMLFromtcreate_volume_xmlR6R7tstringutilstto_strttostringRtVIR_DOMAIN_DEFINE_VALIDATEtdefineXMLFlagstcreateWithFlagstVIR_DOMAIN_START_FORCE_BOOTt(VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_AGENTR(R8twait_for_ipRPRt
do_cleanup(!tvm_tclone_strategyRDtvalidate_xmlR)tkey_filenametcleanuptbaseRtclone_domainteR2txmltkwargst
domain_xmltdescription_elemtdescriptiont	iface_xmlt	agent_xmltsource_elementR`tnew_pathtdisktdrivert	disk_typetsourcetpooltvolumet
new_volumet	clone_xmltvalidate_flagstaddressR,((s=/usr/lib/python2.7/site-packages/salt/cloud/clouds/libvirt.pytcreates�*	

"




 
!
$
"

cC@s2tjd�x|D]}|d}|d}|dkr�tjd||j��y'|j�tjd||j��Wntk
r�nXy;|jtjtj	tj
�tjd||j��Wq�tk
r�q�Xn|dkry'|j�tjd	||j��Wq*tk
r&q*XqqWd
S(uI
    Clean up clone domain leftovers as much as possible.

    Extra robust clean up in order to deal with some small changes in libvirt
    behavior over time. Passed in volumes and domains are deleted, any errors
    are ignored. Used when cloning/provisioning a domain fails.

    :param cleanup: list containing dictonaries with two keys: 'what' and 'item'.
                    If 'what' is domain the 'item' is a libvirt domain object.
                    If 'what' is volume then the item is a libvirt volume object.

    Returns:
        none

    .. versionadded: 2017.7.3
    uCleaning up after exceptionuwhatuitemudomainuCleaning up %s %su%s %s forced offu%s %s undefineduvolumeu%s %s cleaned upN(R
RAR)tdestroyRRt
undefineFlagsRt VIR_DOMAIN_UNDEFINE_MANAGED_SAVEt&VIR_DOMAIN_UNDEFINE_SNAPSHOTS_METADATAtVIR_DOMAIN_UNDEFINE_NVRAMtdelete(R�tleftovertwhattitem((s=/usr/lib/python2.7/site-packages/salt/cloud/clouds/libvirt.pyR}�s0









c
C@s�tjd|�|dkr+td��ng}tjdi�}ggtj|�D]}|jd�^qVD]}|ro|^qo}xt|D]l}t|d�}tjd|d�y.|j|�}	|j	i|	d6|d	6�Wq�t
k
r�q�Xq�W|sd
j|�St|�dkr0dj|�St
d
dddj|�di|d6dtddtd�t|dd	|dd�t
d
dddj|�di|d6dtddtd�dS(u&
    This function irreversibly destroys a virtual machine on the cloud provider.
    Before doing so, it should fire an event on the Salt event bus.

    The tag for this event is `salt/cloud/<vm name>/destroying`.
    Once the virtual machine has been destroyed, another event is fired.
    The tag for that event is `salt/cloud/<vm name>/destroyed`.

    Dependencies:
        list_nodes

    @param name:
    @type name: str
    @param call:
    @type call:
    @return: True if all went well, otherwise an error message
    @rtype: bool|str
    u Attempting to delete instance %sufunctionuEThe destroy action must be called with -d, --destroy, -a or --action.u	providersulibvirtuurlu
looking at %sudomainuconnu&{0} doesn't exist and can't be deletediu4{0} doesn't identify a unique machine leaving thingsucloud.fire_eventueventudestroying instanceusalt/cloud/{0}/destroyingRQunameRRusock_dirRSu	transportiudestroyed instanceusalt/cloud/{0}/destroyedN(R
RAR	RR!RR"R RbRCRRRKR]tdestroy_domain(
R)R*tfoundR+R-R.R/R0RR2((s=/usr/lib/python2.7/site-packages/salt/cloud/clouds/libvirt.pyR�sDD







cC@s�tjd|j��y|j�Wntk
r7nXt||�}x.|D]&}tjd|j��|j�qNWtjd|j��|jt	j
t	jt	j�dS(NuDestroying domain %suRemoving volume %suUndefining domain %s(
R
RAR)R�Rtget_domain_volumesRR�R�RR�R�R�(RR2tvolumesR�((s=/usr/lib/python2.7/site-packages/salt/cloud/clouds/libvirt.pyR�Hs


cC@s�d}tj|�}t|j��|jd�_tjdt|��t	j
|j�d�|jd�_|j�|jd�_t
jjjtj|��}tjd|�|S(Nun<volume>
                    <name>n</name>
                    <capacity>c</capacity>
                    <allocation>0</allocation>
                    <target>
                        <path>p</path>
                        <format type='qcow2'/>
                        <compat>1.1</compat>
                    </target>
                </volume>
                unameu
Volume: %siucapacityu
./target/pathuCreating %s(RRetgenerate_new_nameR)RfRgR
RtdirRt	text_typeRAR`R6R7RtRuRv(R�ttemplatet
volume_xmlt
xml_string((s=/usr/lib/python2.7/site-packages/salt/cloud/clouds/libvirt.pyRsYs%cC@s�d}tj|�}t|j��|jd�_tjdt|��t	j
|j�d�|jd�_|j�|jd�_t
jjjtj|��}tjd|�|S(Nu�<volume>
                    <name>n</name>
                    <capacity>c</capacity>
                    <allocation>0</allocation>
                    <target>
                        <format type='qcow2'/>
                        <compat>1.1</compat>
                    </target>
                    <backingStore>
                        <format type='qcow2'/>
                        <path>p</path>
                    </backingStore>
                </volume>
                unameu
volume: %siucapacityu./backingStore/pathuCreating %s(RReR�R)RfRgR
RR�RR�RAR`R6R7RtRuRv(R�R�R�R�((s=/usr/lib/python2.7/site-packages/salt/cloud/clouds/libvirt.pyRqps%cC@sfxJ|jd�D]9}x0|j�D]"}|j�|kr#||fSq#WqWtdj|���dS(Niiu"Could not find volume for path {0}i(tlistAllStoragePoolstlistAllVolumesR`RR(RR`tsptv((s=/usr/lib/python2.7/site-packages/salt/cloud/clouds/libvirt.pyRo�s
cC@sSd|kr"dj|tj��S|jdd�\}}dj|tj�|�S(Nu.u{0}-{1}iu{0}-{1}.{2}(Rtuuidtuuid1trsplit(t	orig_nameR)text((s=/usr/lib/python2.7/site-packages/salt/cloud/clouds/libvirt.pyR��scC@s�g}tj|jd��}x�|jd�D]~}|jd�dk	r.|jd�jd}y&t||�\}}|j|�Wq�t	k
r�t
jd|�q�Xq.q.W|S(Niu,./devices/disk[@device='disk'][@type='file']u%./driver[@name='qemu'][@type='qcow2']u./sourceufileuDisk not found '%s'(RReRcRkRfRLRmRoRCRR
twarning(RR2R�R�R�R�R�R�((s=/usr/lib/python2.7/site-packages/salt/cloud/clouds/libvirt.pyR��s
(6t__doc__t
__future__RRRtloggingR�R_t	xml.etreeRtsalt.extRRRRZRtImportErrorRtsalt.configRtsalt.utils.cloudR6tsalt.exceptionsRRRR	R%RlRt	getLoggert__name__R
RtregisterErrorHandlerRLRRR R4R5R9R>R'RPR�R}R�R�RsRqRoR�R�(((s=/usr/lib/python2.7/site-packages/salt/cloud/clouds/libvirt.pyt<module>1s\	


"

				'			�	+C				
	

Zerion Mini Shell 1.0