"""hydra cli.

Usage:
   hydra cli ls slaves
   hydra cli ls apps
   hydra cli ls task <app>
   hydra cli [force] stop <app>
   hydra cli scale <app> <scale>
   hydra cli (-h | --help)
   hydra cli --version

Options:
   -h --help  Show this screen.
   --version  Show version.
"""
__author__ = 'sushil'

from docopt import docopt
from pprint import pprint, pformat  # NOQA
from hydra.lib import util, mmapi
import os
import sys
import logging

try:
    # Python 2.x
    from ConfigParser import ConfigParser
except ImportError:
    # Python 3.x
    from configparser import ConfigParser

l = util.createlogger('cli', logging.INFO)
# l.setLevel(logging.DEBUG)


def cli(argv):
    config = ConfigParser()
    config_file_name = 'hydra.ini'
    if len(argv) >= 2 and argv[1].find('.ini') != -1:
        config_file_name = argv[1]
        del argv[1]
    if not os.path.isfile(config_file_name):
        l.error("Unable to open config file %s" % config_file_name)
        sys.exit(1)
    config.read(config_file_name)

    mesos_addr = 'http://' + config.get('mesos', 'ip') + ':' + \
                 config.get('mesos', 'port')
    marathon_addr = 'http://' + config.get('marathon', 'ip') + ':' + \
                    config.get('marathon', 'port')

    argv[0] = 'cli'
    args = docopt(__doc__, argv=argv, version='hydra 0.1.0', )
    # pprint (args)
    if args['ls']:
        if args['slaves']:
            mesos = mmapi.MesosIF(mesos_addr)
            mesos.print_slaves()
        elif args['apps']:
            mt = mmapi.MarathonIF(marathon_addr, '127.0.0.1', None)
            apps = mt.get_apps()
            for app in apps:
                st = "App:" + app.id
                st += " CPU:" + str(app.cpus)
                st += " MEM:" + str(app.mem)
                st += " Instances:" + str(app.instances)
                if len(app.constraints):
                    st += " Constraints:" + pformat(app.constraints)
                l.info(st)
        elif args['task']:
            mt = mmapi.MarathonIF(marathon_addr, '127.0.0.1', None)
            app = mt.get_app(args['<app>'])
            st = "App:" + args['<app>']
            st += " CPU:" + str(app.cpus)
            st += " MEM:" + str(app.mem)
            st += " Instances:" + str(app.instances)
            if len(app.constraints):
                st += " Constraints:" + pformat(app.constraints)
            l.info(st)
            st = "CMD:" + app.cmd
            l.info(st)
            st = "ID:" + app.id
            st += " task_running:" + str(app.tasks_running)
            st += " task_staged:" + str(app.tasks_staged)
            l.info(st)
            tasks = app.tasks
            for task in tasks:
                st = "\tTASK ID:" + task.id + " host:" + task.host
                if len(task.ports):
                    st += " ports:" + pformat(task.ports)
                if len(task.service_ports):
                    st += " service_ports:" + pformat(task.service_ports)
                l.info(st)
    elif args['stop']:
        mt = mmapi.MarathonIF(marathon_addr, '127.0.0.1', None)
        l.info("Deleting app:" + args['<app>'])
        mt.delete_app(args['<app>'], args['force'])
        l.info("Waiting for app removal to complete")
        mt.wait_app_removal(args['<app>'])
    elif args['scale']:
        mt = mmapi.MarathonIF(marathon_addr, '127.0.0.1', None)
        app = args['<app>']
        scale = int(args['<scale>'])
        l.info("Scaling app:" + app + " to scale:" + str(scale))
        mt.scale_app(app, scale)
        l.info("Waiting for app scale to complete")
        mt.wait_app_ready(app, scale)
# SK:Tried to add log collection but no luck so far.
#    elif args['logs']:
#        path = "/tmp/mesos/slaves/"
#        #11323ada-daab-4d76-8749-3113b5448bed-S0/
#        path += "/frameworks/
#        # #11323ada-daab-4d76-8749-3113b5448bed-0007
#        path += "/executors/"
#        #zst-pub.4bdec0e2-e7e3-11e5-a874-fe2077b92eeb
#        path += "/runs/"
#        # d00620ea-8f3e-427d-9404-6f6b9701f64f/
#        app = args['<app>']

[((32, 4, 32, 42), 'hydra.lib.util.createlogger', 'util.createlogger', ({(32, 22, 32, 27): '"""cli"""', (32, 29, 32, 41): 'logging.INFO'}, {}), "('cli', logging.INFO)", False, 'from hydra.lib import util, mmapi\n'), ((37, 13, 37, 27), 'configparser.ConfigParser', 'ConfigParser', ({}, {}), '()', False, 'from configparser import ConfigParser\n'), ((53, 11, 53, 62), 'docopt.docopt', 'docopt', (), '', False, 'from docopt import docopt\n'), ((42, 11, 42, 43), 'os.path.isfile', 'os.path.isfile', ({(42, 26, 42, 42): 'config_file_name'}, {}), '(config_file_name)', False, 'import os\n'), ((44, 8, 44, 19), 'sys.exit', 'sys.exit', ({(44, 17, 44, 18): '(1)'}, {}), '(1)', False, 'import sys\n'), ((57, 20, 57, 45), 'hydra.lib.mmapi.MesosIF', 'mmapi.MesosIF', ({(57, 34, 57, 44): 'mesos_addr'}, {}), '(mesos_addr)', False, 'from hydra.lib import util, mmapi\n'), ((95, 13, 95, 63), 'hydra.lib.mmapi.MarathonIF', 'mmapi.MarathonIF', ({(95, 30, 95, 43): 'marathon_addr', (95, 45, 95, 56): '"""127.0.0.1"""', (95, 58, 95, 62): 'None'}, {}), "(marathon_addr, '127.0.0.1', None)", False, 'from hydra.lib import util, mmapi\n'), ((60, 17, 60, 67), 'hydra.lib.mmapi.MarathonIF', 'mmapi.MarathonIF', ({(60, 34, 60, 47): 'marathon_addr', (60, 49, 60, 60): '"""127.0.0.1"""', (60, 62, 60, 66): 'None'}, {}), "(marathon_addr, '127.0.0.1', None)", False, 'from hydra.lib import util, mmapi\n'), ((101, 13, 101, 63), 'hydra.lib.mmapi.MarathonIF', 'mmapi.MarathonIF', ({(101, 30, 101, 43): 'marathon_addr', (101, 45, 101, 56): '"""127.0.0.1"""', (101, 58, 101, 62): 'None'}, {}), "(marathon_addr, '127.0.0.1', None)", False, 'from hydra.lib import util, mmapi\n'), ((71, 17, 71, 67), 'hydra.lib.mmapi.MarathonIF', 'mmapi.MarathonIF', ({(71, 34, 71, 47): 'marathon_addr', (71, 49, 71, 60): '"""127.0.0.1"""', (71, 62, 71, 66): 'None'}, {}), "(marathon_addr, '127.0.0.1', None)", False, 'from hydra.lib import util, mmapi\n'), ((68, 44, 68, 68), 'pprint.pformat', 'pformat', ({(68, 52, 68, 67): 'app.constraints'}, {}), '(app.constraints)', False, 'from pprint import pprint, pformat\n'), ((78, 40, 78, 64), 'pprint.pformat', 'pformat', ({(78, 48, 78, 63): 'app.constraints'}, {}), '(app.constraints)', False, 'from pprint import pprint, pformat\n'), ((90, 38, 90, 57), 'pprint.pformat', 'pformat', ({(90, 46, 90, 56): 'task.ports'}, {}), '(task.ports)', False, 'from pprint import pprint, pformat\n'), ((92, 46, 92, 73), 'pprint.pformat', 'pformat', ({(92, 54, 92, 72): 'task.service_ports'}, {}), '(task.service_ports)', False, 'from pprint import pprint, pformat\n')]