Support auth token for manifest/bundle downloads

This commit is contained in:
Aaron
2025-12-10 19:51:23 -05:00
parent 4c47a583e3
commit f9297b68e3

View File

@@ -40,6 +40,7 @@ DEFAULT_MANIFEST_URL = os.environ.get(
"PIKIT_MANIFEST_URL", "PIKIT_MANIFEST_URL",
"https://git.44r0n.cc/44r0n7/pi-kit/releases/latest/download/manifest.json", "https://git.44r0n.cc/44r0n7/pi-kit/releases/latest/download/manifest.json",
) )
AUTH_TOKEN = os.environ.get("PIKIT_AUTH_TOKEN")
WEB_ROOT = pathlib.Path("/var/www/pikit-web") WEB_ROOT = pathlib.Path("/var/www/pikit-web")
API_PATH = pathlib.Path("/usr/local/bin/pikit-api.py") API_PATH = pathlib.Path("/usr/local/bin/pikit-api.py")
BACKUP_ROOT = pathlib.Path("/var/backups/pikit") BACKUP_ROOT = pathlib.Path("/var/backups/pikit")
@@ -526,7 +527,10 @@ def save_update_state(state: dict):
def fetch_manifest(url: str = None): def fetch_manifest(url: str = None):
target = url or DEFAULT_MANIFEST_URL target = url or DEFAULT_MANIFEST_URL
resp = urllib.request.urlopen(target, timeout=10) req = urllib.request.Request(target)
if AUTH_TOKEN:
req.add_header("Authorization", f"token {AUTH_TOKEN}")
resp = urllib.request.urlopen(req, timeout=10)
data = resp.read() data = resp.read()
manifest = json.loads(data.decode()) manifest = json.loads(data.decode())
return manifest return manifest
@@ -534,7 +538,10 @@ def fetch_manifest(url: str = None):
def download_file(url: str, dest: pathlib.Path): def download_file(url: str, dest: pathlib.Path):
ensure_dir(dest.parent) ensure_dir(dest.parent)
with urllib.request.urlopen(url, timeout=30) as resp, dest.open("wb") as f: req = urllib.request.Request(url)
if AUTH_TOKEN:
req.add_header("Authorization", f"token {AUTH_TOKEN}")
with urllib.request.urlopen(req, timeout=60) as resp, dest.open("wb") as f:
shutil.copyfileobj(resp, f) shutil.copyfileobj(resp, f)
return dest return dest