1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-08 19:26:48 +00:00

fix(instances): clone name and MAC read the same dhcpd.conf

An optional dhcpd.conf next to the application supplies clone names and
their MAC addresses. get_dhcp_mac_address read it from BASE_DIR, but
guess_clone_name from a fixed /srv/webvirtcloud/dhcpd.conf, so on any
other install path the Guess button found no name while the MAC lookup
still worked. Both use utils.dhcpd_conf() (BASE_DIR) now.
This commit is contained in:
catborise 2026-10-04 19:39:29 +03:00
parent 80a94a36b6
commit 5c4eb97410
3 changed files with 28 additions and 3 deletions

View file

@ -2,18 +2,21 @@
Cloning another user's VM must require change permission on it (templates
excepted), and the clone data must not be taken blindly from the POST body.
"""
import tempfile
from contextlib import contextmanager
from pathlib import Path
from unittest.mock import patch
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Permission
from django.test import TestCase
from django.test import TestCase, override_settings
from django.urls import reverse
from accounts.models import UserInstance
from appsettings.settings import app_settings
from computes.models import Compute
from instances.models import Instance
from instances.utils import get_dhcp_mac_address
SRC_DISK = {
"dev": "vda",
@ -201,3 +204,19 @@ class ClonePermissionTestCase(TestCase):
self.assertEqual(
proxy.clone_instance.call_args[0][0]["disk-vda"], "my-disk-2-clone.qcow2"
)
class DhcpdConfTestCase(TestCase):
"""Clone name and MAC come from the same dhcpd.conf, next to the app."""
def test_guess_clone_name_and_mac_read_the_app_directory(self):
self.client.force_login(get_user_model().objects.create_superuser("dhcp_admin", "d@example.com", "pw"))
with tempfile.TemporaryDirectory() as tmp, override_settings(BASE_DIR=Path(tmp)):
Path(tmp, "dhcpd.conf").write_text(
"host clone7.example.com {\n hardware ethernet 52:54:00:00:07:07;\n}\n"
)
with patch("instances.views.app_settings") as app_settings:
app_settings.CLONE_INSTANCE_DEFAULT_PREFIX = "clone"
response = self.client.get(reverse("instances:guess_clone_name"))
self.assertEqual(response.json(), {"name": "clone7"})
self.assertEqual(get_dhcp_mac_address("clone7"), "52:54:00:00:07:07")

View file

@ -231,8 +231,14 @@ def refr(compute):
refresh_instance_database(compute)
def dhcpd_conf():
"""Optional dhcpd.conf in the application directory; clone names and MAC
addresses are taken from its host entries when it exists."""
return os.path.join(str(settings.BASE_DIR), "dhcpd.conf")
def get_dhcp_mac_address(vname):
dhcp_file = str(settings.BASE_DIR) + "/dhcpd.conf"
dhcp_file = dhcpd_conf()
mac = ""
if os.path.isfile(dhcp_file):
with open(dhcp_file, "r") as f:

View file

@ -273,7 +273,7 @@ def random_mac_address(request):
@superuser_only
def guess_clone_name(request):
dhcp_file = "/srv/webvirtcloud/dhcpd.conf"
dhcp_file = utils.dhcpd_conf()
prefix = app_settings.CLONE_INSTANCE_DEFAULT_PREFIX
if os.path.isfile(dhcp_file):
instance_names = [