import subprocess
from utils import CommandRunner

cr = CommandRunner() 
   
print """#####################install ceilometer##################"""
if cr.ask_run_section("run install ceilometer? (delete existing, reclone, switch, branch, and python setup): "):

    #delete ceilometer 
    cr.run_command("Delete the /opt/stack/ceilometer folder? ([y]/n): ",  "Deleting ceilometer folder...", "sudo rm -rf ceilometer", "/opt/stack/", "Done removing ceilometer folder")
    
    #clone ceilometer
    cr.run_command("clone ceilometer for github eric's repo? ([y]/n): ", "Start clone:", "git clone https://github.com/jyericlin/ceilometer.git", "/opt/stack", "Done clone" )
        
    cr.run_command("Change ceilo branch: ", "Changing", "git checkout devel", "/opt/stack/ceilometer", "Done changing branch")
    
    cr.run_command("Install ceilometer: ", "Start install", "sudo python setup.py install", "/opt/stack/ceilometer", "Done install")
    
    cr.run_command("Install ceilometer devel: ", "Start install devel", "sudo python setup.py develop", "/opt/stack/ceilometer", "Done install")



print """##################install ceilometer client#######################"""
if cr.ask_run_section('run install ceilometer client? (delete existing, reclone, switch branch, and python setup)'):
    cr.run_command("Delete the /opt/stack/python-ceilometerclient folder? ([y]/n): ",  "Deleting ceilometer client folder...", "sudo rm -rf python-ceilometerclient", "/opt/stack/", "Done removing ceilometer client folder")
    
    cr.run_command("clone ceilometer client for github eric's repo? ([y]/n): ", "Start clone:", "git clone https://github.com/jyericlin/python-ceilometerclient.git", "/opt/stack", "Done clone" )
        
    cr.run_command("Change ceilo client branch: ", "Changing", "git checkout s-havana", "/opt/stack/python-ceilometerclient", "Done changing branch")
    
    cr.run_command("Install ceilometer client: ", "Start install", "sudo python setup.py install", "/opt/stack/python-ceilometerclient", "Done install")
#print "check['output'] is: %s"% check['output']
#subprocess.Popen(str(check['output'])+" > /tmp/env.output"+str(randrange(500)), shell=True)


region_name = os.environ['OS_REGION_NAME'].rstrip()
print "Performing Ceilometed Tests for Region: "+str(region_name)

#subprocess.Popen("echo "+region_name+" > /tmp/env.output"+str(randrange(500)), shell=True)


start_time = time.time()

""" ************************* Ceilometer Metering API commands ************************************ """

resource_output = cr.run_command("ceilometer resource-list","done resource-list")

meter_name_output = cr.run_command("ceilometer meter-name-list","done meter-name-list")

meter_output = cr.run_command("ceilometer meter-list | awk '{print $2, $8}'","done meter-list")


number_meters_output = cr.run_command("ceilometer meter-list | wc -l","fetched the number of meters")

meter_output_list = re.split('\n', meter_output['output'])

full_meter_list = filter(lambda x:x != " ", meter_output_list)
full_meter_list = filter(lambda x:x != "", full_meter_list)
full_meter_list = filter(lambda x:x != "Name Resource", full_meter_list)