create-deploy-user.py: deploy_host aus Server-Mapping (H1/H2) setzen und deploy.yml direkt im Repo anlegen
This commit is contained in:
1 parent
a5a3fab167
commit
95ca0108a0
2 files changed
+45
-2
No files matched your search
+42
-1
@@ -10,6 +10,7 @@
|
|||||||
die Zugangsdaten als Gitea-Actions-Secrets/-Variablen."""
|
die Zugangsdaten als Gitea-Actions-Secrets/-Variablen."""
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
|
import base64
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
@@ -24,6 +25,12 @@ from authentik_client.rest import ApiException
|
|||||||
DEFAULT_AUTHENTIK_URL = "https://auth.share-fix.de/api/v3"
|
DEFAULT_AUTHENTIK_URL = "https://auth.share-fix.de/api/v3"
|
||||||
DEFAULT_GITEA_URL = "https://git.share-fix.de"
|
DEFAULT_GITEA_URL = "https://git.share-fix.de"
|
||||||
DEFAULT_SERVER = "H1"
|
DEFAULT_SERVER = "H1"
|
||||||
|
SERVER_HOSTS = {
|
||||||
|
"H1": "host-1.futcloud.de",
|
||||||
|
"H2": "host-2.futcloud.de",
|
||||||
|
}
|
||||||
|
WORKFLOW_SOURCE = Path(__file__).resolve().parent / "examples" / "deploy.yml"
|
||||||
|
WORKFLOW_TARGET = ".gitea/workflows/deploy.yml"
|
||||||
HOME_DIR_BASE = "/home"
|
HOME_DIR_BASE = "/home"
|
||||||
USERNAME_RE = re.compile(r"^[a-z_][a-z0-9_-]{0,31}$")
|
USERNAME_RE = re.compile(r"^[a-z_][a-z0-9_-]{0,31}$")
|
||||||
|
|
||||||
@@ -58,6 +65,22 @@ class Gitea:
|
|||||||
raise
|
raise
|
||||||
self._request("POST", url, json={"value": value})
|
self._request("POST", url, json={"value": value})
|
||||||
|
|
||||||
|
def create_file(self, repo_path: str, file_path: str, content: str, message: str) -> bool:
|
||||||
|
"""Legt eine Datei im Default-Branch an. False, wenn sie bereits existiert."""
|
||||||
|
url = f"/repos/{repo_path}/contents/{file_path}"
|
||||||
|
try:
|
||||||
|
self._request("GET", url)
|
||||||
|
return False
|
||||||
|
except requests.HTTPError as e:
|
||||||
|
if e.response is None or e.response.status_code != 404:
|
||||||
|
raise
|
||||||
|
print(f"Creating {file_path} in {repo_path}")
|
||||||
|
self._request("POST", url, json={
|
||||||
|
"content": base64.b64encode(content.encode()).decode(),
|
||||||
|
"message": message,
|
||||||
|
})
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
def normalize_repo_path(raw: str, gitea_url: str) -> str:
|
def normalize_repo_path(raw: str, gitea_url: str) -> str:
|
||||||
"""Akzeptiert 'owner/repo', Web-URLs und Clone-URLs (mit/ohne .git)."""
|
"""Akzeptiert 'owner/repo', Web-URLs und Clone-URLs (mit/ohne .git)."""
|
||||||
@@ -141,6 +164,11 @@ def parse_args() -> argparse.Namespace:
|
|||||||
p.add_argument("--docker-group",
|
p.add_argument("--docker-group",
|
||||||
help="Gruppe für Docker-Deploys (Standard: server_<SERVER>_docker_compose)")
|
help="Gruppe für Docker-Deploys (Standard: server_<SERVER>_docker_compose)")
|
||||||
p.add_argument("--no-docker-group", action="store_true", help="Docker-Deploy-Gruppe nicht zuweisen")
|
p.add_argument("--no-docker-group", action="store_true", help="Docker-Deploy-Gruppe nicht zuweisen")
|
||||||
|
p.add_argument("--deploy-host",
|
||||||
|
help="Hostname des Zielservers für die Variable deploy_host "
|
||||||
|
f"(Standard: Mapping {', '.join(f'{k}→{v}' for k, v in SERVER_HOSTS.items())})")
|
||||||
|
p.add_argument("--no-workflow", action="store_true",
|
||||||
|
help=f"{WORKFLOW_TARGET} nicht im Repo anlegen")
|
||||||
p.add_argument("--authentik-url", default=DEFAULT_AUTHENTIK_URL)
|
p.add_argument("--authentik-url", default=DEFAULT_AUTHENTIK_URL)
|
||||||
p.add_argument("--authentik-token", default=os.environ.get("AUTHENTIK_TOKEN"),
|
p.add_argument("--authentik-token", default=os.environ.get("AUTHENTIK_TOKEN"),
|
||||||
help="Bearer-Token für die authentik-API (oder Env AUTHENTIK_TOKEN)")
|
help="Bearer-Token für die authentik-API (oder Env AUTHENTIK_TOKEN)")
|
||||||
@@ -176,6 +204,9 @@ def main() -> int:
|
|||||||
|
|
||||||
if not re.fullmatch(r"[A-Za-z0-9_-]+", args.server):
|
if not re.fullmatch(r"[A-Za-z0-9_-]+", args.server):
|
||||||
sys.exit(f"Ungültiger Servername: '{args.server}'")
|
sys.exit(f"Ungültiger Servername: '{args.server}'")
|
||||||
|
deploy_host = args.deploy_host or SERVER_HOSTS.get(args.server)
|
||||||
|
if not deploy_host:
|
||||||
|
sys.exit(f"Kein Host für Server '{args.server}' bekannt (SERVER_HOSTS: {', '.join(SERVER_HOSTS)}); --deploy-host angeben")
|
||||||
if not args.dry_run and not args.authentik_token:
|
if not args.dry_run and not args.authentik_token:
|
||||||
print(f"Authentik-API-Token erstellen unter {args.authentik_url.split('/api')[0]}/if/user/#/settings;page=page-tokens")
|
print(f"Authentik-API-Token erstellen unter {args.authentik_url.split('/api')[0]}/if/user/#/settings;page=page-tokens")
|
||||||
args.authentik_token = getpass("Enter authentik token: ")
|
args.authentik_token = getpass("Enter authentik token: ")
|
||||||
@@ -204,7 +235,10 @@ def main() -> int:
|
|||||||
print(f"Home-Verz.: {home_dir}")
|
print(f"Home-Verz.: {home_dir}")
|
||||||
print(f"Gruppen: {', '.join(group_names)}")
|
print(f"Gruppen: {', '.join(group_names)}")
|
||||||
print(f"Notes: {repo['html_url']}")
|
print(f"Notes: {repo['html_url']}")
|
||||||
print(f"Gitea-Secrets: deploy_user, deploy_pass")
|
print("Gitea-Secrets: deploy_user, deploy_pass")
|
||||||
|
print(f"Gitea-Variable: deploy_host={deploy_host}")
|
||||||
|
if not args.no_workflow:
|
||||||
|
print(f"Workflow: {WORKFLOW_TARGET} (direkt im Default-Branch, falls nicht vorhanden)")
|
||||||
if args.dry_run:
|
if args.dry_run:
|
||||||
print("\n[dry-run] Es wurde nichts angelegt.")
|
print("\n[dry-run] Es wurde nichts angelegt.")
|
||||||
return 0
|
return 0
|
||||||
@@ -222,7 +256,14 @@ def main() -> int:
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
gitea.set_variable(repo_path, "deploy_user", username)
|
gitea.set_variable(repo_path, "deploy_user", username)
|
||||||
|
gitea.set_variable(repo_path, "deploy_host", deploy_host)
|
||||||
gitea.set_secret(repo_path, "deploy_pass", key)
|
gitea.set_secret(repo_path, "deploy_pass", key)
|
||||||
|
# Zuletzt: der Commit löst den ersten Deploy aus, dafür muss alles gesetzt sein
|
||||||
|
if not args.no_workflow:
|
||||||
|
created = gitea.create_file(repo_path, WORKFLOW_TARGET, WORKFLOW_SOURCE.read_text(),
|
||||||
|
"Deploy-Workflow hinzugefügt")
|
||||||
|
if not created:
|
||||||
|
print(f"{WORKFLOW_TARGET} existiert bereits, übersprungen")
|
||||||
except requests.RequestException as e:
|
except requests.RequestException as e:
|
||||||
sys.exit(f"Fehler in Gitea: {e}")
|
sys.exit(f"Fehler in Gitea: {e}")
|
||||||
|
|
||||||
|
|||||||
+3
-1
@@ -6,7 +6,9 @@
|
|||||||
# Voraussetzungen (Setup mit create-deploy-user.py --project-path owner/repo):
|
# Voraussetzungen (Setup mit create-deploy-user.py --project-path owner/repo):
|
||||||
# Variable deploy_user - Deploy-User (wird vom Skript gesetzt)
|
# Variable deploy_user - Deploy-User (wird vom Skript gesetzt)
|
||||||
# Secret deploy_pass - Passwort/App-Token des Users (wird vom Skript gesetzt)
|
# Secret deploy_pass - Passwort/App-Token des Users (wird vom Skript gesetzt)
|
||||||
# Variable deploy_host - Hostname/IP des Zielservers (manuell setzen)
|
# Variable deploy_host - Hostname des Zielservers (wird vom Skript aus --server gesetzt)
|
||||||
|
#
|
||||||
|
# Das Skript legt diese Datei auch selbst als .gitea/workflows/deploy.yml im Repo an.
|
||||||
#
|
#
|
||||||
# Das Repo enthält docker-compose.yml (und optional docker-compose.override.yml)
|
# Das Repo enthält docker-compose.yml (und optional docker-compose.override.yml)
|
||||||
# im Wurzelverzeichnis; sie landen in ~/deployment auf dem Server.
|
# im Wurzelverzeichnis; sie landen in ~/deployment auf dem Server.
|
||||||
|
|||||||
Reference in new issue
Block a user