2018-06-26 18:35:58 -07:00
|
|
|
"""
|
|
|
|
|
Wraps systemctl to install, uninstall, start & stop systemd services.
|
|
|
|
|
|
|
|
|
|
If we use a debian package instead, we can get rid of all this code.
|
|
|
|
|
"""
|
|
|
|
|
import subprocess
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def reload_daemon():
|
|
|
|
|
"""
|
|
|
|
|
Equivalent to systemctl daemon-reload.
|
|
|
|
|
|
|
|
|
|
Makes systemd discover new units.
|
|
|
|
|
"""
|
2021-11-03 23:55:34 +01:00
|
|
|
subprocess.run(["systemctl", "daemon-reload"], check=True)
|
2018-06-26 18:35:58 -07:00
|
|
|
|
|
|
|
|
|
2021-11-03 23:55:34 +01:00
|
|
|
def install_unit(name, unit, path="/etc/systemd/system"):
|
2018-06-26 18:35:58 -07:00
|
|
|
"""
|
2018-08-27 17:56:10 -07:00
|
|
|
Install unit with given name
|
2018-06-26 18:35:58 -07:00
|
|
|
"""
|
2021-11-03 23:55:34 +01:00
|
|
|
with open(os.path.join(path, name), "w") as f:
|
2018-06-27 02:10:26 -07:00
|
|
|
f.write(unit)
|
2018-06-26 18:35:58 -07:00
|
|
|
|
|
|
|
|
|
2021-11-03 23:55:34 +01:00
|
|
|
def uninstall_unit(name, path="/etc/systemd/system"):
|
2018-06-26 18:35:58 -07:00
|
|
|
"""
|
|
|
|
|
Uninstall unit with given name
|
|
|
|
|
"""
|
2021-11-03 23:55:34 +01:00
|
|
|
subprocess.run(["rm", os.path.join(path, name)], check=True)
|
2018-06-26 18:35:58 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def start_service(name):
|
|
|
|
|
"""
|
|
|
|
|
Start service with given name.
|
|
|
|
|
"""
|
2021-11-03 23:55:34 +01:00
|
|
|
subprocess.run(["systemctl", "start", name], check=True)
|
2018-06-27 18:07:59 -07:00
|
|
|
|
|
|
|
|
|
2019-02-18 15:08:53 +02:00
|
|
|
def stop_service(name):
|
|
|
|
|
"""
|
|
|
|
|
Start service with given name.
|
|
|
|
|
"""
|
2021-11-03 23:55:34 +01:00
|
|
|
subprocess.run(["systemctl", "stop", name], check=True)
|
2019-02-18 15:08:53 +02:00
|
|
|
|
|
|
|
|
|
2018-06-27 18:07:59 -07:00
|
|
|
def restart_service(name):
|
|
|
|
|
"""
|
|
|
|
|
Restart service with given name.
|
|
|
|
|
"""
|
2021-11-03 23:55:34 +01:00
|
|
|
subprocess.run(["systemctl", "restart", name], check=True)
|
2018-06-28 00:49:36 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def enable_service(name):
|
|
|
|
|
"""
|
|
|
|
|
Enable a service with given name.
|
|
|
|
|
|
|
|
|
|
This most likely makes the service start on bootup
|
|
|
|
|
"""
|
2021-11-03 23:55:34 +01:00
|
|
|
subprocess.run(["systemctl", "enable", name], check=True)
|
2019-01-22 16:24:38 +02:00
|
|
|
|
|
|
|
|
|
2019-02-18 15:08:53 +02:00
|
|
|
def disable_service(name):
|
|
|
|
|
"""
|
|
|
|
|
Enable a service with given name.
|
|
|
|
|
|
|
|
|
|
This most likely makes the service start on bootup
|
|
|
|
|
"""
|
2021-11-03 23:55:34 +01:00
|
|
|
subprocess.run(["systemctl", "disable", name], check=True)
|
2019-02-18 15:08:53 +02:00
|
|
|
|
|
|
|
|
|
2019-01-22 16:24:38 +02:00
|
|
|
def check_service_active(name):
|
|
|
|
|
"""
|
|
|
|
|
Check if a service is currently active (running)
|
|
|
|
|
"""
|
|
|
|
|
try:
|
2021-11-03 23:55:34 +01:00
|
|
|
subprocess.run(["systemctl", "is-active", name], check=True)
|
2019-01-22 16:24:38 +02:00
|
|
|
return True
|
|
|
|
|
except subprocess.CalledProcessError:
|
2019-02-12 14:35:59 +02:00
|
|
|
return False
|
2019-02-18 15:08:53 +02:00
|
|
|
|
2021-11-01 09:42:45 +01:00
|
|
|
|
2019-02-18 15:08:53 +02:00
|
|
|
def check_service_enabled(name):
|
|
|
|
|
"""
|
|
|
|
|
Check if a service is enabled
|
|
|
|
|
"""
|
|
|
|
|
try:
|
2021-11-03 23:55:34 +01:00
|
|
|
subprocess.run(["systemctl", "is-enabled", name], check=True)
|
2019-02-18 15:08:53 +02:00
|
|
|
return True
|
|
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
|
return False
|