Migrate host config from flat key-value to YAML format

This commit is contained in:
2026-05-13 15:11:05 +08:00
parent 1904288e9b
commit c810f4ef6c
6 changed files with 172 additions and 104 deletions
+37 -8
View File
@@ -42,13 +42,42 @@ tx-ubuntu-ubuntu # → 62.234.90.54 用户 ubuntu
tx-ubuntu-hugo # → 62.234.90.54 用户 hugo tx-ubuntu-hugo # → 62.234.90.54 用户 hugo
``` ```
文件内容: YAML 格式(.yml 后缀),新建主机复制下面模板改:
``` ```yaml
HOST=<IP或域名> # ╔══════════════════════════════════════════════════╗
USER=<SSH用户名> # ║ example · 示例主机 ║
PORT=<SSH端口> # ╚══════════════════════════════════════════════════╝
KEY=keys/<密钥文件>.pem
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/ — 脚本 ### scripts/ — 脚本
@@ -77,8 +106,8 @@ KEY=keys/<密钥文件>.pem
# 1. 注册密钥 # 1. 注册密钥
claw key add my-host C:\Users\me\Downloads\key.pem claw key add my-host C:\Users\me\Downloads\key.pem
# 2. 写主机描述 # 2. 写主机描述 (YAML 格式)
notepad hosts\my-host-ubuntu-root notepad hosts\my-host-ubuntu-root.yml
# 3. 开工 # 3. 开工
claw ssh my-host-ubuntu-root claw ssh my-host-ubuntu-root
+67 -29
View File
@@ -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 # Load host config
# ============================================================ # ============================================================
function Load-Host($Name) { function Load-Host($Name) {
$HostFile = Join-Path $ClawRoot "hosts\$Name" $HostFile = Join-Path $ClawRoot "hosts\$Name.yml"
if (-not (Test-Path $HostFile)) { if (-not (Test-Path $HostFile)) {
Write-Host "ERROR: host '$Name' not found (hosts\$Name)" -ForegroundColor Red # fallback: try without .yml extension (old format)
Write-Host "Available hosts:" -ForegroundColor Yellow $HostFile = Join-Path $ClawRoot "hosts\$Name"
List-Hosts 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 exit 1
} }
$script:HostIP = $null $script:HostIP = if ($h.ip) { $h.ip } else { $null }
$script:HostUser = "root" $script:HostUser = if ($h.user) { $h.user } else { "root" }
$script:HostPort = "22" $script:HostPort = if ($h.port) { $h.port } else { "22" }
$script:HostKey = $null $script:HostKey = if ($h.key) { $h.key } else { $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() }
}
if (-not $HostIP) { 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 exit 1
} }
@@ -86,19 +120,23 @@ function Build-SshArgs {
function List-Hosts { function List-Hosts {
$hostDir = Join-Path $ClawRoot "hosts" $hostDir = Join-Path $ClawRoot "hosts"
if (-not (Test-Path $hostDir) -or @(Get-ChildItem $hostDir).Count -eq 0) { 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 return
} }
Get-ChildItem $hostDir | ForEach-Object { Get-ChildItem $hostDir -Filter *.yml | ForEach-Object {
$name = $_.Name $name = $_.BaseName
$h = ""; $u = "root"; $p = "22" try {
Get-Content $_.FullName | ForEach-Object { $cfg = Parse-Yaml $_.FullName
$line = $_.Trim() $h = $cfg.host
if ($line -match '^HOST=(.+)$') { $h = $matches[1].Trim() } if ($h) {
if ($line -match '^USER=(.+)$') { $u = $matches[1].Trim() } $ip = if ($h.ip) { $h.ip } else { "?" }
if ($line -match '^PORT=(.+)$') { $p = $matches[1].Trim() } $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" $dest = Join-Path $ClawRoot "keys\$Name.pem"
Copy-Item $Src $dest -Force Copy-Item $Src $dest -Force
Write-Host ">>> Key saved: keys\$Name.pem" -ForegroundColor Green 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" { "list" {
Write-Host "Registered keys:" -ForegroundColor Yellow Write-Host "Registered keys:" -ForegroundColor Yellow
-33
View File
@@ -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
+35
View File
@@ -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
-34
View File
@@ -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
+33
View File
@@ -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