From 292b064d743522d11179bd3cd91a08731380f3a9 Mon Sep 17 00:00:00 2001 From: Christian Breunig Date: Fri, 4 Aug 2023 20:12:23 +0200 Subject: dhcp: T5428: provide common direcotry path via vyos.defaults.directories Multiple scripts use the same hardcoded path for DHCP client leases in different direcotries - this can't be worse. --- python/vyos/defaults.py | 3 ++- python/vyos/ifconfig/interface.py | 3 ++- python/vyos/template.py | 2 +- src/conf_mode/vpn_ipsec.py | 5 +++-- src/op_mode/dhcp.py | 3 ++- 5 files changed, 10 insertions(+), 6 deletions(-) diff --git a/python/vyos/defaults.py b/python/vyos/defaults.py index d4ffc249e..cb3e9b654 100644 --- a/python/vyos/defaults.py +++ b/python/vyos/defaults.py @@ -32,7 +32,8 @@ directories = { 'api_schema': f'{base_dir}/services/api/graphql/graphql/schema/', 'api_client_op': f'{base_dir}/services/api/graphql/graphql/client_op/', 'api_templates': f'{base_dir}/services/api/graphql/session/templates/', - 'vyos_udev_dir' : '/run/udev/vyos' + 'vyos_udev_dir' : '/run/udev/vyos', + 'isc_dhclient_dir' : '/run/dhclient' } config_status = '/tmp/vyos-config-status' diff --git a/python/vyos/ifconfig/interface.py b/python/vyos/ifconfig/interface.py index 99ddb2021..fe8752d24 100644 --- a/python/vyos/ifconfig/interface.py +++ b/python/vyos/ifconfig/interface.py @@ -31,6 +31,7 @@ from vyos import ConfigError from vyos.configdict import list_diff from vyos.configdict import dict_merge from vyos.configdict import get_vlan_ids +from vyos.defaults import directories from vyos.template import render from vyos.utils.network import mac2eui64 from vyos.utils.dict import dict_search @@ -1240,7 +1241,7 @@ class Interface(Control): raise ValueError() ifname = self.ifname - config_base = r'/var/lib/dhcp/dhclient' + config_base = directories['isc_dhclient_dir'] + '/dhclient' config_file = f'{config_base}_{ifname}.conf' options_file = f'{config_base}_{ifname}.options' pid_file = f'{config_base}_{ifname}.pid' diff --git a/python/vyos/template.py b/python/vyos/template.py index 7d1c3970f..6469623fd 100644 --- a/python/vyos/template.py +++ b/python/vyos/template.py @@ -420,7 +420,7 @@ def get_dhcp_router(interface): Returns False of no router is found, returns the IP address as string if a router is found. """ - lease_file = f'/var/lib/dhcp/dhclient_{interface}.leases' + lease_file = directories['isc_dhclient_dir'] + f'/dhclient_{interface}.leases' if not os.path.exists(lease_file): return None diff --git a/src/conf_mode/vpn_ipsec.py b/src/conf_mode/vpn_ipsec.py index b0825d0ee..51e2869da 100755 --- a/src/conf_mode/vpn_ipsec.py +++ b/src/conf_mode/vpn_ipsec.py @@ -28,6 +28,7 @@ from vyos.config import Config from vyos.configdict import leaf_node_changed from vyos.configverify import verify_interface_exists from vyos.configdict import dict_merge +from vyos.defaults import directories from vyos.ifconfig import Interface from vyos.pki import encode_public_key from vyos.pki import load_private_key @@ -69,7 +70,6 @@ KEY_PATH = f'{swanctl_dir}/private/' CA_PATH = f'{swanctl_dir}/x509ca/' CRL_PATH = f'{swanctl_dir}/x509crl/' -DHCP_BASE = '/var/lib/dhcp/dhclient' DHCP_HOOK_IFLIST = '/tmp/ipsec_dhcp_waiting' def get_config(config=None): @@ -433,8 +433,9 @@ def verify(ipsec): dhcp_interface = peer_conf['dhcp_interface'] verify_interface_exists(dhcp_interface) + dhcp_base = directories['isc_dhclient_dir'] - if not os.path.exists(f'{DHCP_BASE}_{dhcp_interface}.conf'): + if not os.path.exists(f'{dhcp_base}/dhclient_{dhcp_interface}.conf'): raise ConfigError(f"Invalid dhcp-interface on site-to-site peer {peer}") address = get_dhcp_address(dhcp_interface) diff --git a/src/op_mode/dhcp.py b/src/op_mode/dhcp.py index 20ef698bd..544156e1c 100755 --- a/src/op_mode/dhcp.py +++ b/src/op_mode/dhcp.py @@ -295,8 +295,9 @@ def show_server_leases(raw: bool, family: ArgFamily, pool: typing.Optional[str], def _get_raw_client_leases(family='inet', interface=None): from time import mktime from datetime import datetime + from vyos.defaults import directories - lease_dir = '/var/lib/dhcp' + lease_dir = directories['isc_dhclient_dir'] lease_files = [] lease_data = [] -- cgit v1.2.3