-
Notifications
You must be signed in to change notification settings - Fork 5
/
create.go
170 lines (146 loc) · 4.47 KB
/
create.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package clusters
import (
"crypto/rand"
"encoding/json"
"fmt"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/dns/domains/domainutils"
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/rands"
)
type CreateAction struct {
actionutils.ParentAction
}
func (this *CreateAction) Init() {
this.Nav("", "cluster", "create")
}
func (this *CreateAction) RunGet(params struct{}) {
// 是否有域名
hasDomainsResp, err := this.RPC().DNSDomainRPC().ExistAvailableDomains(this.AdminContext(), &pb.ExistAvailableDomainsRequest{})
if err != nil {
this.ErrorPage(err)
return
}
this.Data["hasDomains"] = hasDomainsResp.Exist
// 菜单:集群总数
totalResp, err := this.RPC().NodeClusterRPC().CountAllEnabledNodeClusters(this.AdminContext(), &pb.CountAllEnabledNodeClustersRequest{})
if err != nil {
this.ErrorPage(err)
return
}
this.Data["totalNodeClusters"] = totalResp.Count
// 菜单:节点总数
totalNodesResp, err := this.RPC().NodeRPC().CountAllEnabledNodes(this.AdminContext(), &pb.CountAllEnabledNodesRequest{})
if err != nil {
this.ErrorPage(err)
return
}
this.Data["totalNodes"] = totalNodesResp.Count
// 随机子域名
var defaultDNSName = "g" + rands.HexString(6) + ".cdn"
{
var b = make([]byte, 3)
_, err = rand.Read(b)
if err == nil {
defaultDNSName = fmt.Sprintf("g%x.cdn", b)
}
}
this.Data["defaultDNSName"] = defaultDNSName
this.Data["dnsName"] = defaultDNSName
this.Show()
}
func (this *CreateAction) RunPost(params struct {
Name string
// 缓存策略
CachePolicyId int64
// WAF策略
HttpFirewallPolicyId int64
// 服务配置
MatchDomainStrictly bool
// SSH相关
GrantId int64
InstallDir string
SystemdServiceIsOn bool
AutoInstallNftables bool
AutoSystemTuning bool
AutoTrimDisks bool
MaxConcurrentReads int32
MaxConcurrentWrites int32
// DNS相关
DnsDomainId int64
DnsName string
DnsTTL int32
Must *actions.Must
}) {
params.Must.
Field("name", params.Name).
Require("请输入集群名称")
// 检查DNS名称
if len(params.DnsName) > 0 {
if !domainutils.ValidateDomainFormat(params.DnsName) {
this.FailField("dnsName", "请输入正确的DNS子域名")
}
// 检查是否已经被使用
resp, err := this.RPC().NodeClusterRPC().CheckNodeClusterDNSName(this.AdminContext(), &pb.CheckNodeClusterDNSNameRequest{
NodeClusterId: 0,
DnsName: params.DnsName,
})
if err != nil {
this.ErrorPage(err)
return
}
if resp.IsUsed {
this.FailField("dnsName", "此DNS子域名已经被使用,请换一个再试")
}
}
// TODO 检查DnsDomainId的有效性
// 全局服务配置
var globalServerConfig = serverconfigs.NewGlobalServerConfig()
globalServerConfig.HTTPAll.MatchDomainStrictly = params.MatchDomainStrictly
globalServerConfigJSON, err := json.Marshal(globalServerConfig)
if err != nil {
this.ErrorPage(err)
return
}
// 系统服务
var systemServices = map[string]any{}
if params.SystemdServiceIsOn {
systemServices[nodeconfigs.SystemServiceTypeSystemd] = &nodeconfigs.SystemdServiceConfig{
IsOn: true,
}
}
systemServicesJSON, err := json.Marshal(systemServices)
if err != nil {
this.ErrorPage(err)
return
}
createResp, err := this.RPC().NodeClusterRPC().CreateNodeCluster(this.AdminContext(), &pb.CreateNodeClusterRequest{
Name: params.Name,
NodeGrantId: params.GrantId,
InstallDir: params.InstallDir,
DnsDomainId: params.DnsDomainId,
DnsName: params.DnsName,
DnsTTL: params.DnsTTL,
HttpCachePolicyId: params.CachePolicyId,
HttpFirewallPolicyId: params.HttpFirewallPolicyId,
SystemServicesJSON: systemServicesJSON,
GlobalServerConfigJSON: globalServerConfigJSON,
AutoInstallNftables: params.AutoInstallNftables,
AutoSystemTuning: params.AutoSystemTuning,
AutoTrimDisks: params.AutoTrimDisks,
MaxConcurrentReads: params.MaxConcurrentReads,
MaxConcurrentWrites: params.MaxConcurrentWrites,
})
if err != nil {
this.ErrorPage(err)
return
}
// 创建日志
defer this.CreateLogInfo(codes.NodeCluster_LogCreateCluster, createResp.NodeClusterId)
this.Data["clusterId"] = createResp.NodeClusterId
this.Success()
}