Ejemplo n.º 1
0
def use(lab, id, lab_name):
    """
    use lab launched elsewhere
    """
    server = VIRLServer()
    client = get_cml_client(server)
    lab_id = None

    if not lab and not id and not lab_name:
        exit(call([get_command(), "use", "--help"]))

    if id:
        lab_id = check_lab_cache_server(id, client)

    # Prefer --lab-name over positional argument
    if lab_name:
        lab = lab_name

    if not id and lab:
        lab_obj = safe_join_existing_lab_by_title(lab, client)
        if lab_obj:
            # Make sure this lab is cached.
            lab_id = check_lab_cache_server(lab_obj.id, client)

    if lab_id:
        set_current_lab(lab_id)
    else:
        click.secho("Unable to find lab in the cache or on the server",
                    fg="red")
        exit(1)
Ejemplo n.º 2
0
def start_lab(lab, provision=False):
    click.secho("Starting lab {} (ID: {})".format(lab.title, lab.id))
    lab.wait_for_convergence = False
    lab.start()
    cache_lab(lab)
    set_current_lab(lab.id)
    if provision:
        # Technically we need to block until all nodes are "reachable".
        # In the CML 2+ case, this means BOOTED.
        click.secho("Waiting for all nodes to be online...")
        ready = False
        while not ready:
            for n in lab.nodes():
                if not n.is_booted():
                    ready = False
                    break
                ready = True
            time.sleep(1)
Ejemplo n.º 3
0
def up(repo=None, provision=False, start=True, **kwargs):
    """
    start a lab
    """
    def_fname = kwargs["f"]
    alt_fname = "topology.virl"
    fname = def_fname
    lid = kwargs["id"]
    lab_name = kwargs["lab_name"]
    lab = None
    clab = None

    server = VIRLServer()
    client = get_cml_client(server)

    current_lab = get_current_lab()
    if current_lab:
        clab = safe_join_existing_lab(current_lab, client)
        if not clab:
            click.secho(
                "Current lab is already set to {}, but that lab is not on server; clearing it."
                .format(current_lab),
                fg="yellow")
            clear_current_lab()

    if not clab or fname or lid or lab_name:
        if clab:
            click.secho(
                "WARNING: Current lab is set to {} (ID: {}); clearing it".
                format(clab.title, current_lab),
                fg="yellow")
            clear_current_lab()

        if not def_fname:
            def_fname = "topology.yaml"
            fname = def_fname

        if not os.path.isfile(def_fname) and os.path.isfile(alt_fname):
            fname = alt_fname

        if lid:
            lab = safe_join_existing_lab(lid, client)
            if not lab:
                # Check the cache
                existing = check_lab_cache(lid)
                if existing:
                    fname = existing

        if not lab and lab_name:
            lab = safe_join_existing_lab_by_title(lab_name, client)

        if not lab and os.path.isfile(fname):
            lname = get_lab_title(fname)
            click.secho("Importing lab {} from file {}".format(lname, fname))
            lab = client.import_lab_from_path(fname, title=lname)
        elif not lab:
            # try to pull from virlfiles
            if repo and os.path.basename(fname) == "topology.yaml":
                rc = call([get_command(), "pull", repo])
                if rc == 0:
                    exit(call([get_command(), "up"]))

        if lab:
            if lab.is_active():
                cache_lab(lab)
                set_current_lab(lab.id)
                click.secho(
                    "Lab is already running (ID: {}, Title: {})".format(
                        lab.id, lab.title))
            elif start:
                start_lab(lab, provision)

        else:
            click.secho("Could not find a lab to start.  Maybe try -f",
                        fg="red")
            exit(1)
    elif clab:
        click.secho("Lab {} (ID: {}) is already set as the current lab".format(
            clab.title, current_lab))
        if not clab.is_active() and start:
            start_lab(clab, provision)