Skip to content

Commit

Permalink
Add '--instance' option to 'netlab down' command
Browse files Browse the repository at this point in the history
This change allows you to use 'netlab down' to shut down an instance
running in another directory. It's useful primarily when you want to
start a lab but 'netlab up' complains that the 'default' instance is
already running.
  • Loading branch information
ipspace committed Sep 13, 2024
1 parent 30843ac commit 77aefb8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
9 changes: 6 additions & 3 deletions docs/netlab/down.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

**netlab down** destroys a virtual lab created with **[netlab up](up.md)** command.

This command uses the lab topology or the snapshot file created by **netlab up** or **[netlab create](create.md)** to find the virtualization provider, and executes provider-specific CLI commands to destroy the virtual lab.
This command uses the lab topology or the snapshot file created by **netlab up** or **[netlab create](create.md)** to find the virtualization provider and executes provider-specific CLI commands to destroy the virtual lab.

## Usage

```
usage: netlab down [-h] [-v] [--cleanup] [--dry-run] [--force] [--snapshot [SNAPSHOT]]
usage: netlab down [-h] [-v] [--cleanup] [--dry-run] [-i INSTANCE] [--force] [--snapshot [SNAPSHOT]]
Destroy the virtual lab
Expand All @@ -17,18 +17,21 @@ options:
-v, --verbose Verbose logging (where applicable)
--cleanup Remove all configuration files created by netlab create
--dry-run Print the commands that would be executed, but do not execute them
-i INSTANCE, --instance INSTANCE
Specify lab instance to shut down
--force Force shutdown or cleanup (use at your own risk)
```

Notes:

* **netlab down** needs transformed topology data to find the virtualization provider and link (bridge) names.
* **netlab down** reads the transformed topology from `netlab.snapshot.yml` file created by **netlab up** or **netlab create**. You can specify a different snapshot file name, but you really should not.
* With the `--instance` flag, you can shut down a lab instance running in a different directory. Use the `netlab status --all` command to display all running instances.
* Use the `--cleanup` flag to delete all Ansible-, Vagrant- or containerlab-related configuration files.
* Use the `--force` flag with the `--cleanup` flag if you want to clean up the directory even when the virtualization provider fails during the shutdown process.

## Conflict Resolution

**netlab down** command checks the _netlab_ status file (default: `~/.netlab/status.yml`) to verify that the current lab instance (default: `default`) is not running in another directory. You can decide to proceed if you want to remove _netlab_ artifacts from the current directory, but the shutdown/cleanup process might impact the lab instance running in another directory.

After a successful completion, **netlab down** command removes the `netlab.lock` file from the current directory, and all information about the lab instance from the _netlab_ status file.
After a successful completion, the **netlab down** command removes the `netlab.lock` file from the current directory and all information about the lab instance from the _netlab_ status file.
29 changes: 28 additions & 1 deletion netsim/cli/down.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from . import external_commands, set_dry_run, is_dry_run
from . import lab_status_change,fs_cleanup,load_snapshot,parser_add_snapshot
from .. import providers
from ..utils import status,strings,log
from ..utils import status,strings,log,read as _read
from .up import provider_probes
#
# CLI parser for 'netlab down' command
Expand All @@ -41,6 +41,11 @@ def down_parse(args: typing.List[str]) -> argparse.Namespace:
dest='dry_run',
action='store_true',
help='Print the commands that would be executed, but do not execute them')
parser.add_argument(
'-i','--instance',
dest='instance',
action='store',
help='Specify lab instance to shut down')
parser.add_argument(
'--force',
dest='force',
Expand Down Expand Up @@ -166,11 +171,33 @@ def stop_all(topology: Box, args: argparse.Namespace) -> None:
if not args.force:
sys.exit(1)

"""
Find a lab instance and change directory so the rest of the shutdown
process works from that directory
"""
def change_lab_instance(instance: str) -> None:
topology = _read.system_defaults()
lab_states = status.read_status(topology)
if not instance in lab_states:
log.fatal(f'Unknown instance {instance}, use "netlab status --all" to display running instances')

target_dir = lab_states[instance].dir
try:
os.chdir(target_dir)
except Exception as ex:
log.fatal(f'Cannot change directory to {target_dir}: {str(ex)}')

log.status_green('CHANGED','')
print(f'Current directory changed to {target_dir}, continuing the shutdown process')

def run(cli_args: typing.List[str]) -> None:
args = down_parse(cli_args)
set_dry_run(args)
if args.instance:
change_lab_instance(args.instance)

topology = load_snapshot(args)
log.status_success()
print(f"Read transformed lab topology from snapshot file {args.snapshot}")

mismatch = lab_dir_mismatch(topology,args)
Expand Down
8 changes: 6 additions & 2 deletions netsim/utils/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,11 +303,15 @@ def repeat_warnings(cmd: str) -> None:
"""
Print colored status headers
"""
def status_green(stat: str, txt: str) -> None:
stat = f'[{stat}]'
strings.print_colored_text(f'{stat:10s}','green',txt)

def status_created() -> None:
strings.print_colored_text('[CREATED] ','green','Created ')
status_green('CREATED','Created')

def status_success() -> None:
strings.print_colored_text('[SUCCESS] ','green','OK: ')
status_green('SUCCESS','OK: ')

def section_header(label: str, text: str, color: str = 'green') -> None:
if not strings.rich_color:
Expand Down

0 comments on commit 77aefb8

Please sign in to comment.