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,164 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"lokideploy/models"
|
||||
"lokideploy/services"
|
||||
"lokideploy/utils"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type HostController struct {
|
||||
service *services.HostService
|
||||
}
|
||||
|
||||
func NewHostController() *HostController {
|
||||
return &HostController{service: services.NewHostService()}
|
||||
}
|
||||
|
||||
type CreateHostRequest struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
HostIP string `json:"hostIP" binding:"required"`
|
||||
Port int `json:"port"`
|
||||
User string `json:"user" binding:"required"`
|
||||
AuthType string `json:"authType" binding:"required,oneof=password key"`
|
||||
Password string `json:"password"`
|
||||
PrivateKey string `json:"privateKey"`
|
||||
KeyPhrase string `json:"keyPhrase"`
|
||||
OS string `json:"os"`
|
||||
Arch string `json:"arch"`
|
||||
Labels string `json:"labels"`
|
||||
}
|
||||
|
||||
func (ctrl *HostController) Create(c *gin.Context) {
|
||||
var req CreateHostRequest
|
||||
if !utils.BindAndValidate(c, &req) {
|
||||
return
|
||||
}
|
||||
host, err := ctrl.service.Create(req.Name, req.HostIP, req.Port, req.User, req.AuthType,
|
||||
req.Password, req.PrivateKey, req.KeyPhrase, req.OS, req.Arch, req.Labels)
|
||||
if err != nil {
|
||||
utils.Error(c, 400, err.Error())
|
||||
return
|
||||
}
|
||||
utils.Success(c, models.SanitizedJSON(host))
|
||||
}
|
||||
|
||||
func (ctrl *HostController) Get(c *gin.Context) {
|
||||
id, err := utils.IntParam(c, "id")
|
||||
if err != nil {
|
||||
utils.BadRequest(c, "ID 无效")
|
||||
return
|
||||
}
|
||||
host, err := ctrl.service.Get(uint(id))
|
||||
if err != nil {
|
||||
utils.NotFound(c, "主机不存在")
|
||||
return
|
||||
}
|
||||
utils.Success(c, models.SanitizedJSON(host))
|
||||
}
|
||||
|
||||
func (ctrl *HostController) List(c *gin.Context) {
|
||||
page, pageSize := utils.Pagination(c)
|
||||
hosts, total, err := ctrl.service.List(page, pageSize)
|
||||
if err != nil {
|
||||
utils.Error(c, 500, err.Error())
|
||||
return
|
||||
}
|
||||
utils.Success(c, utils.PageData{
|
||||
List: models.SanitizedJSON(hosts),
|
||||
Total: total, Page: page, PageSize: pageSize,
|
||||
})
|
||||
}
|
||||
|
||||
func (ctrl *HostController) Update(c *gin.Context) {
|
||||
id, err := utils.IntParam(c, "id")
|
||||
if err != nil {
|
||||
utils.BadRequest(c, "ID 无效")
|
||||
return
|
||||
}
|
||||
var updates map[string]interface{}
|
||||
if !utils.BindAndValidate(c, &updates) {
|
||||
return
|
||||
}
|
||||
if err := ctrl.service.Update(uint(id), updates); err != nil {
|
||||
utils.Error(c, 400, err.Error())
|
||||
return
|
||||
}
|
||||
utils.Success(c, gin.H{"message": "updated"})
|
||||
}
|
||||
|
||||
func (ctrl *HostController) Delete(c *gin.Context) {
|
||||
id, err := utils.IntParam(c, "id")
|
||||
if err != nil {
|
||||
utils.BadRequest(c, "ID 无效")
|
||||
return
|
||||
}
|
||||
if err := ctrl.service.Delete(uint(id)); err != nil {
|
||||
utils.Error(c, 400, err.Error())
|
||||
return
|
||||
}
|
||||
utils.Success(c, gin.H{"message": "deleted"})
|
||||
}
|
||||
|
||||
func (ctrl *HostController) Validate(c *gin.Context) {
|
||||
id, err := utils.IntParam(c, "id")
|
||||
if err != nil {
|
||||
utils.BadRequest(c, "ID 无效")
|
||||
return
|
||||
}
|
||||
go ctrl.service.Validate(uint(id))
|
||||
utils.Success(c, gin.H{"message": "validation started"})
|
||||
}
|
||||
|
||||
func (ctrl *HostController) GetInfo(c *gin.Context) {
|
||||
id, err := utils.IntParam(c, "id")
|
||||
if err != nil {
|
||||
utils.BadRequest(c, "ID 无效")
|
||||
return
|
||||
}
|
||||
host, err := ctrl.service.Get(uint(id))
|
||||
if err != nil {
|
||||
utils.NotFound(c, "主机不存在")
|
||||
return
|
||||
}
|
||||
|
||||
password := host.Password
|
||||
if len(password) > 4 && password[:4] == "ENC:" {
|
||||
password, _ = utils.Decrypt(password[4:])
|
||||
}
|
||||
|
||||
client, err := services.NewSSHClient(host.HostIP, host.Port, host.User, password)
|
||||
if err != nil {
|
||||
utils.Error(c, 500, err.Error())
|
||||
return
|
||||
}
|
||||
defer client.Close()
|
||||
|
||||
info, err := client.GetHostInfo(c.Request.Context())
|
||||
if err != nil {
|
||||
utils.Error(c, 500, err.Error())
|
||||
return
|
||||
}
|
||||
utils.Success(c, info)
|
||||
}
|
||||
|
||||
func (ctrl *HostController) Execute(c *gin.Context) {
|
||||
id, err := utils.IntParam(c, "id")
|
||||
if err != nil {
|
||||
utils.BadRequest(c, "ID 无效")
|
||||
return
|
||||
}
|
||||
var req struct {
|
||||
Command string `json:"command" binding:"required"`
|
||||
}
|
||||
if !utils.BindAndValidate(c, &req) {
|
||||
return
|
||||
}
|
||||
stdout, stderr, err := ctrl.service.ExecuteCommand(uint(id), req.Command)
|
||||
if err != nil {
|
||||
utils.Error(c, 500, err.Error())
|
||||
return
|
||||
}
|
||||
utils.Success(c, gin.H{"stdout": stdout, "stderr": stderr})
|
||||
}
|
||||
Reference in new issue
Block a user