mirror of
https://github.com/ChuckBuilds/LEDMatrix.git
synced 2026-08-21 02:19:07 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ec8591e4ac |
+1
-63
@@ -111,12 +111,6 @@ class RestoreOptions:
|
|||||||
"""Which sections of a backup should be restored."""
|
"""Which sections of a backup should be restored."""
|
||||||
|
|
||||||
restore_config: bool = True
|
restore_config: bool = True
|
||||||
#: Whether to take the backup's display.hardware block as well.
|
|
||||||
#: Off by default: that block describes the panel physically wired to
|
|
||||||
#: *this* device -- its size, chain length, mapping, multiplexing and
|
|
||||||
#: refresh cap. A backup carries the panel of the machine it was taken
|
|
||||||
#: on, and restoring one onto a different rig drives the wrong geometry.
|
|
||||||
restore_hardware: bool = False
|
|
||||||
restore_secrets: bool = True
|
restore_secrets: bool = True
|
||||||
restore_wifi: bool = True
|
restore_wifi: bool = True
|
||||||
restore_fonts: bool = True
|
restore_fonts: bool = True
|
||||||
@@ -555,60 +549,6 @@ def _copy_file(src: Path, dst: Path) -> None:
|
|||||||
raise
|
raise
|
||||||
|
|
||||||
|
|
||||||
_HARDWARE_PATH = ("display", "hardware")
|
|
||||||
|
|
||||||
|
|
||||||
def _restore_config_preserving_hardware(src: Path, dst: Path, keep_hardware: bool) -> None:
|
|
||||||
"""Copy a backed-up config.json, optionally keeping the local panel block.
|
|
||||||
|
|
||||||
display.hardware describes the panel physically attached to this device:
|
|
||||||
cols, rows, chain_length, hardware_mapping, panel_type, multiplexing and
|
|
||||||
the refresh-rate cap. None of that travels with a configuration -- it is a
|
|
||||||
property of the machine. Restoring a backup taken on a 512x64 rig onto a
|
|
||||||
128x32 one used to overwrite the smaller panel's geometry with the larger
|
|
||||||
one's, which is not a setting the user can see going wrong; the display
|
|
||||||
simply stops being right.
|
|
||||||
|
|
||||||
Falls back to a plain copy when either file cannot be parsed, so a restore
|
|
||||||
never fails because of this.
|
|
||||||
"""
|
|
||||||
if not keep_hardware:
|
|
||||||
_copy_file(src, dst)
|
|
||||||
return
|
|
||||||
try:
|
|
||||||
incoming = json.loads(src.read_text(encoding="utf-8"))
|
|
||||||
local = json.loads(dst.read_text(encoding="utf-8")) if dst.exists() else {}
|
|
||||||
except (OSError, ValueError) as exc:
|
|
||||||
logger.warning(
|
|
||||||
"[Backup] Could not merge local panel config (%s); restoring the "
|
|
||||||
"backup's config.json as-is", exc)
|
|
||||||
_copy_file(src, dst)
|
|
||||||
return
|
|
||||||
|
|
||||||
section, key = _HARDWARE_PATH
|
|
||||||
local_hw = (local.get(section) or {}).get(key)
|
|
||||||
if not isinstance(local_hw, dict) or not local_hw:
|
|
||||||
_copy_file(src, dst)
|
|
||||||
return
|
|
||||||
|
|
||||||
if not isinstance(incoming.get(section), dict):
|
|
||||||
incoming[section] = {}
|
|
||||||
incoming_hw = incoming[section].get(key)
|
|
||||||
incoming[section][key] = local_hw
|
|
||||||
if isinstance(incoming_hw, dict) and incoming_hw != local_hw:
|
|
||||||
logger.info(
|
|
||||||
"[Backup] Kept this device's display.hardware; the backup's panel "
|
|
||||||
"was %sx%s chain %s, this one is %sx%s chain %s",
|
|
||||||
incoming_hw.get("cols"), incoming_hw.get("rows"),
|
|
||||||
incoming_hw.get("chain_length"),
|
|
||||||
local_hw.get("cols"), local_hw.get("rows"),
|
|
||||||
local_hw.get("chain_length"))
|
|
||||||
|
|
||||||
tmp_path = dst.with_suffix(dst.suffix + ".restore-tmp")
|
|
||||||
tmp_path.write_text(json.dumps(incoming, indent=2) + "\n", encoding="utf-8")
|
|
||||||
os.replace(tmp_path, dst)
|
|
||||||
|
|
||||||
|
|
||||||
def restore_backup(
|
def restore_backup(
|
||||||
zip_path: Path,
|
zip_path: Path,
|
||||||
project_root: Path,
|
project_root: Path,
|
||||||
@@ -644,9 +584,7 @@ def restore_backup(
|
|||||||
# Main config.
|
# Main config.
|
||||||
if options.restore_config and (tmp_dir / _CONFIG_REL).exists():
|
if options.restore_config and (tmp_dir / _CONFIG_REL).exists():
|
||||||
try:
|
try:
|
||||||
_restore_config_preserving_hardware(
|
_copy_file(tmp_dir / _CONFIG_REL, project_root / _CONFIG_REL)
|
||||||
tmp_dir / _CONFIG_REL, project_root / _CONFIG_REL,
|
|
||||||
keep_hardware=not options.restore_hardware)
|
|
||||||
result.restored.append("config")
|
result.restored.append("config")
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
logger.error("[Backup] Failed to restore config.json: %s", e, exc_info=True)
|
logger.error("[Backup] Failed to restore config.json: %s", e, exc_info=True)
|
||||||
|
|||||||
@@ -1,75 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
"""A restore must not repoint this device at another machine's panel.
|
|
||||||
|
|
||||||
display.hardware describes the panel physically wired to this device -- cols,
|
|
||||||
rows, chain_length, hardware_mapping, panel_type, multiplexing, the refresh
|
|
||||||
cap. A backup carries the panel of the machine it was taken on. Restoring a
|
|
||||||
512x64 rig's backup onto a 128x32 one used to overwrite the smaller panel's
|
|
||||||
geometry with the larger one's, and nothing on screen explains why: the
|
|
||||||
display just stops being right.
|
|
||||||
|
|
||||||
That is not hypothetical. It happened, and the rig it happened to had to be
|
|
||||||
reflashed.
|
|
||||||
"""
|
|
||||||
import json
|
|
||||||
import sys
|
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
|
|
||||||
|
|
||||||
from src.backup_manager import _restore_config_preserving_hardware # noqa: E402
|
|
||||||
|
|
||||||
BIG = {"display": {"hardware": {"cols": 128, "rows": 64, "chain_length": 4,
|
|
||||||
"hardware_mapping": "adafruit-hat-pwm"},
|
|
||||||
"runtime": {"gpio_slowdown": 4}},
|
|
||||||
"timezone": "America/New_York", "some-plugin": {"enabled": True}}
|
|
||||||
SMALL = {"display": {"hardware": {"cols": 64, "rows": 32, "chain_length": 2,
|
|
||||||
"hardware_mapping": "regular"},
|
|
||||||
"runtime": {"gpio_slowdown": 2}},
|
|
||||||
"timezone": "UTC"}
|
|
||||||
|
|
||||||
|
|
||||||
def _run(tmp, keep):
|
|
||||||
src = tmp / "backup_config.json"; src.write_text(json.dumps(BIG))
|
|
||||||
dst = tmp / "config.json"; dst.write_text(json.dumps(SMALL))
|
|
||||||
_restore_config_preserving_hardware(src, dst, keep_hardware=keep)
|
|
||||||
return json.loads(dst.read_text())
|
|
||||||
|
|
||||||
|
|
||||||
def test_local_panel_survives(tmp_path):
|
|
||||||
out = _run(tmp_path, keep=True)
|
|
||||||
hw = out["display"]["hardware"]
|
|
||||||
assert (hw["cols"], hw["rows"], hw["chain_length"]) == (64, 32, 2), (
|
|
||||||
"the restore repointed this device at the backup's panel")
|
|
||||||
assert hw["hardware_mapping"] == "regular", "panel wiring came from the backup"
|
|
||||||
|
|
||||||
|
|
||||||
def test_everything_else_is_restored(tmp_path):
|
|
||||||
out = _run(tmp_path, keep=True)
|
|
||||||
assert out["timezone"] == "America/New_York", "config was not restored"
|
|
||||||
assert out["some-plugin"] == {"enabled": True}, "plugin config was not restored"
|
|
||||||
assert out["display"]["runtime"] == {"gpio_slowdown": 4}, (
|
|
||||||
"only display.hardware should be held back")
|
|
||||||
|
|
||||||
|
|
||||||
def test_opting_in_takes_the_backups_panel(tmp_path):
|
|
||||||
out = _run(tmp_path, keep=False)
|
|
||||||
hw = out["display"]["hardware"]
|
|
||||||
assert (hw["cols"], hw["rows"], hw["chain_length"]) == (128, 64, 4)
|
|
||||||
|
|
||||||
|
|
||||||
def test_a_device_with_no_local_hardware_takes_the_backups(tmp_path):
|
|
||||||
src = tmp_path / "b.json"; src.write_text(json.dumps(BIG))
|
|
||||||
dst = tmp_path / "c.json"; dst.write_text(json.dumps({"timezone": "UTC"}))
|
|
||||||
_restore_config_preserving_hardware(src, dst, keep_hardware=True)
|
|
||||||
out = json.loads(dst.read_text())
|
|
||||||
assert out["display"]["hardware"]["cols"] == 128, (
|
|
||||||
"nothing local to preserve, so the backup's panel should be used")
|
|
||||||
|
|
||||||
|
|
||||||
def test_unparseable_local_config_still_restores(tmp_path):
|
|
||||||
src = tmp_path / "b.json"; src.write_text(json.dumps(BIG))
|
|
||||||
dst = tmp_path / "c.json"; dst.write_text("{ not json")
|
|
||||||
_restore_config_preserving_hardware(src, dst, keep_hardware=True)
|
|
||||||
assert json.loads(dst.read_text())["timezone"] == "America/New_York", (
|
|
||||||
"a restore must never fail because of this merge")
|
|
||||||
@@ -0,0 +1,85 @@
|
|||||||
|
"""A check that could not run must not be reported as "up to date".
|
||||||
|
|
||||||
|
check-update returned update_available=False whenever git failed. The banner
|
||||||
|
is the only route to the update button, so a checkout git refuses to touch
|
||||||
|
looked exactly like a current one -- permanently, and with nothing for the
|
||||||
|
user to act on. The usual cause is an install performed as root, after which
|
||||||
|
every git command fails with "detected dubious ownership".
|
||||||
|
"""
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from flask import Flask
|
||||||
|
|
||||||
|
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
|
||||||
|
|
||||||
|
from web_interface.blueprints import api_v3 as mod # noqa: E402
|
||||||
|
from web_interface.blueprints.api_v3 import api_v3 # noqa: E402
|
||||||
|
|
||||||
|
DUBIOUS = ("fatal: detected dubious ownership in repository at "
|
||||||
|
"'/home/pi/LEDMatrix'\nTo add an exception for this directory, call:\n"
|
||||||
|
"\tgit config --global --add safe.directory /home/pi/LEDMatrix\n")
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def client():
|
||||||
|
app = Flask(__name__)
|
||||||
|
app.config['TESTING'] = True
|
||||||
|
app.register_blueprint(api_v3, url_prefix='/api/v3')
|
||||||
|
mod._update_check_cache['result'] = None
|
||||||
|
mod._update_check_cache['ts'] = 0
|
||||||
|
return app.test_client()
|
||||||
|
|
||||||
|
|
||||||
|
def _fetch_fails(stderr: bytes):
|
||||||
|
def fake_run(args, **kwargs):
|
||||||
|
if args[:2] == ['git', 'fetch']:
|
||||||
|
return subprocess.CompletedProcess(args, 1, stdout=b'', stderr=stderr)
|
||||||
|
return subprocess.CompletedProcess(args, 0, stdout='', stderr='')
|
||||||
|
return fake_run
|
||||||
|
|
||||||
|
|
||||||
|
class TestFailedCheckIsNotSilence:
|
||||||
|
def test_dubious_ownership_is_reported_not_swallowed(self, client):
|
||||||
|
with patch.object(mod.subprocess, 'run', _fetch_fails(DUBIOUS.encode())):
|
||||||
|
data = client.get('/api/v3/system/check-update').get_json()
|
||||||
|
assert data['check_failed'] is True, (
|
||||||
|
"a git failure was reported as a successful 'no update' check")
|
||||||
|
assert data['update_available'] is False
|
||||||
|
|
||||||
|
def test_the_message_tells_the_user_what_to_do(self, client):
|
||||||
|
with patch.object(mod.subprocess, 'run', _fetch_fails(DUBIOUS.encode())):
|
||||||
|
data = client.get('/api/v3/system/check-update').get_json()
|
||||||
|
assert 'chown' in data['error'], (
|
||||||
|
"dubious ownership is unactionable without the fix command")
|
||||||
|
assert 'root' in data['error']
|
||||||
|
|
||||||
|
def test_an_ordinary_git_failure_still_surfaces(self, client):
|
||||||
|
with patch.object(mod.subprocess, 'run',
|
||||||
|
_fetch_fails(b'fatal: some other git problem\n')):
|
||||||
|
data = client.get('/api/v3/system/check-update').get_json()
|
||||||
|
assert data['check_failed'] is True
|
||||||
|
assert 'some other git problem' in data['error']
|
||||||
|
|
||||||
|
def test_offline_reads_as_offline(self, client):
|
||||||
|
with patch.object(mod.subprocess, 'run',
|
||||||
|
_fetch_fails(b'fatal: could not resolve host: github.com\n')):
|
||||||
|
data = client.get('/api/v3/system/check-update').get_json()
|
||||||
|
assert 'Could not reach GitHub' in data['error']
|
||||||
|
|
||||||
|
|
||||||
|
class TestSuccessPathUnchanged:
|
||||||
|
def test_up_to_date_carries_no_failure_flag(self, client):
|
||||||
|
def fake_run(args, **kwargs):
|
||||||
|
if args[:2] == ['git', 'fetch']:
|
||||||
|
return subprocess.CompletedProcess(args, 0, stdout=b'', stderr=b'')
|
||||||
|
if args[:2] == ['git', 'rev-parse']:
|
||||||
|
return subprocess.CompletedProcess(args, 0, stdout='abc123\n', stderr='')
|
||||||
|
return subprocess.CompletedProcess(args, 0, stdout='0\n', stderr='')
|
||||||
|
with patch.object(mod.subprocess, 'run', fake_run):
|
||||||
|
data = client.get('/api/v3/system/check-update').get_json()
|
||||||
|
assert data['update_available'] is False
|
||||||
|
assert not data.get('check_failed'), "a healthy check must not look like a failure"
|
||||||
@@ -1821,6 +1821,33 @@ def get_system_version():
|
|||||||
_update_check_cache: Dict[str, Any] = {'result': None, 'ts': 0.0}
|
_update_check_cache: Dict[str, Any] = {'result': None, 'ts': 0.0}
|
||||||
_UPDATE_CHECK_TTL = 300 # 5 minutes — avoids a git fetch on every page load
|
_UPDATE_CHECK_TTL = 300 # 5 minutes — avoids a git fetch on every page load
|
||||||
|
|
||||||
|
def _update_check_failed(detail: str) -> Dict[str, Any]:
|
||||||
|
"""A check that could not run is not the same as being up to date.
|
||||||
|
|
||||||
|
Reporting update_available=False on a git failure hides the banner, and
|
||||||
|
the banner is the only route to the update button -- so a checkout git
|
||||||
|
refuses to touch looks exactly like a current one, permanently. The most
|
||||||
|
common cause is an install performed as root: git then reports "dubious
|
||||||
|
ownership" and every command fails, including the fetch here.
|
||||||
|
"""
|
||||||
|
return {'update_available': False, 'remote_sha': 'unknown',
|
||||||
|
'commits_behind': 0, 'check_failed': True, 'error': detail}
|
||||||
|
|
||||||
|
|
||||||
|
def _describe_git_failure(stderr: str) -> str:
|
||||||
|
"""Turn git's stderr into something the user can act on."""
|
||||||
|
text = (stderr or '').strip()
|
||||||
|
if 'dubious ownership' in text or 'detected dubious ownership' in text:
|
||||||
|
return ("This checkout is owned by a different user than the one "
|
||||||
|
"running the web interface, so git refuses to use it. It is "
|
||||||
|
"usually the result of installing as root. Fix the ownership "
|
||||||
|
"and the update will work: sudo chown -R $USER:$USER "
|
||||||
|
+ str(PROJECT_ROOT))
|
||||||
|
if 'could not resolve host' in text.lower() or 'network is unreachable' in text.lower():
|
||||||
|
return "Could not reach GitHub to check for updates."
|
||||||
|
return "Could not check for updates: " + (text.splitlines()[0] if text else "git failed")
|
||||||
|
|
||||||
|
|
||||||
@api_v3.route('/system/check-update', methods=['GET'])
|
@api_v3.route('/system/check-update', methods=['GET'])
|
||||||
def check_for_update():
|
def check_for_update():
|
||||||
"""Check whether a newer LEDMatrix commit is available on origin/main."""
|
"""Check whether a newer LEDMatrix commit is available on origin/main."""
|
||||||
@@ -1836,12 +1863,13 @@ def check_for_update():
|
|||||||
capture_output=True, timeout=10, cwd=cwd,
|
capture_output=True, timeout=10, cwd=cwd,
|
||||||
)
|
)
|
||||||
if fetch_result.returncode != 0:
|
if fetch_result.returncode != 0:
|
||||||
|
stderr = fetch_result.stderr.decode(errors='replace').strip()
|
||||||
logger.warning("check-update: git fetch failed (rc=%d): %s",
|
logger.warning("check-update: git fetch failed (rc=%d): %s",
|
||||||
fetch_result.returncode,
|
fetch_result.returncode, stderr)
|
||||||
fetch_result.stderr.decode(errors='replace').strip())
|
failed = _update_check_failed(_describe_git_failure(stderr))
|
||||||
_update_check_cache['result'] = _safe
|
_update_check_cache['result'] = failed
|
||||||
_update_check_cache['ts'] = now
|
_update_check_cache['ts'] = now
|
||||||
return jsonify(_safe)
|
return jsonify(failed)
|
||||||
local = subprocess.run(
|
local = subprocess.run(
|
||||||
['git', 'rev-parse', 'HEAD'],
|
['git', 'rev-parse', 'HEAD'],
|
||||||
capture_output=True, text=True, timeout=5, cwd=cwd,
|
capture_output=True, text=True, timeout=5, cwd=cwd,
|
||||||
@@ -1869,7 +1897,8 @@ def check_for_update():
|
|||||||
return jsonify(result)
|
return jsonify(result)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.warning("check-update failed: %s", e)
|
logger.warning("check-update failed: %s", e)
|
||||||
return jsonify(_safe)
|
return jsonify(_update_check_failed(
|
||||||
|
"Could not check for updates; see logs for details."))
|
||||||
|
|
||||||
@api_v3.route('/system/action', methods=['POST'])
|
@api_v3.route('/system/action', methods=['POST'])
|
||||||
def execute_system_action():
|
def execute_system_action():
|
||||||
|
|||||||
@@ -1107,15 +1107,29 @@
|
|||||||
fetch('/api/v3/system/check-update')
|
fetch('/api/v3/system/check-update')
|
||||||
.then(function(r) { return r.json(); })
|
.then(function(r) { return r.json(); })
|
||||||
.then(function(data) {
|
.then(function(data) {
|
||||||
|
var banner = document.getElementById('update-banner');
|
||||||
|
var btn = document.getElementById('update-banner-btn');
|
||||||
|
if (data.check_failed) {
|
||||||
|
// A check that could not run is not the same as being up
|
||||||
|
// to date. Hiding the banner here made a checkout git
|
||||||
|
// refuses to touch look permanently current, with no
|
||||||
|
// route to the update button and nothing to act on.
|
||||||
|
document.getElementById('update-banner-text').textContent =
|
||||||
|
data.error || 'Could not check for updates.';
|
||||||
|
if (btn) btn.style.display = 'none';
|
||||||
|
banner.style.display = '';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (btn) btn.style.display = '';
|
||||||
if (data.update_available && getDismissedSha() !== data.remote_sha) {
|
if (data.update_available && getDismissedSha() !== data.remote_sha) {
|
||||||
var n = data.commits_behind || 0;
|
var n = data.commits_behind || 0;
|
||||||
var msg = 'A new LEDMatrix update is available';
|
var msg = 'A new LEDMatrix update is available';
|
||||||
if (n > 0) msg += ' (' + n + ' commit' + (n > 1 ? 's' : '') + ')';
|
if (n > 0) msg += ' (' + n + ' commit' + (n > 1 ? 's' : '') + ')';
|
||||||
document.getElementById('update-banner-text').textContent = msg;
|
document.getElementById('update-banner-text').textContent = msg;
|
||||||
document.getElementById('update-banner').style.display = '';
|
banner.style.display = '';
|
||||||
try { sessionStorage.setItem('update-sha', data.remote_sha); } catch(e) {}
|
try { sessionStorage.setItem('update-sha', data.remote_sha); } catch(e) {}
|
||||||
} else {
|
} else {
|
||||||
document.getElementById('update-banner').style.display = 'none';
|
banner.style.display = 'none';
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(function() {});
|
.catch(function() {});
|
||||||
|
|||||||
Reference in New Issue
Block a user