1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-03 17:36:05 +00:00

feat(migrations): a migration's bandwidth can be capped

A migration took all the network it could, its disk copies too, and the VMs
on both hosts felt it. Settings has a Migrations group with "Migration
bandwidth" (MiB/s, 0: libvirt's default, the default); every migration, by
hand, in bulk or for maintenance, passes it to libvirt, and its log line
names it. libvirt applies it to the memory until the final switchover and to
each copied disk on its own; the help and README say so. A refused setting
is now named in the page's language.
This commit is contained in:
catborise 2026-11-20 09:17:45 +03:00
parent 46931bdbdb
commit 3da55342da
14 changed files with 438 additions and 333 deletions

View file

@ -0,0 +1,29 @@
from django.core.management.color import no_style
from django.db import migrations
from django.utils.translation import gettext_noop as _
SETTING = (_("Migration bandwidth"), "MIGRATION_BANDWIDTH", "0",
_("MiB/s for the memory and each copied disk, not the final switchover; 0: libvirt's default"))
def add_setting(apps, schema_editor):
setting = apps.get_model("appsettings", "AppSettings")
db_alias = schema_editor.connection.alias
# see 0014: move a PostgreSQL id sequence past the fixed ids first
with schema_editor.connection.cursor() as cursor:
for sql in schema_editor.connection.ops.sequence_reset_sql(no_style(), [setting]):
cursor.execute(sql)
name, key, value, description = SETTING
setting.objects.using(db_alias).get_or_create(
key=key, defaults={"name": name, "value": value, "choices": "", "description": description})
def del_setting(apps, schema_editor):
apps.get_model("appsettings", "AppSettings").objects.using(schema_editor.connection.alias).filter(
key=SETTING[1]).delete()
class Migration(migrations.Migration):
dependencies = [("appsettings", "0027_alert_settings")]
operations = [migrations.RunPython(add_setting, del_setting)]

View file

@ -72,6 +72,11 @@ class AppSettingsTestCase(TestCase):
self.assertEqual(AppSettings.objects.get(key="WORKER_CONCURRENCY").value, "4")
self.assertTrue(any("between 1 and 16" in str(m) for m in get_messages(response.wsgi_request)))
def test_a_refused_setting_is_named_in_the_pages_language(self):
response = self.client.post(reverse("appsettings"), {"MIGRATION_BANDWIDTH": "-1"}, HTTP_ACCEPT_LANGUAGE="tr")
self.assertEqual(AppSettings.objects.get(key="MIGRATION_BANDWIDTH").value, "0")
self.assertTrue(any(str(m).startswith("Taşıma bant genişliği") for m in get_messages(response.wsgi_request)))
def post(self, key, value):
response = self.client.post(reverse("appsettings"), {key: value})
# not followed: the messages would stay in the cookie for the next post

View file

@ -129,8 +129,9 @@ class RepairNumbersTestCase(TestCase):
def test_a_number_an_older_version_saved_goes_back_to_its_default(self):
from appsettings.views import NUMBERS
# the same ranges as the Settings page checks
self.assertEqual({key: bounds[:2] for key, bounds in repair_numbers.NUMBERS.items()}, NUMBERS)
# the same ranges as the Settings page checks (a setting added since is not the migration's)
self.assertEqual({key: bounds[:2] for key, bounds in repair_numbers.NUMBERS.items()},
{key: NUMBERS[key] for key in repair_numbers.NUMBERS})
for key, value in (("LOGS_PER_PAGE", "abc"), ("WORKER_CONCURRENCY", "0"), ("LOGIN_FAILURE_LIMIT", "")):
AppSettings.objects.filter(key=key).update(value=value)
AppSettings.objects.filter(key="LOG_RETENTION_DAYS").update(value="90")

View file

@ -56,6 +56,7 @@ GROUPS = [
"ALLOW_INSTANCE_MULTIPLE_OWNER", "QUOTA_DEBUG"]),
(gettext_noop("Snapshots and jobs"), ["SNAPSHOT_FREE_SPACE_PERCENT", "SNAPSHOT_FREE_SPACE_MIN_GIB",
"WORKER_CONCURRENCY", "JOB_RETENTION_DAYS"]),
(gettext_noop("Migrations"), ["MIGRATION_BANDWIDTH"]),
(gettext_noop("Login"), ["LOGIN_FAILURE_LIMIT", "LOGIN_LOCKOUT_MINUTES"]),
(gettext_noop("Alerts"), ["ALERT_EMAILS", "ALERT_WEBHOOK"]),
(gettext_noop("Lists and pages"), ["VIEW_INSTANCES_LIST_STYLE", "VM_DRBD_STATUS", "LOGS_PER_PAGE",
@ -67,18 +68,18 @@ def refusal(setting, value):
"""Why the value cannot be saved for the setting, or None."""
if len(value) > AppSettings._meta.get_field("value").max_length:
return _("%(setting)s takes at most %(length)s characters") % {
"setting": setting.name, "length": AppSettings._meta.get_field("value").max_length}
"setting": _(setting.name), "length": AppSettings._meta.get_field("value").max_length}
if setting.key in NUMBERS:
low, high = NUMBERS[setting.key]
if not (value.isascii() and value.isdigit() and len(value) <= 10 and low <= int(value) <= high):
return _("%(setting)s must be a whole number between %(low)s and %(high)s") % {
"setting": setting.name, "low": low, "high": high}
"setting": _(setting.name), "low": low, "high": high}
elif setting.choices and value not in setting.choices_as_list():
return _("%(setting)s must be one of %(choices)s") % {
"setting": setting.name, "choices": ", ".join(setting.choices_as_list())}
"setting": _(setting.name), "choices": ", ".join(setting.choices_as_list())}
elif setting.key == "CLONE_INSTANCE_DEFAULT_PREFIX" and not re.fullmatch(r"[a-zA-Z0-9-]+", value):
# a clone's name is the prefix and a number, and takes only these
return _("%(setting)s takes letters, digits and '-' only") % {"setting": setting.name}
return _("%(setting)s takes letters, digits and '-' only") % {"setting": _(setting.name)}
try:
if setting.key == "ALERT_EMAILS":
for address in filter(None, (a.strip() for a in value.split(","))):
@ -88,7 +89,7 @@ def refusal(setting, value):
if "@" in urlsplit(value.strip()).netloc: # urllib would take user:password@ for the host
raise ValidationError("credentials in the URL")
except ValidationError:
return _("%(setting)s: %(value)s is not an e-mail address or an http(s) URL") % {"setting": setting.name, "value": value}
return _("%(setting)s: %(value)s is not an e-mail address or an http(s) URL") % {"setting": _(setting.name), "value": value}
return None