diff --git a/actions/deluge b/actions/deluge index 9a97206ee..3dc21d975 100755 --- a/actions/deluge +++ b/actions/deluge @@ -25,6 +25,8 @@ import argparse import os import subprocess +from plinth import action_utils + SYSTEMD_SERVICE_PATH = '/etc/systemd/system/deluge-web.service' SYSTEMD_SERVICE = ''' @@ -94,40 +96,21 @@ def subcommand_disable(_): def subcommand_is_running(_): """Get whether deluge-web is running.""" - try: - output = subprocess.check_output(['systemctl', 'status', 'deluge-web']) - except subprocess.CalledProcessError: - print('no') - else: - running = False - for line in output.decode().split('\n'): - if 'Active' in line and 'running' in line: - running = True - break - - print('yes' if running else 'no') + print('yes' if action_utils.service_is_running('deluge-web') else 'no') def enable(): """Start and enable deluge-web service.""" - subprocess.check_call(['systemctl', 'enable', 'deluge-web']) - subprocess.check_call(['systemctl', 'start', 'deluge-web']) - + action_utils.service_enable('deluge-web') subprocess.check_call(['a2enconf', 'deluge-plinth']) - subprocess.check_call(['service', 'apache2', 'reload']) + action_utils.service_reload('apache2') def disable(): """Stop and disable deluge-web service.""" subprocess.check_call(['a2disconf', 'deluge-plinth']) - subprocess.check_call(['service', 'apache2', 'reload']) - - try: - subprocess.check_output(['systemctl', 'stop', 'deluge-web']) - except subprocess.CalledProcessError: - pass - - subprocess.check_output(['systemctl', 'disable', 'deluge-web']) + action_utils.service_reload('apache2') + action_utils.service_disable('deluge-web') def setup(): diff --git a/actions/ikiwiki b/actions/ikiwiki index 29382ee1d..aac7e3dab 100755 --- a/actions/ikiwiki +++ b/actions/ikiwiki @@ -25,6 +25,8 @@ import os import shutil import subprocess +from plinth import action_utils + CONFIG_ENABLE = '/etc/apache2/conf-enabled/ikiwiki-plinth.conf' SETUP_WIKI = '/etc/ikiwiki/plinth-wiki.setup' @@ -93,13 +95,13 @@ def subcommand_get_enabled(_): def subcommand_enable(_): """Enable ikiwiki site.""" subprocess.check_call(['a2enconf', 'ikiwiki-plinth']) - subprocess.check_call(['service', 'apache2', 'reload']) + action_utils.service_reload('apache2') def subcommand_disable(_): """Disable ikiwiki site.""" subprocess.check_call(['a2disconf', 'ikiwiki-plinth']) - subprocess.check_call(['service', 'apache2', 'reload']) + action_utils.service_reload('apache2') def subcommand_get_sites(_): @@ -159,7 +161,7 @@ def setup(): subprocess.check_call(['a2enmod', 'cgi']) subprocess.check_call(['a2enmod', 'authnz_ldap']) subprocess.check_call(['a2enconf', 'ikiwiki-plinth']) - subprocess.check_call(['service', 'apache2', 'restart']) + action_utils.service_restart('apache2') def main(): diff --git a/actions/mumble b/actions/mumble index 588132985..86bdd2505 100755 --- a/actions/mumble +++ b/actions/mumble @@ -24,6 +24,8 @@ Configuration helper for Mumble server import argparse import subprocess +from plinth import action_utils + SERVICE_CONFIG = '/etc/default/mumble-server' @@ -68,12 +70,12 @@ def subcommand_get_enabled(_): def subcommand_enable(_): """Start service.""" set_service_enable(enable=True) - subprocess.call(['service', 'mumble-server', 'start']) + action_utils.service_start('mumble-server') def subcommand_disable(_): """Stop service.""" - subprocess.call(['service', 'mumble-server', 'stop']) + action_utils.service_stop('mumble-server') set_service_enable(enable=False) @@ -95,21 +97,7 @@ def set_service_enable(enable): def subcommand_is_running(_): """Get whether server is running.""" - try: - output = subprocess.check_output(['service', 'mumble-server', - 'status']) - except subprocess.CalledProcessError: - # If daemon is not running we get a status code != 0 and a - # CalledProcessError - print('no') - else: - running = False - for line in output.decode().split('\n'): - if 'Active' in line and 'running' in line: - running = True - break - - print('yes' if running else 'no') + print('yes' if action_utils.service_is_running('mumble-server') else 'no') def main(): diff --git a/actions/pagekite b/actions/pagekite index 0f13da2fc..437cb8854 100755 --- a/actions/pagekite +++ b/actions/pagekite @@ -81,13 +81,6 @@ def parse_arguments(): return parser.parse_args() -def _service(action): - """Start/stop/restart the pagekite service""" - error = subprocess.call(['service', 'pagekite', action]) - if error: - raise Exception('Unable to %s PageKite server' % action) - - def subcommand_is_running(_): """Print whether pagekite is enabled (yes or no)""" print('yes' if action_utils.service_is_running('pagekite') else 'no') @@ -95,7 +88,7 @@ def subcommand_is_running(_): def subcommand_restart(_): """Restart the pagekite service""" - _service('restart') + action_utils.service_restart('pagekite') print('restarted') @@ -103,12 +96,12 @@ def subcommand_start_and_enable(_): aug.remove(PATHS['abort_not_configured']) aug.save() # 'start' alone sometimes fails, even if the service is not running - _service('restart') + action_utils.service_restart('pagekite') print('enabled') def subcommand_stop_and_disable(_): - _service('stop') + action_utils.service_stop('pagekite') aug.set(PATHS['abort_not_configured'], '') aug.save() print('disabled') @@ -175,7 +168,7 @@ def subcommand_remove_service(arguments): file.writelines(lines) # abort to only allow deleting one service break - _service('restart') + action_utils.service_restart('pagekite') def get_existing_service_paths(service): @@ -202,7 +195,7 @@ def subcommand_add_service(arguments): with open(path, 'a') as servicefile: line = "\nservice_on = %s\n" % utils.convert_service_to_string(service) servicefile.write(line) - _service('restart') + action_utils.service_restart('pagekite') def convert_augeas_path_to_filepath(augpath, prefix='/files', diff --git a/actions/roundcube b/actions/roundcube index fa9c4efc0..08ab5bb20 100755 --- a/actions/roundcube +++ b/actions/roundcube @@ -25,6 +25,8 @@ import argparse import re import subprocess +from plinth import action_utils + APACHE_CONF = '/etc/apache2/conf-available/roundcube.conf' APACHE_ENABLED_CONF = '/etc/apache2/conf-enabled/roundcube.conf' @@ -70,7 +72,7 @@ def subcommand_setup(_): else: conffile.write(line) - subprocess.call(['service', 'apache2', 'reload']) + action_utils.service_reload('apache2') def subcommand_get_enabled(_): @@ -85,13 +87,13 @@ def subcommand_get_enabled(_): def subcommand_enable(_): """Start service.""" subprocess.call(['a2enconf', 'roundcube']) - subprocess.call(['service', 'apache2', 'reload']) + action_utils.service_reload('apache2') def subcommand_disable(_): """Stop service.""" subprocess.call(['a2disconf', 'roundcube']) - subprocess.call(['service', 'apache2', 'reload']) + action_utils.service_reload('apache2') def main(): diff --git a/actions/tor b/actions/tor index dd9b87ef3..e0a7e36cc 100755 --- a/actions/tor +++ b/actions/tor @@ -23,11 +23,9 @@ Configuration helper for the Tor service import argparse import os -import subprocess from plinth import action_utils -SERVICE_CONFIG = '/etc/default/tor' TOR_CONFIG = '/etc/tor/torrc' @@ -39,11 +37,11 @@ def parse_arguments(): # Get whether Tor is running subparsers.add_parser('is-running', help='Get whether Tor is running') - # Start Tor and enable service - subparsers.add_parser('start', help='Start Tor service') + # Enable and start the service + subparsers.add_parser('enable', help='Enable and start Tor service') - # Stop Tor and disable service - subparsers.add_parser('stop', help='Stop Tor service') + # Disable and stop the service + subparsers.add_parser('disable', help='Disable and stop Tor service') # Get currently configured Tor hidden service information subparsers.add_parser('get-hs', help='Get hidden service') @@ -62,16 +60,14 @@ def subcommand_is_running(_): print('yes' if action_utils.service_is_running('tor') else 'no') -def subcommand_start(_): - """Start Tor and enable it as service""" - set_tor_service(enable=True) - subprocess.call(['service', 'tor', 'start']) +def subcommand_enable(_): + """Enable and start the service.""" + action_utils.service_enable('tor') -def subcommand_stop(_): - """Stop Tor and disable it as service""" - set_tor_service(enable=False) - subprocess.call(['service', 'tor', 'stop']) +def subcommand_disable(_): + """Disable and stop the service.""" + action_utils.service_disable('tor') def subcommand_get_hs(_): @@ -96,7 +92,7 @@ def subcommand_enable_hs(_): with open(TOR_CONFIG, 'w') as conffile: conffile.writelines(lines) - subprocess.call(['service', 'tor', 'restart']) + action_utils.service_restart('tor') def subcommand_disable_hs(_): @@ -131,22 +127,7 @@ def subcommand_disable_hs(_): with open(TOR_CONFIG, 'w') as conffile: conffile.writelines(filtered_lines) - subprocess.call(['service', 'tor', 'restart']) - - -def set_tor_service(enable): - """Enable/disable Tor service; enable: boolean""" - newline = 'RUN_DAEMON="yes"\n' if enable else 'RUN_DAEMON="no"\n' - - with open(SERVICE_CONFIG, 'r') as file: - lines = file.readlines() - for index, line in enumerate(lines): - if line.startswith('RUN_DAEMON'): - lines[index] = newline - break - - with open(SERVICE_CONFIG, 'w') as file: - file.writelines(lines) + action_utils.service_restart('tor') def get_hidden_service(): diff --git a/actions/transmission b/actions/transmission index d7a836eb1..fc30dfd1a 100755 --- a/actions/transmission +++ b/actions/transmission @@ -25,6 +25,8 @@ import argparse import json import subprocess +from plinth import action_utils + SERVICE_CONFIG = '/etc/default/transmission-daemon' TRANSMISSION_CONFIG = '/etc/transmission-daemon/settings.json' @@ -78,16 +80,16 @@ def subcommand_get_enabled(_): def subcommand_enable(_): """Start Transmission service.""" set_service_enable(enable=True) - subprocess.call(['service', 'transmission-daemon', 'start']) + action_utils.service_start('transmission-daemon') subprocess.call(['a2enconf', 'transmission-plinth']) - subprocess.call(['service', 'apache2', 'reload']) + action_utils.service_reload('apache2') def subcommand_disable(_): """Stop Transmission service.""" subprocess.call(['a2disconf', 'transmission-plinth']) - subprocess.call(['service', 'apache2', 'reload']) - subprocess.call(['service', 'transmission-daemon', 'stop']) + action_utils.service_reload('apache2') + action_utils.service_stop('transmission-daemon') set_service_enable(enable=False) @@ -108,21 +110,7 @@ def set_service_enable(enable): def subcommand_is_running(_): """Get whether Transmission is running.""" - try: - output = subprocess.check_output(['service', 'transmission-daemon', - 'status']) - except subprocess.CalledProcessError: - # If daemon is not running we get a status code != 0 and a - # CalledProcessError - print('no') - else: - running = False - for line in output.decode().split('\n'): - if 'Active' in line and 'running' in line: - running = True - break - - print('yes' if running else 'no') + print('yes' if action_utils.service_is_running('transmission-daemon') else 'no') def subcommand_merge_configuration(arguments): @@ -138,7 +126,7 @@ def subcommand_merge_configuration(arguments): new_configuration = json.dumps(new_configuration, indent=4, sort_keys=True) open(TRANSMISSION_CONFIG, 'w').write(new_configuration) - subprocess.call(['service', 'transmission-daemon', 'reload']) + action_utils.service_reload('transmission-daemon') def main(): diff --git a/actions/xmpp b/actions/xmpp index 9043be126..5978a43cc 100755 --- a/actions/xmpp +++ b/actions/xmpp @@ -28,6 +28,9 @@ import socket import re import yaml +from plinth import action_utils + + JWCHAT_CONFIG = '/etc/jwchat/config.js' EJABBERD_CONFIG = '/etc/ejabberd/ejabberd.yml' EJABBERD_BACKUP = '/var/log/ejabberd/ejabberd.dump' @@ -134,7 +137,7 @@ def subcommand_setup(_): """Setup jwchat apache conf""" subprocess.call(['a2dissite', 'jwchat']) subprocess.call(['a2enconf', 'jwchat-plinth']) - subprocess.call(['service', 'apache2', 'reload']) + action_utils.service_reload('apache2') def subcommand_is_ldap_enabled(_): @@ -250,7 +253,7 @@ def subcommand_pre_change_hostname(arguments): def subcommand_change_hostname(arguments): """Update ejabberd and jwchat with new hostname""" - subprocess.call(['service', 'ejabberd', 'stop']) + action_utils.service_stop('ejabberd') subprocess.call(['pkill', '-u', 'ejabberd']) # Make sure there aren't files in the Mnesia spool dir @@ -258,7 +261,7 @@ def subcommand_change_hostname(arguments): subprocess.call('mv /var/lib/ejabberd/*.* /var/lib/ejabberd/oldfiles/', shell=True) - subprocess.call(['service', 'ejabberd', 'start']) + action_utils.service_start('ejabberd') # restore backup database if os.path.exists(EJABBERD_BACKUP_NEW): @@ -301,7 +304,7 @@ def subcommand_change_domainname(arguments): if domainname in conf['hosts']: return - subprocess.call(['service', 'ejabberd', 'stop']) + action_utils.service_stop('ejabberd') subprocess.call(['pkill', '-u', 'ejabberd']) # Add updated domainname to ejabberd hosts list. @@ -313,7 +316,7 @@ def subcommand_change_domainname(arguments): if re.match(r'\s*hosts:', line): conffile.write(' - "' + domainname + '"\n') - subprocess.call(['service', 'ejabberd', 'start']) + action_utils.service_start('ejabberd') def subcommand_get_vhosts(_): diff --git a/plinth/action_utils.py b/plinth/action_utils.py index 59c972bc0..79b348007 100644 --- a/plinth/action_utils.py +++ b/plinth/action_utils.py @@ -25,18 +25,12 @@ import subprocess def service_is_running(servicename): """Evaluates whether a service is currently running. Returns boolean""" try: - output = subprocess.check_output(['service', servicename, 'status']) + subprocess.check_output(['systemctl', 'status', servicename]) + return True except subprocess.CalledProcessError: - # Usually 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 # thus a CalledProcessError return False - else: - running = False # default value - for line in output.decode('utf-8').split('\n'): - if 'Active' in line and 'running' in line: - running = True - break - return running def service_is_enabled(service_name): @@ -51,15 +45,33 @@ def service_is_enabled(service_name): def service_enable(service_name): """Enable and start a service in systemd and sysvinit using update-rc.d.""" subprocess.call(['systemctl', 'enable', service_name]) - subprocess.call(['systemctl', 'start', service_name]) + service_start(service_name) def service_disable(service_name): """Disable and stop service in systemd and sysvinit using update-rc.d.""" - subprocess.call(['systemctl', 'stop', service_name]) + try: + service_stop(service_name) + except subprocess.CalledProcessError: + pass subprocess.call(['systemctl', 'disable', service_name]) +def service_start(service_name): + """Start a service.""" + subprocess.call(['systemctl', 'start', service_name]) + + +def service_stop(service_name): + """Stop a service.""" + subprocess.call(['systemctl', 'stop', service_name]) + + def service_restart(service_name): """Restart service with systemd.""" subprocess.call(['systemctl', 'restart', service_name]) + + +def service_reload(service_name): + """Reload service with systemd.""" + subprocess.call(['systemctl', 'reload', service_name])