cleaned up actions and configuration form

This commit is contained in:
fonfon 2015-01-22 15:31:44 +00:00
parent 99bfda3b69
commit 3f2c9ff2c8
4 changed files with 76 additions and 62 deletions

View File

@ -28,6 +28,7 @@ import augeas
import os
import subprocess
from util import is_running
from pagekite_util import SERVICE_PARAMS, convert_to_service, \
convert_service_to_string, get_augeas_servicefile_path, CONF_PATH
@ -49,16 +50,10 @@ def parse_arguments():
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest='subcommand', help='Sub command')
# Start/Stop/Restart PageKite service daemon
# it's called daemon to avoid confusing it with pagekite services
daemon = subparsers.add_parser('daemon',
help='start/stop/restart PageKite')
daemon.add_argument('action', choices=['start', 'stop', 'restart'])
# Enable/disable the pagekite service
subparsers.add_parser('is-enabled', help='Get whether PakeKite is enabled')
subparsers.add_parser('enable', help='Enable PageKite service')
subparsers.add_parser('disable', help='Disable PageKite service')
subparsers.add_parser('is-running', help='Get whether PakeKite is running')
subparsers.add_parser('start-and-enable', help='Enable PageKite service')
subparsers.add_parser('stop-and-disable', help='Disable PageKite service')
# Frontend
subparsers.add_parser('get-frontend', help='Get pagekite frontend')
@ -88,34 +83,29 @@ def parse_arguments():
return parser.parse_args()
def subcommand_daemon(arguments):
"""Start/stop/restart the pagekite daemon"""
_daemon(arguments.action)
def _daemon(action):
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_enabled(_):
def subcommand_is_running(_):
"""Print whether pagekite is enabled (yes or no)"""
is_enabled = is_pagekite_enabled()
print 'yes' if is_enabled else 'no'
print 'yes' if is_running('pagekite') else 'no'
def is_pagekite_enabled():
return not bool(aug.match(PATHS['abort_not_configured']))
def subcommand_enable(_):
pagekite_enable()
def subcommand_start_and_enable(_):
aug.remove(PATHS['abort_not_configured'])
aug.save()
_service('start')
print 'enabled'
def subcommand_disable(_):
pagekite_disable()
def subcommand_stop_and_disable(_):
_service('stop')
aug.set(PATHS['abort_not_configured'], '')
aug.save()
print 'disabled'
@ -149,18 +139,6 @@ def enable_pagekitenet_frontend():
print "enabled"
def pagekite_enable():
"""Enable the pagekite daemon"""
aug.remove(PATHS['abort_not_configured'])
aug.save()
def pagekite_disable():
"""Disable the pagekite daemon"""
aug.set(PATHS['abort_not_configured'], '')
aug.save()
def subcommand_get_services(arguments):
""" lists all available (enabled) services """
for match in aug.match(PATHS['service_on']):
@ -191,7 +169,7 @@ def subcommand_remove_service(arguments):
file.writelines(lines)
# abort to only allow deleting one service
break
_daemon('restart')
_service('restart')
def get_existing_service_paths(service):
@ -218,7 +196,7 @@ def subcommand_add_service(arguments):
with open(path, 'a') as servicefile:
line = "\nservice_on = %s\n" % convert_service_to_string(service)
servicefile.write(line)
_daemon('restart')
_service('restart')
def convert_augeas_path_to_filepath(augpath, prefix='/files',

40
actions/util.py Normal file
View File

@ -0,0 +1,40 @@
#!/usr/bin/python3
# -*- mode: python -*-
#
# This file is part of Plinth.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"""
Python action utility functions
"""
import subprocess
def is_running(servicename):
"""Evaluates whether a service is currently running. Returns boolean"""
try:
output = subprocess.check_output(['service', servicename, 'status'])
except subprocess.CalledProcessError:
# Usually 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

View File

@ -42,8 +42,7 @@ class TrimmedCharField(forms.CharField):
class ConfigurationForm(forms.Form):
"""Configure PageKite credentials and frontend"""
enabled = forms.BooleanField(label=_('Enable PageKite'),
required=False)
enabled = forms.BooleanField(label=_('Enable PageKite'), required=False)
server = forms.CharField(
label=_('Server'), required=False,
@ -69,28 +68,24 @@ for your account if no secret is set on the kite'))
LOGGER.info('New status is - %s', new)
if old != new:
_run(['daemon', 'stop'])
if old['enabled'] != new['enabled']:
if new['enabled']:
_run(['enable'])
messages.success(request, _('PageKite enabled'))
else:
_run(['disable'])
messages.success(request, _('PageKite disabled'))
if old['kite_name'] != new['kite_name'] or \
old['kite_secret'] != new['kite_secret']:
_run(['set-kite', '--kite-name', new['kite_name'],
'--kite-secret', new['kite_secret']])
messages.success(request, _('Kite details set'))
if old['kite_name'] != new['kite_name'] or \
old['kite_secret'] != new['kite_secret']:
_run(['set-kite', '--kite-name', new['kite_name'],
'--kite-secret', new['kite_secret']])
messages.success(request, _('Kite details set'))
if old['server'] != new['server']:
_run(['set-frontend', new['server']])
messages.success(request, _('Pagekite server set'))
if old['server'] != new['server']:
_run(['set-frontend', new['server']])
messages.success(request, _('Pagekite server set'))
if old != new:
_run(['daemon', 'start'])
if old['enabled'] != new['enabled']:
if new['enabled']:
_run(['start-and-enable'])
messages.success(request, _('PageKite enabled'))
else:
_run(['stop-and-disable'])
messages.success(request, _('PageKite disabled'))
class DefaultServiceForm(forms.Form):

View File

@ -78,7 +78,8 @@ def get_pagekite_config():
status = {}
# PageKite service enabled/disabled
output = _run(['is-enabled'])
# This assumes that if pagekite is running it's also enabled as a service
output = _run(['is-running'])
status['enabled'] = (output.split()[0] == 'yes')
# PageKite kite details