refactor(connectivity_tester): deploy without compose pull
This commit is contained in:
@@ -102,10 +102,10 @@ When you add a stack, also:
|
||||
|
||||
Brencher does not run docker build or docker push itself. It dispatches a Gitea
|
||||
Actions workflow via `POST /api/v1/repos/{owner}/{repo}/actions/workflows/{file}/dispatches`,
|
||||
waits for the run to complete, then `docker compose pull` + `docker stack deploy`.
|
||||
waits for the run to complete, then runs `docker stack deploy`.
|
||||
|
||||
Use `steps.actions.GiteaActionsTrigger` and `steps.actions.GiteaActionsAwait`
|
||||
in the stack config, set `buildDocker=None` on `DockerSwarmDeploy`, and add
|
||||
`DockerComposePull` before deploy. Credentials come from brencher env
|
||||
(`GITEA_TOKEN`) and the compose image must point to a registry the runner can
|
||||
push to (e.g. Gitea Container Registry `gitea.rudolf.keenetic.link/<owner>/<repo>`).
|
||||
in the stack config, set `buildDocker=None` on `DockerSwarmDeploy`, and let Swarm
|
||||
resolve and pull the image on target nodes during deploy. Credentials must be
|
||||
available to the deploy path and the compose image must point to a registry the
|
||||
runner can push to (e.g. Gitea Container Registry `gitea.rudolf.keenetic.link/<owner>/<repo>`).
|
||||
|
||||
@@ -4,7 +4,6 @@ from enironment import Environment
|
||||
from steps.actions import GiteaActionsAwait, GiteaActionsTrigger
|
||||
from steps.checks import SimpleLog
|
||||
from steps.docker import DockerSwarmCheck, DockerSwarmDeploy
|
||||
from steps.docker_pull import DockerComposePull
|
||||
from steps.git import CheckoutMerged, GitClone, GitUnmerge
|
||||
from steps.shared_state import SharedStateHolderInMemory
|
||||
|
||||
@@ -57,15 +56,6 @@ await_build = GiteaActionsAwait(
|
||||
run=lambda: trigger_build.progress(),
|
||||
)
|
||||
|
||||
pull_image = DockerComposePull(
|
||||
clone,
|
||||
docker_compose_path="connectivity_tester/stack-compose.yml",
|
||||
envs=lambda: {
|
||||
"version": "auto-" + checkoutMerged.progress().version,
|
||||
"registry_owner": "admin",
|
||||
},
|
||||
)
|
||||
|
||||
deployDocker = DockerSwarmDeploy(
|
||||
wd=clone,
|
||||
buildDocker=None,
|
||||
@@ -95,7 +85,6 @@ connectivity_tester = Environment(
|
||||
dockerSwarmCheck,
|
||||
trigger_build,
|
||||
await_build,
|
||||
pull_image,
|
||||
deployDocker,
|
||||
logUrls,
|
||||
],
|
||||
|
||||
@@ -1,100 +0,0 @@
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
import tempfile
|
||||
from typing import Any, Callable, Dict
|
||||
from urllib.parse import urlparse
|
||||
|
||||
import yaml
|
||||
from enironment import AbstractStep
|
||||
from steps.git import GitClone
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _secret_values() -> Dict[str, str]:
|
||||
values: Dict[str, str] = {}
|
||||
try:
|
||||
with open("/run/secrets/brencher-secrets") as f:
|
||||
for line in f:
|
||||
key, separator, value = line.strip().partition("=")
|
||||
if separator:
|
||||
values[key] = value
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
return values
|
||||
|
||||
|
||||
def _registry_login(images: list[str], env: Dict[str, str]) -> None:
|
||||
values = _secret_values()
|
||||
username = (
|
||||
values.get("REGISTRY_USERNAME")
|
||||
or values.get("GITEA_USERNAME")
|
||||
or values.get("GIT_USERNAME", "")
|
||||
)
|
||||
password = (
|
||||
values.get("REGISTRY_TOKEN")
|
||||
or values.get("GITEA_PASSWORD")
|
||||
or values.get("GIT_PASSWORD", "")
|
||||
)
|
||||
registries: set[str] = set()
|
||||
for image in images:
|
||||
registry = urlparse("https://" + image.split("/", 1)[0]).hostname
|
||||
if registry:
|
||||
registries.add(registry)
|
||||
if not username or not password:
|
||||
return
|
||||
for registry in sorted(registries):
|
||||
subprocess.run(
|
||||
["docker", "login", registry, "--username", username, "--password-stdin"],
|
||||
input=password,
|
||||
text=True,
|
||||
check=True,
|
||||
env=env,
|
||||
stdout=subprocess.DEVNULL,
|
||||
)
|
||||
|
||||
|
||||
class DockerComposePull(AbstractStep[Dict[str, str]]):
|
||||
"""Pull images referenced by a docker-compose file from a remote registry."""
|
||||
|
||||
def __init__(self,
|
||||
wd: GitClone,
|
||||
docker_compose_path: str,
|
||||
envs: Callable[[], Dict[str, Any]],
|
||||
**kwargs: Any) -> None:
|
||||
super().__init__(**kwargs)
|
||||
self.wd = wd
|
||||
self.envs = envs
|
||||
self.docker_compose_path = docker_compose_path
|
||||
|
||||
def progress(self) -> Dict[str, str]:
|
||||
env = self.envs()
|
||||
compose_path = os.path.join(self.wd.progress(), self.docker_compose_path)
|
||||
with open(compose_path, "r") as f:
|
||||
content = f.read()
|
||||
content = re.sub(r"\$\{([^}]+)\}", lambda m: env.get(m.group(1), ""), content)
|
||||
compose = yaml.safe_load(content)
|
||||
for svc in compose.get("services", {}).values():
|
||||
if "build" in svc:
|
||||
del svc["build"]
|
||||
tmp_path = compose_path + ".pull.yml"
|
||||
with open(tmp_path, "w") as f:
|
||||
yaml.safe_dump(compose, f)
|
||||
try:
|
||||
images = [svc.get("image", "") for svc in compose.get("services", {}).values()]
|
||||
with tempfile.TemporaryDirectory() as docker_config:
|
||||
env = os.environ.copy()
|
||||
env["DOCKER_CONFIG"] = docker_config
|
||||
_registry_login([image for image in images if image], env)
|
||||
subprocess.run(
|
||||
["docker", "compose", "-f", tmp_path, "pull"],
|
||||
check=True,
|
||||
cwd=self.wd.progress(),
|
||||
env=env,
|
||||
)
|
||||
finally:
|
||||
os.unlink(tmp_path)
|
||||
return {name: svc.get("image", "") for name, svc in compose.get("services", {}).items()}
|
||||
Reference in New Issue
Block a user