Manage shared ARNavi VPS contours and application keys from infrastructure
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Build the three-file, secret-free canonical B2 foundation artifact."""
|
||||
import argparse
|
||||
import gzip
|
||||
import hashlib
|
||||
import io
|
||||
import json
|
||||
from pathlib import Path
|
||||
import tarfile
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
ENTRIES = ('docker-compose.device-b2-foundation.yml', 'deployment/device-b2-foundation-v1.sql',
|
||||
'deployment/device-b2-foundation-v1.json')
|
||||
IDENTITY = 'device-b2-foundation-v1-20260930-001'
|
||||
SQL_SHA = 'dc5608eccb8f628aab49390d245e6758505bb8a459a49705c375c3ca59b3e03b'
|
||||
|
||||
|
||||
def build(destination):
|
||||
files = {name: (ROOT / name).read_bytes() for name in ENTRIES}
|
||||
descriptor = json.loads(files[ENTRIES[2]])
|
||||
if descriptor['releaseId'] != IDENTITY or descriptor['sqlSha256'] != SQL_SHA or hashlib.sha256(files[ENTRIES[1]]).hexdigest() != SQL_SHA:
|
||||
raise ValueError('foundation identity or SQL digest mismatch')
|
||||
payload = {'manifest.env': f'id={IDENTITY}\ncomponent=device-b2-foundation\ntype=app-overlay\n'.encode(),
|
||||
'files.txt': ('\n'.join(ENTRIES)+'\n').encode(), **{'payload/'+p:v for p,v in files.items()}}
|
||||
destination = Path(destination)
|
||||
destination.parent.mkdir(parents=True,exist_ok=True)
|
||||
with destination.open('xb') as output:
|
||||
with gzip.GzipFile(filename='',mode='wb',fileobj=output,mtime=0) as gz:
|
||||
with tarfile.open(fileobj=gz,mode='w',format=tarfile.PAX_FORMAT) as archive:
|
||||
for name,data in sorted(payload.items()):
|
||||
member=tarfile.TarInfo(name);member.size=len(data);member.mode=0o644;member.mtime=0
|
||||
archive.addfile(member,io.BytesIO(data))
|
||||
return {'component':'device-b2-foundation','id':IDENTITY,'artifact':str(destination),
|
||||
'sha256':hashlib.sha256(destination.read_bytes()).hexdigest(),'entries':ENTRIES,
|
||||
'services':['device-control-core'],'build':[],'newPorts':False}
|
||||
|
||||
|
||||
if __name__=='__main__':
|
||||
parser=argparse.ArgumentParser();parser.add_argument('--output',required=True)
|
||||
print(json.dumps(build(parser.parse_args().output),indent=2))
|
||||
@@ -0,0 +1,41 @@
|
||||
import hashlib
|
||||
import importlib.util
|
||||
import json
|
||||
from pathlib import Path
|
||||
import tarfile
|
||||
import tempfile
|
||||
import unittest
|
||||
from unittest import mock
|
||||
|
||||
spec=importlib.util.spec_from_file_location('b2_builder',Path(__file__).with_name('build_device_b2_foundation.py'))
|
||||
B=importlib.util.module_from_spec(spec);spec.loader.exec_module(B)
|
||||
|
||||
|
||||
class Artifact(unittest.TestCase):
|
||||
def test_deterministic_exact_three_files_and_no_overwrite(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
first=Path(tmp)/'one.tgz';second=Path(tmp)/'two.tgz'
|
||||
B.build(first);B.build(second)
|
||||
self.assertEqual(first.read_bytes(),second.read_bytes())
|
||||
with self.assertRaises(FileExistsError):B.build(first)
|
||||
with tarfile.open(first) as archive:
|
||||
self.assertEqual(set(archive.getnames()),{'manifest.env','files.txt',*('payload/'+p for p in B.ENTRIES)})
|
||||
self.assertTrue(all(m.isfile() for m in archive))
|
||||
manifest=archive.extractfile('manifest.env').read().decode()
|
||||
self.assertIn('component=device-b2-foundation\n',manifest)
|
||||
sql=archive.extractfile('payload/'+B.ENTRIES[1]).read()
|
||||
self.assertEqual(hashlib.sha256(sql).hexdigest(),B.SQL_SHA)
|
||||
self.assertNotIn(b'drop table',sql.lower())
|
||||
|
||||
def test_changed_sql_is_rejected_before_archive_exists(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
root=Path(tmp)/'source';root.mkdir()
|
||||
for name in B.ENTRIES:
|
||||
path=root/name;path.parent.mkdir(exist_ok=True);path.write_bytes((B.ROOT/name).read_bytes())
|
||||
(root/B.ENTRIES[1]).write_bytes(b'drop table existing;')
|
||||
target=Path(tmp)/'bad.tgz'
|
||||
with mock.patch.object(B,'ROOT',root),self.assertRaises(ValueError):B.build(target)
|
||||
self.assertFalse(target.exists())
|
||||
|
||||
|
||||
if __name__=='__main__':unittest.main()
|
||||
Reference in New Issue
Block a user