From 22362de0686ac9d84c6e0e6a7ec11bb53608a252 Mon Sep 17 00:00:00 2001 From: catborise Date: Mon, 16 Nov 2026 15:56:01 +0300 Subject: [PATCH] feat(computes): a host in maintenance takes no new VM or migration After a host was emptied with Bulk Migrate for its maintenance, Balance proposed moving VMs back onto it, and new VMs and migrations could still be sent there. A superuser now puts a host in maintenance on its page (and takes it out), each change audited; the form names the state it sets, so a second page sent meanwhile does not turn it back. A host in maintenance (Compute.maintenance, refuse_new_vms): - takes no migration: migration_target refuses it, so the Migrate tab, Bulk Migrate, Balance and queued migrate jobs follow one rule; Bulk Migrate leaves it out of its destinations and its spread; - takes no new VM: the create and clone jobs refuse it when asked for and again before they start (the REST API submits the same jobs); the create pages and the XML define send the user back to the VM list, and the New VM dialog lists it without a link; - is left out of Balance: its VMs neither count nor move, and the Balance job refuses a move from it too, before each migration starts. Its VMs run on; a migration already started finishes. The Computes page and the host's page show that it is in maintenance. --- computes/jobs.py | 17 + computes/migrations/0010_maintenance.py | 18 + computes/models.py | 14 +- computes/templates/computes/balance.html | 3 + computes/templates/computes/list.html | 1 + computes/templates/overview.html | 16 + computes/tests/test_balance.py | 8 + computes/tests/test_bulk_migrate.py | 6 + computes/tests/test_maintenance.py | 106 ++++++ computes/urls.py | 1 + computes/views.py | 29 +- instances/jobs.py | 8 +- instances/templates/create_inst_block.html | 8 + instances/tests/test_create_dialog_cpu.py | 8 + instances/views.py | 14 + locale/tr/LC_MESSAGES/django.mo | Bin 215883 -> 217126 bytes locale/tr/LC_MESSAGES/django.po | 406 ++++++++++++--------- locale/zh/LC_MESSAGES/django.mo | Bin 202147 -> 203435 bytes locale/zh/LC_MESSAGES/django.po | 403 +++++++++++--------- 19 files changed, 704 insertions(+), 362 deletions(-) create mode 100644 computes/migrations/0010_maintenance.py create mode 100644 computes/tests/test_maintenance.py diff --git a/computes/jobs.py b/computes/jobs.py index a467a9c6..73a68f3d 100644 --- a/computes/jobs.py +++ b/computes/jobs.py @@ -44,6 +44,13 @@ class Migrations(Kind): def hosts(self, params): return {host for _vm, source, target in self.moves(params) for host in (source, target)} + def check_move(self, source, target): + """Refuses (OperationError) a move to a host in maintenance, when the + job is asked for and again before the VM's migration starts; the + migrate job checks the rest.""" + if host := Compute.objects.filter(pk=target).first(): + host.refuse_new_vms() + def validate(self, user, params): if not user.is_superuser: raise PermissionDenied @@ -52,6 +59,8 @@ class Migrations(Kind): if not 1 <= params["parallel"] <= parallel_limit(): raise util.OperationError(gettext_noop("At most %(limit)s migrations can run at once"), limit=parallel_limit()) + for _vm, source, target in self.moves(params): + self.check_move(source, target) def precheck(self, op, ctx): if op.user is None or not op.user.is_active or not op.user.is_superuser: @@ -96,6 +105,7 @@ class Migrations(Kind): raw = {"instance": vm, "source": source, "target": target, "live": True, "autoconverge": True, **self.options} try: + self.check_move(source, target) # a host put in maintenance since child = jobs.submit("instance.migrate", op.user, raw, f"bulk-{op.pk.hex}-{vm}", parent=op) except claims.Busy as busy: if busy.blocker is not None and busy.blocker.state == Operation.UNCERTAIN: @@ -189,6 +199,13 @@ class Balance(Migrations): def moves(self, params): return [tuple(move) for move in params["moves"]] + def check_move(self, source, target): + """Balance leaves a host in maintenance out: it takes no VM and gives none.""" + super().check_move(source, target) + if host := Compute.objects.filter(pk=source, maintenance=True).first(): + raise util.OperationError(gettext_noop("%(compute)s is in maintenance: Balance leaves it out"), + compute=host.name) + def validate(self, user, params): super().validate(user, params) if any(source == target for _vm, source, target in self.moves(params)): diff --git a/computes/migrations/0010_maintenance.py b/computes/migrations/0010_maintenance.py new file mode 100644 index 00000000..128bfc80 --- /dev/null +++ b/computes/migrations/0010_maintenance.py @@ -0,0 +1,18 @@ +# Generated by Django 5.2.18 on 2026-11-16 12:49 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('computes', '0009_host_uuid'), + ] + + operations = [ + migrations.AddField( + model_name='compute', + name='maintenance', + field=models.BooleanField(default=False, editable=False), + ), + ] diff --git a/computes/models.py b/computes/models.py index 6254d913..579322c2 100644 --- a/computes/models.py +++ b/computes/models.py @@ -2,12 +2,14 @@ from ipaddress import ip_address from django.core.exceptions import ValidationError from django.db import IntegrityError, transaction -from django.db.models import CharField, IntegerField, Model, Q, UniqueConstraint +from django.db.models import BooleanField, CharField, IntegerField, Model, Q, UniqueConstraint from django.utils.functional import cached_property from django.utils.translation import gettext_lazy as _ +from django.utils.translation import gettext_noop from libvirt import libvirtError, virConnect from computes.validators import validate_gstfsd_key, validate_hostname, validate_login, validate_migration_address +from vrtManager import util from vrtManager.connection import connection_manager from vrtManager.hostdetails import wvmHostDetails @@ -50,9 +52,19 @@ class Compute(Model): # reaches it: one host is one compute, and a compute's VMs stay on it host_uuid = CharField(max_length=36, blank=True, default="", editable=False) + # in maintenance: no new VM and no migration comes to the host, and Balance + # leaves it out; its VMs run on and Bulk Migrate empties it (refuse_new_vms) + maintenance = BooleanField(default=False, editable=False) + class Meta: constraints = [UniqueConstraint(fields=["host_uuid"], condition=~Q(host_uuid=""), name="one_compute_per_host")] + def refuse_new_vms(self): + """Refuses a VM made on or migrated to the host while it is in maintenance.""" + if self.maintenance: + raise util.OperationError(gettext_noop("%(compute)s is in maintenance: no new VM or migration comes to it"), + compute=self.name) + @property def migration_uri(self): """The native migration URI for VMs migrated to this host: its diff --git a/computes/templates/computes/balance.html b/computes/templates/computes/balance.html index 9b7ac08e..9bb769f0 100644 --- a/computes/templates/computes/balance.html +++ b/computes/templates/computes/balance.html @@ -16,6 +16,9 @@ {% if unreachable %}
{% trans "These hosts cannot be read and are left out:" %} {{ unreachable|join:", " }}
{% endif %} +{% if resting %} +
{% trans "These hosts are in maintenance and are left out:" %} {{ resting|join:", " }}
+{% endif %} {# each host's running VMs: the share of its memory now and, light to dark, after the migrations #}
diff --git a/computes/templates/computes/list.html b/computes/templates/computes/list.html index 5df2d13f..06062d15 100644 --- a/computes/templates/computes/list.html +++ b/computes/templates/computes/list.html @@ -75,6 +75,7 @@ {% else %} {% trans "Not connected" %} {% endif %} + {% if compute.maintenance %}{% trans "In maintenance" %}{% endif %} {{ compute.connection_name }}
{{ compute.hostname }}
diff --git a/computes/templates/overview.html b/computes/templates/overview.html index 4a271ee4..d33f1edc 100644 --- a/computes/templates/overview.html +++ b/computes/templates/overview.html @@ -6,6 +6,15 @@ {% block title %}{% trans "Overview" %} - {{ compute.name }}{% endblock %} {% block page_heading_extra %} +
{% csrf_token %} + {% if compute.maintenance %} + + + {% else %} + + + {% endif %} +
{% bs_icon 'pencil' %} {% trans "Edit connection" %} {% bs_icon 'x-circle' %} {% trans "Remove from panel" %} {% endblock page_heading_extra %} @@ -27,6 +36,13 @@ {% include "computes/host_nav.html" with section="overview" %} +{% if compute.maintenance %} +
+ {% url 'bulk_migrate' compute.id as bulk_url %} + {% blocktrans %}In maintenance: no new VM or migration comes to this host and Balance leaves it out. Bulk Migrate moves its VMs to the other hosts.{% endblocktrans %} +
+{% endif %} + {% if not connected %}