def shrink(cur_workers, shrink_ratio, pool):

    ec2 = boto3.resource('ec2')

    #don't do anything if only one worker
    if cur_workers == 1:
        return True

    #number of workers to remove
    target_workers = cur_workers - ceil(cur_workers / shrink_ratio)

    #remove required number of instances from worker pool
    for i in range(target_workers):
        removed_inst = remove_one_ec2(pool)
        removed_item = removed_inst[0]
        remove_from_elb(removed_item)
        sleep(5)
        ec2.instances.filter(InstanceIds=[removed_item]).terminate()

        try:
            pool.remove(removed_inst)
        except Exception:
            pass

        message = "removed instance " + removed_item
        print(message)

    #sleep(5)

    message = "removed " + str(target_workers) + " workers"
    print(message, flush=True)

    return True
Esempio n. 2
0
def ec2_shrink():
    # create connection to ec2
    ec2 = boto3.resource('ec2')
    instances = get_workpool()

    removed_inst = remove_one_ec2(instances)

    if removed_inst == False:
        removed_item = ''
    else:
        removed_item = removed_inst[0]

    if len(instances) == 0:
        flash("There is no Instance Available")
        return redirect(url_for('ec2_list'))

    if removed_item != '':
        remove_from_elb(removed_item)
        sleep(5)
        ec2.instances.filter(InstanceIds=[removed_item]).terminate()
        message = "removed instance " + removed_item

    else:
        message = "Can't Shrink Anymore: Only One Instance Left in the Load Balancer"

    flash(message)
    return redirect(url_for('ec2_list'))
def ec2_destroy(id):
    # create connection to ec2
    ec2 = boto3.resource('ec2')

    remove_from_elb(id)

    ec2.instances.filter(InstanceIds=[id]).terminate()

    return redirect(url_for('ec2_list'))
def ec2_termination():
    # create connection to ec2
    ec2 = boto3.resource('ec2')
    instances = get_workpool()

    #remove instance from the load balancer and terminate
    for inst in instances:
        remove_from_elb(inst[0])
        ec2.instances.filter(InstanceIds=[inst[0]]).terminate()

    flash("All workers have been terminated")
    return redirect(url_for('ec2_list'))
def ec2_deregister(id):

    remove_from_elb(id)

    return redirect(url_for('ec2_list'))