Initial ABSDEV deployment framework development preview
ABSDEV Framework CI / framework-validation (push) Canceled after 0s
ABSDEV Framework CI / framework-validation (push) Canceled after 0s
This commit is contained in:
@@ -0,0 +1,182 @@
|
||||
"""Controlled Docker Compose application lifecycle.
|
||||
|
||||
The command runner must be supplied by the caller.
|
||||
This module does not execute subprocesses directly.
|
||||
"""
|
||||
|
||||
from pathlib import PurePosixPath
|
||||
|
||||
from core.configuration import (
|
||||
ConfigurationError,
|
||||
validate_config,
|
||||
)
|
||||
|
||||
|
||||
class ServiceError(RuntimeError):
|
||||
"""Application service lifecycle operation failed."""
|
||||
|
||||
|
||||
def require(condition, message):
|
||||
|
||||
if not condition:
|
||||
raise ServiceError(message)
|
||||
|
||||
|
||||
class ServiceLifecycle:
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
config,
|
||||
runner,
|
||||
stop_timeout=90,
|
||||
):
|
||||
|
||||
require(
|
||||
callable(runner),
|
||||
"A command runner is required",
|
||||
)
|
||||
|
||||
require(
|
||||
type(stop_timeout) is int
|
||||
and 1 <= stop_timeout <= 300,
|
||||
"Invalid graceful shutdown timeout",
|
||||
)
|
||||
|
||||
try:
|
||||
|
||||
validated = validate_config(config)
|
||||
|
||||
except ConfigurationError:
|
||||
|
||||
raise ServiceError(
|
||||
"Invalid service configuration"
|
||||
) from None
|
||||
|
||||
docker = validated["docker"]
|
||||
|
||||
root = PurePosixPath(
|
||||
validated["site"]["root"]
|
||||
)
|
||||
|
||||
self._prefix = (
|
||||
"docker",
|
||||
"compose",
|
||||
"--project-name",
|
||||
docker["project"],
|
||||
"--project-directory",
|
||||
str(root),
|
||||
"--file",
|
||||
str(root / "compose.yaml"),
|
||||
)
|
||||
|
||||
self._php = docker["php_service"]
|
||||
self._web = docker["web_service"]
|
||||
self._queue = docker["queue_service"]
|
||||
self._scheduler = docker["scheduler_service"]
|
||||
|
||||
self._runner = runner
|
||||
self._stop_timeout = stop_timeout
|
||||
|
||||
def _execute(self, operation, args, timeout):
|
||||
|
||||
command = (
|
||||
*self._prefix,
|
||||
*args,
|
||||
)
|
||||
|
||||
try:
|
||||
|
||||
result = self._runner(
|
||||
command,
|
||||
timeout=timeout,
|
||||
)
|
||||
|
||||
except Exception:
|
||||
|
||||
raise ServiceError(
|
||||
f"Service operation failed: {operation}"
|
||||
) from None
|
||||
|
||||
require(
|
||||
type(result) is int and result == 0,
|
||||
f"Service operation failed: {operation}",
|
||||
)
|
||||
|
||||
def quiesce(self):
|
||||
|
||||
self._execute(
|
||||
"quiesce workers",
|
||||
(
|
||||
"stop",
|
||||
"--timeout",
|
||||
str(self._stop_timeout),
|
||||
self._queue,
|
||||
self._scheduler,
|
||||
),
|
||||
timeout=self._stop_timeout + 30,
|
||||
)
|
||||
|
||||
def restart_php(self):
|
||||
|
||||
self._execute(
|
||||
"restart PHP",
|
||||
(
|
||||
"restart",
|
||||
"--timeout",
|
||||
str(self._stop_timeout),
|
||||
self._php,
|
||||
),
|
||||
timeout=self._stop_timeout + 30,
|
||||
)
|
||||
|
||||
def validate_nginx(self):
|
||||
|
||||
self._execute(
|
||||
"validate nginx",
|
||||
(
|
||||
"exec",
|
||||
"-T",
|
||||
"--interactive=false",
|
||||
self._web,
|
||||
"nginx",
|
||||
"-t",
|
||||
),
|
||||
timeout=30,
|
||||
)
|
||||
|
||||
def reload_nginx(self):
|
||||
|
||||
self.validate_nginx()
|
||||
|
||||
self._execute(
|
||||
"reload nginx",
|
||||
(
|
||||
"exec",
|
||||
"-T",
|
||||
"--interactive=false",
|
||||
self._web,
|
||||
"nginx",
|
||||
"-s",
|
||||
"reload",
|
||||
),
|
||||
timeout=30,
|
||||
)
|
||||
|
||||
def refresh(self):
|
||||
|
||||
self.restart_php()
|
||||
self.reload_nginx()
|
||||
|
||||
def resume(self):
|
||||
|
||||
self._execute(
|
||||
"resume workers",
|
||||
(
|
||||
"up",
|
||||
"-d",
|
||||
"--no-deps",
|
||||
self._queue,
|
||||
self._scheduler,
|
||||
),
|
||||
timeout=120,
|
||||
)
|
||||
Reference in New Issue
Block a user