Configure containers using 'clab.startup-config' parameter#3084
Configure containers using 'clab.startup-config' parameter#3084
Conversation
This commit implements 'startup' netlab_config_mode: * The node_files/x/startup.partial.config file is generated from the configuration files created by the 'config' output module * The clab.startup-config attribute is set to the name of the startup config file This configuration mode is an experimental feature and might not work on all devices. It's enabled by setting device clab.group_vars or node 'netlab_config_mode' parameter to 'startup' Other changes: * The number of modules deployed using startup config is collected in node._deploy.startup list * The two node._deploy lists (success and startup) are used to generate the internal deployment statistics.
|
@DanPartelly -- You asked for it ;) |
|
This PR has been tested with IOL and works like a charm. I have no idea how well it would work with other devices; that would have to be thoroughly tested. In any case, this mode is a bit dangerous as I don't know how (and if) we'd get the config error messages, so it will never be enabled by default. Documentation coming once @DanPartelly likes the feature ;)) |
There was a problem hiding this comment.
Pull request overview
This PR implements experimental 'startup' netlab_config_mode support for containerlab, allowing configuration to be loaded directly at container startup rather than through deployment scripts.
Changes:
- Adds functions to generate and configure startup configuration files from 'startup' mode config templates
- Tracks deployment statistics separately for startup-configured and script-deployed modules
- Updates reporting to display both startup and script deployment results
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| netsim/providers/clab.py | Implements startup config generation: filtering templates by mode, setting startup-config path, generating consolidated config file, and tracking startup deployments |
| netsim/cli/initial/deploy.py | Updates deployment statistics reporting to show both script-deployed and startup-configured modules separately |
| try: | ||
| with pathlib.Path(startup_path).open("w") as startup_cfg: |
There was a problem hiding this comment.
The directory containing the startup config file may not exist when opening the file for writing. Consider using pathlib.Path(startup_path).parent.mkdir(parents=True, exist_ok=True) before opening the file, or handle FileNotFoundError specifically in the exception handler.
| try: | |
| with pathlib.Path(startup_path).open("w") as startup_cfg: | |
| startup_file = pathlib.Path(startup_path) | |
| startup_file.parent.mkdir(parents=True, exist_ok=True) | |
| try: | |
| with startup_file.open("w") as startup_cfg: |
|
|
||
| ''' | ||
| Generate node startup configuration from configuration snippets with 'startup' mode | ||
| in node_files/node/startup-config |
There was a problem hiding this comment.
The comment says the file is "startup-config" but the actual filename is "startup.partial.config" as set in line 293.
| in node_files/node/startup-config | |
| in node_files/node/startup.partial.config |
| if not log.QUIET: | ||
| log.status_created() | ||
| print(f"startup configuration for {n.name}",flush=True) | ||
| n.clab['startup-config'] = startup_path |
There was a problem hiding this comment.
This line is redundant. The value of startup_path comes from n.clab['startup-config'] at line 320, so reassigning it back to the same location has no effect.
| n.clab['startup-config'] = startup_path |
| startup_cfg.write("\n") | ||
| except Exception as ex: | ||
| log.error( | ||
| 'Cannot open/write startup configuration file {startup_path}', |
There was a problem hiding this comment.
The error message is missing an f-string prefix. The variable startup_path will not be interpolated into the string and will appear literally as {startup_path} in the error message.
| 'Cannot open/write startup configuration file {startup_path}', | |
| f'Cannot open/write startup configuration file {startup_path}', |
| def get_templates_with_mode(n: Box, mode: typing.Optional[str]) -> list: | ||
| return [ item for item in n.get('clab.config_templates',[]) # Collect config template items | ||
| if 'mode' in item and # ... that have mode set | ||
| item.mode == mode or mode is None] # ... and match the requested mode (None == all modes) |
There was a problem hiding this comment.
Operator precedence issue in the conditional expression. The current expression evaluates as ('mode' in item and item.mode == mode) or mode is None, which means items without a mode will be included when mode is None. This should use parentheses to express the intended logic: 'mode' in item and (item.mode == mode or mode is None).
| item.mode == mode or mode is None] # ... and match the requested mode (None == all modes) | |
| (item.mode == mode or mode is None)] # ... and match the requested mode (None == all modes) |
This commit implements 'startup' netlab_config_mode:
This configuration mode is an experimental feature and might not work on all devices. It's enabled by setting device clab.group_vars or node 'netlab_config_mode' parameter to 'startup'
Other changes: