<commit_before>import boto.ec2
import boto.manage.cmdshell
import time
import random
import pymysql
import os.path
import configparser

parser = configparser.ConfigParser()
parser.read("../../../halite.ini")
AWS_CONFIG = parser["aws"]
DB_CONFIG = parser["database"]

# Create instance
conn = boto.ec2.connect_to_region("us-east-1", aws_access_key_id=AWS_CONFIG["accessKey"], aws_secret_access_key=AWS_CONFIG["secretAccessKey"])
securityGroup = conn.get_all_security_groups(filters={'group-name': AWS_CONFIG["securityGroupName"]})[0]
reservation = conn.run_instances(AWS_CONFIG["amiID"], key_name=AWS_CONFIG["keyName"], instance_type=AWS_CONFIG["instanceType"], security_groups=[securityGroup])
instance = reservation.instances[0]

# Wait to start
status = instance.update()
while status == "pending":
    time.sleep(1)
    status = instance.update()
    print("Waiting for instance to start...")

# Add worker to db
db = pymysql.connect(host=DB_CONFIG["hostname"], user=DB_CONFIG['username'], passwd=DB_CONFIG['password'], db=DB_CONFIG['name'])
cursor = db.cursor()

ipAddress = instance.ip_address
print(ipAddress)

numRows = 9999
while numRows != 0:
    apiKey = random.randrange(0, 10000000)
    cursor.execute("SELECT * FROM Worker WHERE apiKey="+str(apiKey))
    numRows = int(cursor.rowcount)
cursor.execute("INSERT INTO Worker (apiKey, ipAddress) VALUES ("+str(apiKey)+", '"+str(ipAddress)+"')")

def runCommandOnInstance(instance, command):
    # Connect to ssh
    for a in range(100):
        try:
            ssh_client = boto.manage.cmdshell.sshclient_from_instance(instance, os.path.join("../../../", AWS_CONFIG["keyFilePath"]), user_name="ubuntu")
            print("Connected to server")
            break
        except Exception as e:
            print("except")
            print(str(e))

    # Run install script
    status, stdout, stderr = ssh_client.run(command)
    print(stdout)
    print(stderr)

configFileContents = open("../../../halite.ini").read()
runCommandOnInstance(instance, "sudo apt-get install -y git; git clone https://github.com/HaliteChallenge/Halite.git; cd Halite; echo '"+configFileContents+"' > halite.ini; cd worker; sudo ./install.sh "+str(apiKey)+"; sudo reboot")

time.sleep(10)
runCommandOnInstance(instance, 'screen -dmS test bash -c "cd ~/Halite/worker; sudo python3 worker.py"')
<commit_msg>Use startWorkerScreen in AWS autoscale<commit_after>import boto.ec2
import boto.manage.cmdshell
import time
import random
import pymysql
import os.path
import configparser

parser = configparser.ConfigParser()
parser.read("../../../halite.ini")
AWS_CONFIG = parser["aws"]
DB_CONFIG = parser["database"]

# Create instance
conn = boto.ec2.connect_to_region("us-east-1", aws_access_key_id=AWS_CONFIG["accessKey"], aws_secret_access_key=AWS_CONFIG["secretAccessKey"])
securityGroup = conn.get_all_security_groups(filters={'group-name': AWS_CONFIG["securityGroupName"]})[0]
reservation = conn.run_instances(AWS_CONFIG["amiID"], key_name=AWS_CONFIG["keyName"], instance_type=AWS_CONFIG["instanceType"], security_groups=[securityGroup])
instance = reservation.instances[0]

# Wait to start
status = instance.update()
while status == "pending":
    time.sleep(1)
    status = instance.update()
    print("Waiting for instance to start...")

# Add worker to db
db = pymysql.connect(host=DB_CONFIG["hostname"], user=DB_CONFIG['username'], passwd=DB_CONFIG['password'], db=DB_CONFIG['name'])
cursor = db.cursor()

ipAddress = instance.ip_address
print(ipAddress)

numRows = 9999
while numRows != 0:
    apiKey = random.randrange(0, 10000000)
    cursor.execute("SELECT * FROM Worker WHERE apiKey="+str(apiKey))
    numRows = int(cursor.rowcount)
cursor.execute("INSERT INTO Worker (apiKey, ipAddress) VALUES ("+str(apiKey)+", '"+str(ipAddress)+"')")

def runCommandOnInstance(instance, command):
    # Connect to ssh
    for a in range(100):
        try:
            ssh_client = boto.manage.cmdshell.sshclient_from_instance(instance, os.path.join("../../../", AWS_CONFIG["keyFilePath"]), user_name="ubuntu")
            print("Connected to server")
            break
        except Exception as e:
            print("except")
            print(str(e))

    # Run install script
    status, stdout, stderr = ssh_client.run(command)
    print(stdout)
    print(stderr)

configFileContents = open("../../../halite.ini").read()
runCommandOnInstance(instance, "sudo apt-get install -y git; git clone https://github.com/HaliteChallenge/Halite.git; cd Halite; echo '"+configFileContents+"' > halite.ini; cd worker; sudo ./install.sh "+str(apiKey)+"; sudo reboot")

time.sleep(10)
runCommandOnInstance(instance, 'cd ~/Halite/worker; ./startWorkerScreen.sh')

{'fraction_non_alphanumeric': 0.14773697694278395, 'fraction_numerical': 0.010888129803586678, 'mean_word_length': 4.841645885286783, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6624754', 'n_tokens_mistral': 1523, 'n_tokens_neox': 1472, 'n_words': 317}