2018-03-14 14:12:26 +08:00
|
|
|
// Copyright (C) 2014-2018 Goodrain Co., Ltd.
|
2017-11-07 11:40:44 +08:00
|
|
|
// RAINBOND, Application Management Platform
|
2018-03-14 14:33:31 +08:00
|
|
|
|
2017-11-07 11:40:44 +08:00
|
|
|
// 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.
|
2018-03-14 14:33:31 +08:00
|
|
|
|
2017-11-07 11:40:44 +08:00
|
|
|
// 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.
|
2018-03-14 14:33:31 +08:00
|
|
|
|
2017-11-07 11:40:44 +08:00
|
|
|
// 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 (
|
2017-11-09 14:50:28 +08:00
|
|
|
"sync"
|
|
|
|
|
2018-04-24 16:44:59 +08:00
|
|
|
"github.com/goodrain/rainbond/db/config"
|
|
|
|
"github.com/goodrain/rainbond/db/model"
|
2017-11-07 11:40:44 +08:00
|
|
|
"github.com/jinzhu/gorm"
|
2020-09-19 17:17:28 +08:00
|
|
|
"github.com/sirupsen/logrus"
|
2020-04-10 16:52:06 +08:00
|
|
|
|
|
|
|
// import sql driver manually
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
2017-11-07 11:40:44 +08:00
|
|
|
_ "github.com/jinzhu/gorm/dialects/postgres"
|
|
|
|
)
|
|
|
|
|
|
|
|
//Manager db manager
|
|
|
|
type Manager struct {
|
|
|
|
db *gorm.DB
|
|
|
|
config config.Config
|
|
|
|
initOne sync.Once
|
|
|
|
models []model.Interface
|
|
|
|
}
|
|
|
|
|
2018-04-13 11:14:32 +08:00
|
|
|
//CreateManager create manager
|
2017-11-07 11:40:44 +08:00
|
|
|
func CreateManager(config config.Config) (*Manager, error) {
|
|
|
|
var db *gorm.DB
|
|
|
|
if config.DBType == "mysql" {
|
|
|
|
var err error
|
2021-12-03 10:47:41 +08:00
|
|
|
db, err = gorm.Open("mysql", config.MysqlConnectionInfo+"?charset=utf8mb4&parseTime=True&loc=Local")
|
2017-11-07 11:40:44 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if config.DBType == "cockroachdb" {
|
|
|
|
var err error
|
2018-04-26 11:12:34 +08:00
|
|
|
addr := config.MysqlConnectionInfo
|
2017-11-07 11:40:44 +08:00
|
|
|
db, err = gorm.Open("postgres", addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2021-05-20 00:01:37 +08:00
|
|
|
if config.ShowSQL {
|
|
|
|
db = db.Debug()
|
|
|
|
}
|
2017-11-07 11:40:44 +08:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2021-05-20 00:01:37 +08:00
|
|
|
// DB returns the db.
|
|
|
|
func (m *Manager) DB() *gorm.DB {
|
|
|
|
return m.db
|
|
|
|
}
|
|
|
|
|
2019-11-05 11:34:23 +08:00
|
|
|
// EnsureEndTransactionFunc -
|
|
|
|
func (m *Manager) EnsureEndTransactionFunc() func(tx *gorm.DB) {
|
|
|
|
return func(tx *gorm.DB) {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
logrus.Errorf("Unexpected panic occurred, rollback transaction: %v", r)
|
|
|
|
tx.Rollback()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-07 11:40:44 +08:00
|
|
|
//Print Print
|
|
|
|
func (m *Manager) Print(v ...interface{}) {
|
2019-02-15 17:38:31 +08:00
|
|
|
logrus.Info(v...)
|
2017-11-07 11:40:44 +08:00
|
|
|
}
|
|
|
|
|
2018-04-13 11:14:32 +08:00
|
|
|
//RegisterTableModel register table model
|
2017-11-07 11:40:44 +08:00
|
|
|
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{})
|
2018-11-22 14:33:29 +08:00
|
|
|
m.models = append(m.models, &model.TenantServiceProbe{})
|
2017-11-07 11:40:44 +08:00
|
|
|
m.models = append(m.models, &model.LicenseInfo{})
|
|
|
|
m.models = append(m.models, &model.TenantServicesDelete{})
|
|
|
|
m.models = append(m.models, &model.TenantServiceLBMappingPort{})
|
|
|
|
m.models = append(m.models, &model.TenantPlugin{})
|
|
|
|
m.models = append(m.models, &model.TenantPluginBuildVersion{})
|
2017-11-09 14:50:28 +08:00
|
|
|
m.models = append(m.models, &model.TenantServicePluginRelation{})
|
|
|
|
m.models = append(m.models, &model.TenantPluginVersionEnv{})
|
2019-02-15 17:38:31 +08:00
|
|
|
m.models = append(m.models, &model.TenantPluginVersionDiscoverConfig{})
|
2017-11-14 18:54:27 +08:00
|
|
|
m.models = append(m.models, &model.CodeCheckResult{})
|
2017-11-16 16:30:22 +08:00
|
|
|
m.models = append(m.models, &model.ServiceEvent{})
|
2017-11-20 17:29:39 +08:00
|
|
|
m.models = append(m.models, &model.VersionInfo{})
|
2017-12-03 19:11:16 +08:00
|
|
|
m.models = append(m.models, &model.RegionUserInfo{})
|
2017-12-15 00:28:25 +08:00
|
|
|
m.models = append(m.models, &model.TenantServicesStreamPluginPort{})
|
2017-12-07 09:56:08 +08:00
|
|
|
m.models = append(m.models, &model.RegionAPIClass{})
|
2018-01-02 18:02:08 +08:00
|
|
|
m.models = append(m.models, &model.RegionProcotols{})
|
2018-04-14 15:20:22 +08:00
|
|
|
m.models = append(m.models, &model.LocalScheduler{})
|
2018-05-11 13:44:15 +08:00
|
|
|
m.models = append(m.models, &model.NotificationEvent{})
|
2018-05-10 18:54:32 +08:00
|
|
|
m.models = append(m.models, &model.AppStatus{})
|
2018-05-23 11:08:44 +08:00
|
|
|
m.models = append(m.models, &model.AppBackup{})
|
2018-11-14 23:08:30 +08:00
|
|
|
m.models = append(m.models, &model.ServiceSourceConfig{})
|
2020-09-17 15:45:46 +08:00
|
|
|
m.models = append(m.models, &model.Application{})
|
2020-09-21 18:33:10 +08:00
|
|
|
m.models = append(m.models, &model.ApplicationConfigGroup{})
|
2020-11-13 09:42:17 +08:00
|
|
|
m.models = append(m.models, &model.ConfigGroupService{})
|
2020-10-08 16:10:03 +08:00
|
|
|
m.models = append(m.models, &model.ConfigGroupItem{})
|
2018-11-18 11:53:11 +08:00
|
|
|
// gateway
|
|
|
|
m.models = append(m.models, &model.Certificate{})
|
|
|
|
m.models = append(m.models, &model.RuleExtension{})
|
2018-11-23 20:39:34 +08:00
|
|
|
m.models = append(m.models, &model.HTTPRule{})
|
2022-01-05 17:13:08 +08:00
|
|
|
m.models = append(m.models, &model.HTTPRuleRewrite{})
|
2018-11-23 20:39:34 +08:00
|
|
|
m.models = append(m.models, &model.TCPRule{})
|
2019-01-06 22:13:19 +08:00
|
|
|
m.models = append(m.models, &model.TenantServiceConfigFile{})
|
2019-02-24 05:13:54 +08:00
|
|
|
m.models = append(m.models, &model.Endpoint{})
|
2019-02-28 11:50:54 +08:00
|
|
|
m.models = append(m.models, &model.ThirdPartySvcDiscoveryCfg{})
|
2019-03-11 01:07:10 +08:00
|
|
|
m.models = append(m.models, &model.GwRuleConfig{})
|
2019-12-12 11:59:02 +08:00
|
|
|
|
|
|
|
// volumeType
|
|
|
|
m.models = append(m.models, &model.TenantServiceVolumeType{})
|
2019-11-13 11:05:25 +08:00
|
|
|
// pod autoscaler
|
|
|
|
m.models = append(m.models, &model.TenantServiceAutoscalerRules{})
|
|
|
|
m.models = append(m.models, &model.TenantServiceAutoscalerRuleMetrics{})
|
2019-11-14 15:30:40 +08:00
|
|
|
m.models = append(m.models, &model.TenantServiceScalingRecords{})
|
2020-09-19 17:17:28 +08:00
|
|
|
m.models = append(m.models, &model.TenantServiceMonitor{})
|
2022-07-17 15:25:42 +08:00
|
|
|
m.models = append(m.models, &model.ComponentK8sAttributes{})
|
2022-07-19 12:03:01 +08:00
|
|
|
m.models = append(m.models, &model.K8sResource{})
|
2017-11-07 11:40:44 +08:00
|
|
|
}
|
|
|
|
|
2018-04-13 11:14:32 +08:00
|
|
|
//CheckTable check and create tables
|
2017-11-07 11:40:44 +08:00
|
|
|
func (m *Manager) CheckTable() {
|
|
|
|
m.initOne.Do(func() {
|
|
|
|
for _, md := range m.models {
|
|
|
|
if !m.db.HasTable(md) {
|
|
|
|
if m.config.DBType == "mysql" {
|
2021-12-09 10:36:55 +08:00
|
|
|
err := m.db.Set("gorm:table_options", "ENGINE=InnoDB charset=utf8mb4").CreateTable(md).Error
|
2017-11-07 11:40:44 +08:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|
2018-04-13 11:14:32 +08:00
|
|
|
} else {
|
|
|
|
if err := m.db.AutoMigrate(md).Error; err != nil {
|
|
|
|
logrus.Errorf("auto Migrate table %s to db error."+err.Error(), md.TableName())
|
|
|
|
}
|
2017-11-07 11:40:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
m.patchTable()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-05 11:04:17 +08:00
|
|
|
func (m *Manager) patchTable() {
|
2021-06-02 19:48:47 +08:00
|
|
|
if err := m.db.Exec("alter table tenant_services_envs modify column attr_value text;").Error; err != nil {
|
2019-08-05 11:04:17 +08:00
|
|
|
logrus.Errorf("alter table tenant_services_envs error %s", err.Error())
|
|
|
|
}
|
2019-09-10 12:53:50 +08:00
|
|
|
|
|
|
|
if err := m.db.Exec("alter table tenant_services_event modify column request_body varchar(1024);").Error; err != nil {
|
|
|
|
logrus.Errorf("alter table tenant_services_envent error %s", err.Error())
|
|
|
|
}
|
2019-09-18 17:56:52 +08:00
|
|
|
|
|
|
|
if err := m.db.Exec("update gateway_tcp_rule set ip=? where ip=?", "0.0.0.0", "").Error; err != nil {
|
|
|
|
logrus.Errorf("update gateway_tcp_rule data error %s", err.Error())
|
|
|
|
}
|
2019-12-12 17:40:40 +08:00
|
|
|
if err := m.db.Exec("alter table tenant_services_volume modify column volume_type varchar(64);").Error; err != nil {
|
|
|
|
logrus.Errorf("alter table tenant_services_volume error: %s", err.Error())
|
|
|
|
}
|
2021-11-15 17:39:20 +08:00
|
|
|
if err := m.db.Exec("update tenants set namespace=uuid where namespace is NULL;").Error; err != nil {
|
|
|
|
logrus.Errorf("update tenants namespace error: %s", err.Error())
|
|
|
|
}
|
2021-12-09 20:45:10 +08:00
|
|
|
if err := m.db.Exec("update applications set k8s_app=concat('app-',LEFT(app_id,8)) where k8s_app is NULL;").Error; err != nil {
|
|
|
|
logrus.Errorf("update tenants namespace error: %s", err.Error())
|
|
|
|
}
|
|
|
|
if err := m.db.Exec("update tenant_services set k8s_component_name=service_alias where k8s_component_name is NULL;").Error; err != nil {
|
|
|
|
logrus.Errorf("update tenants namespace error: %s", err.Error())
|
|
|
|
}
|
2019-08-05 11:04:17 +08:00
|
|
|
}
|