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

Adding workflow as library function #4925

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions deployability/Jenkinsfiles/Launcher.groovy
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

String jenkins_reference = params.getOrDefault('JENKINS_REFERENCE', 'enhancement/4751-dtt1-iteration-2-poc')
String launcher_path = "launchers"
String task_flow_launcher = "provision.py"
String launcher_path = "modules/provision"
String task_flow_launcher = "main.py"
String workflow = "modules/workflow_engine/examples/dtt1-managers.yaml"
String schema = "modules/workflow_engine/schema.json"

Expand Down
6 changes: 2 additions & 4 deletions deployability/Jenkinsfiles/Provision.groovy
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@


String provision_path = "${WORKSPACE}/scripts/provision"
String provision_script = "provision.py"
String provision_path = "${WORKSPACE}/modules/provision"
String provision_script = "main.py"
String inventory = "inventory.yaml"
String jenkins_reference = params.getOrDefault('JENKINS_REFERENCE', 'enhancement/4665-dtt1-poc')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os
import sys

project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '../..'))
sys.path.append(project_root)

from modules.allocation import Allocator
Expand Down
5 changes: 3 additions & 2 deletions deployability/modules/generic/ansible.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class Inventory(BaseModel):
class Ansible:
def __init__(self, ansible_data: dict | Inventory, path: str | Path = None):
self.path = path
self.playbooks_path = Path(__file__).parents[2] / 'playbooks'
self.modules_path = Path(__file__).parents[1]
self.provision_playbook_path = self.modules_path / 'provision/playbooks'
self.ansible_data = Inventory(**dict(ansible_data))
self.inventory = self.generate_inventory()
self.logger = Logger(Path(__file__).stem).get_logger()
Expand All @@ -32,7 +33,7 @@ def render_playbooks(self, rendering_variables: dict) -> list[str]:
rendering_variables (dict): Extra variables to render the playbooks.
"""
tasks = []
path_to_render_playbooks = self.playbooks_path / rendering_variables.get("templates_path")
path_to_render_playbooks = self.provision_playbook_path / rendering_variables.get("templates_path")
template_loader = jinja2.FileSystemLoader(searchpath=path_to_render_playbooks)
template_env = jinja2.Environment(loader=template_loader)

Expand Down
8 changes: 4 additions & 4 deletions deployability/modules/provision/componentType.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Package(ComponentType):
"""
Class to define the type of package to be provisioned
"""
TEMPLATE_BASE_PATH = 'provision/wazuh'
TEMPLATE_BASE_PATH = 'wazuh'

def __init__(self, component_info, action):
super().__init__(component_info)
Expand All @@ -58,7 +58,7 @@ class AIO(ComponentType):
"""
Class to define the type of AIO to be provisioned
"""
TEMPLATE_BASE_PATH = 'provision/wazuh'
TEMPLATE_BASE_PATH = 'wazuh'

def __init__(self, component_info, action):
super().__init__(component_info)
Expand All @@ -73,7 +73,7 @@ class Generic(ComponentType):
"""
Class to define the type of generic component to be provisioned
"""
TEMPLATE_BASE_PATH = 'provision/generic'
TEMPLATE_BASE_PATH = 'generic'

def __init__(self, component_info, action):
super().__init__(component_info)
Expand All @@ -88,7 +88,7 @@ class Dependencies(ComponentType):
"""
Class to define the type of dependencies to be provisioned
"""
TEMPLATE_BASE_PATH = 'provision/deps'
TEMPLATE_BASE_PATH = 'deps'

def __init__(self, component_info, action):
super().__init__(component_info)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

# ---------------- Vars ------------------------

project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '../..'))
sys.path.append(project_root)

from modules.provision import Provision, models
Expand Down
42 changes: 42 additions & 0 deletions deployability/modules/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

# Copyright (C) 2015-2024, Wazuh Inc.
# Created by Wazuh, Inc. <[email protected]>.
# This program is free software; you can redistribute it and/or modify it under the terms of GPLv2
import json
from setuptools import setup, find_packages
import os

def get_files_from_directory(directory):
paths = []
for (path, directories, filenames) in os.walk(directory):
for filename in filenames:
paths.append(os.path.join('..', path, filename))
return paths

def get_version():
script_path = os.path.dirname(__file__)
rel_path = "../version.json"
abs_file_path = os.path.join(script_path, rel_path)
f = open(abs_file_path)
data = json.load(f)
version = data['version']
return version

package_data_list = get_files_from_directory("workflow_engine")
scripts_list = ['engine=workflow_engine.__main__:main']

setup(
name='workflow_engine',
version=get_version(),
description='Wazuh testing utilities to help programmers automate deployment tests',
url='https://github.com/wazuh',
author='Wazuh',
author_email='[email protected]',
license='GPLv2',
packages=['workflow_engine'],
package_dir={'workflow_engine': 'workflow_engine'},
package_data={'workflow_engine': package_data_list},
entry_points={'console_scripts': scripts_list},
include_package_data=True,
zip_safe=False
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import sys
import os

project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '../..'))
sys.path.append(project_root)

from modules.testing import Tester, InputPayload
Expand Down
11 changes: 5 additions & 6 deletions deployability/modules/testing/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
from .models import InputPayload, ExtraVars
from .utils import logger


class Tester:
_playbooks_dir = Path('tests')
_playbooks_dir = Path(__file__).parent / 'playbooks'
_setup_playbook = _playbooks_dir / 'setup.yml'
_cleanup_playbook = _playbooks_dir / 'cleanup.yml'
_test_template = _playbooks_dir / 'test.yml'
Expand Down Expand Up @@ -77,7 +76,7 @@ def _run_tests(cls, test_list: list[str], ansible: Ansible, extra_vars: ExtraVar
"""
for test in test_list:
rendering_var = {**extra_vars, 'test': test}
template = str(ansible.playbooks_path / cls._test_template)
template = str(cls._test_template)
playbook = ansible.render_playbook(template, rendering_var)
if not playbook:
logger.warning(f"Test {test} not found. Skipped.")
Expand All @@ -95,7 +94,7 @@ def _setup(cls, ansible: Ansible, remote_working_dir: str = '/tmp') -> None:
"""
extra_vars = {'local_path': str(Path(__file__).parent / 'tests'),
'working_dir': remote_working_dir}
playbook = str(ansible.playbooks_path / cls._setup_playbook)
playbook = str(cls._setup_playbook)
ansible.run_playbook(playbook, extra_vars)

@classmethod
Expand All @@ -108,5 +107,5 @@ def _cleanup(cls, ansible: Ansible, remote_working_dir: str = '/tmp') -> None:
remote_working_dir (str): The remote working directory.
"""
extra_vars = {'working_dir': remote_working_dir}
playbook = str(ansible.playbooks_path / cls._cleanup_playbook)
ansible.run_playbook(playbook, extra_vars)
playbook = str(cls._cleanup_playbook)
ansible.run_playbook(playbook, extra_vars)
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
import sys
import argparse

project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '../..'))
sys.path.append(project_root)

from modules.workflow_engine.workflow_processor import WorkflowProcessor
from modules.workflow_engine.models import InputPayload
from workflow_engine.workflow_processor import WorkflowProcessor
from workflow_engine.models import InputPayload


def parse_arguments() -> argparse.Namespace:
Expand Down
50 changes: 25 additions & 25 deletions deployability/modules/workflow_engine/examples/dtt1-agents-poc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ version: 0.1
description: This workflow is used to test agents deployment por DDT1 PoC
variables:
agents-os:
- linux-ubuntu-20.04-amd64
manager-os: linux-ubuntu-20.04-amd64
- linux-ubuntu-22.04-amd64
manager-os: linux-ubuntu-22.04-amd64
infra-provider: vagrant
working-dir: /tmp/dtt1-poc

Expand All @@ -19,7 +19,7 @@ tasks:
with:
path: python3
args:
- test.py
- testing/main.py
- inventory: "{working-dir}/agent-{agent}/inventory.yaml"
- dependencies:
- manager: "{working-dir}/manager-{manager-os}/inventory.yaml"
Expand All @@ -43,7 +43,7 @@ tasks:
with:
path: python3
args:
- test.py
- testing/main.py
- inventory: "{working-dir}/agent-{agent}/inventory.yaml"
- dependencies:
- manager: "{working-dir}/manager-{manager-os}/inventory.yaml"
Expand All @@ -66,7 +66,7 @@ tasks:
with:
path: python3
args:
- provision.py
- provision/main.py
- inventory-manager: "{working-dir}/manager-{manager-os}/inventory.yaml"
- install:
- component: wazuh-manager
Expand All @@ -82,21 +82,21 @@ tasks:
with:
path: python3
args:
- allocation.py
- allocation/main.py
- action: create
- provider: "{infra-provider}"
- size: large
- composite-name: "{manager-os}"
- inventory-output: "{working-dir}/manager-{manager-os}/inventory.yaml"
- track-output: "{working-dir}/manager-{manager-os}/track.yaml"
cleanup:
this: process
with:
path: python3
args:
- allocation.py
- action: delete
- track-output: "{working-dir}/manager-{manager-os}/track.yaml"
#cleanup:
# this: process
# with:
# path: python3
# args:
# - allocation/main.py
# - action: delete
# - track-output: "{working-dir}/manager-{manager-os}/track.yaml"

# Generic agent provision task
- task: "provision-install-{agent}"
Expand All @@ -106,7 +106,7 @@ tasks:
with:
path: python3
args:
- provision.py
- provision/main.py
- inventory-agent: "{working-dir}/agent-{agent}/inventory.yaml"
- inventory-manager: "{working-dir}/manager-{manager-os}/inventory.yaml"
- install:
Expand All @@ -128,7 +128,7 @@ tasks:
with:
path: python3
args:
- provision.py
- provision/main.py
- inventory-agent: "{working-dir}/agent-{agent}/inventory.yaml"
- inventory-manager: "{working-dir}/manager-{manager-os}/inventory.yaml"
- uninstall:
Expand All @@ -148,21 +148,21 @@ tasks:
with:
path: python3
args:
- allocation.py
- allocation/main.py
- action: create
- provider: "{infra-provider}"
- size: small
- composite-name: "{agent}"
- inventory-output: "{working-dir}/agent-{agent}/inventory.yaml"
- track-output: "{working-dir}/agent-{agent}/track.yaml"
cleanup:
this: process
with:
path: python3
args:
- allocation.py
- action: delete
- track-output: "{working-dir}/agent-{agent}/track.yaml"
#cleanup:
# this: process
# with:
# path: python3
# args:
# - allocation.py
# - action: delete
# - track-output: "{working-dir}/agent-{agent}/track.yaml"
foreach:
- variable: agents-os
as: agent
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import logging
import threading


class ThreadIDFilter(logging.Filter):
"""
A filter that uppercases the name of the log record.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import yaml


def _load_config() -> None:
"""
Loads the logging configuration from 'config.yaml' file.
Expand Down
Empty file.
3 changes: 1 addition & 2 deletions deployability/modules/workflow_engine/schema_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
from pathlib import Path
from ruamel.yaml import YAML

from .logging.logger import logger

from workflow_engine.logger.logger import logger

class SchemaValidator:
"""
Expand Down
7 changes: 1 addition & 6 deletions deployability/modules/workflow_engine/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
import time

from abc import ABC, abstractmethod
from .logging.logger import logger

from workflow_engine.logger.logger import logger

class Task(ABC):
"""Abstract base class for tasks."""
Expand All @@ -17,8 +16,6 @@ class Task(ABC):
def execute(self) -> None:
"""Execute the task."""
pass


class ProcessTask(Task):
"""Task for executing a process."""

Expand Down Expand Up @@ -73,7 +70,6 @@ def execute(self):
message = self.task_parameters.get('message', 'No message provided')
logger.info("%s: %s", message, self.task_name, extra={'tag': self.task_name})


class DummyRandomTask(Task):
def __init__(self, task_name, task_parameters):
self.task_name = task_name
Expand All @@ -88,7 +84,6 @@ def execute(self):

time.sleep(sleep_time)


TASKS_HANDLERS = {
'process': ProcessTask,
'dummy': DummyTask,
Expand Down
7 changes: 3 additions & 4 deletions deployability/modules/workflow_engine/workflow_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
from pathlib import Path
from itertools import product

from .logging.logger import logger
from .schema_validator import SchemaValidator
from .task import Task, TASKS_HANDLERS

from workflow_engine.logger.logger import logger
from workflow_engine.schema_validator import SchemaValidator
from workflow_engine.task import Task, TASKS_HANDLERS

class WorkflowFile:
"""Class for loading and processing a workflow file."""
Expand Down
4 changes: 4 additions & 0 deletions deployability/version.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"version": "1.0",
"revision": "1"
}