<commit_before>from fabric.api import env, prompt, execute, sudo, run, put
from fabric.contrib import files
import boto.ec2
import time
import pprint


env.aws_region = 'us-west-2'
env.key_filename = '~/.ssh/KeepHair.pem'
git_user = 'tlake'
projname = 'learning-journal'
git_path = git_user + '/' + projname
appname = 'journal.py'


def get_ec2_connection():
    if 'ec2' not in env:
            conn = boto.ec2.connect_to_region(env.aws_region)
            if conn is not None:
                env.ec2 = conn
                print "Connected to EC2 region %s" % env.aws_region
            else:
                msg = "Unable to connect to EC2 region %s"
                raise IOError(msg % env.aws_region)
    return env.ec2


def provision_instance(
    wait_for_running=True,
    timeout=60,
    interval=2,
    name_after_project=False
):
    wait_val = int(interval)
    timeout_val = int(timeout)
    conn = get_ec2_connection()
    instance_type = 't2.micro'
    key_name = 'Keep Hair'
    security_group = 'ssh-access'
    # tutorial uses 'ami-d0d8b8e0'
    image_id = 'ami-5189a661'

    reservations = conn.run_instances(
        image_id,
        key_name=key_name,
        instance_type=instance_type,
        security_groups=[security_group, ]
    )

    new_instances = [
        i for i in reservations.instances if i.state == u'pending'
    ]

    for i in new_instances:
        if name_after_project:
            env.ec2.create_tags(i.id, {'Name': projname})

        else:
            name_instance(inst=i)

    running_instance = []

    if wait_for_running:
        waited = 0

        while new_instances and (waited < timeout_val):
            time.sleep(wait_val)
            waited += int(wait_val)

            for instance in new_instances:
                instance.update()
                state = instance.state
                print "Instance %s is %s" % (instance.id, state)

                if state == 'running':
                    running_instance.append(
                        new_instances.pop(new_instances.index(i))
                    )


def list_aws_instances(verbose=True, state='all'):
    conn = get_ec2_connection()

    reservations = conn.get_all_reservations()
    instances = []
    for res in reservations:
        for inst in res.instances:
            if state == 'all' or inst.state == state:
                try:
                    inst_name = inst.tags['Name']
                except KeyError:
                    inst_name = "None"
                inst = {
                    'name': inst_name,
                    'id': inst.id,
                    'type': inst.instance_type,
                    'image': inst.image_id,
                    'state': inst.state,
                    'instance': inst,
                }
                instances.append(inst)
    env.instances = instances
    if verbose:
        pprint.pprint(env.instances)


def select_instance(state='running'):
    if env.get('active_instance', False):
        return

    list_aws_instances(state=state)

    prompt_text = "Please select from the following instances:\n"
    instance_template = " %(ct)d: %(state)s instance %(name)s %(id)s\n"

    for idx, instance in enumerate(env.instances):
        ct = idx + 1
        args = {'ct': ct}
        args.update(instance)
        prompt_text += instance_template % args

    prompt_text += "Choose an instance: "

    def validation(input):
        choice = int(input)
        if choice not in range(1, len(env.instances) + 1):
            raise ValueError("%d is not a valid instance" % choice)
        return choice

    choice = prompt(prompt_text, validate=validation)
    env.active_instance = env.instances[choice - 1]['instance']


def run_command_on_selected_server(command):
    select_instance()
    selected_hosts = [
        'ubuntu@' + env.active_instance.public_dns_name
    ]
    execute(command, hosts=selected_hosts)


def stop_instance():
    select_instance()
    print "Stopping instance %s.\n" % env.active_instance
    env.ec2.stop_instances(instance_ids=[env.active_instance.id, ])


def terminate_instance():
    select_instance(state='stopped')
    prompt_text = (
        "THIS WILL TERMINATE THE INSTANCE.\n"
        "THIS ACTION CANNOT BE UNDONE.\n"
        "ARE YOU SURE? (Y/n)\n"
    )

    def validation(input):
        choice = input
        if choice not in ['Y', 'n']:
            raise ValueError("%s is not a valid choice." % choice)
        return choice

    def terminate_selected_instance():
        termination_segment = [
            "Fetching axe and black hood...",
            "Dragging instance to execution stone...",
            "Ignoring pleas and protests from instance...",
            "Executing instance..."
        ]

        for act in termination_segment:
            print act
            time.sleep(1)

        env.ec2.terminate_instances(instance_ids=[env.active_instance.id, ])

        print "Comforting family of instance..."
        time.sleep(1)
        print "Instance terminated.\nYou monster."

    choice = prompt(prompt_text, validate=validation)
    if choice == 'Y':
        terminate_selected_instance()


def name_instance(inst=None):
    if not inst:
        select_instance()
        inst = env.active_instance

    prompt_text = "Enter a name for this instance:\n"

    new_name = prompt(prompt_text)

    if new_name == '':
        new_name = 'None'

    env.ec2.create_tags(
        inst.id,
        {'Name': new_name}
    )


def _setup_suite():
    # Update apt, install the stuff we need, and update pip
    sudo('apt-get update')
    sudo('apt-get install -y nginx git python-pip supervisor')
    sudo('pip install --upgrade pip')

    # Go ahead and stop supervisord from running; it will be<commit_msg>adding postgresql install to _setup_suite<commit_after>from fabric.api import env, prompt, execute, sudo, run, put
from fabric.contrib import files
import boto.ec2
import time
import pprint


env.aws_region = 'us-west-2'
env.key_filename = '~/.ssh/KeepHair.pem'
git_user = 'tlake'
projname = 'learning-journal'
git_path = git_user + '/' + projname
appname = 'journal.py'


def get_ec2_connection():
    if 'ec2' not in env:
            conn = boto.ec2.connect_to_region(env.aws_region)
            if conn is not None:
                env.ec2 = conn
                print "Connected to EC2 region %s" % env.aws_region
            else:
                msg = "Unable to connect to EC2 region %s"
                raise IOError(msg % env.aws_region)
    return env.ec2


def provision_instance(wait_for_running=True, timeout=60,
                       interval=2, name_after_project=False
                       ):
    wait_val = int(interval)
    timeout_val = int(timeout)
    conn = get_ec2_connection()
    instance_type = 't2.micro'
    key_name = 'Keep Hair'
    security_group = 'ssh-access'
    # tutorial uses 'ami-d0d8b8e0'
    image_id = 'ami-5189a661'

    reservations = conn.run_instances(
        image_id,
        key_name=key_name,
        instance_type=instance_type,
        security_groups=[security_group, ]
    )

    new_instances = [
        i for i in reservations.instances if i.state == u'pending'
    ]

    for i in new_instances:
        if name_after_project:
            env.ec2.create_tags(i.id, {'Name': projname})

        else:
            name_instance(inst=i)

    running_instance = []

    if wait_for_running:
        waited = 0

        while new_instances and (waited < timeout_val):
            time.sleep(wait_val)
            waited += int(wait_val)

            for instance in new_instances:
                instance.update()
                state = instance.state
                print "Instance %s is %s" % (instance.id, state)

                if state == 'running':
                    running_instance.append(
                        new_instances.pop(new_instances.index(i))
                    )


def list_aws_instances(verbose=True, state='all'):
    conn = get_ec2_connection()

    reservations = conn.get_all_reservations()
    instances = []
    for res in reservations:
        for inst in res.instances:
            if state == 'all' or inst.state == state:
                try:
                    inst_name = inst.tags['Name']
                except KeyError:
                    inst_name = "None"
                inst = {
                    'name': inst_name,
                    'id': inst.id,
                    'type': inst.instance_type,
                    'image': inst.image_id,
                    'state': inst.state,
                    'instance': inst,
                }
                instances.append(inst)
    env.instances = instances
    if verbose:
        pprint.pprint(env.instances)


def select_instance(state='running'):
    if env.get('active_instance', False):
        return

    list_aws_instances(state=state)

    prompt_text = "Please select from the following instances:\n"
    instance_template = " %(ct)d: %(state)s instance %(name)s %(id)s\n"

    for idx, instance in enumerate(env.instances):
        ct = idx + 1
        args = {'ct': ct}
        args.update(instance)
        prompt_text += instance_template % args

    prompt_text += "Choose an instance: "

    def validation(input):
        choice = int(input)
        if choice not in range(1, len(env.instances) + 1):
            raise ValueError("%d is not a valid instance" % choice)
        return choice

    choice = prompt(prompt_text, validate=validation)
    env.active_instance = env.instances[choice - 1]['instance']


def run_command_on_selected_server(command):
    select_instance()
    selected_hosts = [
        'ubuntu@' + env.active_instance.public_dns_name
    ]
    execute(command, hosts=selected_hosts)


def stop_instance():
    select_instance()
    print "Stopping instance %s.\n" % env.active_instance
    env.ec2.stop_instances(instance_ids=[env.active_instance.id, ])


def terminate_instance():
    select_instance(state='stopped')
    prompt_text = (
        "THIS WILL TERMINATE THE INSTANCE.\n"
        "THIS ACTION CANNOT BE UNDONE.\n"
        "ARE YOU SURE? (Y/n)\n"
    )

    def validation(input):
        choice = input
        if choice not in ['Y', 'n']:
            raise ValueError("%s is not a valid choice." % choice)
        return choice

    def terminate_selected_instance():
        termination_segment = [
            "Fetching axe and black hood...",
            "Dragging instance to execution stone...",
            "Ignoring pleas and protests from instance...",
            "Executing instance..."
        ]

        for act in termination_segment:
            print act
            time.sleep(1)

        env.ec2.terminate_instances(instance_ids=[env.active_instance.id, ])

        print "Comforting family of instance..."
        time.sleep(1)
        print "Instance terminated.\nYou monster."

    choice = prompt(prompt_text, validate=validation)
    if choice == 'Y':
        terminate_selected_instance()


def name_instance(inst=None):
    if not inst:
        select_instance()
        inst = env.active_instance

    prompt_text = "Enter a name for this instance:\n"

    new_name = prompt(prompt_text)

    if new_name == '':
        new_name = 'None'

    env.ec2.create_tags(
        inst.id,
        {'Name': new_name}
    )


def _setup_suite():
    # Update apt, install the stuff we need, and update pip
    sudo('apt-get update')
    sudo('apt-get install -y '
         'nginx git python-pip supervisor postgresql postgresql-contrb')
    sudo('pip install --upgrade pip')

    # Go ahead and stop supervisord from running; it will be
{'fraction_non_alphanumeric': 0.10132347885871433, 'fraction_numerical': 0.006359573736679271, 'mean_word_length': 2.077757207088072, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '1290508', 'n_tokens_mistral': 3597, 'n_tokens_neox': 3399, 'n_words': 858}