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/utils/__pycache__/virtualbox.cpython-310.pyc
o

�N�g�Y�@s�dZddlZddlZddlZddlZddlZddlmZe�	e
�ZdZzddl
Z
dZWney;dZe�d�Ynwda	gd�gd�d	�Zd
Zgd�Zee�Z	eee��Zdd
�Zdd�Zdd�Zd?dd�Z	d@dd�ZdAdd�ZdBdd�ZdCdd�Z dd�Z!dDd d!�Z"dEd#d$�Z#d%d&�Z$dFd'd(�Z%dFd)d*�Z&dFd+d,�Z'				d@d-d.�Z(d/d0�Z)d1d2�Z*d3d4�Z+d5d6�Z,d7d8�Z-d9d:�Z.d;d<�Z/d=d>�Z0dS)Gz�
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
�N)�wait_forFTzCouldn't import VirtualBox API)�id�nameZ
accessible�description�groups�
memorySizeZOSTypeId�state)ZadapterTypeZslot�enabledZ
MACAddressZbridgedInterfaceZhostOnlyInterfaceZinternalNetworkZ
NATNetworkZ
genericDriverZcableConnected�	lineSpeedr
)�IMachine�INetworkAdapter)ZUnknownz*This state is unknown to us. Might be new?))ZNullz"Null value (never used by the API))Z
PoweredOffz}The machine is not running and has no saved execution state; it has either never been started or been shut down successfully.)ZSavedz�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.)Z
Teleportedz�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).)ZAbortedz�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.)ZRunningz(The machine is currently being executed.)ZPausedz)Execution of the machine has been paused.)ZStuckz}Execution of the machine has reached the 'Guru Meditation' condition. This indicates a severe error in the hypervisor itself.)ZTeleportingz�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.)ZLiveSnapshottinga!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.)ZStartingzJMachine is being started after powering it on from a zero execution state.)ZStoppingzkMachine is being normally stopped powering it off, or after the guest OS has initiated a shutdown sequence.)ZSavingz0Machine is saving its execution state to a file.)Z	RestoringzqExecution state of the machine is being restored from a file after powering it on from the saved execution state.)ZTeleportingPausedVMz�The machine is being teleported to another host or process, but it is not running. This is the paused variant of the Teleporting state.)Z
TeleportingInz>Teleporting the machine state in from another host or process.)ZFaultTolerantSyncingzHThe machine is being synced with a fault tolerant VM running else-where.)ZDeletingSnapshotOnlinezkLike DeletingSnapshot , but the merging of media is ongoing in the background while the machine is running.)ZDeletingSnapshotPausedzlLike DeletingSnapshotOnline , but the machine was paused when the merging of differencing media was started.)ZOnlineSnapshottingzfLike LiveSnapshotting , but the machine was paused when the merging of differencing media was started.)ZRestoringSnapshotzHA machine snapshot is being restored; this typically does not take long.)ZDeletingSnapshotz�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.)Z	SettingUpz'Lengthy setup operation is in progress.)ZSnapshottingzTaking an (offline) snapshot.)ZFirstOnlinezEPseudo-state: first online state (for use in relational expressions).)Z
LastOnlinezDPseudo-state: last online state (for use in relational expressions).)ZFirstTransientzHPseudo-state: first transient state (for use in relational expressions).)Z
LastTransientzGPseudo-state: last transient state (for use in relational expressions).cCs*tdurtrtjj�t�t�dd�atS)z�
    Creates a 'singleton' manager to communicate with a local virtualbox hypervisor.
    @return:
    @rtype: VirtualBoxManager
    N)�_virtualboxManager�HAS_LIBS�salt�utils�compat�reload�vboxapi�VirtualBoxManager�rr�I/opt/saltstack/salt/lib/python3.10/site-packages/salt/utils/virtualbox.py�vb_get_manager�srcCs0t�ztj}W|Styt��}Y|Sw)zm
    Needed for certain operations in the SDK e.g creating sessions
    @return:
    @rtype: IVirtualBox
    )rr
�vbox�AttributeErrorZ
getVirtualBox)rrrr�
vb_get_box�s�
�rcs"t�j��fdd�dD�}t|�S)zR
    Max number of slots any machine can have
    @return:
    @rtype: number
    csg|]}��|��qSr)ZgetMaxNetworkAdapters)�.0Zadapter_type�Zsyspropsrr�
<listcomp>�s��z,vb_get_max_network_slots.<locals>.<listcomp>)��)rZsystemProperties�sum)Ztotalsrrr�vb_get_max_network_slots�s

�r!c	Csj|rt��|�}g}tt��D]#}z|�|�}t|d�}|�d�|d<|�|�Wqty2Yqw|S)z�
    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]
    r�Z
properties)	r�findMachine�ranger!ZgetNetworkAdapter�vb_xpcom_to_attribute_dictZ
getProperties�append�	Exception)�machine_name�machineZnetwork_adapters�iZinetwork_adapterZnetwork_adapterrrr�vb_get_network_adapters�s
��r+cCs|||d�}tt||g|d�S)a�
    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
    )r(r)�wait_for_pattern)�timeout�step�defaultZfunc_kwargs)r�vb_get_network_addresses)r-r.r(r)r,�kwargsrrr�vb_wait_for_network_addresss��r2�UnlockedcCsttjd|�}|j|kS)z�
    @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
    Z
SessionState_)�getattrr
�	constantsr)�
xp_sessionZexpected_stateZstate_valuerrr�_check_session_state-s	
�
r7�
cCs||f}tt||d|d�dS)a�
    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
    F)r-r.r/�	func_argsN)rr7)r6rr-r.�argsrrr�vb_wait_for_session_state<s

�r;c	Cs$|rt��|�}g}t�d�|jtjjkr�t�d�|r*|�|�s*t�d|�dS|�d�}|s9t�d|�dSz7t	|�}t
|�D],}z|�d|�d��}|rU|�|�WqBtyn}zt�|j
�WYd}~qBd}~wwWnty�}z
t�|j
�WYd}~dSd}~wwt�d	|�|S)
a.
    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[]
    zchecking for power on:z
got power on:zwaiting for pattern:%s:Nz/VirtualBox/GuestInfo/Net/Countzwaiting for net count:%s:z/VirtualBox/GuestInfo/Net/z/V4/IPzreturning ip_addresses:%s:)rr#�log�debugrr
r5ZMachineState_RunningZgetGuestPropertyValue�intr$r&r'�message�
ValueError)	r(r)r,Zip_addressesZ_total_slotsZtotal_slotsr*�address�errrr0QsF



�
����	��r0cs&t�}|�t�d�}�fdd�|D�S)z�
    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: [{}]
    �machinescsg|]}t|dfi����qS)r)r%)rr)�r1rrr�s��z$vb_list_machines.<locals>.<listcomp>)rZgetArrayr)r1�managerrCrrDr�vb_list_machines�s

�rFcCsLt�}t�d|�d}d}|�d|||d�}|�|�t�d|�t|d�S)z�
    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
    zCreate virtualbox machine %s N�OtherzFinished creating %sr)rr<�info�
createMachine�registerMachiner%)rrr�
os_type_id�new_machinerrr�vb_create_machine�s

�

rM�'cKsrt�}t�d||�|�|�}d}d}|�d|||d�}	|�|	|d�}
|
�|�t�d||�|�|	�t|	d�S)a@
    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
    z#Clone virtualbox machine %s from %sNrGzFinished cloning %s from %sr)	rr<rHr#rIZcloneTo�waitForCompletionrJr%)rZ
clone_fromZ
clone_moder-r1rZsource_machinerrKrL�progressrrr�vb_clone_vm�s

��


rQc
CsHz|�|dd�WSty#}ztj|jdd�WYd}~dSd}~ww)z�
    Helper to try and start machines

    @param machine:
    @type machine: IMachine
    @param session:
    @type session: ISession
    @return:
    @rtype: IProgress or None
    r"T)�exc_infoN)�launchVMProcessr'r<r=r?)r)�sessionrBrrr�_start_machine�s��rUc	Ks�t��}|d}||}t�}|�|�}t�|�}t�d|t|j��z(||f}	t	t
||	d�}
|
s8|�|dd�}
|t��}|
�|d�Wt�
|�nt�
|�w|t��}t||d�t�d|�t|d�S)z�
    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�zStarting machine %s in state %s)r-r9r")r-zStarted machine %sr)�timerr#r
ZgetSessionObjectr<rH�vb_machinestate_to_strrrrUrSrO�closeMachineSessionr;r%)rr-r1Z
start_timeZtimeout_in_secondsZmax_timerr)rTr:rPZ	time_leftrrr�vb_start_vm�s*

�
rYc	Ks�t�}|�|�}t�d|�t�|�}z|j}|��}|�|�Wt�	|�t
|�n
t�	|�t
|�wt�d|t|j��t
|d�S)a
    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
    zStopping machine %szStopped machine %s is now %sr)rr#r<rHr
ZopenMachineSession�consoleZ	powerDownrOrXr;rWrr%)rr-r1rr)rTrZrPrrr�
vb_stop_vms 




�
�
r[cCsJt�}t�d|�|�|�}|�d�}|�|�}|�|�t�d|�dS)z�
    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
    zDestroying machine %srzFinished destroying machine %sN)rr<rHr#�
unregisterZdeleteConfigrO)rr-rr)�filesrPrrr�vb_destroy_machine2s



r^cCs�|rt�d|��t|��}|st�d|�t�St|p t�|g��}|r)|�	|�}|r0|�
|�}g}|D]&}t|t�rM|d}	|d}
|	t
||	|
��f}n|t
||d�f}|�|�q4t|�S)a�
    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
    zXPCOM.+implementing z7Interface %s is unknown and cannot be converted to dictrrr")�re�search�strr<�warning�dict�set�XPCOM_ATTRIBUTES�get�union�
difference�
isinstance�tupler4r&)ZxpcomZinterface_nameZ
attributesZexcluded_attributesZextra_attributes�mZinterface_attributesZattribute_tuplesZ	attributeZattribute_nameZattribute_class�valuerrrr%Bs,�


r%c
CsL|�|�dd�|�dd�d�|�dd��t|�ggd��d|vr$|d=|S)z�
    Make machine presentable for outside world.

    !!!Modifies the input machine!!!

    @param machine:
    @type machine: dict
    @return: the modified input machine
    @rtype: dict
    rr"�imagez{} MBrr)rrm�sizerZprivate_ipsZ
public_ips)�updaterf�format�machine_get_machinestate_str)r)rrr�treat_machine_dict~s

��rrcC�t|�dS)z�
    Put a name to the state

    @param machinestate: from the machine state enum from XPCOM
    @type machinestate: int
    @return:
    @rtype: str
    r��vb_machinestate_to_tuple��machinestaterrrrW�s
rWcCrs)z�
    Describe the given state

    @param machinestate: from the machine state enum from XPCOM
    @type machinestate: int | str
    @return:
    @rtype: str
    rrtrvrrr�vb_machinestate_to_description�s	rxcCsFt|t�rt�|t�}nt|t�rt�|t�}nt}tjj	j
|dd�S)zw

    @param machinestate:
    @type machinestate: int | str
    @return:
    @rtype: tuple(<name>, <description>)
    T)Zpreserve_tuples)rir>�MACHINE_STATES_ENUMrf�UNKNOWN_MACHINE_STATEra�MACHINE_STATESrr�data�decode)rw�retrrrru�s

rucC�t|�d��S�Nr)rurf�Zmachinedictrrr�machine_get_machinestate_tuple��r�cCrr�)rWrfr�rrrrq�r�rqc
Cs�zt�}|�|�WdStyI}z2t|jt�r|j}nt|d�r0tt|d�t�r0t|d�}nd}d|�d�kr>t	�
|�WYd}~dSd}~ww)z�
    Checks in with the hypervisor to see if the machine with the given name is known
    @param name:
    @type name:
    @return:
    @rtype:
    T�msgr"rz)Could not find a registered machine namedNF)rr#r'rir?ra�hasattrr4�findr<�error)rrrBr?rrr�vb_machine_exists�s

��r�cKs"t�}|�|�}t|dfi|��S)z�
    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
    r)rr#r%)rr1rr)rrr�vb_get_machine�s
r�)NN)NNNN)r3)r3r8N)NNN)N)NNrrN)NrN)1�__doc__�loggingr_rVZsalt.utils.compatrZsalt.utils.dataZsalt.utils.timeoutr�	getLogger�__name__r<rr�ImportErrorrZtracer
rerzZMACHINE_STATE_LISTrcr{�	enumerateryrrr!r+r2r7r;r0rFrMrQrUrYr[r^r%rrrWrxrur�rqr�r�rrrr�<module>sn	
�
�s

�
"

>

#

,

�<