Ejemplo n.º 1
0
def labtainerStop(image, container_id, logger):
    labutils.logger = logger
    here = os.path.dirname(os.path.abspath(__file__))
    labname, name = getLabFromImage(image)
    parent = os.path.dirname(here)
    gparent = os.path.dirname(parent)
    lab_path = os.path.join(gparent, 'labs', labname)
    logger.debug('labtainerStop lab_path is %s' % lab_path)
    labtainer_config, start_config = labutils.GetBothConfigs(lab_path, logger)
    here = os.getcwd()
    student_dir = os.path.join(parent, 'labtainer-student')
    os.chdir(student_dir)
    zip_file_name = None
    for name, container in start_config.containers.items():
        logger.debug('name %s full %s' % (name, container.full_name))
        gimage = getGImage(labname, name)
        if image.startswith(gimage):

            labutils.GatherOtherArtifacts(lab_path, name, container_id,
                                          container.user, container.password,
                                          True)
            base_file_name, zip_file_name = labutils.CreateCopyChownZip(
                start_config,
                labtainer_config,
                name,
                container.full_name,
                container.image_name,
                container.user,
                container.password,
                True,
                True,
                running_container=container_id)
    os.chdir(here)
    return zip_file_name
Ejemplo n.º 2
0
def gatherZips(zip_list, image, logger):
    labutils.logger = logger
    here = os.path.dirname(os.path.abspath(__file__))
    labname, name = getLabFromImage(image)
    parent = os.path.dirname(here)
    gparent = os.path.dirname(parent)
    lab_path = os.path.join(gparent, 'labs', labname)
    logger.debug('gatherZips lab_path is %s' % lab_path)
    labtainer_config, start_config = labutils.GetBothConfigs(lab_path, logger)
    labutils.GatherZips(zip_list, labtainer_config, start_config, labname,
                        lab_path)
Ejemplo n.º 3
0
def getContainer(lab_path, container_name, is_gns3, logger):
    labname = os.path.basename(lab_path)
    labtainer_config, start_config = labutils.GetBothConfigs(lab_path, logger)
    retval = None
    if container_name in start_config.containers:
        if is_gns3:
            image = '%s_%s-labtainer' % (labname, container_name)
        else:
            image = '%s.%s.student' % (labname, container_name)
        retval = labutils.GetContainerID(image)
    return retval
Ejemplo n.º 4
0
def getStartConfig(image_name, logger):
    here = os.path.dirname(os.path.abspath(__file__))
    labname, comp_name = getLabFromImage(image_name)
    if labname is None:
        log.error('no labtainer labname found in image %s' % image_name)
        return None, None, None, None
    parent = os.path.dirname(here)
    gparent = os.path.dirname(parent)
    lab_path = os.path.join(gparent, 'labs', labname)
    logger.debug('paremterizeOne lab_path is %s' % lab_path)
    labtainer_config, start_config = labutils.GetBothConfigs(lab_path, logger)
    return start_config, comp_name, labname, lab_path
Ejemplo n.º 5
0
def labtainerStop(image, container_id, logger):
    labutils.logger = logger
    here = os.path.dirname(os.path.abspath(__file__))
    labname, name = getLabFromImage(image)
    parent = os.path.dirname(here)
    gparent = os.path.dirname(parent)
    lab_path = os.path.join(gparent, 'labs', labname)
    logger.debug('labtainerStop lab_path is %s' % lab_path)
    labtainer_config, start_config = labutils.GetBothConfigs(lab_path, logger)
    here = os.getcwd()
    student_dir = os.path.join(parent, 'labtainer-student')
    os.chdir(student_dir)
    zip_file_name = None
    for name, container in start_config.containers.items():
        logger.debug('name %s full %s' % (name, container.full_name))
        gimage = getGImage(labname, name)
        if image.startswith(gimage):

            labutils.GatherOtherArtifacts(lab_path, name, container_id,
                                          container.user, container.password,
                                          True)
            base_file_name, zip_file_name = labutils.CreateCopyChownZip(
                start_config,
                labtainer_config,
                name,
                container.full_name,
                container.image_name,
                container.user,
                container.password,
                True,
                True,
                running_container=container_id)

            if container.thumb_stop is not None:
                cmd = 'sudo ' + os.path.join(lab_path, container.thumb_stop)
                logger.debug('thumb_stop command %s' % cmd)
                ps = subprocess.Popen(shlex.split(cmd),
                                      stdout=subprocess.PIPE,
                                      stderr=subprocess.PIPE)
                output = ps.communicate()
                if len(output[1].strip()) > 0:
                    logger.error('thumb stop failed %s\%s' %
                                 (container.thumb_stop, output[1]))
                    return None

    os.chdir(here)
    return zip_file_name
Ejemplo n.º 6
0
def moreTerm(image, container_id, logger):
    labutils.logger = logger
    logger.debug('moreTerm mage %s' % image)
    labname, box = getLabFromImage(image)
    here = os.path.dirname(os.path.abspath(__file__))
    gparent = os.path.dirname(os.path.dirname(here))
    lab_path = os.path.join(gparent, 'labs', labname)
    logger.debug('lab_path is %s' % lab_path)

    labtainer_config, start_config = labutils.GetBothConfigs(lab_path, logger)
    for name, container in start_config.containers.items():
        #print('name %s full %s' % (name, container.full_name))
        gimage = getGImage(labname, name)
        if image.startswith(gimage):
            return labutils.DoMoreterm(lab_path,
                                       name,
                                       alt_name=container_id[:12])
    logger.error('moreTerm failed to find container for %s' % image)
    return False
Ejemplo n.º 7
0
def labtainerTerms(images, logger):
    labutils.logger = logger
    logger.debug('labtainerTerms %d images' % len(images))
    labname, box = getLabFromImage(images[0])
    here = os.path.dirname(os.path.abspath(__file__))
    gparent = os.path.dirname(os.path.dirname(here))
    lab_path = os.path.join(gparent, 'labs', labname)
    logger.debug('lab_path is %s' % lab_path)
    image_map = getImageMap(labname, logger)
    container_map = {}
    labtainer_config, start_config = labutils.GetBothConfigs(lab_path, logger)
    for name, container in start_config.containers.items():
        #print('name %s full %s' % (name, container.full_name))
        gimage = getGImage(labname, name)
        for image in image_map:
            if image.startswith(gimage):
                gcontainer = image_map[image]
                #print('got match %s cont %s' % (image, gcontainer))
                container_map[container.full_name] = gcontainer

    labutils.DoTerminals(start_config, lab_path, container_map=container_map)
Ejemplo n.º 8
0
def setup_to_validate(lab_path, labname, validatetestsets,
                      validatetestsets_path, logger):
    # Create TEMPDIR - remove if it exists
    if os.path.exists(TEMPDIR):
        shutil.rmtree(TEMPDIR)
    TEMPLOCAL = os.path.join(TEMPDIR, ".local")
    TEMPLOCALBIN = os.path.join(TEMPDIR, ".local", "bin")
    os.makedirs(TEMPLOCAL)
    os.makedirs(TEMPLOCALBIN)

    # Pick arbitrary e-mail
    user_email = "*****@*****.**" % labname
    #config_path       = os.path.join(lab_path,"config")
    #start_config_path = os.path.join(config_path,"start.config")
    #start_config = ParseStartConfig.ParseStartConfig(start_config_path, labname, "instructor", labutils.logger)
    labtainer_config, start_config = labutils.GetBothConfigs(
        lab_path, 'instructor', logger)

    # Warns if xterm has no instruction.txt file
    for container_name, container in start_config.containers.items():
        if container.xterm is not None:
            # instruction.txt file path
            instruction_path = "%s/%s/instructions.txt" % (lab_path,
                                                           container_name)
            if not (os.path.exists(instruction_path)
                    and os.path.isfile(instruction_path)):
                logger.warning(
                    "container %s instruction_path file %s not found" %
                    (container_name, instruction_path))

    lab_master_seed = start_config.lab_master_seed
    # Create hash using LAB_MASTER_SEED concatenated with user's e-mail
    # LAB_MASTER_SEED is per laboratory - specified in start.config
    string_to_be_hashed = '%s:%s' % (lab_master_seed, user_email)
    mymd5 = md5.new()
    mymd5.update(string_to_be_hashed)
    lab_instance_seed = mymd5.hexdigest()
    labutils.logger.debug("seed %s" % lab_instance_seed)

    # Create files
    LAB_SEEDFILE = os.path.join(TEMPLOCAL, ".seed")
    with open(LAB_SEEDFILE, "w") as fh:
        fh.write("%s\n" % lab_instance_seed)
    fh.close()
    USER_EMAILFILE = os.path.join(TEMPLOCAL, ".email")
    with open(USER_EMAILFILE, "w") as fh:
        fh.write("%s\n" % user_email)
    fh.close()
    LAB_NAMEFILE = os.path.join(TEMPLOCAL, ".labname")
    with open(LAB_NAMEFILE, "w") as fh:
        fh.write("%s\n" % labname)
    fh.close()
    WATERMARK_NAMEFILE = os.path.join(TEMPLOCAL, ".watermark")
    string_to_be_hashed = '%s:%s' % (lab_instance_seed, user_email)
    mymd5 = md5.new()
    mymd5.update(string_to_be_hashed)
    watermark = mymd5.hexdigest()
    labutils.logger.debug("watermark %s" % watermark)
    with open(WATERMARK_NAMEFILE, "w") as fh:
        fh.write("%s\n" % watermark)
    fh.close()

    # Copy 'config' and 'instr_config' from LABPATH to TEMPLOCAL
    LAB_CONFIG = os.path.join(lab_path, "config")
    LAB_INSTRCONFIG = os.path.join(lab_path, "instr_config")
    TEMP_LAB_CONFIG = os.path.join(TEMPLOCAL, "config")
    TEMP_LAB_INSTRCONFIG = os.path.join(TEMPLOCAL, "instr_config")
    shutil.copytree(LAB_CONFIG, TEMP_LAB_CONFIG)
    shutil.copytree(LAB_INSTRCONFIG, TEMP_LAB_INSTRCONFIG)
    # If we are doing validatetestsets - replace the three config files
    if validatetestsets:
        parameterconfig_path = os.path.join(validatetestsets_path,
                                            "parameter.config")
        resultsconfig_path = os.path.join(validatetestsets_path,
                                          "results.config")
        goalsconfig_path = os.path.join(validatetestsets_path, "goals.config")
        target_parameterconfig_path = os.path.join(TEMP_LAB_CONFIG,
                                                   "parameter.config")
        target_resultsconfig_path = os.path.join(TEMP_LAB_INSTRCONFIG,
                                                 "results.config")
        target_goalsconfig_path = os.path.join(TEMP_LAB_INSTRCONFIG,
                                               "goals.config")
        shutil.copy(parameterconfig_path, target_parameterconfig_path)
        shutil.copy(resultsconfig_path, target_resultsconfig_path)
        shutil.copy(goalsconfig_path, target_goalsconfig_path)

    # Get a list of any executable in '_bin' directory
    # except fixlocal.sh, treataslocal, startup.sh
    binfilelist = glob.glob("%s/*/_bin/*" % lab_path)
    for binfilepath in binfilelist:
        binfilename = os.path.basename(binfilepath)
        if not (binfilename == "fixlocal.sh" or binfilename == "treataslocal"
                or binfilename == "startup.sh"):
            if binfilename not in executefilelist:
                executefilelist.append(binfilename)
                shutil.copy(binfilepath, TEMPLOCALBIN)

    email_labname = "%s.%s" % (user_email.replace("@", "_at_"), labname)

    container_list = []
    container_list.append(start_config.grade_container)
    for name, container in start_config.containers.items():
        if container.full_name not in container_list:
            container_list.append(container.full_name)

    return lab_instance_seed, container_list, email_labname
Ejemplo n.º 9
0
labtainers_path = os.path.join(labtainer_dir, 'labs')
parser = argparse.ArgumentParser(
    description='Generate gns3 network interfaces file.')
parser.add_argument('labname', help='Name of labtainers lab')
parser.add_argument('gns3_proj', help='Name of gns3 project')
args = parser.parse_args()

print('labname: %s gns3: %s' % (args.labname, args.gns3_proj))
labutils.logger = LabtainerLogging.LabtainerLogging(
    "genNet.log", 'eh', "../../config/labtainer.config")
gns3_proj = os.path.join(gns3_path, args.gns3_proj, args.gns3_proj + '.gns3')
if not os.path.isfile(gns3_proj):
    print('no gns3 proj file found at %s' % gns3_proj)
    exit(1)
labtainer_lab = os.path.join(labtainers_path, args.labname)
dumb, start_config = labutils.GetBothConfigs(labtainer_lab, labutils.logger)

with open(gns3_proj) as fh:
    gns3_json = json.load(fh)
'''
Look for logo
'''
logo_path = os.path.join(labtainer_lab, 'config', 'logo.png')
if os.path.isfile(logo_path):
    dest = os.path.join(gns3_path, args.gns3_proj, 'project-files', 'images')
    try:
        os.mkdir(dest)
    except:
        pass
    shutil.copy(logo_path, dest)
    about_path = os.path.join(labtainer_lab, 'config', 'about.txt')
Ejemplo n.º 10
0
def RebuildLab(lab_path,
               force_build=False,
               quiet_start=False,
               just_container=None,
               run_container=None,
               servers=None,
               clone_count=None,
               no_pull=False,
               use_cache=True,
               local_build=False,
               just_build=False):
    # Pass 'True' to ignore_stop_error (i.e., ignore certain error encountered during StopLab
    #                                         since it might not even be an error)
    labutils.StopLab(lab_path,
                     True,
                     run_container=run_container,
                     servers=servers,
                     clone_count=clone_count)
    labutils.logger.debug('Back from StopLab clone_count was %s' % clone_count)
    labname = os.path.basename(lab_path)
    my_start_config = os.path.join('./.tmp', labname, 'start.config')
    if os.path.isfile(my_start_config):
        labutils.logger.debug('Cached start.config removed %s' %
                              my_start_config)
        os.remove(my_start_config)
    labtainer_config, start_config = labutils.GetBothConfigs(
        lab_path, labutils.logger, servers, clone_count)

    DoRebuildLab(lab_path,
                 force_build=force_build,
                 just_container=just_container,
                 start_config=start_config,
                 labtainer_config=labtainer_config,
                 run_container=run_container,
                 servers=servers,
                 clone_count=clone_count,
                 no_pull=no_pull,
                 use_cache=use_cache,
                 local_build=local_build)
    if not just_build:
        # Check existence of /home/$USER/$HOST_HOME_XFER directory - create if necessary
        host_home_xfer = labtainer_config.host_home_xfer
        myhomedir = os.environ['HOME']
        host_xfer_dir = '%s/%s' % (myhomedir, host_home_xfer)
        labutils.CreateHostHomeXfer(host_xfer_dir)
        labutils.DoStart(start_config, labtainer_config, lab_path, quiet_start,
                         run_container, servers, clone_count)
    if start_config.gns3.lower() == "yes":
        nonet = os.path.join(os.getenv('LABTAINER_DIR'), 'scripts', 'gns3',
                             'noNet.py')
        cmd = '%s %s' % (nonet, labname)
        ps = subprocess.Popen(shlex.split(cmd),
                              stderr=subprocess.PIPE,
                              stdout=subprocess.PIPE)
        output = ps.communicate()
        if len(output[1]) > 0:
            labutils.logger.error(output[1].decode('utf-8'))
        else:
            for line in output[0].decode('utf-8').splitlines():
                print(line)
        gennet = os.path.join(os.getenv('LABTAINER_DIR'), 'scripts', 'gns3',
                              'genNet.py')
        cmd = '%s %s %s' % (gennet, labname, labname)
        ps = subprocess.Popen(shlex.split(cmd),
                              stderr=subprocess.PIPE,
                              stdout=subprocess.PIPE)
        output = ps.communicate()
        if len(output[1]) > 0:
            labutils.logger.error(output[1].decode('utf-8'))
        else:
            for line in output[0].decode('utf-8').splitlines():
                print(line)
Ejemplo n.º 11
0
def DoRebuildLab(lab_path,
                 force_build=False,
                 just_container=None,
                 start_config=None,
                 labtainer_config=None,
                 run_container=None,
                 servers=None,
                 clone_count=None,
                 no_pull=False,
                 no_build=False,
                 use_cache=True,
                 local_build=False):
    retval = []
    labname = os.path.basename(lab_path)
    labutils.isValidLab(lab_path)
    if start_config is None:
        labtainer_config, start_config = labutils.GetBothConfigs(
            lab_path, labutils.logger, servers, clone_count)
    host_home_xfer = labtainer_config.host_home_xfer

    build_student = 'bin/buildImage.sh'
    LABS_DIR = os.path.abspath('../../labs')
    didfix = False
    ''' hackey assumption about running from labtainers-student or labtainers-instructor '''
    container_bin = './lab_bin'
    for name, container in start_config.containers.items():
        labutils.logger.debug('this container name %s just_container %s' %
                              (name, just_container))
        if just_container is not None and just_container != name:
            continue
        elif just_container == name:
            force_build = True
            print('Force build of %s' % just_container)
        mycontainer_name = container.full_name
        mycontainer_image_name = container.image_name
        if container.registry == labtainer_config.test_registry:
            branch, container_registry = registry.getBranchRegistry()
            base_registry = container_registry
        else:
            container_registry = container.registry
            base_registry = container.base_registry

        clone_names = labutils.GetContainerCloneNames(container)
        for clone_full in clone_names:
            cmd = 'docker rm %s' % clone_full
            ps = subprocess.Popen(shlex.split(cmd),
                                  stdout=subprocess.PIPE,
                                  stderr=subprocess.PIPE)
            output = ps.communicate()
            #labutils.logger.debug("Command was (%s)" % cmd)
            if len(output[1]) > 0:
                if 'running container' in output[1].decode('utf-8'):
                    labutils.logger.debug("Error from command %s was  '%s'" %
                                          (cmd, output[1].decode('utf-8')))
                    exit(1)
                else:
                    labutils.logger.debug(
                        "non-fatal Error from command %s was  '%s'" %
                        (cmd, output[1].decode('utf-8')))

        if container.from_image is not None:
            labutils.logger.debug('skip image taken from %s' %
                                  container.from_image)
            continue

        force_this_build = force_build
        if container.no_pull == 'YES':
            no_pull = True
        labutils.logger.debug('force_this_build: %r no_pull %r' %
                              (force_this_build, no_pull))
        image_info = labutils.imageInfo(mycontainer_image_name,
                                        container_registry,
                                        base_registry,
                                        labtainer_config,
                                        is_rebuild=True,
                                        no_pull=no_pull,
                                        local_build=local_build)
        if not force_this_build and image_info is None:
            if not local_build:
                labutils.logger.debug(
                    'Image %s exists nowhere, so force the build' %
                    mycontainer_image_name)
                print('Image %s exists nowhere, so force the build' %
                      mycontainer_image_name)
            else:
                labutils.logger.debug(
                    'Image %s does not exist locally.  Local build requested, so force the build'
                    % mycontainer_image_name)
                print(
                    'Image %s does not exist locally.  Local build requested, so force the build'
                    % mycontainer_image_name)

            force_this_build = True
        container_dir = os.path.join(lab_path, name)
        try:
            os.mkdir(os.path.join(container_dir, 'home_tar'))
        except:
            pass
        try:
            os.mkdir(os.path.join(container_dir, 'sys_tar'))
        except:
            pass
        removeStrays(container_dir, name, labname)
        ''' make sure big files have been copied before checking tars '''
        BigFiles.BigFiles(lab_path)
        BigExternal.BigExternal(lab_path, labutils.logger)
        ''' create sys_tar and home_tar before checking build dependencies '''
        CheckTars.CheckTars(container_dir, name, labutils.logger)
        if force_this_build or CheckBuild(
                lab_path, mycontainer_image_name, image_info, mycontainer_name,
                name, True, container_bin, start_config, base_registry,
                container.user, local_build):

            if no_build:
                labutils.logger.debug("Would (but won't) rebuild %s" %
                                      (mycontainer_name))
                print("Would (but won't) rebuild %s" % (mycontainer_name))
                return retval

            labutils.logger.debug(
                "Will rebuild %s,  force_this_build: %s  apt_source %s" %
                (mycontainer_name, force_this_build,
                 labtainer_config.apt_source))

            #Check if the container's Dockerfile exists
            dfile = '%s/%s/dockerfiles/Dockerfile.%s.%s.student' % (
                LABS_DIR, labname, labname, name)
            if not os.path.isfile(dfile):
                labutils.logger.error(
                    "Dockerfile.%s.%s.student is missing from labs/%s/dockerfiles."
                    % (labname, name, labname))
                exit(1)
            if local_build:
                ts, thebase = BaseImageTime(dfile, base_registry)
                if ts == 0:
                    labutils.logger.debug(
                        'No base found %s, look for local base' % thebase)
                    ts, thebase = BaseImageTime(dfile, None)
                    if ts == 0:
                        labutils.logger.error(
                            'No local image for %s and local build requested. Try "docker pull %s/%s"'
                            % (thebase, base_registry, the_base))
                        exit(1)
                    else:

                        labutils.logger.debug(
                            'Using local version of base %s' % thebase)
                        base_registry = 'LOCAL'

                else:
                    labutils.logger.debug('got ts, base %s' % thebase)

            retval.append(
                RegistryInfo(name, container.image_name, container_registry,
                             base_registry))

            if os.path.isfile(build_student):
                cmd = '%s %s %s %s %s %s %s %s %s %s %s %s' % (
                    build_student, labname, name, container.user,
                    container.password, True, LABS_DIR,
                    labtainer_config.apt_source, base_registry,
                    labutils.framework_version, str(local_build),
                    str(use_cache))
            else:
                labutils.logger.error("no image rebuild script\n")
                exit(1)
            labutils.logger.debug('cmd is %s' % cmd)
            ps = subprocess.Popen(shlex.split(cmd),
                                  stdout=subprocess.PIPE,
                                  stderr=subprocess.PIPE)
            output = ps.communicate()
            labutils.logger.debug('build_image back from communicate')
            #fatal_error = CheckBuildError(ps.stdout, labname, name)
            fatal_error = CheckBuildError(output[0].decode('utf-8'), labname,
                                          name)
            if not fatal_error:
                labutils.logger.debug('not fatal, do stderr')
                fatal_error = CheckBuildError(output[1].decode('utf-8'),
                                              labname, name)
                #fatal_error = CheckBuildError(ps.stderr, labname, name)
            else:
                labutils.logger.debug('fatal, do stderr')
                CheckBuildError(output[1].decode('utf-8'), labname, name)
                #CheckBuildError(ps.stderr, labname, name)
            labutils.logger.debug('done checkerror fatal %r' % fatal_error)
            if ps.returncode != 0:
                labutils.logger.error(
                    'Problem building %s, check log at $LABTAINER_DIR/logs/docker_build.log'
                    % labname)
                exit(1)
            if fatal_error:
                exit(1)
    return retval