From d15a23774c9b4c050f4ac6618f77022e31003910 Mon Sep 17 00:00:00 2001 From: Difrex Date: Fri, 14 Oct 2016 11:25:53 +0300 Subject: [PATCH] Marathon restart implementation --- conf/surok.json | 3 ++- surok/discovery.py | 1 + surok/system.py | 25 +++++++++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/conf/surok.json b/conf/surok.json index 6fc5196..427a982 100644 --- a/conf/surok.json +++ b/conf/surok.json @@ -1,7 +1,8 @@ { "marathon": { "force": true, - "host": "marathon.mesos:8080" + "host": "http://marathon.mesos:8080", + "enabled": true }, "confd": "/etc/surok/conf.d", "domain": "marathon.mesos", diff --git a/surok/discovery.py b/surok/discovery.py index 604c441..11298de 100644 --- a/surok/discovery.py +++ b/surok/discovery.py @@ -92,6 +92,7 @@ def get_group(service, app): # Parse MARATHON_APP_ID +# Return marathon.group def parse_marathon_app_id(marathon_app_id): marathon_app_id = marathon_app_id.split('/') group = '' diff --git a/surok/system.py b/surok/system.py index 13b4bd6..8a1914b 100644 --- a/surok/system.py +++ b/surok/system.py @@ -1,5 +1,6 @@ import os import logging +import requests # Get old configuration @@ -50,6 +51,10 @@ def reload_conf(service_conf, app_conf, first, conf): logging.info(stdout) return first + # Check marathon enabled in configuration + if conf['marathon']['enabled'] is True: + restart_self_in_marathon(conf['marathon']) + if get_old(app_conf['conf_name'], service_conf) != 1: stdout = do_reload(service_conf, app_conf) logging.info(stdout) @@ -60,3 +65,23 @@ def reload_conf(service_conf, app_conf, first, conf): app_conf['conf_name'] + ' Skip reload') return first + + +# Do POST request to marathon API +# /v2/apps//app/name/restart +def restart_self_in_marathon(marathon): + host = marathon['host'] + + # Check MARATHON_APP_ID environment varible + if os.environ.get('MARATHON_APP_ID') is not True: + logging.error('Cannot find MARATHON_APP_ID. Not in Mesos?') + sys.exit(2) + app_id = os.environ['MARATHON_APP_ID'] + uri = 'http://' + host + '/v2/apps/' + app_id + '/restart' + + # Ok. In this step we made restart request to Marathon + if marathon['force'] is True: + r = requests.post(uri, data = {'force': 'true'}) + else: + r = requests.post(uri, data = {}) +