diff --git a/op-mode-definitions/dhcp.xml.in b/op-mode-definitions/dhcp.xml.in
index eee6937d60a..b3438ab808e 100644
--- a/op-mode-definitions/dhcp.xml.in
+++ b/op-mode-definitions/dhcp.xml.in
@@ -245,7 +245,7 @@
Restart DHCP server
- if cli-shell-api existsActive service dhcp-server; then sudo systemctl restart kea-dhcp4-server.service; else echo "DHCP server not configured"; fi
+ sudo ${vyos_op_scripts_dir}/restart.py restart_service --name dhcp
@@ -264,7 +264,7 @@
Restart DHCPv6 server
- if cli-shell-api existsActive service dhcpv6-server; then sudo systemctl restart kea-dhcp6-server.service; else echo "DHCPv6 server not configured"; fi
+ sudo ${vyos_op_scripts_dir}/restart.py restart_service --name dhcpv6
diff --git a/op-mode-definitions/dns-dynamic.xml.in b/op-mode-definitions/dns-dynamic.xml.in
index 45d58e2e8d1..ef0f0398894 100644
--- a/op-mode-definitions/dns-dynamic.xml.in
+++ b/op-mode-definitions/dns-dynamic.xml.in
@@ -97,7 +97,7 @@
Restart Dynamic DNS service
- if cli-shell-api existsActive service dns dynamic; then sudo systemctl restart ddclient.service; else echo "Dynamic DNS not configured"; fi
+ sudo ${vyos_op_scripts_dir}/restart.py restart_service --name dns_dynamic
diff --git a/op-mode-definitions/dns-forwarding.xml.in b/op-mode-definitions/dns-forwarding.xml.in
index 29bfc61cfd0..fac3fc345ca 100644
--- a/op-mode-definitions/dns-forwarding.xml.in
+++ b/op-mode-definitions/dns-forwarding.xml.in
@@ -73,7 +73,7 @@
Restart DNS Forwarding service
- if cli-shell-api existsActive service dns forwarding; then sudo systemctl restart pdns-recursor.service; else echo "DNS forwarding not configured"; fi
+ sudo ${vyos_op_scripts_dir}/restart.py restart_service --name dns_forwarding
diff --git a/op-mode-definitions/igmp-proxy.xml.in b/op-mode-definitions/igmp-proxy.xml.in
index 8533138d711..d6ad7ed7e24 100644
--- a/op-mode-definitions/igmp-proxy.xml.in
+++ b/op-mode-definitions/igmp-proxy.xml.in
@@ -6,7 +6,7 @@
Restart the IGMP proxy process
- sudo systemctl restart igmpproxy.service
+ sudo ${vyos_op_scripts_dir}/restart.py restart_service --name igmp_proxy
diff --git a/op-mode-definitions/mdns-reflector.xml.in b/op-mode-definitions/mdns-reflector.xml.in
index a90d4d38580..115b2858c81 100644
--- a/op-mode-definitions/mdns-reflector.xml.in
+++ b/op-mode-definitions/mdns-reflector.xml.in
@@ -53,7 +53,7 @@
Restart mDNS repeater service
- sudo systemctl restart avahi-daemon.service
+ sudo ${vyos_op_scripts_dir}/restart.py restart_service --name mdns_repeater
diff --git a/op-mode-definitions/restart-router-advert.xml.in b/op-mode-definitions/restart-router-advert.xml.in
index 304b4dfd3c9..9eea3dfc41c 100644
--- a/op-mode-definitions/restart-router-advert.xml.in
+++ b/op-mode-definitions/restart-router-advert.xml.in
@@ -6,7 +6,7 @@
Restart IPv6 Router Advertisement service
- if cli-shell-api existsActive service router-advert; then sudo systemctl restart radvd.service; else echo "IPv6 Router Advertisement service not configured"; fi
+ sudo ${vyos_op_scripts_dir}/restart.py restart_service --name router_advert
diff --git a/op-mode-definitions/restart-snmp.xml.in b/op-mode-definitions/restart-snmp.xml.in
index 7de27df64aa..e9c43de01c0 100644
--- a/op-mode-definitions/restart-snmp.xml.in
+++ b/op-mode-definitions/restart-snmp.xml.in
@@ -6,7 +6,7 @@
Restart SNMP service
- if cli-shell-api existsActive service snmp; then sudo systemctl restart snmpd.service; else echo "Service SNMP not configured"; fi
+ sudo ${vyos_op_scripts_dir}/restart.py restart_service --name snmp
diff --git a/op-mode-definitions/restart-ssh.xml.in b/op-mode-definitions/restart-ssh.xml.in
index 543cafc24f8..914586df879 100644
--- a/op-mode-definitions/restart-ssh.xml.in
+++ b/op-mode-definitions/restart-ssh.xml.in
@@ -6,7 +6,7 @@
Restart SSH service
- if cli-shell-api existsActive service ssh; then sudo systemctl restart "ssh@*.service"; else echo "Service SSH not configured"; fi
+ sudo ${vyos_op_scripts_dir}/restart.py restart_service --name ssh --vrf "*"
diff --git a/op-mode-definitions/reverse-proxy.xml.in b/op-mode-definitions/reverse-proxy.xml.in
index 4af24880b86..b45ce107f1b 100644
--- a/op-mode-definitions/reverse-proxy.xml.in
+++ b/op-mode-definitions/reverse-proxy.xml.in
@@ -6,7 +6,7 @@
Restart reverse-proxy service
- if cli-shell-api existsActive load-balancing reverse-proxy; then sudo systemctl restart haproxy.service; else echo "Reverse-Proxy not configured"; fi
+ sudo ${vyos_op_scripts_dir}/restart.py restart_service --name reverse_proxy
diff --git a/op-mode-definitions/suricata.xml.in b/op-mode-definitions/suricata.xml.in
new file mode 100644
index 00000000000..ff1f84706d5
--- /dev/null
+++ b/op-mode-definitions/suricata.xml.in
@@ -0,0 +1,23 @@
+
+
+
+
+
+
+ Update Suricata
+
+ if test -f /run/suricata/suricata.yaml; then sudo suricata-update --suricata-conf /run/suricata/suricata.yaml; sudo systemctl restart suricata; else echo "Service Suricata not configured"; fi
+
+
+
+
+
+
+
+ Restart Suricata service
+
+ sudo ${vyos_op_scripts_dir}/restart.py restart_service --name suricata
+
+
+
+
diff --git a/op-mode-definitions/vpn-ipsec.xml.in b/op-mode-definitions/vpn-ipsec.xml.in
index b551af2be9e..0a8671aeb16 100644
--- a/op-mode-definitions/vpn-ipsec.xml.in
+++ b/op-mode-definitions/vpn-ipsec.xml.in
@@ -112,7 +112,7 @@
Restart the IPsec VPN process
- if systemctl is-active --quiet strongswan; then sudo systemctl restart strongswan ; echo "IPsec process restarted"; else echo "IPsec process not running" ; fi
+ sudo ${vyos_op_scripts_dir}/restart.py restart_service --name ipsec
diff --git a/op-mode-definitions/vrrp.xml.in b/op-mode-definitions/vrrp.xml.in
index 34484c70602..158e7093e09 100644
--- a/op-mode-definitions/vrrp.xml.in
+++ b/op-mode-definitions/vrrp.xml.in
@@ -30,7 +30,7 @@
Restart VRRP (Virtual Router Redundancy Protocol) process
- sudo systemctl restart keepalived.service
+ sudo ${vyos_op_scripts_dir}/restart.py restart_service --name vrrp
diff --git a/op-mode-definitions/webproxy.xml.in b/op-mode-definitions/webproxy.xml.in
index 57df44ff872..ba13907b894 100644
--- a/op-mode-definitions/webproxy.xml.in
+++ b/op-mode-definitions/webproxy.xml.in
@@ -34,7 +34,7 @@
Restart WebProxy service
- if cli-shell-api existsActive service webproxy; then sudo systemctl restart squid.service; else echo "Service WebProxy not configured"; fi
+ sudo ${vyos_op_scripts_dir}/restart.py restart_service --name webproxy
diff --git a/src/op_mode/restart.py b/src/op_mode/restart.py
new file mode 100755
index 00000000000..813d3a2b754
--- /dev/null
+++ b/src/op_mode/restart.py
@@ -0,0 +1,127 @@
+#!/usr/bin/env python3
+#
+# Copyright (C) 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 as
+# published by the Free Software Foundation.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see .
+
+import sys
+import typing
+import vyos.opmode
+
+from vyos.configquery import ConfigTreeQuery
+from vyos.utils.process import call
+from vyos.utils.commit import commit_in_progress
+
+config = ConfigTreeQuery()
+
+service_map = {
+ 'dhcp' : {
+ 'systemd_service': 'kea-dhcp4-server',
+ 'path': ['service', 'dhcp-server'],
+ },
+ 'dhcpv6' : {
+ 'systemd_service': 'kea-dhcp6-server',
+ 'path': ['service', 'dhcpv6-server'],
+ },
+ 'dns_dynamic': {
+ 'systemd_service': 'ddclient',
+ 'path': ['service', 'dns', 'dynamic'],
+ },
+ 'dns_forwarding': {
+ 'systemd_service': 'pdns-recursor',
+ 'path': ['service', 'dns', 'forwarding'],
+ },
+ 'igmp_proxy': {
+ 'systemd_service': 'igmpproxy',
+ 'path': ['protocols', 'igmp-proxy'],
+ },
+ 'ipsec': {
+ 'systemd_service': 'strongswan',
+ 'path': ['vpn', 'ipsec'],
+ },
+ 'mdns_repeater': {
+ 'systemd_service': 'avahi-daemon',
+ 'path': ['service', 'mdns', 'repeater'],
+ },
+ 'reverse_proxy': {
+ 'systemd_service': 'haproxy',
+ 'path': ['load-balancing', 'reverse-proxy'],
+ },
+ 'router_advert': {
+ 'systemd_service': 'radvd',
+ 'path': ['service', 'router-advert'],
+ },
+ 'snmp' : {
+ 'systemd_service': 'snmpd',
+ },
+ 'ssh' : {
+ 'systemd_service': 'ssh',
+ },
+ 'suricata' : {
+ 'systemd_service': 'suricata',
+ },
+ 'vrrp' : {
+ 'systemd_service': 'keepalived',
+ 'path': ['high-availability', 'vrrp'],
+ },
+ 'webproxy' : {
+ 'systemd_service': 'squid',
+ },
+}
+services = typing.Literal['dhcp', 'dhcpv6', 'dns_dynamic', 'dns_forwarding', 'igmp_proxy', 'ipsec', 'mdns_repeater', 'reverse_proxy', 'router_advert', 'snmp', 'ssh', 'suricata' 'vrrp', 'webproxy']
+
+def _verify(func):
+ """Decorator checks if DHCP(v6) config exists"""
+ from functools import wraps
+
+ @wraps(func)
+ def _wrapper(*args, **kwargs):
+ config = ConfigTreeQuery()
+ name = kwargs.get('name')
+ human_name = name.replace('_', '-')
+
+ if commit_in_progress():
+ print(f'Cannot restart {human_name} service while a commit is in progress')
+ sys.exit(1)
+
+ # Get optional CLI path from service_mapping dict
+ # otherwise use "service name" CLI path
+ path = ['service', name]
+ if 'path' in service_map[name]:
+ path = service_map[name]['path']
+
+ # Check if config does not exist
+ if not config.exists(path):
+ raise vyos.opmode.UnconfiguredSubsystem(f'Service {human_name} is not configured!')
+ if config.exists(path + ['disable']):
+ raise vyos.opmode.UnconfiguredSubsystem(f'Service {human_name} is disabled!')
+ return func(*args, **kwargs)
+
+ return _wrapper
+
+@_verify
+def restart_service(raw: bool, name: services, vrf: typing.Optional[str]):
+ systemd_service = service_map[name]['systemd_service']
+ if vrf:
+ call(f'systemctl restart "{systemd_service}@{vrf}.service"')
+ else:
+ call(f'systemctl restart "{systemd_service}.service"')
+
+if __name__ == '__main__':
+ try:
+ res = vyos.opmode.run(sys.modules[__name__])
+ if res:
+ print(res)
+ except (ValueError, vyos.opmode.Error) as e:
+ print(e)
+ sys.exit(1)