Files
EdgeAdmin/internal/web/actions/default/clusters/cluster/settings/index.go

211 lines
5.9 KiB
Go
Raw Normal View History

2020-09-06 16:19:34 +08:00
package settings
import (
2022-09-15 15:57:53 +08:00
"encoding/json"
2020-09-06 16:19:34 +08:00
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
2020-10-25 21:27:28 +08:00
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/clusters/grants/grantutils"
2023-06-30 18:08:30 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
2021-10-12 11:43:53 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
2020-10-25 21:27:28 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
2020-09-06 16:19:34 +08:00
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types"
2020-09-06 16:19:34 +08:00
)
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
this.Nav("", "setting", "")
this.SecondMenu("basic")
}
func (this *IndexAction) RunGet(params struct {
ClusterId int64
}) {
2021-10-12 11:43:53 +08:00
// 基本信息
clusterResp, err := this.RPC().NodeClusterRPC().FindEnabledNodeCluster(this.AdminContext(), &pb.FindEnabledNodeClusterRequest{NodeClusterId: params.ClusterId})
2020-09-06 16:19:34 +08:00
if err != nil {
this.ErrorPage(err)
return
}
2020-12-17 17:35:38 +08:00
cluster := clusterResp.NodeCluster
2020-09-06 16:19:34 +08:00
if cluster == nil {
this.WriteString("not found cluster")
return
}
// 认证
var grantMap interface{} = nil
2021-05-25 17:48:51 +08:00
if cluster.NodeGrantId > 0 {
grantResp, err := this.RPC().NodeGrantRPC().FindEnabledNodeGrant(this.AdminContext(), &pb.FindEnabledNodeGrantRequest{NodeGrantId: cluster.NodeGrantId})
2020-09-06 16:19:34 +08:00
if err != nil {
this.ErrorPage(err)
return
}
var grant = grantResp.NodeGrant
2020-09-06 16:19:34 +08:00
if grant != nil {
grantMap = maps.Map{
"id": grant.Id,
"name": grant.Name,
"method": grant.Method,
2023-06-28 19:07:42 +08:00
"methodName": grantutils.FindGrantMethodName(grant.Method, this.LangCode()),
2020-09-06 16:19:34 +08:00
}
}
}
this.Data["grant"] = grantMap
2021-10-12 11:43:53 +08:00
// 时区
this.Data["timeZoneGroups"] = nodeconfigs.FindAllTimeZoneGroups()
this.Data["timeZoneLocations"] = nodeconfigs.FindAllTimeZoneLocations()
if len(cluster.TimeZone) == 0 {
cluster.TimeZone = nodeconfigs.DefaultTimeZoneLocation
}
this.Data["timeZoneLocation"] = nodeconfigs.FindTimeZoneLocation(cluster.TimeZone)
2022-09-15 15:57:53 +08:00
// 时钟
var clockConfig = nodeconfigs.DefaultClockConfig()
if len(cluster.ClockJSON) > 0 {
err = json.Unmarshal(cluster.ClockJSON, clockConfig)
if err != nil {
this.ErrorPage(err)
return
}
if clockConfig == nil {
clockConfig = nodeconfigs.DefaultClockConfig()
}
}
// SSH参数
var sshParams = nodeconfigs.DefaultSSHParams()
if len(cluster.SshParamsJSON) > 0 {
err = json.Unmarshal(cluster.SshParamsJSON, sshParams)
if err != nil {
this.ErrorPage(err)
return
}
}
2022-12-29 17:16:07 +08:00
// DNS信息
var fullDomainName = ""
if len(cluster.DnsName) > 0 && cluster.DnsDomainId > 0 {
domainResp, err := this.RPC().DNSDomainRPC().FindBasicDNSDomain(this.AdminContext(), &pb.FindBasicDNSDomainRequest{DnsDomainId: cluster.DnsDomainId})
if err != nil {
this.ErrorPage(err)
return
}
if domainResp.DnsDomain != nil {
fullDomainName = cluster.DnsName + "." + domainResp.DnsDomain.Name
}
}
2020-09-06 16:19:34 +08:00
this.Data["cluster"] = maps.Map{
2022-09-17 21:04:45 +08:00
"id": cluster.Id,
"name": cluster.Name,
"installDir": cluster.InstallDir,
"timeZone": cluster.TimeZone,
"nodeMaxThreads": cluster.NodeMaxThreads,
"autoOpenPorts": cluster.AutoOpenPorts,
"clock": clockConfig,
"autoRemoteStart": cluster.AutoRemoteStart,
"autoInstallNftables": cluster.AutoInstallNftables,
"autoSystemTuning": cluster.AutoSystemTuning,
"autoTrimDisks": cluster.AutoTrimDisks,
"sshParams": sshParams,
2022-12-29 17:16:07 +08:00
"domainName": fullDomainName,
2020-09-06 16:19:34 +08:00
}
// 默认值
this.Data["defaultNodeMaxThreads"] = nodeconfigs.DefaultMaxThreads
this.Data["defaultNodeMaxThreadsMin"] = nodeconfigs.DefaultMaxThreadsMin
this.Data["defaultNodeMaxThreadsMax"] = nodeconfigs.DefaultMaxThreadsMax
2020-09-06 16:19:34 +08:00
this.Show()
}
2021-04-18 21:19:50 +08:00
// RunPost 保存设置
2020-09-06 16:19:34 +08:00
func (this *IndexAction) RunPost(params struct {
2022-09-17 21:04:45 +08:00
ClusterId int64
Name string
GrantId int64
SshParamsPort int
2022-09-17 21:04:45 +08:00
InstallDir string
TimeZone string
NodeMaxThreads int32
AutoOpenPorts bool
ClockAutoSync bool
ClockServer string
ClockCheckChrony bool
2022-09-17 21:04:45 +08:00
AutoRemoteStart bool
AutoInstallNftables bool
AutoSystemTuning bool
AutoTrimDisks bool
2020-09-06 16:19:34 +08:00
Must *actions.Must
}) {
2020-11-10 21:37:48 +08:00
// 创建日志
2023-06-30 18:08:30 +08:00
defer this.CreateLogInfo(codes.NodeCluster_LogUpdateClusterBasicSettings, params.ClusterId)
2020-11-10 21:37:48 +08:00
2020-09-06 16:19:34 +08:00
params.Must.
Field("name", params.Name).
Require("请输入集群名称")
if params.NodeMaxThreads > 0 {
params.Must.
Field("nodeMaxThreads", params.NodeMaxThreads).
Gte(int64(nodeconfigs.DefaultMaxThreadsMin), "单节点最大线程数最小值不能小于"+types.String(nodeconfigs.DefaultMaxThreadsMin)).
Lte(int64(nodeconfigs.DefaultMaxThreadsMax), "单节点最大线程数最大值不能大于"+types.String(nodeconfigs.DefaultMaxThreadsMax))
}
// ssh
var sshParams = nodeconfigs.DefaultSSHParams()
sshParams.Port = params.SshParamsPort
sshParamsJSON, err := json.Marshal(sshParams)
if err != nil {
this.ErrorPage(err)
return
}
// clock
2022-09-15 15:57:53 +08:00
var clockConfig = nodeconfigs.DefaultClockConfig()
clockConfig.AutoSync = params.ClockAutoSync
clockConfig.Server = params.ClockServer
clockConfig.CheckChrony = params.ClockCheckChrony
2022-09-15 15:57:53 +08:00
clockConfigJSON, err := json.Marshal(clockConfig)
if err != nil {
this.ErrorPage(err)
return
}
err = clockConfig.Init()
if err != nil {
this.ErrorPage(err)
return
}
_, err = this.RPC().NodeClusterRPC().UpdateNodeCluster(this.AdminContext(), &pb.UpdateNodeClusterRequest{
2022-09-17 21:04:45 +08:00
NodeClusterId: params.ClusterId,
Name: params.Name,
NodeGrantId: params.GrantId,
InstallDir: params.InstallDir,
TimeZone: params.TimeZone,
NodeMaxThreads: params.NodeMaxThreads,
AutoOpenPorts: params.AutoOpenPorts,
ClockJSON: clockConfigJSON,
AutoRemoteStart: params.AutoRemoteStart,
AutoInstallNftables: params.AutoInstallNftables,
AutoSystemTuning: params.AutoSystemTuning,
AutoTrimDisks: params.AutoTrimDisks,
SshParamsJSON: sshParamsJSON,
2020-09-06 16:19:34 +08:00
})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}