Files
ecs-relay-service/deployable/scripts/package_offline.py

100 lines
4.7 KiB
Python

"""Package current source with existing, checksum-verified Linux ARM64 dependencies.
Usage: python scripts/package_offline.py --dependencies /path/to/old-bundle --output /path/to/new.tar.gz
The dependency directory must contain SHA256SUMS, images/, hainuo-relay/wheelhouse/.
Only dependencies are reused; source in the old bundle is never packaged.
"""
import argparse
import hashlib
import io
import json
import tarfile
import zipfile
from email.parser import Parser
from pathlib import Path
def digest(path):
with path.open('rb') as handle:
return hashlib.file_digest(handle, 'sha256').hexdigest()
def package(dependencies, output):
root = Path(__file__).resolve().parents[1]
dependencies, output = Path(dependencies), Path(output)
if output.exists():
raise ValueError('Output already exists; refusing to overwrite')
checks = {}
for line in (dependencies/'SHA256SUMS').read_text(encoding='utf-8').splitlines():
sha, rel = line.split(None, 1)
checks[rel.strip().lstrip('*')] = sha
assets = sorted(p for folder in [dependencies/'images', dependencies/'hainuo-relay/wheelhouse', dependencies/'licenses']
for p in folder.rglob('*') if p.is_file())
if not assets or not (dependencies/'licenses').is_dir():
raise ValueError('Dependency bundle must include images, wheelhouse and license notices')
wheel_versions = {}
mapped = {}
for path in assets:
rel = path.relative_to(dependencies).as_posix()
if rel not in checks or digest(path) != checks[rel]:
raise ValueError('Dependency checksum mismatch: ' + rel)
if path.suffix == '.whl':
if not (path.name.endswith('none-any.whl') or 'aarch64' in path.name):
raise ValueError('Unsupported wheel architecture: ' + path.name)
with zipfile.ZipFile(path) as wheel:
name = next(x for x in wheel.namelist() if x.endswith('.dist-info/METADATA'))
meta = Parser().parsestr(wheel.read(name).decode('utf-8'))
wheel_versions[meta['Name'].lower().replace('_','-')] = meta['Version']
target = 'wheelhouse/' + path.name
else:
target = rel
mapped[target] = path
required = {}
for line in (root/'requirements.lock').read_text(encoding='utf-8').splitlines():
if line.strip() and not line.startswith('#'):
name, version = line.split('==')
required[name.lower().replace('_','-')] = version
assert all(wheel_versions.get(n) == v for n,v in required.items()), 'Pinned wheel dependency mismatch'
image_tars = list((dependencies/'images').glob('*.tar'))
assert len(image_tars) == 4, 'Expected four base images'
for path in image_tars:
with tarfile.open(path) as image:
manifest = json.load(image.extractfile('manifest.json'))
for entry in manifest:
config = json.load(image.extractfile(entry['Config']))
assert config['architecture'] == 'arm64' and config['os'] == 'linux'
for path in root.rglob('*'):
rel = path.relative_to(root)
if not path.is_file() or any(x in {'runtime','backups','wheelhouse','images','__pycache__','.pytest_cache','.git'} for x in rel.parts):
continue
if path.name in {'.env','settings.env'} or path.suffix == '.pyc':
continue
mapped[rel.as_posix()] = path
manifest=''.join(f'{digest(path)} {name}\n' for name,path in sorted(mapped.items()))
output.parent.mkdir(parents=True, exist_ok=True)
with tarfile.open(output, 'w:gz', compresslevel=6) as tf:
for name,path in sorted(mapped.items()):
info=tf.gettarinfo(str(path), arcname='hainuo-relay-deployable/'+name)
info.mode=0o755 if path.suffix=='.sh' else 0o644
info.uid=info.gid=0
info.uname=info.gname=''
with path.open('rb') as handle:
tf.addfile(info,handle)
data=manifest.encode('utf-8')
info=tarfile.TarInfo('hainuo-relay-deployable/OFFLINE_SHA256SUMS')
info.size=len(data)
info.mode=0o644
tf.addfile(info,io.BytesIO(data))
checksum=digest(output)
Path(str(output)+'.sha256').write_text(checksum+' '+output.name+'\n',encoding='utf-8')
print(json.dumps({'output':str(output),'bytes':output.stat().st_size,'sha256':checksum,
'pinned_requirements':len(required),'verified_wheels':len(wheel_versions),'verified_images':len(image_tars)},indent=2))
if __name__=='__main__':
parser=argparse.ArgumentParser()
parser.add_argument('--dependencies',required=True)
parser.add_argument('--output',required=True)
args=parser.parse_args()
package(args.dependencies,args.output)