Skip to content

Commit 59f7864

Browse files
committed
Add support for passing node ID to start and stop.
1 parent ee2e7db commit 59f7864

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

virl/cli/start/commands.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
import click
2+
from subprocess import call
23
from virl2_client.exceptions import NodeNotFound
34

45
from virl.api import VIRLServer
5-
from virl.helpers import (get_cml_client, get_current_lab,
6-
safe_join_existing_lab)
6+
from virl.helpers import get_cml_client, get_current_lab, safe_join_existing_lab, get_command
77

88

99
@click.command()
10-
@click.argument("node", nargs=1)
10+
@click.argument("node", required=False)
11+
@click.option("--id", required=False, help="An existing node ID to start (the node name argument is ignored)")
1112
def start(node):
1213
"""
1314
start a node
1415
"""
16+
if not node and not id:
17+
exit(call([get_command(), "start", "--help"]))
18+
1519
server = VIRLServer()
1620
client = get_cml_client(server)
1721

@@ -20,15 +24,18 @@ def start(node):
2024
lab = safe_join_existing_lab(current_lab, client)
2125
if lab:
2226
try:
23-
node_obj = lab.get_node_by_label(node)
27+
if id:
28+
node_obj = lab.get_node_by_id(id)
29+
else:
30+
node_obj = lab.get_node_by_label(node)
2431

2532
if not node_obj.is_active():
2633
node_obj.start(wait=True)
2734
click.secho("Started node {}".format(node_obj.label))
2835
else:
2936
click.secho("Node {} is already active".format(node_obj.label), fg="yellow")
3037
except NodeNotFound:
31-
click.secho("Node {} was not found in lab {}".format(node, current_lab), fg="red")
38+
click.secho("Node {} was not found in lab {}".format(id if id else node, current_lab), fg="red")
3239
exit(1)
3340
else:
3441
click.secho("Unable to find lab {}".format(current_lab), fg="red")

virl/cli/stop/commands.py

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
import click
2+
from subprocess import call
3+
24
from virl2_client.exceptions import NodeNotFound
35

46
from virl.api import VIRLServer
5-
from virl.helpers import (get_cml_client, get_current_lab,
6-
safe_join_existing_lab)
7+
from virl.helpers import get_cml_client, get_current_lab, safe_join_existing_lab, get_command
78

89

910
@click.command()
10-
@click.argument("node", nargs=1)
11+
@click.argument("node", required=False)
12+
@click.option("--id", required=False, help="An existing node ID to stop (the node name argument is ignored)")
1113
def stop(node):
1214
"""
1315
stop a node
1416
"""
17+
if not node and not id:
18+
exit(call([get_command(), "stop", "--help"]))
19+
1520
server = VIRLServer()
1621
client = get_cml_client(server)
1722

@@ -20,15 +25,18 @@ def stop(node):
2025
lab = safe_join_existing_lab(current_lab, client)
2126
if lab:
2227
try:
23-
node_obj = lab.get_node_by_label(node)
28+
if id:
29+
node_obj = lab.get_node_by_id(id)
30+
else:
31+
node_obj = lab.get_node_by_label(node)
2432

2533
if node_obj.is_active():
2634
node_obj.stop(wait=True)
2735
click.secho("Stopped node {}".format(node_obj.label))
2836
else:
2937
click.secho("Node {} is already stopped".format(node_obj.label), fg="yellow")
3038
except NodeNotFound:
31-
click.secho("Node {} was not found in lab {}".format(node, current_lab), fg="red")
39+
click.secho("Node {} was not found in lab {}".format(id if id else node, current_lab), fg="red")
3240
exit(1)
3341
else:
3442
click.secho("Unable to find lab {}".format(current_lab), fg="red")

0 commit comments

Comments
 (0)