From 373760f2c5e5d9722742fb3354ac368ee3737649 Mon Sep 17 00:00:00 2001 From: catborise Date: Mon, 9 Nov 2026 21:21:05 +0300 Subject: [PATCH] fix(instances): a VM name of any length libvirt takes is kept libvirt names a VM by its .xml file, so a name can have up to 251 bytes. The panel's VM, removed-VM and log records held 120 characters: on PostgreSQL one longer name made the host's sync fail and the VM list answer with an error. They now hold 255. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea --- computes/tests/test_reconciliation.py | 20 ++++++++++++++++- instances/jobs.py | 2 +- instances/migrations/0016_vm_name_length.py | 23 ++++++++++++++++++++ instances/models.py | 5 +++-- logs/audit.py | 2 +- logs/migrations/0006_instance_name_length.py | 18 +++++++++++++++ logs/models.py | 2 +- logs/tests/test_audit.py | 4 ++-- 8 files changed, 68 insertions(+), 8 deletions(-) create mode 100644 instances/migrations/0016_vm_name_length.py create mode 100644 logs/migrations/0006_instance_name_length.py diff --git a/computes/tests/test_reconciliation.py b/computes/tests/test_reconciliation.py index 0e30fe6a..c87f9e49 100644 --- a/computes/tests/test_reconciliation.py +++ b/computes/tests/test_reconciliation.py @@ -7,7 +7,8 @@ from django.test.utils import CaptureQueriesContext from accounts.models import UserInstance from computes.models import Compute from computes.utils import refresh_instance_database -from instances.models import Instance +from instances.models import Instance, InstanceTombstone +from logs.models import Logs from computes.tests.fakes import Domain @@ -41,6 +42,23 @@ class InstanceReconciliationTestCase(TestCase): self.assertEqual(instances[1].name, "vm-beta") self.assertEqual(instances[1].uuid, "22222222-2222-2222-2222-222222222222") + def test_a_longest_libvirt_name_is_kept_and_buried(self): + # libvirt's limit: its .xml file; PostgreSQL refused names over 120 + name = "v" * 251 + uuid = "55555555-5555-5555-5555-555555555555" + mock_proxy = MagicMock() + mock_proxy.wvm.listAllDomains.return_value = [Domain(name=name, uuid=uuid)] + self.compute.__dict__["proxy"] = mock_proxy + self.compute.__dict__["status"] = True + + refresh_instance_database(self.compute) + UserInstance.objects.create(instance=Instance.objects.get(uuid=uuid), user=self.user) + mock_proxy.wvm.listAllDomains.return_value = [] + refresh_instance_database(self.compute) + + self.assertEqual(InstanceTombstone.objects.get(uuid=uuid).name, name) + self.assertEqual(Logs.objects.filter(instance_uuid=uuid).first().instance, name) + def test_domain_deleted_in_libvirt_is_deleted_from_db(self): inst = Instance.objects.create( compute=self.compute, diff --git a/instances/jobs.py b/instances/jobs.py index 4f9d9a9a..c28036d2 100644 --- a/instances/jobs.py +++ b/instances/jobs.py @@ -204,7 +204,7 @@ def clone_data(user, instance, fields): quota=utils.quota_message(quota_msg), clone_name=data["name"]) if check_instance: raise util.OperationError(_("Instance '%(clone_name)s' already exists!"), clone_name=data["name"]) - if len(data["name"]) > 64: # as a new VM's: the panel's records hold 120 + if len(data["name"]) > 64: # as a new VM's: the panel's records hold 255 raise util.OperationError(_("Instance name '%(clone_name)s' is longer than 64 characters"), clone_name=data["name"]) if not re.match(r"^[a-zA-Z0-9-]+$", data["name"]): diff --git a/instances/migrations/0016_vm_name_length.py b/instances/migrations/0016_vm_name_length.py new file mode 100644 index 00000000..cab2d514 --- /dev/null +++ b/instances/migrations/0016_vm_name_length.py @@ -0,0 +1,23 @@ +# Generated by Django 5.2.18 on 2026-11-09 18:20 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('instances', '0015_remove_api_only_models'), + ] + + operations = [ + migrations.AlterField( + model_name='instance', + name='name', + field=models.CharField(db_index=True, max_length=255, verbose_name='name'), + ), + migrations.AlterField( + model_name='instancetombstone', + name='name', + field=models.CharField(max_length=255), + ), + ] diff --git a/instances/models.py b/instances/models.py index 3e2c4412..5059c31a 100644 --- a/instances/models.py +++ b/instances/models.py @@ -29,7 +29,8 @@ class InstanceManager(models.Manager): class Instance(models.Model): compute = models.ForeignKey(Compute, on_delete=models.CASCADE) - name = models.CharField(_("name"), max_length=120, db_index=True) + # libvirt takes any name its .xml file can have: up to 251 bytes + name = models.CharField(_("name"), max_length=255, db_index=True) uuid = models.CharField(_("uuid"), max_length=36, db_index=True) is_template = models.BooleanField(_("is template"), default=False) created = models.DateTimeField(_("created"), auto_now_add=True) @@ -409,7 +410,7 @@ class InstanceTombstone(models.Model): """ uuid = models.CharField(max_length=36, db_index=True) - name = models.CharField(max_length=120) + name = models.CharField(max_length=255) host = models.CharField(max_length=64, blank=True) # the compute's name: the compute may be deleted is_template = models.BooleanField(default=False) owners = models.JSONField(default=list) # [{"user": id, "is_change": .., "is_delete": .., "is_vnc": ..}] diff --git a/logs/audit.py b/logs/audit.py index f780f294..2863a041 100644 --- a/logs/audit.py +++ b/logs/audit.py @@ -17,7 +17,7 @@ def write(user, host, instance, message, success=None, ip=None, uuid=""): Save one Logs row. A failure is written to the log file instead and never raised, so an audit write cannot break the action it records. """ - user, host, instance, uuid = user[:150], host[:255], instance[:120], uuid[:36] + user, host, instance, uuid = user[:150], host[:255], instance[:255], uuid[:36] try: with transaction.atomic(): Logs.objects.create(user=user, host=host, instance=instance, instance_uuid=uuid, message=message, diff --git a/logs/migrations/0006_instance_name_length.py b/logs/migrations/0006_instance_name_length.py new file mode 100644 index 00000000..9b232d1a --- /dev/null +++ b/logs/migrations/0006_instance_name_length.py @@ -0,0 +1,18 @@ +# Generated by Django 5.2.18 on 2026-11-09 18:20 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('logs', '0005_instance_uuid'), + ] + + operations = [ + migrations.AlterField( + model_name='logs', + name='instance', + field=models.CharField(max_length=255, verbose_name='instance'), + ), + ] diff --git a/logs/models.py b/logs/models.py index b92c7914..85204a73 100644 --- a/logs/models.py +++ b/logs/models.py @@ -4,7 +4,7 @@ from django.utils.translation import gettext_lazy as _ class Logs(Model): user = CharField(_("user"), max_length=150) host = CharField(_("host"), max_length=255, default="-") - instance = CharField(_("instance"), max_length=120) + instance = CharField(_("instance"), max_length=255) instance_uuid = CharField(_("instance UUID"), max_length=36, blank=True, default="", db_index=True) message = TextField(_("message")) date = DateTimeField(_("date"), auto_now_add=True, db_index=True) diff --git a/logs/tests/test_audit.py b/logs/tests/test_audit.py index ea74e1d6..fc62d8fb 100644 --- a/logs/tests/test_audit.py +++ b/logs/tests/test_audit.py @@ -25,11 +25,11 @@ class WriteTests(TestCase): self.assertEqual(row.ip, "192.0.2.7") def test_long_values_are_cut(self): - write("u" * 200, "h" * 300, "i" * 200, "m" * 5000) + write("u" * 200, "h" * 300, "i" * 300, "m" * 5000) row = Logs.objects.get() self.assertEqual(len(row.user), 150) self.assertEqual(len(row.host), 255) - self.assertEqual(len(row.instance), 120) + self.assertEqual(len(row.instance), 255) self.assertEqual(len(row.message), 5000) def test_a_failed_write_inside_a_transaction_leaves_it_usable(self):