From 11a38641e84d319085d3718038afdb6498b3396a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Lebleu?= Date: Sun, 18 May 2014 16:01:11 +0200 Subject: [PATCH] [enh] Split app_remove action into service_add/remove --- actionsmap/yunohost.yml | 47 +++++++++++++------------ app.py | 39 --------------------- locales/en.json | 4 +++ service.py | 76 +++++++++++++++++++++++++++++++++++++++-- 4 files changed, 103 insertions(+), 63 deletions(-) diff --git a/actionsmap/yunohost.yml b/actionsmap/yunohost.yml index 4752801f3..43a9d5b5a 100644 --- a/actionsmap/yunohost.yml +++ b/actionsmap/yunohost.yml @@ -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 diff --git a/app.py b/app.py index 5c15c55a3..bfd8435ee 100644 --- a/app.py +++ b/app.py @@ -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 diff --git a/locales/en.json b/locales/en.json index cfd2d36d8..96796b616 100644 --- a/locales/en.json +++ b/locales/en.json @@ -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", diff --git a/service.py b/service.py index c2582c4d2..5e1920c25 100644 --- a/service.py +++ b/service.py @@ -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): """