def check_lab_cache_server(lab_id, client): """ check if a lab exists in either the cache or the server. if on server and not in cache, cache the lab. """ ret = None if not check_lab_cache(lab_id): lab_obj = safe_join_existing_lab(lab_id, client) if lab_obj: cache_lab(lab_obj) ret = lab_id else: ret = lab_id return ret
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)
def extract(update_cache, **kwargs): """ extract configurations from all nodes in a lab """ server = VIRLServer() client = get_cml_client(server) current_lab = get_current_lab() if current_lab: lab = safe_join_existing_lab(current_lab, client) if lab: extract_configurations(lab) if update_cache: cache_lab(lab, force=True) else: click.secho("Failed to find running lab {}".format(current_lab), fg="red") exit(1) else: click.secho("Current lab is not set", fg="red") exit(1)
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)