Skip to content

stereon/goops

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GoOps - 轻量级运维自动化工具

Go Version License

GoOps 是一个基于 Go 语言开发的轻量级运维自动化工具,采用 Agent-Server 架构,支持批量执行命令、文件传输等运维操作。

✨ 特性

  • 🚀 零依赖 - 编译后的二进制文件无需任何运行时依赖
  • 🔐 无需SSH - 基于HTTP API的Agent模式,更安全更灵活
  • 高性能 - Go原生并发,支持大规模批量操作
  • 🎯 多种输出格式 - 支持text、json、raw、table等多种输出格式
  • 🔄 自动注册 - Agent启动后自动注册到Server
  • 📦 单文件部署 - 每个组件都是单个可执行文件

🏗️ 架构

┌─────────────┐     HTTP API      ┌─────────────┐
│   Client    │ ──────────────────>│   Server    │
│   (CLI)     │                    │  (中心管理)  │
└─────────────┘                    └──────┬──────┘
                                          │
                                    HTTP API
                                          │
                    ┌─────────────────────┼─────────────────────┐
                    │                     │                     │
              ┌─────▼─────┐        ┌─────▼─────┐        ┌─────▼─────┐
              │   Agent    │        │   Agent    │        │   Agent    │
              │ (执行节点) │        │ (执行节点) │        │ (执行节点) │
              └───────────┘        └───────────┘        └───────────┘

📋 系统要求

  • Linux (Ubuntu 18.04+, CentOS 7+, Debian 9+)
  • 2GB+ RAM
  • Go 1.19+ (仅编译时需要)

🚀 快速开始

1. 编译

# 克隆代码
git clone https://github.com/yourusername/goops.git
cd goops

# 编译
./build.sh

# 生成文件
# - dist/goops-server  (服务端)
# - dist/goops-agent   (Agent)
# - dist/goops         (客户端CLI)

2. 部署

Server部署(中心管理节点)

# 安装Server
cp dist/goops-server /usr/local/bin/
chmod +x /usr/local/bin/goops-server

# 创建systemd服务
cat > /etc/systemd/system/goops-server.service << EOF
[Unit]
Description=GoOps Server
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/bin/goops-server -host 0.0.0.0 -port 8000
Restart=always

[Install]
WantedBy=multi-user.target
EOF

# 启动服务
systemctl daemon-reload
systemctl enable goops-server
systemctl start goops-server

Agent部署(执行节点)

# 安装Agent
cp dist/goops-agent /usr/local/bin/
chmod +x /usr/local/bin/goops-agent

# 创建systemd服务
cat > /etc/systemd/system/goops-agent.service << EOF
[Unit]
Description=GoOps Agent
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/bin/goops-agent -port 9000 -name $(hostname) -server http://SERVER_IP:8000
Restart=always

[Install]
WantedBy=multi-user.target
EOF

# 启动服务
systemctl daemon-reload
systemctl enable goops-agent
systemctl start goops-agent

3. 使用

# 查看所有Agent
goops -s http://SERVER_IP:8000 agents list

# 执行命令(默认在所有Agent上执行)
goops -s http://SERVER_IP:8000 exec 'df -h'

# 不同输出格式
goops -s http://SERVER_IP:8000 exec -o raw 'ls /tmp'    # 原始输出
goops -s http://SERVER_IP:8000 exec -o json 'uptime'    # JSON格式
goops -s http://SERVER_IP:8000 exec -o table 'free -m'  # 表格格式

# 指定Agent执行
goops -s http://SERVER_IP:8000 exec -a agent1,agent2 'ps aux'

# 使用标签执行
goops -s http://SERVER_IP:8000 exec -t production 'systemctl status nginx'

📖 使用示例

批量查看磁盘使用情况

$ goops -s http://192.168.0.3:8000 exec 'df -h'

执行命令: df -h
============================================================

► vm-dev-es-automizely-01-asea1b-9000:
----------------------------------------
Filesystem      Size  Used Avail Use% Mounted on
/dev/root        20G  7.8G   12G  40% /
tmpfs           2.0G     0  2.0G   0% /dev/shm
tmpfs           783M  1.1M  782M   1% /run
✓ 成功 (耗时: 8ms)

► vm-dev-es-automizely-02-asea1a-9000:
----------------------------------------
Filesystem      Size  Used Avail Use% Mounted on
/dev/root        20G  6.5G   14G  33% /
tmpfs           2.0G     0  2.0G   0% /dev/shm
tmpfs           783M  1.0M  782M   1% /run
✓ 成功 (耗时: 7ms)

============================================================
执行完成: 2 个Agent

JSON格式输出

$ goops -s http://192.168.0.3:8000 exec -o json 'uptime'
{
  "success": true,
  "results": {
    "vm-dev-es-automizely-01-asea1b-9000": {
      "Host": "",
      "Output": " 10:45:23 up 5 days, 18:32,  1 user,  load average: 0.15, 0.12, 0.09\n",
      "Error": "",
      "Success": true
    }
  }
}

表格格式输出

$ goops -s http://192.168.0.3:8000 exec -o table 'free -m'

命令: free -m
+----------------------------------------+--------+----------------------+
|                 AGENT                  | STATUS |    OUTPUT (前5行)     |
+----------------------------------------+--------+----------------------+
| vm-dev-es-automizely-01-asea1b-9000   || total  used  free    |
|                                        |        | Mem:   3935  3050    |
|                                        |        | Swap:     0     0    |
+----------------------------------------+--------+----------------------+

🔧 配置

Server配置

Server通过命令行参数配置:

goops-server -host 0.0.0.0 -port 8000

Agent配置

Agent通过命令行参数配置:

goops-agent -port 9000 -name node1 -server http://192.168.0.3:8000 -token secret123

参数说明:

  • -port: Agent监听端口(默认9000)
  • -name: Agent名称(默认为hostname)
  • -server: Server地址(可选,用于自动注册)
  • -token: 认证Token(可选)

📡 API接口

获取Agent列表

GET /api/v1/agents

执行命令

POST /api/v1/exec
Content-Type: application/json

{
  "command": "df -h",
  "agents": ["agent1", "agent2"],  // 可选,默认所有
  "tags": ["production"],           // 可选,按标签选择
  "timeout": 30                     // 可选,超时秒数
}

Agent心跳

POST /api/v1/agents/:id/heartbeat

获取Agent指标

GET /api/v1/agents/:id/metrics

🛠️ 开发

项目结构

goops/
├── cmd/
│   ├── server/         # Server入口
│   ├── agent/          # Agent入口
│   └── client/         # CLI客户端入口
├── internal/
│   ├── server/         # Server核心逻辑
│   ├── executor/       # 命令执行器
│   └── client/         # 客户端SDK
├── build.sh            # 编译脚本
├── go.mod              # Go模块定义
└── README.md           # 本文件

本地开发

# 安装依赖
go mod tidy

# 运行Server
go run cmd/server/main.go

# 运行Agent
go run cmd/agent/main.go -server http://localhost:8000

# 运行CLI
go run cmd/client/main.go exec 'ls'

测试

# 运行单元测试
go test ./...

# 运行集成测试
go test -tags=integration ./...

🚢 生产部署建议

  1. 安全配置

    • 配置Token认证
    • 使用HTTPS(可配合nginx反向代理)
    • 限制Server端口访问(防火墙规则)
  2. 高可用

    • Server可部署多实例,使用负载均衡
    • Agent自动重连机制
  3. 监控

    • 集成Prometheus监控
    • 配置日志收集(journald/syslog)
  4. 性能优化

    • 根据需求调整并发数
    • 合理设置超时时间

📝 更新日志

v1.0.0 (2024-09-12)

  • 初始版本发布
  • 支持Agent-Server架构
  • 支持批量命令执行
  • 多种输出格式支持

🤝 贡献

欢迎提交Issue和Pull Request!

📄 许可证

MIT License

🙏 致谢

📮 联系方式

About

轻量级运维自动化工具

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published