init: ops-assistant codebase
This commit is contained in:
138
cmd/main.go
Normal file
138
cmd/main.go
Normal file
@@ -0,0 +1,138 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"ops-assistant/config"
|
||||
"ops-assistant/internal/bot"
|
||||
"ops-assistant/internal/channel"
|
||||
"ops-assistant/internal/core/ops"
|
||||
"ops-assistant/internal/core/runbook"
|
||||
"ops-assistant/internal/feishu"
|
||||
"ops-assistant/internal/qq"
|
||||
"ops-assistant/internal/service"
|
||||
"ops-assistant/internal/web"
|
||||
"ops-assistant/models"
|
||||
"ops-assistant/version"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) > 1 && (os.Args[1] == "-v" || os.Args[1] == "--version" || os.Args[1] == "version") {
|
||||
fmt.Println(version.Info())
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("🦞 %s", version.Info())
|
||||
|
||||
cfgPath := "config.yaml"
|
||||
if len(os.Args) > 1 {
|
||||
cfgPath = os.Args[1]
|
||||
}
|
||||
|
||||
cfg, err := config.LoadConfig(cfgPath)
|
||||
if err != nil {
|
||||
log.Fatalf("无法加载配置: %v", err)
|
||||
}
|
||||
|
||||
db, err := gorm.Open(sqlite.Open(cfg.Database.Path), &gorm.Config{})
|
||||
if err != nil {
|
||||
log.Fatalf("无法连接数据库: %v", err)
|
||||
}
|
||||
|
||||
if err := models.Migrate(db); err != nil {
|
||||
log.Fatalf("数据库迁移失败: %v", err)
|
||||
}
|
||||
|
||||
if err := channel.InitSecretCipher(cfg.Server.Key); err != nil {
|
||||
log.Fatalf("初始化渠道密钥加密失败: %v", err)
|
||||
}
|
||||
|
||||
if config.IsWeakPassword(cfg.Admin.Password) {
|
||||
log.Printf("⚠️ admin 密码过弱或为默认值,请尽快修改")
|
||||
}
|
||||
|
||||
// DB 渠道配置覆盖 YAML 配置
|
||||
if err := channel.ApplyChannelConfig(db, cfg); err != nil {
|
||||
log.Printf("⚠️ 渠道配置加载失败,继续使用 YAML: %v", err)
|
||||
}
|
||||
|
||||
finance := service.NewFinanceService(db)
|
||||
defer finance.Close()
|
||||
|
||||
if err := runbook.SeedDefaultTargets(db); err != nil {
|
||||
log.Printf("⚠️ 初始化ops targets失败: %v", err)
|
||||
}
|
||||
|
||||
opsSvc := ops.BuildDefault(db, cfg.Database.Path, ".")
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
if cfg.Telegram.Enabled {
|
||||
tgBot, err := bot.NewTGBot(db, cfg.Telegram.Token, finance, opsSvc)
|
||||
if err != nil {
|
||||
log.Printf("⚠️ TG Bot 启动失败: %v", err)
|
||||
} else {
|
||||
go tgBot.Start(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
if cfg.QQBot.Enabled {
|
||||
qqBot := qq.NewQQBot(db, cfg.QQBot.AppID, cfg.QQBot.Secret, finance, opsSvc)
|
||||
go qqBot.Start(ctx)
|
||||
}
|
||||
|
||||
engine := gin.New()
|
||||
engine.Use(gin.Recovery())
|
||||
engine.Use(gin.Logger())
|
||||
|
||||
reloadFn := func() (string, error) {
|
||||
if err := channel.ApplyChannelConfig(db, cfg); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return fmt.Sprintf("reload ok: tg=%v qq=%v feishu=%v", cfg.Telegram.Enabled, cfg.QQBot.Enabled, cfg.Feishu.Enabled), nil
|
||||
}
|
||||
|
||||
webServer := web.NewWebServer(db, cfg.Database.Path, ".", finance, cfg.Server.Port, cfg.Admin.Username, cfg.Admin.Password, cfg.Server.Key, reloadFn)
|
||||
webServer.RegisterRoutes(engine)
|
||||
|
||||
if cfg.Feishu.Enabled {
|
||||
fsBot := feishu.NewBot(db, finance, opsSvc, cfg.Feishu.AppID, cfg.Feishu.AppSecret, cfg.Feishu.VerificationToken, cfg.Feishu.EncryptKey)
|
||||
fsBot.RegisterRoutes(engine)
|
||||
go fsBot.Start(ctx)
|
||||
}
|
||||
|
||||
go func() {
|
||||
logAddr := fmt.Sprintf(":%d", cfg.Server.Port)
|
||||
log.Printf("🌐 Web后台运行在 http://127.0.0.1%s", logAddr)
|
||||
if err := engine.Run(logAddr); err != nil {
|
||||
log.Printf("❌ Web服务启动失败: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
log.Println("🛠️ Ops-Assistant 已全面启动")
|
||||
sig := make(chan os.Signal, 1)
|
||||
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
|
||||
<-sig
|
||||
|
||||
log.Println("⏳ 正在关闭服务...")
|
||||
cancel()
|
||||
time.Sleep(2 * time.Second)
|
||||
|
||||
sqlDB, err := db.DB()
|
||||
if err == nil {
|
||||
sqlDB.Close()
|
||||
}
|
||||
|
||||
log.Println("👋 Ops-Assistant 已关闭")
|
||||
}
|
||||
132
cmd/ops-runner/main.go
Normal file
132
cmd/ops-runner/main.go
Normal file
@@ -0,0 +1,132 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"ops-assistant/internal/core/ops"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 4 {
|
||||
fmt.Println("usage: ops-runner <db_path> <base_dir> <command_text>")
|
||||
os.Exit(2)
|
||||
}
|
||||
dbPath := os.Args[1]
|
||||
baseDir := os.Args[2]
|
||||
cmd := os.Args[3]
|
||||
|
||||
parts := strings.Fields(cmd)
|
||||
if len(parts) < 2 {
|
||||
fmt.Println("ERR: invalid command")
|
||||
os.Exit(2)
|
||||
}
|
||||
|
||||
switch {
|
||||
case len(parts) >= 2 && parts[0] == "/cf" && parts[1] == "dnsadd":
|
||||
if len(parts) < 4 {
|
||||
fmt.Println("ERR: /cf dnsadd <name> <content> [on|off] [type]")
|
||||
os.Exit(2)
|
||||
}
|
||||
inputs := map[string]string{
|
||||
"name": parts[2],
|
||||
"content": parts[3],
|
||||
"type": "A",
|
||||
"proxied": "false",
|
||||
}
|
||||
if len(parts) >= 5 {
|
||||
switch strings.ToLower(parts[4]) {
|
||||
case "on":
|
||||
inputs["proxied"] = "true"
|
||||
if len(parts) >= 6 {
|
||||
inputs["type"] = parts[5]
|
||||
}
|
||||
case "off":
|
||||
inputs["proxied"] = "false"
|
||||
if len(parts) >= 6 {
|
||||
inputs["type"] = parts[5]
|
||||
}
|
||||
case "true":
|
||||
inputs["proxied"] = "true"
|
||||
if len(parts) >= 6 {
|
||||
inputs["type"] = parts[5]
|
||||
}
|
||||
case "false":
|
||||
inputs["proxied"] = "false"
|
||||
if len(parts) >= 6 {
|
||||
inputs["type"] = parts[5]
|
||||
}
|
||||
default:
|
||||
inputs["type"] = parts[4]
|
||||
}
|
||||
}
|
||||
jobID, _, err := ops.RunOnce(dbPath, filepath.Clean(baseDir), cmd, "cf_dns_add", 1, inputs)
|
||||
if err != nil {
|
||||
fmt.Printf("ERR: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Printf("OK job=%d\n", jobID)
|
||||
|
||||
case len(parts) >= 2 && parts[0] == "/cf" && parts[1] == "dnsproxy":
|
||||
if len(parts) < 4 {
|
||||
fmt.Println("ERR: /cf dnsproxy <record_id|name> on|off")
|
||||
os.Exit(2)
|
||||
}
|
||||
mode := strings.ToLower(parts[3])
|
||||
if mode != "on" && mode != "off" {
|
||||
fmt.Println("ERR: /cf dnsproxy <record_id|name> on|off")
|
||||
os.Exit(2)
|
||||
}
|
||||
proxied := "false"
|
||||
if mode == "on" {
|
||||
proxied = "true"
|
||||
}
|
||||
target := parts[2]
|
||||
inputs := map[string]string{
|
||||
"proxied": proxied,
|
||||
"record_id": "__empty__",
|
||||
"name": "__empty__",
|
||||
}
|
||||
if strings.Contains(target, ".") {
|
||||
inputs["name"] = target
|
||||
} else {
|
||||
inputs["record_id"] = target
|
||||
}
|
||||
jobID, _, err := ops.RunOnce(dbPath, filepath.Clean(baseDir), cmd, "cf_dns_proxy", 1, inputs)
|
||||
if err != nil {
|
||||
fmt.Printf("ERR: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Printf("OK job=%d\n", jobID)
|
||||
|
||||
case len(parts) >= 2 && parts[0] == "/cpa" && parts[1] == "status":
|
||||
jobID, _, err := ops.RunOnce(dbPath, filepath.Clean(baseDir), cmd, "cpa_status", 1, map[string]string{})
|
||||
if err != nil {
|
||||
fmt.Printf("ERR: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Printf("OK job=%d\n", jobID)
|
||||
case len(parts) >= 3 && parts[0] == "/cpa" && parts[1] == "usage" && parts[2] == "backup":
|
||||
jobID, _, err := ops.RunOnce(dbPath, filepath.Clean(baseDir), cmd, "cpa_usage_backup", 1, map[string]string{})
|
||||
if err != nil {
|
||||
fmt.Printf("ERR: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Printf("OK job=%d\n", jobID)
|
||||
case len(parts) >= 4 && parts[0] == "/cpa" && parts[1] == "usage" && parts[2] == "restore":
|
||||
inputs := map[string]string{
|
||||
"backup_id": parts[3],
|
||||
}
|
||||
jobID, _, err := ops.RunOnce(dbPath, filepath.Clean(baseDir), cmd, "cpa_usage_restore", 1, inputs)
|
||||
if err != nil {
|
||||
fmt.Printf("ERR: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Printf("OK job=%d\n", jobID)
|
||||
default:
|
||||
fmt.Println("ERR: unsupported command")
|
||||
os.Exit(2)
|
||||
}
|
||||
}
|
||||
66
cmd/runbook_test.go
Normal file
66
cmd/runbook_test.go
Normal file
@@ -0,0 +1,66 @@
|
||||
//go:build runbooktest
|
||||
// +build runbooktest
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"ops-assistant/internal/core/ops"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 4 {
|
||||
fmt.Println("usage: runbook_test <db_path> <base_dir> <command_text>")
|
||||
os.Exit(2)
|
||||
}
|
||||
dbPath := os.Args[1]
|
||||
baseDir := os.Args[2]
|
||||
cmd := os.Args[3]
|
||||
inputs := map[string]string{}
|
||||
// minimal parse for /cf dnsadd <name> <content> [true] [type]
|
||||
parts := split(cmd)
|
||||
if len(parts) >= 4 && parts[0] == "/cf" && parts[1] == "dnsadd" {
|
||||
inputs["name"] = parts[2]
|
||||
inputs["content"] = parts[3]
|
||||
inputs["type"] = "A"
|
||||
inputs["proxied"] = "false"
|
||||
if len(parts) >= 5 {
|
||||
if parts[4] == "true" {
|
||||
inputs["proxied"] = "true"
|
||||
if len(parts) >= 6 {
|
||||
inputs["type"] = parts[5]
|
||||
}
|
||||
} else {
|
||||
inputs["type"] = parts[4]
|
||||
}
|
||||
}
|
||||
}
|
||||
jobID, _, err := ops.RunOnce(dbPath, filepath.Clean(baseDir), cmd, "cf_dns_add", 1, inputs)
|
||||
if err != nil {
|
||||
fmt.Printf("ERR: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Printf("OK job=%d\n", jobID)
|
||||
}
|
||||
|
||||
func split(s string) []string {
|
||||
out := []string{}
|
||||
cur := ""
|
||||
for _, r := range s {
|
||||
if r == ' ' || r == '\t' || r == '\n' {
|
||||
if cur != "" {
|
||||
out = append(out, cur)
|
||||
cur = ""
|
||||
}
|
||||
continue
|
||||
}
|
||||
cur += string(r)
|
||||
}
|
||||
if cur != "" {
|
||||
out = append(out, cur)
|
||||
}
|
||||
return out
|
||||
}
|
||||
Reference in New Issue
Block a user