Release v117 with persistent logs and WAN tunnel recovery
This commit is contained in:
1 parent
5f65a3d811
commit
b62db40daf
61 files changed
+3080
-438
No files matched your search
@@ -0,0 +1,187 @@
|
||||
param(
|
||||
[ValidateNotNullOrEmpty()][string]$Vps = 'root@193.233.82.38',
|
||||
[ValidateSet('Install', 'Rollback')][string]$Action = 'Install',
|
||||
[switch]$PrepareOnly
|
||||
)
|
||||
|
||||
$ErrorActionPreference = 'Stop'
|
||||
$RepoRoot = Split-Path -Parent $PSScriptRoot
|
||||
$UploadName = 'owrt-persistent-logs-vps-' + [guid]::NewGuid().ToString('N')
|
||||
$StageRoot = Join-Path ([System.IO.Path]::GetTempPath()) $UploadName
|
||||
$Utf8 = [System.Text.UTF8Encoding]::new($false)
|
||||
|
||||
$RemoteScript = @'
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
stage=/tmp/__UPLOAD_NAME__
|
||||
target=/opt/owrt-remote/owrt-remote-hub.py
|
||||
backup=/opt/owrt-remote/owrt-remote-hub.py.bak-before-persistent-logs-sync-v117
|
||||
candidate=$target.new-__UPLOAD_NAME__
|
||||
changed=0
|
||||
|
||||
show_failure() {
|
||||
echo '--- owrt-remote status ---' >&2
|
||||
systemctl --no-pager -l status owrt-remote >&2 || true
|
||||
echo '--- owrt-remote journal ---' >&2
|
||||
journalctl -u owrt-remote -n 80 --no-pager -l >&2 || true
|
||||
}
|
||||
|
||||
cleanup() {
|
||||
code=$?
|
||||
trap - EXIT
|
||||
if [ "$code" -ne 0 ]; then
|
||||
echo "Remote step failed with rc=$code" >&2
|
||||
show_failure
|
||||
fi
|
||||
if [ "$code" -ne 0 ] && [ "$changed" = 1 ] && [ -s "$stage/previous.py" ]; then
|
||||
echo 'Restoring the previous Hub file...' >&2
|
||||
cp -p "$stage/previous.py" "$candidate"
|
||||
mv -f "$candidate" "$target"
|
||||
systemctl restart owrt-remote || true
|
||||
fi
|
||||
rm -f "$candidate"
|
||||
rm -rf "$stage"
|
||||
exit "$code"
|
||||
}
|
||||
trap cleanup EXIT
|
||||
|
||||
test -s "$target"
|
||||
python_bin=/opt/owrt-remote/venv/bin/python
|
||||
[ -x "$python_bin" ] || python_bin=python3
|
||||
|
||||
if [ '__ACTION__' = Install ]; then
|
||||
test -s "$stage/hub.py"
|
||||
echo 'Validating candidate...'
|
||||
"$python_bin" -m py_compile "$stage/hub.py"
|
||||
"$python_bin" - "$stage/hub.py" <<'PY'
|
||||
from pathlib import Path
|
||||
import sys
|
||||
source = Path(sys.argv[1]).read_text(encoding='utf-8')
|
||||
required = (
|
||||
'def receive_router_logs(',
|
||||
'def router_log_storage(',
|
||||
'def router_log_diagnosis(',
|
||||
'def router_logs_html(',
|
||||
'def router_logs_api(',
|
||||
'data-global-logs-link=',
|
||||
'ROUTER_LOG_MAX_BYTES = 2 * 1024 * 1024',
|
||||
)
|
||||
missing = [marker for marker in required if marker not in source]
|
||||
if missing:
|
||||
for marker in missing:
|
||||
print('Missing marker:', repr(marker), file=sys.stderr)
|
||||
raise SystemExit(1)
|
||||
print('Candidate validation OK')
|
||||
PY
|
||||
# Read only the running Hub's relevant environment/argv; never print tokens.
|
||||
hub_pid=$(systemctl show -p MainPID --value owrt-remote)
|
||||
case "$hub_pid" in ''|*[!0-9]*|0) echo 'Cannot identify the running Hub storage.' >&2; exit 1 ;; esac
|
||||
"$python_bin" - "$stage/hub.py" "$hub_pid" <<'PY'
|
||||
import importlib.util
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import sys
|
||||
sys.dont_write_bytecode = True
|
||||
pid = sys.argv[2]
|
||||
environment = Path('/proc/' + pid + '/environ').read_bytes().split(b'\0')
|
||||
for item in environment:
|
||||
key, _, value = item.partition(b'=')
|
||||
if key in (b'OWRT_REMOTE_STATE_DIR', b'OWRT_REMOTE_DB'):
|
||||
os.environ[key.decode()] = os.fsdecode(value)
|
||||
spec = importlib.util.spec_from_file_location('storage_preflight', sys.argv[1])
|
||||
hub = importlib.util.module_from_spec(spec)
|
||||
sys.modules[spec.name] = hub
|
||||
spec.loader.exec_module(hub)
|
||||
db_path = hub.DB_PATH
|
||||
args = Path('/proc/' + pid + '/cmdline').read_bytes().split(b'\0')
|
||||
for index, arg in enumerate(args):
|
||||
if arg == b'--db' and index + 1 < len(args):
|
||||
db_path = Path(os.fsdecode(args[index + 1]))
|
||||
elif arg.startswith(b'--db='):
|
||||
db_path = Path(os.fsdecode(arg[5:]))
|
||||
if not db_path.is_absolute():
|
||||
db_path = Path('/proc/' + pid + '/cwd').resolve() / db_path
|
||||
storage = hub.router_log_storage(db_path)
|
||||
print('VPS log archive storage:', json.dumps(storage, ensure_ascii=False))
|
||||
if storage['persistent'] is not True or not storage.get('writable'):
|
||||
raise SystemExit('Archive is not confirmed on writable persistent disk. Nothing installed; check the Hub database mount.')
|
||||
if not db_path.is_file() or not os.access(db_path, os.W_OK):
|
||||
raise SystemExit('Hub database is missing or not writable. Nothing installed.')
|
||||
PY
|
||||
if [ ! -e "$backup" ]; then
|
||||
cp -p "$target" "$backup"
|
||||
fi
|
||||
source_file="$stage/hub.py"
|
||||
else
|
||||
test -s "$backup"
|
||||
"$python_bin" -m py_compile "$backup"
|
||||
source_file="$backup"
|
||||
fi
|
||||
|
||||
cp -p "$target" "$stage/previous.py"
|
||||
cp "$source_file" "$candidate"
|
||||
chmod 0755 "$candidate"
|
||||
changed=1
|
||||
mv -f "$candidate" "$target"
|
||||
echo 'Restarting owrt-remote...'
|
||||
if ! systemctl restart owrt-remote; then
|
||||
show_failure
|
||||
exit 1
|
||||
fi
|
||||
|
||||
healthy=0
|
||||
for attempt in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15; do
|
||||
if systemctl is-active --quiet owrt-remote && curl -fsS --max-time 5 http://127.0.0.1:8088/health; then
|
||||
healthy=1
|
||||
break
|
||||
fi
|
||||
sleep 2
|
||||
done
|
||||
if [ "$healthy" -ne 1 ]; then
|
||||
echo 'Health check did not become ready.' >&2
|
||||
show_failure
|
||||
exit 1
|
||||
fi
|
||||
cmp "$source_file" "$target"
|
||||
printf '\nBackup: %s\nPERSISTENT_LOGS_VPS_V117_%s_OK\n' "$backup" '__ACTION__'
|
||||
'@
|
||||
|
||||
$RemoteScript = $RemoteScript.Replace('__UPLOAD_NAME__', $UploadName).Replace('__ACTION__', $Action)
|
||||
|
||||
try {
|
||||
[System.IO.Directory]::CreateDirectory($StageRoot) | Out-Null
|
||||
[System.IO.File]::WriteAllText(
|
||||
(Join-Path $StageRoot 'run.sh'),
|
||||
$RemoteScript.Replace("`r`n", "`n") + "`n",
|
||||
$Utf8
|
||||
)
|
||||
|
||||
if ($Action -eq 'Install') {
|
||||
$Content = [System.IO.File]::ReadAllText((Join-Path $RepoRoot 'vps\owrt-remote-hub.py')).Replace("`r`n", "`n")
|
||||
[System.IO.File]::WriteAllText((Join-Path $StageRoot 'hub.py'), $Content, $Utf8)
|
||||
}
|
||||
|
||||
if ($PrepareOnly) {
|
||||
Write-Host "Prepared only: $StageRoot"
|
||||
return
|
||||
}
|
||||
|
||||
& scp -O -r -- $StageRoot "${Vps}:/tmp/"
|
||||
if ($LASTEXITCODE -ne 0) { throw 'Upload failed. VPS was not changed.' }
|
||||
& ssh -T -o ConnectTimeout=10 $Vps "sh /tmp/$UploadName/run.sh"
|
||||
if ($LASTEXITCODE -ne 0) { throw "$Action failed. See the output above; success was not confirmed." }
|
||||
Write-Host "Persistent logs VPS $Action finished successfully. Refresh Hub with Ctrl+F5."
|
||||
}
|
||||
finally {
|
||||
if (-not $PrepareOnly) {
|
||||
$Resolved = [System.IO.Path]::GetFullPath($StageRoot)
|
||||
$Expected = [System.IO.Path]::GetFullPath((Join-Path ([System.IO.Path]::GetTempPath()) $UploadName))
|
||||
if ($Resolved -ne $Expected -or (Split-Path -Leaf $Resolved) -ne $UploadName) {
|
||||
throw 'Unsafe staging cleanup path.'
|
||||
}
|
||||
if (Test-Path -LiteralPath $Resolved) {
|
||||
Remove-Item -LiteralPath $Resolved -Recurse -Force
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user