#!/usr/bin/python
# need to give it permissions in makefile
#chmod 0744
import sys, getopt
import subprocess

# ec2-54-167-4-20.compute-1.amazonaws.com	Origin server (running Web server on port 8080)

replica_servers = [
"ec2-54-210-1-206.compute-1.amazonaws.com",
"ec2-54-67-25-76.us-west-1.compute.amazonaws.com",
"ec2-35-161-203-105.us-west-2.compute.amazonaws.com",
"ec2-52-213-13-179.eu-west-1.compute.amazonaws.com",
"ec2-52-196-161-198.ap-northeast-1.compute.amazonaws.com",
"ec2-54-255-148-115.ap-southeast-1.compute.amazonaws.com",
"ec2-13-54-30-86.ap-southeast-2.compute.amazonaws.com",
"ec2-52-67-177-90.sa-east-1.compute.amazonaws.com",
"ec2-35-156-54-135.eu-central-1.compute.amazonaws.com"]

def validate_args():
    # we do not really need the port number, origin_server in this file
    PORT = ''
    ORIGIN_SERVER = ''
    CDN_SERVER = 'cs5700cdnproject.ccs.neu.edu'
    RSA_KEY = ''
    USER_NAME = ''

    try:
        opts, args = getopt.getopt(sys.argv[1:], "p:o:n:u:i:h" ,["help"])
    except getopt.GetoptError:
        print "./[deploy|run|stop]CDN -p <port> -o <origin> -n <name> -u <username> -i <keyfile>"
        sys.exit(2)

    for opt, arg in opts:
        if opt in ('-h', '--help'):
            print "./[deploy|run|stop]CDN -p <port> -o <origin> -n <name> -u <username> -i <keyfile>"
            sys.exit(1)
        elif opt in ('-p'):
            PORT = arg

        elif opt in ('-o'):
            ORIGIN_SERVER = arg

        elif opt in ('-n'):
            resolve_name = arg

        elif opt in ('-u'):
            USER_NAME = arg

        elif opt in ('-i'):
            RSA_KEY = arg


    # print "port : ", PORT
    # print "origin_server : ", ORIGIN_SERVER
    # print "cdn_server : ", CDN_SERVER
    # print "username : ", USER_NAME
    # print "rsa key file : ", RSA_KEY

    return PORT, ORIGIN_SERVER, CDN_SERVER, USER_NAME, RSA_KEY

def run_dns_server(PORT, CDN_SERVER, USER_NAME, RSA_KEY):
    # getting this right is tricky
    run_command = "./dnsserver -p " + PORT + " -n " + "cs5700cdn.example.com"
    # print run_command
    # I still do not understand what I am doing here : "< /dev/null > std.out 2> std.err"
    command = "ssh -i "+ RSA_KEY + " " + USER_NAME + "@" + CDN_SERVER + " '"+ run_command +"< /dev/null > std.out 2> std.err &" +"'"
            #   " '"+"./dnsserver -p "+str(PORT_NUMBER)+" -n "+"cs5700cdn.example.com"+" &"+"'"
    # print command
    # print "ssh -i "+ RSA_KEY + " " + USER_NAME + "@"+ CDN_SERVER + " '"+"cd scripts_project5; ./dnsserver -p " + PORT + " -n "+ "cs5700cdn.example.com"+"< /dev/null > std.out 2> std.err" + " &'"
    subprocess.check_output(command, shell=True)
    # print "deployed dns"



def run_http_servers(PORT, ORIGIN_SERVER, USER_NAME, RSA_KEY):
    # iterate through each replica server
    # "ec2-54-210-1-206.compute-1.amazonaws.com"
    for replica in replica_servers:
        run_command = "./httpserver -p "+ PORT + " -o "+ORIGIN_SERVER
        command = "ssh -i "+ RSA_KEY + " " + USER_NAME + "@" + replica + " '"+run_command+"< /dev/null > std.out 2> std.err &"+ "'"
        # print command
        # print "ssh -i "+RSA_KEY + " " + USER_NAME + "@" + replica + " '"+ "./httpserver -p "+PORT + " -o "+ ORIGIN_SERVER + "&'"
        subprocess.check_output(command ,shell=True)

    # print "deployed"


if __name__ == '__main__':

    PORT, ORIGIN_SERVER, CDN_SERVER, USER_NAME, RSA_KEY = validate_args()

    run_dns_server(PORT, CDN_SERVER, USER_NAME, RSA_KEY)
    run_http_servers(PORT, ORIGIN_SERVER, USER_NAME, RSA_KEY)
