#!/usr/bin/python

import argparse
import yaml

INSTALLER_SLS = "pillar/installer.sls"


def generate_installer(deployment_type,
                       deployment_mode,
                       admin_user,
                       admin_pass,
                       amqp_host,
                       graphite_host,
                       nfs_enabled,
                       nfs_server,
                       nfs_network,
                       storagedriver_queue_name,
                       fwdriver_queue_name,
                       fwdriver_gateway,
                       fwdriwer_external_net,
                       fwdriver_external_if
                       ):

    config = {

        "user": "cloud",

        "proxy_secret": "xooquageire7uX1",
        "secret_key": "Ga4aex3Eesohngo",
        "timezone": "Europe/Budapest",

        "deployment_type": deployment_type,
        "deployment_mode": deployment_mode,

        "admin_user": admin_user,
        "admin_pass": admin_pass,

        "database": {
            "name": "circle",
            "user": "circle",
            "password": "hoGei6paiN0ieda",
            },

        "amqp": {
            "user": "cloud",
            "password": "password",
            "host": amqp_host,
            "port": 5672,
            "vhost": "circle",
            },

        "graphite": {
            "user": "monitor",
            "password": "monitor",
            "host": graphite_host,
            "port": 5672,
            "vhost": "monitor",
            "queue": "monitor",
            "secret_key": "ahf2aim7ahLeo8n",
            },

        "cache": "pylibmc://127.0.0.1:11211/",

        "nfs": {
            "enabled": nfs_enabled,
            "server": nfs_server,
            "network": nfs_network,
            "directory": "/datastore"
            },

        "storagedriver": {
            "queue_name": storagedriver_queue_name
            },

        "fwdriver": {
            "queue_name": fwdriver_queue_name,
            "gateway": fwdriver_gateway,
            "external_net": fwdriwer_external_net,
            "external_if": fwdriver_external_if,
            "trunk_if": "linkb",
            "management_if": "ethy"
            }
    }

    return yaml.dump(config, default_flow_style=False)


def main():

    parser = argparse.ArgumentParser(
        description='Generate installer.sls')

    parser.add_argument('-m', '--mode', dest='mode', required=True,
                        help='standalone or multinode')
    parser.add_argument('-i', '--ip', dest='ip', required=True,
                        help='IP addres of head machine')
    parser.add_argument('-q', '--queue', dest='queue', required=True,
                        help='host name of head machine')

    args = parser.parse_args()

    config = generate_installer(deployment_type="local",
                                deployment_mode=args.mode,
                                admin_user="admin",
                                admin_pass="admin",
                                amqp_host=args.ip,
                                graphite_host=args.ip,
                                nfs_enabled=True,
                                nfs_server=args.ip,
                                nfs_network=args.ip+"/16",
                                storagedriver_queue_name=args.queue,
                                fwdriver_queue_name=args.queue,
                                fwdriver_gateway="10.9.255.254",
                                fwdriwer_external_net=args.ip+"/16",
                                fwdriver_external_if="eth0")

    with open(INSTALLER_SLS, "w") as f:
        f.write(config)


if __name__ == "__main__":
    main()
