This commit is contained in:
2024-11-13 11:51:38 +03:00
parent aa3f84095f
commit dd2fca15f3
32 changed files with 9538 additions and 713 deletions

View File

@@ -69,11 +69,11 @@ options:
required: no
arch:
description:
- Architecture of the KVM VM. DECORT supports KVM hosts based on Intel x86 and IBM PowerPC hardware.
- Architecture of the KVM VM. DECORT supports KVM hosts based on Intel x86.
- This parameter is used when new KVM VM is created and ignored for all other operations.
- Module may fail if your DECORT installation does not have physical nodes of specified architecture.
default: X86_64
choices: [ X86_64, PPC64_LE ]
choices: [ X86_64 ]
required: yes
authenticator:
description:
@@ -355,6 +355,8 @@ class decort_kvmvm(DecortController):
# call superclass constructor first
super(decort_kvmvm, self).__init__(arg_amodule)
self.check_amodule_args()
self.comp_should_exist = False
# This following flag is used to avoid extra (and unnecessary) get of compute details prior to
# packaging facts before the module completes. As ""
@@ -415,19 +417,59 @@ class decort_kvmvm(DecortController):
check_state=False)
if self.comp_id:
if self.comp_info['status'] != 'DESTROYED' and self.comp_info['arch'] not in ["X86_64", "PPC64_LE"]:
# If we found a Compute in a non-DESTROYED state and it is not of type valid arch, abort the module
self.result['failed'] = True
self.result['msg'] = ("Compute ID {} architecture '{}' is not supported by "
"decort_kvmvm module.").format(self.comp_id,
self.amodule.params['arch'])
self.amodule.fail_json(**self.result)
# fail the module - exit
self.comp_should_exist = True
self.acc_id = self.comp_info['accountId']
return
def check_amodule_args(self):
"""
Additional Ansible Module arguments validation that
cannot be implemented using Ansible Argument spec.
"""
# Check parameter "networks" for DPDK type
aparam_nets = self.aparams['networks']
if aparam_nets:
net_types = {net['type'] for net in aparam_nets}
DPDK = 'DPDK'
if DPDK in net_types and not net_types.issubset({'DPDK', 'EMPTY'}):
self.message(
'Check for parameter "networks" failed: a compute cannot'
' be connected to a DPDK network and a network of another'
' type at the same time.'
)
self.exit(fail=True)
# Check for unacceptable parameters for a blank Compute
if (
self.aparams['image_id'] is None
and self.aparams['image_name'] is None
):
for parameter in (
'ssh_key',
'ssh_key_user',
'ci_user_data',
):
if self.aparams[parameter] is not None:
self.message(
f'Check for parameter "{parameter}" failed: '
f'"image_id" or "image_name" must be specified '
f'to set {parameter}.'
)
self.exit(fail=True)
if (
self.aparams['sep_id'] is not None
and self.aparams['boot_disk'] is None
):
self.message(
'Check for parameter "sep_id" failed: '
'"image_id" or "image_name" or "boot_disk" '
'must be specified to set sep_id.'
)
self.exit(fail=True)
def nop(self):
"""No operation (NOP) handler for Compute management by decort_kvmvm module.
This function is intended to be called from the main switch construct of the module
@@ -475,42 +517,45 @@ class decort_kvmvm(DecortController):
# each of the following calls will abort if argument is missing
self.check_amodule_argument('cpu')
self.check_amodule_argument('ram')
validated_bdisk_size = self.amodule.params['boot_disk'] or 0
if self.amodule.params['arch'] not in ["X86_64", "PPC64_LE"]:
self.result['failed'] = True
self.result['msg'] = ("Unsupported architecture '{}' is specified for "
"KVM VM create.").format(self.amodule.params['arch'])
self.amodule.fail_json(**self.result)
# fail the module - exit
validated_bdisk_size = 0
image_facts = None
# either image_name or image_id must be present
if self.check_amodule_argument('image_id', abort=False) and self.amodule.params['image_id'] > 0 :
# find image by image ID and account ID
# image_find(self, image_id, image_name, account_id, rg_id=0, sepid=0, pool=""):
_, image_facts = self.image_find(image_id=self.amodule.params['image_id'],
image_name="",
account_id=self.acc_id)
elif self.check_amodule_argument('image_name', abort=False) and self.amodule.params['image_name'] != "":
# find image by image name and account ID
_, image_facts = self.image_find(image_id=0,
image_name=self.amodule.params['image_name'],
account_id=self.acc_id)
image_id, image_facts = None, None
if (
self.amodule.params['image_id'] is None
and self.amodule.params['image_name'] is None
):
if self.amodule.params['state'] not in ('poweredoff', 'halted'):
self.result['msg'] = (
'"state" parameter for a blank Compute must be either '
'"poweredoff" or "halted".'
)
self.exit(fail=True)
else:
# neither image_name nor image_id are set - abort the script
self.result['failed'] = True
self.result['msg'] = "Missing both 'image_name' and 'image_id'. You need to specify one to create a Compute."
self.amodule.fail_json(**self.result)
# fail the module - exit
# either image_name or image_id must be present
if (
self.check_amodule_argument('image_id', abort=False)
and self.amodule.params['image_id'] > 0
):
# find image by image ID and account ID
# image_find(self, image_id, image_name, account_id, rg_id=0, sepid=0, pool=""):
image_id, image_facts = self.image_find(
image_id=self.amodule.params['image_id'],
image_name="",
account_id=self.acc_id)
elif (
self.check_amodule_argument('image_name', abort=False)
and self.amodule.params['image_name'] != ""
):
# find image by image name and account ID
image_id, image_facts = self.image_find(
image_id=0,
image_name=self.amodule.params['image_name'],
account_id=self.acc_id,
)
if ((not self.check_amodule_argument('boot_disk', False)) or
self.amodule.params['boot_disk'] <= image_facts['size']):
# adjust disk size to the minimum allowed by OS image, which will be used to spin off this Compute
validated_bdisk_size = image_facts['size']
else:
validated_bdisk_size =self.amodule.params['boot_disk']
if validated_bdisk_size <= image_facts['size']:
# adjust disk size to the minimum allowed by OS image, which will be used to spin off this Compute
validated_bdisk_size = image_facts['size']
# NOTE: due to a libvirt "feature", that impacts management of a VM created without any network interfaces,
# we create KVM VM in HALTED state.
@@ -536,17 +581,24 @@ class decort_kvmvm(DecortController):
cloud_init_params = None
# if we get through here, all parameters required to create new Compute instance should be at hand
match self.amodule.params['chipset'].lower():
case 'q35':
chipset = 'Q35'
case 'i440fx':
chipset = 'i440fx'
# NOTE: KVM VM is created in HALTED state and must be explicitly started
self.comp_id = self.kvmvm_provision(rg_id=self.rg_id,
comp_name=self.amodule.params['name'], arch=self.amodule.params['arch'],
comp_name=self.amodule.params['name'],
cpu=self.amodule.params['cpu'], ram=self.amodule.params['ram'],
boot_disk=validated_bdisk_size,
image_id=image_facts['id'],
image_id=image_id,
annotation=self.amodule.params['annotation'],
userdata=cloud_init_params,
sep_id=self.amodule.params['sep_id' ] if "sep_id" in self.amodule.params else None,
pool_name=self.amodule.params['pool'] if "pool" in self.amodule.params else None,
start_on_create=start_compute)
start_on_create=start_compute,
chipset=chipset)
self.comp_should_exist = True
# Originally we would have had to re-read comp_info after VM was provisioned
@@ -574,9 +626,17 @@ class decort_kvmvm(DecortController):
# Compute was created
#
# Setup network connections
self.compute_networks(self.comp_info, self.amodule.params['networks'])
if self.amodule.params['networks'] is not None:
self.compute_networks(
comp_dict=self.comp_info,
new_networks=self.amodule.params['networks'],
)
# Next manage data disks
self.compute_data_disks(self.comp_info, self.amodule.params['data_disks'])
if self.amodule.params['data_disks'] is not None:
self.compute_data_disks(
comp_dict=self.comp_info,
new_data_disks=self.amodule.params['data_disks'],
)
self.compute_affinity(self.comp_info,
self.amodule.params['tag'],
@@ -623,25 +683,49 @@ class decort_kvmvm(DecortController):
Note that it does not modify power state of KVM VM.
"""
self.compute_networks(self.comp_info, self.amodule.params['networks'])
if self.amodule.params['networks'] is not None:
self.compute_networks(
comp_dict=self.comp_info,
new_networks=self.aparams['networks'],
order_changing=self.aparams['network_order_changing'],
)
boot_disk_new_size = self.amodule.params['boot_disk']
if boot_disk_new_size:
self.compute_bootdisk_size(self.comp_info, boot_disk_new_size)
self.compute_data_disks(self.comp_info, self.amodule.params['data_disks'])
if self.amodule.params['data_disks'] is not None:
self.compute_data_disks(self.comp_info, self.amodule.params['data_disks'])
self.compute_resize(self.comp_info,
self.amodule.params['cpu'], self.amodule.params['ram'],
wait_for_state_change=arg_wait_cycles)
self.compute_affinity(self.comp_info,
self.amodule.params['tag'],
self.amodule.params['aff_rule'],
self.amodule.params['aaff_rule'],
label=self.amodule.params['affinity_label'])
if self.compute_update_args:
self.compute_update(
compute_id=self.comp_info['id'],
**self.compute_update_args,
)
return
@property
def compute_update_args(self) -> dict:
result_args = {}
aparam_name = self.amodule.params['name']
if aparam_name is not None and aparam_name != self.comp_info['name']:
result_args['name'] = aparam_name
return result_args
def package_facts(self, check_mode=False):
"""Package a dictionary of KVM VM facts according to the decort_kvmvm module specification.
This dictionary will be returned to the upstream Ansible engine at the completion of decort_kvmvm
@@ -670,6 +754,8 @@ class decort_kvmvm(DecortController):
private_ips=[], # IPs on ViNSes; usually, at least one IP is listed
nat_ip="", # IP of the external ViNS interface; can be empty.
tags={},
chipset="",
interfaces=[],
)
if check_mode or self.comp_info is None:
@@ -722,6 +808,10 @@ class decort_kvmvm(DecortController):
# if it is a data disk - append its ID to the list of data disks IDs
ret_dict['data_disks'].append(ddisk['id'])
ret_dict['chipset'] = self.comp_info['chipset']
ret_dict['interfaces'] = self.comp_info['interfaces']
return ret_dict
@staticmethod
@@ -745,7 +835,6 @@ class decort_kvmvm(DecortController):
required=False,
fallback=(env_fallback, ['DECORT_APP_SECRET']),
no_log=True),
arch=dict(type='str', choices=['X86_64', 'PPC64_LE'], default='X86_64'),
authenticator=dict(type='str',
required=True,
choices=['legacy', 'oauth2', 'jwt']),
@@ -756,7 +845,7 @@ class decort_kvmvm(DecortController):
# count=dict(type='int', required=False, default=1),
cpu=dict(type='int', required=False),
# datacenter=dict(type='str', required=False, default=''),
data_disks=dict(type='list', default=[], required=False), # list of integer disk IDs
data_disks=dict(type='list', required=False), # list of integer disk IDs
id=dict(type='int', required=False, default=0),
image_id=dict(type='int', required=False),
image_name=dict(type='str', required=False),
@@ -765,7 +854,39 @@ class decort_kvmvm(DecortController):
fallback=(env_fallback, ['DECORT_JWT']),
no_log=True),
name=dict(type='str'),
networks=dict(type='list', default=[], required=False), # list of dictionaries
networks=dict(
type='list',
elements='dict',
options=dict(
type=dict(
type='str',
required=True,
choices=[
'VINS',
'EXTNET',
'VFNIC',
'DPDK',
'EMPTY',
],
),
id=dict(
type='int',
),
ip_addr=dict(
type='str',
),
),
required_if=[
('type', 'VINS', ('id',)),
('type', 'EXTNET', ('id',)),
('type', 'VFNIC', ('id',)),
('type', 'DPDK', ('id',)),
],
),
network_order_changing=dict(
type='bool',
default=False,
),
oauth2_url=dict(type='str',
required=False,
fallback=(env_fallback, ['DECORT_OAUTH2_URL'])),
@@ -794,6 +915,11 @@ class decort_kvmvm(DecortController):
# wait_for_ip_address=dict(type='bool', required=False, default=False),
workflow_callback=dict(type='str', required=False),
workflow_context=dict(type='str', required=False),
chipset=dict(
type='str',
default='i440fx',
choices=['Q35', 'q35', 'I440FX', 'i440fx']
),
)
# Workflow digest: