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 %}