#!/usr/bin/env python

# Copyright (c) 2013 Mirantis Inc.
#
# 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 argparse

import eho.server.main as server
from eventlet import wsgi
import eventlet


def main():
    parser = argparse.ArgumentParser(description='EHO server')
    parser.add_argument(
        '--dev', action='store_true', dest='dev', help='enable dev mode'
    )
    parser.add_argument(
        '--host', default='', dest='host', help='set host (default: \'\' all)'
    )
    parser.add_argument(
        '-p', '--port', type=int, default=8080, dest='port',
        help='specify http port (default: 8080)'
    )
    parser.add_argument(
        '--log-level, --log_level', default='WARN', dest='log_level',
        help='set logging level (default: WARN)'
    )
    parser.add_argument(
        '--reset-db, --reset_db', action='store_true', dest='reset_db',
        help='reset db'
    )
    parser.add_argument(
        '--stub-data, --stub_data', action='store_true', dest='stub_data',
        help='put stub templates and clusters into db'
    )
    parser.add_argument(
        '--allow-cluster-ops, --allow_cluster_ops', action='store_true',
        dest='allow_cluster_ops', help='allow cluster ops (default: False)'
    )

    args = parser.parse_args()
    opts = dict()
    opts['DEBUG'] = args.dev
    opts['LOG_LEVEL'] = args.log_level
    opts['RESET_DB'] = args.reset_db
    opts['STUB_DATA'] = args.stub_data
    opts['ALLOW_CLUSTER_OPS'] = args.allow_cluster_ops

    app = server.make_app(**opts)

    wsgi.server(eventlet.listen((args.host, args.port), backlog=500), app,
                debug=args.dev)


if __name__ == '__main__':
    main()
