forked from goodrain/rainbond
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql.go
227 lines (211 loc) · 7.21 KB
/
mysql.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
// 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 mysql
import (
"sync"
"github.com/goodrain/rainbond/db/config"
"github.com/goodrain/rainbond/db/model"
"github.com/Sirupsen/logrus"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "github.com/jinzhu/gorm/dialects/postgres"
)
//Manager db manager
type Manager struct {
db *gorm.DB
config config.Config
initOne sync.Once
models []model.Interface
}
//CreateManager create manager
func CreateManager(config config.Config) (*Manager, error) {
var db *gorm.DB
if config.DBType == "mysql" {
var err error
db, err = gorm.Open("mysql", config.MysqlConnectionInfo+"?charset=utf8&parseTime=True&loc=Local")
if err != nil {
return nil, err
}
}
if config.DBType == "cockroachdb" {
var err error
addr := config.MysqlConnectionInfo
db, err = gorm.Open("postgres", addr)
if err != nil {
return nil, err
}
}
manager := &Manager{
db: db,
config: config,
initOne: sync.Once{},
}
db.SetLogger(manager)
manager.RegisterTableModel()
manager.CheckTable()
logrus.Debug("mysql db driver create")
return manager, nil
}
//CloseManager 关闭管理器
func (m *Manager) CloseManager() error {
return m.db.Close()
}
//Begin begin a transaction
func (m *Manager) Begin() *gorm.DB {
return m.db.Begin()
}
//Print Print
func (m *Manager) Print(v ...interface{}) {
logrus.Info(v)
}
//RegisterTableModel register table model
func (m *Manager) RegisterTableModel() {
m.models = append(m.models, &model.Tenants{})
m.models = append(m.models, &model.TenantServices{})
m.models = append(m.models, &model.TenantServicesPort{})
m.models = append(m.models, &model.TenantServiceRelation{})
m.models = append(m.models, &model.TenantServiceEnvVar{})
m.models = append(m.models, &model.TenantServiceMountRelation{})
m.models = append(m.models, &model.TenantServiceVolume{})
m.models = append(m.models, &model.TenantServiceLable{})
m.models = append(m.models, &model.K8sService{})
m.models = append(m.models, &model.K8sDeployReplication{})
m.models = append(m.models, &model.K8sPod{})
m.models = append(m.models, &model.ServiceProbe{})
m.models = append(m.models, &model.TenantServiceStatus{})
m.models = append(m.models, &model.LicenseInfo{})
m.models = append(m.models, &model.TenantServicesDelete{})
//vs map port
m.models = append(m.models, &model.TenantServiceLBMappingPort{})
m.models = append(m.models, &model.TenantPlugin{})
m.models = append(m.models, &model.TenantPluginBuildVersion{})
m.models = append(m.models, &model.TenantServicePluginRelation{})
m.models = append(m.models, &model.TenantPluginVersionEnv{})
m.models = append(m.models, &model.CodeCheckResult{})
m.models = append(m.models, &model.AppPublish{})
m.models = append(m.models, &model.ServiceEvent{})
m.models = append(m.models, &model.VersionInfo{})
m.models = append(m.models, &model.RegionUserInfo{})
m.models = append(m.models, &model.TenantServicesStreamPluginPort{})
m.models = append(m.models, &model.RegionAPIClass{})
m.models = append(m.models, &model.RegionProcotols{})
m.models = append(m.models, &model.LocalScheduler{})
m.models = append(m.models, &model.NotificationEvent{})
m.models = append(m.models, &model.AppStatus{})
m.models = append(m.models, &model.AppBackup{})
}
//CheckTable check and create tables
func (m *Manager) CheckTable() {
m.initOne.Do(func() {
for _, md := range m.models {
if !m.db.HasTable(md) {
if m.config.DBType == "mysql" {
err := m.db.Set("gorm:table_options", "ENGINE=InnoDB charset=utf8").CreateTable(md).Error
if err != nil {
logrus.Errorf("auto create table %s to db error."+err.Error(), md.TableName())
} else {
logrus.Infof("auto create table %s to db success", md.TableName())
}
} else { //cockroachdb
err := m.db.CreateTable(md).Error
if err != nil {
logrus.Errorf("auto create cockroachdb table %s to db error."+err.Error(), md.TableName())
} else {
logrus.Infof("auto create cockroachdb table %s to db success", md.TableName())
}
}
} else {
if err := m.db.AutoMigrate(md).Error; err != nil {
logrus.Errorf("auto Migrate table %s to db error."+err.Error(), md.TableName())
}
}
}
m.patchTable()
})
}
func (m *Manager) patchTable() {
// Permissions set
var rac model.RegionAPIClass
if err := m.db.Where("class_level=? and prefix=?", "server_source", "/v2/show").Find(&rac).Error; err != nil {
if err == gorm.ErrRecordNotFound {
data := map[string]string{
"/v2/show": "server_source",
"/v2/opentsdb": "server_source",
"/v2/resources": "server_source",
"/v2/builder": "server_source",
"/v2/tenants": "server_source",
"/v2/app": "server_source",
"/api/v1": "server_source",
"/v2/nodes": "node_manager",
"/v2/job": "node_manager",
"/v2/tasks": "node_manager",
"/v2/taskgroups": "node_manager",
"/v2/tasktemps": "node_manager",
"/v2/configs": "node_manager",
}
tx := m.Begin()
var rollback bool
for k, v := range data {
if err := m.RegionAPIClassDaoTransactions(tx).AddModel(&model.RegionAPIClass{
ClassLevel: v,
Prefix: k,
}); err != nil {
tx.Rollback()
rollback = true
break
}
}
if !rollback {
tx.Commit()
}
}
}
//Port Protocol support
var rps model.RegionProcotols
if err := m.db.Where("protocol_group=? and protocol_child=?", "http", "http").Find(&rps).Error; err != nil {
if err == gorm.ErrRecordNotFound {
data := map[string][]string{
"http": []string{"http"},
"stream": []string{"mysql", "tcp", "udp"},
}
tx := m.Begin()
var rollback bool
for k, v := range data {
for _, v1 := range v {
if err := m.RegionProcotolsDaoTransactions(tx).AddModel(&model.RegionProcotols{
ProtocolGroup: k,
ProtocolChild: v1,
APIVersion: "v2",
IsSupport: true,
}); err != nil {
tx.Rollback()
rollback = true
break
}
}
}
if !rollback {
tx.Commit()
}
}
}
//set plugin version image name length
if err := m.db.Exec("alter table tenant_plugin_build_version modify column base_image varchar(200);").Error; err != nil {
logrus.Errorf("alter table tenant_plugin_build_version error %s", err.Error())
}
if err := m.db.Exec("alter table tenant_plugin_build_version modify column build_local_image varchar(200);").Error; err != nil {
logrus.Errorf("alter table tenant_plugin_build_version error %s", err.Error())
}
}