#!/usr/bin/python3

# Copyright (c) 2016, Daniele Venzano
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import logging

from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop

from zoe_master.platform_manager import PlatformManager
from zoe_master.scheduler_policies import FIFOSchedulerPolicy
import zoe_master.config as config
from zoe_master.rest_api import init as api_init
from zoe_master.state.manager import StateManager
from zoe_master.state.blobs.fs import FSBlobs
from zoe_lib.metrics.influxdb import InfluxDBMetricSender
from zoe_lib.metrics.base import BaseMetricSender
from zoe_master.stats_manager import StatsManager
from zoe_master.workspace.filesystem import ZoeFSWorkspace

log = logging.getLogger("main")
LOG_FORMAT = '%(asctime)-15s %(levelname)s %(name)s (%(threadName)s): %(message)s'


def main():
    """
    The entrypoint for the zoe-master script.
    :return: int
    """
    config.load_configuration()
    args = config.get_conf()
    if args.debug:
        logging.basicConfig(level=logging.DEBUG, format=LOG_FORMAT)

    else:
        logging.basicConfig(level=logging.INFO, format=LOG_FORMAT)

    logging.getLogger('kazoo').setLevel(logging.WARNING)
    logging.getLogger('requests').setLevel(logging.WARNING)
    logging.getLogger('urllib3').setLevel(logging.WARNING)
    logging.getLogger('docker').setLevel(logging.INFO)
    logging.getLogger("tornado").setLevel(logging.DEBUG)
    logging.getLogger('passlib').setLevel(logging.INFO)

    log.info("Initializing state")
    state_manager = StateManager(FSBlobs)
    state_manager.init()
    config.singletons['state_manager'] = state_manager

    log.info("Initializing platform manager")
    pm = PlatformManager(FIFOSchedulerPolicy)
    config.singletons['platform_manager'] = pm

    log.info("Initializing workspace managers")
    fswk = ZoeFSWorkspace()
    config.singletons['workspace_managers'] = [fswk]

#    try:
    log.info("Checking state consistency")
    pm.check_workspaces()
    pm.check_state_swarm_consistency()
#    except:
#        log.error('State is seriously corrupted, delete and restart')

    log.info("Initializing API")
    app = api_init(state_manager, pm)

    if config.get_conf().influxdb_enable:
        metrics_th = InfluxDBMetricSender(config.get_conf())
        metrics_th.start()
        config.singletons['metric'] = metrics_th
    else:
        metrics_th = BaseMetricSender('metrics-logger', config.get_conf())
        config.singletons['metric'] = metrics_th

    stats_th = StatsManager()
#    stats_th.start()  # TODO Broken Docker API
    config.singletons['stats_manager'] = stats_th

    log.info("Starting HTTP REST server...")
    app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024

    http_server = HTTPServer(WSGIContainer(app))
    http_server.listen(args.listen_port, args.listen_address)  # Initialized like this it is single-threaded/single-process
    ioloop = IOLoop.instance()
    try:
        ioloop.start()
    except KeyboardInterrupt:
        if config.singletons['metric'] is not None:
            config.singletons['metric'].quit()
            config.singletons['metric'].join()
        print("CTRL-C detected, terminating")

@@ -619,124 +619,8 @@
 ng%0A%0A
-from tornado.wsgi import WSGIContainer%0Afrom tornado.httpserver import HTTPServer%0Afrom tornado.ioloop import IOLoop%0A%0A
 from
@@ -663,32 +663,32 @@
 PlatformManager%0A
+
 from zoe_master.
@@ -784,20 +784,22 @@
 _master.
-rest
+master
 _api imp
@@ -806,24 +806,18 @@
 ort 
-init as api_init
+APIManager
 %0Afro
@@ -1566,65 +1566,8 @@
 T)%0A%0A
-    logging.getLogger('kazoo').setLevel(logging.WARNING)%0A
     
@@ -2485,80 +2485,8 @@
 ')%0A%0A
-    log.info(%22Initializing API%22)%0A    app = api_init(state_manager, pm)%0A%0A
     
@@ -2951,17 +2951,15 @@
 ing 
-HTTP REST
+ZMQ API
  ser
@@ -2975,515 +2975,94 @@
     
-app.
 config
-%5B'MAX_CONTENT_LENGTH'%5D = 16 * 1024 * 1024%0A%0A    http_server = HTTPServer(WSGIContainer(app))%0A    http_server.listen(args.listen_port, args.listen_address)  # Initialized like this it is single-threaded/single-process%0A    ioloop = IOLoop.instance()%0A    try:%0A        ioloop.start()%0A    except KeyboardInterrupt:%0A        if config.singletons%5B'metric'%5D is not None:%0A            config.singletons%5B'metric'%5D.quit()%0A            config.singletons%5B'metric'%5D.join()%0A        print(%22CTRL-C detected, terminating%22
+.singletons%5B'api_server'%5D = APIManager()%0A    config.singletons%5B'api_server'%5D.loop(
 )%0A

Remove tornado and Flask from the master process