Skip to content
Snippets Groups Projects
Commit 4d2dcf3b authored by Benjamin Bertrand's avatar Benjamin Bertrand
Browse files

Add post install job after VM creation

After a VM or VIOC creation, a post install job can be triggered.
The job to trigger is based on the domain and defined in the
AWX_POST_INSTALL dictionary.

Admin users have the option to skip this post job if required.

The job is enqueued in RQ with a dependency on the creation job.
So it's only triggered after successful completion of the first one.

JIRA INFRA-769
parent 944ce9f6
No related branches found
No related tags found
No related merge requests found
......@@ -231,6 +231,7 @@ class CreateVMForm(CSEntryForm):
cores = SelectField("Cores", default=2, coerce=int)
memory = SelectField("Memory (GB)", default=2, coerce=int)
disk = SelectField("Disk (GB)", default=15, coerce=int)
skip_post_install_job = BooleanField("Skip post install job", default=False)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
......
......@@ -192,7 +192,7 @@ def view_host(name):
try:
host.ansible_vars.update(csentry_vars)
# If we don't flag the field as modified, it's not saved to the database
# Might be because we update an existing directory
# Might be because we update an existing dict
sa.orm.attributes.flag_modified(host, "ansible_vars")
except AttributeError:
host.ansible_vars = csentry_vars
......@@ -202,6 +202,7 @@ def view_host(name):
interface,
csentry_vars["csentry_vm_memory"],
csentry_vars["csentry_vm_cores"],
skip_post_install_job=form.skip_post_install_job.data,
)
db.session.commit()
current_app.logger.info(f"Creation of {name} requested: task {task.id}")
......
......@@ -94,6 +94,14 @@ AWX_CORE_SERVICES_UPDATE = "ics-ans-core @ DHCP test"
AWX_CREATE_VM = "deploy-vm-in-proxmox"
AWX_CREATE_VIOC = "ics-ans-deploy-vioc"
AWX_ZTP_CONFIGURATION = "ics-ans-ztp"
AWX_POST_INSTALL = {
"VIOC": {"esss.lu.se": "", "tn.esss.lu.se": "", "cslab.esss.lu.se": ""},
"VM": {
"esss.lu.se": "",
"tn.esss.lu.se": "",
"cslab.esss.lu.se": "customize-LabVM",
},
}
AWX_JOB_ENABLED = False
AWX_VM_CREATION_ENABLED = False
......
......@@ -81,8 +81,9 @@ class TaskWorker(Worker):
super().prepare_job_execution(job)
def trigger_vm_creation(name, interface, memory, cores):
def trigger_vm_creation(name, interface, memory, cores, skip_post_install_job):
"""Trigger a job to create a virtual machine or virtual IOC"""
domain = interface.network.domain.name
extra_vars = [
f"vmname={name}",
f"memory={memory}",
......@@ -95,13 +96,15 @@ def trigger_vm_creation(name, interface, memory, cores):
if interface.is_ioc:
task_name = "trigger_vioc_creation"
job_template = current_app.config["AWX_CREATE_VIOC"]
post_job_template = current_app.config["AWX_POST_INSTALL"]["VIOC"].get(domain)
else:
task_name = "trigger_vm_creation"
job_template = current_app.config["AWX_CREATE_VM"]
post_job_template = current_app.config["AWX_POST_INSTALL"]["VM"].get(domain)
extra_vars.extend(
[
f"ip_address={interface.ip}",
f"domain={interface.network.domain.name}",
f"domain={domain}",
f"netmask={interface.network.netmask}",
f"gateway={interface.network.gateway}",
]
......@@ -116,6 +119,19 @@ def trigger_vm_creation(name, interface, memory, cores):
extra_vars=extra_vars,
timeout=500,
)
if post_job_template and not skip_post_install_job:
current_user.launch_task(
"trigger_post_install_job",
func="launch_job_template",
job_template=post_job_template,
limit=f"{name}.{domain}",
depends_on=task.id,
timeout=500,
)
current_app.logger.info(
f"Trigger post install job: run {post_job_template} on {name}.{domain}"
)
return task
......
......@@ -100,7 +100,7 @@
{% if field.type == 'BooleanField' %}
<div class="col-sm-{{ input_size }} offset-sm-{{ label_size }}">
<div class="form-check">
{{ field(class_="form-check-input") }}
{{ field(class_="form-check-input", **kwargs) }}
{{ field.label(class_="form-check-label") }}
</div>
</div>
......
......@@ -19,7 +19,7 @@
{% block hosts_main %}
<div class="row">
<div class="col-sm-9">
<div class="col-sm-8">
<dl class="row">
<dt class="col-sm-3">Hostname</dt>
<dd class="col-sm-9">
......@@ -70,17 +70,20 @@
{% else %}
{% set vm_type = 'VM' %}
{% endif %}
<div class="col-sm-3">
<div class="col-sm-4">
<form id="createVMForm" method="POST">
{{ form.hidden_tag() }}
{{ render_field(form.cores, label_size='6', input_size='6') }}
{{ render_field(form.memory, label_size='6', input_size='6') }}
{{ render_field(form.disk, label_size='6', input_size='6') }}
{{ render_field(form.cores, label_size='5', input_size='7') }}
{{ render_field(form.memory, label_size='5', input_size='7') }}
{{ render_field(form.disk, label_size='5', input_size='7') }}
{% if current_user.is_admin %}
{{ render_field(form.skip_post_install_job, label_size='5', input_size='7') }}
{% endif %}
{{ submit_button_with_confirmation('Create ' + vm_type, 'Do you really want to create the ' + vm_type + ' ' + host.name + '?') }}
</form>
</div>
{% elif host.device_type.name == 'Network' and host.model and current_user.is_admin %}
<div class="col-sm-3">
<div class="col-sm-4">
<form id="generateZTPForm" method="POST">
{{ form.hidden_tag() }}
{{ submit_button_with_confirmation('Generate ZTP configuration', 'Do you really want to generate the ZTP configuration for ' + host.name + '?') }}
......
docs/_static/create_vioc.png

212 KiB | W: | H:

docs/_static/create_vioc.png

732 KiB | W: | H:

docs/_static/create_vioc.png
docs/_static/create_vioc.png
docs/_static/create_vioc.png
docs/_static/create_vioc.png
  • 2-up
  • Swipe
  • Onion skin
docs/_static/create_vm.png

212 KiB | W: | H:

docs/_static/create_vm.png

731 KiB | W: | H:

docs/_static/create_vm.png
docs/_static/create_vm.png
docs/_static/create_vm.png
docs/_static/create_vm.png
  • 2-up
  • Swipe
  • Onion skin
docs/_static/create_vm_confirmation.png

209 KiB | W: | H:

docs/_static/create_vm_confirmation.png

726 KiB | W: | H:

docs/_static/create_vm_confirmation.png
docs/_static/create_vm_confirmation.png
docs/_static/create_vm_confirmation.png
docs/_static/create_vm_confirmation.png
  • 2-up
  • Swipe
  • Onion skin
......@@ -137,6 +137,13 @@ In both case, a confirmation dialog will be displayed when you submit the form.
.. image:: _static/create_vm_confirmation.png
To create a VM or VIOC, a job template is triggered on AWX (**AWX_CREATE_VM** or **AWX_CREATE_VIOC**).
If a post install job template is defined, a task is enqueued to run after the successful completion of the first one.
The post install job name can be defined per domain in the **AWX_POST_INSTALL** dictionary.
Admin users can disable the post install job run by selecting the *Skip post install job* checkbox.
This checkbox is only visible to admin users.
Ansible inventory
-----------------
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment