From c810f4ef6c32eabdef941f38ce2cb26981f1ba57 Mon Sep 17 00:00:00 2001 From: fukunee Date: Wed, 13 May 2026 15:11:05 +0800 Subject: [PATCH] Migrate host config from flat key-value to YAML format --- README.md | 45 ++++++++++++++---- claw.ps1 | 96 ++++++++++++++++++++++++++------------ hosts/tx-ubuntu-hugo | 33 ------------- hosts/tx-ubuntu-hugo.yml | 35 ++++++++++++++ hosts/tx-ubuntu-ubuntu | 34 -------------- hosts/tx-ubuntu-ubuntu.yml | 33 +++++++++++++ 6 files changed, 172 insertions(+), 104 deletions(-) delete mode 100644 hosts/tx-ubuntu-hugo create mode 100644 hosts/tx-ubuntu-hugo.yml delete mode 100644 hosts/tx-ubuntu-ubuntu create mode 100644 hosts/tx-ubuntu-ubuntu.yml diff --git a/README.md b/README.md index 91c2151..a38d00c 100644 --- a/README.md +++ b/README.md @@ -42,13 +42,42 @@ tx-ubuntu-ubuntu # → 62.234.90.54 用户 ubuntu tx-ubuntu-hugo # → 62.234.90.54 用户 hugo ``` -文件内容: +YAML 格式(.yml 后缀),新建主机复制下面模板改: -``` -HOST= -USER= -PORT= -KEY=keys/<密钥文件>.pem +```yaml +# ╔══════════════════════════════════════════════════╗ +# ║ example · 示例主机 ║ +# ╚══════════════════════════════════════════════════╝ + +host: + ip: 1.2.3.4 + user: root + port: 22 + key: keys/example.pem + +info: + name: example-server + id: lhins-xxxxxxxx + status: 运行中 + region: + city: 北京 + zone: 北京七区 + +spec: + cpu: 4核 + memory: 4GB + disk: SSD云硬盘 40GB + image: Ubuntu 24.04 LTS + +network: + public_ip: 1.2.3.4 + private_ip: 10.0.0.1 + bandwidth: 3Mbps + traffic: 300GB/月 + +billing: + expire: 2027-01-01 + auto_renew: false ``` ### scripts/ — 脚本 @@ -77,8 +106,8 @@ KEY=keys/<密钥文件>.pem # 1. 注册密钥 claw key add my-host C:\Users\me\Downloads\key.pem -# 2. 写主机描述 -notepad hosts\my-host-ubuntu-root +# 2. 写主机描述 (YAML 格式) +notepad hosts\my-host-ubuntu-root.yml # 3. 开工 claw ssh my-host-ubuntu-root diff --git a/claw.ps1 b/claw.ps1 index 596d469..2bc599e 100644 --- a/claw.ps1 +++ b/claw.ps1 @@ -28,34 +28,68 @@ Examples: "@ } +# ============================================================ +# Minimal YAML parser (handles nested key: value, no arrays) +# ============================================================ +function Parse-Yaml($Path) { + $result = @{} + $stack = New-Object System.Collections.ArrayList + $null = $stack.Add(@{ node = $result; indent = -1; key = $null }) + foreach ($line in (Get-Content $Path -Encoding UTF8)) { + $trimmed = $line.TrimEnd() + if ($trimmed -match '^\s*#' -or $trimmed -match '^\s*$') { continue } + if ($trimmed -notmatch '^(\s*)([^:]+):\s*(.*)') { continue } + $indent = $matches[1].Length + $key = $matches[2].Trim() + $value = $matches[3].Trim() + + # Pop stack until we find the parent with smaller indent + while ($stack.Count -gt 1 -and $stack[-1].indent -ge $indent) { + $null = $stack.RemoveAt($stack.Count - 1) + } + $parent = $stack[-1].node + + if ($value) { + $parent[$key] = $value + } else { + $child = @{} + $parent[$key] = $child + $null = $stack.Add(@{ node = $child; indent = $indent; key = $key }) + } + } + return $result +} + # ============================================================ # Load host config # ============================================================ function Load-Host($Name) { - $HostFile = Join-Path $ClawRoot "hosts\$Name" + $HostFile = Join-Path $ClawRoot "hosts\$Name.yml" if (-not (Test-Path $HostFile)) { - Write-Host "ERROR: host '$Name' not found (hosts\$Name)" -ForegroundColor Red - Write-Host "Available hosts:" -ForegroundColor Yellow - List-Hosts + # fallback: try without .yml extension (old format) + $HostFile = Join-Path $ClawRoot "hosts\$Name" + if (-not (Test-Path $HostFile)) { + Write-Host "ERROR: host '$Name' not found (hosts\$Name.yml)" -ForegroundColor Red + Write-Host "Available hosts:" -ForegroundColor Yellow + List-Hosts + exit 1 + } + } + + $cfg = Parse-Yaml $HostFile + $h = $cfg.host + if (-not $h) { + Write-Host "ERROR: hosts\$Name missing 'host:' section" -ForegroundColor Red exit 1 } - $script:HostIP = $null - $script:HostUser = "root" - $script:HostPort = "22" - $script:HostKey = $null - - Get-Content $HostFile | ForEach-Object { - $line = $_.Trim() - if ($line -match '^\s*#' -or $line -eq '') { return } - if ($line -match '^HOST=(.+)$') { $script:HostIP = $matches[1].Trim() } - if ($line -match '^USER=(.+)$') { $script:HostUser = $matches[1].Trim() } - if ($line -match '^PORT=(.+)$') { $script:HostPort = $matches[1].Trim() } - if ($line -match '^KEY=(.+)$') { $script:HostKey = $matches[1].Trim() } - } + $script:HostIP = if ($h.ip) { $h.ip } else { $null } + $script:HostUser = if ($h.user) { $h.user } else { "root" } + $script:HostPort = if ($h.port) { $h.port } else { "22" } + $script:HostKey = if ($h.key) { $h.key } else { $null } if (-not $HostIP) { - Write-Host "ERROR: hosts\$Name missing HOST=" -ForegroundColor Red + Write-Host "ERROR: hosts\${Name}: host.ip is required" -ForegroundColor Red exit 1 } @@ -86,19 +120,23 @@ function Build-SshArgs { function List-Hosts { $hostDir = Join-Path $ClawRoot "hosts" if (-not (Test-Path $hostDir) -or @(Get-ChildItem $hostDir).Count -eq 0) { - Write-Host " (no hosts yet, create a file in hosts\)" + Write-Host " (no hosts yet, create a .yml file in hosts\)" return } - Get-ChildItem $hostDir | ForEach-Object { - $name = $_.Name - $h = ""; $u = "root"; $p = "22" - Get-Content $_.FullName | ForEach-Object { - $line = $_.Trim() - if ($line -match '^HOST=(.+)$') { $h = $matches[1].Trim() } - if ($line -match '^USER=(.+)$') { $u = $matches[1].Trim() } - if ($line -match '^PORT=(.+)$') { $p = $matches[1].Trim() } + Get-ChildItem $hostDir -Filter *.yml | ForEach-Object { + $name = $_.BaseName + try { + $cfg = Parse-Yaml $_.FullName + $h = $cfg.host + if ($h) { + $ip = if ($h.ip) { $h.ip } else { "?" } + $u = if ($h.user) { $h.user } else { "root" } + $p = if ($h.port) { $h.port } else { "22" } + Write-Host (" {0,-25} {1}@{2}:{3}" -f $name, $u, $ip, $p) + } + } catch { + Write-Host (" {0,-25} (parse error)" -f $name) } - Write-Host (" {0,-20} {1}@{2}:{3}" -f $name, $u, $h, $p) } } @@ -169,7 +207,7 @@ function Invoke-Key($Action, $Name, $Src) { $dest = Join-Path $ClawRoot "keys\$Name.pem" Copy-Item $Src $dest -Force Write-Host ">>> Key saved: keys\$Name.pem" -ForegroundColor Green - Write-Host ">>> In host file, use: KEY=keys\$Name.pem" + Write-Host ">>> In host file, set: host.key: keys\$Name.pem" } "list" { Write-Host "Registered keys:" -ForegroundColor Yellow diff --git a/hosts/tx-ubuntu-hugo b/hosts/tx-ubuntu-hugo deleted file mode 100644 index 1533709..0000000 --- a/hosts/tx-ubuntu-hugo +++ /dev/null @@ -1,33 +0,0 @@ -# ╔══════════════════════════════════════════════════╗ -# ║ tx-ubuntu · 腾讯云轻量应用服务器 (hugo 用户) ║ -# ╚══════════════════════════════════════════════════╝ -# -# 与 tx-ubuntu-ubuntu 同一台机器,仅 SSH 用户不同 -# -# --- 基本信息 --- -# 实例名称 tx_ubuntu_62.234.90.54@20260512 -# 实例 ID lhins-61pjmw9y -# 状态 运行中 -# 地域 北京 · 北京七区 -# -# --- 配置 --- -# CPU 4 核 -# 内存 4 GB -# 系统盘 SSD 云硬盘 40 GB -# 镜像 OpenClaw(龙虾) · Ubuntu Server 24.04 LTS 64bit -# -# --- 网络 --- -# 公网 IPv4 62.234.90.54 -# 内网 IPv4 10.2.0.9 -# 带宽 3 Mbps -# 流量包 300 GB/月(每月 13 日重置) -# IPv6 不支持 -# -# --- 费用 --- -# 到期时间 2027-05-12 13:49:28 -# 自动续费 未开启 - -HOST=62.234.90.54 -USER=hugo -PORT=22 -KEY=keys/tx-ubuntu-hugo.pem diff --git a/hosts/tx-ubuntu-hugo.yml b/hosts/tx-ubuntu-hugo.yml new file mode 100644 index 0000000..798ac06 --- /dev/null +++ b/hosts/tx-ubuntu-hugo.yml @@ -0,0 +1,35 @@ +# ╔══════════════════════════════════════════════════╗ +# ║ tx-ubuntu · 腾讯云轻量应用服务器 (hugo 用户) ║ +# ╚══════════════════════════════════════════════════╝ +# +# 与 tx-ubuntu-ubuntu 同一台机器,仅 SSH 用户不同 + +host: + ip: 62.234.90.54 + user: hugo + port: 22 + key: keys/tx-ubuntu-hugo.pem + +info: + name: tx_ubuntu_62.234.90.54@20260512 + id: lhins-61pjmw9y + status: 运行中 + region: + city: 北京 + zone: 北京七区 + +spec: + cpu: 4核 + memory: 4GB + disk: SSD云硬盘 40GB + image: OpenClaw(龙虾) · Ubuntu 24.04 LTS + +network: + public_ip: 62.234.90.54 + private_ip: 10.2.0.9 + bandwidth: 3Mbps + traffic: 300GB/月 + +billing: + expire: 2027-05-12 + auto_renew: false diff --git a/hosts/tx-ubuntu-ubuntu b/hosts/tx-ubuntu-ubuntu deleted file mode 100644 index 5cb905b..0000000 --- a/hosts/tx-ubuntu-ubuntu +++ /dev/null @@ -1,34 +0,0 @@ -# ╔══════════════════════════════════════════════════╗ -# ║ tx-ubuntu · 腾讯云轻量应用服务器 ║ -# ╚══════════════════════════════════════════════════╝ -# -# --- 基本信息 --- -# 实例名称 tx_ubuntu_62.234.90.54@20260512 -# 实例 ID lhins-61pjmw9y -# 状态 运行中 -# 地域 北京 · 北京七区 -# -# --- 配置 --- -# CPU 4 核 -# 内存 4 GB -# 系统盘 SSD 云硬盘 40 GB -# 镜像 OpenClaw(龙虾) · Ubuntu Server 24.04 LTS 64bit -# -# --- 网络 --- -# 公网 IPv4 62.234.90.54 -# 内网 IPv4 10.2.0.9 -# 带宽 3 Mbps -# 流量包 300 GB/月(每月 13 日重置) -# IPv6 不支持 -# -# --- 密钥 --- -# 密钥 ID lhkp-ienai1os -# -# --- 费用 --- -# 到期时间 2027-05-12 13:49:28 -# 自动续费 未开启 - -HOST=62.234.90.54 -USER=ubuntu -PORT=22 -KEY=keys/tx-ubuntu.pem diff --git a/hosts/tx-ubuntu-ubuntu.yml b/hosts/tx-ubuntu-ubuntu.yml new file mode 100644 index 0000000..51e44f9 --- /dev/null +++ b/hosts/tx-ubuntu-ubuntu.yml @@ -0,0 +1,33 @@ +# ╔══════════════════════════════════════════════════╗ +# ║ tx-ubuntu · 腾讯云轻量应用服务器 ║ +# ╚══════════════════════════════════════════════════╝ + +host: + ip: 62.234.90.54 + user: ubuntu + port: 22 + key: keys/tx-ubuntu.pem + +info: + name: tx_ubuntu_62.234.90.54@20260512 + id: lhins-61pjmw9y + status: 运行中 + region: + city: 北京 + zone: 北京七区 + +spec: + cpu: 4核 + memory: 4GB + disk: SSD云硬盘 40GB + image: OpenClaw(龙虾) · Ubuntu 24.04 LTS + +network: + public_ip: 62.234.90.54 + private_ip: 10.2.0.9 + bandwidth: 3Mbps + traffic: 300GB/月 + +billing: + expire: 2027-05-12 + auto_renew: false