#!/bin/bash # Sourced by the CI/CD deploy entrypoint, never a standalone deploy command. # Invoked only by the authenticated Stage CI entrypoint under its rollout lock. stage_runtime_provision() { [ "${GITHUB_ACTIONS:-${GITEA_ACTIONS:-}}" = true ] [ "${STAGE_PROVISION_ENABLED:-}" = true ] export DEPLOY_SHA STAGE_PROVISION_APPROVED_SHA="$DEPLOY_SHA" CI=true python3 - <<'PY' import os, json, subprocess, importlib.util, resource, secrets, re, stat from pathlib import Path resource.setrlimit(resource.RLIMIT_AS, (1536*1024*1024, 1536*1024*1024)) resource.setrlimit(resource.RLIMIT_CPU, (60, 60)) resource.setrlimit(resource.RLIMIT_FSIZE, (64*1024*1024, 64*1024*1024)) def docker(*args): return subprocess.check_output(['docker', *args], text=True, timeout=45) spec=importlib.util.spec_from_file_location('provision','scripts/provision-stage-runtime.py') m=importlib.util.module_from_spec(spec);spec.loader.exec_module(m) m.ci_gate() def inspect(name): return json.loads(docker('inspect',name))[0] b=inspect('canino_backend_stage') if not b['State'].get('Running') or b['State'].get('Pid',0) <= 0: raise ValueError('Running backend required') if b['Config']['User'] not in ('node','1000','1000:1000') or b['HostConfig'].get('UsernsMode') not in ('','default'): raise ValueError('Unreviewed runtime identity/userns') def mapped(kind): for row in Path('/proc/%s/%s_map'%(b['State']['Pid'],kind)).read_text().splitlines(): inside,outside,count=map(int,row.split()) if inside <= 1000 < inside+count: return outside+1000-inside raise ValueError('Unmapped runtime identity') uid,gid=mapped('uid'),mapped('gid') if min(uid,gid)<=0: raise ValueError('Root identity forbidden') h=inspect('canina-hermes') env=dict(e.split('=',1) for e in h['Config']['Env'] if '=' in e) home=Path(env.get('HERMES_HOME','')) if not home.is_absolute() or '..' in home.parts: raise ValueError('Explicit Hermes home required') mounts=[v for v in h['Mounts'] if home==Path(v['Destination']) or Path(v['Destination']) in home.parents] if not mounts: raise ValueError('Home mount required') mount=max(mounts,key=lambda v:len(v['Destination'])) if mount.get('Type') not in ('bind','volume'): raise ValueError('Filesystem home mount required') m.safe_path(Path(mount['Source'])) db=Path(mount['Source'])/home.relative_to(mount['Destination'])/'state.db' m.safe_path(db.parent) for p in [db,Path(str(db)+'-wal'),Path(str(db)+'-shm')]: if p.exists() or p.is_symlink(): st=p.lstat() if not stat.S_ISREG(st.st_mode) or st.st_nlink != 1: raise ValueError('Unsafe SQLite source') size=sum(p.stat().st_size for p in [db,Path(str(db)+'-wal')] if p.exists()) if not 0{try{const rows=await p.$transaction(async tx=>{ await tx.$executeRawUnsafe('SET TRANSACTION READ ONLY'); return tx.$queryRawUnsafe(\"SELECT id::text FROM users WHERE lower(replace(replace(role::text, '_', ''), '-', '')) IN ('admin','superadmin') ORDER BY id LIMIT 1001\"); });process.stdout.write(JSON.stringify(rows));}catch{process.exitCode=1;}finally{await p.$disconnect();}})();""" rows=json.loads(docker('exec','-w','/app','canino_backend_stage','node','-e',query)) if not 1<=len(rows)<=1000 or any(not isinstance(r,dict) or set(r)!={'id'} or not isinstance(r['id'],str) or not re.fullmatch('[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}',r['id']) for r in rows): raise ValueError('Verified admin subjects required') m.provision(root,uid,gid) # Leave the validated deny-all sentinel unchanged for repeat provisioning. capture=os.environ['DEPLOY_SHA']+'-'+secrets.token_hex(8) dest=root/'snapshots'/capture m.export_snapshot(db,dest,'default',uid,gid) grants=root/'snapshots'/(capture+'-grants.json') m.write_private(grants,json.dumps({r['id']:['default'] for r in rows}),uid,gid) Path('.stage-runtime.json').write_text(json.dumps({'snapshot':str(dest),'grants':str(grants)})) print('Stage projected snapshot captured; existing admin grants verified; no executor started') PY } stage_runtime_render() { export STAGE_RUNTIME_SERVE_FILE="$TARGET_DIR/infra/hermes-runtime/serve.py" export STAGE_RUNTIME_SNAPSHOT STAGE_RUNTIME_SNAPSHOT=$(python3 -c 'import json;print(json.load(open(".stage-runtime.json"))["snapshot"])') command docker compose -p stage --env-file /dev/null --project-directory "$TARGET_DIR" \ -f "$TARGET_DIR/.stage-compose.json" -f infra/hermes-runtime/compose.stage-provision.yml \ --profile stage-runtime-inventory config --format json > .stage-runtime-merged.json python3 - <<'PY' import json from pathlib import Path p=Path('.stage-runtime-merged.json');c=json.loads(p.read_text()) b=c['services']['backend_stage'] b.setdefault('environment',{})['CANINA_RUNTIME_READ_ENABLED']='true' b.setdefault('networks',{})['traefik_public']=None b.setdefault('labels',{})['traefik.docker.network']='traefik_public' c['secrets']['hermes_runtime_grants']['file']=json.loads(Path('.stage-runtime.json').read_text())['grants'] assert c['networks']['hermes_inventory_backend']['internal'] is True assert c['networks']['traefik_public']['external'] is True assert set(c['services'])=={'db_stage','redis_stage','frontend_stage','backend_stage','runtime-inventory'} Path('.stage-compose.json').write_text(json.dumps(c));p.unlink() PY } stage_rollout() { # Maintenance is deliberate: neither old nor new app serves during DDL. docker compose stop frontend_stage backend_stage docker compose run --rm --no-deps --entrypoint ./node_modules/.bin/prisma backend_stage migrate deploy if [ "${STAGE_PROVISION_ENABLED:-false}" = true ]; then docker compose up -d --no-deps runtime-inventory fi docker compose up -d --no-deps backend_stage if ! docker compose exec -T backend_stage node -e "$(< scripts/rollout-health.cjs)"; then docker compose stop frontend_stage backend_stage echo 'ERROR: backend acceptance failed; maintenance retained' >&2 return 1 fi docker compose up -d --no-deps frontend_stage }