import sys
import os
from optparse import OptionParser

from lib.cluster import Cluster

if __name__ == "__main__":
    usage = """usage: reset_cluster.py
    --cong=<name-of-conf>
    """

    parser = OptionParser(usage=usage)

    parser.add_option("", "--conf",
                      action="store", type="string", dest="conf", default=None,
                      help="name of configuration in conf/ to reset cluster with")

    arg_parameters = sys.argv[1:]

    (opts, args) = parser.parse_args(arg_parameters)

    cluster = Cluster()
    cluster.reset(opts.conf)


    (opts, args) = parser.parse_args(arg_parameters)
    
    if opts.test_id is None:
        print "You must provide a test identifier to run the test"
        sys.exit(1)

    test_run_id = "{}_{}".format(opts.test_id, time.strftime("%Y-%m-%d-%H-%M-%S"))

    # Create test results directory
    os.makedirs("performance_results/{}".format(test_run_id))

    if opts.reset_sync_gateway:
        print "Resetting Sync Gateway"
        cluster = Cluster()
        mode = cluster.reset("performance/sync_gateway_default_performance.json")
        print("Running in mode: {}".format(mode))

    # Copy provisioning_config to performance_results/ folder
    shutil.copy("provisioning_config", "performance_results/{}".format(test_run_id))

    # Start load generator
    run_tests(
        number_pullers=opts.number_pullers,
        number_pushers=opts.number_pushers,
        use_gateload=opts.use_gateload,
        gen_gateload_config=opts.gen_gateload_config,
        test_id=test_run_id
    )

    # write expvars to file, will exit when gateload scenario is done