Esempio n. 1
0
def start_instance(boxname, env_vars, DB):

    if boxname not in DB['stopped_instances']:
        print('the box is not available check again:')
        sys.exit()

    print('Waiting for confirmation from AWS')
    region = env_vars['aws_region']
    home_folder = env_vars['HOME']
    access_key = env_vars['access_key']
    secret_key = env_vars['secret_key']
    awsf = AWSec2Funcs(region, access_key, secret_key)

    my_ssh_key_path = env_vars['key_pair_path']
    ssh_key_name = env_vars['key_pair_name']

    id = DB['stopped_instances'][boxname]['id']
    DB['running_instances'][boxname] = awsf.start_ec2_instance(id)
    DB['running_instances'][boxname]['sdrive'] = DB['stopped_instances'][
        boxname]['sdrive']
    del (DB['stopped_instances'][boxname])
    # DB['available_names'].append(boxname)
    save_database(DB, env_vars['db_path'])
    write_into_text(
        boxname, '''
Host {0}
    HostName {1}
    User ubuntu
    IdentityFile {2}
    ForwardAgent yes
    StrictHostKeyChecking no
'''.format(boxname, DB['running_instances'][boxname]['public_ip'],
           my_ssh_key_path), os.path.join(home_folder, '.ssh/config'))

    print('ec2 instance {0} started successfully'.format(boxname))
Esempio n. 2
0
def create_instance(boxname, boxtype, shut_down_time, env_vars, DB):

    update_status(env_vars, DB)

    region = env_vars['aws_region']
    home_folder = env_vars['HOME']
    access_key = env_vars['access_key']
    secret_key = env_vars['secret_key']
    awsf = AWSec2Funcs(region, access_key, secret_key)

    my_ssh_key_path = env_vars['key_pair_path']
    ssh_key_name = env_vars['key_pair_name']

    if not boxname:
        boxname = get_box_name(DB, env_vars['db_path'])
    else:
        if boxname[:3] == 'box' or \
                boxname in DB['running_instances'] or \
                boxname in DB['stopped_instances']:
            print("enter a better name. either exists or starts with box")
            sys.exit()

    params = {}
    params['ssh_key_name'] = ssh_key_name
    params['aws_ami'] = env_vars['aws_ami']
    params['aws_iam_role'] = env_vars['role_name']
    params['vpc'] = DB[env_vars['vpc_name']]
    params['box_type'] = boxtype
    params['name'] = env_vars['your_name'] + boxname

    print('wainting for confirmation from AWS')

    DB['running_instances'][boxname] = awsf.create_ec2_instance(params)
    write_into_text(
        boxname, '''
Host {0}
    HostName {1}
    User ubuntu
    IdentityFile {2}
    ForwardAgent yes
    StrictHostKeyChecking no
'''.format(boxname, DB['running_instances'][boxname]['public_ip'],
           my_ssh_key_path), os.path.join(home_folder, '.ssh/config'))
    save_database(DB, env_vars['db_path'])

    tmp_dir = os.path.join(env_vars['env_dir'], 'tmp')
    tmp_tclock = os.path.join(tmp_dir, 'tclock.py')
    tmp_cron = os.path.join(tmp_dir, 'cron')

    if not os.path.isdir(tmp_dir):
        os.mkdir(tmp_dir)

    dir_path = os.path.dirname(os.path.realpath(__file__))
    tmp_tclock = os.path.join(dir_path, 'scripts', 'tclock.py')

    with open(tmp_cron, 'wb') as f:
        towrite = '*/{0} * * * * python /home/ubuntu/.provisionpad/tclock.py\n'.format(
            shut_down_time)
        f.write(towrite.encode('UTF-8'))

    print('Setting up ec2 instance')
    time.sleep(30)

    out = run_command(['ssh', boxname, 'pip', 'install', 'psutil'])
    if out != 0:
        raise Exception('Failed to install psutil on server')
    out = run_command(['ssh', boxname, 'mkdir', '-p', '.provisionpad/data'])
    if out != 0:
        raise Exception('Failed to create .provisionpad/data')
    out = run_command(
        ['ssh', boxname, 'touch', '.provisionpad/data/total_idle.out'])
    if out != 0:
        raise Exception('Failed to create .provisionpad/data/total_idle.out')
    out = run_command(
        ['scp', tmp_cron, '{0}:~/.provisionpad/'.format(boxname)])
    if out != 0:
        raise Exception('Failed to copy the crontab file')
    out = run_command(
        ['scp', tmp_tclock, '{0}:~/.provisionpad/'.format(boxname)])
    if out != 0:
        raise Exception('Failed to copy the tmp_tclock')
    out = run_command(
        ['ssh', boxname, 'crontab', '/home/ubuntu/.provisionpad/cron'])
    if out != 0:
        raise Exception('schedule a cron job')
    os.remove(tmp_cron)

    print('ec2 instance {} created successfully'.format(boxname))
    show_status(env_vars, DB)
Esempio n. 3
0
def update_status(env_vars, DB):

    region = env_vars['aws_region']
    home_folder = env_vars['HOME']
    access_key = env_vars['access_key']
    secret_key = env_vars['secret_key']
    awsec2f = AWSec2Funcs(region, access_key, secret_key)
    aws_inst_info = awsec2f.instance_state(env_vars['your_name'])
    aws_inst_info_d = deepcopy(aws_inst_info)

    clean_propad_from_file(os.path.join(home_folder, '.ssh/config'))

    for ins in aws_inst_info:
        if aws_inst_info[ins][0] == 'terminated':
            aws_inst_info_d.pop(ins)
        if not (aws_inst_info[ins][0] == 'stopped' or aws_inst_info[ins][0]
                == 'running' or aws_inst_info[ins][0] == 'terminated'):
            print('Waiting to complete the transition. Try again a bit later.')
            sys.exit()

    aws_inst_info = aws_inst_info_d

    DBD = deepcopy(DB)
    for ins, ins_info in DBD['running_instances'].items():
        if ins_info['id'] not in aws_inst_info:
            print('seems like the instance you have created ')
            print('has been removed from the aws manually most likely')
            print('removing it from the database')
            del (DB['running_instances'][ins])
            if ins[0:3] == 'box':
                DB['available_names'].append(ins)
            save_database(DB, env_vars['db_path'])
            delete_text_from_file(ins, os.path.join(home_folder,
                                                    '.ssh/config'))
        elif ins_info['id'] in aws_inst_info and aws_inst_info[
                ins_info['id']][0] == 'stopped':
            print('seems like the instance has been stopped')
            print('removing it from the running instances')
            DB['stopped_instances'][ins] = DB['running_instances'][ins]
            del (DB['running_instances'][ins])
            save_database(DB, env_vars['db_path'])
            delete_text_from_file(ins, os.path.join(home_folder,
                                                    '.ssh/config'))
        else:
            write_into_text(
                ins, '''
Host {0}
    HostName {1}
    User ubuntu
    IdentityFile {2}
    ForwardAgent yes
    StrictHostKeyChecking no
'''.format(ins, DB['running_instances'][ins]['public_ip'],
            env_vars['key_pair_path']), os.path.join(home_folder,
                                                    '.ssh/config'))
            print('{0} is fine as expected'.format(ins))

    DBD = deepcopy(DB)
    for ins, ins_info in DBD['stopped_instances'].items():
        if ins_info['id'] not in aws_inst_info:
            print('seems like the instance you have created ')
            print('has been removed from the aws manually most likely')
            print('removing it from the database')
            del (DB['stopped_instances'][ins])
            if ins[0:3] == 'box':
                DB['available_names'].append(ins)
            save_database(DB, env_vars['db_path'])
            delete_text_from_file(ins, os.path.join(home_folder,
                                                    '.ssh/config'))
        elif ins_info['id'] in aws_inst_info and aws_inst_info[
                ins_info['id']][0] == 'running':
            print('seems like the instance has started manually')
            print('moving from stopped to running')
            DB['running_instances'][ins] = DB['stopped_instances'][ins]
            DB['running_instances'][ins]['public_ip'] = aws_inst_info[
                ins_info['id']][1]
            del (DB['stopped_instances'][ins])
            save_database(DB, env_vars['db_path'])
            write_into_text(
                ins, '''
Host {0}
    HostName {1}
    User ubuntu
    IdentityFile {2}
    ForwardAgent yes
    StrictHostKeyChecking no
'''.format(ins, DB['running_instances'][ins]['public_ip'],
            env_vars['key_pair_path']), os.path.join(home_folder,
                                                    '.ssh/config'))
        else:
            print('{0} is fine as expected'.format(ins))

    ids_db = set([])
    for x in DB['running_instances']:
        ids_db.add(DB['running_instances'][x]['id'])
    for x in DB['stopped_instances']:
        ids_db.add(DB['stopped_instances'][x]['id'])

    to_create = set(aws_inst_info) - ids_db
    for x in to_create:
        ins = get_box_name(DB, env_vars['db_path'])
        thekeyname = 'stopped_instances'
        if aws_inst_info[x][0] == 'running':
            thekeyname = 'running_instances'
        boxname = get_box_name(DB, env_vars['db_path'])
        DB[thekeyname][boxname] = awsec2f.get_instance_info(x)
        write_into_text(
            ins, '''
Host {0}
    HostName {1}
    User ubuntu
    IdentityFile {2}
    ForwardAgent yes
    StrictHostKeyChecking no
'''.format(ins,
           DB[thekeyname][boxname]['public_ip'], env_vars['key_pair_path']),
            os.path.join(home_folder, '.ssh/config'))

    if len(ids_db - set(aws_inst_info)) > 0:
        raise Exception('this should not happen in status')

    save_database(DB, env_vars['db_path'])
Esempio n. 4
0
def create_instance(boxname, boxtype, shut_down_time, env_vars, DB):

    update_status(env_vars, DB)

    region = env_vars['aws_region']
    home_folder = env_vars['HOME']
    access_key = env_vars['access_key']
    secret_key = env_vars['secret_key']
    awsf = AWSec2Funcs(region, access_key, secret_key)

    my_ssh_key_path = env_vars['key_pair_path']
    ssh_key_name = env_vars['key_pair_name']

    if not boxname:
        boxname = get_box_name(DB, env_vars['db_path'])
    else:
        if boxname[:3] == 'box' or \
                boxname in DB['running_instances'] or \
                boxname in DB['stopped_instances']:
            print("enter a better name. either exists or starts with box")
            sys.exit()

    params = {}
    params['ssh_key_name'] = ssh_key_name
    params['aws_ami'] = env_vars['aws_ami']
    params['aws_iam_role'] = env_vars['role_name']
    params['vpc'] = DB[env_vars['vpc_name']]
    params['box_type'] = boxtype
    params['name'] = env_vars['your_name'] + boxname

    print('Waiting for confirmation from AWS')

    DB['running_instances'][boxname] = awsf.create_ec2_instance(params)
    write_into_text(
        boxname, '''
Host {0}
    HostName {1}
    User ubuntu
    IdentityFile {2}
    ForwardAgent yes
    StrictHostKeyChecking no
'''.format(boxname, DB['running_instances'][boxname]['public_ip'],
           my_ssh_key_path), os.path.join(home_folder, '.ssh/config'))

    drivel = 'fgh'
    DB['running_instances'][boxname]['sdrive_names'] = [
        (params['name'] + 'VOL{0}'.format(i), '/dev/xvd{0}'.format(drivel[i]))
        for i in range(len(drivel))
    ]
    DB['running_instances'][boxname]['sdrive'] = {}
    save_database(DB, env_vars['db_path'])

    tmp_dir = os.path.join(env_vars['env_dir'], 'tmp')
    tmp_tclock = os.path.join(tmp_dir, 'tclock.py')
    tmp_cron = os.path.join(tmp_dir, 'cron')

    if not os.path.isdir(tmp_dir):
        os.mkdir(tmp_dir)

    dir_path = os.path.dirname(os.path.realpath(__file__))
    tmp_tclock = os.path.join(dir_path, 'scripts', 'tclock.py')

    # The following line is OS dependent if it changes from ubuntu to something else then
    # It will cause trouble for auto shutdown -- Amir
    with open(tmp_cron, 'wb') as f:
        towrite = '*/{0} * * * * python /home/ubuntu/.provisionpad/tclock.py\n'.format(
            shut_down_time)
        f.write(towrite.encode('UTF-8'))

    print('Setting up EC2 instance')

    awssgf = AWSsgFuncs(region, access_key, secret_key)
    awssgf.check_public_ip(env_vars, DB)
    vpcparams = DB[env_vars['vpc_name']]
    awssgf.revoke_sg_permissions_all(vpcparams['vpc_id'])
    awssgf.set_sg_sshonly_local_ip(vpcparams['sg_id'], DB['public_ip'])
    awssgf.set_sg_http_egress(vpcparams['sg_id'])

    # some times it has problem ssh ing into instance
    # I have added this 30 seconds sleep to be safe
    # Is there a better way? -- Amir
    time.sleep(30)

    out = run_command(['ssh', boxname, 'pip', 'install', 'psutil'])
    if out != 0:
        raise Exception('Failed to install psutil on server')
    out = run_command(['ssh', boxname, 'mkdir', '-p', '.provisionpad/data'])
    if out != 0:
        raise Exception('Failed to create .provisionpad/data')
    out = run_command(
        ['ssh', boxname, 'touch', '.provisionpad/data/total_idle.out'])
    if out != 0:
        raise Exception('Failed to create .provisionpad/data/total_idle.out')
    out = run_command(
        ['scp', tmp_cron, '{0}:~/.provisionpad/'.format(boxname)])
    if out != 0:
        raise Exception('Failed to copy the crontab file')
    out = run_command(
        ['scp', tmp_tclock, '{0}:~/.provisionpad/'.format(boxname)])
    if out != 0:
        raise Exception('Failed to copy the tmp_tclock')
    out = run_command(
        ['ssh', boxname, 'crontab', '/home/ubuntu/.provisionpad/cron'])
    if out != 0:
        raise Exception('schedule a cron job')
    os.remove(tmp_cron)

    awssgf.revoke_sg_permissions_all(vpcparams['vpc_id'])
    awssgf.set_sg_sshonly_local_ip(vpcparams['sg_id'], DB['public_ip'])

    print('ec2 instance {} created successfully'.format(boxname))
    show_status(env_vars, DB)