ЛР 6: OpenTelemetry, Prometheus, Loki, Tempo и Grafana — на 3
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
$ErrorActionPreference = 'Stop'
|
||||
$root = Split-Path -Parent $PSScriptRoot
|
||||
$utf8 = [Text.UTF8Encoding]::new($false)
|
||||
$secretDir = Join-Path $root 'secrets'
|
||||
[void][IO.Directory]::CreateDirectory($secretDir)
|
||||
|
||||
# Compose bind-mounts these files via Docker Desktop. Keep the host ACL private.
|
||||
$sid = [Security.Principal.WindowsIdentity]::GetCurrent().User
|
||||
if (Test-Path -LiteralPath $secretDir -PathType Container) {
|
||||
$security = [Security.AccessControl.DirectorySecurity]::new()
|
||||
$security.SetOwner($sid)
|
||||
$security.SetAccessRuleProtection($true, $false)
|
||||
$inheritance = [Security.AccessControl.InheritanceFlags]'ContainerInherit, ObjectInherit'
|
||||
foreach ($principal in @($sid, [Security.Principal.SecurityIdentifier]::new('S-1-5-18'))) {
|
||||
$rule = [Security.AccessControl.FileSystemAccessRule]::new(
|
||||
$principal, 'FullControl', $inheritance, 'None', 'Allow'
|
||||
)
|
||||
$security.AddAccessRule($rule)
|
||||
}
|
||||
$current = Get-Acl -LiteralPath $secretDir
|
||||
$identityType = [Security.Principal.SecurityIdentifier]
|
||||
$principals = @($sid.Value, 'S-1-5-18') | Select-Object -Unique
|
||||
$rules = @($current.GetAccessRules($true, $true, $identityType))
|
||||
$matching = @($rules | Where-Object {
|
||||
$_.IdentityReference.Value -in $principals -and
|
||||
$_.FileSystemRights -eq 'FullControl' -and $_.AccessControlType -eq 'Allow' -and
|
||||
$_.InheritanceFlags -eq $inheritance -and $_.PropagationFlags -eq 'None' -and
|
||||
-not $_.IsInherited
|
||||
})
|
||||
$owner = $current.GetOwner($identityType)
|
||||
if (-not $current.AreAccessRulesProtected -or $owner.Value -ne $sid.Value -or
|
||||
$rules.Count -ne $principals.Count -or $matching.Count -ne $principals.Count -or
|
||||
@($matching.IdentityReference.Value | Select-Object -Unique).Count -ne $principals.Count) {
|
||||
Set-Acl -LiteralPath $secretDir -AclObject $security
|
||||
}
|
||||
}
|
||||
|
||||
function New-HexSecret([string]$name, [int]$length) {
|
||||
$file = Join-Path $secretDir $name
|
||||
if (-not (Test-Path -LiteralPath $file)) {
|
||||
$bytes = New-Object byte[] $length
|
||||
$rng = [Security.Cryptography.RandomNumberGenerator]::Create()
|
||||
try { $rng.GetBytes($bytes) } finally { $rng.Dispose() }
|
||||
$value = [BitConverter]::ToString($bytes).Replace('-', '').ToLowerInvariant()
|
||||
[IO.File]::WriteAllText($file, $value, $utf8)
|
||||
}
|
||||
}
|
||||
New-HexSecret 'broker_password' 32
|
||||
$brokerPassword = [IO.File]::ReadAllText((Join-Path $secretDir 'broker_password')).Trim()
|
||||
if ($brokerPassword -cnotmatch '^[a-f0-9]{64}$') { throw 'Invalid local RabbitMQ secret format.' }
|
||||
$brokerUrl = Join-Path $secretDir 'broker_url'
|
||||
$brokerExpected = "amqp://messenger:$brokerPassword@broker:5672/%2F"
|
||||
if (-not (Test-Path -LiteralPath $brokerUrl)) {
|
||||
[IO.File]::WriteAllText($brokerUrl, $brokerExpected, $utf8)
|
||||
}
|
||||
if ([IO.File]::ReadAllText($brokerUrl).Trim() -cne $brokerExpected) {
|
||||
throw 'Existing broker_url and broker_password disagree.'
|
||||
}
|
||||
New-HexSecret 'grafana_admin_password' 24
|
||||
New-HexSecret 'postgres_password' 32
|
||||
New-HexSecret 's3_access_key_id' 10
|
||||
New-HexSecret 's3_secret_access_key' 24
|
||||
|
||||
$password = [IO.File]::ReadAllText((Join-Path $secretDir 'postgres_password')).Trim()
|
||||
if ($password -cnotmatch '^[a-f0-9]{64}$') { throw 'Invalid local PostgreSQL secret format.' }
|
||||
$url = Join-Path $secretDir 'database_url'
|
||||
$expected = "postgresql://messenger:${password}@postgres:5432/messenger"
|
||||
if (-not (Test-Path -LiteralPath $url)) {
|
||||
[IO.File]::WriteAllText($url, $expected, $utf8)
|
||||
}
|
||||
if ([IO.File]::ReadAllText($url).Trim() -cne $expected) {
|
||||
throw 'Existing database_url and postgres_password disagree.'
|
||||
}
|
||||
|
||||
$envFile = Join-Path $root '.env'
|
||||
if (-not (Test-Path -LiteralPath $envFile)) {
|
||||
Copy-Item -LiteralPath (Join-Path $root '.env.example') -Destination $envFile
|
||||
}
|
||||
Write-Output 'Lab 6 local configuration ready.'
|
||||
Executable
+18
@@ -0,0 +1,18 @@
|
||||
#!/bin/sh
|
||||
# Docker socket actions are scoped to the explicitly named course stack.
|
||||
case "${LAB6_PROJECT:-}" in
|
||||
mtusi-lab6-grade3|mtusi-lab6-grade3-*) ;;
|
||||
*) echo 'LAB6_PROJECT must identify the Lab 6 test stack' >&2; exit 2 ;;
|
||||
esac
|
||||
|
||||
container() {
|
||||
ids=$(docker ps -aq --filter "label=com.docker.compose.project=$LAB6_PROJECT" \
|
||||
--filter "label=com.docker.compose.service=$1" \
|
||||
--filter 'label=com.docker.compose.oneoff=False')
|
||||
set -- $ids
|
||||
if [ "$#" -ne 1 ]; then
|
||||
echo 'Expected one Lab 6 service container' >&2
|
||||
return 2
|
||||
fi
|
||||
printf '%s\n' "$1"
|
||||
}
|
||||
Executable
+20
@@ -0,0 +1,20 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
. "$(dirname "$0")/lab-containers.sh"
|
||||
broker=$(container broker)
|
||||
docker stop --time 3 "$(container worker)"
|
||||
docker restart --time 4 "$(container postgres)" "$(container s3)" "$broker"
|
||||
docker restart --time 4 "$(container api1)" "$(container api2)" "$(container lb)"
|
||||
|
||||
# API readiness covers PostgreSQL and S3. Let AMQP recover before returning.
|
||||
remaining=55
|
||||
while [ "$(docker inspect --format '{{.State.Health.Status}}' "$broker")" != healthy ]; do
|
||||
remaining=$((remaining - 1))
|
||||
if [ "$remaining" -le 0 ]; then
|
||||
echo 'RabbitMQ did not recover after restart' >&2
|
||||
exit 1
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
docker start "$(container worker)"
|
||||
docker restart --time 10 "$(container prometheus)" "$(container loki)" "$(container tempo)" "$(container grafana)" "$(container otel-collector)"
|
||||
Executable
+9
@@ -0,0 +1,9 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
case "${TARGET_INSTANCE_ID:-}" in
|
||||
api1|api2) ;;
|
||||
*) echo 'TARGET_INSTANCE_ID must be api1 or api2' >&2; exit 2 ;;
|
||||
esac
|
||||
. "$(dirname "$0")/lab-containers.sh"
|
||||
id=$(container "$TARGET_INSTANCE_ID")
|
||||
docker stop --time 4 "$id"
|
||||
@@ -0,0 +1,39 @@
|
||||
$ErrorActionPreference = 'Stop'
|
||||
Push-Location (Split-Path -Parent $PSScriptRoot)
|
||||
try {
|
||||
$project = 'mtusi-lab6-grade3'
|
||||
$api = @('api1', 'api2')
|
||||
$tested = @{}
|
||||
for ($attempt = 1; $attempt -le 8 -and $tested.Count -lt 2; $attempt++) {
|
||||
Write-Output "Public failover attempt $attempt"
|
||||
docker compose -p $project start --wait --wait-timeout 60 api1 api2
|
||||
if ($LASTEXITCODE) { throw 'Could not restore both replicas.' }
|
||||
# An odd number of GETs changes the initial round-robin position
|
||||
# on even attempts. The unmodified public test still chooses the target.
|
||||
if ($attempt % 2 -eq 0) {
|
||||
docker compose -p $project exec -T lb wget -q -O /dev/null http://127.0.0.1:8080/health/live
|
||||
if ($LASTEXITCODE) { throw 'HAProxy is not ready.' }
|
||||
}
|
||||
try {
|
||||
docker compose -p $project run --rm --no-deps tools make test-failover BASE_URL=http://lb:8080 ACTION_SCRIPT=scripts/stop-one.sh
|
||||
if ($LASTEXITCODE) { throw 'Public failover test failed.' }
|
||||
$stopped = @(docker compose -p $project ps --status exited --services api1 api2)
|
||||
if ($LASTEXITCODE -or $stopped.Count -ne 1 -or $stopped[0] -notin $api) {
|
||||
throw 'Expected exactly one stopped API replica.'
|
||||
}
|
||||
$running = @(docker compose -p $project ps --status running --services api1 api2)
|
||||
if ($LASTEXITCODE -or $running.Count -ne 1 -or $running[0] -notin $api) {
|
||||
throw 'Expected exactly one surviving API replica.'
|
||||
}
|
||||
$tested[$stopped[0]] = $true
|
||||
Write-Output "Verified stopped=$($stopped[0]) survivor=$($running[0])"
|
||||
} finally {
|
||||
docker compose -p $project start --wait --wait-timeout 60 api1 api2
|
||||
if ($LASTEXITCODE) { throw 'Could not restore both replicas after failover.' }
|
||||
}
|
||||
}
|
||||
if ($tested.Count -ne 2) { throw 'The public test did not select both API replicas.' }
|
||||
Write-Output 'PASS: public failover for api1 and api2, both replicas recovered.'
|
||||
} finally {
|
||||
Pop-Location
|
||||
}
|
||||
Reference in New Issue
Block a user