Skip to content
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

Implement execution ID to workflow engine #5115

Draft
wants to merge 342 commits into
base: 4.9.0
Choose a base branch
from

Conversation

QU3B1M
Copy link
Member

@QU3B1M QU3B1M commented Mar 18, 2024

Description

This PR adds the possibility to have and use internal/magic variables in the workflow execution, it is something similar to what Ansible have (link). For now only one variable is configured, the execution_id this var will be used to easily identify each execution and its reports on the observability module.

The magic variables are also configured in each execution as environment vars, so any tool, module, script executed in a task can access these variables through the ENV vars.

The InfluxDB pytest plugin was also updated to take adventage of this new variable.

Usage + Test proof

Full test.py file to use for testing purposes
import argparse
import os


parser = argparse.ArgumentParser()
parser.add_argument('--execution_id', type=str)
parser.parse_args()


if exec_id := parser.parse_args().execution_id:
    # Get the value of the argument 'execution_id'
    print(f'The value of the Execution ID var as argument is: {exec_id}')
elif exec_id := os.getenv('execution_id'):
    # Get the value of the environment variable 'execution_id'
    print(f'The value of the Execution ID environment variable is: {exec_id}')

# Get the value of the workflow defined env variable 'TEST'
if test_env := os.getenv('TEST'):
    print(f'The value of the workflow defined env var is: {test_env}')

Basically there are two ways to take adventage of this magic variables:

  1. Actively calling this var in the task to inject it (as a tipical declared variable but without the need of declaration)
    workflow.yaml
    version: 0.1
    description: This workflow is used to test
    
    tasks:
      - task: "test-magic-vars"
        description: "Use a simple script to the new magic & env variables."
        do:
          this: process
          with:
            path: python3
            args:
              - /home/quebim/Wazuh/repos/wazuh-qa/deployability/test.py
              - execution_id: '{execution_id}' # Usage as injected var
    test.py
    parser = argparse.ArgumentParser()
    parser.add_argument('--execution_id', type=str, default=None)
    parser.parse_args()
    
    
    exec_id = parser.parse_args().execution_id
    print(f'The value of the Execution ID var as argument is: {exec_id}')
    
    Result:
    [2024-03-20 10:51:55] [INFO] [9669] [ThreadPoolExecutor-0_0] [workflow_engine]: [test-magic-vars] Starting task.
    [2024-03-20 10:51:55] [DEBUG] [9669] [ThreadPoolExecutor-0_0] [workflow_engine]: Running task "test-magic-vars" with arguments: ['/home/quebim/Wazuh/repos/wazuh-qa/deployability/test.py', '725e117f-f9ce-4e00-8dc4-8a7cf6348439']
    [2024-03-20 10:51:55] [DEBUG] [9669] [ThreadPoolExecutor-0_0] [workflow_engine]: Finished task "test-magic-vars" execution with result:
    The value of the Execution ID var as argument is: 725e117f-f9ce-4e00-8dc4-8a7cf6348439
  2. Consuming it as Environment variable
    workflow.yaml
    version: 0.1
    description: This workflow is used to test
    
    tasks:
      - task: "test-magic-vars"
        description: "Use a simple script to the new magic & env variables."
        do:
          this: process
          with:
            path: python3
            args:
              - /home/quebim/Wazuh/repos/wazuh-qa/deployability/test.py
    test.py
    exec_id = os.getenv('execution_id')
    # Get the value of the environment variable 'execution_id'
    print(f'The value of the Execution ID environment variable is: {exec_id}')
    Result:
    [2024-03-20 10:55:13] [INFO] [9887] [ThreadPoolExecutor-0_0] [workflow_engine]: [test-magic-vars] Starting task.
    [2024-03-20 10:55:13] [DEBUG] [9887] [ThreadPoolExecutor-0_0] [workflow_engine]: Running task "test-magic-vars" with arguments: ['/home/quebim/Wazuh/repos/wazuh-qa/deployability/test.py', '--execution_id=108fe6ba-a174-48fb-b6bf-4adda112a93a']
    [2024-03-20 10:55:13] [DEBUG] [9887] [ThreadPoolExecutor-0_0] [workflow_engine]: Finished task "test-magic-vars" execution with result:
    The value of the Execution ID var as argument is: 108fe6ba-a174-48fb-b6bf-4adda112a93a

Extra

A new env tag was implemented in the Task block to let the user define environment variables that can be used in the task execution.

  • Example:
    workflow.yaml
    version: 0.1
    description: This workflow is used to test
    
    tasks:
      - task: "test-magic-vars"
        description: "Use a simple script to the new magic & env variables."
        do:
          this: process
          with:
            path: python3
            args:
              - /home/quebim/Wazuh/repos/wazuh-qa/deployability/test.py
            env:
              TEST: 'Im a test'
    test.py
    test_env = os.getenv('TEST')
    print(f'The value of the workflow defined env var is: {test_env}')
    Result:
    24-03-20 10:59:13] [INFO] [10051] [ThreadPoolExecutor-0_0] [workflow_engine]: [test-magic-vars] Starting task.
    [2024-03-20 10:59:13] [DEBUG] [10051] [ThreadPoolExecutor-0_0] [workflow_engine]: Running task "test-magic-vars" with arguments: ['/home/quebim/Wazuh/repos/wazuh-qa/deployability/test.py']
    [2024-03-20 10:59:13] [DEBUG] [10051] [ThreadPoolExecutor-0_0] [workflow_engine]: Finished task "test-magic-vars" execution with result:
    The value of the workflow defined env var is: Im a test

fcaffieri and others added 30 commits November 27, 2023 16:13
…ugin

Enhancement/4736 dtt1 influxdb plugin
fcaffieri and others added 17 commits February 19, 2024 11:27
…remove-duplicated-examples

Remove duplicated examples
…-workflow

Merge workflow engine into main DTT1 branch
…odule-improvements-and-fixes

Merge provision module into main DTT1
…-fixes' into enhancement/4940-dtt1-provision-multiple-dependencies-inventories
…ultiple-dependencies-inventories

Enhancement/4940 dtt1 provision multiple dependencies inventories
…-allocation-module-change-the-location-of-the-inventory-and-track-files

Moved the inventory file and track file to instance dir
…ion-module-fix-box_version-param-casting-to-str

Cast box and box_version to str
@QU3B1M QU3B1M requested a review from fcaffieri March 18, 2024 19:20
@QU3B1M QU3B1M self-assigned this Mar 18, 2024
@QU3B1M QU3B1M linked an issue Mar 18, 2024 that may be closed by this pull request
8 tasks
pro-akim
pro-akim previously approved these changes Mar 20, 2024
Copy link
Member

@pro-akim pro-akim left a comment

Choose a reason for hiding this comment

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

Review Notes

LGTM

@rauldpm rauldpm changed the base branch from enhancement/4495-DTT1 to 4.9.0 April 11, 2024 17:45
@rauldpm rauldpm dismissed pro-akim’s stale review April 11, 2024 17:45

The base branch was changed.

@rauldpm rauldpm marked this pull request as draft April 11, 2024 17:45
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.

DTT2 - Workflow engine - Implement observability plugin execution ID
8 participants