/
model.go
278 lines (253 loc) · 7.65 KB
/
model.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Copyright (C) 2014-2018 Goodrain Co., Ltd.
// RAINBOND, Application Management Platform
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. For any non-GPL usage of Rainbond,
// one or multiple Commercial Licenses authorized by Goodrain Co., Ltd.
// must be obtained first.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package model
import (
"time"
"github.com/goodrain/rainbond/mq/api/grpc/pb"
"github.com/pquerna/ffjson/ffjson"
"github.com/tidwall/gjson"
)
//TaskType 任务类型
type TaskType string
//Task 任务
type Task struct {
Type TaskType `json:"type"`
Body TaskBody `json:"body"`
CreateTime time.Time `json:"time,omitempty"`
User string `json:"user"`
}
//NewTask 从json bytes data create task
func NewTask(data []byte) (*Task, error) {
taskType := gjson.GetBytes(data, "type").String()
body := CreateTaskBody(taskType)
task := Task{
Body: body,
}
err := ffjson.Unmarshal(data, &task)
if err != nil {
return nil, err
}
return &task, err
}
//TransTask transtask
func TransTask(task *pb.TaskMessage) (*Task, error) {
timeT, _ := time.Parse(time.RFC3339, task.CreateTime)
return &Task{
Type: TaskType(task.TaskType),
Body: NewTaskBody(task.TaskType, task.TaskBody),
CreateTime: timeT,
User: task.User,
}, nil
}
func NewTaskBody(taskType string, body []byte) TaskBody {
switch taskType {
case "start":
b := StartTaskBody{}
err := ffjson.Unmarshal(body, &b)
if err != nil {
return nil
}
return b
case "stop":
b := StopTaskBody{}
err := ffjson.Unmarshal(body, &b)
if err != nil {
return nil
}
return b
case "restart":
b := RestartTaskBody{}
err := ffjson.Unmarshal(body, &b)
if err != nil {
return nil
}
return b
case "rolling_upgrade":
b := RollingUpgradeTaskBody{}
err := ffjson.Unmarshal(body, &b)
if err != nil {
return nil
}
return b
case "rollback":
b := RollBackTaskBody{}
err := ffjson.Unmarshal(body, &b)
if err != nil {
return nil
}
return b
case "group_start":
b := GroupStartTaskBody{}
err := ffjson.Unmarshal(body, &b)
if err != nil {
return nil
}
return b
case "group_stop":
b := GroupStopTaskBody{}
err := ffjson.Unmarshal(body, &b)
if err != nil {
return nil
}
return b
case "horizontal_scaling":
b := HorizontalScalingTaskBody{}
err := ffjson.Unmarshal(body, &b)
if err != nil {
return nil
}
return b
case "vertical_scaling":
b := VerticalScalingTaskBody{}
err := ffjson.Unmarshal(body, &b)
if err != nil {
return nil
}
return b
default:
return DefaultTaskBody{}
}
}
//CreateTaskBody 通过类型串创建实体
func CreateTaskBody(taskType string) TaskBody {
switch taskType {
case "start":
return StartTaskBody{}
case "stop":
return StopTaskBody{}
case "restart":
return RestartTaskBody{}
case "rolling_upgrade":
return RollingUpgradeTaskBody{}
case "rollback":
return RollBackTaskBody{}
case "group_start":
return GroupStartTaskBody{}
case "group_stop":
return GroupStopTaskBody{}
case "horizontal_scaling":
return HorizontalScalingTaskBody{}
case "vertical_scaling":
return VerticalScalingTaskBody{}
default:
return DefaultTaskBody{}
}
}
//TaskBody task body
type TaskBody interface{}
//StartTaskBody 启动操作任务主体
type StartTaskBody struct {
TenantID string `json:"tenant_id"`
ServiceID string `json:"service_id"`
DeployVersion string `json:"deploy_version"`
EventID string `json:"event_id"`
}
//StopTaskBody 停止操作任务主体
type StopTaskBody struct {
TenantID string `json:"tenant_id"`
ServiceID string `json:"service_id"`
DeployVersion string `json:"deploy_version"`
EventID string `json:"event_id"`
}
//HorizontalScalingTaskBody 水平伸缩操作任务主体
type HorizontalScalingTaskBody struct {
TenantID string `json:"tenant_id"`
ServiceID string `json:"service_id"`
Replicas int32 `json:"replicas"`
EventID string `json:"event_id"`
}
//VerticalScalingTaskBody 垂直伸缩操作任务主体
type VerticalScalingTaskBody struct {
TenantID string `json:"tenant_id"`
ServiceID string `json:"service_id"`
ContainerCPU int `json:"container_cpu"`
ContainerMemory int `json:"container_memory"`
EventID string `json:"event_id"`
}
//RestartTaskBody 重启操作任务主体
type RestartTaskBody struct {
TenantID string `json:"tenant_id"`
ServiceID string `json:"service_id"`
DeployVersion string `json:"deploy_version"`
EventID string `json:"event_id"`
//重启策略,此策略不保证生效
//例如应用如果为有状态服务,此策略如配置为先启动后关闭,此策略不生效
//无状态服务默认使用先启动后关闭,保证服务不受影响
Strategy []string `json:"strategy"`
}
//StrategyIsValid 验证策略是否有效
//策略包括以下值:
// prestart 先启动后关闭
// prestop 先关闭后启动
// rollingupdate 滚动形式
// grayupdate 灰度形式
// bluegreenupdate 蓝绿形式
//
func StrategyIsValid(strategy []string, serviceDeployType string) bool {
return false
}
//RollingUpgradeTaskBody 升级操作任务主体
type RollingUpgradeTaskBody struct {
TenantID string `json:"tenant_id"`
ServiceID string `json:"service_id"`
//当前前版本
CurrentDeployVersion string `json:"current_deploy_version"`
//升级目标版本
NewDeployVersion string `json:"new_deploy_version"`
EventID string `json:"event_id"`
//重启策略,此策略不保证生效
//例如应用如果为有状态服务,此策略如配置为先启动后关闭,此策略不生效
//无状态服务默认使用先启动后关闭,保证服务不受影响
//如需使用滚动升级等策略,使用多策略方式
Strategy []string `json:"strategy"`
}
//RollBackTaskBody 回滚操作任务主体
type RollBackTaskBody struct {
TenantID string `json:"tenant_id"`
ServiceID string `json:"service_id"`
//当前版本
CurrentDeployVersion string `json:"current_deploy_version"`
//回滚目标版本
OldDeployVersion string `json:"old_deploy_version"`
EventID string `json:"event_id"`
//重启策略,此策略不保证生效
//例如应用如果为有状态服务,此策略如配置为先启动后关闭,此策略不生效
//无状态服务默认使用先启动后关闭,保证服务不受影响
//如需使用滚动升级等策略,使用多策略方式
Strategy []string `json:"strategy"`
}
//GroupStartTaskBody 组应用启动操作任务主体
type GroupStartTaskBody struct {
Services []StartTaskBody `json:"services"`
Dependences []Dependence `json:"dependences"`
//组启动策略
//顺序启动,无序并发启动
Strategy []string `json:"strategy"`
}
//Dependence 依赖关系
type Dependence struct {
CurrentServiceID string `json:"current_service_id"`
DependServiceID string `json:"depend_service_id"`
}
//GroupStopTaskBody 组应用停止操作任务主体
type GroupStopTaskBody struct {
Services []StartTaskBody `json:"services"`
Dependences []Dependence `json:"dependences"`
//组关闭策略
//顺序关系,无序并发关闭
Strategy []string `json:"strategy"`
}
//DefaultTaskBody 默认操作任务主体
type DefaultTaskBody map[string]interface{}