Show MySQL installation progress

This commit is contained in:
王鹏
2026-10-09 09:48:42 +08:00
parent eb5a276f44
commit 51cb348731
4 changed files with 83 additions and 18 deletions

View File

@@ -100,7 +100,7 @@ def build_install_command(tool_name: str, version: str | None = None) -> list[st
return command
def build_mysql_installer_command(installer_path: Path) -> list[str]:
def build_mysql_installer_command(installer_path: Path, log_path: Path) -> list[str]:
console_args = (
f"CONSOLEARGS=install server;{MYSQL_VERSION};x64:*:"
"type=config;open_win_firewall=false;tcp_ip=true;port=3306;"
@@ -110,12 +110,20 @@ def build_mysql_installer_command(installer_path: Path) -> list[str]:
"msiexec.exe",
"/i",
str(installer_path),
"/qn",
"/passive",
"/norestart",
"/L*V",
str(log_path),
console_args,
]
def default_mysql_install_log_path() -> Path:
local_app_data = os.environ.get("LOCALAPPDATA", "").strip()
base = Path(local_app_data) if local_app_data else Path.home() / ".autodeploy"
return base / "AutoDeploy" / "logs" / "mysql-install.log"
def find_mysql_executable() -> Path | None:
discovered = shutil.which("mysql")
if discovered:
@@ -215,9 +223,13 @@ def install_mysql(
on_output: Callable[[str], None],
opener: Callable[..., BinaryIO] | None = None,
elevated_runner: Callable[[list[str]], int] | None = None,
log_path: Path | None = None,
) -> Path:
open_url = opener or urllib.request.urlopen
run_elevated = elevated_runner or run_elevated_command
install_log = Path(log_path) if log_path is not None else default_mysql_install_log_path()
install_log.parent.mkdir(parents=True, exist_ok=True)
if install_log.exists():
install_log.unlink()
with tempfile.TemporaryDirectory(prefix="autodeploy-mysql-") as temporary:
installer_path = Path(temporary) / MYSQL_INSTALLER_ARCHIVE_NAME
on_output(f"正在从 MySQL 官方归档地址下载 MySQL Installer {MYSQL_VERSION}...")
@@ -226,9 +238,19 @@ def install_mysql(
if actual_checksum.lower() != MYSQL_INSTALLER_SHA256.lower():
raise RuntimeError("MySQL 安装包 SHA-256 校验失败,已停止安装。")
on_output("MySQL 安装包校验通过,正在请求管理员权限安装...")
exit_code = run_elevated(build_mysql_installer_command(installer_path))
on_output("确认管理员授权后会显示 Windows Installer 进度窗口,请勿关闭 AutoDeploy。")
on_output(f"MySQL 详细安装日志: {install_log}")
command = build_mysql_installer_command(installer_path, install_log)
if elevated_runner is None:
exit_code = run_elevated_command(
command,
on_wait=lambda: on_output("MySQL 安装仍在进行,请继续等待进度窗口完成..."),
)
else:
exit_code = elevated_runner(command)
if exit_code not in {0, 1641, 3010}:
raise RuntimeError(f"MySQL 安装命令失败,退出码: {exit_code}")
raise RuntimeError(f"MySQL 安装命令失败,退出码: {exit_code}。详细日志: {install_log}")
on_output("Windows Installer 已完成,正在检测 MySQL 服务和命令行工具...")
if exit_code in {1641, 3010}:
on_output("MySQL 安装完成,Windows 提示稍后需要重新启动。")
executable = wait_for_mysql_executable()
@@ -236,7 +258,11 @@ def install_mysql(
return executable
def run_elevated_command(command: list[str]) -> int:
def run_elevated_command(
command: list[str],
on_wait: Callable[[], None] | None = None,
wait_notice_interval: float = 30.0,
) -> int:
if os.name != "nt":
return subprocess.run(command, shell=False).returncode
@@ -270,14 +296,27 @@ def run_elevated_command(command: list[str]) -> int:
info.lpVerb = "runas"
info.lpFile = command[0]
info.lpParameters = subprocess.list2cmdline(command[1:])
info.nShow = 0
info.nShow = 1 # SW_SHOWNORMAL: show the Windows Installer progress window.
if not shell_execute(ctypes.byref(info)):
error_code = ctypes.windll.kernel32.GetLastError()
if error_code == 1223:
raise RuntimeError("用户取消了 MySQL 安装所需的管理员授权。")
raise OSError(error_code, "无法以管理员权限启动 MySQL 安装程序")
try:
ctypes.windll.kernel32.WaitForSingleObject(info.hProcess, 0xFFFFFFFF)
wait_timeout_ms = 1000
wait_timeout = 0x00000102
wait_object_0 = 0x00000000
last_notice = time.monotonic()
while True:
wait_result = ctypes.windll.kernel32.WaitForSingleObject(info.hProcess, wait_timeout_ms)
if wait_result == wait_object_0:
break
if wait_result != wait_timeout:
raise OSError(ctypes.windll.kernel32.GetLastError(), "等待 MySQL 安装程序时发生错误")
now = time.monotonic()
if on_wait is not None and now - last_notice >= wait_notice_interval:
on_wait()
last_notice = now
exit_code = wintypes.DWORD()
if not ctypes.windll.kernel32.GetExitCodeProcess(info.hProcess, ctypes.byref(exit_code)):
raise OSError(ctypes.windll.kernel32.GetLastError(), "无法读取 MySQL 安装程序退出码")