[enh] Split app_remove action into service_add/remove

This commit is contained in:
Jérôme Lebleu 2014-05-18 16:01:11 +02:00
parent 2f6af85b46
commit 11a38641e8
4 changed files with 103 additions and 63 deletions

View file

@ -423,27 +423,6 @@ app:
help: Delete the key
action: store_true
### app_service()
service:
action_help: Add or remove a YunoHost monitored service
api: POST /services
arguments:
service:
help: Service to add/remove
-s:
full: --status
help: Custom status command
-l:
full: --log
help: Absolute path to log file to display
-r:
full: --runlevel
help: Runlevel priority of the service
-R:
full: --remove
help: Remove service
action: store_true
### app_checkport()
checkport:
action_help: Check availability of a local port
@ -707,6 +686,32 @@ service:
category_help: Manage services
actions:
### service_add()
add:
action_help: Add a service
# api: POST /services
arguments:
name:
help: Service name to add
-s:
full: --status
help: Custom status command
-l:
full: --log
help: Absolute path to log file to display
-r:
full: --runlevel
help: Runlevel priority of the service
type: int
### service_remove()
remove:
action_help: Remove a service
# api: DELETE /services
arguments:
name:
help: Service name to remove
### service_start()
start:
action_help: Start one or more services

39
app.py
View file

@ -740,45 +740,6 @@ def app_setting(app, key, value=None, delete=False):
yaml.safe_dump(app_settings, f, default_flow_style=False)
def app_service(service, status=None, log=None, runlevel=None, remove=False):
"""
Add or remove a YunoHost monitored service
Keyword argument:
service -- Service to add/remove
status -- Custom status command
log -- Absolute path to log file to display
runlevel -- Runlevel priority of the service
remove -- Remove service
"""
service_file = '/etc/yunohost/services.yml'
try:
with open(service_file) as f:
services = yaml.load(f)
except IOError:
# Do not fail if service file is not there
services = {}
if remove and service in services:
del services[service]
else:
if status is None:
services[service] = { 'status': 'service' }
else:
services[service] = { 'status': status }
if log is not None:
services[service]['log'] = log
if runlevel is not None:
services[service]['runlevel'] = runlevel
with open(service_file, 'w') as f:
yaml.safe_dump(services, f, default_flow_style=False)
def app_checkport(port):
"""
Check availability of a local port

View file

@ -85,6 +85,10 @@
"monitor_glances_con_failed" : "Unable to connect to Glances server",
"service_unknown" : "Unknown service '%s'",
"service_add_failed" : "Unable to add service '%s'",
"service_added" : "Service successfully added",
"service_remove_failed" : "Unable to remove service '%s'",
"service_removed" : "Service successfully removed",
"service_start_failed" : "Unable to start service '%s'",
"service_already_started" : "Service '%s' is already started",
"service_started" : "Service '%s' successfully started",

View file

@ -32,6 +32,61 @@ import os.path
from moulinette.core import MoulinetteError
def service_add(name, status=None, log=None, runlevel=None):
"""
Add a custom service
Keyword argument:
name -- Service name to add
status -- Custom status command
log -- Absolute path to log file to display
runlevel -- Runlevel priority of the service
"""
services = _get_services()
if not status:
services[name] = { 'status': 'service' }
else:
services[name] = { 'status': status }
if log is not None:
services[name]['log'] = log
if runlevel is not None:
services[name]['runlevel'] = runlevel
try:
_save_services(services)
except:
raise MoulinetteError(errno.EIO, m18n.n('service_add_failed') % name)
msignals.display(m18n.n('service_added'), 'success')
def service_remove(name):
"""
Remove a custom service
Keyword argument:
name -- Service name to remove
"""
services = _get_services()
try:
del services[name]
except KeyError:
raise MoulinetteError(errno.EINVAL, m18n.n('service_unknown') % name)
try:
_save_services(services)
except:
raise MoulinetteError(errno.EIO, m18n.n('service_remove_failed') % name)
msignals.display(m18n.n('service_removed'), 'success')
def service_start(names):
"""
Start one or more services
@ -238,10 +293,25 @@ def _get_services():
Get a dict of managed services with their parameters
"""
with open('/etc/yunohost/services.yml', 'r') as f:
services = yaml.load(f)
return services
try:
with open('/etc/yunohost/services.yml', 'r') as f:
services = yaml.load(f)
except:
return {}
else:
return services
def _save_services(services):
"""
Save managed services to files
Keyword argument:
services -- A dict of managed services with their parameters
"""
# TODO: Save to custom services.yml
with open('/etc/yunohost/services.yml', 'w') as f:
yaml.safe_dump(services, f, default_flow_style=False)
def _tail(file, n, offset=None):
"""