fix: preserve signed terminal nonstarts across repeated and restarted sync
Rust development slice / linux (push) Failing after 6s
Rust development slice / linux (push) Failing after 6s
This commit is contained in:
@@ -136,3 +136,70 @@ def vm_attempt(payload):
|
||||
else:
|
||||
result[name] = dict(consumed=[dict(r) for r in db.execute('SELECT grant_id,agent,generation,result IS NOT NULL AS result_present FROM consumed WHERE job=? AND attempt=?',(job,attempt))])
|
||||
return result
|
||||
|
||||
|
||||
# Retain the original stored-checkpoint reader entry points.
|
||||
def vm_chain(payload):
|
||||
import sqlite3
|
||||
def wire(raw):
|
||||
result={};i=0
|
||||
def number():
|
||||
nonlocal i
|
||||
value=0;shift=0
|
||||
while True:
|
||||
require(i<len(raw) and shift<70,'stored protobuf bounds');b=raw[i];i+=1;value|=(b&127)<<shift
|
||||
if not b&128:return value
|
||||
shift+=7
|
||||
while i<len(raw):
|
||||
key=number();field=key>>3;kind=key&7
|
||||
if kind==0:value=number()
|
||||
elif kind==2:
|
||||
length=number();require(i+length<=len(raw),'stored protobuf length');value=raw[i:i+length];i+=length
|
||||
else:raise RuntimeError('stored protobuf wire type')
|
||||
result.setdefault(field,[]).append(value)
|
||||
return result
|
||||
def request(raw):
|
||||
fields=wire(raw);result={}
|
||||
for n,k in ((1,'request_id'),(2,'journal_id'),(4,'previous_decision_id'),(9,'helper_journal_id'),(11,'helper_previous_decision_id'),(12,'previous_request_id')):
|
||||
result[k]=fields.get(n,[b''])[0].decode('ascii')
|
||||
for n,k in ((3,'checkpoint'),(10,'helper_checkpoint')):result[k]=fields.get(n,[0])[0]
|
||||
return result
|
||||
def decision(value):
|
||||
names=('decision_id','request_id','journal_id','checkpoint','helper_journal_id','helper_checkpoint','ready','helper_ready','reason','issued_at','connection_generation')
|
||||
return {k:value.get(k) for k in names}
|
||||
result={}
|
||||
path=Path('/var/lib/rmm-agent-remote/journal.sqlite')
|
||||
with sqlite3.connect(path.as_uri()+'?mode=ro',uri=True) as db:
|
||||
require(db.execute('PRAGMA integrity_check').fetchone()[0]=='ok','agent journal integrity')
|
||||
names=[r[1] for r in db.execute('PRAGMA table_info(reconcile_meta)')]
|
||||
result['agent_meta']=dict(zip(names,db.execute('SELECT * FROM reconcile_meta').fetchone()))
|
||||
requests=[]
|
||||
for ident,generation,raw,response in db.execute('SELECT id,generation,request,response FROM reconcile_requests ORDER BY rowid DESC LIMIT 12'):
|
||||
item=dict(id=ident,generation=generation,request=request(raw),response_sha256=hashlib.sha256(response).hexdigest() if response else None)
|
||||
if response:item['decision']=decision(json.loads(wire(response)[1][0]))
|
||||
requests.append(item)
|
||||
result['agent_requests']=requests
|
||||
path=Path('/var/lib/rmm-helper/consumption.sqlite')
|
||||
with sqlite3.connect(path.as_uri()+'?mode=ro',uri=True) as db:
|
||||
require(db.execute('PRAGMA integrity_check').fetchone()[0]=='ok','helper ledger integrity')
|
||||
names=[r[1] for r in db.execute('PRAGMA table_info(helper_sync_meta)')]
|
||||
result['helper_meta']=dict(zip(names,db.execute('SELECT * FROM helper_sync_meta').fetchone()))
|
||||
decisions=[]
|
||||
for ident,checkpoint,response,raw in db.execute('SELECT decision_id,checkpoint,response,request FROM helper_sync_decisions ORDER BY rowid DESC LIMIT 12'):
|
||||
signed=json.loads(response);value=json.loads(bytes(signed['decision_bytes']))
|
||||
decisions.append(dict(id=ident,checkpoint=checkpoint,request=request(raw),decision=decision(value)))
|
||||
result['helper_decisions']=decisions
|
||||
return result
|
||||
|
||||
|
||||
def server_chain(payload):
|
||||
root=Path('/opt/rust-discovery');agent=str(uuid.UUID(payload['agent_id']))
|
||||
cmd=['docker','compose','--env-file',str(root/'devstate/.env'),'-f',str(root/'deploy/server.compose.yaml'),
|
||||
'exec','-T','postgres','psql','-X','-qAt','-v','ON_ERROR_STOP=1','-U','postgres','-d','rmm']
|
||||
scope="agent_id='"+agent+"' AND tenant_id='11111111-1111-4111-8111-111111111111'"
|
||||
query=("SELECT json_build_object('meta',(SELECT row_to_json(s) FROM agent_sync_state s WHERE "+scope+"),"
|
||||
"'decisions',(SELECT json_agg(row_to_json(d)) FROM (SELECT request_id,decision_id,journal_id,checkpoint,connection_generation,request_hash,created_at,"
|
||||
"convert_from(payload,'UTF8')::jsonb->>'reason' AS reason,convert_from(payload,'UTF8')::jsonb->>'ready' AS ready,"
|
||||
"convert_from(payload,'UTF8')::jsonb->>'helper_ready' AS helper_ready FROM sync_checkpoints WHERE "+scope+" ORDER BY created_at DESC LIMIT 20) d));")
|
||||
raw=subprocess.run(cmd,input=query.encode(),capture_output=True,check=True).stdout.strip()
|
||||
return json.loads(raw)
|
||||
|
||||
Reference in New Issue
Block a user