def main():
    parser = argparse.ArgumentParser(
        description=
        'Stop a Labtainers lab.  If no arguments are provided, then all labs are stopped.'
    )
    parser.add_argument('lab', nargs='?', default='all')
    args = parser.parse_args()

    labname = args.lab
    lablist = []
    if labname != 'all':
        labutils.logger = LabtainerLogging.LabtainerLogging(
            "labtainer.log", labname, "../../config/labtainer.config")
        lablist.append(labname)
    else:
        labutils.logger = LabtainerLogging.LabtainerLogging(
            "labtainer.log", labname, "../../config/labtainer.config")
        lablist = labutils.GetListRunningLab()

    for labname in lablist:
        labutils.logger.info("Begin logging stop.py for %s lab" % labname)
        # Pass 'False' to ignore_stop_error (i.e., do not ignore error)
        lab_path = os.path.join(os.path.abspath('../../labs'), labname)
        has_running_containers, running_containers_list = labutils.GetRunningContainersList(
        )
        if has_running_containers:
            has_lab_role, labnamelist = labutils.GetRunningLabNames(
                running_containers_list)
            if has_lab_role:
                if labname not in labnamelist:
                    labutils.logger.error(
                        "No lab named %s in currently running labs!" % labname)
                    sys.exit(1)
            else:
                labutils.logger.error("Student is not running any labs")
                sys.exit(1)
        else:
            labutils.logger.error("No running labs at all")
            sys.exit(1)
        current_lab = CurrentLab.CurrentLab()
        clone_count = current_lab.get('clone_count')
        servers = current_lab.get('servers')
        labutils.StopLab(lab_path,
                         False,
                         servers=servers,
                         clone_count=clone_count)
        current_lab.clear()
        current_dir = os.getcwd()
        grade_dir = '../labtainer-instructor'
        os.chdir(grade_dir)
        subprocess.call('gradelab {}'.format(labname), shell=True)
        os.chdir(current_dir)
    subprocess.call(
        'python3 ../labtainer-instructor/assess_bin/json_upload.py',
        shell=True)

    return 0
Beispiel #2
0
def main():
    parser = argparse.ArgumentParser(description='Stop a Labtainers lab.  If no arguments are provided, then all labs are stopped.')
    parser.add_argument('lab', nargs='?', default='all')
    args = parser.parse_args()
    labutils.logger = LabtainerLogging.LabtainerLogging("labtainer.log", 'stop.py', "../../config/labtainer.config")
    labutils.logger.debug('stop.py %s' % args.lab)
    lablist = labutils.GetListRunningLab()
    current_lab = CurrentLab.CurrentLab()
    clone_count = current_lab.get('clone_count')        
    servers = current_lab.get('servers')        
    current_lab_name = current_lab.get('lab_name')        
    if len(lablist) == 0:
        print('No labs are running.')
        labutils.logger.debug('No labs are running.')
    else:
        if args.lab == 'all':
            for lab in lablist:
                lab_path = os.path.join(os.path.abspath('../../labs'), lab)
                if current_lab_name is not None and lab != current_lab_name:
                    labutils.StopLab(lab_path, False)
                    print('Stopped lab %s, but was not current lab %s.  Servers and clones may not have stopped' % (lab, current_lab_name))
                    labutils.logger.debug('Stopped lab %s, but was not current lab.  Servers and clones may not have stopped')
                else:
                    labutils.StopLab(lab_path, False, servers=servers, clone_count=clone_count)
                    current_lab.clear()
        else:
            if args.lab in lablist: 
                lab_path = os.path.join(os.path.abspath('../../labs'), args.lab)
                if args.lab == current_lab_name:
                    labutils.StopLab(lab_path, False, servers=servers, clone_count=clone_count)
                    current_lab.clear()
                else:
                    labutils.StopLab(lab_path, False)
                    print('Stopped lab %s, but was not current lab.  Servers and clones may not have stopped' % args.lab)
                    labutils.logger.debug('Stopped lab %s, but was not current lab.  Servers and clones may not have stopped' % args.lab)
            else:
                print('Lab %s is not runnning, however %s is running.' % (args.lab, lablist[0]))
                labutils.logger.debug('Lab %s is not runnning, however %s is running.' % (args.lab, lablist[0]))

    return 0
Beispiel #3
0
def main():
    parser = argparse.ArgumentParser(
        description=
        'Stop a Labtainers lab.  If no arguments are provided, then all labs are stopped.'
    )
    parser.add_argument('lab', nargs='?', default='all')
    args = parser.parse_args()

    labname = args.lab
    lablist = []
    if labname != 'all':
        labutils.logger = LabtainerLogging.LabtainerLogging(
            "labtainer.log", labname, "../../config/labtainer.config")
        lablist.append(labname)
    else:
        labutils.logger = LabtainerLogging.LabtainerLogging(
            "labtainer.log", labname, "../../config/labtainer.config")
        lablist = labutils.GetListRunningLab()

    for labname in lablist:
        labutils.logger.INFO("Begin logging stop.py for %s lab" % labname)
        # Pass 'False' to ignore_stop_error (i.e., do not ignore error)
        lab_path = os.path.join(os.path.abspath('../../labs'), labname)
        has_running_containers, running_containers_list = labutils.GetRunningContainersList(
        )
        if has_running_containers:
            has_lab_role, labnamelist = labutils.GetRunningLabNames(
                running_containers_list, "student")
            if has_lab_role:
                if labname not in labnamelist:
                    labutils.logger.ERROR(
                        "No lab named %s in currently running labs!" % labname)
                    sys.exit(1)
            else:
                labutils.logger.ERROR("No running labs in student's role")
                sys.exit(1)
        else:
            labutils.logger.ERROR("No running labs at all")
            sys.exit(1)
        current_lab = CurrentLab.CurrentLab()
        clone_count = current_lab.get('clone_count')
        servers = current_lab.get('servers')
        labutils.StopLab(lab_path,
                         "student",
                         False,
                         servers=servers,
                         clone_count=clone_count)
        current_lab.clear()

    return 0
Beispiel #4
0
def main():
    if len(sys.argv) > 2:
        sys.stderr.write("Usage: stop.py [<labname>]\n")
        sys.exit(1)

    lablist = []
    if len(sys.argv) == 2:
        labname = sys.argv[1]
        labutils.logger = LabtainerLogging.LabtainerLogging(
            "labtainer.log", labname, "../../config/labtainer.config")
        lablist.append(labname)
    else:
        labname = "all"
        # labutils.logger need to be set before calling GetListRunningLab()
        labutils.logger = LabtainerLogging.LabtainerLogging(
            "labtainer.log", labname, "../../config/labtainer.config")
        lablist = labutils.GetListRunningLab()

    for labname in lablist:
        labutils.logger.info("Begin logging stop.py for %s lab" % labname)
        labutils.logger.debug("Instructor CWD = (%s), Student CWD = (%s)" %
                              (instructor_cwd, student_cwd))
        # Pass 'False' to ignore_stop_error (i.e., do not ignore error)
        lab_path = os.path.join(os.path.abspath('../../labs'), labname)
        has_running_containers, running_containers_list = labutils.GetRunningContainersList(
        )
        if has_running_containers:
            has_lab_role, labnamelist = labutils.GetRunningLabNames(
                running_containers_list, "instructor")
            if has_lab_role:
                if labname not in labnamelist:
                    labutils.logger.error(
                        "No lab named %s in currently running labs!" % labname)
                    sys.exit(1)
            else:
                labutils.logger.error("No running labs in instructor's role")
                sys.exit(1)
        else:
            labutils.logger.error("No running labs at all")
            sys.exit(1)
        labutils.StopLab(lab_path, "instructor", False)

    return 0
Beispiel #5
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)