import subprocess
import argparse
import re
import paramiko
import time

parser = argparse.ArgumentParser(description='move pool to online')
parser.add_argument('--pool', help='pool to move')
args = parser.parse_args()
# print(args.accumulate(args.integers))

def move_pool_to_online(pool):
	
	if pool == 'default':
		print 'I am not moving servers in default to online !!!!'
	
	pattern = re.compile('rack\d\d-server\d\d')
	servers_in_pool_command ='/root/racktools/printStatusPerServer.sh  | grep :{pool}:'.format(pool=pool)
	print servers_in_pool_command
	servers = subprocess.check_output(servers_in_pool_command, shell=True, cwd='/root/racktools')
	servers_to_move_online = []
	for line in servers.splitlines():	
		if pattern.match(line.split(':')[0]): # and line.split(':')[-1] != 'CHECKED_IN':
			servers_to_move_online.append(line.split(':')[0])		
 	print servers_to_move_online
	if servers_to_move_online:	
		move_to_online_command = "/root/racktools/rackattack-tools-cli --servers {} --pool default".format(' '.join(servers_to_move_online))
		print move_to_online_command
		print subprocess.check_output(move_to_online_command, shell=True)
		
move_pool_to_online(args.pool)

