diff --git a/action_map.yml b/action_map.yml index 6e7c1284..b3189d61 100644 --- a/action_map.yml +++ b/action_map.yml @@ -339,6 +339,21 @@ app: help: Delete the key action: store_true + ### app_service() + service: + action_help: Add or remove a YunoHost monitored service + api: POST /app/service/{service} + arguments: + service: + help: Service to add/remove + -s: + full: --status + help: Custom status command + -r: + full: --remove + help: Remove service + action: store_true + ### app_checkport() checkport: action_help: Check availability of a local port diff --git a/yunohost_app.py b/yunohost_app.py index a1ab185b..58d4ab43 100644 --- a/yunohost_app.py +++ b/yunohost_app.py @@ -581,6 +581,37 @@ 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, remove=False): + """ + Add or remove a YunoHost monitored service + + Keyword argument: + service -- Service to add/remove + status -- Custom status command + 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 } + + 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