-
Notifications
You must be signed in to change notification settings - Fork 46
Introducing Broker Scenarios #446
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Features: - Introduce the new 'scenarios' feature, enabling the definition and execution of complex workflows as chained Broker actions within YAML files. - Add a new `broker scenarios` CLI group to manage these workflows: - `list`: Displays all available scenario files found in the scenarios directory. - `execute <scenario>`: Runs a specified scenario, supporting command-line variable and configuration overrides, and background execution. - `info <scenario>`: Provides a summary of a scenario's configuration, variables, and steps. - `validate <scenario>`: Checks a scenario file against its defined JSON schema for structural correctness. - Scenario functionality includes: - Jinja2 templating for dynamic values within scenario definitions. - Conditional step execution using `when` clauses. - Iterative step execution via `loop` constructs over iterables or inventory filters. - Capture of step outputs into new scenario variables for subsequent steps. - Configurable error handling with `on_error` blocks and `exit_on_error` flags. - Management of a scenario-specific inventory for `checkout` and `checkin` actions. - Support for nesting and running other scenarios as steps. - Implement `ScenarioRunner` as the core class for loading, validating, and executing scenario definitions. - Add `ScenarioError` to handle exceptions specific to scenario execution. Configuration: - Introduce `broker/scenario_schema.json` to formally define and validate the structure of scenario YAML files. - Update `pyproject.toml` to include `paramiko` as a recognized SSH session and interactive shell entry point.
Features:
- Add `output` action for scenario steps to write content to stdout, stderr, or files, with automatic format detection based on file extension.
- Introduce `provider_info` action to query configured providers for available resources (e.g., Ansible Tower workflows, OpenStack images) and retrieve structured data for programmatic use.
- Significantly enhance scenario loop capabilities:
- Allow the `iterable` argument to be a Jinja2 expression, variable, or inventory filter (`@inv`, `@scenario_inv`).
- Support tuple unpacking for `iter_var` (e.g., `key, value` for iterating over dict items).
- Implement `capture.key` to dynamically set keys for loop output within the step memory.
- Evaluate `when` conditions on a per-iteration basis within loops.
- Allow `on_error` in scenario steps to be either a list of recovery steps or the string `"continue"` to proceed despite failures.
- Export the new `save_file` helper for general use.
- Add the `scenarios` command to the interactive Broker shell.
- Provider `provider_help` methods (e.g., AnsibleTower, Beaker, Container, Foreman, OpenStack) now return structured data in addition to printing it, enabling programmatic use within scenarios.
Fixes:
- Correct the `helpers.merge_dicts` behavior to ensure values from the second dictionary argument consistently take precedence over the first.
- Update all `merge_dicts` call sites (e.g., Broker initialization, inventory updates) to correctly reflect the desired precedence, ensuring user-provided arguments or new data override defaults or existing data.
- Ensure scenario configuration values passed via CLI are deep-merged into existing settings rather than overwriting top-level dictionaries.
- Resolve a definition order issue for `broker_shell` commands in `commands.py` by moving shell definition.
Refactoring:
- Improve scenario host resolution to support filtering against both the main Broker inventory (`@inv`) and the scenario-specific inventory (`@scenario_inv`), including proper host reconstruction.
- Refine `_action_ssh`, `_action_scp`, and `_action_sftp` to return a single `Result` object for single-host operations and a dictionary mapping hostnames to `Result` objects for multiple hosts, simplifying templating.
- Simplify `list_scenarios` implementation for clarity.
- Add `_clear_scenario_inventory` to ensure scenarios start with a fresh inventory, preventing carry-over from previous runs.
Documentation:
- Update `scenario_schema.json` to reflect new actions, enhanced loop features, and flexible `on_error` handling.
- Clarify the `merge_dicts` docstring regarding value precedence.
- Add a `.gitignore` file to the `scenarios` directory to exclude non-versioned files.
…mentation Features: - Implement custom scenario-specific logging, allowing users to configure a dedicated log file for each scenario with flexible path resolution rules. - Improve Jinja2 templating to preserve Python types (e.g., int, bool, list, dict) for simple variable references, preventing unnecessary string conversions. - Introduce a comprehensive JSON schema (`scenario_schema.json`) for validating scenario files, formalizing the structure of steps, actions, arguments, loops, conditionals, and error handling. This enables a richer set of scenario capabilities. Fixes: - Improve container hostname resolution in `container_info`, adding robust fallbacks to container name or short ID if the `Config` or `Hostname` attributes are missing. Refactoring: - Adjust the log level for scenario initialization messages from 'info' to 'debug' for cleaner default output. Documentation: - Add a new, in-depth `scenarios_tutorial.md` covering all aspects of scenario creation and execution, including available actions, configuration options, templating, loops, error handling, and examples. Tests: - Introduce new functional tests for scenarios, including a comprehensive test for the Container provider, and specific tests for deploying/checking in containers and querying provider details. - Add `test_scenarios.py` to integrate and run these new functional scenario tests.
d0ca075 to
4dc5047
Compare
There was a problem hiding this 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 pull request introduces a comprehensive "Broker Scenarios" feature that enables users to define and execute complex, chained workflows as YAML files. The feature provides powerful automation capabilities for infrastructure and cloud operations by orchestrating Broker actions in structured, dynamic, and testable workflows.
Key Changes
- New Scenarios Module: Complete implementation of scenario execution engine with support for templating, loops, conditionals, variable capture, and error handling
- CLI Integration: New
broker scenarioscommand group with subcommands for listing, executing, validating, and inspecting scenarios - Schema Validation: JSON schema for validating scenario files with comprehensive coverage of all actions and configuration options
Reviewed changes
Copilot reviewed 30 out of 30 changed files in this pull request and generated 19 comments.
Show a summary per file
| File | Description |
|---|---|
| broker/scenarios.py | Core scenarios module with ScenarioRunner class and action handlers (1283 lines) |
| broker/scenario_schema.json | JSON schema defining valid scenario structure and validation rules |
| broker/commands.py | CLI command integration with scenarios subgroup and helper commands |
| broker/exceptions.py | New ScenarioError exception with contextual error reporting |
| broker/helpers/dict_utils.py | Fixed merge_dicts to ensure dict2 values take precedence |
| broker/helpers/file_utils.py | Added save_file helper with format detection and fixed UUID slicing bug |
| broker/settings.py | Enhanced config merging to support nested dictionary updates |
| broker/providers/*.py | Updated provider_help methods to return structured data for programmatic use |
| tests/test_scenarios.py | Comprehensive unit tests for scenarios utilities and ScenarioRunner |
| tests/functional/test_scenarios.py | Functional tests for end-to-end scenario execution |
| tests/data/scenarios/*.yaml | Test scenario files covering various features |
| tests/functional/scenarios/*.yaml | Real-world scenario examples for functional testing |
| scenarios/scenarios_tutorial.md | Extensive tutorial with examples and best practices (1068 lines) |
| pyproject.toml | Added jinja2 and jsonschema dependencies |
| tox.toml | Added test_scenarios.py to test suite |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
4dc5047 to
8886fe9
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 30 out of 30 changed files in this pull request and generated 14 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
8886fe9 to
5e203cc
Compare
Tests: - Introduce `test_scenarios.py` with extensive unit tests for the `broker.scenarios` module. - Cover scenario loading, schema validation, Jinja2 templating, expression evaluation, and conditional logic. - Verify support for loop iteration, output capture, and argument mapping within scenario steps. - Add specific sample scenario YAML files (`capture_scenario.yaml`, `conditions_scenario.yaml`, `loop_scenario.yaml`, `invalid_schema.yaml`, `valid_scenario.yaml`) to exercise various scenario features and validation rules. - Test `StepMemory` functionality and `ScenarioRunner` initialization, configuration, and built-in actions (`output`, `exit`). Configuration: - Add `jinja2` and `jsonschema` as core dependencies to `pyproject.toml` to enable scenario templating and schema validation. - Update `tox.toml` to include the new `test_scenarios.py` in the test suite.
5e203cc to
6101317
Compare
|
I've removed the scenarios tutorial from the codebase and put it in the wiki: https://github.com/SatelliteQE/broker/wiki/Scenarios-Tutorial |
This pull request introduces the brand new Broker scenarios, providing a powerful way to define and execute complex, chained workflows (scenarios) as YAML files, and follows up with significant feature additions, fixes, and comprehensive documentation.
This feature allows users to automate sophisticated infrastructure and cloud operations by leveraging all existing Broker actions within a structured, dynamic, and testable workflow.
Key Features Introduced and Enhanced
The following features were implemented across the three commits:
1. Core Scenario Workflow Management
broker scenariosCLI Group:list: Display available scenarios.execute <scenario>: Run a specified scenario with support for overrides and background execution.info <scenario>: Get a summary of a scenario's configuration and steps.validate <scenario>: Structural validation against the formal schema.whenclauses.2. Advanced Workflow Control and Data Handling
iterableto be a Jinja2 expression, variable, or inventory filter (@inv,@scenario_inv).iter_var(e.g.,key, valuefor dict iteration).capture.keyfor dynamic variable key assignment within loops.whenconditions on a per-iteration basis.on_errorcan now be a list of recovery steps or the string"continue"to ignore the failure and proceed.provider_infoaction to programmatically query configured providers for structured resource data (e.g., available images, workflows) for use within the scenario.provider_helpmethods now return structured data in addition to printing it.outputaction to write content tostdout,stderr, or files, with automatic format detection.int,bool,list,dict) for simple variable references, preventing unwanted string conversions.3. Configuration and Observability
scenario_schema.jsonfor validating scenario files, formalizing the structure of all steps, actions, arguments, and control flow elements.Key Fixes and Refactoring
merge_dictsFix: Correctedhelpers.merge_dictsto ensure values from the second dictionary argument consistently take precedence, and updated all call sites to reflect the desired precedence behavior (e.g., ensuring CLI arguments correctly override defaults)._clear_scenario_inventoryto ensure each scenario starts with a fresh inventory._action_ssh,_action_scp, and_action_sftpto consistently return a singleResultobject for single-host operations and a dictionary for multi-host operations, simplifying subsequent templating.Documentation and Testing
scenarios_tutorial.mdcovering all aspects of scenario creation, execution, templating, loops, error handling, and examples.test_scenarios.py.