Files
Investidor-V2/tools/portal-service-followup-bootstrap-clone.py
T
nala 287e991486
tests / ci (push) Waiting to run
oportunidades 7
2026-10-13 11:40:29 +02:00

25 lines
2.1 KiB
Python

from pathlib import Path
from datetime import datetime,timezone
import json,subprocess,os,re,shutil
R=Path(__file__).resolve().parent.parent;P=R/'storage/app/private/investmoz-v2/portal-audit'
plan=json.loads((P/'service-followup-latest.json').read_text());S=Path(plan['source'])
assert plan['target']['database']=='workflow_upgrade_20261011_155252_test' and plan['target']['port']==55441
shutil.copyfile(P/'service-followup-latest.json',P/'service-followup-services-environment-fixed.json')
shutil.copyfile(P/'service-followup-actors-private.json',P/'service-followup-services-actors-private.json')
shutil.copyfile(P/'service-followup-preflight-20261011.json',P/'service-followup-services-preflight-20261011.json')
stamp=datetime.now(timezone.utc).strftime('%Y%m%d_%H%M%S');name='workflow_upgrade_'+stamp+'_test'
proof=json.loads(Path(plan['backup_proof']).read_text());dump=proof['dump_path']
assert proof['restore_verified'] and Path(dump).is_file()
import hashlib
assert hashlib.sha256(Path(dump).read_bytes()).hexdigest()==proof['dump_sha256']
bin=Path('C:/Program Files/PostgreSQL/17/bin');env=dict(os.environ);env['PGPASSWORD']=''
def run(args):subprocess.run([str(bin/args[0]),*args[1:]],env=env,check=True,capture_output=True,timeout=300)
run(['createdb.exe','-h','127.0.0.1','-p','55441','-U','postgres',name])
run(['pg_restore.exe','-h','127.0.0.1','-p','55441','-U','postgres','-d',name,'--exit-on-error','--no-owner','--no-privileges',dump])
plan['target']['database']=name;plan['scope']='bootstrap pending-request UI delta only; previous services clone/proofs preserved'
plan['created_at']=datetime.now(timezone.utc).isoformat()
f=S/'.env';t=f.read_text();t=re.sub(r'^DB_DATABASE=.*$', 'DB_DATABASE='+name,t,flags=re.M);t=re.sub(r'^SESSION_COOKIE=.*$','SESSION_COOKIE=investmoz_service_bootstrap_'+stamp+'_qa',t,flags=re.M);f.write_text(t,encoding='utf-8')
(P/'service-followup-latest.json').write_text(json.dumps(plan,indent=2),encoding='utf-8')
(S/'upgrade-qa.json').write_text(json.dumps(plan,indent=2),encoding='utf-8')
print(json.dumps({'result':'fresh_bootstrap_clone_restored','target':plan['target'],'active_writes':0}))