def stop1(node): """ stop a node """ if len(node) == 2: # we received env and node name env = node[0] running = helpers.check_sim_running(env) node = node[1] elif len(node) == 1: # assume default env env = "default" running = helpers.check_sim_running(env) node = node[0] else: exit(call([get_command(), "stop", "--help"])) if running: sim_name = running server = VIRLServer() resp = server.stop_node(sim_name, node) if resp.ok: click.secho("Stopped node {}".format(node)) else: click.secho("Error Stopping Node {}: {}".format(node, resp), fg="red")
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)
def telnet1(node): """ telnet to a node """ if len(node) == 2: # we received env and node name env = node[0] running = helpers.check_sim_running(env) node = node[1] elif len(node) == 1: # assume default env env = "default" running = helpers.check_sim_running(env) node = node[0] else: exit(call([get_command(), "telnet", "--help"])) if running: sim_name = running server = VIRLServer() details = server.get_sim_roster(sim_name) if node: try: node_dict = get_node_from_roster(node, details) node_name = node_dict.get("NodeName") ip = node_dict["managementIP"] proxy = node_dict.get("managementProxy") # use user specified telnet command if "VIRL_TELNET_COMMAND" in server.config: cmd = server.config["VIRL_TELNET_COMMAND"] cmd = cmd.format(host=ip) print("Calling user specified command: {}".format(cmd)) exit(call(cmd.split())) if proxy == "lxc": lxc = get_mgmt_lxc_ip(details) click.secho("Attemping telnet connection" " to {} at {} via ssh {}".format( node_name, ip, lxc)) cmd = 'ssh -t {}@{} "telnet {}"' cmd = cmd.format(server.user, lxc, ip) exit(call(cmd, shell=True)) else: # handle the "flat" networking case click.secho("Attemping telnet connection" " to {} at {}".format(node_name, ip)) exit(call(["telnet", ip])) except AttributeError: click.secho("Could not find management info " "for {}:{}".format(env, node), fg="red") except KeyError: click.secho("Unknown node {}:{}".format(env, node), fg="red") else: return details.json()
def __call__(self, *args, **kwargs): try: return self.main(*args, **kwargs) except Exception as exc: click.secho("Exception raised while running your command", fg="red") if not virl.debug: click.secho( "Please re-run as '{} --debug ...' and collect the output before opening an issue" .format(get_command()), fg="red", ) else: click.secho("Please open an issue and provide this output:", fg="red") click.secho("%s" % exc, fg="red") if virl.debug: click.secho(traceback.format_exc(), fg="red") exit(1)
def console1(node, display, **kwargs): """ console for node """ server = VIRLServer() if len(node) == 2: # we received env and node name env = node[0] running = helpers.check_sim_running(env) node = node[1] elif display: # only displaying output env = "default" running = helpers.check_sim_running(env) node = None elif len(node) == 1: # assume default env env = "default" running = helpers.check_sim_running(env) node = node[0] else: # node was not specified, display usage exit(call([get_command(), "console", "--help"])) if running: sim_name = running resp = server.get_node_console(sim_name, node=node) if node: click.secho("Attempting to connect to console " "of {}".format(node)) try: ip, port = resp.json()[node].split(":") # use user specified telnet command if "VIRL_CONSOLE_COMMAND" in server.config: cmd = server.config["VIRL_CONSOLE_COMMAND"] cmd = cmd.format(host=ip, port=port) print("Calling user specified command: {}".format(cmd)) exit(call(cmd.split())) # someone still uses windows elif platform.system() == "Windows": with helpers.disable_file_system_redirection(): exit(call(["telnet", ip, port])) # why is shit so complicated? else: exit(call(["telnet", ip, port])) except AttributeError: click.secho("Could not find console info for " "{}:{}".format(env, node), fg="red") except KeyError: click.secho("Unknown node {}:{}".format(env, node), fg="red") else: # defaults to displaying table console_table1(resp.json())
def up1(repo=None, provision=False, **kwargs): """ start a virl simulation """ fname = kwargs["f"] env = kwargs["e"] wait_time = kwargs["wait_time"] if os.path.exists(fname): running = check_sim_running(env) if not running: click.secho("Creating {} environment from {}".format(env, fname)) with open(fname) as fh: data = fh.read() server = VIRLServer() # we can expose fairly aribtary substitutions here... # anything that may differ usually related to networking.... # <dirty hack> subs = { "{{ gateway }}": server.get_gateway_for_network("flat"), "{{ flat1_gateway }}": server.get_gateway_for_network("flat1"), "{{ dns_server }}": server.get_dns_server_for_network("flat"), } # also can change some VIRL/ANK defaults subs["rsa modulus 768"] = "rsa modulus 1024" for tag, value in subs.items(): if tag in data: if value: # split off the braces humanize = tag click.secho("Localizing {} with: {}".format( humanize, value)) data = data.replace(tag, value) # </dirty hack> dirpath = os.getcwd() foldername = os.path.basename(dirpath) sim_name = "{}_{}_{}".format(foldername, env, generate_sim_id()) resp = server.launch_simulation(sim_name, data) store_sim_info(resp.text, env=env) # 'topology-2lkx2' if provision: nodes = server.get_node_list(sim_name) msg = "Waiting {} minutes for nodes to come online...." msg = msg.format(wait_time) click.secho(msg) maxtime = time.time() + 60 * int(wait_time) with click.progressbar(nodes) as all_nodes: for node in all_nodes: if time.time() > maxtime: click.secho("") click.secho("Max time expired", fg="red") click.secho("All nodes may not be online", fg="red") break node_online = False while not node_online: if time.time() > maxtime: break time.sleep(20) node_online = server.check_node_reachable( sim_name, node) else: click.secho("Sim {} already running".format(running)) else: # try to pull from virlfiles if repo: call([get_command(), "pull", repo]) call([get_command(), "up"]) else: click.secho("Could not find topology.virl. Maybe try -f", fg="red")
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)