summaryrefslogtreecommitdiff
path: root/python/vyos/ifconfig
diff options
context:
space:
mode:
Diffstat (limited to 'python/vyos/ifconfig')
-rw-r--r--python/vyos/ifconfig/dhcp.py82
-rw-r--r--python/vyos/ifconfig/interface.py41
2 files changed, 49 insertions, 74 deletions
diff --git a/python/vyos/ifconfig/dhcp.py b/python/vyos/ifconfig/dhcp.py
index a8b9a2a87..5f99a0b7e 100644
--- a/python/vyos/ifconfig/dhcp.py
+++ b/python/vyos/ifconfig/dhcp.py
@@ -14,25 +14,22 @@
# License along with this library. If not, see <http://www.gnu.org/licenses/>.
import os
+import jmespath
-from vyos.dicts import FixedDict
+from vyos.configdict import dict_merge
+from vyos.configverify import verify_dhcpv6
from vyos.ifconfig.control import Control
from vyos.template import render
class _DHCPv4 (Control):
def __init__(self, ifname):
super().__init__()
- config_base = r'/var/lib/dhcp/dhclient_'
- self.options = FixedDict(**{
- 'ifname': ifname,
- 'hostname': '',
- 'client_id': '',
- 'vendor_class_id': '',
- 'conf_file': config_base + f'{ifname}.conf',
- 'options_file': config_base + f'{ifname}.options',
- 'pid_file': config_base + f'{ifname}.pid',
- 'lease_file': config_base + f'{ifname}.leases',
- })
+ config_base = r'/var/lib/dhcp/dhclient'
+ self._conf_file = f'{config_base}_{ifname}.conf'
+ self._options_file = f'{config_base}_{ifname}.options'
+ self._pid_file = f'{config_base}_{ifname}.pid'
+ self._lease_file = f'{config_base}_{ifname}.leases'
+ self.options = {'ifname' : ifname}
# replace dhcpv4/v6 with systemd.networkd?
def set(self):
@@ -41,19 +38,24 @@ class _DHCPv4 (Control):
started in background!
Example:
-
>>> from vyos.ifconfig import Interface
>>> j = Interface('eth0')
>>> j.dhcp.v4.set()
"""
- if not self.options['hostname']:
+
+ if jmespath.search('dhcp_options.host_name', self.options) == None:
# read configured system hostname.
# maybe change to vyos hostd client ???
+ hostname = 'vyos'
with open('/etc/hostname', 'r') as f:
- self.options['hostname'] = f.read().rstrip('\n')
+ hostname = f.read().rstrip('\n')
+ tmp = {'dhcp_options' : { 'host_name' : hostname}}
+ self.options = dict_merge(tmp, self.options)
- render(self.options['options_file'], 'dhcp-client/daemon-options.tmpl', self.options)
- render(self.options['conf_file'], 'dhcp-client/ipv4.tmpl', self.options)
+ render(self._options_file, 'dhcp-client/daemon-options.tmpl',
+ self.options, trim_blocks=True)
+ render(self._conf_file, 'dhcp-client/ipv4.tmpl',
+ self.options, trim_blocks=True)
return self._cmd('systemctl restart dhclient@{ifname}.service'.format(**self.options))
@@ -63,62 +65,53 @@ class _DHCPv4 (Control):
pid, config and lease will be removed.
Example:
-
>>> from vyos.ifconfig import Interface
>>> j = Interface('eth0')
>>> j.dhcp.v4.delete()
"""
- if not os.path.isfile(self.options['pid_file']):
+ if not os.path.isfile(self._pid_file):
self._debug_msg('No DHCP client PID found')
return None
self._cmd('systemctl stop dhclient@{ifname}.service'.format(**self.options))
# cleanup old config files
- for name in ('conf_file', 'options_file', 'pid_file', 'lease_file'):
- if os.path.isfile(self.options[name]):
- os.remove(self.options[name])
+ for file in [self._conf_file, self._options_file, self._pid_file, self._lease_file]:
+ if os.path.isfile(file):
+ os.remove(file)
class _DHCPv6 (Control):
def __init__(self, ifname):
super().__init__()
- self.options = FixedDict(**{
- 'ifname': ifname,
- 'dhcpv6_prm_only': False,
- 'dhcpv6_temporary': False,
- 'dhcpv6_pd_interfaces': [],
- 'dhcpv6_pd_length': ''
- })
- self._conf_file = f'/run/dhcp6c/dhcp6c.{ifname}.conf'
+ self.options = {'ifname' : ifname}
+ self._config = f'/run/dhcp6c/dhcp6c.{ifname}.conf'
def set(self):
"""
- Configure interface as DHCPv6 client. The dhclient binary is automatically
- started in background!
+ Configure interface as DHCPv6 client. The client is automatically
+ started in background when address is configured as DHCP.
Example:
-
>>> from vyos.ifconfig import Interface
>>> j = Interface('eth0')
>>> j.dhcp.v6.set()
"""
- # better save then sorry .. should be checked in interface script
- # but if you missed it we are safe!
- if self.options['dhcpv6_prm_only'] and self.options['dhcpv6_temporary']:
- raise Exception(
- 'DHCPv6 temporary and parameters-only options are mutually exclusive!')
+ # better save then sorry .. should be checked in interface script but if you
+ # missed it we are safe!
+ verify_dhcpv6(self.options)
- render(self._conf_file, 'dhcp-client/ipv6.tmpl', self.options, trim_blocks=True)
- return self._cmd('systemctl restart dhcp6c@{ifname}.service'.format(**self.options))
+ render(self._config, 'dhcp-client/ipv6.tmpl',
+ self.options, trim_blocks=True)
+ return self._cmd('systemctl restart dhcp6c@{ifname}.service'.format(
+ **self.options))
def delete(self):
"""
- De-configure interface as DHCPv6 clinet. All auto generated files like
+ De-configure interface as DHCPv6 client. All auto generated files like
pid, config and lease will be removed.
Example:
-
>>> from vyos.ifconfig import Interface
>>> j = Interface('eth0')
>>> j.dhcp.v6.delete()
@@ -126,9 +119,8 @@ class _DHCPv6 (Control):
self._cmd('systemctl stop dhcp6c@{ifname}.service'.format(**self.options))
# cleanup old config files
- if os.path.isfile(self._conf_file):
- os.remove(self._conf_file)
-
+ if os.path.isfile(self._config):
+ os.remove(self._config)
class DHCP(object):
def __init__(self, ifname):
diff --git a/python/vyos/ifconfig/interface.py b/python/vyos/ifconfig/interface.py
index d477153e8..214ece8cd 100644
--- a/python/vyos/ifconfig/interface.py
+++ b/python/vyos/ifconfig/interface.py
@@ -822,6 +822,14 @@ class Interface(Control):
value = '2' if 'disable_link_detect' in config else '1'
self.set_link_detect(value)
+ # DHCP options
+ if 'dhcp_options' in config:
+ self.dhcp.v4.options = config
+
+ # DHCPv6 options
+ if 'dhcpv6_options' in config:
+ self.dhcp.v6.options = config
+
# Configure assigned interface IP addresses. No longer
# configured addresses will be removed first
new_addr = config.get('address', [])
@@ -849,35 +857,6 @@ class Interface(Control):
# checked before
self.set_vrf(config.get('vrf', ''))
- # DHCP options
- if 'dhcp_options' in config:
- dhcp_options = config.get('dhcp_options')
- if 'client_id' in dhcp_options:
- self.dhcp.v4.options['client_id'] = dhcp_options.get('client_id')
-
- if 'host_name' in dhcp_options:
- self.dhcp.v4.options['hostname'] = dhcp_options.get('host_name')
-
- if 'vendor_class_id' in dhcp_options:
- self.dhcp.v4.options['vendor_class_id'] = dhcp_options.get('vendor_class_id')
-
- # DHCPv6 options
- if 'dhcpv6_options' in config:
- dhcpv6_options = config.get('dhcpv6_options')
- if 'parameters_only' in dhcpv6_options:
- self.dhcp.v6.options['dhcpv6_prm_only'] = True
-
- if 'temporary' in dhcpv6_options:
- self.dhcp.v6.options['dhcpv6_temporary'] = True
-
- if 'prefix_delegation' in dhcpv6_options:
- prefix_delegation = dhcpv6_options.get('prefix_delegation')
- if 'length' in prefix_delegation:
- self.dhcp.v6.options['dhcpv6_pd_length'] = prefix_delegation.get('length')
-
- if 'interface' in prefix_delegation:
- self.dhcp.v6.options['dhcpv6_pd_interfaces'] = prefix_delegation.get('interface')
-
# Configure ARP cache timeout in milliseconds - has default value
tmp = jmespath.search('ip.arp_cache_timeout', config)
value = tmp if (tmp != None) else '30'
@@ -982,9 +961,11 @@ class Interface(Control):
self.del_vlan(vif_s_id)
# create/update 802.1ad (Q-in-Q VLANs)
+ ifname = config['ifname']
for vif_s_id, vif_s in config.get('vif_s', {}).items():
tmp=get_ethertype(vif_s.get('ethertype', '0x88A8'))
s_vlan = self.add_vlan(vif_s_id, ethertype=tmp)
+ vif_s['ifname'] = f'{ifname}.{vif_s_id}'
s_vlan.update(vif_s)
# remove no longer required client VLAN (vif-c)
@@ -994,6 +975,7 @@ class Interface(Control):
# create/update client VLAN (vif-c) interface
for vif_c_id, vif_c in vif_s.get('vif_c', {}).items():
c_vlan = s_vlan.add_vlan(vif_c_id)
+ vif_c['ifname'] = f'{ifname}.{vif_s_id}.{vif_c_id}'
c_vlan.update(vif_c)
# remove no longer required 802.1q VLAN interfaces
@@ -1003,4 +985,5 @@ class Interface(Control):
# create/update 802.1q VLAN interfaces
for vif_id, vif in config.get('vif', {}).items():
vlan = self.add_vlan(vif_id)
+ vif['ifname'] = f'{ifname}.{vif_id}'
vlan.update(vif)