#!/usr/bin/env python
from scenariobuilder.build import make, cli_get, log, wait
from scenariobuilder.rebuild import rebuild
from scenariobuilder.clean import kill
from scenariobuilder.metadata import show as meta_show
from scenariobuilder.bootstrap import show as boot_show

from novaclient.v1_1 import client as nclient
from neutronclient.neutron import client as qclient

import argparse
import os

def main():
    user = os.environ.get('OS_USERNAME')
    tenant = os.environ.get('OS_TENANT_NAME')
    auth_url = os.environ.get('OS_AUTH_URL')
    password = os.environ.get('OS_PASSWORD')
    n = nclient.Client(user, password, tenant, auth_url, service_type="compute", insecure=True)
    q = qclient.Client('2.0', auth_url=auth_url, username=user, tenant_name=tenant, password=password)

    parser = argparse.ArgumentParser(description='Stack Builder: a tool for deploying Openstack on Openstack using puppet')
    subparsers = parser.add_subparsers()

    parser_make = subparsers.add_parser('make', help='Make a test run')
    parser_make.add_argument('-i', '--image', default='precise-x86_64', help='name of image to use')
    parser_make.add_argument('-s', '--scenario', default='2_role', help='Scenario to run')
    parser_make.add_argument('-p', '--data_path', default='./data', help='Path to the data directory containing yaml config')
    parser_make.add_argument('-d', '--debug', action='store_true', help='print debug output')
    parser_make.add_argument('-n', '--public_network', default='public', help='The name of the public network on the cluster')
    parser_make.add_argument('-a', '--nameserver', default='171.70.168.183', help='The DNS nameserver to use for all nodes being booted.')
    parser_make.set_defaults(func=make)

    parser_rebuild = subparsers.add_parser('rebuild', help='Rebuild a test using existing resources')
    parser_rebuild.add_argument('test_id', help='id of the test to get')
    parser_rebuild.add_argument('-i', '--image', default='precise-x86_64', help='name of image to use')
    parser_rebuild.add_argument('-s', '--scenario', default='2_role', help='Scenario to run')
    parser_rebuild.add_argument('-p', '--data_path', default='./data', help='Path to the data directory containing yaml config')
    parser_rebuild.add_argument('-d', '--debug', action='store_true', help='print debug output')
    parser_rebuild.add_argument('-n', '--public_network', default='public', help='The name of the public network on the cluster')
    parser_rebuild.set_defaults(func=rebuild)

    parser_get = subparsers.add_parser('get', help='Get info on current test runs')
    parser_get.add_argument('-t', '--test_id', default=None, help='id of the test to get')
    parser_get.set_defaults(func=cli_get)

    parser_kill = subparsers.add_parser('kill', help='Destroy resources associated with test runs')
    parser_kill.add_argument('-t', '--test_id', default=None, help='id of the test to kill')
    parser_kill.set_defaults(func=kill)

    parser_boot = subparsers.add_parser('boot', help='Print bootstrap script that will be generated for a given node')
    parser_boot.add_argument('node', help='node to build a bootstrap script for')
    parser_boot.add_argument('-y', '--yaml_dir', default='./data', help='name of image to use')
    parser_boot.add_argument('-s', '--scenario', default='2_role', help='Scenario to use')
    parser_boot.set_defaults(func=boot_show)

    parser_log = subparsers.add_parser('log', help='Get logs from test nodes')
    parser_log.add_argument('-t', '--test_id', default=None, help='id of the test to get logs from')
    parser_log.add_argument('-s', '--scenario', default='2_role', help='Scenario of test')
    parser_log.add_argument('-p', '--data_path', default='./data', help='Path to the data directory containing yaml config')
    parser_log.set_defaults(func=log)

    parser_wait = subparsers.add_parser('wait', help='Wait for all test nodes to complete building')
    parser_wait.add_argument('-t', '--test_id', default=None, help='id of the build to wait for')
    parser_wait.set_defaults(func=wait)

    parser_meta = subparsers.add_parser('meta', help='Print the metadata dictionary for a test run')
    parser_meta.add_argument('node', help='node to build metadata for')
    parser_meta.add_argument('-y', '--yaml_dir', default='./data', help='name of image to use')
    parser_meta.add_argument('-s', '--scenario', default='2_role', help='Scenario to use')
    parser_meta.add_argument('-c', '--config', default='config', help='Type of config to build - user or config')
    parser_meta.set_defaults(func=meta_show)

    args = parser.parse_args()

    k = ""

    args.func(n, q, k, args)

if __name__ == '__main__':
    main()  
