-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathgenerate_systemctl_config.py
More file actions
68 lines (53 loc) · 2 KB
/
generate_systemctl_config.py
File metadata and controls
68 lines (53 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""
Generates a systemd service configuration file for a Python application, and deploys and installs it at the systemd directory.
Must be run with root privileges, e.g.,
```
sudo $(which uv) run generate_systemctl_config.py
```
Usage: Edit the template below and run it with `uv` in the same directory as this script.
"""
import sys
import os
import subprocess
service_file_content = f"""
[Unit]
Description=IoT Inspector Packet Collector
After=network.target
[Service]
# ExecStart=/usr/bin/gunicorn -w 4 -b 0.0.0.0:5000 src.iot_inspector.server.packet_collector:app
ExecStart=/usr/bin/gunicorn -w 4 -b 0.0.0.0:5000 --access-logfile - --access-logformat '%%(h)s "%%(r)s" %%(s)s %%(b)s' src.iot_inspector.server.packet_collector:app
WorkingDirectory={os.path.dirname(os.path.abspath(__file__))}
User={os.getenv("SUDO_USER", "root")}
Environment=PYTHONUNBUFFERED=1
# Restart service automatically if it crashes
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
"""
def main():
# Check if the script is run with root privileges
if os.geteuid() != 0:
print("This script must be run with root privileges.")
sys.exit(1)
# Write the service file to the systemd directory
systemd_dir = "/etc/systemd/system"
service_name = "iot-inspector-packet-collector.service"
service_file_path = os.path.join(systemd_dir, service_name)
with open(service_file_path, "w") as f:
f.write(service_file_content)
commands = [
["systemctl", "daemon-reload"],
["systemctl", "enable", service_name],
["systemctl", "start", service_name],
]
for cmd in commands:
try:
subprocess.run(cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
except subprocess.CalledProcessError as e:
print(f"Command {' '.join(cmd)} failed (exit {e.returncode}): {e.stderr.strip()}")
sys.exit(e.returncode)
else:
print(f"Success: {' '.join(cmd)}")
if __name__ == "__main__":
main()