1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-11 12:46:49 +00:00

fix(instances): a VM name of any length libvirt takes is kept

libvirt names a VM by its <name>.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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea
This commit is contained in:
catborise 2026-11-09 21:21:05 +03:00
parent e83bdcbd44
commit 373760f2c5
8 changed files with 68 additions and 8 deletions

View file

@ -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 <name>.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,

View file

@ -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"]):

View file

@ -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),
),
]

View file

@ -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 <name>.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": ..}]

View file

@ -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,

View file

@ -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'),
),
]

View file

@ -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)

View file

@ -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):