mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-09-19 04:59:01 +00:00
action_utils: Limited fallback to sysvinit
In case of start/stop/restart/reload and to check whether service is running, fallback to sysvinit scripts when systemd is not running. This happens inside a chroot environment when setup of essential components is happening.
This commit is contained in:
parent
401d3870f6
commit
8d9480901b
@ -20,18 +20,30 @@ Python action utility functions.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
from django.utils.translation import ugettext as _
|
from django.utils.translation import ugettext as _
|
||||||
|
import os
|
||||||
import psutil
|
import psutil
|
||||||
import socket
|
import socket
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
|
|
||||||
|
def is_systemd_running():
|
||||||
|
"""Return if we are running under systemd."""
|
||||||
|
return os.path.exists('/run/systemd')
|
||||||
|
|
||||||
|
|
||||||
def service_is_running(servicename):
|
def service_is_running(servicename):
|
||||||
"""Return whether a service is currently running.
|
"""Return whether a service is currently running.
|
||||||
|
|
||||||
Does not need to run as root.
|
Does not need to run as root.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
subprocess.check_output(['systemctl', 'status', servicename])
|
if is_systemd_running():
|
||||||
|
subprocess.run(['systemctl', 'status', servicename], check=True,
|
||||||
|
stdout=subprocess.DEVNULL)
|
||||||
|
else:
|
||||||
|
subprocess.run(['service', servicename, 'status'], check=True,
|
||||||
|
stdout=subprocess.DEVNULL)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
except subprocess.CalledProcessError:
|
except subprocess.CalledProcessError:
|
||||||
# If a service is not running we get a status code != 0 and
|
# If a service is not running we get a status code != 0 and
|
||||||
@ -64,23 +76,43 @@ def service_disable(service_name):
|
|||||||
|
|
||||||
|
|
||||||
def service_start(service_name):
|
def service_start(service_name):
|
||||||
"""Start a service."""
|
"""Start a service with systemd or sysvinit."""
|
||||||
subprocess.call(['systemctl', 'start', service_name])
|
if is_systemd_running():
|
||||||
|
subprocess.run(['systemctl', 'start', service_name],
|
||||||
|
stdout=subprocess.DEVNULL)
|
||||||
|
else:
|
||||||
|
subprocess.run(['service', service_name, 'start'],
|
||||||
|
stdout=subprocess.DEVNULL)
|
||||||
|
|
||||||
|
|
||||||
def service_stop(service_name):
|
def service_stop(service_name):
|
||||||
"""Stop a service."""
|
"""Stop a service with systemd or sysvinit."""
|
||||||
subprocess.call(['systemctl', 'stop', service_name])
|
if is_systemd_running():
|
||||||
|
subprocess.run(['systemctl', 'stop', service_name],
|
||||||
|
stdout=subprocess.DEVNULL)
|
||||||
|
else:
|
||||||
|
subprocess.run(['service', service_name, 'stop'],
|
||||||
|
stdout=subprocess.DEVNULL)
|
||||||
|
|
||||||
|
|
||||||
def service_restart(service_name):
|
def service_restart(service_name):
|
||||||
"""Restart service with systemd."""
|
"""Restart a service with systemd or sysvinit."""
|
||||||
subprocess.call(['systemctl', 'restart', service_name])
|
if is_systemd_running():
|
||||||
|
subprocess.run(['systemctl', 'restart', service_name],
|
||||||
|
stdout=subprocess.DEVNULL)
|
||||||
|
else:
|
||||||
|
subprocess.run(['service', service_name, 'restart'],
|
||||||
|
stdout=subprocess.DEVNULL)
|
||||||
|
|
||||||
|
|
||||||
def service_reload(service_name):
|
def service_reload(service_name):
|
||||||
"""Reload service with systemd."""
|
"""Reload a service with systemd or sysvinit."""
|
||||||
subprocess.call(['systemctl', 'reload', service_name])
|
if is_systemd_running():
|
||||||
|
subprocess.run(['systemctl', 'reload', service_name],
|
||||||
|
stdout=subprocess.DEVNULL)
|
||||||
|
else:
|
||||||
|
subprocess.run(['service', service_name, 'reload'],
|
||||||
|
stdout=subprocess.DEVNULL)
|
||||||
|
|
||||||
|
|
||||||
def webserver_is_enabled(name, kind='config'):
|
def webserver_is_enabled(name, kind='config'):
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user