deluge: Auto start deluge-web using systemd

This commit is contained in:
Sunil Mohan Adapa 2015-05-10 14:45:51 +05:30
parent a6fa7abbad
commit 939f330816
4 changed files with 54 additions and 74 deletions

View File

@ -26,9 +26,8 @@ import os
import subprocess import subprocess
SITE_CONF = '/etc/apache2/conf-available/deluge-web.conf' APACHE_CONF_PATH = '/etc/apache2/conf-available/deluge-web.conf'
SITE_ENABLED = '/etc/apache2/conf-enabled/deluge-web.conf' APACHE_CONF_ENABLED_PATH = '/etc/apache2/conf-enabled/deluge-web.conf'
APACHE_CONF = ''' APACHE_CONF = '''
## ##
## On all sites, provide Deluge on a default path: /deluge ## On all sites, provide Deluge on a default path: /deluge
@ -41,6 +40,27 @@ APACHE_CONF = '''
</Location> </Location>
''' '''
SYSTEMD_SERVICE_PATH = '/etc/systemd/system/deluge-web.service'
SYSTEMD_SERVICE = '''
#
# This file is managed and overwritten by Plinth. If you wish to edit
# it, disable Deluge in Plinth, remove this file and manage it manually.
#
[Unit]
Description=Deluge Web Interface
Documentation=man:deluge-web(1)
After=network.target
[Service]
ExecStart=/usr/bin/deluge-web --base=deluge
Restart=on-failure
User=debian-deluged
Group=debian-deluged
[Install]
WantedBy=multi-user.target
'''
def parse_arguments(): def parse_arguments():
"""Return parsed command line arguments as dictionary.""" """Return parsed command line arguments as dictionary."""
@ -61,18 +81,13 @@ def parse_arguments():
subparsers.add_parser('is-running', subparsers.add_parser('is-running',
help='Get whether deluge-web is running') help='Get whether deluge-web is running')
# Start deluge-web
subparsers.add_parser('start', help='Start deluge-web')
# Stop deluge-web
subparsers.add_parser('stop', help='Stop deluge-web')
return parser.parse_args() return parser.parse_args()
def subcommand_get_enabled(_): def subcommand_get_enabled(_):
"""Get whether deluge-web site is enabled.""" """Get whether deluge-web site is enabled."""
if os.path.isfile(SITE_ENABLED): if os.path.isfile(APACHE_CONF_ENABLED_PATH) and \
os.path.isfile(SYSTEMD_SERVICE_PATH):
print('yes') print('yes')
else: else:
print('no') print('no')
@ -80,11 +95,9 @@ def subcommand_get_enabled(_):
def subcommand_enable(_): def subcommand_enable(_):
"""Enable deluge-web site and start deluge-web.""" """Enable deluge-web site and start deluge-web."""
if not os.path.isfile(SITE_CONF): setup()
setup()
subcommand_start(_)
start()
subprocess.check_call(['a2enconf', 'deluge-web']) subprocess.check_call(['a2enconf', 'deluge-web'])
subprocess.check_call(['service', 'apache2', 'reload']) subprocess.check_call(['service', 'apache2', 'reload'])
@ -93,47 +106,50 @@ def subcommand_disable(_):
"""Disable deluge-web site and stop deluge-web.""" """Disable deluge-web site and stop deluge-web."""
subprocess.check_call(['a2disconf', 'deluge-web']) subprocess.check_call(['a2disconf', 'deluge-web'])
subprocess.check_call(['service', 'apache2', 'reload']) subprocess.check_call(['service', 'apache2', 'reload'])
stop()
subcommand_stop(_)
def subcommand_is_running(_): def subcommand_is_running(_):
"""Get whether deluge-web is running.""" """Get whether deluge-web is running."""
try: try:
subprocess.check_call(['start-stop-daemon', '--status', output = subprocess.check_output(['systemctl', 'status', 'deluge-web'])
'--user', 'debian-deluged',
'--name', 'deluge-web',
'--pidfile', '/var/run/deluge-web.pid'])
print('yes')
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
print('no') 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')
def subcommand_start(_): def start():
"""Start deluge-web.""" """Start deluge-web."""
subprocess.check_call(['start-stop-daemon', '--start', '--oknodo', subprocess.check_call(['systemctl', 'enable', 'deluge-web'])
'--background', '--make-pidfile', subprocess.check_call(['systemctl', 'start', 'deluge-web'])
'--name', 'deluge-web',
'--user', 'debian-deluged',
'--startas', '/usr/bin/deluge-web',
'--pidfile', '/var/run/deluge-web.pid',
'--chuid', 'debian-deluged:debian-deluged',
'--', '--base=deluge'])
def subcommand_stop(_): def stop():
"""Stop deluge-web.""" """Stop deluge-web."""
subprocess.check_call(['start-stop-daemon', '--stop', '--retry', '5', try:
'--oknodo', '--name', 'deluge-web', subprocess.check_output(['systemctl', 'stop', 'deluge-web'])
'--user', 'debian-deluged', except subprocess.CalledProcessError:
'--pidfile', '/var/run/deluge-web.pid', pass
'--remove-pidfile'])
def setup(): def setup():
"""Perform initial setup for deluge-web site.""" """Perform initial setup for deluge-web site."""
with open(SITE_CONF, 'w') as conffile: if not os.path.isfile(APACHE_CONF_PATH):
conffile.write(APACHE_CONF) with open(APACHE_CONF_PATH, 'w') as conffile:
conffile.write(APACHE_CONF)
if not os.path.isfile(SYSTEMD_SERVICE_PATH):
with open(SYSTEMD_SERVICE_PATH, 'w') as file_handle:
file_handle.write(SYSTEMD_SERVICE)
subprocess.check_call(['systemctl', 'daemon-reload'])
def main(): def main():

View File

@ -44,26 +44,11 @@
{% if status.enabled %} {% if status.enabled %}
<h3>Status</h3> <h3>Status</h3>
<p>If your {{ cfg.box_name }} is restarted, you will need to start deluge-web
manually. In the future, this will be handled automatically.</p>
<p> <p>
{% if status.is_running %} {% if status.is_running %}
<span class="running-status active"></span> deluge-web is running <span class="running-status active"></span> deluge-web is running
<form class="form" method="post" action="{% url 'deluge:stop' %}">
{% csrf_token %}
<input type="submit" class="btn btn-primary" value="Stop deluge-web"/>
</form>
{% else %} {% else %}
<span class="running-status inactive"></span> deluge-web is not running <span class="running-status inactive"></span> deluge-web is not running
<form class="form" method="post" action="{% url 'deluge:start' %}">
{% csrf_token %}
<input type="submit" class="btn btn-primary" value="Start deluge-web"/>
</form>
{% endif %} {% endif %}
</p> </p>
{% endif %} {% endif %}

View File

@ -25,6 +25,4 @@ from django.conf.urls import patterns, url
urlpatterns = patterns( urlpatterns = patterns(
'plinth.modules.deluge.views', 'plinth.modules.deluge.views',
url(r'^apps/deluge/$', 'index', name='index'), url(r'^apps/deluge/$', 'index', name='index'),
url(r'^apps/deluge/start/$', 'start', name='start'),
url(r'^apps/deluge/stop/$', 'stop', name='stop'),
) )

View File

@ -21,10 +21,7 @@ Plinth module to configure a Deluge web client.
from django.contrib import messages from django.contrib import messages
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.core.urlresolvers import reverse_lazy
from django.shortcuts import redirect
from django.template.response import TemplateResponse from django.template.response import TemplateResponse
from django.views.decorators.http import require_POST
from gettext import gettext as _ from gettext import gettext as _
from .forms import DelugeForm from .forms import DelugeForm
@ -56,22 +53,6 @@ def index(request):
'form': form}) 'form': form})
@login_required
@require_POST
def start(request):
"""Start deluge-web."""
actions.run('deluge', ['start'])
return redirect(reverse_lazy('deluge:index'))
@login_required
@require_POST
def stop(request):
"""Stop deluge-web."""
actions.run('deluge', ['stop'])
return redirect(reverse_lazy('deluge:index'))
def get_status(): def get_status():
"""Get the current settings.""" """Get the current settings."""
output = actions.run('deluge', ['get-enabled']) output = actions.run('deluge', ['get-enabled'])