Retry Node downloads with Windows trust
This commit is contained in:
@@ -6,9 +6,11 @@ import re
|
|||||||
import hashlib
|
import hashlib
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
|
import ssl
|
||||||
import subprocess
|
import subprocess
|
||||||
import tempfile
|
import tempfile
|
||||||
import time
|
import time
|
||||||
|
import urllib.error
|
||||||
import urllib.request
|
import urllib.request
|
||||||
import zipfile
|
import zipfile
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
@@ -834,9 +836,16 @@ def install_node(
|
|||||||
) -> tuple[Path, Path, str]:
|
) -> tuple[Path, Path, str]:
|
||||||
root = Path(install_root) if install_root is not None else default_maven_install_root()
|
root = Path(install_root) if install_root is not None else default_maven_install_root()
|
||||||
open_url = opener or urllib.request.urlopen
|
open_url = opener or urllib.request.urlopen
|
||||||
|
allow_windows_fallback = opener is None and os.name == "nt"
|
||||||
on_output(f"正在读取 Node.js 官方版本索引,项目要求: {' 且 '.join(requirements)}")
|
on_output(f"正在读取 Node.js 官方版本索引,项目要求: {' 且 '.join(requirements)}")
|
||||||
with open_url(f"{NODE_DIST_BASE}/index.json", timeout=30) as response:
|
index_data = _read_url_bytes(
|
||||||
release = resolve_node_release(response.read(), requirements)
|
f"{NODE_DIST_BASE}/index.json",
|
||||||
|
open_url,
|
||||||
|
30,
|
||||||
|
on_output,
|
||||||
|
allow_windows_fallback=allow_windows_fallback,
|
||||||
|
)
|
||||||
|
release = resolve_node_release(index_data, requirements)
|
||||||
|
|
||||||
version = release.version
|
version = release.version
|
||||||
directory_name = f"node-v{version}-win-x64"
|
directory_name = f"node-v{version}-win-x64"
|
||||||
@@ -855,8 +864,21 @@ def install_node(
|
|||||||
temp_dir = Path(temporary)
|
temp_dir = Path(temporary)
|
||||||
archive_path = temp_dir / archive_name
|
archive_path = temp_dir / archive_name
|
||||||
on_output(f"已自动选择 Node.js {version},正在从官方地址下载...")
|
on_output(f"已自动选择 Node.js {version},正在从官方地址下载...")
|
||||||
_download_to_file(archive_url, archive_path, open_url, on_output)
|
_download_to_file_with_windows_fallback(
|
||||||
expected_checksum = _download_node_checksum(checksum_url, archive_name, open_url)
|
archive_url,
|
||||||
|
archive_path,
|
||||||
|
open_url,
|
||||||
|
on_output,
|
||||||
|
allow_windows_fallback=allow_windows_fallback,
|
||||||
|
)
|
||||||
|
checksum_data = _read_url_bytes(
|
||||||
|
checksum_url,
|
||||||
|
open_url,
|
||||||
|
30,
|
||||||
|
on_output,
|
||||||
|
allow_windows_fallback=allow_windows_fallback,
|
||||||
|
)
|
||||||
|
expected_checksum = _parse_node_checksum(checksum_data, archive_name)
|
||||||
actual_checksum = _sha256(archive_path)
|
actual_checksum = _sha256(archive_path)
|
||||||
if actual_checksum.lower() != expected_checksum.lower():
|
if actual_checksum.lower() != expected_checksum.lower():
|
||||||
raise RuntimeError("Node.js 安装包 SHA-256 校验失败,已停止安装。")
|
raise RuntimeError("Node.js 安装包 SHA-256 校验失败,已停止安装。")
|
||||||
@@ -872,13 +894,8 @@ def install_node(
|
|||||||
return node_executable, npm_executable, version
|
return node_executable, npm_executable, version
|
||||||
|
|
||||||
|
|
||||||
def _download_node_checksum(
|
def _parse_node_checksum(data: bytes, archive_name: str) -> str:
|
||||||
url: str,
|
text = data.decode("ascii", errors="strict")
|
||||||
archive_name: str,
|
|
||||||
opener: Callable[..., BinaryIO],
|
|
||||||
) -> str:
|
|
||||||
with opener(url, timeout=30) as response:
|
|
||||||
text = response.read().decode("ascii", errors="strict")
|
|
||||||
for line in text.splitlines():
|
for line in text.splitlines():
|
||||||
parts = line.split()
|
parts = line.split()
|
||||||
if len(parts) >= 2 and parts[-1].lstrip("*") == archive_name:
|
if len(parts) >= 2 and parts[-1].lstrip("*") == archive_name:
|
||||||
@@ -888,6 +905,97 @@ def _download_node_checksum(
|
|||||||
raise RuntimeError(f"Node.js 校验清单中未找到 {archive_name}。")
|
raise RuntimeError(f"Node.js 校验清单中未找到 {archive_name}。")
|
||||||
|
|
||||||
|
|
||||||
|
def _read_url_bytes(
|
||||||
|
url: str,
|
||||||
|
opener: Callable[..., BinaryIO],
|
||||||
|
timeout: int,
|
||||||
|
on_output: Callable[[str], None],
|
||||||
|
*,
|
||||||
|
allow_windows_fallback: bool,
|
||||||
|
) -> bytes:
|
||||||
|
try:
|
||||||
|
with opener(url, timeout=timeout) as response:
|
||||||
|
return response.read()
|
||||||
|
except Exception as exc:
|
||||||
|
if not allow_windows_fallback or not _is_certificate_verification_failure(exc):
|
||||||
|
raise
|
||||||
|
on_output("Python 未信任当前网络证书,正在改用 Windows 系统证书存储重试下载...")
|
||||||
|
with tempfile.TemporaryDirectory(prefix="autodeploy-windows-download-") as temporary:
|
||||||
|
destination = Path(temporary) / "download.bin"
|
||||||
|
_download_with_windows_certificates(url, destination, timeout)
|
||||||
|
return destination.read_bytes()
|
||||||
|
|
||||||
|
|
||||||
|
def _download_to_file_with_windows_fallback(
|
||||||
|
url: str,
|
||||||
|
destination: Path,
|
||||||
|
opener: Callable[..., BinaryIO],
|
||||||
|
on_output: Callable[[str], None],
|
||||||
|
*,
|
||||||
|
allow_windows_fallback: bool,
|
||||||
|
) -> None:
|
||||||
|
try:
|
||||||
|
_download_to_file(url, destination, opener, on_output)
|
||||||
|
except Exception as exc:
|
||||||
|
if not allow_windows_fallback or not _is_certificate_verification_failure(exc):
|
||||||
|
raise
|
||||||
|
destination.unlink(missing_ok=True)
|
||||||
|
on_output("Python 未信任当前网络证书,正在改用 Windows 系统证书存储重试下载...")
|
||||||
|
_download_with_windows_certificates(url, destination, 60)
|
||||||
|
|
||||||
|
|
||||||
|
def _is_certificate_verification_failure(exc: BaseException) -> bool:
|
||||||
|
if isinstance(exc, ssl.SSLCertVerificationError):
|
||||||
|
return True
|
||||||
|
reason = getattr(exc, "reason", None)
|
||||||
|
if isinstance(reason, ssl.SSLCertVerificationError):
|
||||||
|
return True
|
||||||
|
message = str(exc).upper()
|
||||||
|
return "CERTIFICATE_VERIFY_FAILED" in message or "SELF-SIGNED CERTIFICATE" in message
|
||||||
|
|
||||||
|
|
||||||
|
def _download_with_windows_certificates(url: str, destination: Path, timeout: int) -> None:
|
||||||
|
powershell = shutil.which("powershell.exe") or str(
|
||||||
|
Path(os.environ.get("SystemRoot", r"C:\Windows"))
|
||||||
|
/ "System32"
|
||||||
|
/ "WindowsPowerShell"
|
||||||
|
/ "v1.0"
|
||||||
|
/ "powershell.exe"
|
||||||
|
)
|
||||||
|
script = (
|
||||||
|
"& { param([string]$Url,[string]$Destination,[int]$TimeoutSeconds) "
|
||||||
|
"$ErrorActionPreference='Stop'; $ProgressPreference='SilentlyContinue'; "
|
||||||
|
"Invoke-WebRequest -UseBasicParsing -Uri $Url -OutFile $Destination -TimeoutSec $TimeoutSeconds }"
|
||||||
|
)
|
||||||
|
completed = subprocess.run(
|
||||||
|
[
|
||||||
|
powershell,
|
||||||
|
"-NoProfile",
|
||||||
|
"-NonInteractive",
|
||||||
|
"-ExecutionPolicy",
|
||||||
|
"Bypass",
|
||||||
|
"-Command",
|
||||||
|
script,
|
||||||
|
url,
|
||||||
|
str(destination),
|
||||||
|
str(timeout),
|
||||||
|
],
|
||||||
|
stdout=subprocess.PIPE,
|
||||||
|
stderr=subprocess.STDOUT,
|
||||||
|
text=True,
|
||||||
|
errors="replace",
|
||||||
|
shell=False,
|
||||||
|
creationflags=getattr(subprocess, "CREATE_NO_WINDOW", 0) if os.name == "nt" else 0,
|
||||||
|
)
|
||||||
|
if completed.returncode != 0 or not destination.is_file() or destination.stat().st_size == 0:
|
||||||
|
detail = completed.stdout.strip() or f"退出码 {completed.returncode}"
|
||||||
|
raise RuntimeError(
|
||||||
|
"Windows 系统证书存储也无法完成 HTTPS 下载。"
|
||||||
|
"请将网络代理的根证书安装到 Windows 受信任的根证书颁发机构。"
|
||||||
|
f"\n{detail}"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def _node_version_matches(version: str, requirement: str, lts: object) -> bool:
|
def _node_version_matches(version: str, requirement: str, lts: object) -> bool:
|
||||||
text = requirement.strip().lower()
|
text = requirement.strip().lower()
|
||||||
if text in {"", "*", "node", "latest"}:
|
if text in {"", "*", "node", "latest"}:
|
||||||
|
|||||||
@@ -2,9 +2,11 @@ import hashlib
|
|||||||
import io
|
import io
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
import ssl
|
||||||
import subprocess
|
import subprocess
|
||||||
import tempfile
|
import tempfile
|
||||||
import unittest
|
import unittest
|
||||||
|
import urllib.error
|
||||||
import zipfile
|
import zipfile
|
||||||
import ctypes
|
import ctypes
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
@@ -195,6 +197,49 @@ class InstallerTests(unittest.TestCase):
|
|||||||
self.assertTrue(npm.exists())
|
self.assertTrue(npm.exists())
|
||||||
self.assertTrue(any("自动选择 Node.js 20.12.2" in message for message in messages))
|
self.assertTrue(any("自动选择 Node.js 20.12.2" in message for message in messages))
|
||||||
|
|
||||||
|
def test_node_download_retries_with_windows_certificate_store(self):
|
||||||
|
version = "20.12.2"
|
||||||
|
directory = f"node-v{version}-win-x64"
|
||||||
|
archive_name = f"{directory}.zip"
|
||||||
|
archive_buffer = io.BytesIO()
|
||||||
|
with zipfile.ZipFile(archive_buffer, "w") as archive:
|
||||||
|
archive.writestr(f"{directory}/node.exe", "node")
|
||||||
|
archive.writestr(f"{directory}/npm.cmd", "npm")
|
||||||
|
archive_bytes = archive_buffer.getvalue()
|
||||||
|
checksum = hashlib.sha256(archive_bytes).hexdigest()
|
||||||
|
index_bytes = json.dumps(
|
||||||
|
[{"version": f"v{version}", "lts": "Iron", "files": ["win-x64-zip"]}]
|
||||||
|
).encode("utf-8")
|
||||||
|
|
||||||
|
def certificate_failure(_url, timeout):
|
||||||
|
raise urllib.error.URLError(ssl.SSLCertVerificationError("self-signed certificate"))
|
||||||
|
|
||||||
|
def windows_download(url, destination, timeout):
|
||||||
|
if url.endswith("index.json"):
|
||||||
|
payload = index_bytes
|
||||||
|
elif url.endswith("SHASUMS256.txt"):
|
||||||
|
payload = f"{checksum} {archive_name}\n".encode("ascii")
|
||||||
|
else:
|
||||||
|
payload = archive_bytes
|
||||||
|
destination.write_bytes(payload)
|
||||||
|
|
||||||
|
with tempfile.TemporaryDirectory() as tmp:
|
||||||
|
with (
|
||||||
|
patch("deploy_tool.installer.urllib.request.urlopen", side_effect=certificate_failure),
|
||||||
|
patch(
|
||||||
|
"deploy_tool.installer._download_with_windows_certificates",
|
||||||
|
side_effect=windows_download,
|
||||||
|
) as fallback,
|
||||||
|
patch("deploy_tool.installer.os.name", "nt"),
|
||||||
|
):
|
||||||
|
messages = []
|
||||||
|
node, npm, installed_version = install_node((">=18 <21",), messages.append, Path(tmp))
|
||||||
|
self.assertEqual(installed_version, version)
|
||||||
|
self.assertTrue(node.exists())
|
||||||
|
self.assertTrue(npm.exists())
|
||||||
|
self.assertEqual(fallback.call_count, 3)
|
||||||
|
self.assertTrue(any("Windows 系统证书存储" in message for message in messages))
|
||||||
|
|
||||||
def test_builds_unattended_mysql_msi_install_command(self):
|
def test_builds_unattended_mysql_msi_install_command(self):
|
||||||
command = build_mysql_installer_command(Path("mysql-installer.msi"), Path("mysql-install.log"))
|
command = build_mysql_installer_command(Path("mysql-installer.msi"), Path("mysql-install.log"))
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user