PipelineManager 0.1: gemeinsame Docker-Werkzeuge und Strato-Deployment-Vertrag
PipelineManager Image / image (push) Successful in 2m44s
PipelineManager Image / image (push) Successful in 2m44s
This commit is contained in:
commit
a6e159d125
12 files changed
+807
No files matched your search
@@ -0,0 +1,4 @@
|
|||||||
|
.git
|
||||||
|
__pycache__
|
||||||
|
*.pyc
|
||||||
|
.env*
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
* text=auto eol=lf
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
name: PipelineManager Image
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [develop, main]
|
||||||
|
tags: ['v*']
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
image:
|
||||||
|
# Raspi5-Paperless, same Docker-capable host runner used by Tsumugu.
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- name: Werkzeuge auf dem Runner prüfen
|
||||||
|
run: |
|
||||||
|
set -eu
|
||||||
|
uname -m
|
||||||
|
docker version
|
||||||
|
docker info --format '{{.OSType}}/{{.Architecture}}'
|
||||||
|
- name: Image bauen und Manager testen
|
||||||
|
run: |
|
||||||
|
set -eu
|
||||||
|
IMAGE="pipeline-manager:$GITHUB_SHA"
|
||||||
|
docker build --build-arg "VCS_REF=$GITHUB_SHA" -t "$IMAGE" .
|
||||||
|
docker run --rm "$IMAGE" pipeline-manager --version
|
||||||
|
docker run --rm "$IMAGE" php --version
|
||||||
|
docker run --rm "$IMAGE" node --version
|
||||||
|
docker run --rm "$IMAGE" composer --version
|
||||||
|
- name: Geprüfte Version lokal freigeben
|
||||||
|
if: startsWith(github.ref, 'refs/tags/v')
|
||||||
|
run: |
|
||||||
|
set -eu
|
||||||
|
VERSION="${GITHUB_REF#refs/tags/v}"
|
||||||
|
test "$VERSION" = "$(cat VERSION)"
|
||||||
|
SOURCE="pipeline-manager:$GITHUB_SHA"
|
||||||
|
TARGET="pipeline-manager:$VERSION"
|
||||||
|
if docker image inspect "$TARGET" >/dev/null 2>&1; then
|
||||||
|
test "$(docker image inspect --format '{{.Id}}' "$SOURCE")" = "$(docker image inspect --format '{{.Id}}' "$TARGET")"
|
||||||
|
else
|
||||||
|
docker tag "$SOURCE" "$TARGET"
|
||||||
|
fi
|
||||||
|
echo "Freigegeben auf diesem Runner: $TARGET"
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
__pycache__/
|
||||||
|
*.pyc
|
||||||
|
.env*
|
||||||
|
.pipeline/
|
||||||
+26
@@ -0,0 +1,26 @@
|
|||||||
|
FROM node:22-bookworm-slim AS node
|
||||||
|
FROM composer:2 AS composer
|
||||||
|
FROM php:8.4-cli-bookworm
|
||||||
|
ARG VCS_REF=unknown
|
||||||
|
LABEL org.opencontainers.image.revision=$VCS_REF
|
||||||
|
|
||||||
|
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||||
|
python3 git openssh-client curl ca-certificates tar gzip unzip \
|
||||||
|
libicu-dev libzip-dev libonig-dev libsqlite3-dev libpng-dev libjpeg62-turbo-dev libfreetype6-dev \
|
||||||
|
&& docker-php-ext-configure gd --with-freetype --with-jpeg \
|
||||||
|
&& docker-php-ext-install -j2 bcmath exif gd intl mbstring pdo_mysql pdo_sqlite zip \
|
||||||
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
COPY --from=node /usr/local/bin/node /usr/local/bin/node
|
||||||
|
COPY --from=node /usr/local/lib/node_modules /usr/local/lib/node_modules
|
||||||
|
RUN ln -s /usr/local/lib/node_modules/npm/bin/npm-cli.js /usr/local/bin/npm \
|
||||||
|
&& ln -s /usr/local/lib/node_modules/npm/bin/npx-cli.js /usr/local/bin/npx
|
||||||
|
COPY --from=composer /usr/bin/composer /usr/local/bin/composer
|
||||||
|
COPY . /opt/pipeline-manager
|
||||||
|
RUN cd /opt/pipeline-manager && python3 -m unittest discover -s tests -v \
|
||||||
|
&& chmod +x bin/pipeline-manager \
|
||||||
|
&& ln -s /opt/pipeline-manager/bin/pipeline-manager /usr/local/bin/pipeline-manager
|
||||||
|
ENV COMPOSER_ALLOW_SUPERUSER=1 \
|
||||||
|
COMPOSER_CACHE_DIR=/cache/composer \
|
||||||
|
npm_config_cache=/cache/npm
|
||||||
|
WORKDIR /workspace
|
||||||
|
CMD ["pipeline-manager", "--help"]
|
||||||
@@ -0,0 +1,98 @@
|
|||||||
|
# PipelineManager
|
||||||
|
|
||||||
|
Gemeinsame, versionierte Werkzeuge für Tests, Dateivorbereitung und atomare Deployments nach Strato. Die Anwendung wird weiter bei Strato betrieben. Der Manager läuft nur während eines Auftrags als Docker-Container auf einem Gitea-Runner.
|
||||||
|
|
||||||
|
## Aufteilung
|
||||||
|
|
||||||
|
- Dieses Repository enthält das Werkzeug-Image, die Konfigurationsschnittstelle und die gemeinsame Deployment-Logik.
|
||||||
|
- Jede Anwendung enthält eine `pipeline.toml`: eigene Vorbereitungs- und Testbefehle, benötigte Dateien, persistente Pfade und Zielverzeichnisse. Fachlogik und Mandantendaten bleiben in der Anwendung.
|
||||||
|
- Gitea startet die Aufträge. Ein zusätzlicher HTTP-Dienst ist für Version 0.1 nicht erforderlich.
|
||||||
|
|
||||||
|
## Image-Lebenszyklus
|
||||||
|
|
||||||
|
Ein Push auf `main` oder `develop` baut auf `Raspi5-Paperless` (bestehendes Label `ubuntu-latest`) ein Kandidaten-Image `pipeline-manager:<commit>`. Der Docker-Build installiert die Werkzeuge im Image und führt die Manager-Tests aus. Auf dem Host werden keine Pakete installiert. Der Ansatz folgt Tsumugus vorhandenen Docker-Aufträgen, verwendet aber Docker-Caches statt `--no-cache`.
|
||||||
|
|
||||||
|
Nach grünem Image-Build wird derselbe Stand mit `v0.1.0` markiert. Der Tag-Workflow prüft und gibt lokal `pipeline-manager:0.1.0` frei. Eine bereits belegte Versionskennung wird nicht auf ein anderes Image umgebogen. Anwendungen verwenden eine konkrete freigegebene Version. Nur wenn Manager oder Werkzeuge geändert werden, entsteht eine neue Version. Ein Container wird je Auftrag neu gestartet; das Image und die gemounteten Download-Caches bleiben erhalten.
|
||||||
|
|
||||||
|
Die erste Lieferung baut nativ auf dem ausgewählten Pi. Das Image ist dort lokal vorhanden; ein anderer Runner darf es erst verwenden, wenn es auf seiner passenden CPU-Architektur ebenfalls bereitgestellt wurde. Gemeinsame Registry und Multiarch-Veröffentlichung sind eine spätere Erweiterung, kein verstecktes Erfordernis für den ersten Pi.
|
||||||
|
|
||||||
|
## Befehle
|
||||||
|
|
||||||
|
Im Image stehen PHP 8.4 mit den benötigten Erweiterungen, Composer 2, Node 22/npm, Python 3, Git, SSH, curl und tar zur Verfügung. Die Manager-Logik verwendet nur die Python-Standardbibliothek.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
pipeline-manager check --project /workspace
|
||||||
|
pipeline-manager check --project /workspace --environment staging
|
||||||
|
pipeline-manager prepare --project /workspace --artifact /workspace/.pipeline/release.tgz
|
||||||
|
pipeline-manager deploy --project /workspace --environment staging \
|
||||||
|
--artifact /workspace/.pipeline/release.tgz --backup-root /backups
|
||||||
|
```
|
||||||
|
|
||||||
|
`check --environment` liefert Exitcode 3 für ein bewusst gesperrtes Ziel, sonst 0 bei gültiger Konfiguration oder 1 bei einem Fehler. `prepare` exportiert exakt den aktuellen Git-Commit in ein temporäres Verzeichnis, führt prepare → test → finalize aus und erstellt Paket, Prüfsumme und Commit-/Projekt-/Testnachweis. Uncommittete Änderungen werden nicht übernommen. Ein Testfehler verhindert das Paket.
|
||||||
|
|
||||||
|
`deploy` prüft Ziel, Branch, Workflow-Commit, aktuellen Remote-Branch und Paket. Dann folgen Deployment-Sperre, Backup von Datenbank und persistenten Dateien, Upload mit serverseitiger Prüfsumme, Aktivierung, Gesundheitscheck und Aufräumen. Eine fehlgeschlagene Sicherung verhindert den Upload. Ein fehlgeschlagener Gesundheitscheck löst den Rückfall auf die vorige Codeversion aus. Datenbank-Migrationen werden dabei nicht zurückgenommen.
|
||||||
|
|
||||||
|
## Projektkonfiguration, Vertrag 1
|
||||||
|
|
||||||
|
```toml
|
||||||
|
schema = 1
|
||||||
|
project = "example"
|
||||||
|
manager = "0.1.0"
|
||||||
|
|
||||||
|
[build]
|
||||||
|
prepare = [["npm", "ci"], ["npm", "run", "build"]]
|
||||||
|
test = [["npm", "test"]]
|
||||||
|
finalize = []
|
||||||
|
exclude = ["node_modules", ".gitea", "tests", "docs"]
|
||||||
|
required = ["dist/index.html"]
|
||||||
|
|
||||||
|
[remote]
|
||||||
|
public = "dist"
|
||||||
|
backup = []
|
||||||
|
activate = []
|
||||||
|
|
||||||
|
[targets.staging]
|
||||||
|
branch = "develop"
|
||||||
|
path = "test.example.de"
|
||||||
|
url = "https://test.example.de"
|
||||||
|
health_path = "/"
|
||||||
|
enabled = true
|
||||||
|
|
||||||
|
[targets.production]
|
||||||
|
branch = "main"
|
||||||
|
path = "prod.example.de"
|
||||||
|
url = "https://example.de"
|
||||||
|
health_path = "/"
|
||||||
|
enabled = false
|
||||||
|
```
|
||||||
|
|
||||||
|
Hooks sind Argumentlisten ohne implizite Shell-Auswertung. Für PHP auf Strato ersetzt `@php` die PHP-CLI der Zielkonfiguration (Standard `/opt/RZphp84/bin/php-cli`). Beispiel: `activate = [["@php", "artisan", "migrate", "--force", "--no-interaction"]]`. Ein Projekt kann in `[build.environment]` seine isolierten Test-Einstellungen setzen. Die Anwendung FeWo liefert das Laravel-Pilotprofil.
|
||||||
|
|
||||||
|
Persistente Pfade werden über `[remote.shared]` zugeordnet, beispielsweise `".env" = ".env"`, `storage = "storage"`, `"public/storage" = "storage/app/public"`. Diese Quellen müssen unter `<target>/shared/` vor der Erstinstallation existieren. Sie werden nicht durch Code-Deployments erzeugt oder mit Daten einer anderen Umgebung befüllt. Die Projekt-Ausschlussliste muss die persistenten Ziele aus dem Paket ausschließen.
|
||||||
|
|
||||||
|
## Runner, Verzeichnisse und Geheimnisse
|
||||||
|
|
||||||
|
Die Anwendung startet das Image auf dem bisherigen Docker-fähigen Host-Runner. Standard-Verzeichnisse auf dem Pi folgen Tsumugu:
|
||||||
|
|
||||||
|
```text
|
||||||
|
/home/lars/docker/pipeline-manager/
|
||||||
|
cache/composer/
|
||||||
|
cache/npm/
|
||||||
|
backups/<project>/<environment>/
|
||||||
|
```
|
||||||
|
|
||||||
|
Diese Verzeichnisse werden gezielt in den Job-Container gemountet. Der Container erhält weder den Docker-Socket noch andere Anwendungsverzeichnisse. Er läuft mit UID/GID des Runners und mit Ressourcenlimits. Build und Deployment sind getrennte Containeraufrufe: Der Build bekommt keine Strato-Zugänge.
|
||||||
|
|
||||||
|
Nur der Deployment-Schritt erhält die Secrets der jeweiligen Anwendung: `DEPLOY_HOST`, `DEPLOY_USER`, optional `DEPLOY_PORT`, `DEPLOY_SSH_KEY` (Schlüsselinhalt) und `DEPLOY_KNOWN_HOSTS` (bereits geprüfte SSH-Hostschlüssel). Private Schlüssel werden kurzfristig mit Modus 0600 geschrieben und anschließend entfernt. Nichts davon gehört ins Image oder Repository. Backups enthalten sensible Daten einschließlich Server-Konfiguration und liegen mit restriktiven Rechten außerhalb des kurzlebigen Containers.
|
||||||
|
|
||||||
|
## Prüfung und aktueller Stand
|
||||||
|
|
||||||
|
```sh
|
||||||
|
python -m unittest discover -s tests -v
|
||||||
|
docker build -t pipeline-manager:test .
|
||||||
|
docker run --rm pipeline-manager:test pipeline-manager --version
|
||||||
|
```
|
||||||
|
|
||||||
|
Die lokalen Manager-Tests decken eine statische Anwendung, Commit-Auswahl, Testfehler, Paketmanipulation, Pfadwechsel, Geheimnisdateien, gesperrte Ziele, Branch-/Commit-Verwechslungen, veraltete Jobs, Sicherungsreihenfolge, Sperren und Rückfall ab. Der Docker-Build und echte Runner-/Strato-Nachweise werden getrennt protokolliert. Eine erfolgreiche lokale Python-Suite allein ist keine Freigabe für Produktion.
|
||||||
|
|
||||||
|
Tsumugu wurde ausschließlich als Referenz gelesen und bleibt unverändert. Seine parallelen Deploy-/Testläufe und `--no-cache`-Builds sind nicht Teil dieses Managers: Die Tests laufen vor dem Upload am exakten Commit in einem eigenen Arbeitsverzeichnis.
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
||||||
|
from manager.cli import main
|
||||||
|
|
||||||
|
raise SystemExit(main())
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
"""Shared Strato deployment tooling; no application or tenant data."""
|
||||||
+300
@@ -0,0 +1,300 @@
|
|||||||
|
"""Pipeline contract v1. Python standard library only; applications supply TOML and argv hooks."""
|
||||||
|
import argparse
|
||||||
|
import hashlib
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
from pathlib import Path, PurePosixPath
|
||||||
|
import re
|
||||||
|
import shlex
|
||||||
|
import subprocess
|
||||||
|
import tarfile
|
||||||
|
import tempfile
|
||||||
|
import time
|
||||||
|
import tomllib
|
||||||
|
import urllib.request
|
||||||
|
|
||||||
|
VERSION = "0.1.0"
|
||||||
|
NAME = re.compile(r"^[a-zA-Z0-9][a-zA-Z0-9_.-]*$")
|
||||||
|
COMMIT = re.compile(r"^[0-9a-f]{40}$")
|
||||||
|
|
||||||
|
|
||||||
|
class PipelineError(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def relative_path(value):
|
||||||
|
if not isinstance(value, str) or not value or "\\" in value:
|
||||||
|
raise PipelineError("Ungültiger relativer Pfad.")
|
||||||
|
path = PurePosixPath(value)
|
||||||
|
if path.is_absolute() or any(part in ("..", ".") for part in value.split("/")):
|
||||||
|
raise PipelineError("Absolute Pfade und Pfadwechsel sind nicht erlaubt.")
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
def commands(value):
|
||||||
|
if not isinstance(value, list) or any(
|
||||||
|
not isinstance(command, list) or not command
|
||||||
|
or any(not isinstance(arg, str) or not arg or "\x00" in arg for arg in command)
|
||||||
|
for command in value
|
||||||
|
):
|
||||||
|
raise PipelineError("Befehle müssen Listen von Argumentlisten sein.")
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
def load_config(path):
|
||||||
|
with open(path, "rb") as source:
|
||||||
|
config = tomllib.load(source)
|
||||||
|
if config.get("schema") != 1 or not NAME.fullmatch(config.get("project", "")):
|
||||||
|
raise PipelineError("Projektname oder Konfigurationsversion ungültig.")
|
||||||
|
if config.get("manager") != VERSION:
|
||||||
|
raise PipelineError("Dieses Projekt benötigt eine andere Manager-Version.")
|
||||||
|
build = config.get("build", {})
|
||||||
|
for phase in ("prepare", "test", "finalize"):
|
||||||
|
commands(build.get(phase, []))
|
||||||
|
if not build.get("test"):
|
||||||
|
raise PipelineError("Mindestens ein Testbefehl ist erforderlich.")
|
||||||
|
for item in build.get("exclude", []):
|
||||||
|
relative_path(item)
|
||||||
|
for item in build.get("required", []):
|
||||||
|
relative_path(item)
|
||||||
|
for target in config.get("targets", {}).values():
|
||||||
|
if not isinstance(target.get("enabled", False), bool):
|
||||||
|
raise PipelineError("enabled muss true oder false sein.")
|
||||||
|
directory = target.get("path", "")
|
||||||
|
if not directory or any(not NAME.fullmatch(part) or part in (".", "..") for part in directory.split("/")):
|
||||||
|
raise PipelineError("Unsicheres Zielverzeichnis.")
|
||||||
|
if not target.get("url", "").startswith("https://"):
|
||||||
|
raise PipelineError("Gesundheitscheck benötigt HTTPS.")
|
||||||
|
remote = config.get("remote", {})
|
||||||
|
relative_path(remote.get("public", "public"))
|
||||||
|
for destination, source in remote.get("shared", {}).items():
|
||||||
|
relative_path(destination)
|
||||||
|
relative_path(source)
|
||||||
|
commands(remote.get("backup", []))
|
||||||
|
commands(remote.get("activate", []))
|
||||||
|
return config
|
||||||
|
|
||||||
|
|
||||||
|
def execute(argv, cwd=None, env=None, **options):
|
||||||
|
return subprocess.run(argv, cwd=cwd, env=env, check=True, **options)
|
||||||
|
|
||||||
|
|
||||||
|
def git(project, *arguments):
|
||||||
|
return execute(["git", "-c", f"safe.directory={Path(project).resolve()}", "-C", str(project), *arguments], capture_output=True, text=True).stdout.strip()
|
||||||
|
|
||||||
|
|
||||||
|
def digest(path):
|
||||||
|
with open(path, "rb") as source:
|
||||||
|
return hashlib.file_digest(source, "sha256").hexdigest()
|
||||||
|
|
||||||
|
|
||||||
|
def inspect_package(archive, config, revision):
|
||||||
|
checksum = Path(str(archive) + ".sha256")
|
||||||
|
expected = checksum.read_text().strip()
|
||||||
|
if not re.fullmatch(r"[0-9a-f]{64}", expected) or digest(archive) != expected:
|
||||||
|
raise PipelineError("Paket-Prüfsumme stimmt nicht.")
|
||||||
|
with tarfile.open(archive, "r:gz") as package:
|
||||||
|
names = set()
|
||||||
|
for member in package.getmembers():
|
||||||
|
name = relative_path(member.name)
|
||||||
|
if member.issym() or member.islnk() or not (member.isfile() or member.isdir()):
|
||||||
|
raise PipelineError("Links und Spezialdateien sind im Paket nicht erlaubt.")
|
||||||
|
if any(part == ".git" or part == ".env" or part.startswith(".env.") for part in PurePosixPath(name).parts):
|
||||||
|
raise PipelineError("Geheimnisdatei im Paket.")
|
||||||
|
if name in names:
|
||||||
|
raise PipelineError("Doppelter Paketpfad.")
|
||||||
|
names.add(name)
|
||||||
|
metadata = json.load(package.extractfile("PIPELINE.json"))
|
||||||
|
if metadata != {"schema": 1, "project": config["project"], "revision": revision, "tests": "passed"}:
|
||||||
|
raise PipelineError("Paket gehört nicht zum geprüften Projekt und Commit.")
|
||||||
|
if not set(config["build"].get("required", [])).issubset(names):
|
||||||
|
raise PipelineError("Pflichtdateien fehlen im Paket.")
|
||||||
|
return expected
|
||||||
|
|
||||||
|
|
||||||
|
def prepare(project, config, output):
|
||||||
|
project = Path(project).resolve()
|
||||||
|
revision = git(project, "rev-parse", "HEAD")
|
||||||
|
if not COMMIT.fullmatch(revision):
|
||||||
|
raise PipelineError("Ungültige Git-Revision.")
|
||||||
|
output = Path(output).resolve()
|
||||||
|
output.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
if output.exists():
|
||||||
|
raise PipelineError("Ausgabepaket existiert bereits.")
|
||||||
|
with tempfile.TemporaryDirectory(prefix="pipeline-build-") as temp:
|
||||||
|
work = Path(temp)
|
||||||
|
export = work / "source.tar"
|
||||||
|
execute(["git", "-C", str(project), "archive", "--output", str(export), revision])
|
||||||
|
source = work / "source"
|
||||||
|
source.mkdir()
|
||||||
|
with tarfile.open(export) as package:
|
||||||
|
for member in package.getmembers():
|
||||||
|
relative_path(member.name.rstrip("/"))
|
||||||
|
if member.issym() or member.islnk() or not (member.isfile() or member.isdir()):
|
||||||
|
raise PipelineError("Git-Export enthält einen Link oder eine Spezialdatei.")
|
||||||
|
package.extractall(source)
|
||||||
|
environment = os.environ.copy()
|
||||||
|
# Deployment secrets are supplied only to the later deployment step.
|
||||||
|
for key in list(environment):
|
||||||
|
if key.startswith(("DEPLOY_", "SSH_")):
|
||||||
|
del environment[key]
|
||||||
|
environment.update({str(key): str(value) for key, value in config["build"].get("environment", {}).items()})
|
||||||
|
for phase in ("prepare", "test", "finalize"):
|
||||||
|
print(f"Phase: {phase}", flush=True)
|
||||||
|
for command in config["build"].get(phase, []):
|
||||||
|
execute(command, cwd=source, env=environment)
|
||||||
|
metadata = {"schema": 1, "project": config["project"], "revision": revision, "tests": "passed"}
|
||||||
|
(source / "PIPELINE.json").write_text(json.dumps(metadata), encoding="utf-8")
|
||||||
|
excluded = [PurePosixPath(value) for value in config["build"].get("exclude", [])]
|
||||||
|
with tarfile.open(output, "w:gz", dereference=False) as package:
|
||||||
|
for file in sorted(source.rglob("*")):
|
||||||
|
relative = file.relative_to(source)
|
||||||
|
name = PurePosixPath(relative.as_posix())
|
||||||
|
if any(name == item or item in name.parents for item in excluded):
|
||||||
|
continue
|
||||||
|
if any(part in (".git", ".env") or part.startswith(".env.") for part in name.parts):
|
||||||
|
continue
|
||||||
|
if file.is_symlink():
|
||||||
|
raise PipelineError("Symlinks im Build-Verzeichnis sind nicht erlaubt.")
|
||||||
|
package.add(file, arcname=name.as_posix(), recursive=False)
|
||||||
|
Path(str(output) + ".sha256").write_text(digest(output) + "\n", encoding="ascii")
|
||||||
|
inspect_package(output, config, revision)
|
||||||
|
return revision
|
||||||
|
|
||||||
|
|
||||||
|
def render_remote(config):
|
||||||
|
remote = config.get("remote", {})
|
||||||
|
text = (Path(__file__).with_name("remote.sh")).read_text(encoding="utf-8")
|
||||||
|
hooks = {}
|
||||||
|
for phase in ("backup", "activate"):
|
||||||
|
hooks[phase] = "\n".join(
|
||||||
|
" ".join('"$php"' if arg == "@php" else shlex.quote(arg) for arg in command)
|
||||||
|
for command in remote.get(phase, [])
|
||||||
|
) or ":"
|
||||||
|
mounts = "\n".join(
|
||||||
|
f"link_shared {shlex.quote(destination)} {shlex.quote(source)}"
|
||||||
|
for destination, source in remote.get("shared", {}).items()
|
||||||
|
)
|
||||||
|
return text.replace("# PUBLIC_PATH", "public_path=" + shlex.quote(remote.get("public", "public"))).replace(
|
||||||
|
"# BACKUP_HOOKS", hooks["backup"]
|
||||||
|
).replace("# ACTIVATE_HOOKS", hooks["activate"]).replace("# SHARED_LINKS", mounts or ":")
|
||||||
|
|
||||||
|
|
||||||
|
class Transport:
|
||||||
|
def __init__(self, config, target, key, known_hosts):
|
||||||
|
host = os.environ.get("DEPLOY_HOST", "")
|
||||||
|
user = os.environ.get("DEPLOY_USER", "")
|
||||||
|
if not NAME.fullmatch(host) or not re.fullmatch(r"[a-zA-Z0-9_-]+", user):
|
||||||
|
raise PipelineError("DEPLOY_HOST und DEPLOY_USER fehlen oder sind ungültig.")
|
||||||
|
port = os.environ.get("DEPLOY_PORT", "22")
|
||||||
|
if not port.isdigit() or not 1 <= int(port) <= 65535:
|
||||||
|
raise PipelineError("Ungültiger SSH-Port.")
|
||||||
|
self.options = ["ssh", "-p", port, "-o", "BatchMode=yes", "-o", "StrictHostKeyChecking=yes", "-o",
|
||||||
|
f"UserKnownHostsFile={known_hosts}", "-i", str(key), f"{user}@{host}"]
|
||||||
|
self.target = target
|
||||||
|
self.remote = render_remote(config)
|
||||||
|
|
||||||
|
def call(self, command, *arguments, stdout=None):
|
||||||
|
argv = ["bash", "-s", "--", self.target["path"], self.target.get("php", "/opt/RZphp84/bin/php-cli"), command, *arguments]
|
||||||
|
return execute([*self.options, shlex.join(argv)], input=self.remote.encode(), stdout=stdout)
|
||||||
|
|
||||||
|
def upload(self, archive, release):
|
||||||
|
destination = f"{self.target['path']}/upload-{release}.tgz"
|
||||||
|
with open(archive, "rb") as source:
|
||||||
|
execute([*self.options, "cat > " + shlex.quote(destination)], stdin=source)
|
||||||
|
|
||||||
|
|
||||||
|
def health(url):
|
||||||
|
for attempt in range(5):
|
||||||
|
try:
|
||||||
|
with urllib.request.urlopen(url, timeout=15) as response:
|
||||||
|
if response.status == 200:
|
||||||
|
return True
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
if attempt < 4:
|
||||||
|
time.sleep(3)
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def deploy(project, config, environment, archive, backup_root, transport_factory=Transport):
|
||||||
|
target = config.get("targets", {}).get(environment)
|
||||||
|
if not target or not target.get("enabled", False):
|
||||||
|
raise PipelineError("Ziel ist gesperrt oder nicht konfiguriert.")
|
||||||
|
expected_branch = target.get("branch")
|
||||||
|
ref = os.environ.get("GITHUB_REF", os.environ.get("GITEA_REF", ""))
|
||||||
|
if ref != f"refs/heads/{expected_branch}":
|
||||||
|
raise PipelineError("Branch und Ziel stimmen nicht überein.")
|
||||||
|
revision = git(project, "rev-parse", "HEAD")
|
||||||
|
if revision != os.environ.get("GITHUB_SHA", os.environ.get("GITEA_SHA", "")):
|
||||||
|
raise PipelineError("Checkout und Workflow-Commit stimmen nicht überein.")
|
||||||
|
current = git(project, "ls-remote", "origin", ref).split()
|
||||||
|
if not current or current[0] != revision:
|
||||||
|
raise PipelineError("Veralteter Auftrag; Branch wurde inzwischen weitergeschoben.")
|
||||||
|
checksum = inspect_package(archive, config, revision)
|
||||||
|
key_text = os.environ.get("DEPLOY_SSH_KEY", "")
|
||||||
|
known_text = os.environ.get("DEPLOY_KNOWN_HOSTS", "")
|
||||||
|
if not key_text.strip() or not known_text.strip():
|
||||||
|
raise PipelineError("SSH-Schlüssel und geprüfte Hostschlüssel fehlen.")
|
||||||
|
if not backup_root or not Path(backup_root).is_dir():
|
||||||
|
raise PipelineError("Persistenter Sicherungsordner fehlt.")
|
||||||
|
release = time.strftime("%Y%m%d%H%M%S", time.gmtime()) + "-" + revision[:8]
|
||||||
|
backup = Path(backup_root) / config["project"] / environment / f"{release}.tar.gz"
|
||||||
|
backup.parent.mkdir(parents=True, exist_ok=True, mode=0o700)
|
||||||
|
with tempfile.TemporaryDirectory(prefix="pipeline-ssh-") as temp:
|
||||||
|
key = Path(temp) / "key"
|
||||||
|
known_hosts = Path(temp) / "known_hosts"
|
||||||
|
key.write_text(key_text.rstrip() + "\n", encoding="utf-8")
|
||||||
|
key.chmod(0o600)
|
||||||
|
known_hosts.write_text(known_text.rstrip() + "\n", encoding="utf-8")
|
||||||
|
transport = transport_factory(config, target, key, known_hosts)
|
||||||
|
transport.call("lock")
|
||||||
|
try:
|
||||||
|
with open(backup, "xb") as destination:
|
||||||
|
backup.chmod(0o600)
|
||||||
|
transport.call("backup", stdout=destination)
|
||||||
|
with tarfile.open(backup, "r:gz") as saved:
|
||||||
|
if not saved.getmembers():
|
||||||
|
raise PipelineError("Sicherung ist leer.")
|
||||||
|
transport.call("cleanup")
|
||||||
|
transport.upload(archive, release)
|
||||||
|
transport.call("receive", release, checksum)
|
||||||
|
transport.call("activate", release, revision)
|
||||||
|
if not health(target["url"].rstrip("/") + target.get("health_path", "/up")):
|
||||||
|
transport.call("rollback")
|
||||||
|
raise PipelineError("Gesundheitscheck fehlgeschlagen; Rückfall ausgeführt (Erstinstallation gesperrt).")
|
||||||
|
transport.call("cleanup")
|
||||||
|
finally:
|
||||||
|
transport.call("unlock")
|
||||||
|
print(f"Ausgeliefert: {config['project']} / {environment} / {revision}")
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
parser = argparse.ArgumentParser(description="Gemeinsame Vorbereitung und Strato-Deployments")
|
||||||
|
parser.add_argument("--version", action="version", version=VERSION)
|
||||||
|
parser.add_argument("command", choices=["check", "prepare", "deploy"])
|
||||||
|
parser.add_argument("--project", default=".")
|
||||||
|
parser.add_argument("--config", default="pipeline.toml")
|
||||||
|
parser.add_argument("--artifact", default=".pipeline/release.tgz")
|
||||||
|
parser.add_argument("--environment", choices=["staging", "production"])
|
||||||
|
parser.add_argument("--backup-root", default=os.environ.get("PIPELINE_BACKUP_ROOT"))
|
||||||
|
args = parser.parse_args()
|
||||||
|
try:
|
||||||
|
config = load_config(Path(args.project) / args.config)
|
||||||
|
if args.command == "prepare":
|
||||||
|
prepare(args.project, config, args.artifact)
|
||||||
|
elif args.command == "deploy":
|
||||||
|
deploy(args.project, config, args.environment, args.artifact, args.backup_root)
|
||||||
|
else:
|
||||||
|
if args.environment and not config.get("targets", {}).get(args.environment, {}).get("enabled", False):
|
||||||
|
print("Ziel ist gesperrt; kein Build oder Deployment.")
|
||||||
|
return 3
|
||||||
|
print(f"Konfiguration gültig: {config['project']} / Manager {VERSION}")
|
||||||
|
return 0
|
||||||
|
except (PipelineError, subprocess.CalledProcessError, OSError, ValueError, KeyError, tarfile.TarError) as error:
|
||||||
|
print(f"Pipeline fehlgeschlagen: {error}")
|
||||||
|
return 1
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
raise SystemExit(main())
|
||||||
@@ -0,0 +1,109 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
set -euo pipefail
|
||||||
|
base="$1"; php="$2"; command="$3"; shift 3
|
||||||
|
# PUBLIC_PATH
|
||||||
|
[[ "$base" =~ ^[a-zA-Z0-9_./-]+$ && "$base" != /* && "$base" != *..* && "$base" != '.' && ! -L "$base" ]] || exit 1
|
||||||
|
mkdir -p "$base"
|
||||||
|
cd "$base"
|
||||||
|
base="$(pwd -P)"
|
||||||
|
|
||||||
|
active_release() {
|
||||||
|
local active
|
||||||
|
active="$(readlink current 2>/dev/null || true)"
|
||||||
|
[[ -z "$active" || "$active" =~ ^releases/[a-zA-Z0-9_-]+$ ]] || exit 1
|
||||||
|
printf '%s' "$active"
|
||||||
|
}
|
||||||
|
|
||||||
|
switch_to() {
|
||||||
|
local target="$1" version web_php
|
||||||
|
[[ "$target" =~ ^releases/[a-zA-Z0-9_-]+$ && -d "$target/$public_path" ]] || exit 1
|
||||||
|
version="${target#releases/}"
|
||||||
|
web_php="$(printf '%s' "$php" | grep -oE 'RZphp[0-9]+' | grep -oE '[0-9]+' || true)"
|
||||||
|
{
|
||||||
|
[[ -z "$web_php" ]] || echo "AddType application/x-httpd-php$web_php .php"
|
||||||
|
cat <<HTACCESS
|
||||||
|
<IfModule mod_rewrite.c>
|
||||||
|
RewriteEngine On
|
||||||
|
RewriteRule ^(releases/(?!$version/$public_path/)|shared(/|$)|current(/|$)|\.|upload-) - [F,L]
|
||||||
|
RewriteCond %{REQUEST_URI} !^/releases/$version/$public_path/
|
||||||
|
RewriteRule ^(.*)\$ releases/$version/$public_path/\$1 [L]
|
||||||
|
</IfModule>
|
||||||
|
<IfModule !mod_rewrite.c>
|
||||||
|
Require all denied
|
||||||
|
</IfModule>
|
||||||
|
HTACCESS
|
||||||
|
} > .htaccess.new
|
||||||
|
ln -sfn "$target" current.new
|
||||||
|
mv -Tf current.new current
|
||||||
|
mv -f .htaccess.new .htaccess
|
||||||
|
}
|
||||||
|
|
||||||
|
link_shared() {
|
||||||
|
local destination="$1" source="$2"
|
||||||
|
[[ -e "$base/shared/$source" ]] || { echo "Persistenter Pfad fehlt: shared/$source" >&2; exit 1; }
|
||||||
|
mkdir -p "$(dirname "$dir/$destination")"
|
||||||
|
rm -rf "$dir/$destination"
|
||||||
|
ln -s "$base/shared/$source" "$dir/$destination"
|
||||||
|
}
|
||||||
|
|
||||||
|
case "$command" in
|
||||||
|
lock)
|
||||||
|
mkdir -p releases
|
||||||
|
mkdir .deploy-lock || { echo 'Deployment läuft bereits; .deploy-lock prüfen.' >&2; exit 1; }
|
||||||
|
;;
|
||||||
|
unlock) rmdir .deploy-lock ;;
|
||||||
|
backup)
|
||||||
|
if [[ -n "$(active_release)" ]]; then
|
||||||
|
(
|
||||||
|
cd current
|
||||||
|
# BACKUP_HOOKS
|
||||||
|
) >&2
|
||||||
|
fi
|
||||||
|
tar -czf - shared
|
||||||
|
;;
|
||||||
|
receive)
|
||||||
|
release="$1"; checksum="$2"
|
||||||
|
[[ "$release" =~ ^[a-zA-Z0-9_-]+$ && "$checksum" =~ ^[0-9a-f]{64}$ ]] || exit 1
|
||||||
|
[[ "$(sha256sum "upload-$release.tgz" | cut -d ' ' -f1)" == "$checksum" ]] || exit 1
|
||||||
|
mkdir "releases/$release"
|
||||||
|
tar --no-same-owner -xzf "upload-$release.tgz" -C "releases/$release"
|
||||||
|
rm "upload-$release.tgz"
|
||||||
|
;;
|
||||||
|
activate)
|
||||||
|
release="$1"; revision="$2"
|
||||||
|
[[ "$release" =~ ^[a-zA-Z0-9_-]+$ && "$revision" =~ ^[0-9a-f]{40}$ ]] || exit 1
|
||||||
|
dir="releases/$release"
|
||||||
|
[[ -d "$dir/$public_path" ]] || exit 1
|
||||||
|
# SHARED_LINKS
|
||||||
|
printf '%s\n' "$revision" > "$dir/REVISION"
|
||||||
|
(
|
||||||
|
cd "$dir"
|
||||||
|
# ACTIVATE_HOOKS
|
||||||
|
)
|
||||||
|
active_release > .previous_release
|
||||||
|
switch_to "$dir"
|
||||||
|
;;
|
||||||
|
rollback)
|
||||||
|
target="$(cat .previous_release 2>/dev/null || true)"
|
||||||
|
if [[ -z "$target" ]]; then
|
||||||
|
echo 'Erstinstallation fehlgeschlagen; Zugriff gesperrt.' >&2
|
||||||
|
printf 'Require all denied\n' > .htaccess
|
||||||
|
[[ ! -L current ]] || rm current
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
switch_to "$target"
|
||||||
|
;;
|
||||||
|
cleanup)
|
||||||
|
active="$(active_release)"
|
||||||
|
for dir in releases/*; do
|
||||||
|
if [[ -d "$dir" && "$dir" != "$active" ]]; then
|
||||||
|
[[ "$dir" =~ ^releases/[a-zA-Z0-9_-]+$ && ! -L "$dir" ]] || exit 1
|
||||||
|
rm -rf "$dir"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
for archive in upload-*.tgz; do
|
||||||
|
[[ ! -f "$archive" ]] || rm -f "$archive"
|
||||||
|
done
|
||||||
|
;;
|
||||||
|
*) echo 'Unbekannter Remote-Befehl.' >&2; exit 1 ;;
|
||||||
|
esac
|
||||||
@@ -0,0 +1,213 @@
|
|||||||
|
import copy
|
||||||
|
import hashlib
|
||||||
|
import io
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
import tarfile
|
||||||
|
import tempfile
|
||||||
|
import unittest
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
from manager.cli import PipelineError, deploy, digest, inspect_package, load_config, prepare, render_remote
|
||||||
|
|
||||||
|
|
||||||
|
class ManagerTests(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.temp = tempfile.TemporaryDirectory()
|
||||||
|
self.addCleanup(self.temp.cleanup)
|
||||||
|
self.root = Path(self.temp.name)
|
||||||
|
self.config = {
|
||||||
|
"schema": 1, "project": "static-demo", "manager": "0.1.0",
|
||||||
|
"build": {"prepare": [], "test": [[sys.executable, "-c", "pass"]], "finalize": [],
|
||||||
|
"exclude": ["node_modules"], "required": ["web/index.html"]},
|
||||||
|
"remote": {"public": "web", "shared": {}, "backup": [], "activate": []},
|
||||||
|
"targets": {"staging": {"path": "test.example.de", "url": "https://example.test", "enabled": True,
|
||||||
|
"branch": "develop"},
|
||||||
|
"production": {"path": "prod.example.de", "url": "https://www.example.test", "enabled": False,
|
||||||
|
"branch": "main"}},
|
||||||
|
}
|
||||||
|
self.revision = "a" * 40
|
||||||
|
|
||||||
|
def package(self, changes=None, extra=None):
|
||||||
|
archive = self.root / "release.tgz"
|
||||||
|
metadata = {"schema": 1, "project": "static-demo", "revision": self.revision, "tests": "passed"}
|
||||||
|
metadata.update(changes or {})
|
||||||
|
with tarfile.open(archive, "w:gz") as package:
|
||||||
|
for name, value in {"PIPELINE.json": json.dumps(metadata).encode(), "web/index.html": b"ok", **(extra or {})}.items():
|
||||||
|
member = tarfile.TarInfo(name)
|
||||||
|
member.size = len(value)
|
||||||
|
package.addfile(member, io.BytesIO(value))
|
||||||
|
self.checksum(archive)
|
||||||
|
return archive
|
||||||
|
|
||||||
|
def checksum(self, archive):
|
||||||
|
Path(str(archive) + ".sha256").write_text(digest(archive))
|
||||||
|
|
||||||
|
def repository(self):
|
||||||
|
project = self.root / "source"
|
||||||
|
project.mkdir()
|
||||||
|
subprocess.run(["git", "init", "-b", "develop", str(project)], check=True, capture_output=True)
|
||||||
|
(project / "web").mkdir()
|
||||||
|
(project / "web/index.html").write_text("Versioned page")
|
||||||
|
(project / ".env.example").write_text("Example only")
|
||||||
|
subprocess.run(["git", "-C", str(project), "add", "."], check=True, capture_output=True)
|
||||||
|
subprocess.run(["git", "-C", str(project), "-c", "user.name=Test", "-c", "user.email=test@example.test",
|
||||||
|
"commit", "-m", "Fixture"], check=True, capture_output=True)
|
||||||
|
return project
|
||||||
|
|
||||||
|
def test_static_application_builds_without_laravel_hooks(self):
|
||||||
|
project = self.repository()
|
||||||
|
archive = self.root / "prepared.tgz"
|
||||||
|
with patch.dict(os.environ, {"DEPLOY_SSH_KEY": "must-not-reach-build"}):
|
||||||
|
self.config["build"]["test"] = [[sys.executable, "-c", "import os; assert 'DEPLOY_SSH_KEY' not in os.environ"]]
|
||||||
|
revision = prepare(project, self.config, archive)
|
||||||
|
self.assertEqual(digest(archive), inspect_package(archive, self.config, revision))
|
||||||
|
with tarfile.open(archive) as package:
|
||||||
|
self.assertEqual(b"Versioned page", package.extractfile("web/index.html").read())
|
||||||
|
self.assertNotIn(".env.example", package.getnames())
|
||||||
|
|
||||||
|
def test_failed_test_prevents_package_creation(self):
|
||||||
|
project = self.repository()
|
||||||
|
archive = self.root / "failed.tgz"
|
||||||
|
self.config["build"]["test"] = [[sys.executable, "-c", "raise SystemExit(7)"]]
|
||||||
|
with self.assertRaises(subprocess.CalledProcessError):
|
||||||
|
prepare(project, self.config, archive)
|
||||||
|
self.assertFalse(archive.exists())
|
||||||
|
|
||||||
|
def test_prepare_uses_committed_source_not_uncommitted_edits(self):
|
||||||
|
project = self.repository()
|
||||||
|
(project / "web/index.html").write_text("Uncommitted edit")
|
||||||
|
archive = self.root / "committed.tgz"
|
||||||
|
prepare(project, self.config, archive)
|
||||||
|
with tarfile.open(archive) as package:
|
||||||
|
self.assertEqual(b"Versioned page", package.extractfile("web/index.html").read())
|
||||||
|
|
||||||
|
def test_modified_archive_fails_integrity_validation(self):
|
||||||
|
archive = self.package()
|
||||||
|
with archive.open("ab") as output:
|
||||||
|
output.write(b"altered")
|
||||||
|
with self.assertRaisesRegex(PipelineError, "Prüfsumme"):
|
||||||
|
inspect_package(archive, self.config, self.revision)
|
||||||
|
|
||||||
|
def test_another_project_or_commit_cannot_be_deployed(self):
|
||||||
|
for change in ({"project": "other"}, {"revision": "b" * 40}, {"tests": "failed"}):
|
||||||
|
with self.subTest(change=change):
|
||||||
|
archive = self.package(change)
|
||||||
|
with self.assertRaisesRegex(PipelineError, "Commit"):
|
||||||
|
inspect_package(archive, self.config, self.revision)
|
||||||
|
|
||||||
|
def test_archive_cannot_contain_secrets_or_traversal(self):
|
||||||
|
for name in (".env", "nested/.env.production", "../outside", "/absolute", ".git/config"):
|
||||||
|
with self.subTest(name=name):
|
||||||
|
archive = self.package(extra={name: b"fixture"})
|
||||||
|
with self.assertRaises(PipelineError):
|
||||||
|
inspect_package(archive, self.config, self.revision)
|
||||||
|
|
||||||
|
def test_archive_cannot_contain_symlinks(self):
|
||||||
|
archive = self.root / "link.tgz"
|
||||||
|
with tarfile.open(archive, "w:gz") as package:
|
||||||
|
member = tarfile.TarInfo("escape")
|
||||||
|
member.type = tarfile.SYMTYPE
|
||||||
|
member.linkname = "/outside"
|
||||||
|
package.addfile(member)
|
||||||
|
self.checksum(archive)
|
||||||
|
with self.assertRaisesRegex(PipelineError, "Links"):
|
||||||
|
inspect_package(archive, self.config, self.revision)
|
||||||
|
|
||||||
|
def test_application_required_files_are_enforced(self):
|
||||||
|
archive = self.package()
|
||||||
|
self.config["build"]["required"].append("missing.html")
|
||||||
|
with self.assertRaisesRegex(PipelineError, "Pflichtdateien"):
|
||||||
|
inspect_package(archive, self.config, self.revision)
|
||||||
|
|
||||||
|
def test_remote_hooks_are_application_supplied_and_arguments_remain_quoted(self):
|
||||||
|
self.config["remote"]["activate"] = [["@php", "artisan", "migrate", "--force"], ["printf", "$(secret)"]]
|
||||||
|
remote = render_remote(self.config)
|
||||||
|
self.assertIn('"$php" artisan migrate --force', remote)
|
||||||
|
self.assertIn("printf '$(secret)'", remote)
|
||||||
|
self.assertIn("public_path=web", remote)
|
||||||
|
|
||||||
|
def test_disabled_production_stops_before_any_git_or_remote_call(self):
|
||||||
|
with patch("manager.cli.git") as git_call:
|
||||||
|
with self.assertRaisesRegex(PipelineError, "gesperrt"):
|
||||||
|
deploy(self.root, self.config, "production", self.root / "missing", self.root)
|
||||||
|
git_call.assert_not_called()
|
||||||
|
|
||||||
|
def test_wrong_branch_stops_before_remote_access(self):
|
||||||
|
with patch.dict(os.environ, {"GITHUB_REF": "refs/heads/main"}):
|
||||||
|
with self.assertRaisesRegex(PipelineError, "Branch"):
|
||||||
|
deploy(self.root, self.config, "staging", self.root / "missing", self.root)
|
||||||
|
|
||||||
|
def test_stale_queued_job_cannot_replace_a_newer_version(self):
|
||||||
|
with patch.dict(os.environ, {"GITHUB_REF": "refs/heads/develop", "GITHUB_SHA": self.revision}):
|
||||||
|
with patch("manager.cli.git", side_effect=[self.revision, "b" * 40 + "\trefs/heads/develop"]):
|
||||||
|
with self.assertRaisesRegex(PipelineError, "Veralteter"):
|
||||||
|
deploy(self.root, self.config, "staging", self.root / "missing", self.root)
|
||||||
|
|
||||||
|
def run_deployment(self, failure=None, healthy=True):
|
||||||
|
archive = self.package()
|
||||||
|
events = []
|
||||||
|
|
||||||
|
class FakeTransport:
|
||||||
|
def __init__(self, *arguments):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def call(self, command, *arguments, stdout=None):
|
||||||
|
events.append(command)
|
||||||
|
if command == failure:
|
||||||
|
raise PipelineError("Fixture failure")
|
||||||
|
if command == "backup":
|
||||||
|
with tarfile.open(fileobj=stdout, mode="w:gz") as saved:
|
||||||
|
item = tarfile.TarInfo("shared/.env")
|
||||||
|
item.size = 7
|
||||||
|
saved.addfile(item, io.BytesIO(b"fixture"))
|
||||||
|
|
||||||
|
def upload(self, *arguments):
|
||||||
|
events.append("upload")
|
||||||
|
|
||||||
|
self.events = events
|
||||||
|
with patch.dict(os.environ, {"GITHUB_REF": "refs/heads/develop", "GITHUB_SHA": self.revision,
|
||||||
|
"DEPLOY_SSH_KEY": "fixture", "DEPLOY_KNOWN_HOSTS": "fixture"}):
|
||||||
|
with patch("manager.cli.git", side_effect=[self.revision, self.revision + "\trefs/heads/develop"]):
|
||||||
|
with patch("manager.cli.health", return_value=healthy):
|
||||||
|
deploy(self.root, self.config, "staging", archive, self.root, FakeTransport)
|
||||||
|
return events
|
||||||
|
|
||||||
|
def test_backup_is_complete_before_upload_and_activation(self):
|
||||||
|
self.assertEqual(["lock", "backup", "cleanup", "upload", "receive", "activate", "cleanup", "unlock"], self.run_deployment())
|
||||||
|
backups = list((self.root / "static-demo/staging").glob("*.tar.gz"))
|
||||||
|
self.assertEqual(1, len(backups))
|
||||||
|
with tarfile.open(backups[0]) as backup:
|
||||||
|
self.assertIn("shared/.env", backup.getnames())
|
||||||
|
|
||||||
|
def test_backup_failure_prevents_upload_and_releases_lock(self):
|
||||||
|
with self.assertRaisesRegex(PipelineError, "Fixture"):
|
||||||
|
self.run_deployment(failure="backup")
|
||||||
|
self.assertEqual(["lock", "backup", "unlock"], self.events)
|
||||||
|
|
||||||
|
def test_existing_deployment_lock_is_never_removed_by_a_second_job(self):
|
||||||
|
with self.assertRaisesRegex(PipelineError, "Fixture"):
|
||||||
|
self.run_deployment(failure="lock")
|
||||||
|
self.assertEqual(["lock"], self.events)
|
||||||
|
|
||||||
|
def test_failed_health_check_rolls_back_before_releasing_lock(self):
|
||||||
|
with self.assertRaisesRegex(PipelineError, "Gesundheitscheck"):
|
||||||
|
self.run_deployment(healthy=False)
|
||||||
|
self.assertEqual(["rollback", "unlock"], self.events[-2:])
|
||||||
|
|
||||||
|
def test_configuration_rejects_unsafe_targets_and_incompatible_versions(self):
|
||||||
|
path = self.root / "pipeline.toml"
|
||||||
|
for directory in ("../outside", "/absolute", "x;command", "a//b"):
|
||||||
|
path.write_text(f'schema=1\nproject="demo"\nmanager="0.1.0"\n[build]\ntest=[["true"]]\n[targets.staging]\npath="{directory}"\nurl="https://example.test"\nenabled=true\n')
|
||||||
|
with self.subTest(directory=directory), self.assertRaises(PipelineError):
|
||||||
|
load_config(path)
|
||||||
|
path.write_text('schema=1\nproject="demo"\nmanager="9.0.0"\n')
|
||||||
|
with self.assertRaisesRegex(PipelineError, "Manager-Version"):
|
||||||
|
load_config(path)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
Reference in new issue
Block a user