Exemple #1
0
def launch_instances(conn, user_vpc, script, lab,
                     labmod, cfg, security_groups, subnets):
    """Launch lab instances for each user"""

    instances = []
    lab_tag = labs.calculate_lab_tag(conn, user_vpc, lab)
    print "Launching '{0}' lab with tag: {1}".format(lab, lab_tag)

    # debug log directory
    if not os.path.exists('train/logs'):
        os.makedirs('train/logs')

    zone_count = vpc.get_starting_zone(subnets)
    zone_max = zone_count + len(subnets)
    with open(USER_FILE) as users:
        for user in users:
            user = user.split(',')[0].strip()
            amikey = 0
            for instance in cfg['instance']:
                amibuild = True
                for count in range(instance['COUNT']):
                    current = instance.copy()
                    if 'NAME' in instance:
                        if current['COUNT'] > 1:
                            current['NAME'] = instance['NAME'] + '-' + str(count)
                        else:
                            current['NAME'] = instance['NAME']
                    if 'NAMES' in instance:
                        current['NAME'] = instance['NAMES'][count]

                    # autorotate zones
                    if instance['COUNT'] > 1:
                        current['ZONE'] = zone_count
                        zone_count += 1
                        if zone_count == zone_max:
                            zone_count = vpc.get_starting_zone(subnets)

                    # check for unique 'Name' tag
                    # Removed this check for speed
                    # TODO: Handle somehow - prompt or autoset a name
                    #_check_name_tag(conn, user_vpc, current)

                    # security group ids
                    sids = vpc.get_sg_ids(cfg, current, security_groups, VPC_TAG)

                    # device info
                    bdm, dinfo = configure_devices(current)

                    # network interface
                    interface = vpc.create_interface(vpc.get_subnet_id(current, subnets), sids)

                    # ami id
                    ami_id = AMIS[getattr(labmod, current['AMI_KEY'])]

                    # custom ami available?
                    images = conn.get_all_images(owners = ['self'])
                    name_tag = TRAINER + '-{0}-'.format(VPC) + \
                                         '{0}-'.format(lab) + \
                                         '{0}-'.format(script) + \
                                         '{0}'.format(amikey)
                    for image in images:
                        if 'Lab' in image.tags:
                            if image.tags['Name'] == name_tag:
                                current['SCRIPT'] = 'AMIBUILD'
                                ami_id = image.id

                    # user data script
                    udata = getattr(labmod, current['SCRIPT'])

                    # save the 'user data' script for reference
                    # useful for lab creation/debug
                    with open('train/logs/{0}.sh'.format(current['NAME']), 'w') as file:
                        file.write(udata.format(fqdn=current['NAME'], dinfo=dinfo))

                    # launch instance
                    print "Launching instance: {0}-{1} ...".format(user, current['NAME'])
                    reservation = conn.run_instances(image_id=ami_id,
                                                     key_name=user + '-{0}'.format(VPC),
                                                     user_data=udata.format(fqdn=current['NAME'],
                                                                            dinfo=dinfo),
                                                     instance_type=current['INSTANCE_TYPE'],
                                                     network_interfaces=interface,
                                                     block_device_map = bdm,
                                                     instance_profile_name=VPC_TAG)

                    # get instance object
                    current_res = reservation.instances[0]

                    # save instance/current
                    instances.append([current, current_res, user, amibuild, amikey, script])
                    amibuild = False

                amikey += 1

    # wait for all instances to finish booting
    print "Waiting for instances to initialize ..."
    time.sleep(20)
    for instance in instances:
        while instance[1].state != 'running':
            print "Waiting for instance '{0}-{1}' to initialize ...".format(instance[2],
                                                                              instance[0]['NAME'])
            time.sleep(0.5)
            instance[1].update()

    # set elastic ips and tag instances
    for instance in instances:
        # disable elastic_ips (for now)
        #_create_elastic_ips(conn, instance[0], instance[1], instance[2])
        _create_tags(conn, instance[0], instance[1], lab_tag,
                      instance[2], instance[3], instance[4], instance[5])

    final = labs.get_lab_instance_info(conn, user_vpc, lab_tag)
    output_user_files(conn, user_vpc, lab_tag)

    print "\nLab '{0}' launched with tag '{1}':".format(lab, lab_tag)
    print "\n  Instances:"
    for instance in final:
        print instance
    print ''
Exemple #2
0
def launch_instances(conn, user_vpc, lab, labmod, cfg, security_groups, subnets):
    """Launch lab instances for each user"""

    instances = []
    lab_tag = labs.calculate_lab_tag(conn, user_vpc, lab)
    print "Launching '{0}' lab with tag: {1}".format(lab, lab_tag)

    # debug log directory
    if not os.path.exists('train/logs'):
        os.makedirs('train/logs')

    with open(USER_FILE) as users:
        for user in users:
            user = user.strip()
            for instance in cfg['instance']:

                # check for unique 'Name' tag
                # Removed this check for speed
                # TODO: Handle somehow - prompt or autoset a name
                #_check_name_tag(conn, user_vpc, instance)

                # security group ids
                sids = vpc.get_sg_ids(cfg, instance, security_groups, VPC_TAG)

                # device info
                bdm, dinfo = configure_devices(instance)

                # network interface
                interface = vpc.create_interface(vpc.get_subnet_id(instance, subnets), sids)

                # user data script
                udata = getattr(labmod, instance['ID'])

                # save the 'user data' script for reference
                # useful for lab creation/debug
                with open('train/logs/{0}.sh'.format(instance['NAME']), 'w') as file:
                    file.write(udata.format(fqdn=instance['NAME'], dinfo=dinfo))

                #initialize_udata(labmod, instance, udata)
                # launch instance
                print "Launching instance: {0}-{1} ...".format(user, instance['NAME'])
                reservation = conn.run_instances(image_id=AMIS[getattr(labmod, instance['OS'])],
                                                 key_name=user + '-{0}'.format(TRAIN_TAG),
                                                 user_data=udata.format(fqdn=instance['NAME'],
                                                                        dinfo=dinfo),
                                                 instance_type=instance['INSTANCE_TYPE'],
                                                 network_interfaces=interface,
                                                 block_device_map = bdm,
                                                 instance_profile_name=VPC_TAG)

                # get instance object
                current = reservation.instances[0]

                # save instance/current
                instances.append([instance, current, user])

    # wait for all instances to finish booting
    print "Waiting for instances to initialize ..."
    time.sleep(20)
    for instance in instances:
        while instance[1].state != 'running':
            print "Waiting for instance '{0}-{1}' to initialize ...".format(instance[2],
                                                                              instance[0]['NAME'])
            time.sleep(1)
            instance[1].update()

    # set elastic ips and tag instances
    for instance in instances:
        _create_elastic_ips(conn, instance[0], instance[1], instance[2])
        _create_tags(conn, instance[0], instance[1], lab_tag, instance[2])

    final = labs.get_lab_instance_info(conn, user_vpc, lab_tag)
    output_user_files(conn, user_vpc, lab_tag)

    print "\nLab '{0}' launched with tag '{1}':".format(lab, lab_tag)
    print "\n  Instances:"
    for instance in final:
        print instance
    print ''