feat: LokiDeploy 初始提交 - 轻量级 K8s Loki 部署平台
Go(Gin) + React(Vite + shadcn/ui + Tailwind) 前后端分离,全中文 UI - 集群管理:Kubeconfig / Token 双接入,kubectl + helm 部署 - 主机管理:SSH 密钥 / 密码双认证,CPU/内存/磁盘使用率监控,SSH 错误中文友好提示 - 存储配置、部署管理、系统设置 - Docker 镜像内置 kubectl + helm,开箱即用 - run.sh 一键启停(start/stop/restart/status) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
commit
2a93de600d
134 files changed
+19595
No files matched your search
@@ -0,0 +1,177 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"lokideploy/models"
|
||||
"lokideploy/utils"
|
||||
)
|
||||
|
||||
type HostService struct{}
|
||||
|
||||
func NewHostService() *HostService {
|
||||
return &HostService{}
|
||||
}
|
||||
|
||||
func (s *HostService) Create(name, hostIP string, port int, user, authType, password, privateKey, keyPhrase, os, arch, labels string) (*models.Host, error) {
|
||||
if port == 0 {
|
||||
port = 22
|
||||
}
|
||||
host := models.Host{
|
||||
ID: models.GetDB().NextID("hosts"),
|
||||
Name: name,
|
||||
HostIP: hostIP,
|
||||
Port: port,
|
||||
User: user,
|
||||
AuthType: authType,
|
||||
OS: os,
|
||||
Arch: arch,
|
||||
Labels: labels,
|
||||
Status: "pending",
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
}
|
||||
if password != "" {
|
||||
enc, _ := utils.Encrypt(password)
|
||||
host.Password = "ENC:" + enc
|
||||
}
|
||||
if privateKey != "" {
|
||||
enc, _ := utils.Encrypt(privateKey)
|
||||
host.PrivateKey = "ENC:" + enc
|
||||
}
|
||||
if keyPhrase != "" {
|
||||
enc, _ := utils.Encrypt(keyPhrase)
|
||||
host.KeyPhrase = "ENC:" + enc
|
||||
}
|
||||
|
||||
db := models.GetDB()
|
||||
db.Lock()
|
||||
db.Hosts = append(db.Hosts, host)
|
||||
db.Unlock()
|
||||
go db.Save()
|
||||
|
||||
go s.Validate(host.ID)
|
||||
return &host, nil
|
||||
}
|
||||
|
||||
func (s *HostService) Get(id uint) (*models.Host, error) {
|
||||
db := models.GetDB()
|
||||
db.RLock()
|
||||
defer db.RUnlock()
|
||||
for i := range db.Hosts {
|
||||
if db.Hosts[i].ID == id {
|
||||
h := db.Hosts[i]
|
||||
return &h, nil
|
||||
}
|
||||
}
|
||||
return nil, utils.ErrNotFound
|
||||
}
|
||||
|
||||
func (s *HostService) List(page, pageSize int) ([]models.Host, int64, error) {
|
||||
db := models.GetDB()
|
||||
db.RLock()
|
||||
defer db.RUnlock()
|
||||
|
||||
total := int64(len(db.Hosts))
|
||||
start := (page - 1) * pageSize
|
||||
if start < 0 || start >= len(db.Hosts) {
|
||||
return []models.Host{}, total, nil
|
||||
}
|
||||
end := start + pageSize
|
||||
if end > len(db.Hosts) {
|
||||
end = len(db.Hosts)
|
||||
}
|
||||
result := make([]models.Host, end-start)
|
||||
copy(result, db.Hosts[start:end])
|
||||
return result, total, nil
|
||||
}
|
||||
|
||||
func (s *HostService) Update(id uint, updates map[string]interface{}) error {
|
||||
db := models.GetDB()
|
||||
db.Lock()
|
||||
defer db.Unlock()
|
||||
for i := range db.Hosts {
|
||||
if db.Hosts[i].ID == id {
|
||||
if v, ok := updates["name"].(string); ok {
|
||||
db.Hosts[i].Name = v
|
||||
}
|
||||
if v, ok := updates["status"].(string); ok {
|
||||
db.Hosts[i].Status = v
|
||||
}
|
||||
if v, ok := updates["os"].(string); ok {
|
||||
db.Hosts[i].OS = v
|
||||
}
|
||||
if v, ok := updates["arch"].(string); ok {
|
||||
db.Hosts[i].Arch = v
|
||||
}
|
||||
db.Hosts[i].UpdatedAt = time.Now()
|
||||
go db.Save()
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return utils.ErrNotFound
|
||||
}
|
||||
|
||||
func (s *HostService) Delete(id uint) error {
|
||||
db := models.GetDB()
|
||||
db.Lock()
|
||||
defer db.Unlock()
|
||||
for i, h := range db.Hosts {
|
||||
if h.ID == id {
|
||||
db.Hosts = append(db.Hosts[:i], db.Hosts[i+1:]...)
|
||||
go db.Save()
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return utils.ErrNotFound
|
||||
}
|
||||
|
||||
func (s *HostService) Validate(id uint) {
|
||||
host, err := s.Get(id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||||
defer cancel()
|
||||
|
||||
password := host.Password
|
||||
if strings.HasPrefix(password, "ENC:") {
|
||||
password, _ = utils.Decrypt(password[4:])
|
||||
}
|
||||
|
||||
client, err := NewSSHClient(host.HostIP, host.Port, host.User, password)
|
||||
if err != nil {
|
||||
s.Update(id, map[string]interface{}{"status": "error"})
|
||||
return
|
||||
}
|
||||
defer client.Close()
|
||||
|
||||
if info, err := client.GetHostInfo(ctx); err == nil {
|
||||
s.Update(id, map[string]interface{}{"status": "active", "os": info.OS, "arch": info.Arch})
|
||||
} else {
|
||||
s.Update(id, map[string]interface{}{"status": "active"})
|
||||
}
|
||||
}
|
||||
|
||||
func (s *HostService) ExecuteCommand(id uint, command string) (string, string, error) {
|
||||
host, err := s.Get(id)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
||||
defer cancel()
|
||||
|
||||
password := host.Password
|
||||
if strings.HasPrefix(password, "ENC:") {
|
||||
password, _ = utils.Decrypt(password[4:])
|
||||
}
|
||||
|
||||
client, err := NewSSHClient(host.HostIP, host.Port, host.User, password)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
defer client.Close()
|
||||
return client.Execute(ctx, command)
|
||||
}
|
||||
Reference in new issue
Block a user