Files
mikrotik-olsr/tools/build-release.py
T
2026-11-19 08:14:59 +01:00

136 lines
6.9 KiB
Python
Executable File

#!/usr/bin/env python3
"""Download official inputs and produce a verified custom-key release directory."""
import argparse
import hashlib
import json
from pathlib import Path
import re
import shutil
import subprocess
import sys
import tarfile
import urllib.request
import zipfile
ROOT = Path(__file__).resolve().parents[1]
def download(url, output):
if not output.exists():
temporary = output.with_suffix(output.suffix + '.part')
with urllib.request.urlopen(url, timeout=60) as response, temporary.open('wb') as target:
shutil.copyfileobj(response, target)
temporary.replace(output)
return {'url': url, 'sha256': hashlib.sha256(output.read_bytes()).hexdigest()}
def run(*args):
subprocess.run([sys.executable, str(ROOT / 'tools/routeros.py'), *map(str, args)], check=True)
def build(args):
if not re.fullmatch(r'7\.\d+(?:\.\d+)?(?:beta\d+|rc\d+)?', args.version):
raise ValueError('Expected a RouterOS v7 version')
version = args.version
arch = args.arch
cache = ROOT / 'build' / f'inputs-{version}-{arch}'
output = Path(args.output).resolve()
cache.mkdir(parents=True, exist_ok=True)
output.mkdir(parents=True, exist_ok=True)
if any(output.iterdir()):
raise ValueError('Use an empty release output directory to avoid mixing keysets')
base = f'https://download.mikrotik.com/routeros/{version}'
inputs = {}
def get(name):
path = cache / name
inputs[name] = download(f'{base}/{name}', path)
return path
system = get(f'routeros-{version}-{arch}.npk')
wifi = get(f'{args.wifi}-{version}-{arch}.npk') if args.wifi else None
linux_archive = get(f'netinstall-{version}.tar.gz')
windows_archive = get(f'netinstall-{version}.zip')
linux = cache / 'linux'
linux.mkdir(exist_ok=True)
with tarfile.open(linux_archive) as archive:
archive.extractall(linux, filter='data')
windows = cache / 'netinstall.exe'
with zipfile.ZipFile(windows_archive) as archive:
windows.write_bytes(archive.read('netinstall.exe'))
for source, target in ((linux / 'netinstall-cli', output / 'netinstall-cli'),
(windows, output / 'netinstall.exe')):
run('netinstall', source, '--keys', args.keys, '--output', target)
run('npk', system, '--arch', arch, '--patch-system', '--keys', args.keys, '--output', output / system.name)
if wifi:
run('npk', wifi, '--arch', arch, '--keys', args.keys, '--output', output / wifi.name)
payloads = {'option': args.option_root, 'olsr': args.olsr_root}
if args.olsr_root:
subprocess.run([sys.executable, str(ROOT / 'tools/build-webfig.py'), '--experimental', '--root', args.olsr_root, '--version', version], check=True)
if any(payloads.values()):
extras = get(f'all_packages-{arch}-{version}.zip')
template = cache / f'gps-{version}-{arch}.npk'
with zipfile.ZipFile(extras) as archive:
matches = [n for n in archive.namelist() if Path(n).name == template.name]
if len(matches) != 1:
raise ValueError('Expected exactly one GPS package template')
template.write_bytes(archive.read(matches[0]))
for name, directory in payloads.items():
if directory:
if not Path(directory).is_dir() or not any(Path(directory).rglob('*')):
raise ValueError(f'Missing or empty {name} payload')
run('create', '--template', template, '--root', directory, '--name', name,
'--description', f'{name} development package', '--keys', args.keys,
'--output', output / f'{name}-{version}-{arch}.npk')
if any(payloads.values()):
subprocess.run([sys.executable, str(ROOT / 'tools/bundle-sources.py'), '--output', str(output / 'runtime-sources.tar.gz')], check=True)
run('public-key', '--keys', args.keys, '--output', output / 'public-key.json')
# Keep the original upstream installer license in the Linux archive.
archive_name = output / f'netinstall-{version}-linux.tar.gz'
with tarfile.open(archive_name, 'w:gz') as archive:
archive.add(output / 'netinstall-cli', arcname='netinstall-cli')
if (linux / 'LICENSE.txt').exists():
archive.add(linux / 'LICENSE.txt', arcname='LICENSE.txt')
with zipfile.ZipFile(output / f'netinstall-{version}-windows.zip', 'w', zipfile.ZIP_DEFLATED) as archive:
archive.write(output / 'netinstall.exe', 'netinstall.exe')
(output / 'netinstall.exe').unlink()
(output / 'netinstall-cli').unlink()
npk_sizes = {p.name: p.stat().st_size for p in sorted(output.glob('*.npk'))}
core_names = [f'{name}-{version}-{arch}.npk' for name in ('routeros', 'option', 'olsr')]
core_names = [name for name in core_names if name in npk_sizes]
profiles = {'ethernet': {'packages': core_names,
'npk_bytes': sum(npk_sizes[name] for name in core_names)}}
if wifi:
names = core_names + [wifi.name]
profiles['with_wifi'] = {'packages': names,
'npk_bytes': sum(npk_sizes[name] for name in names)}
storage = {'package_bytes': npk_sizes, 'profiles': profiles,
'note': 'NPK transfer sizes only. Installed usage and upgrade headroom require hardware measurement. Do not override Netinstall space checks.'}
manifest = {'routeros_version': version, 'architecture': arch,
'storage': storage,
'status': 'experimental; see RELEASE-NOTES.md for measured hardware results and UI limitations',
'inputs': inputs, 'public_key': json.loads((output / 'public-key.json').read_text()),
'source_commit': subprocess.check_output(['git', 'rev-parse', 'HEAD'], cwd=ROOT, text=True).strip(),
'payloads': [name for name, directory in payloads.items() if directory]}
(output / 'manifest.json').write_text(json.dumps(manifest, indent=2) + '\n')
for name in ('RELEASE-NOTES.md', 'INSTALL.md'):
shutil.copyfile(ROOT / 'docs' / name, output / name)
shutil.copyfile(ROOT / 'tools/configure-device.py', output / 'configure-device.py')
checksums = ''.join(f'{hashlib.sha256(p.read_bytes()).hexdigest()} {p.name}\n'
for p in sorted(output.iterdir()) if p.is_file())
(output / 'SHA256SUMS').write_text(checksums)
print(f'Release artifacts: {output}')
def main():
p = argparse.ArgumentParser(description=__doc__)
p.add_argument('--version', required=True)
p.add_argument('--arch', choices=['arm'], default='arm', help='Only ARM system packages have been exercised so far')
p.add_argument('--wifi', choices=['wifi-qcom-ac', 'wireless'])
p.add_argument('--keys', required=True)
p.add_argument('--output', required=True)
p.add_argument('--option-root', help='Audited runtime payload directory')
p.add_argument('--olsr-root', help='Built OLSR payload directory')
build(p.parse_args())
if __name__ == '__main__':
main()