lab5 done
This commit is contained in:
+44
-14
@@ -10,13 +10,13 @@ import urllib.request
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
os.chdir(ROOT)
|
||||
STACK = os.environ.get('STACK_NAME', 'mtusi-lab4')
|
||||
API = os.environ.get('API_IMAGE', 'mtusi-messenger:lab4')
|
||||
MIGRATIONS = os.environ.get('MIGRATIONS_IMAGE', 'mtusi-migrations:lab4')
|
||||
TESTS = os.environ.get('TESTS_IMAGE', 'mtusi-messenger-tests:lab4')
|
||||
STACK = os.environ.get('STACK_NAME', 'mtusi-lab5')
|
||||
API = os.environ.get('API_IMAGE', 'mtusi-messenger:lab5')
|
||||
MIGRATIONS = os.environ.get('MIGRATIONS_IMAGE', 'mtusi-migrations:lab5')
|
||||
TESTS = os.environ.get('TESTS_IMAGE', 'mtusi-messenger-tests:lab5')
|
||||
S3 = os.environ.get('S3_IMAGE', 'mtusi-minio:RELEASE.2025-09-07T16-13-09Z')
|
||||
MC = os.environ.get('S3_CLIENT_IMAGE', 'mtusi-mc:RELEASE.2025-08-13T08-35-41Z')
|
||||
PORT = os.environ.get('PORT', '8080')
|
||||
PORT = os.environ.get('PORT', '18085')
|
||||
SECRETS = ('postgres_password', 'cursor_key', 's3_root_access_key',
|
||||
's3_root_secret_key', 's3_access_key', 's3_secret_key')
|
||||
|
||||
@@ -71,7 +71,7 @@ def job(kind, image, command, integration=False):
|
||||
# Fixed service name is a deployment lock: concurrent runners fail to create it.
|
||||
args = ['service', 'create', '--detach', '--no-resolve-image', '--name', name, '--mode', 'replicated-job',
|
||||
'--replicas', '1', '--restart-condition', 'none', '--network', STACK+'_data',
|
||||
'--constraint', 'node.labels.mtusi_lab4_storage==true',
|
||||
'--constraint', 'node.labels.mtusi_lab5_storage==true',
|
||||
'--limit-cpu', '1', '--limit-memory', '1g' if integration else '128m',
|
||||
'--secret', 'source='+STACK+'_postgres_password,target=postgres_password',
|
||||
'--env', 'POSTGRES_PASSWORD_FILE=/run/secrets/postgres_password']
|
||||
@@ -113,10 +113,10 @@ def bootstrap_s3():
|
||||
name = STACK+'_s3-bootstrap'
|
||||
args = ['service', 'create', '--detach', '--no-resolve-image', '--name', name, '--mode', 'replicated-job',
|
||||
'--replicas', '1', '--restart-condition', 'none', '--network', STACK+'_data',
|
||||
'--constraint', 'node.labels.mtusi_lab4_storage==true',
|
||||
'--constraint', 'node.labels.mtusi_lab5_storage==true',
|
||||
'--limit-cpu', '0.5', '--limit-memory', '512m', '--entrypoint', '/bin/sh',
|
||||
'--config', 'source='+STACK+'_s3_bootstrap,target=/bootstrap.sh',
|
||||
'--config', 'source='+STACK+'_s3_policy,target=/policy.json']
|
||||
'--config', 'source='+STACK+'_s3_policy_v5,target=/policy.json']
|
||||
for secret in SECRETS[2:]:
|
||||
args += ['--secret', 'source='+STACK+'_'+secret+',target='+secret]
|
||||
docker(*args, MC, '/bootstrap.sh')
|
||||
@@ -131,9 +131,18 @@ def reconcile():
|
||||
docker('exec', container, '/usr/local/bin/reconcile', *sys.argv[2:])
|
||||
|
||||
|
||||
def worker_cli(args):
|
||||
active=[t for t in tasks('worker') if t['Status']['State']=='running']
|
||||
if not active: raise RuntimeError('start worker before using its operator CLI')
|
||||
docker('exec',active[0]['Status']['ContainerStatus']['ContainerID'],'/usr/local/bin/worker',*args)
|
||||
|
||||
|
||||
def build():
|
||||
docker('build', '-f', 'Dockerfile.storage', '--target', 'minio', '-t', S3, '.')
|
||||
docker('build', '-f', 'Dockerfile.storage', '--target', 'mc', '-t', MC, '.')
|
||||
if os.environ.get('SKIP_STORAGE_BUILD') != '1':
|
||||
docker('build', '-f', 'Dockerfile.storage', '--target', 'minio', '-t', S3, '.')
|
||||
docker('build', '-f', 'Dockerfile.storage', '--target', 'mc', '-t', MC, '.')
|
||||
else:
|
||||
docker('image','inspect',S3,MC,capture=True)
|
||||
docker('build', '-t', API, '.')
|
||||
docker('build', '--target', 'migrations', '-t', MIGRATIONS, '.')
|
||||
|
||||
@@ -147,7 +156,7 @@ def up():
|
||||
raise RuntimeError('run on the Swarm manager')
|
||||
if len(docker('node', 'ls', '-q', capture=True).split()) != 1:
|
||||
raise RuntimeError('automatic lifecycle supports one Swarm node; see REPORT.md for image distribution')
|
||||
docker('node', 'update', '--label-add', 'mtusi_lab4_storage=true', swarm['NodeID'])
|
||||
docker('node', 'update', '--label-add', 'mtusi_lab5_storage=true', swarm['NodeID'])
|
||||
subprocess.run(['./scripts/bootstrap.sh'], check=True)
|
||||
for secret in SECRETS:
|
||||
known = docker('secret', 'ls', '--format', '{{.Name}}', capture=True).split()
|
||||
@@ -158,6 +167,13 @@ def up():
|
||||
# Upgrade the shared schema before replacing any running API image.
|
||||
fresh = STACK+'_api' not in existing
|
||||
if not fresh:
|
||||
# A maintenance barrier prevents old lab4 writers inserting ready-only rows
|
||||
# after the one-shot async backfill but before the new API image starts.
|
||||
docker('service','scale','--detach',STACK+'_api=0')
|
||||
deadline=time.monotonic()+45
|
||||
while docker('ps','--filter','label=com.docker.swarm.service.name='+STACK+'_api','-q',capture=True).strip():
|
||||
if time.monotonic()>deadline: raise RuntimeError('old API did not drain before migrations')
|
||||
time.sleep(.5)
|
||||
job('migrations', MIGRATIONS, ['up'])
|
||||
# Initial deployment keeps API at zero until the one-shot migration succeeds.
|
||||
env = dict(os.environ)
|
||||
@@ -176,12 +192,13 @@ def up():
|
||||
wait_api()
|
||||
else:
|
||||
update()
|
||||
wait_service('worker')
|
||||
print('PASS: Swarm stack ready at http://localhost:'+PORT)
|
||||
|
||||
|
||||
def restart():
|
||||
# Force replaces every task while retaining the same Swarm secrets and volumes.
|
||||
services = ('postgres', 'redis', 'minio', 'api', 'lb')
|
||||
services = ('postgres', 'redis', 'minio', 'api', 'lb', 'worker')
|
||||
previous = {name: {t['ID'] for t in tasks(name)} for name in services}
|
||||
for service in services:
|
||||
docker('service', 'update', '--detach', '--force', STACK+'_'+service)
|
||||
@@ -216,7 +233,9 @@ def update():
|
||||
state = inspect('service', STACK+'_api').get('UpdateStatus', {}).get('State')
|
||||
if state == 'completed' and previous.isdisjoint(t['ID'] for t in tasks('api')):
|
||||
wait_api()
|
||||
print('PASS: rolling update completed')
|
||||
docker('service','update','--detach','--force','--no-resolve-image','--image',API,STACK+'_worker')
|
||||
wait_service('worker')
|
||||
print('PASS: rolling update completed (API and worker image refreshed)')
|
||||
return
|
||||
if state in ('paused', 'rollback_started', 'rollback_paused', 'rollback_completed'):
|
||||
raise RuntimeError('rolling update: '+state)
|
||||
@@ -226,7 +245,7 @@ def update():
|
||||
|
||||
def status():
|
||||
result = {'stack': STACK, 'docker': docker('version','--format','{{.Server.Version}}',capture=True).strip(), 'services': {}}
|
||||
for name in ('lb','api','postgres','redis','minio'):
|
||||
for name in ('lb','api','worker','postgres','redis','minio'):
|
||||
service = inspect('service', STACK+'_'+name)
|
||||
spec = service['Spec']
|
||||
template = spec['TaskTemplate']
|
||||
@@ -248,6 +267,17 @@ def main():
|
||||
elif action == 'build': build()
|
||||
elif action == 'bootstrap-s3': bootstrap_s3()
|
||||
elif action == 'reconcile': reconcile()
|
||||
elif action == 'worker-stop':
|
||||
docker('service','scale','--detach',STACK+'_worker=0')
|
||||
deadline=time.monotonic()+30
|
||||
while docker('ps','--filter','label=com.docker.swarm.service.name='+STACK+'_worker','-q',capture=True).strip():
|
||||
if time.monotonic()>deadline: raise RuntimeError('worker did not stop')
|
||||
time.sleep(.2)
|
||||
elif action == 'worker-start':
|
||||
docker('service','scale','--detach',STACK+'_worker=1')
|
||||
wait_service('worker')
|
||||
elif action == 'failed-jobs': worker_cli(['--failed'])
|
||||
elif action == 'replay': worker_cli(sys.argv[2:])
|
||||
elif action == 'wait': wait_api()
|
||||
elif action == 'restart': restart()
|
||||
elif action == 'stop-one': stop_one()
|
||||
|
||||
Reference in New Issue
Block a user