Перенес основы lab3 в lab4

This commit is contained in:
Daria0w0
2026-11-06 14:44:30 +03:00
parent ec540a1a72
commit 9197e151ba
59 changed files with 4761 additions and 3 deletions
+38
View File
@@ -0,0 +1,38 @@
"""Explicit local Compose hooks. No credentials or session tokens are printed."""
import os
from pathlib import Path
import subprocess
import sys
ROOT = Path(__file__).resolve().parent.parent
def docker(*args, capture=False):
return subprocess.run(["docker", *args], cwd=ROOT, check=True,
text=True, capture_output=capture)
def main():
action = sys.argv[1]
if action == "restart":
# Stop all running application components before restarting shared stores.
docker("compose", "--env-file", ".env", "stop", "lb", "api", "redis", "postgres")
docker("compose", "--env-file", ".env", "up", "-d", "--wait", "--wait-timeout", "60")
elif action == "stop-one":
target = os.environ["TARGET_INSTANCE_ID"]
containers = docker("compose", "ps", "-q", "api", capture=True).stdout.split()
matches = []
for container in containers:
alias = docker("exec", container, "/server", "instance-id", capture=True).stdout.strip()
if alias == target:
matches.append(container)
if len(matches) != 1:
raise RuntimeError("Expected exactly one API container for target alias")
# One second grace keeps this explicit failure hook within the test budget.
docker("stop", "--time", "1", matches[0])
else:
raise ValueError("Unknown action")
if __name__ == "__main__":
main()
+35
View File
@@ -0,0 +1,35 @@
$ErrorActionPreference = 'Stop'
$repoRoot = Split-Path -Parent $PSScriptRoot
$envPath = Join-Path $repoRoot '.env'
if (-not (Test-Path -LiteralPath $envPath)) {
Copy-Item -LiteralPath (Join-Path $repoRoot '.env.example') -Destination $envPath
Write-Output 'Local .env created (non-secret session TTL).'
} else {
Write-Output 'Existing .env preserved.'
}
$secretsDirectory = Join-Path $repoRoot 'secrets'
$passwordPath = Join-Path $secretsDirectory 'postgres_password.txt'
New-Item -ItemType Directory -Path $secretsDirectory -Force | Out-Null
if (Test-Path -LiteralPath $passwordPath) {
if ((Get-Item -LiteralPath $passwordPath).Length -eq 0) {
throw 'Existing password file is empty. Supply a local password before starting PostgreSQL.'
}
Write-Output 'Existing PostgreSQL password preserved.'
exit 0
}
$randomBytes = New-Object byte[] 32
$generator = [System.Security.Cryptography.RandomNumberGenerator]::Create()
try {
$generator.GetBytes($randomBytes)
} finally {
$generator.Dispose()
}
$password = [Convert]::ToBase64String($randomBytes)
[System.IO.File]::WriteAllText($passwordPath, $password, [System.Text.Encoding]::ASCII)
Write-Output 'Local PostgreSQL password created in secrets/ (value not printed).'
+6
View File
@@ -0,0 +1,6 @@
@echo off
if defined LAB_PYTHON (
"%LAB_PYTHON%" -X utf8 "%~dp0compose_actions.py" restart
) else (
py -X utf8 "%~dp0compose_actions.py" restart
)
+4
View File
@@ -0,0 +1,4 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
exec python3 scripts/compose_actions.py restart
+6
View File
@@ -0,0 +1,6 @@
@echo off
if defined LAB_PYTHON (
"%LAB_PYTHON%" -X utf8 "%~dp0compose_actions.py" stop-one
) else (
py -X utf8 "%~dp0compose_actions.py" stop-one
)
+4
View File
@@ -0,0 +1,4 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
exec python3 scripts/compose_actions.py stop-one