Skip to content

Commit

Permalink
refactor: rename OperationLog class to OperationModel
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulFarault committed Nov 8, 2023
1 parent f345908 commit 8e2f4e7
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 83 deletions.
12 changes: 6 additions & 6 deletions tdp/cli/commands/browse.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
from tdp.cli.queries import (
get_deployment,
get_deployments,
get_operation_log,
get_operation_records,
get_planned_deployment,
)
from tdp.cli.session import get_session
from tdp.cli.utils import database_dsn, print_deployment, print_object, print_table
from tdp.core.models import DeploymentModel, OperationLog
from tdp.core.models import DeploymentModel, OperationModel


@click.command(short_help="Browse deployments")
Expand Down Expand Up @@ -61,7 +61,7 @@ def browse(

# Print a specific operation
if deployment_id and operation:
_print_operations(get_operation_log(session, deployment_id, operation))
_print_operations(get_operation_records(session, deployment_id, operation))
return

# Print a specific deployment
Expand Down Expand Up @@ -101,7 +101,7 @@ def _print_deployment(deployment: DeploymentModel) -> None:
print_deployment(deployment, filter_out=["logs"])


def _print_operations(operations: list[OperationLog]) -> None:
def _print_operations(operations: list[OperationModel]) -> None:
"""Print a list of operations in a human readable format.
Args:
Expand All @@ -111,7 +111,7 @@ def _print_operations(operations: list[OperationLog]) -> None:
click.secho("Operation(s) details", bold=True)
for operation in operations:
click.echo(print_object(operation.to_dict()))
# Print operation logs
# Print operation records
if operation.logs:
click.secho("\nOperation logs", bold=True)
click.secho("\nOperations", bold=True)
click.echo(str(operation.logs, "utf-8"))
12 changes: 6 additions & 6 deletions tdp/cli/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from tdp.core.models import (
DeploymentModel,
OperationLog,
OperationModel,
SCHStatusLogModel,
)

Expand Down Expand Up @@ -447,25 +447,25 @@ def get_planned_deployment(session: Session) -> Optional[DeploymentModel]:
return session.query(DeploymentModel).filter_by(status="PLANNED").one_or_none()


def get_operation_log(
def get_operation_records(
session: Session, deployment_id: int, operation_name: str
) -> list[OperationLog]:
"""Get an operation log.
) -> list[OperationModel]:
"""Get an operation records.
Args:
session: The database session.
deployment_id: The deployment id.
operation_name: The operation name.
Returns:
List of matching operation logs.
List of matching operation records.
Raises:
NoResultFound: If the operation does not exist.
"""
try:
return (
session.query(OperationLog)
session.query(OperationModel)
.filter_by(deployment_id=deployment_id, operation=operation_name)
.all()
)
Expand Down
44 changes: 22 additions & 22 deletions tdp/core/deployment/deployment_iterator.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
DeploymentModel,
DeploymentStateEnum,
NothingToReconfigureError,
OperationLog,
OperationModel,
OperationStateEnum,
SCHStatusLogModel,
SCHStatusLogSourceEnum,
Expand All @@ -38,9 +38,9 @@ def _group_hosts_by_operation(
Example:
>>> _group_hosts_by_operation(DeploymentModel(
... operations=[
... OperationLog(operation="s_c_install", host="host1"),
... OperationLog(operation="s_c_config", host="host1"),
... OperationLog(operation="s_c_config", host="host2"),
... OperationModel(operation="s_c_install", host="host1"),
... OperationModel(operation="s_c_config", host="host1"),
... OperationModel(operation="s_c_config", host="host2"),
... ]
... ))
{'s_c_install': {'host1'}, 's_c_config': {'host1', 'host2'}}
Expand All @@ -49,9 +49,9 @@ def _group_hosts_by_operation(
return None

operation_to_hosts_set = OrderedDict()
for operation_log in deployment.operations:
operation_to_hosts_set.setdefault(operation_log.operation, set()).add(
operation_log.host
for operation in deployment.operations:
operation_to_hosts_set.setdefault(operation.operation, set()).add(
operation.host
)
return operation_to_hosts_set

Expand All @@ -68,7 +68,7 @@ def __init__(
deployment: DeploymentModel,
*,
collections: Collections,
run_method: Callable[[OperationLog], None],
run_method: Callable[[OperationModel], None],
cluster_variables: ClusterVariables,
cluster_status: ClusterStatus,
force_stale_update: bool,
Expand Down Expand Up @@ -102,25 +102,25 @@ def __init__(
def __next__(self) -> Optional[list[SCHStatusLogModel]]:
try:
while True:
operation_log: OperationLog = next(self._iter)
operation_rec: OperationModel = next(self._iter)

# Return early if deployment failed
if self.deployment.status == DeploymentStateEnum.FAILURE:
operation_log.state = OperationStateEnum.HELD
operation_rec.state = OperationStateEnum.HELD
return

# Retrieve operation to access parsed attributes and playbook
operation = self._collections.get_operation(operation_log.operation)
operation = self._collections.get_operation(operation_rec.operation)

# Run the operation
if operation.noop:
# A noop operation is always successful
operation_log.state = OperationStateEnum.SUCCESS
operation_rec.state = OperationStateEnum.SUCCESS
else:
self._run_operation(operation_log)
self._run_operation(operation_rec)

# Set deployment status to failure if the operation failed
if operation_log.state != OperationStateEnum.SUCCESS:
if operation_rec.state != OperationStateEnum.SUCCESS:
self.deployment.status = DeploymentStateEnum.FAILURE
# Return early as status is not updated
return
Expand Down Expand Up @@ -153,13 +153,13 @@ def __next__(self) -> Optional[list[SCHStatusLogModel]]:
first_reconfigure_operation = None

can_update_stale = self.force_stale_update or (
operation_log.operation == first_reconfigure_operation
operation_rec.operation == first_reconfigure_operation
)

# Log a warning if the operation affect a stale SCH which is not the first reconfigure operation (if any)
if not can_update_stale:
logger.warning(
f"can't update stale {sc_name} with {operation_log.operation}\n"
f"can't update stale {sc_name} with {operation_rec.operation}\n"
+ "first operation is {first_reconfigure_operation}"
)
else:
Expand All @@ -168,7 +168,7 @@ def __next__(self) -> Optional[list[SCHStatusLogModel]]:
# fmt: off
hosts = (
[None] if operation.noop # A noop operation doesn't have any host
else [operation_log.host] if operation_log.host # Only one operation is launched on a single host
else [operation_rec.host] if operation_rec.host # Only one operation is launched on a single host
else operation.host_names # Host is not specified, hence the operation is launched on all host
)
# fmt: on
Expand All @@ -193,16 +193,16 @@ def __next__(self) -> Optional[list[SCHStatusLogModel]]:
# Update the reconfigure_operations dict
if self._reconfigure_operations:
hosts = self._reconfigure_operations.get(
operation_log.operation, set()
operation_rec.operation, set()
)
# If host is defined and needed to be reconfigured,
# remove it from the reconfigure_operations dict
if operation_log.host and operation_log.host in hosts:
hosts.remove(operation_log.host)
if operation_rec.host and operation_rec.host in hosts:
hosts.remove(operation_rec.host)
# If no host is defined, or no host is left,
# remove the entire operation from the reconfigure_operations dict
if not operation_log.host or len(hosts) == 0:
self._reconfigure_operations.pop(operation_log.operation, None)
if not operation_rec.host or len(hosts) == 0:
self._reconfigure_operations.pop(operation_rec.operation, None)

return sch_status_logs
# StopIteration is a "normal" exception raised when the iteration has stopped
Expand Down
32 changes: 16 additions & 16 deletions tdp/core/deployment/deployment_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from tdp.core.cluster_status import ClusterStatus
from tdp.core.collections import Collections
from tdp.core.deployment.executor import Executor
from tdp.core.models import DeploymentModel, OperationLog
from tdp.core.models import DeploymentModel, OperationModel

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -43,26 +43,26 @@ def __init__(
self._cluster_variables = cluster_variables
self._cluster_status = cluster_status

def _run_operation(self, operation_log: OperationLog) -> None:
def _run_operation(self, operation_rec: OperationModel) -> None:
"""Run operation.
Args:
operation_log: Operation to run, modified in place with the result.
operation_rec: Operation record to run, modified in place with the result.
"""
operation_log.start_time = datetime.utcnow()
operation_rec.start_time = datetime.utcnow()

operation = self._collections.get_operation(operation_log.operation)
operation = self._collections.get_operation(operation_rec.operation)

# Check if the operation is available for the given host
if operation_log.host and operation_log.host not in operation.host_names:
if operation_rec.host and operation_rec.host not in operation.host_names:
logs = (
f"Operation '{operation_log.operation}' not available for host "
+ f"'{operation_log.host}'"
f"Operation '{operation_rec.operation}' not available for host "
+ f"'{operation_rec.host}'"
)
logger.error(logs)
operation_log.state = OperationStateEnum.FAILURE
operation_log.logs = logs.encode("utf-8")
operation_log.end_time = datetime.utcnow()
operation_rec.state = OperationStateEnum.FAILURE
operation_rec.logs = logs.encode("utf-8")
operation_rec.end_time = datetime.utcnow()
return

# Execute the operation
Expand All @@ -71,10 +71,10 @@ def _run_operation(self, operation_log: OperationLog) -> None:
]
state, logs = self._executor.execute(
playbook=playbook_file,
host=operation_log.host,
extra_vars=operation_log.extra_vars,
host=operation_rec.host,
extra_vars=operation_rec.extra_vars,
)
operation_log.end_time = datetime.utcnow()
operation_rec.end_time = datetime.utcnow()

# ? This case shouldn't happen as the executor should return a valid state
if state not in OperationStateEnum:
Expand All @@ -84,8 +84,8 @@ def _run_operation(self, operation_log: OperationLog) -> None:
state = OperationStateEnum.FAILURE
elif not isinstance(state, OperationStateEnum):
state = OperationStateEnum(state)
operation_log.state = state
operation_log.logs = logs
operation_rec.state = state
operation_rec.logs = logs

def run(
self,
Expand Down
2 changes: 1 addition & 1 deletion tdp/core/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
NothingToReconfigureError,
NothingToResumeError,
)
from tdp.core.models.operation_log import OperationLog
from tdp.core.models.operation_model import OperationModel
from tdp.core.models.sch_status_log_model import (
SCHStatusLogModel,
SCHStatusLogSourceEnum,
Expand Down
20 changes: 10 additions & 10 deletions tdp/core/models/deployment_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from tdp.core.collections import OPERATION_SLEEP_NAME, OPERATION_SLEEP_VARIABLE
from tdp.core.dag import Dag
from tdp.core.models.base_model import BaseModel
from tdp.core.models.operation_log import OperationLog
from tdp.core.models.operation_model import OperationModel
from tdp.core.models.state_enum import DeploymentStateEnum, OperationStateEnum
from tdp.core.utils import BaseEnum

Expand Down Expand Up @@ -76,9 +76,9 @@ class DeploymentModel(BaseModel):
doc="Deployment type."
)

operations: Mapped[list[OperationLog]] = relationship(
operations: Mapped[list[OperationModel]] = relationship(
back_populates="deployment",
order_by="OperationLog.operation_order",
order_by="OperationModel.operation_order",
cascade="all, delete-orphan",
doc="List of operations.",
)
Expand Down Expand Up @@ -159,7 +159,7 @@ def from_dag(
status=DeploymentStateEnum.PLANNED,
)
deployment.operations = [
OperationLog(
OperationModel(
operation=operation.name,
operation_order=i,
host=None,
Expand Down Expand Up @@ -226,7 +226,7 @@ def from_operations(
else [None]
):
deployment.operations.append(
OperationLog(
OperationModel(
operation=operation.name,
operation_order=i,
host=host_name,
Expand All @@ -237,7 +237,7 @@ def from_operations(
if can_perform_rolling_restart:
i += 1
deployment.operations.append(
OperationLog(
OperationModel(
operation=OPERATION_SLEEP_NAME,
operation_order=i,
host=host_name,
Expand Down Expand Up @@ -283,7 +283,7 @@ def from_operations_hosts_vars(
)

deployment.operations.append(
OperationLog(
OperationModel(
operation=operation_name,
operation_order=order,
host=host_name,
Expand Down Expand Up @@ -355,7 +355,7 @@ def from_stale_components(
operation_order = 1
for operation, host in operation_hosts_sorted:
deployment.operations.append(
OperationLog(
OperationModel(
operation=operation.name,
operation_order=operation_order,
host=host,
Expand All @@ -367,7 +367,7 @@ def from_stale_components(
if rolling_interval is not None and operation.action_name == "restart":
operation_order += 1
deployment.operations.append(
OperationLog(
OperationModel(
operation=OPERATION_SLEEP_NAME,
operation_order=operation_order,
host=None,
Expand Down Expand Up @@ -426,7 +426,7 @@ def from_failed_deployment(
status=DeploymentStateEnum.PLANNED,
)
deployment.operations = [
OperationLog(
OperationModel(
operation=operation,
operation_order=i,
host=host,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
from tdp.core.models import DeploymentModel


class OperationLog(BaseModel):
"""Operation log model.
class OperationModel(BaseModel):
"""Operation model.
Hold past and current operation information linked to a deployment.
"""
Expand All @@ -43,7 +43,7 @@ class OperationLog(BaseModel):
start_time: Mapped[Optional[datetime]] = mapped_column(doc="Operation start time.")
end_time: Mapped[Optional[datetime]] = mapped_column(doc="Operation end time.")
state: Mapped[OperationStateEnum] = mapped_column(doc="Operation state.")
logs: Mapped[Optional[bytes]] = mapped_column(doc="Operation logs.")
logs: Mapped[Optional[bytes]] = mapped_column(doc="Operation.")

deployment: Mapped[DeploymentModel] = relationship(
back_populates="operations", doc="deployment."
Expand Down
Loading

0 comments on commit 8e2f4e7

Please sign in to comment.