Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

virt.vm_info and other functions in virt module #52561

Closed
wants to merge 10 commits into from
231 changes: 166 additions & 65 deletions salt/modules/virt.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,57 +324,52 @@ def _parse_qemu_img_info(info):

def _get_uuid(dom):
'''
Return a uuid from the named vm

CLI Example:

.. code-block:: bash

salt '*' virt.get_uuid <domain>
Get uuid from a libvirt domain object.
'''
return ElementTree.fromstring(get_xml(dom)).find('uuid').text
uuid = ElementTree.fromstring(dom.XMLDesc(0)).find('uuid').text
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are all these having to be redefined?

What changed? This has worked in the past. Do you have a reason to not use the get_xml() function that already exists, and looks like it is doing the same thing?

I want to make sure we are not breaking other peoples environments.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @gtmanfred, I'm pretty sure this will not break other peoples environments, few reasons:

  • the previous version of the code used to reference dom as string a "name" while it should be an actual domain object.
  • all the other _get functions are expecting a domain object not a string and many functions are raising exceptions because of treating an object as a string, then failing with ERROR: The VM "<libvirt.virDomain object at 0x7f24e9e73198>" is not present

take a look at this one here for example:

    def _info(dom):
        '''
        Compute the infos of a domain
        '''
        raw = dom.info()
        return {'cpu': raw[3],
                'cputime': int(raw[4]),
                'disks': _get_disks(dom),
                'graphics': _get_graphics(dom),
                'nics': _get_nics(dom),
                'uuid': _get_uuid(dom),
                'loader': _get_loader(dom),
                'on_crash': _get_on_crash(dom),
                'on_reboot': _get_on_reboot(dom),
                'on_poweroff': _get_on_poweroff(dom),
                'maxMem': int(raw[1]),
                'mem': int(raw[2]),
                'state': VIRT_STATE_NAME_MAP.get(raw[0], 'unknown')}

dom is a domain object and not a domain name string.


return uuid


def _get_on_poweroff(dom):
'''
Return `on_poweroff` setting from the named vm

CLI Example:

.. code-block:: bash

salt '*' virt.get_on_restart <domain>
Get on_poweroff from a libvirt domain object.
'''
node = ElementTree.fromstring(get_xml(dom)).find('on_poweroff')
node = ElementTree.fromstring(dom.XMLDesc(0)).find('on_poweroff')

return node.text if node is not None else ''


def _get_on_reboot(dom):
'''
Return `on_reboot` setting from the named vm
Get on_reboot from a libvirt domain object.
'''
node = ElementTree.fromstring(dom.XMLDesc(0)).find('on_reboot')

CLI Example:
return node.text if node is not None else ''

.. code-block:: bash

salt '*' virt.get_on_reboot <domain>
def _get_on_crash(dom):
'''
Get on_crash from a libvirt domain object.
'''
node = ElementTree.fromstring(get_xml(dom)).find('on_reboot')
node = ElementTree.fromstring(dom.XMLDesc(0)).find('on_crash')

return node.text if node is not None else ''


def _get_on_crash(dom):
def _get_macs(dom):
'''
Return `on_crash` setting from the named vm

CLI Example:
Get mac addresses (macs) from a libvirt domain object.
'''
return [node.get('address') for node in dom.XMLDesc(0).findall('devices/interface/mac')]

.. code-block:: bash

salt '*' virt.get_on_crash <domain>
def _get_disk_devs(dom):
'''
node = ElementTree.fromstring(get_xml(dom)).find('on_crash')
return node.text if node is not None else ''
Get the disk devices names from a libvirt domain object.
'''
return [target.get('dev') for target in dom.XMLDesc(0).findall('devices/disk/target')]


def _get_nics(dom):
Expand Down Expand Up @@ -2345,8 +2340,11 @@ def get_macs(vm_, **kwargs):

salt '*' virt.get_macs <domain>
'''
doc = ElementTree.fromstring(get_xml(vm_, **kwargs))
return [node.get('address') for node in doc.findall('devices/interface/mac')]
conn = __get_conn(**kwargs)
macs = _get_macs(_get_domain(conn, vm_))
conn.close()

return macs


def get_graphics(vm_, **kwargs):
Expand Down Expand Up @@ -2426,6 +2424,115 @@ def get_disks(vm_, **kwargs):
return disks


def get_uuid(vm_, **kwargs):
'''
Return a uuid from the named vm

:param vm_: name of the domain
:param connection: libvirt connection URI, overriding defaults

.. versionadded:: 2019.2.0
:param username: username to connect with, overriding defaults

.. versionadded:: 2019.2.0
:param password: password to connect with, overriding defaults

.. versionadded:: 2019.2.0

CLI Example:

.. code-block:: bash

salt '*' virt.get_uuid <domain>
'''
conn = __get_conn(**kwargs)
uuid = _get_uuid(_get_domain(conn, vm_))

return uuid


def get_on_poweroff(vm_, **kwargs):
'''
Return a on_poweroff from the named vm

:param vm_: name of the domain
:param connection: libvirt connection URI, overriding defaults

.. versionadded:: 2019.2.0
:param username: username to connect with, overriding defaults

.. versionadded:: 2019.2.0
:param password: password to connect with, overriding defaults

.. versionadded:: 2019.2.0


CLI Example:

.. code-block:: bash

salt '*' virt.get_on_poweroff <domain>
'''
conn = __get_conn(**kwargs)
on_poweroff = _get_on_poweroff(_get_domain(conn, vm_))

return on_poweroff


def get_on_reboot(vm_, **kwargs):
'''
Return a on_reboot from the named vm

:param vm_: name of the domain
:param connection: libvirt connection URI, overriding defaults

.. versionadded:: 2019.2.0
:param username: username to connect with, overriding defaults

.. versionadded:: 2019.2.0
:param password: password to connect with, overriding defaults

.. versionadded:: 2019.2.0

CLI Example:

.. code-block:: bash

salt '*' virt.get_on_reboot <domain>
'''
conn = __get_conn(**kwargs)
on_reboot = _get_on_reboot(_get_domain(conn, vm_))

return on_reboot


def get_on_crash(vm_, **kwargs):
'''
Return a on_crash from the named vm

:param vm_: name of the domain
:param connection: libvirt connection URI, overriding defaults

.. versionadded:: 2019.2.0
:param username: username to connect with, overriding defaults

.. versionadded:: 2019.2.0
:param password: password to connect with, overriding defaults

.. versionadded:: 2019.2.0

CLI Example:

.. code-block:: bash

salt '*' virt.get_on_crash <domain>
'''
conn = __get_conn(**kwargs)
on_crash = _get_on_crash(_get_domain(conn, vm_))

return on_crash


def setmem(vm_, memory, config=False, **kwargs):
'''
Changes the amount of memory allocated to VM. The VM must be shutdown
Expand Down Expand Up @@ -2519,6 +2626,33 @@ def setvcpus(vm_, vcpus, config=False, **kwargs):
return ret1 == ret2 == 0


def get_xml(vm_, **kwargs):
'''
Returns the XML for a given vm

:param vm_: domain name
:param connection: libvirt connection URI, overriding defaults

.. versionadded:: 2019.2.0
:param username: username to connect with, overriding defaults

.. versionadded:: 2019.2.0
:param password: password to connect with, overriding defaults

.. versionadded:: 2019.2.0

CLI Example:

.. code-block:: bash

salt '*' virt.get_xml <domain>
'''
conn = __get_conn(**kwargs)
xml_desc = _get_domain(conn, vm_).XMLDesc(0)
conn.close()
return xml_desc


def _freemem(conn):
'''
Internal variant of freemem taking a libvirt connection as parameter
Expand Down Expand Up @@ -2626,33 +2760,6 @@ def full_info(**kwargs):
return info


def get_xml(vm_, **kwargs):
'''
Returns the XML for a given vm

:param vm_: domain name
:param connection: libvirt connection URI, overriding defaults

.. versionadded:: 2019.2.0
:param username: username to connect with, overriding defaults

.. versionadded:: 2019.2.0
:param password: password to connect with, overriding defaults

.. versionadded:: 2019.2.0

CLI Example:

.. code-block:: bash

salt '*' virt.get_xml <domain>
'''
conn = __get_conn(**kwargs)
xml_desc = _get_domain(conn, vm_).XMLDesc(0)
conn.close()
return xml_desc


def get_profiles(hypervisor=None, **kwargs):
'''
Return the virt profiles for hypervisor.
Expand Down Expand Up @@ -3747,20 +3854,14 @@ def vm_diskstats(vm_=None, **kwargs):

salt '*' virt.vm_blockstats
'''
def get_disk_devs(dom):
'''
Extract the disk devices names from the domain XML definition
'''
doc = ElementTree.fromstring(get_xml(dom, **kwargs))
return [target.get('dev') for target in doc.findall('devices/disk/target')]

def _info(dom):
'''
Compute the disk stats of a domain
'''
# Do not use get_disks, since it uses qemu-img and is very slow
# and unsuitable for any sort of real time statistics
disks = get_disk_devs(dom)
disks = _get_disk_devs(dom)
ret = {'rd_req': 0,
'rd_bytes': 0,
'wr_req': 0,
Expand Down