import{mkdir,readFile,writeFile}from'node:fs/promises'; import{resolve}from'node:path'; import{dashboardMatches,rulesMatch}from'../scripts/lib/grafana-provisioning'; if(process.argv.includes('--check')){console.log('Grafana metadata acceptance validated without credentials or network');process.exit(0)} if(process.platform!=='linux')throw Error('Run native Grafana acceptance on the owned Linux host'); const root=resolve(import.meta.dir,'..'),out=resolve(root,'.local/grafana-native-'+crypto.randomUUID()); const expectedDashboard=await Bun.file(resolve(root,'config/grafana/dashboards/messenger.json')).json(); const alertConfig:any=Bun.YAML.parse(await Bun.file(resolve(root,'config/grafana/provisioning/alerting/rules.yaml')).text()); const expectedRules=(alertConfig.groups??[]).flatMap((g:any)=>(g.rules??[]).map((r:any)=>r.uid)); await mkdir(out,{mode:0o700}); const password=(await readFile(resolve(root,'.local/grafana_password'),'utf8')).trim(),auth='Basic '+Buffer.from('admin:'+password).toString('base64'),checks=[]; for(const[name,path]of[['server','/api/health'],['metrics-source','/api/datasources/uid/prometheus/health'],['plugin-source','/api/datasources/uid/openobserve/health'],['provisioned-dashboard','/api/dashboards/uid/messenger'],['rules','/api/v1/provisioning/alert-rules']]){ let response:Response|undefined; for(let attempt=0;attempt<60;attempt++){try{response=await fetch('http://127.0.0.1:28306'+path,{headers:{Authorization:auth},signal:AbortSignal.timeout(5000)});break}catch{await Bun.sleep(500)}} if(!response)throw Error('Native Grafana unavailable at '+name); const parts:Uint8Array[]=[];let length=0; for await(const part of response.body??[]){length+=part.length;if(length>2097152)throw Error('Grafana metadata response bound');parts.push(part)} const raw=Buffer.concat(parts);await writeFile(resolve(out,name+'.private.json'),raw,{flag:'wx',mode:0o600}); let body:any;try{body=JSON.parse(raw.toString())}catch{} const contentMatches=name==='server'?body?.version==='13.3.1'&&body.database==='ok':name.endsWith('source')?body?.status==='OK':name==='provisioned-dashboard'?dashboardMatches(body,expectedDashboard):rulesMatch(body,expectedRules); checks.push({name,http:response.status,...(name==='server'?{version:body?.version,database:body?.database}:{}),...(name.endsWith('source')?{status:body?.status}:{}),...(name==='provisioned-dashboard'?{uid:body?.dashboard?.uid,panels:body?.dashboard?.panels?.length}:{}),...(name==='rules'?{count:Array.isArray(body)?body.length:null,uids:Array.isArray(body)?body.map(r=>r.uid):[]}:{}),contentMatches,pass:response.status===200&&contentMatches}); } const result={checkedAt:new Date().toISOString(),checks,expectedDashboardUID:expectedDashboard.uid,expectedPanelIDs:expectedDashboard.panels.map((p:any)=>p.id),expectedRuleUIDs:expectedRules,passed:checks.every(c=>c.pass),out}; await writeFile(resolve(out,'RESULT.json'),JSON.stringify(result,null,2)+'\n',{flag:'wx',mode:0o600});console.log(JSON.stringify(result));if(!result.passed)process.exitCode=1;