Skip to content

Commit

Permalink
Merge pull request #3199 from vyos/mergify/bp/sagitta/pr-3194
Browse files Browse the repository at this point in the history
op-mode: T6175: "renew dhcp interface <name>" does not check for DHCP interface (backport #3194)
  • Loading branch information
c-po authored Mar 28, 2024
2 parents 9ee515c + e37f58b commit 9bdd00b
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 7 deletions.
3 changes: 0 additions & 3 deletions data/templates/dhcp-client/override.conf.j2
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
{% set if_metric = '-e IF_METRIC=' ~ dhcp_options.default_route_distance if dhcp_options.default_route_distance is vyos_defined else '' %}
{% set dhclient_options = '-d -nw -cf ' ~ isc_dhclient_dir ~ '/dhclient_' ~ ifname ~ '.conf -pf ' ~ isc_dhclient_dir ~ '/dhclient_' ~ ifname ~ '.pid -lf ' ~ isc_dhclient_dir ~ '/dhclient_' ~ ifname ~ '.leases ' ~ if_metric %}

[Unit]
ConditionPathExists={{ isc_dhclient_dir }}/dhclient_%i.conf

[Service]
ExecStart=
ExecStart={{ vrf_command }}/sbin/dhclient -4 {{ dhclient_options }} {{ ifname }}
Expand Down
4 changes: 2 additions & 2 deletions op-mode-definitions/dhcp.xml.in
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@
<script>${vyos_completion_dir}/list_interfaces</script>
</completionHelp>
</properties>
<command>sudo systemctl restart "dhclient@$4.service"</command>
<command>sudo ${vyos_op_scripts_dir}/dhcp.py renew_client_lease --family inet --interface "$4"</command>
</tagNode>
</children>
</node>
Expand All @@ -227,7 +227,7 @@
<script>${vyos_completion_dir}/list_interfaces</script>
</completionHelp>
</properties>
<command>sudo systemctl restart "dhcp6c@$4.service"</command>
<command>sudo ${vyos_op_scripts_dir}/dhcp.py renew_client_lease --family inet6 --interface "$4"</command>
</tagNode>
</children>
</node>
Expand Down
2 changes: 1 addition & 1 deletion python/vyos/opmode.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class InternalError(Error):


def _is_op_mode_function_name(name):
if re.match(r"^(show|clear|reset|restart|add|update|delete|generate|set)", name):
if re.match(r"^(show|clear|reset|restart|add|update|delete|generate|set|renew)", name):
return True
else:
return False
Expand Down
32 changes: 31 additions & 1 deletion src/op_mode/dhcp.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
#
# Copyright (C) 2022-2023 VyOS maintainers and contributors
# Copyright (C) 2022-2024 VyOS maintainers and contributors
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or later as
Expand Down Expand Up @@ -33,6 +33,7 @@
from vyos.utils.file import read_file
from vyos.utils.process import cmd
from vyos.utils.process import is_systemd_service_running
from vyos.utils.process import call

time_string = "%a %b %d %H:%M:%S %Z %Y"

Expand Down Expand Up @@ -266,6 +267,25 @@ def _wrapper(*args, **kwargs):
return func(*args, **kwargs)
return _wrapper

def _verify_client(func):
"""Decorator checks if interface is configured as DHCP client"""
from functools import wraps
from vyos.ifconfig import Section

@wraps(func)
def _wrapper(*args, **kwargs):
config = ConfigTreeQuery()
family = kwargs.get('family')
v = 'v6' if family == 'inet6' else ''
interface = kwargs.get('interface')
interface_path = Section.get_config_path(interface)
unconf_message = f'DHCP{v} client not configured on interface {interface}!'

# Check if config does not exist
if not config.exists(f'interfaces {interface_path} address dhcp{v}'):
raise vyos.opmode.UnconfiguredSubsystem(unconf_message)
return func(*args, **kwargs)
return _wrapper

@_verify
def show_pool_statistics(raw: bool, family: ArgFamily, pool: typing.Optional[str]):
Expand Down Expand Up @@ -395,6 +415,16 @@ def show_client_leases(raw: bool, family: ArgFamily, interface: typing.Optional[
else:
return _get_formatted_client_leases(lease_data, family=family)

@_verify_client
def renew_client_lease(raw: bool, family: ArgFamily, interface: str):
if not raw:
v = 'v6' if family == 'inet6' else ''
print(f'Restarting DHCP{v} client on interface {interface}...')
if family == 'inet6':
call(f'systemctl restart dhcp6c@{interface}.service')
else:
call(f'systemctl restart dhclient@{interface}.service')

if __name__ == '__main__':
try:
res = vyos.opmode.run(sys.modules[__name__])
Expand Down
1 change: 1 addition & 0 deletions src/systemd/dhclient@.service
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Description=DHCP client on %i
Documentation=man:dhclient(8)
StartLimitIntervalSec=0
After=vyos-router.service
ConditionPathExists=/run/dhclient/dhclient_%i.conf

[Service]
Type=exec
Expand Down

0 comments on commit 9bdd00b

Please sign in to comment.