Skip to content

Commit

Permalink
refactor: remove Executor abstraction
Browse files Browse the repository at this point in the history
The executor abstraction is not necessary as TDP is build on top of Ansible.
  • Loading branch information
PaulFarault committed Jun 30, 2023
1 parent efb12eb commit 5ddc290
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 81 deletions.
4 changes: 2 additions & 2 deletions tdp/cli/commands/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
validate,
vars,
)
from tdp.core.deployment import AnsibleExecutor, DeploymentPlan, DeploymentRunner
from tdp.core.deployment import Executor, DeploymentPlan, DeploymentRunner
from tdp.core.models import DeploymentStateEnum
from tdp.core.variables import ClusterVariables

Expand Down Expand Up @@ -47,7 +47,7 @@ def deploy(

deployment_runner = DeploymentRunner(
collections,
AnsibleExecutor(
Executor(
run_directory=run_directory.absolute() if run_directory else None,
dry=dry or mock_deploy,
),
Expand Down
1 change: 0 additions & 1 deletion tdp/core/deployment/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Copyright 2022 TOSIT.IO
# SPDX-License-Identifier: Apache-2.0

from .ansible_executor import AnsibleExecutor
from .deployment_iterator import DeploymentIterator
from .deployment_plan import (
DeploymentPlan,
Expand Down
69 changes: 0 additions & 69 deletions tdp/core/deployment/ansible_executor.py

This file was deleted.

68 changes: 60 additions & 8 deletions tdp/core/deployment/executor.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,75 @@
# Copyright 2022 TOSIT.IO
# SPDX-License-Identifier: Apache-2.0

from abc import ABC, abstractmethod
import io
import logging
import subprocess
from typing import Tuple

from tdp.core.models import OperationStateEnum

logger = logging.getLogger("tdp").getChild("ansible_executor")

class Executor(ABC):
"""An Executor is an object able to run operations."""

@abstractmethod
def execute(self, operation: str) -> Tuple[OperationStateEnum, bytes]:
class Executor:
"""Allow to execute commands using Ansible."""

def __init__(self, run_directory=None, dry: bool = False):
"""Initialize the executor.
Args:
run_directory: Directory where to run the ansible command.
dry: Whether or not to run the command in dry mode.
"""
# TODO configurable via config file
self._rundir = run_directory
self._dry = dry

def _execute_ansible_command(self, command: str) -> Tuple[OperationStateEnum, str]:
"""Execute an ansible command.
Args:
command: Command to execute.
Returns:
A tuple with the state of the command and the output of the command.
"""
with io.BytesIO() as byte_stream:
try:
res = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
cwd=self._rundir,
universal_newlines=True,
)
if res.stdout is None:
raise Exception("Process has not stdout")
for stdout_line in iter(res.stdout.readline, ""):
print(stdout_line, end="")
byte_stream.write(bytes(stdout_line, "utf-8"))
state = (
OperationStateEnum.SUCCESS
if res.wait() == 0
else OperationStateEnum.FAILURE
)
except KeyboardInterrupt:
logger.debug("KeyboardInterrupt caught")
byte_stream.write(b"\nKeyboardInterrupt")
return OperationStateEnum.FAILURE, byte_stream.getvalue()
return state, byte_stream.getvalue()

def execute(self, operation: str) -> Tuple[OperationStateEnum, str]:
"""Executes an operation.
Args:
operation: Operation name.
operation: Name of the operation to execute.
Returns:
Whether an operation is a success as well as its logs in UTF-8 bytes.
A tuple with the state of the command and the output of the command in UTF-8.
"""
pass
command = ["ansible-playbook", str(operation)]
if self._dry:
logger.info("[DRY MODE] Ansible command: " + " ".join(command))
return OperationStateEnum.SUCCESS, b""
return self._execute_ansible_command(command)
2 changes: 1 addition & 1 deletion tests/local/test_action_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from tdp.core.models import Base, OperationStateEnum
from tdp.core.models.deployment_log import DeploymentLog
from tdp.core.runner.action_runner import ActionRunner
from tdp.core.runner.executor import Executor
from tdp.core.deployment import Executor

logger = logging.getLogger("tdp").getChild("test_action_runner")

Expand Down

0 comments on commit 5ddc290

Please sign in to comment.