Skip to content

Implement CLI hooks in netlab up/down commands#3355

Open
ipspace wants to merge 2 commits intodevfrom
cli-hooks
Open

Implement CLI hooks in netlab up/down commands#3355
ipspace wants to merge 2 commits intodevfrom
cli-hooks

Conversation

@ipspace
Copy link
Copy Markdown
Owner

@ipspace ipspace commented May 7, 2026

The CLI hooks allow the lab topology to define extra commands that are executed at various points of the 'netlab up' or 'netlab down' processing. The platform integration test has an example of the netlab up hooks.

Implements #3338

The CLI hooks allow the lab topology to define extra commands that are
executed at various points of the 'netlab up' or 'netlab down'
processing. The platform integration test has an example of the
netlab up hooks.

Implements #3338
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds support for user-defined CLI hooks that can run extra commands at specific lifecycle points during netlab up and netlab down, configured via defaults.netlab.<command>.<hook> in the topology/defaults. It addresses the workflow requested in #3338 (running an external licensing/apply step as part of up).

Changes:

  • Added run_cli_hooks() helper to execute configured hook commands and abort on failure.
  • Wired hook execution into multiple stages of netlab up (probes, lab start, initial config, tool start) and netlab down (lab stop, cleanup).
  • Added a platform integration test topology demonstrating netlab up hooks.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
netsim/cli/external_commands.py Introduces run_cli_hooks() for running hook commands from defaults.
netsim/cli/up.py Executes configured up hooks at key phases (pre-probe/start/config/tools and post-start/config/tools).
netsim/cli/down.py Executes configured down hooks around lab stop and cleanup.
tests/platform-integration/hooks/01-up-hooks.yml Adds an integration example/test validating pre/post start hooks create artifacts.

Comment on lines +195 to +203
def run_cli_hooks(settings: Box, cli_command: str, hook: str) -> None:
cmd = settings.get(f'netlab.{cli_command}.{hook}',None)
if not cmd:
return
if log.VERBOSE:
log.info(f'Running {hook} CLI hook',module=cli_command,more_data=[cmd])
if not run_command(cmd):
log.fatal(f'CLI hook {hook} returned an error, aborting...',cli_command)

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@a-v-popov -- What do you think? I'm OK with having str/list option, or saying "it's a single command, use the files plugin to create a bash script if needed".

In any case, the final value passed to run_command has to be a str/list to allow passing "weird" arguments (for example, embedded spaces) into run_command.

Comment thread tests/platform-integration/hooks/01-up-hooks.yml Outdated
@ipspace
Copy link
Copy Markdown
Owner Author

ipspace commented May 7, 2026

@a-v-popov -- This should give you the hooks needed to run the Fortinet licensing script. If you need more hooks, just let me know.

I'll add documentation once you're OK with the concept.

@ipspace ipspace linked an issue May 7, 2026 that may be closed by this pull request
ipspace added a commit that referenced this pull request May 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FortiOS license hook

2 participants