-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: remove Executor abstraction
The executor abstraction is not necessary as TDP is build on top of Ansible.
- Loading branch information
1 parent
efb12eb
commit 5ddc290
Showing
5 changed files
with
63 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters