Skip to content

Commit d2e864d

Browse files
committed
switch to using sudo
1 parent 53c74f2 commit d2e864d

5 files changed

Lines changed: 25 additions & 8 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ python3 -m venv .venv
2828
Run the existing scheduled check with mail credentials and a log file:
2929

3030
```bash
31-
.venv/bin/python wireguard.py check \
31+
sudo .venv/bin/python wireguard.py check \
3232
--user noc@example.org \
3333
--password 'mail-password' \
3434
--log /var/log/ffshmon.log
@@ -41,7 +41,7 @@ The command exits after one health cycle. If the FastD service is down, the conn
4141
Start the long-running monitor with:
4242

4343
```bash
44-
.venv/bin/python wireguard.py serve \
44+
sudo .venv/bin/python wireguard.py serve \
4545
--user noc@example.org \
4646
--password 'mail-password' \
4747
--log /var/log/ffshmon.log
@@ -71,7 +71,7 @@ A value of `1` means the latest check succeeded. A value of `0` means the FastD
7171
The listener and polling interval can be changed with `--host`, `--port`, and `--interval`:
7272

7373
```bash
74-
.venv/bin/python wireguard.py serve \
74+
sudo .venv/bin/python wireguard.py serve \
7575
--user noc@example.org \
7676
--password 'mail-password' \
7777
--log /var/log/ffshmon.log \

config_manager.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1+
"""Regenerate and restart the WireGuard configuration."""
2+
13
import subprocess
24
import logging
35

46

57
def new_config(interface):
8+
"""Regenerate the WireGuard configuration and restart its service."""
69
result = subprocess.run(
7-
["python3", "/opt/wg-conf-gen/wg-conf-gen.py", "recreate"],
10+
["sudo", "python3", "/opt/wg-conf-gen/wg-conf-gen.py", "recreate"],
811
capture_output=True,
912
text=True,
13+
check=False,
1014
)
1115
if result.returncode != 0:
1216
logging.error("Error while recreating wireguard config:")
@@ -18,8 +22,9 @@ def new_config(interface):
1822
logging.info("Output of wg-conf-gen:")
1923
logging.info(result.stdout)
2024
subprocess.run(
21-
["systemctl", "restart", f"wg-quick@{interface}.service"],
25+
["sudo", "systemctl", "restart", f"wg-quick@{interface}.service"],
2226
stdout=subprocess.DEVNULL,
2327
stderr=subprocess.DEVNULL,
28+
check=False,
2429
)
2530
logging.info("Restarted wireguard service")

hard_stop.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
"""Stop the FastD and WireGuard services during failed recovery."""
2+
13
import subprocess
24
import logging
35

46

57
def stop_fastd(service_name):
68
"""Stop fastd"""
79
subprocess.run(
8-
["systemctl", "stop", f"fastd@{service_name}.service"],
10+
["sudo", "systemctl", "stop", f"fastd@{service_name}.service"],
911
stdout=subprocess.DEVNULL,
1012
stderr=subprocess.DEVNULL,
1113
check=True,
@@ -15,8 +17,9 @@ def stop_fastd(service_name):
1517

1618
# stop wg tunnel
1719
def stop_wg(service_name):
20+
"""Stop the WireGuard service for the configured interface."""
1821
subprocess.run(
19-
["systemctl", "stop", f"wg-quick@{service_name}.service"],
22+
["sudo", "systemctl", "stop", f"wg-quick@{service_name}.service"],
2023
stdout=subprocess.DEVNULL,
2124
stderr=subprocess.DEVNULL,
2225
check=True,

test_wireguard.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def test_interface_probe_uses_requested_interface(self):
2020
self.assertTrue(wireguard.test_interface("wg-test"))
2121

2222
command = run.call_args.args[0]
23+
self.assertEqual(command[0], "sudo")
2324
self.assertIn("wg-test", command)
2425

2526
def test_metrics_endpoint_exposes_cached_status(self):

wireguard.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,14 @@
2121
def is_service_running(service_name):
2222
"""Return whether the configured FastD service is running."""
2323
result = subprocess.run(
24-
["systemctl", "show", "-p", "SubState", f"fastd@{service_name}.service"],
24+
[
25+
"sudo",
26+
"systemctl",
27+
"show",
28+
"-p",
29+
"SubState",
30+
f"fastd@{service_name}.service",
31+
],
2532
capture_output=True,
2633
text=True,
2734
check=False,
@@ -32,6 +39,7 @@ def is_service_running(service_name):
3239
def test_interface(interface_name):
3340
"""Returns True if interface is ok, returns False if interface is not ok."""
3441
curl_cmd = [
42+
"sudo",
3543
"curl",
3644
"--connect-timeout",
3745
"10",

0 commit comments

Comments
 (0)