forked from goodrain/rainbond
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_plugin.go
293 lines (277 loc) · 11.1 KB
/
service_plugin.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// 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 handler
import (
"context"
"fmt"
"strings"
"github.com/jinzhu/gorm"
"github.com/Sirupsen/logrus"
api_model "github.com/goodrain/rainbond/api/model"
"github.com/goodrain/rainbond/api/util"
"github.com/goodrain/rainbond/db"
dbmodel "github.com/goodrain/rainbond/db/model"
"github.com/pquerna/ffjson/ffjson"
)
//GetTenantServicePluginRelation GetTenantServicePluginRelation
func (s *ServiceAction) GetTenantServicePluginRelation(serviceID string) ([]*dbmodel.TenantServicePluginRelation, *util.APIHandleError) {
gps, err := db.GetManager().TenantServicePluginRelationDao().GetALLRelationByServiceID(serviceID)
if err != nil {
return nil, util.CreateAPIHandleErrorFromDBError("get service relation by ID", err)
}
return gps, nil
}
//TenantServiceDeletePluginRelation uninstall plugin for app
func (s *ServiceAction) TenantServiceDeletePluginRelation(serviceID, pluginID string) *util.APIHandleError {
tx := db.GetManager().Begin()
if err := db.GetManager().TenantServicePluginRelationDaoTransactions(tx).DeleteRelationByServiceIDAndPluginID(serviceID, pluginID); err != nil {
if err != gorm.ErrRecordNotFound {
tx.Rollback()
return util.CreateAPIHandleErrorFromDBError("delete plugin relation", err)
}
}
if err := db.GetManager().TenantPluginVersionENVDaoTransactions(tx).DeleteEnvByPluginID(serviceID, pluginID); err != nil {
if err != gorm.ErrRecordNotFound {
tx.Rollback()
return util.CreateAPIHandleErrorFromDBError("delete relation env", err)
}
}
if err := db.GetManager().TenantServicesStreamPluginPortDaoTransactions(tx).DeleteAllPluginMappingPortByServiceID(serviceID); err != nil {
if err != gorm.ErrRecordNotFound {
tx.Rollback()
return util.CreateAPIHandleErrorFromDBError("delete upstream plugin mapping port", err)
}
}
if err := tx.Commit().Error; err != nil {
tx.Rollback()
return util.CreateAPIHandleErrorFromDBError("commit delete err", err)
}
return nil
}
//SetTenantServicePluginRelation SetTenantServicePluginRelation
func (s *ServiceAction) SetTenantServicePluginRelation(tenantID, serviceID string, pss *api_model.PluginSetStruct) (*dbmodel.TenantServicePluginRelation, *util.APIHandleError) {
plugin, err := db.GetManager().TenantPluginDao().GetPluginByID(pss.Body.PluginID, tenantID)
if err != nil {
return nil, util.CreateAPIHandleErrorFromDBError("get plugin by plugin id", err)
}
crt, err := db.GetManager().TenantServicePluginRelationDao().CheckSomeModelLikePluginByServiceID(
serviceID,
plugin.PluginModel,
)
if err != nil {
return nil, util.CreateAPIHandleErrorFromDBError("check plugin model", err)
}
if crt {
return nil, util.CreateAPIHandleError(400, fmt.Errorf("can not add this kind plugin, a same kind plugin has been linked"))
}
pluginversion, err := db.GetManager().TenantPluginBuildVersionDao().GetBuildVersionByVersionID(plugin.PluginID, pss.Body.VersionID)
if err != nil {
return nil, util.CreateAPIHandleErrorFromDBError("plugin version get error ", err)
}
var openPorts = make(map[int]bool)
if plugin.PluginModel == dbmodel.UpNetPlugin {
ports, err := db.GetManager().TenantServicesPortDao().GetPortsByServiceID(serviceID)
if err != nil {
return nil, util.CreateAPIHandleErrorFromDBError("get ports by service id", err)
}
for _, p := range ports {
if p.IsInnerService || p.IsOuterService {
openPorts[p.ContainerPort] = true
}
}
}
tx := db.GetManager().Begin()
if configs := pss.Body.ConfigEnvs.ComplexEnvs; configs != nil {
if configs.BasePorts != nil && plugin.PluginModel == dbmodel.UpNetPlugin {
for _, p := range configs.BasePorts {
pluginPort, err := db.GetManager().TenantServicesStreamPluginPortDaoTransactions(tx).SetPluginMappingPort(
tenantID,
serviceID,
dbmodel.UpNetPlugin,
p.Port,
)
if err != nil {
tx.Rollback()
logrus.Errorf(fmt.Sprintf("set upstream port %d error, %v", p.Port, err))
return nil, util.CreateAPIHandleErrorFromDBError(
fmt.Sprintf("set upstream port %d error ", p.Port),
err,
)
}
logrus.Debugf("set plugin upsteam port %d->%d", p.Port, pluginPort)
p.ListenPort = pluginPort
}
}
if err := s.upComplexEnvs(plugin.TenantID, pss.ServiceAlias, plugin.PluginID, pss.Body.ConfigEnvs.ComplexEnvs); err != nil {
tx.Rollback()
return nil, util.CreateAPIHandleError(500, fmt.Errorf("set complex error, %v", err))
}
}
if err := s.normalEnvs(tx, serviceID, plugin.PluginID, pss.Body.ConfigEnvs.NormalEnvs); err != nil {
tx.Rollback()
return nil, util.CreateAPIHandleErrorFromDBError("set service plugin env error ", err)
}
relation := &dbmodel.TenantServicePluginRelation{
VersionID: pss.Body.VersionID,
ServiceID: serviceID,
PluginID: pss.Body.PluginID,
Switch: pss.Body.Switch,
PluginModel: plugin.PluginModel,
ContainerCPU: pluginversion.ContainerCPU,
ContainerMemory: pluginversion.ContainerMemory,
}
if err := db.GetManager().TenantServicePluginRelationDaoTransactions(tx).AddModel(relation); err != nil {
tx.Rollback()
return nil, util.CreateAPIHandleErrorFromDBError("set service plugin relation", err)
}
if err := tx.Commit().Error; err != nil {
tx.Rollback()
return nil, util.CreateAPIHandleErrorFromDBError("commit set service plugin relation", err)
}
return relation, nil
}
//UpdateTenantServicePluginRelation UpdateTenantServicePluginRelation
func (s *ServiceAction) UpdateTenantServicePluginRelation(serviceID string, pss *api_model.PluginSetStruct) (*dbmodel.TenantServicePluginRelation, *util.APIHandleError) {
relation, err := db.GetManager().TenantServicePluginRelationDao().GetRelateionByServiceIDAndPluginID(serviceID, pss.Body.PluginID)
if err != nil {
return nil, util.CreateAPIHandleErrorFromDBError("get relation by serviceid and pluginid", err)
}
relation.VersionID = pss.Body.VersionID
relation.Switch = pss.Body.Switch
if pss.Body.PluginCPU != 0 {
relation.ContainerCPU = pss.Body.PluginCPU
}
if pss.Body.PluginMemory != 0 {
relation.ContainerMemory = pss.Body.PluginMemory
}
err = db.GetManager().TenantServicePluginRelationDao().UpdateModel(relation)
if err != nil {
return nil, util.CreateAPIHandleErrorFromDBError("update relation between plugin and service", err)
}
return relation, nil
}
func (s *ServiceAction) normalEnvs(tx *gorm.DB, serviceID, pluginID string, envs []*api_model.VersionEnv) error {
for _, env := range envs {
tpv := &dbmodel.TenantPluginVersionEnv{
PluginID: pluginID,
ServiceID: serviceID,
EnvName: env.EnvName,
EnvValue: env.EnvValue,
}
if err := db.GetManager().TenantPluginVersionENVDaoTransactions(tx).AddModel(tpv); err != nil {
return err
}
}
return nil
}
//DeleteComplexEnvs DeleteComplexEnvs
func (s *ServiceAction) DeleteComplexEnvs(tenantID, serviceAlias, pluginID string) *util.APIHandleError {
k := fmt.Sprintf("/resources/define/%s/%s/%s",
tenantID,
serviceAlias,
pluginID)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
_, err := s.EtcdCli.Delete(ctx, k)
if err != nil {
logrus.Errorf("delete k %s from etcd error, %v", k, err)
return util.CreateAPIHandleError(500, fmt.Errorf("delete k %s from etcd error, %v", k, err))
}
return nil
}
//UpdateVersionEnv UpdateVersionEnv
func (s *ServiceAction) UpdateVersionEnv(uve *api_model.SetVersionEnv) *util.APIHandleError {
plugin, err := db.GetManager().TenantPluginDao().GetPluginByID(uve.PluginID, uve.Body.TenantID)
if err != nil {
return util.CreateAPIHandleErrorFromDBError("get plugin by plugin id", err)
}
tx := db.GetManager().Begin()
if len(uve.Body.ConfigEnvs.NormalEnvs) != 0 {
if err := s.upNormalEnvs(tx, uve); err != nil {
tx.Rollback()
return util.CreateAPIHandleErrorFromDBError("update version env", err)
}
}
if uve.Body.ConfigEnvs.ComplexEnvs != nil {
if uve.Body.ConfigEnvs.ComplexEnvs.BasePorts != nil && plugin.PluginModel == dbmodel.UpNetPlugin {
for _, p := range uve.Body.ConfigEnvs.ComplexEnvs.BasePorts {
pluginPort, err := db.GetManager().TenantServicesStreamPluginPortDaoTransactions(tx).SetPluginMappingPort(
uve.Body.TenantID,
uve.Body.ServiceID,
dbmodel.UpNetPlugin,
p.Port,
)
if err != nil {
tx.Rollback()
logrus.Errorf(fmt.Sprintf("set upstream port %d error, %v", p.Port, err))
return util.CreateAPIHandleErrorFromDBError(
fmt.Sprintf("set upstream port %d error ", p.Port),
err,
)
}
logrus.Debugf("set plugin upsteam port %d->%d", p.Port, pluginPort)
p.ListenPort = pluginPort
}
}
if err := s.upComplexEnvs(uve.Body.TenantID, uve.ServiceAlias, uve.PluginID, uve.Body.ConfigEnvs.ComplexEnvs); err != nil {
if strings.Contains(err.Error(), "is not exist") {
tx.Rollback()
return util.CreateAPIHandleError(405, err)
}
tx.Rollback()
return util.CreateAPIHandleError(500, fmt.Errorf("update complex error, %v", err))
}
}
if err := s.upNormalEnvs(tx, uve); err != nil {
tx.Rollback()
return util.CreateAPIHandleError(500, fmt.Errorf("update env config error, %v", err))
}
if err := tx.Commit().Error; err != nil {
tx.Rollback()
return util.CreateAPIHandleErrorFromDBError("commit set service plugin env", err)
}
return nil
}
func (s *ServiceAction) upNormalEnvs(tx *gorm.DB, uve *api_model.SetVersionEnv) *util.APIHandleError {
err := db.GetManager().TenantPluginVersionENVDaoTransactions(tx).DeleteEnvByPluginID(uve.Body.ServiceID, uve.PluginID)
if err != nil {
if err != gorm.ErrRecordNotFound {
return util.CreateAPIHandleErrorFromDBError("delete version env", err)
}
}
if err := s.normalEnvs(tx, uve.Body.ServiceID, uve.PluginID, uve.Body.ConfigEnvs.NormalEnvs); err != nil {
return util.CreateAPIHandleErrorFromDBError("update version env", err)
}
return nil
}
func (s *ServiceAction) upComplexEnvs(tenantID, serviceAlias, pluginID string, config *api_model.ResourceSpec) *util.APIHandleError {
if config == nil {
return nil
}
k := fmt.Sprintf("/resources/define/%s/%s/%s", tenantID, serviceAlias, pluginID)
v, err := ffjson.Marshal(config)
if err != nil {
logrus.Errorf("mashal etcd value error, %v", err)
return util.CreateAPIHandleError(500, err)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
_, err = s.EtcdCli.Put(ctx, k, string(v))
if err != nil {
logrus.Errorf("put k %s into etcd error, %v", k, err)
return util.CreateAPIHandleError(500, err)
}
return nil
}