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>
112 lines
2.5 KiB
Go
112 lines
2.5 KiB
Go
package models
|
|
|
|
import (
|
|
"encoding/json"
|
|
"lokideploy/config"
|
|
"os"
|
|
"sync"
|
|
)
|
|
|
|
type JsonDB struct {
|
|
mu sync.RWMutex
|
|
dataFile string
|
|
Users []User `json:"users"`
|
|
Clusters []Cluster `json:"clusters"`
|
|
Hosts []Host `json:"hosts"`
|
|
Storages []StorageConfig `json:"storages"`
|
|
Deployments []LokiDeployment `json:"deployments"`
|
|
Logs []DeploymentLog `json:"logs"`
|
|
Settings []SystemSetting `json:"settings"`
|
|
sequences map[string]uint
|
|
}
|
|
|
|
var db *JsonDB
|
|
|
|
func GetDB() *JsonDB {
|
|
return db
|
|
}
|
|
|
|
func InitDB() error {
|
|
cfg := config.GetConfig()
|
|
_ = os.MkdirAll(cfg.DataDir, 0755)
|
|
|
|
d := &JsonDB{
|
|
dataFile: cfg.DatabasePath,
|
|
Users: []User{},
|
|
Clusters: []Cluster{},
|
|
Hosts: []Host{},
|
|
Storages: []StorageConfig{},
|
|
Deployments: []LokiDeployment{},
|
|
Logs: []DeploymentLog{},
|
|
Settings: []SystemSetting{},
|
|
sequences: map[string]uint{
|
|
"users": 0,
|
|
"clusters": 0,
|
|
"hosts": 0,
|
|
"storages": 0,
|
|
"deployments": 0,
|
|
"logs": 0,
|
|
},
|
|
}
|
|
|
|
if data, err := os.ReadFile(d.dataFile); err == nil {
|
|
if err := json.Unmarshal(data, d); err == nil {
|
|
d.sequences = map[string]uint{}
|
|
for _, u := range d.Users { if u.ID > d.sequences["users"] { d.sequences["users"] = u.ID } }
|
|
for _, c := range d.Clusters { if c.ID > d.sequences["clusters"] { d.sequences["clusters"] = c.ID } }
|
|
for _, h := range d.Hosts { if h.ID > d.sequences["hosts"] { d.sequences["hosts"] = h.ID } }
|
|
for _, s := range d.Storages { if s.ID > d.sequences["storages"] { d.sequences["storages"] = s.ID } }
|
|
for _, de := range d.Deployments { if de.ID > d.sequences["deployments"] { d.sequences["deployments"] = de.ID } }
|
|
for _, l := range d.Logs { if l.ID > d.sequences["logs"] { d.sequences["logs"] = l.ID } }
|
|
}
|
|
}
|
|
|
|
db = d
|
|
return nil
|
|
}
|
|
|
|
func (d *JsonDB) Save() error {
|
|
d.mu.Lock()
|
|
defer d.mu.Unlock()
|
|
data, err := json.MarshalIndent(d, "", " ")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return os.WriteFile(d.dataFile, data, 0644)
|
|
}
|
|
|
|
func (d *JsonDB) NextID(table string) uint {
|
|
d.mu.Lock()
|
|
defer d.mu.Unlock()
|
|
d.sequences[table]++
|
|
return d.sequences[table]
|
|
}
|
|
|
|
func (d *JsonDB) Lock() {
|
|
d.mu.Lock()
|
|
}
|
|
|
|
func (d *JsonDB) Unlock() {
|
|
d.mu.Unlock()
|
|
}
|
|
|
|
func (d *JsonDB) RLock() {
|
|
d.mu.RLock()
|
|
}
|
|
|
|
func (d *JsonDB) RUnlock() {
|
|
d.mu.RUnlock()
|
|
}
|
|
|
|
func (d *JsonDB) CreateUser(user *User) error {
|
|
d.mu.Lock()
|
|
defer d.mu.Unlock()
|
|
if user.ID == 0 {
|
|
user.ID = d.sequences["users"] + 1
|
|
d.sequences["users"] = user.ID
|
|
}
|
|
d.Users = append(d.Users, *user)
|
|
go d.Save()
|
|
return nil
|
|
}
|