forked from mtusi2026/mtusi2026lab5
Перенес изменения
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Generate local synthetic storage credentials without rotating existing keys."""
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import secrets
|
||||
|
||||
root=Path(__file__).resolve().parents[1]/'secrets'/'lab5'
|
||||
root.mkdir(parents=True,exist_ok=True)
|
||||
for name in ('s3_admin_access','s3_admin_secret','s3_api_access','s3_api_secret'):
|
||||
p=root/name
|
||||
if not p.exists():
|
||||
p.write_text(secrets.token_hex(24)+'\n')
|
||||
p.chmod(0o444) # Non-root containers must read these local synthetic secrets.
|
||||
bucket=os.environ.get('S3_BUCKET','messenger-attachments')
|
||||
identities=[]
|
||||
for role,actions in [('admin',['Admin','Read','Write','List','Tagging']),('api',['Read:'+bucket,'Write:'+bucket,'List:'+bucket])]:
|
||||
identities.append({'name':role,'credentials':[{'accessKey':(root/('s3_'+role+'_access')).read_text().strip(),'secretKey':(root/('s3_'+role+'_secret')).read_text().strip()}],'actions':actions})
|
||||
p=root/'s3.json'
|
||||
content=json.dumps({'identities':identities})+'\n'
|
||||
if not p.exists() or p.read_text()!=content:
|
||||
if p.exists(): p.chmod(0o600)
|
||||
p.write_text(content)
|
||||
p.chmod(0o444)
|
||||
print('Local storage credentials ready; existing keys preserved.')
|
||||
Executable
+23
@@ -0,0 +1,23 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
umask 077
|
||||
mkdir -p secrets/lab5
|
||||
if [ ! -f .env.lab5 ]; then
|
||||
{ printf 'POSTGRES_PASSWORD='; openssl rand -hex 24
|
||||
printf 'REDIS_PASSWORD='; openssl rand -hex 24
|
||||
printf 'HOST_PORT=18085\nAPI_REPLICAS=2\n'
|
||||
} > .env.lab5
|
||||
fi
|
||||
if [ ! -f secrets/lab5/private_key.pem ]; then
|
||||
openssl genrsa -out secrets/lab5/private_key.pem 2048 2>/dev/null
|
||||
fi
|
||||
if [ ! -f secrets/lab5/public_key.pem ]; then
|
||||
openssl rsa -in secrets/lab5/private_key.pem -pubout -out secrets/lab5/public_key.pem 2>/dev/null
|
||||
fi
|
||||
# Only synthetic stand keys, readable by the non-root application inside containers.
|
||||
chmod 755 secrets secrets/lab5
|
||||
chmod 444 secrets/lab5/*.pem
|
||||
printf '%s\n' 'Local lab5 settings and synthetic keys ready; existing files preserved.'
|
||||
|
||||
python3 scripts/bootstrap-storage.py
|
||||
Executable
+4
@@ -0,0 +1,4 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
exec docker compose --env-file .env.lab5 "$@"
|
||||
@@ -0,0 +1,17 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Convert a temporary generation view to the oapi-codegen 3.0 subset.
|
||||
Runtime handlers enforce anyOf and required fields; normative contract is untouched.
|
||||
"""
|
||||
import json
|
||||
from pathlib import Path
|
||||
s=json.loads(Path('contracts/openapi.json').read_text())
|
||||
for schema in s['components']['schemas'].values():
|
||||
schema.pop('anyOf',None)
|
||||
for prop in schema.get('properties',{}).values():
|
||||
if prop.get('type')==['string','null']:
|
||||
prop.update(type='string',nullable=True)
|
||||
# Preserve pointers so handlers can distinguish missing fields from empty values.
|
||||
for name,field in [('CreateUser','password'),('CreateMessage','text')]:
|
||||
s['components']['schemas'][name]['properties'][field]['x-go-type-skip-optional-pointer']=False
|
||||
s['components']['schemas'][name]['required'].remove(field)
|
||||
print(json.dumps(s))
|
||||
Executable
+7
@@ -0,0 +1,7 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
./scripts/compose.sh stop api lb
|
||||
./scripts/compose.sh restart postgres redis s3
|
||||
./scripts/compose.sh run --rm -T s3-init
|
||||
./scripts/compose.sh up -d --wait --wait-timeout 90 api lb
|
||||
@@ -0,0 +1,45 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Drain and replace API processes individually, retaining other container IDs."""
|
||||
import json,os,subprocess,time
|
||||
from pathlib import Path
|
||||
ROOT=Path(__file__).resolve().parents[1]
|
||||
def run(args,input=None):
|
||||
p=subprocess.run(args,cwd=ROOT,input=input,text=True,capture_output=True,timeout=100)
|
||||
if p.returncode:raise RuntimeError(p.stderr.strip()[:300])
|
||||
return p.stdout.strip()
|
||||
def runtime(lb,cmd):return run(['docker','exec','-i',lb,'nc','-w','1','127.0.0.1','9999'],cmd+'\n')
|
||||
def ids():return run(['./scripts/compose.sh','ps','-q','api']).split()
|
||||
def states(lb):
|
||||
lines=runtime(lb,'show servers state').splitlines();head=lines[1].lstrip('# ').split()
|
||||
return [dict(zip(head,l.split())) for l in lines[2:] if l.strip()]
|
||||
def main():
|
||||
initial=ids()
|
||||
if len(initial)<2:raise SystemExit('rolling update requires at least two running API replicas')
|
||||
lb=run(['./scripts/compose.sh','ps','-q','lb'])
|
||||
for old in initial:
|
||||
info=json.loads(run(['docker','inspect',old]))[0]
|
||||
ip=next(iter(info['NetworkSettings']['Networks'].values()))['IPAddress']
|
||||
backend=next(s['srv_name'] for s in states(lb) if s['srv_addr']==ip and s['be_name']=='api')
|
||||
runtime(lb,f'set server api/{backend} state drain')
|
||||
try:
|
||||
deadline=time.monotonic()+10
|
||||
while True:
|
||||
# Existing transactions get a chance to finish before SIGTERM.
|
||||
import csv,io
|
||||
raw=run(['docker','exec',lb,'wget','-qO-','http://127.0.0.1:8404/stats;csv'])
|
||||
row=next(r for r in csv.DictReader(io.StringIO(raw)) if r['# pxname']=='api' and r['svname']==backend)
|
||||
if int(row['scur'])==0:break
|
||||
if time.monotonic()>deadline:raise RuntimeError('backend did not drain')
|
||||
time.sleep(.1)
|
||||
before=set(ids());run(['docker','stop','--time','8',old]);run(['docker','rm',old])
|
||||
run(['./scripts/compose.sh','up','-d','--no-deps','--no-recreate','--scale',f'api={len(initial)}','api'])
|
||||
after=set(ids())
|
||||
if not (before-{old}).issubset(after):raise RuntimeError('another replica was replaced unexpectedly')
|
||||
new=(after-before).pop();deadline=time.monotonic()+60
|
||||
while run(['docker','inspect','--format','{{.State.Health.Status}}',new])!='healthy':
|
||||
if time.monotonic()>deadline:raise RuntimeError('replacement failed readiness')
|
||||
time.sleep(.5)
|
||||
print(json.dumps({'old_container':old,'new_container':new,'name':info['Name'],'drained_backend':backend}),flush=True)
|
||||
finally:runtime(lb,f'set server api/{backend} state ready')
|
||||
time.sleep(3) # allow DNS refresh and a successful active readiness check
|
||||
if __name__=='__main__':main()
|
||||
Executable
+4
@@ -0,0 +1,4 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
exec python3 scripts/rolling-update.py
|
||||
Executable
+17
@@ -0,0 +1,17 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
: "${TARGET_INSTANCE_ID:?missing target alias}"
|
||||
for id in $(./scripts/compose.sh ps -q api); do
|
||||
hostname=$(docker inspect --format '{{.Config.Hostname}}' "$id")
|
||||
case "$TARGET_INSTANCE_ID" in
|
||||
"$hostname"-*)
|
||||
current=$(docker exec "$id" wget -T 2 -qO- http://127.0.0.1:8080/internal/metrics | python3 -c 'import json,sys; print(json.load(sys.stdin)["instance_id"])')
|
||||
[ "$current" = "$TARGET_INSTANCE_ID" ] || { printf '%s\n' 'Target process was already replaced' >&2; exit 1; }
|
||||
docker stop --time 8 "$id" >/dev/null
|
||||
exit 0 ;;
|
||||
|
||||
esac
|
||||
done
|
||||
printf '%s\n' 'Target does not match a running lab5 API container' >&2
|
||||
exit 1
|
||||
Executable
+11
@@ -0,0 +1,11 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
mkdir -p bin
|
||||
(cd service && GOOS=linux GOARCH=$(docker info --format '{{.Architecture}}' | sed 's/aarch64/arm64/;s/x86_64/amd64/') CGO_ENABLED=0 go test -c -o ../bin/storage.test ./internal/adapter/repository/storage)
|
||||
database="lab5_atomic_$(date +%s)_$$"
|
||||
./scripts/compose.sh exec -T postgres psql -U messenger -d messenger -v ON_ERROR_STOP=1 -c "CREATE DATABASE $database" >/dev/null
|
||||
trap './scripts/compose.sh exec -T postgres psql -U messenger -d messenger -c "DROP DATABASE IF EXISTS $database WITH (FORCE)" >/dev/null' EXIT
|
||||
id=$(./scripts/compose.sh ps -q api | head -n 1)
|
||||
docker cp bin/storage.test "$id:/tmp/storage.test" >/dev/null
|
||||
docker exec -e "TEST_DATABASE_NAME=$database" "$id" /tmp/storage.test -test.v
|
||||
Reference in New Issue
Block a user