refactor(modules): 添加类型注解并优化 WSL 检测
- 全模块补全类型注解 (files, after_update, on_change) - docker: 仅在 WSL 环境禁用 networkd-wait-online - docker: 添加 _is_wsl() 自动检测函数 - zsh/docker: 改进错误输出使用 decman.error - wsl-init: 统一使用 [[ ]] 并格式化代码
This commit is contained in:
+1
-1
@@ -8,7 +8,7 @@ class BaseModule(Module):
|
|||||||
super().__init__("base")
|
super().__init__("base")
|
||||||
self.user = user
|
self.user = user
|
||||||
|
|
||||||
def files(self):
|
def files(self) -> dict[str, File]:
|
||||||
return {
|
return {
|
||||||
"/etc/pacman.conf": File(
|
"/etc/pacman.conf": File(
|
||||||
source_file="./system/etc/pacman.conf",
|
source_file="./system/etc/pacman.conf",
|
||||||
|
|||||||
+4
-4
@@ -2,13 +2,13 @@ import decman
|
|||||||
from decman import File, Module
|
from decman import File, Module
|
||||||
from decman.plugins.pacman import packages as pacman_packages
|
from decman.plugins.pacman import packages as pacman_packages
|
||||||
|
|
||||||
BUN_GLOBAL_PACKAGES = [
|
BUN_GLOBAL_PACKAGES: list[str] = [
|
||||||
"@vue/language-server",
|
"@vue/language-server",
|
||||||
"dockerfile-language-server-nodejs",
|
"dockerfile-language-server-nodejs",
|
||||||
"opencode-ai",
|
"opencode-ai",
|
||||||
]
|
]
|
||||||
|
|
||||||
GO_INSTALL_PACKAGES = [
|
GO_INSTALL_PACKAGES: list[str] = [
|
||||||
"github.com/code-yeongyu/go-claude-code-comment-checker/cmd/comment-checker@latest",
|
"github.com/code-yeongyu/go-claude-code-comment-checker/cmd/comment-checker@latest",
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -18,7 +18,7 @@ class DevModule(Module):
|
|||||||
super().__init__("dev")
|
super().__init__("dev")
|
||||||
self.user = user
|
self.user = user
|
||||||
|
|
||||||
def files(self):
|
def files(self) -> dict[str, File]:
|
||||||
return {
|
return {
|
||||||
f"/home/{self.user}/.config/mise/config.toml": File(
|
f"/home/{self.user}/.config/mise/config.toml": File(
|
||||||
source_file="./home/.config/mise/config.toml",
|
source_file="./home/.config/mise/config.toml",
|
||||||
@@ -50,7 +50,7 @@ class DevModule(Module):
|
|||||||
"zellij",
|
"zellij",
|
||||||
}
|
}
|
||||||
|
|
||||||
def after_update(self, store):
|
def after_update(self, store: object) -> None:
|
||||||
failures: list[str] = []
|
failures: list[str] = []
|
||||||
for pkg in BUN_GLOBAL_PACKAGES:
|
for pkg in BUN_GLOBAL_PACKAGES:
|
||||||
try:
|
try:
|
||||||
|
|||||||
+23
-8
@@ -1,4 +1,5 @@
|
|||||||
import subprocess
|
import subprocess
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
import decman
|
import decman
|
||||||
from decman import Module
|
from decman import Module
|
||||||
@@ -6,10 +7,19 @@ from decman.plugins.pacman import packages as pacman_packages
|
|||||||
from decman.plugins.systemd import units
|
from decman.plugins.systemd import units
|
||||||
|
|
||||||
|
|
||||||
|
def _is_wsl() -> bool:
|
||||||
|
"""检测是否在 WSL 环境"""
|
||||||
|
try:
|
||||||
|
return "microsoft" in Path("/proc/version").read_text().lower()
|
||||||
|
except OSError:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
class DockerModule(Module):
|
class DockerModule(Module):
|
||||||
def __init__(self, user: str):
|
def __init__(self, user: str):
|
||||||
super().__init__("docker")
|
super().__init__("docker")
|
||||||
self.user = user
|
self.user = user
|
||||||
|
self._is_wsl = _is_wsl()
|
||||||
|
|
||||||
@pacman_packages
|
@pacman_packages
|
||||||
def pacman_packages(self) -> set[str]:
|
def pacman_packages(self) -> set[str]:
|
||||||
@@ -19,26 +29,31 @@ class DockerModule(Module):
|
|||||||
def units(self) -> set[str]:
|
def units(self) -> set[str]:
|
||||||
return {"docker.socket"}
|
return {"docker.socket"}
|
||||||
|
|
||||||
def after_update(self, store):
|
def after_update(self, store: object) -> None:
|
||||||
self._ensure_user_in_docker_group()
|
self._ensure_user_in_docker_group()
|
||||||
self._disable_networkd_wait_online()
|
if self._is_wsl:
|
||||||
|
self._disable_networkd_wait_online()
|
||||||
|
|
||||||
def _ensure_user_in_docker_group(self):
|
def _ensure_user_in_docker_group(self) -> None:
|
||||||
result = subprocess.run(
|
result = subprocess.run(
|
||||||
["id", "-nG", self.user], capture_output=True, text=True
|
["id", "-nG", self.user],
|
||||||
|
capture_output=True,
|
||||||
|
text=True,
|
||||||
|
check=False,
|
||||||
)
|
)
|
||||||
if result.returncode != 0:
|
if result.returncode != 0:
|
||||||
|
decman.error(f"无法读取用户 {self.user} 的组信息")
|
||||||
return
|
return
|
||||||
if "docker" not in result.stdout.split():
|
if "docker" not in result.stdout.split():
|
||||||
decman.prg(["gpasswd", "-a", self.user, "docker"])
|
decman.prg(["gpasswd", "-a", self.user, "docker"])
|
||||||
|
|
||||||
def _disable_networkd_wait_online(self):
|
def _disable_networkd_wait_online(self) -> None:
|
||||||
# systemd-networkd-wait-online.service 会阻塞 network-online.target
|
"""WSL 环境:禁用 systemd-networkd-wait-online 避免启动阻塞"""
|
||||||
# 直到所有 link ready 或 120s 超时,导致 docker.service 启动卡约两分钟
|
|
||||||
result = subprocess.run(
|
result = subprocess.run(
|
||||||
["systemctl", "is-enabled", "systemd-networkd-wait-online.service"],
|
["systemctl", "is-enabled", "systemd-networkd-wait-online.service"],
|
||||||
capture_output=True,
|
capture_output=True,
|
||||||
text=True,
|
text=True,
|
||||||
|
check=False,
|
||||||
)
|
)
|
||||||
if result.stdout.strip() == "enabled":
|
if result.returncode == 0 and result.stdout.strip() == "enabled":
|
||||||
decman.prg(["systemctl", "disable", "systemd-networkd-wait-online.service"])
|
decman.prg(["systemctl", "disable", "systemd-networkd-wait-online.service"])
|
||||||
|
|||||||
+2
-2
@@ -6,11 +6,11 @@ class LocaleModule(Module):
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__("locale")
|
super().__init__("locale")
|
||||||
|
|
||||||
def files(self):
|
def files(self) -> dict[str, File]:
|
||||||
return {
|
return {
|
||||||
"/etc/locale.conf": File(content="LANG=en_US.UTF-8\n"),
|
"/etc/locale.conf": File(content="LANG=en_US.UTF-8\n"),
|
||||||
"/etc/locale.gen": File(content="en_US.UTF-8 UTF-8\n"),
|
"/etc/locale.gen": File(content="en_US.UTF-8 UTF-8\n"),
|
||||||
}
|
}
|
||||||
|
|
||||||
def on_change(self, store):
|
def on_change(self, store: object) -> None:
|
||||||
decman.prg(["locale-gen"])
|
decman.prg(["locale-gen"])
|
||||||
|
|||||||
+7
-3
@@ -11,7 +11,7 @@ class ZshModule(Module):
|
|||||||
super().__init__("zsh")
|
super().__init__("zsh")
|
||||||
self.user = user
|
self.user = user
|
||||||
|
|
||||||
def files(self):
|
def files(self) -> dict[str, File]:
|
||||||
return {
|
return {
|
||||||
f"/home/{self.user}/.zshrc": File(
|
f"/home/{self.user}/.zshrc": File(
|
||||||
source_file="./home/.zshrc",
|
source_file="./home/.zshrc",
|
||||||
@@ -35,11 +35,15 @@ class ZshModule(Module):
|
|||||||
"fzf-tab-git",
|
"fzf-tab-git",
|
||||||
}
|
}
|
||||||
|
|
||||||
def after_update(self, store):
|
def after_update(self, store: object) -> None:
|
||||||
result = subprocess.run(
|
result = subprocess.run(
|
||||||
["getent", "passwd", self.user], capture_output=True, text=True
|
["getent", "passwd", self.user],
|
||||||
|
capture_output=True,
|
||||||
|
text=True,
|
||||||
|
check=False,
|
||||||
)
|
)
|
||||||
if result.returncode != 0:
|
if result.returncode != 0:
|
||||||
|
decman.error(f"无法读取用户 {self.user} 的 passwd 信息")
|
||||||
return
|
return
|
||||||
# passwd 格式: name:x:uid:gid:gecos:home:shell
|
# passwd 格式: name:x:uid:gid:gecos:home:shell
|
||||||
shell = result.stdout.strip().split(":")[-1]
|
shell = result.stdout.strip().split(":")[-1]
|
||||||
|
|||||||
+17
-17
@@ -1,40 +1,40 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
|
|
||||||
if [ "$(id -u)" -ne 0 ]; then
|
if [[ "$(id -u)" -ne 0 ]]; then
|
||||||
echo "❌ 请以 root 身份运行此脚本"
|
echo "❌ 请以 root 身份运行此脚本"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
USERNAME="${1:-}"
|
USERNAME="${1:-}"
|
||||||
if [ -z "$USERNAME" ]; then
|
if [[ -z "$USERNAME" ]]; then
|
||||||
echo "用法: wsl-init.sh <用户名>"
|
echo "用法: wsl-init.sh <用户名>"
|
||||||
echo "示例: wsl-init.sh imbytecat"
|
echo "示例: wsl-init.sh imbytecat"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "🔄 更新系统..."
|
echo "🔄 更新系统..."
|
||||||
pacman -Syu --noconfirm
|
pacman -Syu --noconfirm
|
||||||
|
|
||||||
if ! command -v sudo &> /dev/null; then
|
if ! command -v sudo &>/dev/null; then
|
||||||
echo "📦 安装 sudo..."
|
echo "📦 安装 sudo..."
|
||||||
pacman -S --needed --noconfirm sudo
|
pacman -S --needed --noconfirm sudo
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "🔐 配置 sudo 权限..."
|
echo "🔐 配置 sudo 权限..."
|
||||||
cat > /etc/sudoers.d/10-wheel << 'EOF'
|
cat >/etc/sudoers.d/10-wheel <<'EOF'
|
||||||
%wheel ALL=(ALL) NOPASSWD: ALL
|
%wheel ALL=(ALL) NOPASSWD: ALL
|
||||||
EOF
|
EOF
|
||||||
chmod 440 /etc/sudoers.d/10-wheel
|
chmod 440 /etc/sudoers.d/10-wheel
|
||||||
|
|
||||||
echo "👤 创建用户 $USERNAME..."
|
echo "👤 创建用户 $USERNAME..."
|
||||||
if id "$USERNAME" &> /dev/null; then
|
if id "$USERNAME" &>/dev/null; then
|
||||||
echo "⏩ 用户 $USERNAME 已存在,确保 wheel 组成员"
|
echo "⏩ 用户 $USERNAME 已存在,确保 wheel 组成员"
|
||||||
usermod -aG wheel "$USERNAME"
|
usermod -aG wheel "$USERNAME"
|
||||||
else
|
else
|
||||||
useradd -m -G wheel -s /bin/bash "$USERNAME"
|
useradd -m -G wheel -s /bin/bash "$USERNAME"
|
||||||
echo "请设置 $USERNAME 的密码:"
|
echo "请设置 $USERNAME 的密码:"
|
||||||
passwd "$USERNAME" < /dev/tty
|
passwd "$USERNAME" </dev/tty
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo ""
|
echo ""
|
||||||
|
|||||||
Reference in New Issue
Block a user