Add initial Claw cloud host manager implementation

This commit is contained in:
2026-05-12 16:05:50 +08:00
commit 15680ddf5c
8 changed files with 322 additions and 0 deletions
+10
View File
@@ -0,0 +1,10 @@
# 密钥文件 - 绝对不能提交
keys/*.pem
keys/*.key
keys/*.ppk
# 临时文件
*.swp
*.swo
*~
.DS_Store
+2
View File
@@ -0,0 +1,2 @@
@echo off
powershell -ExecutionPolicy Bypass -File "%~dp0claw.ps1" %*
+203
View File
@@ -0,0 +1,203 @@
# Claw - Cloud Host Manager (PowerShell)
param($Command, $Arg1, $Arg2, $Arg3)
$ErrorActionPreference = "Stop"
$ClawRoot = $PSScriptRoot
function Usage {
@"
Claw - Cloud Host Manager
Usage: .\claw.ps1 <command> [args...]
claw <command> [args...]
Commands:
list List all hosts
ssh <host> SSH into a host
run <host> <script> Run a script on a host (from scripts\)
push <host> <file/dir> Push file/dir to host (from payloads\)
key add <name> <pem> Register a PEM key into keys\
key list List all registered keys
Examples:
claw list
claw ssh my-server
claw run my-server hello.sh
claw push my-server my-app
claw key add my-server C:\Users\me\Downloads\key.pem
"@
}
# ============================================================
# Load host config
# ============================================================
function Load-Host($Name) {
$HostFile = Join-Path $ClawRoot "hosts\$Name"
if (-not (Test-Path $HostFile)) {
Write-Host "ERROR: host '$Name' not found (hosts\$Name)" -ForegroundColor Red
Write-Host "Available hosts:" -ForegroundColor Yellow
List-Hosts
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() }
}
if (-not $HostIP) {
Write-Host "ERROR: hosts\$Name missing HOST=" -ForegroundColor Red
exit 1
}
if ($HostKey) {
$script:HostKey = Join-Path $ClawRoot $HostKey
if (-not (Test-Path $HostKey)) {
Write-Host "ERROR: key file not found: $HostKey" -ForegroundColor Red
exit 1
}
# Fix Windows permissions (SSH requires key NOT readable by other users)
icacls $HostKey /inheritance:r /grant "${env:USERNAME}:(R)" 2>$null | Out-Null
}
}
# ============================================================
# Build SSH args
# ============================================================
function Build-SshArgs {
$a = @("-o", "StrictHostKeyChecking=accept-new")
if ($HostPort) { $a += "-p"; $a += $HostPort }
if ($HostKey) { $a += "-i"; $a += $HostKey }
return $a
}
# ============================================================
# List all hosts
# ============================================================
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\)"
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() }
}
Write-Host (" {0,-20} {1}@{2}:{3}" -f $name, $u, $h, $p)
}
}
# ============================================================
# SSH connect
# ============================================================
function Invoke-Ssh($Name) {
Load-Host $Name
$sa = Build-SshArgs
Write-Host ">>> Connecting to $Name ($HostUser@$HostIP`:$HostPort)" -ForegroundColor Green
& ssh @sa "$HostUser@$HostIP"
}
# ============================================================
# Run script on host
# ============================================================
function Invoke-Run($Name, $Script) {
Load-Host $Name
$sp = Join-Path $ClawRoot "scripts\$Script"
if (-not (Test-Path $sp)) {
Write-Host "ERROR: script '$Script' not found (scripts\$Script)" -ForegroundColor Red
Write-Host "Available scripts:" -ForegroundColor Yellow
Get-ChildItem (Join-Path $ClawRoot "scripts") | ForEach-Object { Write-Host " $($_.Name)" }
exit 1
}
$sa = Build-SshArgs
Write-Host ">>> Running $Script on $Name" -ForegroundColor Green
Get-Content $sp -Raw | & ssh @sa "$HostUser@$HostIP" "bash -s"
}
# ============================================================
# Push files to host
# ============================================================
function Invoke-Push($Name, $Target) {
Load-Host $Name
$src = Join-Path $ClawRoot "payloads\$Target"
if (-not (Test-Path $src)) {
Write-Host "ERROR: '$Target' not found (payloads\$Target)" -ForegroundColor Red
Write-Host "Available payloads:" -ForegroundColor Yellow
Get-ChildItem (Join-Path $ClawRoot "payloads") | ForEach-Object { Write-Host " $($_.Name)" }
exit 1
}
$sca = @("-o", "StrictHostKeyChecking=accept-new")
if ($HostPort) { $sca += "-P"; $sca += $HostPort }
if ($HostKey) { $sca += "-i"; $sca += $HostKey }
if (Test-Path $src -PathType Container) { $sca += "-r" }
Write-Host ">>> Pushing $Target -> $Name`:$HostUser@$HostIP`:/tmp/" -ForegroundColor Green
& scp @sca $src "$HostUser@$HostIP`:/tmp/"
Write-Host ">>> Done. Files at /tmp/$Target" -ForegroundColor Green
}
# ============================================================
# Key management
# ============================================================
function Invoke-Key($Action, $Name, $Src) {
switch ($Action) {
"add" {
if (-not $Name -or -not $Src) {
Write-Host "Usage: claw key add <name> <path-to.pem>" -ForegroundColor Red
exit 1
}
if (-not (Test-Path $Src)) {
Write-Host "ERROR: file not found: $Src" -ForegroundColor Red
exit 1
}
$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"
}
"list" {
Write-Host "Registered keys:" -ForegroundColor Yellow
$kd = Join-Path $ClawRoot "keys"
if ((Test-Path $kd) -and @(Get-ChildItem $kd -Filter *.pem).Count -gt 0) {
Get-ChildItem $kd -Filter *.pem | ForEach-Object {
Write-Host " $($_.Name)"
}
} else {
Write-Host " (none)"
}
}
default {
Write-Host "Usage: claw key <add|list>" -ForegroundColor Red
exit 1
}
}
}
# ============================================================
# Main
# ============================================================
switch ($Command) {
"list" { Write-Host "Hosts:" -ForegroundColor Yellow; List-Hosts }
"ssh" { Invoke-Ssh $Arg1 }
"run" { Invoke-Run $Arg1 $Arg2 }
"push" { Invoke-Push $Arg1 $Arg2 }
"key" { Invoke-Key $Arg1 $Arg2 $Arg3 }
{ $_ -in @("-h", "--help", "help", "", $null) } { Usage }
default { Write-Host "Unknown command: $Command" -ForegroundColor Red; Usage; exit 1 }
}
+33
View File
@@ -0,0 +1,33 @@
# ╔══════════════════════════════════════════════════╗
# ║ example-server · 示例主机 ║
# ╚══════════════════════════════════════════════════╝
#
# --- 基本信息 ---
# 实例名称 example-server
# 实例 ID lhins-xxxxxxxx
# 状态 运行中
# 地域 北京 · 北京七区
#
# --- 配置 ---
# CPU 4 核
# 内存 4 GB
# 系统盘 SSD 云硬盘 40 GB
# 镜像 Ubuntu Server 24.04 LTS 64bit
#
# --- 网络 ---
# 公网 IPv4 1.2.3.4
# 内网 IPv4 10.0.0.1
# 带宽 3 Mbps
# 流量包 300 GB/月
#
# --- 密钥 ---
# 密钥 ID lhkp-xxxxxxxx
#
# --- 费用 ---
# 到期时间 2027-01-01
# 自动续费 未开启
HOST=1.2.3.4
USER=root
PORT=22
KEY=keys/example.pem
+34
View File
@@ -0,0 +1,34 @@
# ╔══════════════════════════════════════════════════╗
# ║ 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
View File
+13
View File
@@ -0,0 +1,13 @@
#!/usr/bin/env bash
# 示例脚本 - 查看主机基本信息
echo "===== 主机信息 ====="
echo "主机名: $(hostname)"
echo "系统: $(cat /etc/os-release | grep PRETTY_NAME | cut -d= -f2 | tr -d '"')"
echo "内核: $(uname -r)"
echo "运行时间: $(uptime -p)"
echo ""
echo "===== 磁盘 ====="
df -h /
echo ""
echo "===== 内存 ====="
free -h
+27
View File
@@ -0,0 +1,27 @@
#!/usr/bin/env bash
# 安装 Docker (Ubuntu/Debian)
set -e
echo ">>> 安装 Docker..."
# 卸载旧版本
apt-get remove -y docker docker-engine docker.io containerd runc 2>/dev/null || true
# 安装依赖
apt-get update
apt-get install -y ca-certificates curl gnupg
# 添加 Docker GPG 密钥
install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
chmod a+r /etc/apt/keyrings/docker.gpg
# 添加源
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
# 安装
apt-get update
apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
echo ">>> Docker 安装完成"
docker --version