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

Fluorine backport of pr #49510 #49540

Merged
merged 2 commits into from
Sep 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 33 additions & 14 deletions salt/modules/virt.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,14 +243,6 @@ def __get_conn(**kwargs):
return conn


def _get_domain_types(**kwargs):
'''
Return the list of possible values for the <domain> type attribute.
'''
caps = capabilities(**kwargs)
return sorted(set([x for y in [guest['arch']['domains'].keys() for guest in caps['guests']] for x in y]))


def _get_domain(conn, *vms, **kwargs):
'''
Return a domain object for the named VM or return domain object for all VMs.
Expand Down Expand Up @@ -538,6 +530,8 @@ def _gen_xml(name,
diskp,
nicp,
hypervisor,
os_type,
arch,
graphics=None,
**kwargs):
'''
Expand Down Expand Up @@ -589,16 +583,17 @@ def _gen_xml(name,
context['console'] = True

context['disks'] = {}
disk_bus_map = {'virtio': 'vd', 'xen': 'xvd', 'fdc': 'fd', 'ide': 'hd'}
for i, disk in enumerate(diskp):
context['disks'][disk['name']] = {}
context['disks'][disk['name']]['file_name'] = disk['filename']
context['disks'][disk['name']]['source_file'] = disk['source_file']
if hypervisor in ['qemu', 'kvm']:
context['disks'][disk['name']]['target_dev'] = 'vd{0}'.format(string.ascii_lowercase[i])
prefix = disk_bus_map.get(disk['model'], 'sd')
context['disks'][disk['name']]['target_dev'] = '{0}{1}'.format(prefix, string.ascii_lowercase[i])
if hypervisor in ['qemu', 'kvm', 'xen']:
context['disks'][disk['name']]['address'] = False
context['disks'][disk['name']]['driver'] = True
elif hypervisor in ['esxi', 'vmware']:
context['disks'][disk['name']]['target_dev'] = 'sd{0}'.format(string.ascii_lowercase[i])
context['disks'][disk['name']]['address'] = True
context['disks'][disk['name']]['driver'] = False
context['disks'][disk['name']]['disk_bus'] = disk['model']
Expand All @@ -607,6 +602,9 @@ def _gen_xml(name,

context['nics'] = nicp

context['os_type'] = os_type
context['arch'] = arch

fn_ = 'libvirt_domain.jinja'
try:
template = JINJA.get_template(fn_)
Expand Down Expand Up @@ -1107,6 +1105,8 @@ def init(name,
enable_vnc=False,
enable_qcow=False,
graphics=None,
os_type=None,
arch=None,
**kwargs):
'''
Initialize a new vm
Expand Down Expand Up @@ -1165,6 +1165,16 @@ def init(name,
Dictionary providing details on the graphics device to create. (Default: ``None``)
See :ref:`init-graphics-def` for more details on the possible values.

.. versionadded:: Fluorine
:param os_type:
type of virtualization as found in the ``//os/type`` element of the libvirt definition.
The default value is taken from the host capabilities, with a preference for ``hvm``.

.. versionadded:: Fluorine
:param arch:
architecture of the virtual machine. The default value is taken from the host capabilities,
but ``x86_64`` is prefed over ``i686``.

.. versionadded:: Fluorine
:param enable_qcow:
``True`` to create a QCOW2 overlay image, rather than copying the image
Expand Down Expand Up @@ -1327,7 +1337,10 @@ def init(name,
virt:
images: /data/my/vm/images/
'''
hypervisors = _get_domain_types(**kwargs)
caps = capabilities(**kwargs)
os_types = sorted(set([guest['os_type'] for guest in caps['guests']]))
arches = sorted(set([guest['arch']['name'] for guest in caps['guests']]))
hypervisors = sorted(set([x for y in [guest['arch']['domains'].keys() for guest in caps['guests']] for x in y]))
hypervisor = __salt__['config.get']('libvirt:hypervisor', hypervisor)
if hypervisor is not None:
salt.utils.versions.warn_until(
Expand Down Expand Up @@ -1448,7 +1461,10 @@ def init(name,
'\'enable_vnc\' will be removed in {version}. ')
graphics = {'type': 'vnc'}

vm_xml = _gen_xml(name, cpu, mem, diskp, nicp, hypervisor, graphics, **kwargs)
os_type = 'hvm' if 'hvm' in os_types else os_types[0]
arch = 'x86_64' if 'x86_64' in arches else arches[0]

vm_xml = _gen_xml(name, cpu, mem, diskp, nicp, hypervisor, os_type, arch, graphics, **kwargs)
conn = __get_conn(**kwargs)
try:
conn.defineXML(vm_xml)
Expand Down Expand Up @@ -1692,6 +1708,8 @@ def update(name,
_disk_profile(disk_profile, hypervisor, disks, name, **kwargs),
_get_merged_nics(hypervisor, nic_profile, interfaces),
hypervisor,
domain.OSType(),
desc.find('.//os/type').get('arch'),
graphics,
**kwargs))

Expand Down Expand Up @@ -2378,7 +2396,8 @@ def get_profiles(hypervisor=None, **kwargs):
'''
ret = {}

hypervisors = _get_domain_types(**kwargs)
caps = capabilities(**kwargs)
hypervisors = sorted(set([x for y in [guest['arch']['domains'].keys() for guest in caps['guests']] for x in y]))
default_hypervisor = 'kvm' if 'kvm' in hypervisors else hypervisors[0]

if not hypervisor:
Expand Down
17 changes: 16 additions & 1 deletion salt/states/virt.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,9 @@ def running(name,
update=False,
connection=None,
username=None,
password=None):
password=None,
os_type=None,
arch=None):
'''
Starts an existing guest, or defines and starts a new VM with specified arguments.

Expand Down Expand Up @@ -326,6 +328,17 @@ def running(name,
:param password: password to connect with, overriding defaults

.. versionadded:: Fluorine
:param os_type:
type of virtualization as found in the ``//os/type`` element of the libvirt definition.
The default value is taken from the host capabilities, with a preference for ``hvm``.
Only used when creating a new virtual machine.

.. versionadded:: Neon
:param arch:
architecture of the virtual machine. The default value is taken from the host capabilities,
but ``x86_64`` is prefed over ``i686``. Only used when creating a new virtual machine.

.. versionadded:: Neon

.. rubric:: Example States

Expand Down Expand Up @@ -429,6 +442,8 @@ def running(name,
__salt__['virt.init'](name,
cpu=cpu,
mem=mem,
os_type=os_type,
arch=arch,
image=image,
hypervisor=vm_type,
disk=disk_profile,
Expand Down
2 changes: 1 addition & 1 deletion salt/templates/virt/libvirt_domain.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<vcpu>{{ cpu }}</vcpu>
<memory unit='KiB'>{{ mem }}</memory>
<os>
<type>hvm</type>
<type arch='{{ arch }}'>{{ os_type }}</type>
{% for dev in boot_dev %}
<boot dev='{{ dev }}' />
{% endfor %}
Expand Down
50 changes: 46 additions & 4 deletions tests/unit/modules/test_virt.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,14 @@ def test_boot_default_dev(self):
512,
diskp,
nicp,
'kvm'
'kvm',
'hvm',
'x86_64'
)
root = ET.fromstring(xml_data)
self.assertEqual(root.find('os/boot').attrib['dev'], 'hd')
self.assertEqual(root.find('os/type').attrib['arch'], 'x86_64')
self.assertEqual(root.find('os/type').text, 'hvm')

def test_boot_custom_dev(self):
'''
Expand All @@ -134,6 +138,8 @@ def test_boot_custom_dev(self):
diskp,
nicp,
'kvm',
'hvm',
'x86_64',
boot_dev='cdrom'
)
root = ET.fromstring(xml_data)
Expand All @@ -152,6 +158,8 @@ def test_boot_multiple_devs(self):
diskp,
nicp,
'kvm',
'hvm',
'x86_64',
boot_dev='cdrom network'
)
root = ET.fromstring(xml_data)
Expand All @@ -171,6 +179,8 @@ def test_gen_xml_for_serial_console(self):
diskp,
nicp,
'kvm',
'hvm',
'x86_64',
serial_type='pty',
console=True
)
Expand All @@ -191,6 +201,8 @@ def test_gen_xml_for_telnet_console(self):
diskp,
nicp,
'kvm',
'hvm',
'x86_64',
serial_type='tcp',
console=True,
telnet_port=22223
Expand All @@ -213,6 +225,8 @@ def test_gen_xml_for_telnet_console_unspecified_port(self):
diskp,
nicp,
'kvm',
'hvm',
'x86_64',
serial_type='tcp',
console=True
)
Expand All @@ -234,6 +248,8 @@ def test_gen_xml_for_serial_no_console(self):
diskp,
nicp,
'kvm',
'hvm',
'x86_64',
serial_type='pty',
console=False
)
Expand All @@ -254,6 +270,8 @@ def test_gen_xml_for_telnet_no_console(self):
diskp,
nicp,
'kvm',
'hvm',
'x86_64',
serial_type='tcp',
console=False,
)
Expand All @@ -273,7 +291,9 @@ def test_gen_xml_nographics_default(self):
512,
diskp,
nicp,
'kvm'
'kvm',
'hvm',
'x86_64'
)
root = ET.fromstring(xml_data)
self.assertIsNone(root.find('devices/graphics'))
Expand All @@ -291,6 +311,8 @@ def test_gen_xml_vnc_default(self):
diskp,
nicp,
'kvm',
'hvm',
'x86_64',
graphics={'type': 'vnc', 'port': 1234, 'tlsPort': 5678,
'listen': {'type': 'address', 'address': 'myhost'}},
)
Expand All @@ -316,6 +338,8 @@ def test_gen_xml_spice_default(self):
diskp,
nicp,
'kvm',
'hvm',
'x86_64',
graphics={'type': 'spice'},
)
root = ET.fromstring(xml_data)
Expand All @@ -338,6 +362,8 @@ def test_gen_xml_spice(self):
diskp,
nicp,
'kvm',
'hvm',
'x86_64',
graphics={'type': 'spice', 'port': 1234, 'tls_port': 5678, 'listen': {'type': 'none'}},
)
root = ET.fromstring(xml_data)
Expand Down Expand Up @@ -431,6 +457,8 @@ def test_gen_xml_for_kvm_default_profile(self):
diskp,
nicp,
'kvm',
'hvm',
'x86_64',
)
root = ET.fromstring(xml_data)
self.assertEqual(root.attrib['type'], 'kvm')
Expand Down Expand Up @@ -473,6 +501,8 @@ def test_gen_xml_for_esxi_default_profile(self):
diskp,
nicp,
'vmware',
'hvm',
'x86_64',
)
root = ET.fromstring(xml_data)
self.assertEqual(root.attrib['type'], 'vmware')
Expand Down Expand Up @@ -527,6 +557,8 @@ def test_gen_xml_for_esxi_custom_profile(self):
diskp,
nicp,
'vmware',
'hvm',
'x86_64',
)
root = ET.fromstring(xml_data)
self.assertEqual(root.attrib['type'], 'vmware')
Expand Down Expand Up @@ -563,6 +595,8 @@ def test_gen_xml_for_kvm_custom_profile(self):
diskp,
nicp,
'kvm',
'hvm',
'x86_64',
)
root = ET.fromstring(xml_data)
self.assertEqual(root.attrib['type'], 'kvm')
Expand Down Expand Up @@ -633,7 +667,9 @@ def test_controller_for_esxi(self):
512,
diskp,
nicp,
'vmware'
'vmware',
'hvm',
'x86_64',
)
root = ET.fromstring(xml_data)
controllers = root.findall('.//devices/controller')
Expand All @@ -653,7 +689,9 @@ def test_controller_for_kvm(self):
512,
diskp,
nicp,
'kvm'
'kvm',
'hvm',
'x86_64',
)
root = ET.fromstring(xml_data)
controllers = root.findall('.//devices/controller')
Expand Down Expand Up @@ -757,6 +795,9 @@ def test_update(self):
<memory unit='KiB'>1048576</memory>
<currentMemory unit='KiB'>1048576</currentMemory>
<vcpu placement='auto'>1</vcpu>
<os>
<type arch='x86_64' machine='pc-i440fx-2.6'>hvm</type>
</os>
<devices>
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/>
Expand Down Expand Up @@ -802,6 +843,7 @@ def test_update(self):
</domain>
'''
domain_mock = self.set_mock_vm('myvm', xml)
domain_mock.OSType = MagicMock(return_value='hvm')
define_mock = MagicMock(return_value=True)
self.mock_conn.defineXML = define_mock

Expand Down
5 changes: 5 additions & 0 deletions tests/unit/states/test_virt.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ def test_running(self):
mem=2048,
image='/path/to/img.qcow2'), ret)
init_mock.assert_called_with('myvm', cpu=2, mem=2048, image='/path/to/img.qcow2',
os_type=None, arch=None,
disk=None, disks=None, nic=None, interfaces=None,
graphics=None, hypervisor=None,
seed=True, install=True, pub_key=None, priv_key=None,
Expand Down Expand Up @@ -289,6 +290,8 @@ def test_running(self):
self.assertDictEqual(virt.running('myvm',
cpu=2,
mem=2048,
os_type='linux',
arch='i686',
vm_type='qemu',
disk_profile='prod',
disks=disks,
Expand All @@ -305,6 +308,8 @@ def test_running(self):
init_mock.assert_called_with('myvm',
cpu=2,
mem=2048,
os_type='linux',
arch='i686',
image=None,
disk='prod',
disks=disks,
Expand Down