%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /usr/lib/python2.7/site-packages/salt/utils/
Upload File :
Create Path :
Current File : //usr/lib/python2.7/site-packages/salt/utils/virtualbox.pyc

�
���^c@@s�dZddlmZmZmZddlZddlZddlZddlZ	ddl
Z	ddlmZddl
jjZeje�ZddlmZddlmZeZyddlZeZWn$ek
r�dZejd�nXdaidd	d
ddd
ddgd6ddddddddddddgd6ZdrZ dsdtdudvdwdxdydzd{d|d}d~dd�d�d�d�d�d�d�d�d�d�d�d�d�d�d�gZ!e"e!�Z#e"e$e!��Z%dW�Z&dX�Z'dY�Z(dddZ�Z)ddddd[�Z*d\d]�Z+d\d^dd_�Z,dddd`�Z-da�Z.ddb�Z/ddddcdd�Z0de�Z1ddcdf�Z2ddcdg�Z3ddcdh�Z4dddddi�Z5dj�Z6dk�Z7dl�Z8dm�Z9dn�Z:do�Z;dp�Z<dq�Z=dS(�u�
Utilities to help make requests to virtualbox

The virtualbox SDK reference can be found at http://download.virtualbox.org/virtualbox/SDKRef.pdf

This code assumes vboxapi.py from VirtualBox distribution
being in PYTHONPATH, or installed system-wide
i(tabsolute_importtprint_functiontunicode_literalsN(twait_for(tsix(trangeuCouldn't import VirtualBox APIuidunameu
accessibleudescriptionugroupsu
memorySizeuOSTypeIdustateuIMachineuadapterTypeuslotuenabledu
MACAddressubridgedInterfaceuhostOnlyInterfaceuinternalNetworku
NATNetworku
genericDriverucableConnectedu	lineSpeeduINetworkAdapteruUnknownu*This state is unknown to us. Might be new?uNullu"Null value (never used by the API)u
PoweredOffu}The machine is not running and has no saved execution state; it has either never been started or been shut down successfully.uSavedu�The machine is not currently running, but the execution state of the machine has been saved to an external file when it was running, from where it can be resumed.u
Teleportedu�The machine was teleported to a different host (or process) and then powered off. Take care when powering it on again may corrupt resources it shares with the teleportation target (e.g. disk and network).uAbortedu�The process running the machine has terminated abnormally. This may indicate a crash of the VM process in host execution context, or the VM process has been terminated externally.uRunningu(The machine is currently being executed.uPausedu)Execution of the machine has been paused.uStucku}Execution of the machine has reached the 'Guru Meditation' condition. This indicates a severe error in the hypervisor itself.uTeleportingu�The machine is about to be teleported to a different host or process. It is possible to pause a machine in this state, but it will go to the TeleportingPausedVM state and it will not be possible to resume it again unless the teleportation fails.uLiveSnapshottingu!A live snapshot is being taken. The machine is running normally, but some of the runtime configuration options are inaccessible. Also, if paused while in this state it will transition to OnlineSnapshotting and it will not be resume the execution until the snapshot operation has completed.uStartinguJMachine is being started after powering it on from a zero execution state.uStoppingukMachine is being normally stopped powering it off, or after the guest OS has initiated a shutdown sequence.uSavingu0Machine is saving its execution state to a file.u	RestoringuqExecution state of the machine is being restored from a file after powering it on from the saved execution state.uTeleportingPausedVMu�The machine is being teleported to another host or process, but it is not running. This is the paused variant of the Teleporting state.u
TeleportingInu>Teleporting the machine state in from another host or process.uFaultTolerantSyncinguHThe machine is being synced with a fault tolerant VM running else-where.uDeletingSnapshotOnlineukLike DeletingSnapshot , but the merging of media is ongoing in the background while the machine is running.uDeletingSnapshotPausedulLike DeletingSnapshotOnline , but the machine was paused when the merging of differencing media was started.uOnlineSnapshottingufLike LiveSnapshotting , but the machine was paused when the merging of differencing media was started.uRestoringSnapshotuHA machine snapshot is being restored; this typically does not take long.uDeletingSnapshotu�A machine snapshot is being deleted; this can take a long time since this may require merging differencing media. This value indicates that the machine is not running while the snapshot is being deleted.u	SettingUpu'Lengthy setup operation is in progress.uSnapshottinguTaking an (offline) snapshot.uFirstOnlineuEPseudo-state: first online state (for use in relational expressions).u
LastOnlineuDPseudo-state: last online state (for use in relational expressions).uFirstTransientuHPseudo-state: first transient state (for use in relational expressions).u
LastTransientuGPseudo-state: last transient state (for use in relational expressions).cC@s>tdkr:tr:tjjjt�tjdd�antS(u�
    Creates a 'singleton' manager to communicate with a local virtualbox hypervisor.
    @return:
    @rtype: VirtualBoxManager
    N(	t_virtualboxManagertNonetHAS_LIBStsalttutilstcompattreloadtvboxapitVirtualBoxManager(((s9/usr/lib/python2.7/site-packages/salt/utils/virtualbox.pytvb_get_manager�scC@s8t�y
tj}Wntk
r3tj�}nX|S(um
    Needed for certain operations in the SDK e.g creating sessions
    @return:
    @rtype: IVirtualBox
    (RRtvboxtAttributeErrort
getVirtualBox(R((s9/usr/lib/python2.7/site-packages/salt/utils/virtualbox.pyt
vb_get_box�s

cC@s>t�j}gddgD]}|j|�^q}t|�S(uR
    Max number of slots any machine can have
    @return:
    @rtype: number
    ii(RtsystemPropertiestgetMaxNetworkAdapterstsum(tsyspropstadapter_typettotals((s9/usr/lib/python2.7/site-packages/salt/utils/virtualbox.pytvb_get_max_network_slots�s
"cC@s�|rt�j|�}ng}xmtt��D]\}yB|j|�}t|d�}|jd�|d<|j|�Wq1tk
r�q1Xq1W|S(u�
    A valid machine_name or a machine is needed to make this work!

    @param machine_name:
    @type machine_name: str
    @param machine:
    @type machine: IMachine
    @return: INetorkAdapter's converted to dicts
    @rtype: [dict]
    uINetworkAdapteruu
properties(	RtfindMachineRRtgetNetworkAdaptertvb_xpcom_to_attribute_dictt
getPropertiestappendt	Exception(tmachine_nametmachinetnetwork_adapterstitinetwork_adaptertnetwork_adapter((s9/usr/lib/python2.7/site-packages/salt/utils/virtualbox.pytvb_get_network_adapters�s
c
C@s=i|d6|d6|d6}ttd|d|dgd|�S(u�
    Wait until a machine has a network address to return or quit after the timeout

    @param timeout: in seconds
    @type timeout: float
    @param step: How regularly we want to check for ips (in seconds)
    @type step: float
    @param machine_name:
    @type machine_name: str
    @param machine:
    @type machine: IMachine
    @type wait_for_pattern: str
    @param wait_for_pattern:
    @type machine: str
    @return:
    @rtype: list
    umachine_nameumachineuwait_for_patternttimeouttsteptdefaulttfunc_kwargs(Rtvb_get_network_addresses(R(R)R!R"twait_for_patterntkwargs((s9/usr/lib/python2.7/site-packages/salt/utils/virtualbox.pytvb_wait_for_network_address�s

uUnlockedcC@s#ttjd|�}|j|kS(u�
    @param xp_session:
    @type xp_session: ISession from the Virtualbox API
    @param expected_state: The constant descriptor according to the docs
    @type expected_state: str
    @return:
    @rtype: bool
    u
SessionState_(tgetattrRt	constantststate(t
xp_sessiontexpected_statetstate_value((s9/usr/lib/python2.7/site-packages/salt/utils/virtualbox.pyt_check_session_state�s	i
c
C@s2||f}ttd|d|dtd|�dS(u�
    Waits until a session state has been reached, checking at regular intervals.

    @param xp_session:
    @type xp_session: ISession from the Virtualbox API
    @param state: The constant descriptor according to the docs
    @type state: str
    @param timeout: in seconds
    @type timeout: int | float
    @param step: Intervals at which the value is checked
    @type step: int | float
    @return: Did we reach the state?
    @rtype: bool
    R(R)R*t	func_argsN(RR6tFalse(R3R2R(R)targs((s9/usr/lib/python2.7/site-packages/salt/utils/virtualbox.pytvb_wait_for_session_state�sc	C@s`|rt�j|�}ng}tjd�|jtjjkrLtjd�|rz|j|�rztjd|�dS|jd�}|s�tjd|�dSyt
|�}xlt|�D]^}y2|jdj|��}|r�|j
|�nWq�tk
r}tj|j�q�Xq�WWqLtk
rH}tj|j�dSXntjd|�|S(	u.
    TODO distinguish between private and public addresses

    A valid machine_name or a machine is needed to make this work!

    !!!
    Guest prerequisite: GuestAddition
    !!!

    Thanks to Shrikant Havale for the StackOverflow answer http://stackoverflow.com/a/29335390

    More information on guest properties: https://www.virtualbox.org/manual/ch04.html#guestadd-guestprops

    @param machine_name:
    @type machine_name: str
    @param machine:
    @type machine: IMachine
    @return: All the IPv4 addresses we could get
    @rtype: str[]
    uchecking for power on:u
got power on:uwaiting for pattern:%s:u/VirtualBox/GuestInfo/Net/Countuwaiting for net count:%s:u#/VirtualBox/GuestInfo/Net/{0}/V4/IPureturning ip_addresses:%s:N(RRtlogtdebugR2RR1tMachineState_RunningtgetGuestPropertyValueRtintRtformatRR tmessaget
ValueError(	R!R"R-tip_addressest_total_slotsttotal_slotsR$taddresste((s9/usr/lib/python2.7/site-packages/salt/utils/virtualbox.pyR,
s6

cK@sAt�}|jt�d�}g|D]}t|d|�^q%S(u�
    Which machines does the hypervisor have
    @param kwargs: Passed to vb_xpcom_to_attribute_dict to filter the attributes
    @type kwargs: dict
    @return: Untreated dicts of the machines known to the hypervisor
    @rtype: [{}]
    umachinesuIMachine(RtgetArrayRR(R.tmanagertmachinesR"((s9/usr/lib/python2.7/site-packages/salt/utils/virtualbox.pytvb_list_machinesIs	cC@sjt�}tjd|�d}d}|jd|||d�}|j|�tjd|�t|d�S(u�
    Creates a machine on the virtualbox hypervisor

    TODO pass more params to customize machine creation
    @param name:
    @type name: str
    @return: Representation of the created machine
    @rtype: dict
    uCreate virtualbox machine %s uOtheruFinished creating %suIMachineN(RR;tinfoRt
createMachinetregisterMachineR(tnameRtgroupst
os_type_idtnew_machine((s9/usr/lib/python2.7/site-packages/salt/utils/virtualbox.pytvb_create_machineYs
		
i'cK@s�t�}tjd||�|j|�}d}d}|jd|||d�}	|j|	|d�}
|
j|�tjd||�|j|	�t	|	d�S(u@
    Tells virtualbox to create a VM by cloning from an existing one

    @param name: Name for the new VM
    @type name: str
    @param clone_from:
    @type clone_from: str
    @param timeout: maximum time in milliseconds to wait or -1 to wait indefinitely
    @type timeout: int
    @return dict of resulting VM
    u#Clone virtualbox machine %s from %suOtheruFinished cloning %s from %suIMachineN(
RR;RLRRRMtcloneTotwaitForCompletionRNR(ROt
clone_fromt
clone_modeR(R.Rtsource_machineRPRQRRtprogress((s9/usr/lib/python2.7/site-packages/salt/utils/virtualbox.pytvb_clone_vmss&			

cC@sHy|j|dd�SWn*tk
rC}tj|jdt�dSXdS(u�
    Helper to try and start machines

    @param machine:
    @type machine: IMachine
    @param session:
    @type session: ISession
    @return:
    @rtype: IProgress or None
    utexc_infoN(tlaunchVMProcessR R;R<RAtTrueR(R"tsessionRG((s9/usr/lib/python2.7/site-packages/salt/utils/virtualbox.pyt_start_machine�s
cK@stj�}|d}||}t�}|j|�}tj|�}tjd|t|j��zg||f}	t	t
d|d|	�}
|
s�|j|dd�}
n|tj�}|
j|d�Wdtj
|�X|tj�}t|d|�tjd|�t|d�S(	u�
    Tells Virtualbox to start up a VM.
    Blocking function!

    @param name:
    @type name: str
    @param timeout: Maximum time in milliseconds to wait or -1 to wait indefinitely
    @type timeout: int
    @return untreated dict of started VM
    i�uStarting machine %s in state %sR(R7uNuStarted machine %suIMachine(ttimeRRRtgetSessionObjectR;RLtvb_machinestate_to_strR2RR_R\RUtcloseMachineSessionR:R(ROR(R.t
start_timettimeout_in_secondstmax_timeRR"R^R9RYt	time_left((s9/usr/lib/python2.7/site-packages/salt/utils/virtualbox.pytvb_start_vm�s&

	cK@s�t�}|j|�}tjd|�tj|�}z&|j}|j�}|j|�Wdtj	|�t
|�Xtjd|t|j��t
|d�S(u
    Tells Virtualbox to stop a VM.
    This is a blocking function!

    @param name:
    @type name: str
    @param timeout: Maximum time in milliseconds to wait or -1 to wait indefinitely
    @type timeout: int
    @return untreated dict of stopped VM
    uStopping machine %sNuStopped machine %s is now %suIMachine(RRR;RLRtopenMachineSessiontconsolet	powerDownRURcR:RbR2R(ROR(R.RR"R^RjRY((s9/usr/lib/python2.7/site-packages/salt/utils/virtualbox.pyt
vb_stop_vm�s		
cC@sgt�}tjd|�|j|�}|jd�}|j|�}|j|�tjd|�dS(u�
    Attempts to get rid of a machine and all its files from the hypervisor
    @param name:
    @type name: str
    @param timeout int timeout in milliseconds
    uDestroying machine %siuFinished destroying machine %sN(RR;RLRt
unregistertdeleteConfigRU(ROR(RR"tfilesRY((s9/usr/lib/python2.7/site-packages/salt/utils/virtualbox.pytvb_destroy_machine�s	
cC@s|rJtjdj|�tj|��}|sJtjd|�t�Snt|pbt	j
|g��}|r�|j|�}n|r�|j|�}ng}xt|D]l}t
|t�r�|d}	|d}
|	t||	|
��f}n|t||d�f}|j|�q�Wt|�S(u�
    Attempts to build a dict from an XPCOM object.
    Attributes that don't exist in the object return an empty string.

    attribute_list = list of str or tuple(str,<a class>)

    e.g attributes=[('bad_attribute', list)] --> { 'bad_attribute': [] }

    @param xpcom:
    @type xpcom:
    @param interface_name: Which interface we will be converting from.
                           Without this it's best to specify the list of attributes you want
    @type interface_name: str
    @param attributes: Overrides the attributes used from XPCOM_ATTRIBUTES
    @type attributes: attribute_list
    @param excluded_attributes: Which should be excluded in the returned dict.
                                !!These take precedence over extra_attributes!!
    @type excluded_attributes: attribute_list
    @param extra_attributes: Which should be retrieved in addition those already being retrieved
    @type extra_attributes: attribute_list
    @return:
    @rtype: dict
    uXPCOM.+implementing {0}u7Interface %s is unknown and cannot be converted to dictiiu(tretsearchR@Rt	text_typeR;twarningtdicttsettXPCOM_ATTRIBUTEStgettuniont
differencet
isinstancettupleR0R(txpcomtinterface_namet
attributestexcluded_attributestextra_attributestmtinterface_attributestattribute_tuplest	attributetattribute_nametattribute_classtvalue((s9/usr/lib/python2.7/site-packages/salt/utils/virtualbox.pyRs&$



cC@s�|ji|jdd�d6|jdd�d6dj|jdd��d6t|�d6gd	6gd
6�d|kr�|d=n|S(u�
    Make machine presentable for outside world.

    !!!Modifies the input machine!!!

    @param machine:
    @type machine: dict
    @return: the modified input machine
    @rtype: dict
    uiduuimageu{0} MBu
memorySizeiusizeustateuprivate_ipsu
public_ips(tupdateRxR@tmachine_get_machinestate_str(R"((s9/usr/lib/python2.7/site-packages/salt/utils/virtualbox.pyttreat_machine_dict@s	

cC@st|�dS(u�
    Put a name to the state

    @param machinestate: from the machine state enum from XPCOM
    @type machinestate: int
    @return:
    @rtype: str
    i(tvb_machinestate_to_tuple(tmachinestate((s9/usr/lib/python2.7/site-packages/salt/utils/virtualbox.pyRbZs
cC@st|�dS(u�
    Describe the given state

    @param machinestate: from the machine state enum from XPCOM
    @type machinestate: int | str
    @return:
    @rtype: str
    i(R�(R�((s9/usr/lib/python2.7/site-packages/salt/utils/virtualbox.pytvb_machinestate_to_descriptiongs	cC@sjt|t�r$tj|t�}n-t|tj�rKtj|t�}nt}tj	j
j|dt�S(uw

    @param machinestate:
    @type machinestate: int | str
    @return:
    @rtype: tuple(<name>, <description>)
    tpreserve_tuples(
R{R?tMACHINE_STATES_ENUMRxtUNKNOWN_MACHINE_STATERtstring_typestMACHINE_STATESR	R
tdatatdecodeR](R�tret((s9/usr/lib/python2.7/site-packages/salt/utils/virtualbox.pyR�sscC@st|jd��S(Nustate(R�Rx(tmachinedict((s9/usr/lib/python2.7/site-packages/salt/utils/virtualbox.pytmachine_get_machinestate_tuple�scC@st|jd��S(Nustate(RbRx(R�((s9/usr/lib/python2.7/site-packages/salt/utils/virtualbox.pyR��scC@s�yt�}|j|�tSWn�tk
r�}t|jtj�rQ|j}nBt|d�r�tt	|d�tj�r�t	|d�}nd}d|j
d�kr�tj|�nt
SXdS(u�
    Checks in with the hypervisor to see if the machine with the given name is known
    @param name:
    @type name:
    @return:
    @rtype:
    umsguiu)Could not find a registered machine namedN(RRR]R R{RARR�thasattrR0tfindR;terrorR8(RORRGRA((s9/usr/lib/python2.7/site-packages/salt/utils/virtualbox.pytvb_machine_exists�s	
*cK@s(t�}|j|�}t|d|�S(u�
    Attempts to fetch a machine from Virtualbox and convert it to a dict

    @param name: The unique name of the machine
    @type name:
    @param kwargs: To be passed to vb_xpcom_to_attribute_dict
    @type kwargs:
    @return:
    @rtype: dict
    uIMachine(RRR(ROR.RR"((s9/usr/lib/python2.7/site-packages/salt/utils/virtualbox.pytvb_get_machine�s	(uUnknownu*This state is unknown to us. Might be new?(uNullu"Null value (never used by the API)(u
PoweredOffu}The machine is not running and has no saved execution state; it has either never been started or been shut down successfully.(uSavedu�The machine is not currently running, but the execution state of the machine has been saved to an external file when it was running, from where it can be resumed.(u
Teleportedu�The machine was teleported to a different host (or process) and then powered off. Take care when powering it on again may corrupt resources it shares with the teleportation target (e.g. disk and network).(uAbortedu�The process running the machine has terminated abnormally. This may indicate a crash of the VM process in host execution context, or the VM process has been terminated externally.(uRunningu(The machine is currently being executed.(uPausedu)Execution of the machine has been paused.(uStucku}Execution of the machine has reached the 'Guru Meditation' condition. This indicates a severe error in the hypervisor itself.(uTeleportingu�The machine is about to be teleported to a different host or process. It is possible to pause a machine in this state, but it will go to the TeleportingPausedVM state and it will not be possible to resume it again unless the teleportation fails.(uLiveSnapshottingu!A live snapshot is being taken. The machine is running normally, but some of the runtime configuration options are inaccessible. Also, if paused while in this state it will transition to OnlineSnapshotting and it will not be resume the execution until the snapshot operation has completed.(uStartinguJMachine is being started after powering it on from a zero execution state.(uStoppingukMachine is being normally stopped powering it off, or after the guest OS has initiated a shutdown sequence.(uSavingu0Machine is saving its execution state to a file.(u	RestoringuqExecution state of the machine is being restored from a file after powering it on from the saved execution state.(uTeleportingPausedVMu�The machine is being teleported to another host or process, but it is not running. This is the paused variant of the Teleporting state.(u
TeleportingInu>Teleporting the machine state in from another host or process.(uFaultTolerantSyncinguHThe machine is being synced with a fault tolerant VM running else-where.(uDeletingSnapshotOnlineukLike DeletingSnapshot , but the merging of media is ongoing in the background while the machine is running.(uDeletingSnapshotPausedulLike DeletingSnapshotOnline , but the machine was paused when the merging of differencing media was started.(uOnlineSnapshottingufLike LiveSnapshotting , but the machine was paused when the merging of differencing media was started.(uRestoringSnapshotuHA machine snapshot is being restored; this typically does not take long.(uDeletingSnapshotu�A machine snapshot is being deleted; this can take a long time since this may require merging differencing media. This value indicates that the machine is not running while the snapshot is being deleted.(u	SettingUpu'Lengthy setup operation is in progress.(uSnapshottinguTaking an (offline) snapshot.(uFirstOnlineuEPseudo-state: first online state (for use in relational expressions).(u
LastOnlineuDPseudo-state: last online state (for use in relational expressions).(uFirstTransientuHPseudo-state: first transient state (for use in relational expressions).(u
LastTransientuGPseudo-state: last transient state (for use in relational expressions).(>t__doc__t
__future__RRRtloggingRqR`tsalt.utils.compatR	tsalt.utils.datatsalt.utils.timeoutRtsalt.ext.sixtextRt	getLoggert__name__R;tsalt.exttsalt.ext.six.movesRR8RR
R]tImportErrorRRttraceRRwR�tMACHINE_STATE_LISTRuR�t	enumerateR�RRRR'R/R6R:R,RKRSRZR_RhRlRpRR�RbR�R�R�R�R�R�(((s9/usr/lib/python2.7/site-packages/salt/utils/virtualbox.pyt<module>	s�



				
<	+	*4		
					

Zerion Mini Shell 1.0