Files
AutoDeploy/deploy_tool/installer.py

580 lines
21 KiB
Python
Raw Normal View History

2026-07-14 16:30:36 +08:00
from __future__ import annotations
2026-10-09 09:38:44 +08:00
import ctypes
2026-07-14 16:30:36 +08:00
import re
import hashlib
import os
import shutil
2026-07-14 16:30:36 +08:00
import subprocess
import tempfile
import time
import urllib.request
import zipfile
2026-07-14 16:30:36 +08:00
from dataclasses import dataclass
from pathlib import Path
from typing import BinaryIO, Callable
2026-07-14 16:30:36 +08:00
from .scanner import ScanResult
JDK_PACKAGES = {
"8": "EclipseAdoptium.Temurin.8.JDK",
"11": "EclipseAdoptium.Temurin.11.JDK",
"17": "EclipseAdoptium.Temurin.17.JDK",
"21": "EclipseAdoptium.Temurin.21.JDK",
}
WINGET_PACKAGES = {
"Gradle": "Gradle.Gradle",
"Node.js": "OpenJS.NodeJS.LTS",
"Git": "Git.Git",
}
MAVEN_VERSION = "3.10.0"
MAVEN_ARCHIVE_NAME = f"apache-maven-{MAVEN_VERSION}-bin.zip"
MAVEN_DOWNLOAD_BASE = f"https://dlcdn.apache.org/maven/maven-3/{MAVEN_VERSION}/binaries"
TOMCAT_VERSION = "9.0.122"
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"
2026-10-09 08:45:18 +08:00
MYSQL_VERSION = "8.0.43"
MYSQL_ROOT_PASSWORD = "123456"
2026-10-09 09:31:36 +08:00
MYSQL_INSTALLER_ARCHIVE_NAME = f"mysql-installer-community-{MYSQL_VERSION}.0.msi"
MYSQL_INSTALLER_URL = (
"https://downloads.mysql.com/archives/get/p/25/file/"
f"{MYSQL_INSTALLER_ARCHIVE_NAME}"
)
MYSQL_INSTALLER_SHA256 = "F60502EB1EE10A9C1D0A26706A627D224DFAE466D82EF3EB9D76A8A718DB9D80"
2026-07-14 16:30:36 +08:00
ANSI_PATTERN = re.compile(r"\x1b\[[0-?]*[ -/]*[@-~]")
PERCENT_PATTERN = re.compile(r"(\d{1,3})\s*%")
PROGRESS_CHARS = "█▓▒░▏▎▍▌▋▊▉■□▪▫▬─━═|/-\\"
@dataclass(frozen=True)
class JdkRecommendation:
version: str
reason: str
def recommend_jdk_version(scan: ScanResult | None) -> JdkRecommendation:
if not scan or not scan.backend:
return JdkRecommendation("8", "\u672a\u626b\u63cf\u5230\u540e\u7aef\u7248\u672c\u4fe1\u606f\uff0c\u9ed8\u8ba4\u63a8\u8350 JDK 8\u3002")
metadata = scan.backend.metadata
java_version = metadata.get("java_version")
if java_version:
mapped = _map_java_version(java_version)
source = metadata.get("java_version_source", "\u9879\u76ee\u914d\u7f6e")
return JdkRecommendation(mapped, f"\u6839\u636e {source}={java_version} \u63a8\u8350 JDK {mapped}\u3002")
boot_version = metadata.get("spring_boot_version", "")
if boot_version.startswith("3."):
return JdkRecommendation("17", f"Spring Boot 3.x \u9700\u8981 Java 17+\uff0c\u68c0\u6d4b\u5230 Spring Boot {boot_version}\uff0c\u63a8\u8350 JDK 17\u3002")
if boot_version.startswith("2."):
return JdkRecommendation("8", f"\u68c0\u6d4b\u5230 Spring Boot {boot_version}\uff0c\u9ed8\u8ba4\u63a8\u8350\u517c\u5bb9\u6027\u66f4\u597d\u7684 JDK 8\u3002")
return JdkRecommendation("8", "\u672a\u53d1\u73b0\u660e\u786e Java \u7248\u672c\uff0c\u9ed8\u8ba4\u63a8\u8350 JDK 8\u3002")
def build_install_command(tool_name: str, version: str | None = None) -> list[str]:
if tool_name == "JDK":
package_id = JDK_PACKAGES.get(version or "8")
if not package_id:
raise ValueError(f"\u4e0d\u652f\u6301\u7684 JDK \u7248\u672c: {version}")
else:
package_id = WINGET_PACKAGES.get(tool_name)
if not package_id:
raise ValueError(f"\u4e0d\u652f\u6301\u81ea\u52a8\u5b89\u88c5: {tool_name}")
2026-10-09 08:45:18 +08:00
command = [
"winget",
"install",
"--exact",
"--id",
package_id,
2026-10-09 08:45:18 +08:00
]
command.extend([
"--accept-source-agreements",
"--accept-package-agreements",
"--disable-interactivity",
2026-10-09 08:45:18 +08:00
])
return command
2026-07-14 16:30:36 +08:00
2026-10-09 09:31:36 +08:00
def build_mysql_installer_command(installer_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",
str(installer_path),
"/qn",
"/norestart",
console_args,
]
def find_mysql_executable() -> Path | None:
discovered = shutil.which("mysql")
if discovered:
return Path(discovered)
roots = [
Path(os.environ.get("ProgramFiles", r"C:\Program Files")),
Path(os.environ.get("ProgramFiles(x86)", r"C:\Program Files (x86)")),
]
for root in roots:
for relative in (
Path("MySQL") / "MySQL Server 8.0" / "bin" / "mysql.exe",
Path("MySQL") / "MySQL Server 8.4" / "bin" / "mysql.exe",
):
candidate = root / relative
if candidate.exists():
return candidate
return None
def wait_for_mysql_executable(timeout: float = 90.0, poll_interval: float = 1.0) -> Path:
deadline = time.monotonic() + timeout
while True:
executable = find_mysql_executable()
if executable is not None:
return executable
if time.monotonic() >= deadline:
raise RuntimeError("MySQL 安装命令已结束,但未找到 mysql.exe。")
time.sleep(poll_interval)
def configure_mysql_weak_password_policy(
mysql_executable: Path,
on_output: Callable[[str], None],
password: str = MYSQL_ROOT_PASSWORD,
attempts: int = 30,
retry_delay: float = 2.0,
) -> None:
on_output("正在连接 MySQL,并永久启用 6 位弱密码策略...")
variables_result: subprocess.CompletedProcess[str] | None = None
for attempt in range(max(attempts, 1)):
variables_result = _run_mysql_sql(
mysql_executable,
"SHOW VARIABLES LIKE 'validate_password%';",
password,
)
if variables_result.returncode == 0:
break
if attempt + 1 < max(attempts, 1):
time.sleep(retry_delay)
assert variables_result is not None
if variables_result.returncode != 0:
detail = _process_error(variables_result)
raise RuntimeError(f"无法使用 root 账号连接 MySQL,不能写入弱密码策略。{detail}")
variable_names = variables_result.stdout.lower()
statements: list[str] = []
if "validate_password.policy" in variable_names:
statements.extend(
[
"SET PERSIST validate_password.policy = 'LOW'",
"SET PERSIST validate_password.length = 6",
"SET PERSIST validate_password.mixed_case_count = 0",
"SET PERSIST validate_password.number_count = 0",
"SET PERSIST validate_password.special_char_count = 0",
]
)
elif "validate_password_policy" in variable_names:
statements.extend(
[
"SET PERSIST validate_password_policy = 'LOW'",
"SET PERSIST validate_password_length = 6",
"SET PERSIST validate_password_mixed_case_count = 0",
"SET PERSIST validate_password_number_count = 0",
"SET PERSIST validate_password_special_char_count = 0",
]
)
else:
on_output("MySQL 未启用 validate_password 组件,当前已允许 6 位弱密码。")
escaped_password = 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)
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)
if verify_result.returncode != 0:
detail = _process_error(verify_result)
raise RuntimeError(f"MySQL 已配置,但 root 登录验证失败。{detail}")
on_output("MySQL 已永久允许 6 位弱密码,root 默认密码配置完成。")
2026-10-09 09:31:36 +08:00
def install_mysql(
on_output: Callable[[str], None],
opener: Callable[..., BinaryIO] | None = None,
elevated_runner: Callable[[list[str]], int] | None = None,
) -> Path:
open_url = opener or urllib.request.urlopen
run_elevated = elevated_runner or run_elevated_command
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 安装包校验通过,正在请求管理员权限安装...")
exit_code = run_elevated(build_mysql_installer_command(installer_path))
if exit_code not in {0, 1641, 3010}:
raise RuntimeError(f"MySQL 安装命令失败,退出码: {exit_code}")
2026-10-09 09:31:36 +08:00
if exit_code in {1641, 3010}:
on_output("MySQL 安装完成,Windows 提示稍后需要重新启动。")
executable = wait_for_mysql_executable()
configure_mysql_weak_password_policy(executable, on_output)
return executable
2026-10-09 09:31:36 +08:00
def run_elevated_command(command: list[str]) -> int:
if os.name != "nt":
return subprocess.run(command, shell=False).returncode
from ctypes import wintypes
class ShellExecuteInfo(ctypes.Structure):
_fields_ = [
("cbSize", wintypes.DWORD),
("fMask", wintypes.ULONG),
("hwnd", wintypes.HWND),
("lpVerb", wintypes.LPCWSTR),
("lpFile", wintypes.LPCWSTR),
("lpParameters", wintypes.LPCWSTR),
("lpDirectory", wintypes.LPCWSTR),
("nShow", ctypes.c_int),
("hInstApp", wintypes.HINSTANCE),
("lpIDList", wintypes.LPVOID),
("lpClass", wintypes.LPCWSTR),
("hkeyClass", wintypes.HKEY),
("dwHotKey", wintypes.DWORD),
("hIcon", wintypes.HANDLE),
("hProcess", wintypes.HANDLE),
]
shell_execute = ctypes.windll.shell32.ShellExecuteExW
shell_execute.argtypes = [ctypes.POINTER(ShellExecuteInfo)]
shell_execute.restype = wintypes.BOOL
info = ShellExecuteInfo()
info.cbSize = ctypes.sizeof(info)
info.fMask = 0x00000040 # SEE_MASK_NOCLOSEPROCESS
info.lpVerb = "runas"
info.lpFile = command[0]
info.lpParameters = subprocess.list2cmdline(command[1:])
info.nShow = 0
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)
exit_code = wintypes.DWORD()
if not ctypes.windll.kernel32.GetExitCodeProcess(info.hProcess, ctypes.byref(exit_code)):
raise OSError(ctypes.windll.kernel32.GetLastError(), "无法读取 MySQL 安装程序退出码")
return int(exit_code.value)
finally:
ctypes.windll.kernel32.CloseHandle(info.hProcess)
def _run_mysql_sql(mysql_executable: Path, sql: str, password: str) -> subprocess.CompletedProcess[str]:
environment = os.environ.copy()
environment["MYSQL_PWD"] = password
return subprocess.run(
[
str(mysql_executable),
"--protocol=TCP",
"--host=127.0.0.1",
"--user=root",
"--batch",
"--skip-column-names",
],
input=sql,
capture_output=True,
text=True,
timeout=15,
shell=False,
env=environment,
)
def _process_error(completed: subprocess.CompletedProcess[str]) -> str:
detail = (completed.stderr or completed.stdout or "").strip()
return f" 错误: {detail}" if detail else ""
def default_maven_install_root() -> 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" / "tools"
def install_maven(
on_output: Callable[[str], None],
install_root: Path | None = None,
opener: Callable[..., BinaryIO] | None = None,
) -> Path:
root = Path(install_root) if install_root is not None else default_maven_install_root()
archive_url = f"{MAVEN_DOWNLOAD_BASE}/{MAVEN_ARCHIVE_NAME}"
return _install_verified_apache_archive(
product_name="Maven",
version=MAVEN_VERSION,
archive_name=MAVEN_ARCHIVE_NAME,
archive_url=archive_url,
extracted_dir_name=f"apache-maven-{MAVEN_VERSION}",
executable_relative=Path("bin") / "mvn.cmd",
on_output=on_output,
install_root=root,
opener=opener,
)
def install_tomcat(
on_output: Callable[[str], None],
install_root: Path | None = None,
opener: Callable[..., BinaryIO] | None = None,
) -> Path:
root = Path(install_root) if install_root is not None else default_maven_install_root()
archive_url = f"{TOMCAT_DOWNLOAD_BASE}/{TOMCAT_ARCHIVE_NAME}"
return _install_verified_apache_archive(
product_name="Tomcat",
version=TOMCAT_VERSION,
archive_name=TOMCAT_ARCHIVE_NAME,
archive_url=archive_url,
extracted_dir_name=f"apache-tomcat-{TOMCAT_VERSION}",
executable_relative=Path("bin") / "catalina.bat",
on_output=on_output,
install_root=root,
opener=opener,
)
def _install_verified_apache_archive(
*,
product_name: str,
version: str,
archive_name: str,
archive_url: str,
extracted_dir_name: str,
executable_relative: Path,
on_output: Callable[[str], None],
install_root: Path,
opener: Callable[..., BinaryIO] | None,
) -> Path:
product_home = install_root / extracted_dir_name
executable = product_home / executable_relative
if executable.exists():
on_output(f"{product_name} {version} 已安装: {executable}")
return executable
open_url = opener or urllib.request.urlopen
checksum_url = f"{archive_url}.sha512"
install_root.mkdir(parents=True, exist_ok=True)
with tempfile.TemporaryDirectory(prefix=f"autodeploy-{product_name.lower()}-") as temporary:
temp_dir = Path(temporary)
archive_path = temp_dir / archive_name
on_output(f"正在从 Apache 官方地址下载 {product_name} {version}...")
_download_to_file(archive_url, archive_path, open_url, on_output)
expected_checksum = _download_checksum(checksum_url, open_url)
actual_checksum = _sha512(archive_path)
if actual_checksum.lower() != expected_checksum.lower():
raise RuntimeError(f"{product_name} 安装包 SHA-512 校验失败,已停止安装。")
on_output(f"{product_name} 安装包校验通过,正在解压...")
extract_root = temp_dir / "extracted"
_safe_extract_zip(archive_path, extract_root)
extracted_home = extract_root / extracted_dir_name
extracted_executable = extracted_home / executable_relative
if not extracted_executable.exists():
raise RuntimeError(f"{product_name} 安装包结构异常,未找到 {executable_relative}。")
shutil.copytree(extracted_home, product_home, dirs_exist_ok=True)
if not executable.exists():
raise RuntimeError(f"{product_name} 解压完成,但未找到 {executable_relative}。")
on_output(f"{product_name} {version} 安装完成: {executable}")
return executable
def _download_to_file(
url: str,
destination: Path,
opener: Callable[..., BinaryIO],
on_output: Callable[[str], None],
) -> None:
with opener(url, timeout=60) as response, destination.open("wb") as output:
headers = getattr(response, "headers", {})
total_text = headers.get("Content-Length", "") if hasattr(headers, "get") else ""
total = int(total_text) if str(total_text).isdigit() else 0
downloaded = 0
last_percent = -10
while True:
chunk = response.read(1024 * 256)
if not chunk:
break
output.write(chunk)
downloaded += len(chunk)
if total:
percent = min(downloaded * 100 // total, 100)
if percent >= last_percent + 10 or percent == 100:
on_output(f"下载进度: {percent}%")
last_percent = percent
def _download_checksum(url: str, opener: Callable[..., BinaryIO]) -> str:
with opener(url, timeout=30) as response:
text = response.read().decode("ascii", errors="strict").strip()
checksum = text.split()[0] if text else ""
if not re.fullmatch(r"[0-9a-fA-F]{128}", checksum):
raise RuntimeError("Apache Maven 校验文件格式无效。")
return checksum
def _sha512(path: Path) -> str:
digest = hashlib.sha512()
with path.open("rb") as source:
for chunk in iter(lambda: source.read(1024 * 1024), b""):
digest.update(chunk)
return digest.hexdigest()
2026-10-09 09:31:36 +08:00
def _sha256(path: Path) -> str:
digest = hashlib.sha256()
with path.open("rb") as source:
for chunk in iter(lambda: source.read(1024 * 1024), b""):
digest.update(chunk)
return digest.hexdigest()
def _safe_extract_zip(archive_path: Path, destination: Path) -> None:
destination.mkdir(parents=True, exist_ok=True)
destination_resolved = destination.resolve()
with zipfile.ZipFile(archive_path) as archive:
for member in archive.infolist():
target = (destination / member.filename).resolve()
if target != destination_resolved and destination_resolved not in target.parents:
raise RuntimeError("Maven 安装包包含不安全的文件路径。")
archive.extractall(destination)
2026-07-14 16:30:36 +08:00
def run_install_command(command: list[str], on_output) -> int:
output_lines: list[str] = []
process = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
shell=False,
bufsize=0,
)
assert process.stdout is not None
last_line = ""
for raw_line in process.stdout:
for line in format_install_output(raw_line):
if line and line != last_line:
on_output(line)
output_lines.append(line)
last_line = line
exit_code = process.wait()
failure_hint = explain_install_failure(_tool_name_from_command(command), exit_code, output_lines)
if failure_hint:
on_output(failure_hint)
return exit_code
def explain_install_failure(tool_name: str | None, exit_code: int, output_lines: list[str]) -> str:
if exit_code == 0:
return ""
output_text = "\n".join(output_lines)
if tool_name == "Maven" and ("找不到与输入条件匹配的程序包" in output_text or "No package found" in output_text):
return (
"winget 当前源找不到 Maven 包。请手动下载 Apache Maven,解压后在工具里选择 "
"apache-maven 的 bin\\mvn.cmd,或把该 bin 目录加入 PATH。"
)
return ""
def format_install_output(raw: bytes) -> list[str]:
text = _decode_process_bytes(raw)
text = ANSI_PATTERN.sub("", text).replace("\b", "")
lines: list[str] = []
for part in re.split(r"[\r\n]+", text):
cleaned = _clean_install_line(part)
if cleaned:
lines.append(cleaned)
return lines
def _decode_process_bytes(raw: bytes) -> str:
for encoding in ("utf-8", "utf-16", "gb18030", "mbcs"):
try:
return raw.decode(encoding)
except (LookupError, UnicodeDecodeError):
continue
return raw.decode("utf-8", errors="replace")
def _clean_install_line(line: str) -> str:
text = line.strip()
if not text:
return ""
percent = PERCENT_PATTERN.search(text)
progress_only = _looks_like_progress_line(text)
if percent and progress_only:
value = min(int(percent.group(1)), 100)
return f"\u5b89\u88c5\u8fdb\u5ea6: {value}%"
if progress_only:
return ""
return text
def _looks_like_progress_line(text: str) -> bool:
if not text:
return False
progress_count = sum(1 for char in text if char in PROGRESS_CHARS)
percent = bool(PERCENT_PATTERN.search(text))
if percent and progress_count:
return True
visible = [char for char in text if not char.isspace()]
if not visible:
return False
return progress_count / len(visible) > 0.45
def _tool_name_from_command(command: list[str]) -> str | None:
package_ids = {package_id: name for name, package_id in WINGET_PACKAGES.items()}
package_ids.update({package_id: "JDK" for package_id in JDK_PACKAGES.values()})
for part in command:
name = package_ids.get(part)
if name:
return name
return None
def _map_java_version(version: str) -> str:
try:
major = int(version.split(".", 1)[0])
except ValueError:
return "8"
if major <= 8:
return "8"
if major <= 11:
return "11"
if major <= 17:
return "17"
return "21"