Track MySQL server installation separately
This commit is contained in:
@@ -38,6 +38,7 @@ TOMCAT_ARCHIVE_NAME = f"apache-tomcat-{TOMCAT_VERSION}-windows-x64.zip"
|
||||
TOMCAT_DOWNLOAD_BASE = f"https://dlcdn.apache.org/tomcat/tomcat-9/v{TOMCAT_VERSION}/bin"
|
||||
MYSQL_VERSION = "8.0.43"
|
||||
MYSQL_ROOT_PASSWORD = "123456"
|
||||
MYSQL_INITIAL_ROOT_PASSWORD = "AutoDeploy#2026"
|
||||
MYSQL_INSTALLER_ARCHIVE_NAME = f"mysql-installer-community-{MYSQL_VERSION}.0.msi"
|
||||
MYSQL_INSTALLER_URL = (
|
||||
"https://downloads.mysql.com/archives/get/p/25/file/"
|
||||
@@ -101,11 +102,6 @@ def build_install_command(tool_name: str, version: str | None = None) -> list[st
|
||||
|
||||
|
||||
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;"
|
||||
f"root_passwd={MYSQL_ROOT_PASSWORD} --silent --auto-handle-prereqs"
|
||||
)
|
||||
return [
|
||||
"msiexec.exe",
|
||||
"/i",
|
||||
@@ -114,21 +110,22 @@ def build_mysql_installer_command(installer_path: Path, log_path: Path) -> list[
|
||||
"/norestart",
|
||||
"/L*V",
|
||||
str(log_path),
|
||||
console_args,
|
||||
]
|
||||
|
||||
|
||||
def build_windows_command_parameters(command: list[str]) -> str:
|
||||
parameters: list[str] = []
|
||||
for argument in command[1:]:
|
||||
if argument.startswith("CONSOLEARGS="):
|
||||
value = argument.removeprefix("CONSOLEARGS=")
|
||||
if '"' in value:
|
||||
raise ValueError("MySQL CONSOLEARGS 包含不支持的双引号。")
|
||||
parameters.append(f'CONSOLEARGS="{value}"')
|
||||
else:
|
||||
parameters.append(subprocess.list2cmdline([argument]))
|
||||
return " ".join(parameters)
|
||||
def build_mysql_server_install_command(console_path: Path) -> list[str]:
|
||||
server = (
|
||||
f"server;{MYSQL_VERSION};x64:*:"
|
||||
"type=config;open_win_firewall=false;tcp_ip=true;port=3306;"
|
||||
f"root_passwd={MYSQL_INITIAL_ROOT_PASSWORD}"
|
||||
)
|
||||
return [
|
||||
str(console_path),
|
||||
"--install",
|
||||
server,
|
||||
"--silent",
|
||||
"--auto-handle-prereqs",
|
||||
]
|
||||
|
||||
|
||||
def default_mysql_install_log_path() -> Path:
|
||||
@@ -157,6 +154,33 @@ def find_mysql_executable() -> Path | None:
|
||||
return None
|
||||
|
||||
|
||||
def find_mysql_installer_console() -> Path | None:
|
||||
discovered = shutil.which("MySQLInstallerConsole")
|
||||
if discovered:
|
||||
return Path(discovered)
|
||||
|
||||
roots = [
|
||||
Path(os.environ.get("ProgramFiles(x86)", r"C:\Program Files (x86)")),
|
||||
Path(os.environ.get("ProgramFiles", r"C:\Program Files")),
|
||||
]
|
||||
for root in roots:
|
||||
candidate = root / "MySQL" / "MySQL Installer for Windows" / "MySQLInstallerConsole.exe"
|
||||
if candidate.exists():
|
||||
return candidate
|
||||
return None
|
||||
|
||||
|
||||
def wait_for_mysql_installer_console(timeout: float = 30.0, poll_interval: float = 1.0) -> Path:
|
||||
deadline = time.monotonic() + timeout
|
||||
while True:
|
||||
executable = find_mysql_installer_console()
|
||||
if executable is not None:
|
||||
return executable
|
||||
if time.monotonic() >= deadline:
|
||||
raise RuntimeError("MySQL Installer 已结束,但未找到 MySQLInstallerConsole.exe。")
|
||||
time.sleep(poll_interval)
|
||||
|
||||
|
||||
def wait_for_mysql_executable(timeout: float = 90.0, poll_interval: float = 1.0) -> Path:
|
||||
deadline = time.monotonic() + timeout
|
||||
while True:
|
||||
@@ -171,7 +195,8 @@ def wait_for_mysql_executable(timeout: float = 90.0, poll_interval: float = 1.0)
|
||||
def configure_mysql_weak_password_policy(
|
||||
mysql_executable: Path,
|
||||
on_output: Callable[[str], None],
|
||||
password: str = MYSQL_ROOT_PASSWORD,
|
||||
initial_password: str = MYSQL_INITIAL_ROOT_PASSWORD,
|
||||
final_password: str = MYSQL_ROOT_PASSWORD,
|
||||
attempts: int = 30,
|
||||
retry_delay: float = 2.0,
|
||||
) -> None:
|
||||
@@ -181,7 +206,7 @@ def configure_mysql_weak_password_policy(
|
||||
variables_result = _run_mysql_sql(
|
||||
mysql_executable,
|
||||
"SHOW VARIABLES LIKE 'validate_password%';",
|
||||
password,
|
||||
initial_password,
|
||||
)
|
||||
if variables_result.returncode == 0:
|
||||
break
|
||||
@@ -218,14 +243,18 @@ def configure_mysql_weak_password_policy(
|
||||
else:
|
||||
on_output("MySQL 未启用 validate_password 组件,当前已允许 6 位弱密码。")
|
||||
|
||||
escaped_password = password.replace("'", "''")
|
||||
escaped_password = final_password.replace("'", "''")
|
||||
statements.append(f"ALTER USER 'root'@'localhost' IDENTIFIED BY '{escaped_password}'")
|
||||
configure_result = _run_mysql_sql(mysql_executable, ";\n".join(statements) + ";\n", password)
|
||||
configure_result = _run_mysql_sql(
|
||||
mysql_executable,
|
||||
";\n".join(statements) + ";\n",
|
||||
initial_password,
|
||||
)
|
||||
if configure_result.returncode != 0:
|
||||
detail = _process_error(configure_result)
|
||||
raise RuntimeError(f"MySQL 弱密码策略配置失败。{detail}")
|
||||
|
||||
verify_result = _run_mysql_sql(mysql_executable, "SELECT 1;", password)
|
||||
verify_result = _run_mysql_sql(mysql_executable, "SELECT 1;", final_password)
|
||||
if verify_result.returncode != 0:
|
||||
detail = _process_error(verify_result)
|
||||
raise RuntimeError(f"MySQL 已配置,但 root 登录验证失败。{detail}")
|
||||
@@ -240,32 +269,50 @@ def install_mysql(
|
||||
) -> Path:
|
||||
open_url = opener or urllib.request.urlopen
|
||||
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}...")
|
||||
_download_to_file(MYSQL_INSTALLER_URL, installer_path, open_url, on_output)
|
||||
actual_checksum = _sha256(installer_path)
|
||||
if actual_checksum.lower() != MYSQL_INSTALLER_SHA256.lower():
|
||||
raise RuntimeError("MySQL 安装包 SHA-256 校验失败,已停止安装。")
|
||||
on_output("MySQL 安装包校验通过,正在请求管理员权限安装...")
|
||||
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}。详细日志: {install_log}")
|
||||
on_output("Windows Installer 已完成,正在检测 MySQL 服务和命令行工具...")
|
||||
if exit_code in {1641, 3010}:
|
||||
on_output("MySQL 安装完成,Windows 提示稍后需要重新启动。")
|
||||
console = find_mysql_installer_console()
|
||||
if console is None:
|
||||
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}...")
|
||||
_download_to_file(MYSQL_INSTALLER_URL, installer_path, open_url, on_output)
|
||||
actual_checksum = _sha256(installer_path)
|
||||
if actual_checksum.lower() != MYSQL_INSTALLER_SHA256.lower():
|
||||
raise RuntimeError("MySQL 安装包 SHA-256 校验失败,已停止安装。")
|
||||
on_output("MySQL 安装包校验通过,正在请求管理员权限安装...")
|
||||
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 Installer 正在安装,请继续等待进度窗口完成..."),
|
||||
)
|
||||
else:
|
||||
exit_code = elevated_runner(command)
|
||||
if exit_code not in {0, 1641, 3010}:
|
||||
raise RuntimeError(f"MySQL Installer 安装失败,退出码: {exit_code}。详细日志: {install_log}")
|
||||
if exit_code in {1641, 3010}:
|
||||
on_output("MySQL Installer 安装完成,Windows 提示稍后需要重新启动。")
|
||||
console = wait_for_mysql_installer_console()
|
||||
on_output("MySQL Installer 已完成,正在启动 MySQL Server 安装任务...")
|
||||
else:
|
||||
on_output(f"检测到已有 MySQL Installer,将直接安装 MySQL Server: {console}")
|
||||
|
||||
server_command = build_mysql_server_install_command(console)
|
||||
on_output("即将再次请求管理员权限,请在出现的窗口中确认安装 MySQL Server。")
|
||||
if elevated_runner is None:
|
||||
server_exit_code = run_elevated_command(
|
||||
server_command,
|
||||
on_wait=lambda: on_output("MySQL Server 仍在安装和配置,请继续等待..."),
|
||||
)
|
||||
else:
|
||||
server_exit_code = elevated_runner(server_command)
|
||||
if server_exit_code not in {0, 1641, 3010}:
|
||||
raise RuntimeError(f"MySQL Server 安装失败,退出码: {server_exit_code}。")
|
||||
on_output("MySQL Server 安装任务已完成,正在检测命令行工具...")
|
||||
executable = wait_for_mysql_executable()
|
||||
configure_mysql_weak_password_policy(executable, on_output)
|
||||
return executable
|
||||
@@ -308,7 +355,7 @@ def run_elevated_command(
|
||||
info.fMask = 0x00000040 # SEE_MASK_NOCLOSEPROCESS
|
||||
info.lpVerb = "runas"
|
||||
info.lpFile = command[0]
|
||||
info.lpParameters = build_windows_command_parameters(command)
|
||||
info.lpParameters = subprocess.list2cmdline(command[1:])
|
||||
info.nShow = 1 # SW_SHOWNORMAL: show the Windows Installer progress window.
|
||||
if not shell_execute(ctypes.byref(info)):
|
||||
error_code = ctypes.windll.kernel32.GetLastError()
|
||||
|
||||
Reference in New Issue
Block a user