|
| 1 | +import hashlib |
| 2 | +import io |
| 3 | +import time |
| 4 | + |
| 5 | +import boto3 |
| 6 | +import click |
| 7 | +import httpx |
| 8 | +from flask import cli as flask_cli |
| 9 | + |
| 10 | +from .models import Firmware, db |
| 11 | +from .settings import config |
| 12 | + |
| 13 | +MEMFAULT_API = "https://api.memfault.com/api/v0/releases/latest" |
| 14 | + |
| 15 | +# Core Devices hardware revisions. These short names are both the `hardware` |
| 16 | +# value we store in the firmwares table and the `hardware_version` string |
| 17 | +# Memfault expects. Based on the mobileapp WatchHardwarePlatform. |
| 18 | +CORE_DEVICES_DEVICES = ( |
| 19 | + "asterix", |
| 20 | + "obelix_evt", |
| 21 | + "obelix_dvt", |
| 22 | + "obelix_pvt", |
| 23 | + "getafix_evt", |
| 24 | + "getafix_dvt", |
| 25 | + "obelix_bb", |
| 26 | + "obelix_bb2", |
| 27 | +) |
| 28 | + |
| 29 | + |
| 30 | +def _fetch_latest(client, token, hw_revision): |
| 31 | + resp = client.get( |
| 32 | + MEMFAULT_API, |
| 33 | + params={ |
| 34 | + "hardware_version": hw_revision, |
| 35 | + "software_type": "pebbleos", |
| 36 | + "device_serial": "REBBLE_COHORTS_CRON", |
| 37 | + }, |
| 38 | + headers={"Memfault-Project-Key": token}, |
| 39 | + ) |
| 40 | + if resp.status_code == 204: |
| 41 | + return None |
| 42 | + resp.raise_for_status() |
| 43 | + return resp.json() |
| 44 | + |
| 45 | + |
| 46 | +def _download_and_hash(client, url): |
| 47 | + sha256 = hashlib.sha256() |
| 48 | + buf = io.BytesIO() |
| 49 | + with client.stream("GET", url) as resp: |
| 50 | + resp.raise_for_status() |
| 51 | + for chunk in resp.iter_bytes(chunk_size=8192): |
| 52 | + buf.write(chunk) |
| 53 | + sha256.update(chunk) |
| 54 | + buf.seek(0) |
| 55 | + return buf, sha256.hexdigest() |
| 56 | + |
| 57 | + |
| 58 | +def _s3_client(): |
| 59 | + return boto3.client( |
| 60 | + "s3", |
| 61 | + aws_access_key_id=config["AWS_ACCESS_KEY"], |
| 62 | + aws_secret_access_key=config["AWS_SECRET_KEY"], |
| 63 | + endpoint_url=config["S3_ENDPOINT"], |
| 64 | + ) |
| 65 | + |
| 66 | + |
| 67 | +def _upload(data, s3_key): |
| 68 | + data.seek(0) |
| 69 | + _s3_client().upload_fileobj( |
| 70 | + data, |
| 71 | + config["S3_BUCKET"], |
| 72 | + s3_key, |
| 73 | + ExtraArgs={"ContentType": "application/octet-stream"}, |
| 74 | + ) |
| 75 | + |
| 76 | + |
| 77 | +@click.command(name="fetch_firmware") |
| 78 | +@click.option( |
| 79 | + "--token", |
| 80 | + default=None, |
| 81 | + help="Memfault project key (falls back to MEMFAULT_TOKEN env).", |
| 82 | +) |
| 83 | +@flask_cli.with_appcontext |
| 84 | +def fetch_firmware_command(token): |
| 85 | + """Check Memfault for the latest firmware for each CoreDevice hardware, |
| 86 | + download + re-upload to our CDN, and upsert into the firmwares table.""" |
| 87 | + token = token or config["MEMFAULT_TOKEN"] |
| 88 | + if not token: |
| 89 | + raise click.UsageError("MEMFAULT_TOKEN not set (pass --token or set the env var).") |
| 90 | + for var in ("AWS_ACCESS_KEY", "AWS_SECRET_KEY", "S3_BUCKET"): |
| 91 | + if not config[var]: |
| 92 | + raise click.UsageError(f"{var} env var not set.") |
| 93 | + |
| 94 | + added = 0 |
| 95 | + skipped = 0 |
| 96 | + failed = 0 |
| 97 | + with httpx.Client(follow_redirects=True, timeout=60.0) as client: |
| 98 | + for hardware in CORE_DEVICES_DEVICES: |
| 99 | + click.echo(f"[{hardware}]: ", nl=False) |
| 100 | + try: |
| 101 | + info = _fetch_latest(client, token, hardware) |
| 102 | + except httpx.HTTPStatusError as e: |
| 103 | + click.echo(f"lookup FAILED ({e.response.status_code})") |
| 104 | + failed += 1 |
| 105 | + continue |
| 106 | + |
| 107 | + if info is None: |
| 108 | + click.echo("no update available") |
| 109 | + continue |
| 110 | + |
| 111 | + version = info["version"] |
| 112 | + notes = info.get("notes") or None |
| 113 | + artifact_url = info["artifacts"][0]["url"] |
| 114 | + |
| 115 | + existing = Firmware.query.filter_by( |
| 116 | + hardware=hardware, kind="normal", version=version |
| 117 | + ).one_or_none() |
| 118 | + if existing is not None: |
| 119 | + click.echo(f"{version} already in DB, skipping") |
| 120 | + skipped += 1 |
| 121 | + continue |
| 122 | + |
| 123 | + filename = f"Pebble-{version}-{hardware}.pbz" |
| 124 | + s3_key = f"{config['S3_PATH']}{hardware}/{filename}" |
| 125 | + public_url = f"{config['FIRMWARE_ROOT']}/{hardware}/{filename}" |
| 126 | + |
| 127 | + click.echo(f"{version} downloading... ", nl=False) |
| 128 | + try: |
| 129 | + data, sha256 = _download_and_hash(client, artifact_url) |
| 130 | + except httpx.HTTPStatusError as e: |
| 131 | + click.echo(f"download FAILED ({e.response.status_code})") |
| 132 | + failed += 1 |
| 133 | + continue |
| 134 | + |
| 135 | + click.echo("uploading... ", nl=False) |
| 136 | + try: |
| 137 | + _upload(data, s3_key) |
| 138 | + except Exception as e: |
| 139 | + click.echo(f"upload FAILED ({e})") |
| 140 | + failed += 1 |
| 141 | + continue |
| 142 | + |
| 143 | + Firmware.upsert( |
| 144 | + hardware=hardware, |
| 145 | + kind="normal", |
| 146 | + version=version, |
| 147 | + url=public_url, |
| 148 | + sha256=sha256, |
| 149 | + timestamp=int(time.time()), |
| 150 | + notes=notes, |
| 151 | + ) |
| 152 | + db.session.commit() |
| 153 | + click.echo("OK") |
| 154 | + added += 1 |
| 155 | + |
| 156 | + click.echo(f"done. {added} added, {skipped} already present, {failed} failed.") |
0 commit comments