OTA now checks files by default instead of overwriting

This commit is contained in:
2026-04-11 23:32:49 +02:00
parent 25aa09298c
commit db3798dc07

118
ota.py
View File

@@ -63,60 +63,84 @@ import utime
# Default configuration — override via /ota_config.json # Default configuration — override via /ota_config.json
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
GITEA_BASE = "http://git.baumann.gr" # no trailing slash GITEA_BASE = "http://git.baumann.gr" # no trailing slash
REPO_OWNER = "adrian" REPO_OWNER = "adrian"
REPO_NAME = "esp32-gauge" REPO_NAME = "esp32-gauge"
REPO_FOLDER = "firmware" # folder inside repo to sync REPO_FOLDER = "firmware" # folder inside repo to sync
REPO_BRANCH = "main" REPO_BRANCH = "main"
API_TOKEN = None # set to string for private repos API_TOKEN = None # set to string for private repos
WIFI_SSID = None WIFI_SSID = None
WIFI_PASSWORD = None WIFI_PASSWORD = None
SETTINGS_FILE = "/ota_config.json" SETTINGS_FILE = "/ota_config.json"
MANIFEST_FILE = "/.ota_manifest.json" MANIFEST_FILE = "/.ota_manifest.json"
OK_FLAG_FILE = "/.ota_ok" OK_FLAG_FILE = "/.ota_ok"
OTA_MANIFEST = "ota_manifest.txt" OTA_MANIFEST = "ota_manifest.txt"
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Logging # Logging
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
def _ts(): def _ts():
ms = utime.ticks_ms() ms = utime.ticks_ms()
return f"{(ms//3600000)%24:02d}:{(ms//60000)%60:02d}:{(ms//1000)%60:02d}.{ms%1000:03d}" return f"{(ms // 3600000) % 24:02d}:{(ms // 60000) % 60:02d}:{(ms // 1000) % 60:02d}.{ms % 1000:03d}"
def _log(level, msg):
print(f"[{_ts()}] {level:5s} [OTA] {msg}")
def info(msg):
_log("INFO", msg)
def warn(msg):
_log("WARN", msg)
def log_err(msg):
_log("ERROR", msg)
def _log(level, msg): print(f"[{_ts()}] {level:5s} [OTA] {msg}")
def info(msg): _log("INFO", msg)
def warn(msg): _log("WARN", msg)
def log_err(msg): _log("ERROR", msg)
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# HTTP helpers # HTTP helpers
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
def _headers(): def _headers():
h = {"Accept": "application/json"} h = {"Accept": "application/json"}
if API_TOKEN: if API_TOKEN:
h["Authorization"] = f"token {API_TOKEN}" h["Authorization"] = f"token {API_TOKEN}"
return h return h
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Config loader # Config loader
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
def load_config(): def load_config():
global GITEA_BASE, REPO_OWNER, REPO_NAME, REPO_FOLDER, REPO_BRANCH, API_TOKEN, WIFI_SSID, WIFI_PASSWORD global \
GITEA_BASE, \
REPO_OWNER, \
REPO_NAME, \
REPO_FOLDER, \
REPO_BRANCH, \
API_TOKEN, \
WIFI_SSID, \
WIFI_PASSWORD
try: try:
with open(SETTINGS_FILE) as f: with open(SETTINGS_FILE) as f:
cfg = ujson.load(f) cfg = ujson.load(f)
GITEA_BASE = cfg.get("gitea_base", GITEA_BASE) GITEA_BASE = cfg.get("gitea_base", GITEA_BASE)
REPO_OWNER = cfg.get("repo_owner", REPO_OWNER) REPO_OWNER = cfg.get("repo_owner", REPO_OWNER)
REPO_NAME = cfg.get("repo_name", REPO_NAME) REPO_NAME = cfg.get("repo_name", REPO_NAME)
REPO_FOLDER = cfg.get("repo_folder", REPO_FOLDER) REPO_FOLDER = cfg.get("repo_folder", REPO_FOLDER)
REPO_BRANCH = cfg.get("repo_branch", REPO_BRANCH) REPO_BRANCH = cfg.get("repo_branch", REPO_BRANCH)
API_TOKEN = cfg.get("api_token", API_TOKEN) API_TOKEN = cfg.get("api_token", API_TOKEN)
WIFI_SSID = cfg.get("wifi_ssid", WIFI_SSID) WIFI_SSID = cfg.get("wifi_ssid", WIFI_SSID)
WIFI_PASSWORD = cfg.get("wifi_password", WIFI_PASSWORD) WIFI_PASSWORD = cfg.get("wifi_password", WIFI_PASSWORD)
info(f"Config loaded from {SETTINGS_FILE}") info(f"Config loaded from {SETTINGS_FILE}")
except OSError: except OSError:
@@ -124,10 +148,12 @@ def load_config():
except Exception as e: except Exception as e:
warn(f"Config parse error: {e} — using defaults") warn(f"Config parse error: {e} — using defaults")
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Helpers # Helpers
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
def _match_pattern(name, pattern): def _match_pattern(name, pattern):
if "*" not in pattern: if "*" not in pattern:
return name == pattern return name == pattern
@@ -150,11 +176,9 @@ def _match_pattern(name, pattern):
i += 1 i += 1
return i == n and j == m return i == n and j == m
def _fetch_commit_sha(): def _fetch_commit_sha():
url = ( url = f"{GITEA_BASE}/api/v1/repos/{REPO_OWNER}/{REPO_NAME}/branches/{REPO_BRANCH}"
f"{GITEA_BASE}/api/v1/repos/{REPO_OWNER}/{REPO_NAME}"
f"/branches/{REPO_BRANCH}"
)
try: try:
r = urequests.get(url, headers=_headers()) r = urequests.get(url, headers=_headers())
if r.status_code == 200: if r.status_code == 200:
@@ -166,6 +190,7 @@ def _fetch_commit_sha():
log_err(f"Failed to fetch commit: {e}") log_err(f"Failed to fetch commit: {e}")
return None return None
def _fetch_manifest(): def _fetch_manifest():
url = ( url = (
f"{GITEA_BASE}/api/v1/repos/{REPO_OWNER}/{REPO_NAME}" f"{GITEA_BASE}/api/v1/repos/{REPO_OWNER}/{REPO_NAME}"
@@ -178,6 +203,7 @@ def _fetch_manifest():
r.close() r.close()
if data.get("content"): if data.get("content"):
import ubinascii import ubinascii
content = ubinascii.a2b_base64(data["content"]).decode() content = ubinascii.a2b_base64(data["content"]).decode()
patterns = [line.strip() for line in content.splitlines()] patterns = [line.strip() for line in content.splitlines()]
return [p for p in patterns if p and not p.startswith("#")] return [p for p in patterns if p and not p.startswith("#")]
@@ -188,6 +214,7 @@ def _fetch_manifest():
log_err(f"Failed to fetch manifest: {e}") log_err(f"Failed to fetch manifest: {e}")
return None return None
def _fetch_dir(path): def _fetch_dir(path):
url = ( url = (
f"{GITEA_BASE}/api/v1/repos/{REPO_OWNER}/{REPO_NAME}" f"{GITEA_BASE}/api/v1/repos/{REPO_OWNER}/{REPO_NAME}"
@@ -195,6 +222,7 @@ def _fetch_dir(path):
) )
return _api_get(url) return _api_get(url)
def _api_get(url): def _api_get(url):
"""GET a URL and return parsed JSON, or None on failure.""" """GET a URL and return parsed JSON, or None on failure."""
try: try:
@@ -209,6 +237,7 @@ def _api_get(url):
log_err(f"GET {url} failed: {e}") log_err(f"GET {url} failed: {e}")
return None return None
def _download(url, dest_path): def _download(url, dest_path):
"""Download url to dest_path. Returns True on success.""" """Download url to dest_path. Returns True on success."""
tmp = dest_path + ".tmp" tmp = dest_path + ".tmp"
@@ -236,6 +265,7 @@ def _download(url, dest_path):
pass pass
return False return False
def _load_manifest(): def _load_manifest():
try: try:
with open(MANIFEST_FILE) as f: with open(MANIFEST_FILE) as f:
@@ -243,6 +273,7 @@ def _load_manifest():
except Exception: except Exception:
return {} return {}
def _save_manifest(manifest, commit_sha=None): def _save_manifest(manifest, commit_sha=None):
try: try:
with open(MANIFEST_FILE, "w") as f: with open(MANIFEST_FILE, "w") as f:
@@ -252,6 +283,7 @@ def _save_manifest(manifest, commit_sha=None):
except Exception as e: except Exception as e:
warn(f"Could not save manifest: {e}") warn(f"Could not save manifest: {e}")
def _wipe_manifest(): def _wipe_manifest():
try: try:
os.remove(MANIFEST_FILE) os.remove(MANIFEST_FILE)
@@ -259,6 +291,7 @@ def _wipe_manifest():
except OSError: except OSError:
pass pass
def _ok_flag_exists(): def _ok_flag_exists():
try: try:
os.stat(OK_FLAG_FILE) os.stat(OK_FLAG_FILE)
@@ -266,12 +299,14 @@ def _ok_flag_exists():
except OSError: except OSError:
return False return False
def _clear_ok_flag(): def _clear_ok_flag():
try: try:
os.remove(OK_FLAG_FILE) os.remove(OK_FLAG_FILE)
except OSError: except OSError:
pass pass
def mark_ok(): def mark_ok():
""" """
Call this from main.py after successful startup. Call this from main.py after successful startup.
@@ -283,10 +318,12 @@ def mark_ok():
except Exception as e: except Exception as e:
warn(f"Could not write OK flag: {e}") warn(f"Could not write OK flag: {e}")
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Core update logic # Core update logic
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
def _fetch_file_list(): def _fetch_file_list():
""" """
Returns list of {name, sha, download_url} dicts based on the Returns list of {name, sha, download_url} dicts based on the
@@ -319,11 +356,13 @@ def _fetch_file_list():
if _match_pattern(name, p) or _match_pattern(entry["path"], p): if _match_pattern(name, p) or _match_pattern(entry["path"], p):
if entry["path"] not in visited: if entry["path"] not in visited:
visited.add(entry["path"]) visited.add(entry["path"])
files.append({ files.append(
"name": entry["path"], {
"sha": entry["sha"], "name": entry["path"],
"download_url": entry["download_url"], "sha": entry["sha"],
}) "download_url": entry["download_url"],
}
)
break break
root = _fetch_dir(REPO_FOLDER) root = _fetch_dir(REPO_FOLDER)
@@ -333,12 +372,15 @@ def _fetch_file_list():
fetch_matching(root, manifest_patterns) fetch_matching(root, manifest_patterns)
return files return files
def _do_update(commit_sha=None): def _do_update(commit_sha=None):
""" """
Fetch file list, download changed files, update manifest. Fetch file list, download changed files, update manifest.
Returns True if all succeeded (or nothing needed updating). Returns True if all succeeded (or nothing needed updating).
""" """
info(f"Checking {GITEA_BASE}/{REPO_OWNER}/{REPO_NAME}/{REPO_FOLDER} @ {REPO_BRANCH}") info(
f"Checking {GITEA_BASE}/{REPO_OWNER}/{REPO_NAME}/{REPO_FOLDER} @ {REPO_BRANCH}"
)
file_list = _fetch_file_list() file_list = _fetch_file_list()
if file_list is None: if file_list is None:
log_err("Could not fetch file list — skipping update") log_err("Could not fetch file list — skipping update")
@@ -346,12 +388,12 @@ def _do_update(commit_sha=None):
info(f"Found {len(file_list)} file(s) to sync") info(f"Found {len(file_list)} file(s) to sync")
manifest = _load_manifest() manifest = _load_manifest()
updated = [] updated = []
failed = [] failed = []
for entry in file_list: for entry in file_list:
name = entry["name"] name = entry["name"]
sha = entry["sha"] sha = entry["sha"]
if manifest.get(name) == sha: if manifest.get(name) == sha:
info(f" {name} up to date") info(f" {name} up to date")
@@ -382,10 +424,12 @@ def _do_update(commit_sha=None):
return True return True
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Public entry point # Public entry point
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
def update(): def update():
""" """
Main entry point. Call from boot.py before importing application code. Main entry point. Call from boot.py before importing application code.
@@ -408,9 +452,7 @@ def update():
if not ok_flag: if not ok_flag:
warn("OK flag missing — last boot may have failed") warn("OK flag missing — last boot may have failed")
warn("Wiping manifest to force full re-fetch") warn("Re-checking all files, will only download changed ones")
_wipe_manifest()
manifest = {}
else: else:
info("OK flag present — last boot was good") info("OK flag present — last boot was good")