%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /lib/python2.7/site-packages/salt/modules/
Upload File :
Create Path :
Current File : //lib/python2.7/site-packages/salt/modules/xapi_virt.pyo

�
���^c@@sBdZddlmZmZmZddlZddlZddlZddlm	Z	ddlm
Z
yddlZeZ
Wnek
r�eZ
nXddlZddlZddlZddlZddlmZdZd�Zd	�Zejd
��Zd�Zd�Zd
�Zd�Zd�Z d�Z!d�Z"d�Z#dd�Z%dd�Z&d�Z'd�Z(d�Z)d�Z*d�Z+d�Z,d�Z-d�Z.d�Z/d�Z0d�Z1d �Z2d!�Z3d"�Z4d#�Z5d$�Z6d%dd&ddd'�Z7d(�Z8d)�Z9dd*�Z:dd+�Z;dd,�Z<dS(-u�
This module (mostly) uses the XenAPI to manage Xen virtual machines.

Big fat warning: the XenAPI used in this file is the one bundled with
Xen Source, NOT XenServer nor Xen Cloud Platform. As a matter of fact it
*will* fail under those platforms. From what I've read, little work is needed
to adapt this code to XS/XCP, mostly playing with XenAPI version, but as
XCP is not taking precedence on Xen Source on many platforms, please keep
compatibility in mind.

Useful documentation:

. http://downloads.xen.org/Wiki/XenAPI/xenapi-1.0.6.pdf
. http://docs.vmd.citrix.com/XenServer/6.0.0/1.0/en_gb/api/
. https://github.com/xapi-project/xen-api/tree/master/scripts/examples/python
. http://xenbits.xen.org/gitweb/?p=xen.git;a=tree;f=tools/python/xen/xm;hb=HEAD
i(tabsolute_importtprint_functiontunicode_literalsN(trange(tmap(tCommandExecutionErroruvirtcC@s�tddkrwd}tjj|�rwtjjj|�}dj|�}tjj	|�rtt
jj|�qtqwny'tr�t
jd�Std�jjSWnttfk
r�tSXdS(NuosuDebianu#/usr/lib/xen-common/bin/xen-versionu/usr/lib/xen-{0}/lib/pythonu
xen.xm.XenAPI(t
__grains__tostpathtisfiletsalttmodulestcmdmodt
_run_quiettformattisdirtsystappendt
HAS_IMPORTLIBt	importlibt
import_modulet
__import__txmtXenAPItImportErrortAttributeErrortFalse(tdebian_xen_versiont
xenversiontxapipath((s:/usr/lib/python2.7/site-packages/salt/modules/xapi_virt.pyt
_check_xenapi3s
cC@st�tk	rtStdfS(Nu Module xapi: xenapi check failed(RRt__virtualname__(((s:/usr/lib/python2.7/site-packages/salt/modules/xapi_virt.pyt__virtual__Escc@s�t�}tdd�}tdd�}tdd�}|sHd}n|sWd}n|sfd}nzRy.|j|�}|jj||�|jVWntk
r�td��nXWd|jjj�XdS(	uI
    Get a session to XenAPI. By default, use the local UNIX socket.
    u
config.optionuxapi.uriu
xapi.loginu
xapi.passwordu"httpu:///var/run/xend/xen-api.sockuu#Failed to connect to XenAPI socket.N(	Rt__salt__tSessiontxenapitlogin_with_passwordt	ExceptionRtsessiontlogout(t_xenapitxapi_urit
xapi_logint
xapi_passwordR&((s:/usr/lib/python2.7/site-packages/salt/modules/xapi_virt.pyt_get_xapi_sessionKs$				
cC@s@x9ddgD]+}tjjj|�}|dk	r
|Sq
WdS(u6
    Internal, returns xl or xm command line path
    uxluxmN(R
tutilsRtwhichtNone(txtoolR((s:/usr/lib/python2.7/site-packages/salt/modules/xapi_virt.pyt
_get_xtoolrscC@st||�j�S(u2
    Internal, returns all members of rectype
    (tgetattrtget_all(txapitrectype((s:/usr/lib/python2.7/site-packages/salt/modules/xapi_virt.pyt_get_all|scC@s7yt||�j|�dSWntk
r2tSXdS(u(
    Internal, returns label's uuid
    iN(R2tget_by_name_labelR%R(R4R5tlabel((s:/usr/lib/python2.7/site-packages/salt/modules/xapi_virt.pyt_get_label_uuid�s
cC@st||�j|�S(u2
    Internal, returns a full record for uuid
    (R2t
get_record(R4R5tuuid((s:/usr/lib/python2.7/site-packages/salt/modules/xapi_virt.pyt_get_record�scC@s8t|||�}|tkr"tSt||�j|�S(u2
    Internal, returns a full record for uuid
    (R9RR2R:(R4R5R8R;((s:/usr/lib/python2.7/site-packages/salt/modules/xapi_virt.pyt_get_record_by_label�scC@s)|d}t|dj|��j|�S(u8
    Internal, returns metrics record for a rectype
    umetricsu{0}_metrics(R2RR:(R4R5trecordt
metrics_id((s:/usr/lib/python2.7/site-packages/salt/modules/xapi_virt.pyt_get_metrics_record�s
cC@s8|}x+|D]#}||kr,||}q
dSq
W|S(u)
    Internal, get value from record
    N(R/(R>tkeystdatatkey((s:/usr/lib/python2.7/site-packages/salt/modules/xapi_virt.pyt_get_val�s

cC@sxt��i}|jj�}g}xF|D]>}|jj|�dtkr(|j|jj|��q(q(W|SWdQXdS(u�
    Return a list of virtual machine names on the minion

    CLI Example:

    .. code-block:: bash

        salt '*' virt.list_domains
    uis_control_domainN(R,tVMR3R:RRtget_name_label(R4thoststrett_host((s:/usr/lib/python2.7/site-packages/salt/modules/xapi_virt.pytlist_domains�s

 c@s�t�����fd�}i}|rO||�}|dk	r�|||<q�n?x<t�D]1}||�}|dk	rY||�||<qYqYW|SWdQXdS(u
    Return detailed information about the vms.

    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.

    CLI Example:

    .. code-block:: bash

        salt '*' virt.vm_info
    c@s�t�d|�}|tkr"tSt�d|�}i|dd6t|dg�d6|dd6t|�d6t|�d	6tt|d
g��d6t|d�d
6t|dg�d6S(NuVMuVCPUs_numberucpuu	VCPUs_maxumaxCPUuVCPUs_utilisationucputimeudisksunicsumemory_dynamic_maxumaxMemu
memory_actualumemupower_stateustate(R=RR@RDt	get_diskstget_nicstint(tvm_tvm_rectvm_metrics_rec(R4(s:/usr/lib/python2.7/site-packages/salt/modules/xapi_virt.pyt_info�s

N(R,R/RJ(RNRQtinfoRH((R4s:/usr/lib/python2.7/site-packages/salt/modules/xapi_virt.pytvm_info�s
c	C@srt��c}i}|r6t|d|�d||<|Sx+t�D] }t|d|�d||<q@W|SWdQXdS(u
    Return list of all the vms and 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.

    CLI Example:

    .. code-block:: bash

        salt '*' virt.vm_state <vm name>
    uVMupower_stateN(R,R=RJ(RNR4RR((s:/usr/lib/python2.7/site-packages/salt/modules/xapi_virt.pytvm_state�s
c
@swt��h}t|dt|d�d�}t|d|dd��t|d|�}�fd�}�fd�}�fd�}it|dd	g�d
6|�d6|�d6t|d
dg�d6t|ddg�d6t|d�ddd6t|ddg�d6|�d6t|d�ddd6t|d
dg�d6t|d
dg�d6t|d
dg�d6djt|dg��d6t|d g�d!6t|d"d#g�d#6t|d"d$g�d$6t|d"d%g�d%6t|d
d&g�d&6t|d
d'g�d'6t|d
d(g�d(6t|d
d)g�d)6t|d
d*g�d*6t|d
d+g�d+6}|SWd,QXd,S(-u�
    Return a dict with information about this node

    CLI Example:

    .. code-block:: bash

        salt '*' virt.node_info
    uhostiuhost_cpuu	host_CPUsc@sQg�D]"}d|krt�d�^q}|rIt|�t|�SdSdS(Nuspeedi(RMtsumtlen(thost_cpu_itt
cpu_speeds(thost_cpu_rec(s:/usr/lib/python2.7/site-packages/salt/modules/xapi_virt.pyt	getCpuMhzs
"c@s�r�dSdS(Nufeatures(((RY(s:/usr/lib/python2.7/site-packages/salt/modules/xapi_virt.pytgetCpuFeatures(sc@s>d}x1�D])}t�d�dkr
|d7}q
q
W|S(Niucpu_pooli(RV(tcntRW(RY(s:/usr/lib/python2.7/site-packages/salt/modules/xapi_virt.pytgetFreeCpuCount,s

ucpu_configurationunr_cpusucpucoresucpufeaturesucpumhzusoftware_versionumachineucpuarchuthreads_per_coreu
cputhreadsumemory_totaliu	phymemoryucores_per_socketucores_per_socketsu	free_cpusumemory_freeufree_memoryu	xen_majoru	xen_minoru	xen_extrau ucapabilitiesuxen_capsusched_policyu
xen_scheduleruother_configuxen_pagesizeuplatform_paramsuxen_commandlineu
xen_changesetucc_compileru
cc_compile_byucc_compile_domainucc_compile_dateuxend_config_formatN(R,R<R6R@RDRMtjoin(R4thost_recthost_metrics_recRZR[R]RR((RYs:/usr/lib/python2.7/site-packages/salt/modules/xapi_virt.pyt	node_infosb
	



c	C@s�t���}i}t|d|�}|tkr4tSxS|dD]G}t|d|�}i|dd6|dd6|dd6||d<q?W|SWd	QXd	S(
u�
    Return info about the network interfaces of a named vm

    CLI Example:

    .. code-block:: bash

        salt '*' virt.get_nics <vm name>
    uVMuVIFsuVIFuMACumacudeviceuMTUumtuN(R,R=RR<(RNR4tnicROtviftvif_rec((s:/usr/lib/python2.7/site-packages/salt/modules/xapi_virt.pyRLas
cC@sDg}t|�}|dkr"dSx|D]}|j|�q)W|S(u�
    Return a list off MAC addresses from the named vm

    CLI Example:

    .. code-block:: bash

        salt '*' virt.get_macs <vm name>
    N(RLR/R(RNtmacstnicsRb((s:/usr/lib/python2.7/site-packages/salt/modules/xapi_virt.pytget_macs|s

cC@s�t���}i}t|d|�}|tkr4tSxu|jj|�D]a}|jj|�}|skqGn|jj|�}i|dd6|dd6|dd6||<qGW|SWdQXdS(u
    Return the disks of a named vm

    CLI Example:

    .. code-block:: bash

        salt '*' virt.get_disks <vm name>
    uVMubackendudevice-typeutypeuprotocolN(R,R9RREtget_VBDstVBDt
get_devicetget_runtime_properties(RNR4tdisktvm_uuidtvbdtdevtprop((s:/usr/lib/python2.7/site-packages/salt/modules/xapi_virt.pyRK�s
c	C@s�t���}t|�dd}t|d|�}|tkrBtSy.|jj||�|jj||�tSWntk
r�tSXWdQXdS(u�
    Changes the amount of memory allocated to VM.

    Memory is to be specified in MB

    CLI Example:

    .. code-block:: bash

        salt '*' virt.setmem myvm 768
    iuVMN(	R,RMR9RREtset_memory_dynamic_max_livetset_memory_dynamic_min_livetTrueR%(RNtmemoryR4t
mem_targetRm((s:/usr/lib/python2.7/site-packages/salt/modules/xapi_virt.pytsetmem�s
c	C@sht��Y}t|d|�}|tkr.tSy|jj||�tSWntk
r]tSXWdQXdS(u�
    Changes the amount of vcpus allocated to VM.

    vcpus is an int representing the number to be assigned

    CLI Example:

    .. code-block:: bash

        salt '*' virt.setvcpus myvm 2
    uVMN(R,R9RREtset_VCPUs_number_liveRsR%(RNtvcpusR4Rm((s:/usr/lib/python2.7/site-packages/salt/modules/xapi_virt.pytsetvcpus�s
c
C@s�t���}t|d|�}|tkr.tSd�}|dkrR|d�}n|dj|��}y'|jj|dj|�|�tSWn7tk
r�tddjt	�|||�d	t�SXWd
QXd
S(u�
    Set which CPUs a VCPU can use.

    CLI Example:

    .. code-block:: bash

        salt 'foo' virt.vcpu_pin domU-id 2 1
        salt 'foo' virt.vcpu_pin domU-id 2 2-6
    uVMcS@sg}x�|jd�D]�}|dkr.qnd|kr�|jd�\}}x�tt|�t|�d�D]}|jt|��qoWq|ddkr�g|D]"}|t|d�kr�|^q�}q|jt|��qW|j�djtt|��S(Nu,uu-iiu^(tsplitRRMRtsortR^Rtstr(tcpulisttcpustctxtyti((s:/usr/lib/python2.7/site-packages/salt/modules/xapi_virt.pytcpu_make_map�s&2
uallu0-63u{0}u	cpumap{0}ucmd.runu{0} vcpu-pin {1} {2} {3}tpython_shellN(
R,R9RRREtadd_to_VCPUs_params_liveRsR%R!R1(RNtvcpuR~R4RmR�tcpumap((s:/usr/lib/python2.7/site-packages/salt/modules/xapi_virt.pytvcpu_pin�s 	
cC@st�dS(u�
    Return an int representing the amount of memory that has not been given
    to virtual machines on this node

    CLI Example:

    .. code-block:: bash

        salt '*' virt.freemem
    ufree_memory(Ra(((s:/usr/lib/python2.7/site-packages/salt/modules/xapi_virt.pytfreememscC@st�dS(u�
    Return an int representing the number of unallocated cpus on this
    hypervisor

    CLI Example:

    .. code-block:: bash

        salt '*' virt.freecpu
    u	free_cpus(Ra(((s:/usr/lib/python2.7/site-packages/salt/modules/xapi_virt.pytfreecpu%scC@sit�d6t�d6S(u�
    Return the node_info, vm_info and freemem

    CLI Example:

    .. code-block:: bash

        salt '*' virt.full_info
    u	node_infouvm_info(RaRS(((s:/usr/lib/python2.7/site-packages/salt/modules/xapi_virt.pyt	full_info3s
c	C@set��V}t|d|�}|tkr.tSy|jj|�tSWntk
rZtSXWdQXdS(u�
    Send a soft shutdown signal to the named vm

    CLI Example:

    .. code-block:: bash

        salt '*' virt.shutdown <vm name>
    uVMN(R,R9RREtclean_shutdownRsR%(RNR4Rm((s:/usr/lib/python2.7/site-packages/salt/modules/xapi_virt.pytshutdown@s

c	C@set��V}t|d|�}|tkr.tSy|jj|�tSWntk
rZtSXWdQXdS(uo
    Pause the named vm

    CLI Example:

    .. code-block:: bash

        salt '*' virt.pause <vm name>
    uVMN(R,R9RREtpauseRsR%(RNR4Rm((s:/usr/lib/python2.7/site-packages/salt/modules/xapi_virt.pyR�Us

c	C@set��V}t|d|�}|tkr.tSy|jj|�tSWntk
rZtSXWdQXdS(uq
    Resume the named vm

    CLI Example:

    .. code-block:: bash

        salt '*' virt.resume <vm name>
    uVMN(R,R9RREtunpauseRsR%(RNR4Rm((s:/usr/lib/python2.7/site-packages/salt/modules/xapi_virt.pytresumejs

cC@s#tddjt�|�dt�S(u�
    Start a defined domain

    CLI Example:

    .. code-block:: bash

        salt '*' virt.start <path to Xen cfg file>
    ucmd.runu{0} create {1}R�(R!RR1R(tconfig_((s:/usr/lib/python2.7/site-packages/salt/modules/xapi_virt.pytstartsc	C@set��V}t|d|�}|tkr.tSy|jj|�tSWntk
rZtSXWdQXdS(u~
    Reboot a domain via ACPI request

    CLI Example:

    .. code-block:: bash

        salt '*' virt.reboot <vm name>
    uVMN(R,R9RREtclean_rebootRsR%(RNR4Rm((s:/usr/lib/python2.7/site-packages/salt/modules/xapi_virt.pytreboot�s

c	C@set��V}t|d|�}|tkr.tSy|jj|�tSWntk
rZtSXWdQXdS(u�
    Reset a VM by emulating the reset button on a physical machine

    CLI Example:

    .. code-block:: bash

        salt '*' virt.reset <vm name>
    uVMN(R,R9RREthard_rebootRsR%(RNR4Rm((s:/usr/lib/python2.7/site-packages/salt/modules/xapi_virt.pytreset�s

ii����c
	C@s�t���}t|d|�}|tkr.tSi|d6|d6|d6|d6}	y'|jj||t|�|	�tSWntk
r�tSXWdQXdS(u�
    Migrates the virtual machine to another hypervisor

    CLI Example:

    .. code-block:: bash

        salt '*' virt.migrate <vm name> <target hypervisor> [live] [port] [node] [ssl] [change_home_server]

    Optional values:

    live
        Use live migration
    port
        Use a specified port
    node
        Use specified NUMA node on target
    ssl
        use ssl connection for migration
    change_home_server
        change home server for managed domains
    uVMuportunodeussluchange_home_serverN(R,R9RREtmigratetboolRsR%(
RNttargettlivetporttnodetssltchange_home_serverR4Rmtother_config((s:/usr/lib/python2.7/site-packages/salt/modules/xapi_virt.pyR��s

c	C@set��V}t|d|�}|tkr.tSy|jj|�tSWntk
rZtSXWdQXdS(u�
    Hard power down the virtual machine, this is equivalent to pulling the
    power

    CLI Example:

    .. code-block:: bash

        salt '*' virt.stop <vm name>
    uVMN(R,R9RREt
hard_shutdownRsR%(RNR4Rm((s:/usr/lib/python2.7/site-packages/salt/modules/xapi_virt.pytstop�s
cC@s�ytddkrtSWntk
r,tSXyGtjjjd��,}dtjjj|j	��krmtSWdQXWnt
tfk
r�tSXdtdtd�kS(	u�
    Returns a bool whether or not this node is a hypervisor of any kind

    CLI Example:

    .. code-block:: bash

        salt '*' virt.is_hyper
    uvirtual_subtypeuXen Dom0u
/proc/modulesuxen_Nuxenstoreucmd.runups(
RRtKeyErrorR
R-tfilestfopentstringutilst
to_unicodetreadtOSErrortIOErrorR!(tfp_((s:/usr/lib/python2.7/site-packages/salt/modules/xapi_virt.pytis_hyper�s

!c@smt��^��fd�}i}|r;||�||<|Sx!t�D]}||�||<qEW|SWdQXdS(u�
    Return cputime used by the vms on this hyper in a
    list of dicts:

    .. code-block:: python

        [
            'your-vm': {
                'cputime' <int>
                'cputime_percent' <int>
                },
            ...
            ]

    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.

    CLI Example:

    .. code-block:: bash

        salt '*' virt.vm_cputime
    c@s�t�d|�}t|d�}|tkr2tSt�d|�}t|d�}t|dd�}d}|r�d|||}nit|�d6td	j|��d
6S(NuVMu	host_CPUsuVCPUs_numberuVCPUs_utilisationu0igH�����z>ucputimeu{0:.0f}ucputime_percent(R=RVRR@RMR(RNR_t	host_cpusthost_metricsRxtcputimetcputime_percent(R4(s:/usr/lib/python2.7/site-packages/salt/modules/xapi_virt.pyRQ,sN(R,RJ(RNRQRR((R4s:/usr/lib/python2.7/site-packages/salt/modules/xapi_virt.pyt
vm_cputimesc@slt��]��fd�}i}|r:||�||<n$x!t�D]}||�||<qDW|SWdQXdS(uV
    Return combined network counters used by the vms on this hyper in a
    list of dicts:

    .. code-block:: python

        [
            'your-vm': {
                'io_read_kbs'           : 0,
                'io_total_read_kbs'     : 0,
                'io_total_write_kbs'    : 0,
                'io_write_kbs'          : 0
                },
            ...
            ]

    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.

    CLI Example:

    .. code-block:: bash

        salt '*' virt.vm_netstats
    c@s|i}t�d|�}|tkr(tSxM|dD]A}t�d|�}t�d|�||d<||dd=q3W|S(NuVMuVIFsuVIFudeviceulast_updated(R=RR<R@(RNRHRORcRd(R4(s:/usr/lib/python2.7/site-packages/salt/modules/xapi_virt.pyRQ`s	N(R,RJ(RNRQRR((R4s:/usr/lib/python2.7/site-packages/salt/modules/xapi_virt.pytvm_netstatsEs
c@slt��]��fd�}i}|r:||�||<n$x!t�D]}||�||<qDW|SWdQXdS(u�
    Return disk usage counters used by the vms on this hyper in a
    list of dicts:

    .. code-block:: python

        [
            'your-vm': {
                'io_read_kbs'   : 0,
                'io_write_kbs'  : 0
                },
            ...
            ]

    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.

    CLI Example:

    .. code-block:: bash

        salt '*' virt.vm_diskstats
    c@s�i}t�d|�}|tkr(tSxU�jj|�D]A}t�d|�}t�d|�||d<||dd=q;W|S(NuVMuVBDudeviceulast_updated(R9RRERhR<R@(RNRHRmRntvbd_rec(R4(s:/usr/lib/python2.7/site-packages/salt/modules/xapi_virt.pyRQ�s	N(R,RJ(RNRQRR((R4s:/usr/lib/python2.7/site-packages/salt/modules/xapi_virt.pytvm_diskstatsvs
(=t__doc__t
__future__RRRRt
contextlibRtsalt.ext.six.movesRRRRsRRRtsalt.utils.filesR
tsalt.utils.pathtsalt.utils.stringutilstsalt.modules.cmdmodtsalt.exceptionsRRRR tcontextmanagerR,R1R6R9R<R=R@RDRJR/RSRTRaRLRgRKRvRyR�R�R�R�R�R�R�R�R�R�R�R�R�R�R�R�(((s:/usr/lib/python2.7/site-packages/salt/modules/xapi_virt.pyt<module>sh


		'	
		
		
		
	+	U						7			
						(		21

Zerion Mini Shell 1.0