|
|
|
@@ -0,0 +1,83 @@
|
|
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
"""One owner-authorized research-only acceptance; reviewer closes before inference."""
|
|
|
|
|
import hashlib,json,os,subprocess,sys,time,uuid,urllib.request
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
SHA='c8063b5f2afe420df8f920a52364ea73e3f2e513'
|
|
|
|
|
SESSION='v4-natural-c8063b5-20261024'
|
|
|
|
|
REQUEST='08c7a9de-4549-44f7-bb50-89fc77d558da'
|
|
|
|
|
ROOT=Path('/opt/services/canina/acceptance')/SESSION
|
|
|
|
|
|
|
|
|
|
def orchestrate(open_,approve,close,verify,launch):
|
|
|
|
|
try:
|
|
|
|
|
open_(); task=approve()
|
|
|
|
|
finally: close()
|
|
|
|
|
verify()
|
|
|
|
|
return launch(task)
|
|
|
|
|
|
|
|
|
|
def cmd(args):return subprocess.check_output(args,stderr=subprocess.DEVNULL,timeout=90).decode()
|
|
|
|
|
def docker(*args):return cmd(['docker','exec',*args])
|
|
|
|
|
class NoRedirect(urllib.request.HTTPRedirectHandler):
|
|
|
|
|
def redirect_request(self,*args,**kwargs):return None
|
|
|
|
|
def request(token,route,body=None):
|
|
|
|
|
opener=urllib.request.build_opener(urllib.request.ProxyHandler({}),NoRedirect())
|
|
|
|
|
req=urllib.request.Request('https://stageapi.canina.ir/api/'+route,data=None if body is None else json.dumps(body).encode(),headers={'Authorization':'Bearer '+token,'Content-Type':'application/json'})
|
|
|
|
|
with opener.open(req,timeout=15) as r:
|
|
|
|
|
data=r.read(262145)
|
|
|
|
|
if len(data)>262144:raise ValueError('response bound')
|
|
|
|
|
return json.loads(data)
|
|
|
|
|
def worker_token():return docker('--user','hermes','canina-hermes','cat','/opt/data/editorial-v4/credentials/editorial-pmo-token').strip()
|
|
|
|
|
def account(action):
|
|
|
|
|
return cmd(['env','CI=true','STAGE_TEST_APPROVAL=owner-authorized-stage-editorial','bash','/opt/services/canina/releases/'+SHA+'/scripts/stage-test-principal-host.sh',SHA,SESSION,'600',action])
|
|
|
|
|
def verify_cleanup():
|
|
|
|
|
code="const {PrismaClient}=require('@prisma/client');const p=new PrismaClient();p.user.count({where:{email:'"+EMAIL+"'}}).then(n=>{console.log(n);return p.$disconnect()})"
|
|
|
|
|
if docker('canino_backend_stage','node','-e',code).strip()!='0':raise ValueError('account remains')
|
|
|
|
|
if subprocess.run(['docker','exec','canino_backend_stage','test','!','-e','/app/private-stage-test/'+SESSION+'.json'],stdout=subprocess.DEVNULL).returncode:raise ValueError('handle remains')
|
|
|
|
|
def approve():
|
|
|
|
|
handle=json.loads(docker('canino_backend_stage','cat','/app/private-stage-test/'+SESSION+'.json'))
|
|
|
|
|
if handle['user']['role']!='SUPER_ADMIN':raise ValueError('independent reviewer required')
|
|
|
|
|
admin=handle['accessToken']; token=worker_token()
|
|
|
|
|
a=lambda r,b=None:request(admin,'admin/orchestration/'+r,b)
|
|
|
|
|
old=[a('tasks/'+t['id']) for t in a('tasks') if t.get('key','').startswith('editorial-v1:')]
|
|
|
|
|
failed=[t for t in old if t['state']=='FAILED']
|
|
|
|
|
if len(failed)!=4 or sum(x['amountCents'] for t in failed for x in t['budgetLedger'])!=80:raise ValueError('prior failed unknown80 fence')
|
|
|
|
|
ROOT.joinpath('prior.json').write_text(json.dumps({t['id']:t['budgetLedger'] for t in failed}))
|
|
|
|
|
if any(t['state'] not in ('FAILED','CANCELLED') for t in old):raise ValueError('existing active attempt')
|
|
|
|
|
proposal={'requestId':REQUEST,'researchTopic':'Canina pet supplement market research and evidence-grounded editorial opportunities','acceptanceCriteria':['Research to brief only; record real retrieved sources and explicit analytics/inventory gaps','Provide distinct scored editorial candidates; independent QA remains pending; no publication or Production authority'],'pilotKind':'editorial-research-to-brief-v1','operationMode':'PREPARE','environment':'staging','scope':'EDITORIAL_RESEARCH_TO_BRIEF','commitSha':SHA,'budgetLimitCents':20}
|
|
|
|
|
result=request(token,'internal/prepare-worker/editorial-proposals',proposal); task=result['taskId']; row=a('tasks/'+task)
|
|
|
|
|
if row['runs'] or row['budgetLedger']:raise ValueError('started task rejected')
|
|
|
|
|
approved=a('tasks/'+task+'/gates/1/decide',{'decision':'APPROVED','expectedVersion':row['version'],'idempotencyKey':SESSION+':gate1','expiresAt':time.strftime('%Y-%m-%dT%H:%M:%SZ',time.gmtime(time.time()+450)),'notes':'Owner authorized research-only functional bringup; independent Stage QA pending; no Production release.'})
|
|
|
|
|
if approved['state']!='READY':raise ValueError('READY required')
|
|
|
|
|
ROOT.joinpath('task.json').write_text(json.dumps({'taskId':task,'requestId':REQUEST,'commitSha':SHA}))
|
|
|
|
|
return task
|
|
|
|
|
# Read canonical fixed seed email from tracked module, never customer data.
|
|
|
|
|
EMAIL='stage-editorial-reviewer@ci.canina.invalid'
|
|
|
|
|
def launch(task):
|
|
|
|
|
unit=SESSION
|
|
|
|
|
subprocess.run(['systemd-run','--unit='+unit,'--property=Type=exec','--property=Restart=no','/usr/bin/python3',str(ROOT/'runner.py'),'--worker',task],check=True,stdout=subprocess.DEVNULL)
|
|
|
|
|
result={'status':'ASYNC_STARTED_NOT_COMPLETE','persistentJobHandle':unit+'.service','taskId':task,'commitSha':SHA,'cleanupAccountCount':0,'cleanupHandleAbsent':True,'automaticPaidRetry':False}
|
|
|
|
|
ROOT.joinpath('status.json').write_text(json.dumps(result));print(json.dumps(result))
|
|
|
|
|
def work(task):
|
|
|
|
|
code="import sys;sys.path.insert(0,'/opt/data/editorial-v4/runtime-package');import runner;from pathlib import Path;runner.run(Path('/opt/data'),sys.argv[1])"
|
|
|
|
|
rc=subprocess.run(['docker','exec','--user','hermes','canina-hermes','python3','-c',code,task],stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL).returncode
|
|
|
|
|
status=request(worker_token(),'internal/prepare-worker/editorial-proposals/'+task)
|
|
|
|
|
result={'taskId':task,'state':status['state'],'runnerExit':rc,'costStatus':'unknown-reservation-retained','qaStatus':status.get('qaStatus'),'automaticPaidRetry':False}
|
|
|
|
|
if status['state']=='VERIFYING' and status.get('qaStatus')=='PENDING_INDEPENDENT_QA':
|
|
|
|
|
artifact=status['artifact']; data=artifact['bytes'];binding=json.loads(data)
|
|
|
|
|
if hashlib.sha256(data.encode()).hexdigest()!=artifact['sha256'] or binding['taskId']!=task or binding['commitSha']!=SHA:raise ValueError('digest binding')
|
|
|
|
|
result['artifactSha256']=artifact['sha256']
|
|
|
|
|
ROOT.joinpath('result.json').write_text(json.dumps(result))
|
|
|
|
|
def main():
|
|
|
|
|
if os.getuid()!=0 or Path('/.dockerenv').exists():raise ValueError('host only')
|
|
|
|
|
if sys.argv[1:2]==['--worker']:work(sys.argv[2]);return
|
|
|
|
|
logs=Path('/opt/services/git/gitea/gitea/gitea/actions_log/parsa/canina')
|
|
|
|
|
if not any(SHA in (s:=p.read_text()) and 'Deployment for develop completed successfully!' in s and '🏁 Job succeeded' in s for p in logs.glob('*/*.log')):raise ValueError('exact CI success required')
|
|
|
|
|
if docker('canino_backend_stage','cat','/app/STAGE_TEST_ARTIFACT_SHA').strip()!=SHA or json.loads(docker('canina-hermes','cat','/opt/data/editorial-v4/deployment.json'))['commitSha']!=SHA:raise ValueError('deployment mismatch')
|
|
|
|
|
ROOT.mkdir(mode=0o700,parents=True,exist_ok=False)
|
|
|
|
|
source=sys.stdin.read()
|
|
|
|
|
if not source:raise ValueError('tracked persistent source required')
|
|
|
|
|
ROOT.joinpath('runner.py').write_text(source)
|
|
|
|
|
orchestrate(lambda:account('open'),approve,lambda:account('close'),verify_cleanup,launch)
|
|
|
|
|
if __name__=='__main__':
|
|
|
|
|
try:main()
|
|
|
|
|
except Exception:
|
|
|
|
|
print('Acceptance stopped; no automatic paid retry; inspect allowlisted status',file=sys.stderr);sys.exit(1)
|