Skip to content

Commit

Permalink
Implement 'message' attribute that's displayed after successful 'netl…
Browse files Browse the repository at this point in the history
…ab up' or 'netlab initial'

Also, accept 'args.Namespace' or 'Box' whenever dealing with CLI arguments -- sometimes we
have to pass fake parsed arguments to common routines, and it's easier to pass an empty Box
  • Loading branch information
ipspace committed Sep 8, 2022
1 parent d470746 commit e0f8e9f
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 7 deletions.
2 changes: 1 addition & 1 deletion netsim/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env python3

__version__ = "1.3-post1"
__version__ = "1.3.1-dev2"
20 changes: 18 additions & 2 deletions netsim/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def fs_cleanup(filelist: typing.List[str], verbose: bool = False) -> None:

# Common topology loader (used by create and down)

def load_topology(args: argparse.Namespace) -> Box:
def load_topology(args: typing.Union[argparse.Namespace,Box]) -> Box:
common.set_logging_flags(args)
topology = read_topology.load(args.topology.name,args.defaults,"package:topology-defaults.yml")

Expand All @@ -77,7 +77,7 @@ def load_topology(args: argparse.Namespace) -> Box:

# Snapshot-or-topology loader (used by down)

def load_snapshot_or_topology(args: argparse.Namespace) -> typing.Optional[Box]:
def load_snapshot_or_topology(args: typing.Union[argparse.Namespace,Box]) -> typing.Optional[Box]:
common.set_logging_flags(args)
if args.device or args.provider or args.settings: # If we have -d, -p or -s flag
if not args.topology: # ... then the user wants to use the topology file
Expand All @@ -92,6 +92,22 @@ def load_snapshot_or_topology(args: argparse.Namespace) -> typing.Optional[Box]:
args.snapshot = args.snapshot or 'netlab.snapshot.yml'
return read_topology.read_yaml(filename=args.snapshot)

# get_message: get action-specific message from topology file
#

def get_message(topology: Box, action: str, default_message: bool = False) -> typing.Optional[str]:
if not 'message' in topology:
return None

if isinstance(topology.message,str): # We have a single message
return topology.message if default_message else None # If the action is OK with getting the default message return it

if not isinstance(topology.message,Box): # Otherwise we should be dealing with a dict
common.fatal('topology message should be a string or a dict')
return None

return topology.message.get(action,None) # Return action-specific message if it exists

#
# Main command dispatcher
#
Expand Down
8 changes: 7 additions & 1 deletion netsim/cli/initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
import os
import argparse

from . import common_parse_args
from . import common_parse_args,get_message,load_snapshot_or_topology
from . import ansible
from box import Box

#
# CLI parser for 'netlab initial' command
Expand Down Expand Up @@ -76,3 +77,8 @@ def run(cli_args: typing.List[str]) -> None:
print("\nInitial configurations have been created in the %s directory" % args.output)
else:
ansible.playbook('initial-config.ansible',rest)
topology = load_snapshot_or_topology(Box({},default_box=True,box_dots=True))
if topology:
message = get_message(topology,'initial',True)
if message:
print(f"\n\n{message}")
5 changes: 4 additions & 1 deletion netsim/cli/up.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from .. import common
from . import create
from . import external_commands
from . import common_parse_args, load_snapshot_or_topology
from . import common_parse_args, load_snapshot_or_topology, get_message
from .. import providers
from .. import read_topology

Expand Down Expand Up @@ -87,5 +87,8 @@ def run(cli_args: typing.List[str]) -> None:

if not args.no_config:
external_commands.deploy_configs(4,"netlab up",args.fast_config)
message = get_message(topology,'up',False)
if message:
print(f"\n\n{message}")
else:
print("\nInitial configuration skipped, run 'netlab initial' to configure the devices")
2 changes: 1 addition & 1 deletion netsim/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def print_verbose(t: typing.Any) -> None:
#
# Internal debugging flags (RAISE_ON_ERROR, WARNING) cannot be set with this function
#
def set_logging_flags(args: argparse.Namespace) -> None:
def set_logging_flags(args: typing.Union[argparse.Namespace,Box]) -> None:
global VERBOSE, LOGGING, DEBUG, QUIET, WARNING, RAISE_ON_ERROR

if 'verbose' in args and args.verbose:
Expand Down
2 changes: 1 addition & 1 deletion netsim/read_topology.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def load(fname: str , local_defaults: str, sys_defaults: str) -> Box:

return topology

def add_cli_args(topo: Box, args: argparse.Namespace) -> None:
def add_cli_args(topo: Box, args: typing.Union[argparse.Namespace,Box]) -> None:
if args.device:
topo.defaults.device = args.device

Expand Down

0 comments on commit e0f8e9f

Please sign in to comment.