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

@@ -8,7 +8,7 @@ AutoDeploy 是一个面向 Windows 的 Spring Boot / SSM + Vue 本地自动化
- 从 Maven、Gradle 和 Spring Boot 配置中推断 Java 版本,推荐 JDK 8、11、17 或 21。 - 从 Maven、Gradle 和 Spring Boot 配置中推断 Java 版本,推荐 JDK 8、11、17 或 21。
- 检测 JDK、Maven、Tomcat、Gradle、Node.js、npm、Vue CLI、MySQL 和 Git。 - 检测 JDK、Maven、Tomcat、Gradle、Node.js、npm、Vue CLI、MySQL 和 Git。
- 支持手动指定工具路径,并可将工具目录写入当前用户的 `PATH`;JDK、Maven、Tomcat 和 Gradle 会同步设置对应的环境变量。 - 支持手动指定工具路径,并可将工具目录写入当前用户的 `PATH`;JDK、Maven、Tomcat 和 Gradle 会同步设置对应的环境变量。
- Maven 和 Tomcat 从 Apache 官方地址下载并校验后安装到当前用户目录;MySQL 从 Oracle 官方归档下载安装包并校验 SHA-256,固定安装 8.0.43,默认将 root 密码设置为 `123456` 并永久允许 6 位弱密码;JDK、Node.js 等环境通过 `winget` 接受软件源和包协议后无人值守安装,执行安装前会要求用户确认。 - Maven 和 Tomcat 从 Apache 官方地址下载并校验后安装到当前用户目录;MySQL 从 Oracle 官方归档下载安装包并校验 SHA-256,固定安装 8.0.43,默认将 root 密码设置为 `123456` 并永久允许 6 位弱密码。MySQL 安装时会显示 Windows Installer 进度窗口,详细日志保存在 `%LOCALAPPDATA%\AutoDeploy\logs\mysql-install.log`。JDK、Node.js 等环境通过 `winget` 接受软件源和包协议后无人值守安装,执行安装前会要求用户确认。
- 使用 Maven 或 Gradle 构建后端;已有可运行 JAR/WAR 时自动跳过重复构建。 - 使用 Maven 或 Gradle 构建后端;已有可运行 JAR/WAR 时自动跳过重复构建。
- 将传统 SSM 项目的 WAR 发布到 Tomcat 9,并等待 8080 端口就绪。 - 将传统 SSM 项目的 WAR 发布到 Tomcat 9,并等待 8080 端口就绪。
- 前端缺少 `node_modules` 时自动执行 `npm install`,随后运行 `serve`、`dev` 或 `start` 脚本。 - 前端缺少 `node_modules` 时自动执行 `npm install`,随后运行 `serve`、`dev` 或 `start` 脚本。

View File

@@ -398,7 +398,8 @@ class DeployWorkbench(tk.Tk):
"确认安装 MySQL", "确认安装 MySQL",
f"将从 MySQL 官方归档下载 MySQL Community Server {MYSQL_VERSION} 完整安装包(约 354 MB)," f"将从 MySQL 官方归档下载 MySQL Community Server {MYSQL_VERSION} 完整安装包(约 354 MB),"
f"校验 SHA-256 后安装。\n\nroot 默认密码为 {MYSQL_ROOT_PASSWORD},并永久允许 6 位弱密码;" f"校验 SHA-256 后安装。\n\nroot 默认密码为 {MYSQL_ROOT_PASSWORD},并永久允许 6 位弱密码;"
"仅开放本机连接,不自动开放 Windows 防火墙端口。\n\n安装时会请求管理员权限,是否继续?", "仅开放本机连接,不自动开放 Windows 防火墙端口。\n\n"
"安装时会请求管理员权限,确认后将显示 Windows Installer 进度窗口。是否继续?",
): ):
return return
self._append_log(f"开始安装 MySQL {MYSQL_VERSION}...") self._append_log(f"开始安装 MySQL {MYSQL_VERSION}...")

View File

@@ -100,7 +100,7 @@ def build_install_command(tool_name: str, version: str | None = None) -> list[st
return command 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 = ( console_args = (
f"CONSOLEARGS=install server;{MYSQL_VERSION};x64:*:" f"CONSOLEARGS=install server;{MYSQL_VERSION};x64:*:"
"type=config;open_win_firewall=false;tcp_ip=true;port=3306;" "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", "msiexec.exe",
"/i", "/i",
str(installer_path), str(installer_path),
"/qn", "/passive",
"/norestart", "/norestart",
"/L*V",
str(log_path),
console_args, 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: def find_mysql_executable() -> Path | None:
discovered = shutil.which("mysql") discovered = shutil.which("mysql")
if discovered: if discovered:
@@ -215,9 +223,13 @@ def install_mysql(
on_output: Callable[[str], None], on_output: Callable[[str], None],
opener: Callable[..., BinaryIO] | None = None, opener: Callable[..., BinaryIO] | None = None,
elevated_runner: Callable[[list[str]], int] | None = None, elevated_runner: Callable[[list[str]], int] | None = None,
log_path: Path | None = None,
) -> Path: ) -> Path:
open_url = opener or urllib.request.urlopen 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: with tempfile.TemporaryDirectory(prefix="autodeploy-mysql-") as temporary:
installer_path = Path(temporary) / MYSQL_INSTALLER_ARCHIVE_NAME installer_path = Path(temporary) / MYSQL_INSTALLER_ARCHIVE_NAME
on_output(f"正在从 MySQL 官方归档地址下载 MySQL Installer {MYSQL_VERSION}...") on_output(f"正在从 MySQL 官方归档地址下载 MySQL Installer {MYSQL_VERSION}...")
@@ -226,9 +238,19 @@ def install_mysql(
if actual_checksum.lower() != MYSQL_INSTALLER_SHA256.lower(): if actual_checksum.lower() != MYSQL_INSTALLER_SHA256.lower():
raise RuntimeError("MySQL 安装包 SHA-256 校验失败,已停止安装。") raise RuntimeError("MySQL 安装包 SHA-256 校验失败,已停止安装。")
on_output("MySQL 安装包校验通过,正在请求管理员权限安装...") 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}: 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}: if exit_code in {1641, 3010}:
on_output("MySQL 安装完成,Windows 提示稍后需要重新启动。") on_output("MySQL 安装完成,Windows 提示稍后需要重新启动。")
executable = wait_for_mysql_executable() executable = wait_for_mysql_executable()
@@ -236,7 +258,11 @@ def install_mysql(
return executable 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": if os.name != "nt":
return subprocess.run(command, shell=False).returncode return subprocess.run(command, shell=False).returncode
@@ -270,14 +296,27 @@ def run_elevated_command(command: list[str]) -> int:
info.lpVerb = "runas" info.lpVerb = "runas"
info.lpFile = command[0] info.lpFile = command[0]
info.lpParameters = subprocess.list2cmdline(command[1:]) 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)): if not shell_execute(ctypes.byref(info)):
error_code = ctypes.windll.kernel32.GetLastError() error_code = ctypes.windll.kernel32.GetLastError()
if error_code == 1223: if error_code == 1223:
raise RuntimeError("用户取消了 MySQL 安装所需的管理员授权。") raise RuntimeError("用户取消了 MySQL 安装所需的管理员授权。")
raise OSError(error_code, "无法以管理员权限启动 MySQL 安装程序") raise OSError(error_code, "无法以管理员权限启动 MySQL 安装程序")
try: 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() exit_code = wintypes.DWORD()
if not ctypes.windll.kernel32.GetExitCodeProcess(info.hProcess, ctypes.byref(exit_code)): if not ctypes.windll.kernel32.GetExitCodeProcess(info.hProcess, ctypes.byref(exit_code)):
raise OSError(ctypes.windll.kernel32.GetLastError(), "无法读取 MySQL 安装程序退出码") raise OSError(ctypes.windll.kernel32.GetLastError(), "无法读取 MySQL 安装程序退出码")

View File

@@ -17,6 +17,7 @@ from deploy_tool.installer import (
MYSQL_INSTALLER_URL, MYSQL_INSTALLER_URL,
build_mysql_installer_command, build_mysql_installer_command,
configure_mysql_weak_password_policy, configure_mysql_weak_password_policy,
default_mysql_install_log_path,
TOMCAT_VERSION, TOMCAT_VERSION,
install_maven, install_maven,
install_mysql, install_mysql,
@@ -111,9 +112,13 @@ class InstallerTests(unittest.TestCase):
self.assertIn("--disable-interactivity", command) self.assertIn("--disable-interactivity", command)
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")) command = build_mysql_installer_command(Path("mysql-installer.msi"), Path("mysql-install.log"))
self.assertEqual(command[0:3], ["msiexec.exe", "/i", "mysql-installer.msi"]) self.assertEqual(command[0:3], ["msiexec.exe", "/i", "mysql-installer.msi"])
self.assertIn("/passive", command)
self.assertNotIn("/qn", command)
self.assertIn("/L*V", command)
self.assertIn("mysql-install.log", command)
self.assertEqual(MYSQL_VERSION, "8.0.43") self.assertEqual(MYSQL_VERSION, "8.0.43")
self.assertIn("root_passwd=123456", command[-1]) self.assertIn("root_passwd=123456", command[-1])
self.assertIn("open_win_firewall=false", command[-1]) self.assertIn("open_win_firewall=false", command[-1])
@@ -137,19 +142,39 @@ class InstallerTests(unittest.TestCase):
self.assertTrue(Path(command[2]).exists()) self.assertTrue(Path(command[2]).exists())
return 0 return 0
with ( with tempfile.TemporaryDirectory() as tmp:
patch("deploy_tool.installer.MYSQL_INSTALLER_SHA256", checksum), install_log = Path(tmp) / "logs" / "mysql-install.log"
patch("deploy_tool.installer.wait_for_mysql_executable", return_value=Path("mysql.exe")), with (
patch("deploy_tool.installer.configure_mysql_weak_password_policy") as configure, patch("deploy_tool.installer.MYSQL_INSTALLER_SHA256", checksum),
): patch("deploy_tool.installer.wait_for_mysql_executable", return_value=Path("mysql.exe")),
messages = [] patch("deploy_tool.installer.configure_mysql_weak_password_policy") as configure,
executable = install_mysql(messages.append, opener=opener, elevated_runner=elevated_runner) ):
messages = []
executable = install_mysql(
messages.append,
opener=opener,
elevated_runner=elevated_runner,
log_path=install_log,
)
self.assertEqual(executable, Path("mysql.exe")) self.assertEqual(executable, Path("mysql.exe"))
self.assertEqual(commands[0][0], "msiexec.exe") self.assertEqual(commands[0][0], "msiexec.exe")
self.assertIn("/passive", commands[0])
self.assertIn(str(install_log), commands[0])
self.assertTrue(any("校验通过" in message for message in messages)) self.assertTrue(any("校验通过" in message for message in messages))
self.assertTrue(any("进度窗口" in message for message in messages))
self.assertTrue(any(str(install_log) in message for message in messages))
configure.assert_called_once() configure.assert_called_once()
def test_uses_persistent_mysql_install_log_location(self):
with patch.dict("os.environ", {"LOCALAPPDATA": r"C:\Users\tester\AppData\Local"}):
log_path = default_mysql_install_log_path()
self.assertEqual(
log_path,
Path(r"C:\Users\tester\AppData\Local") / "AutoDeploy" / "logs" / "mysql-install.log",
)
def test_windows_elevation_dependency_is_imported(self): def test_windows_elevation_dependency_is_imported(self):
self.assertIs(installer_module.ctypes, ctypes) self.assertIs(installer_module.ctypes, ctypes)