Skip to content

Commit

Permalink
fix(backend): dbconfig migrate 修复 close #1038
Browse files Browse the repository at this point in the history
  • Loading branch information
seanlook authored and ymakedaq committed Sep 12, 2023
1 parent eca857e commit 4754ec6
Show file tree
Hide file tree
Showing 13 changed files with 4,979 additions and 2,408 deletions.
4,039 changes: 2,750 additions & 1,289 deletions dbm-services/common/db-config/docs/swagger.json

Large diffs are not rendered by default.

3,103 changes: 1,998 additions & 1,105 deletions dbm-services/common/db-config/docs/swagger.yaml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ func MigrateSensitive(db *gorm.DB) error {
ValueDefault: "", FlagEncrypt: 1,
},
}
//confNameIdStart := 1000000
for _, c := range confNames {
confNameIdStart := 1000000
for i, c := range confNames {
if c.ValueDefault == "" {
c.ValueDefault = password.MustGenerate(12, 3, 0, false, true)
logger.Info("sensitive: {Namespace:%s ConfType:%s ConfFile:%s ConfName:%s ValueDefault:%s}",
Expand All @@ -110,15 +110,15 @@ func MigrateSensitive(db *gorm.DB) error {
c.ValueDefault, _ = crypt.EncryptString(c.ValueDefault, key, constvar.EncryptEnableZip)
}
}
//c.ID = uint64(confNameIdStart + i)
c.ID = uint64(confNameIdStart + i)
c.ValueType = "STRING"
c.ValueTypeSub = ""
c.ValueAllowed = ""
c.FlagStatus = 1
}
err := db.Transaction(func(tx *gorm.DB) error {

if err1 := tx.Omit("id", "value_formula", "order_index", "since_version", "stage").
if err1 := tx.Omit("value_formula", "order_index", "since_version", "stage").
Create(confNames).Error; err1 != nil {
return errors.WithMessage(err1, "init sensitive conf_name")
}
Expand Down
6 changes: 2 additions & 4 deletions dbm-services/common/go-pubpkg/cmutil/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,10 @@ func ExecShellCommand(isSudo bool, param string) (stdoutStr string, err error) {
// return stdout, stderr ,err
func ExecCommand(bash bool, cwd string, cmdName string, args ...string) (string, string, error) {
stdout, stderr, err := ExecCommandReturnBytes(bash, cwd, cmdName, args...)
if err != nil {
return "", "", err
}
stdoutStr := strings.TrimSpace(string(stdout))
stderrStr := strings.TrimSpace(string(stderr))
return stdoutStr, stderrStr, nil

return stdoutStr, stderrStr, err
}

// ExecCommandReturnBytes run exec.Command
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func NewMysqlCommand() *cobra.Command {
NewOpenAreaImportSchemaCommand(),
NewOpenAreaDumpData(),
NewOpenAreaImportData(),
OSCmdRunCommand(),
},
},
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package mysqlcmd

import (
"fmt"

"dbm-services/common/go-pubpkg/logger"
"dbm-services/mysql/db-tools/dbactuator/internal/subcmd"
"dbm-services/mysql/db-tools/dbactuator/pkg/components/mysql"
"dbm-services/mysql/db-tools/dbactuator/pkg/util"

"github.com/spf13/cobra"
)

// OSCmdRunAct TODO
type OSCmdRunAct struct {
*subcmd.BaseOptions
Payload mysql.OSCmdRunComp
}

// OSCmdRunCommand godoc
//
// @Summary 执行os简单命令
// @Description 执行os简单命令
// @Tags mysql
// @Accept json
// @Param body body mysql.OSCmds true "short description"
// @Success 200 {object} mysql.OSCmdRunResp
// @Router /mysql/oscmd-run [post]
func OSCmdRunCommand() *cobra.Command {
act := OSCmdRunAct{
BaseOptions: subcmd.GBaseOptions,
}
cmd := &cobra.Command{
Use: "oscmd-run",
Short: "执行os简单命令",
Example: fmt.Sprintf(
`dbactuator mysql oscmd-run %s %s`,
subcmd.CmdBaseExampleStr, subcmd.ToPrettyJson(act.Payload.Example()),
),
Run: func(cmd *cobra.Command, args []string) {
util.CheckErr(act.Validate())
util.CheckErr(act.Init())
util.CheckErr(act.Run())
},
}
return cmd
}

// Init TODO
func (d *OSCmdRunAct) Init() (err error) {
if err = d.BaseOptions.Validate(); err != nil { // @todo 应该在一开始就validate
return err
}
if err = d.Deserialize(&d.Payload.Params); err != nil {
logger.Error("DeserializeAndValidate err %s", err.Error())
return err
}
return
}

// Validate TODO
func (d *OSCmdRunAct) Validate() error {
return nil
}

// Run TODO
func (d *OSCmdRunAct) Run() (err error) {
defer util.LoggerErrorStack(logger.Error, err)
steps := subcmd.Steps{
{
FunName: "执行命令集",
Func: d.Payload.Start,
},
}
if err = steps.Run(); err != nil {
return err
}
logger.Info("oscmd-run done")
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type DumpSchemaAct struct {
Service tbinlogdumper.DumpSchemaComp
}

// NewSenmanticDumpSchemaCommand godoc
// NewDumpSchemaCommand godoc
//
// @Summary 备份表结构并导入
// @Description 备份表结构并导入
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ type DeployTbinlogDumperAct struct {
Service tbinlogdumper.InstallTbinlogDumperComp
}

// NewDeployMySQLInstanceCommand godoc
// NewDeployTbinlogDumperCommand godoc
//
// @Summary 部署 tbinlogdumper 实例
// @Description 部署 tbinlogdumper 实例说明
// @Tags tbinlogdumper
// @Accept json
// @Param body body mysql.InstallTbinlogDumperComp true "short description"
// @Param body body tbinlogdumper.InstallTbinlogDumperComp true "short description"
// @Router /tbinlogdumper/deploy [post]
func NewDeployTbinlogDumperCommand() *cobra.Command {
act := DeployTbinlogDumperAct{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ type UnInstallTbinlogDumperAct struct {
Service tbinlogdumper.UnInstallTbinlogDumperComp
}

// NewUnInstallMysqlCommand TODO
// NewUnInstallTbinlogDumperCommand TODO
//
// @Summary 卸载 tbinlogdumper 实例
// @Description 卸载 tbinlogdumper 实例说明
// @Tags tbinlogdumper
// @Accept json
// @Param body body mysql.InstallTbinlogDumperComp true "short description"
// @Param body body tbinlogdumper.UnInstallTbinlogDumperComp true "short description"
// @Router /tbinlogdumper/uninstall [post]
func NewUnInstallTbinlogDumperCommand() *cobra.Command {
act := UnInstallTbinlogDumperAct{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package mysql
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package mysql

import (
"encoding/json"
"fmt"
"strings"

"dbm-services/common/go-pubpkg/logger"

"github.com/pkg/errors"

"dbm-services/common/go-pubpkg/cmutil"
"dbm-services/mysql/db-tools/dbactuator/pkg/components"
)

// OSCmdRunComp TODO
type OSCmdRunComp struct {
Params OSCmds `json:"extend"`
results []*SimpleCmdResult
errIndex int
}

// OSCmds TODO
type OSCmds struct {
Cmds []SimpleCmd `json:"cmds" validate:"required"`
WorkDir string `json:"work_dir"`
RunUser string `json:"run_user"`
}
type SimpleCmd struct {
CmdName string `json:"cmd_name" validate:"required"`
CmdArgs []string `json:"cmd_args"`
}

type SimpleCmdResult struct {
CmdLine string `json:"cmd_line"`
CmdStdout string `json:"cmd_stdout"`
CmdStderr string `json:"cmd_stderr"`
ErrMsg string `json:"err_msg"`
err error
}

type OSCmdRunResp struct {
Code int `json:"code"`
Message string `json:"message"`
Data []*SimpleCmdResult `json:"data"`
}

func (s *SimpleCmd) Run(workDir string) *SimpleCmdResult {
cmdLine := fmt.Sprintf(`%s %s`, s.CmdName, strings.Join(s.CmdArgs, " "))
cmdResult := &SimpleCmdResult{CmdLine: cmdLine}
if strings.Contains(cmdLine, ";") {
cmdResult.err = errors.New("danger cmd")
cmdResult.ErrMsg = cmdResult.err.Error()
return cmdResult
}
fmt.Println(cmdLine)
switch s.CmdName {
case "mkdir", "ls", "cd", "chown", "chmod":
logger.Info("oscmd_run command:", cmdLine)
stdout, stderr, err := cmutil.ExecCommand(false, workDir, s.CmdName, s.CmdArgs...)
cmdResult.CmdStdout = stdout
cmdResult.CmdStderr = stderr
cmdResult.err = err
default:
cmdResult.err = errors.New("cmd_name is not allowed")
cmdResult.ErrMsg = cmdResult.err.Error()
return cmdResult
}
if cmdResult.err != nil {
cmdResult.ErrMsg = cmdResult.err.Error()
}
return cmdResult
}

// Example TODO
func (s *OSCmdRunComp) Example() interface{} {
comp := OSCmdRunComp{
Params: OSCmds{
Cmds: []SimpleCmd{
{CmdName: "mkdir", CmdArgs: []string{"/data/dbbak/123"}},
{CmdName: "ls", CmdArgs: []string{"/data/dbbak"}},
}},
}
return comp
}

// String 用于打印
func (s *OSCmdRunComp) String() string {
str, _ := json.Marshal(s)
return string(str)
}

// Start TODO
func (s *OSCmdRunComp) Start() error {
s.errIndex = -1
for i, c := range s.Params.Cmds {
res := c.Run(s.Params.WorkDir)
s.results = append(s.results, res)
if res.err != nil {
s.errIndex = i
_ = s.OutputCtx()
return res.err
}
}
return s.OutputCtx()
}

// WaitDone TODO
func (s *OSCmdRunComp) WaitDone() error {
return nil
}

// OutputCtx TODO
func (s *OSCmdRunComp) OutputCtx() error {
var resp OSCmdRunResp
if s.errIndex >= 0 {
resp = OSCmdRunResp{
Message: s.results[s.errIndex].CmdStderr,
Code: 1,
Data: s.results,
}
} else {
resp = OSCmdRunResp{
Code: 0,
Data: s.results,
}
}
ss, err := components.WrapperOutput(resp)
if err != nil {
return err
}
fmt.Println(ss)
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,13 @@ func (r *Reporter) ExecuteBackupClient(fileName string) (taskid string, err erro
}
logger.Log.Infof("upload register file %s", fileName)
taskid, err = backupClient.Upload(fileName)
logger.Log.Infof("upload register file %s with taskid=%s, err=%v", fileName, taskid, err)
if err != nil {
return "", err
}
} else {
taskid = "-1"
logger.Log.Infof("backup_client is not enabled: %s taskid=%s", fileName, taskid)
}
return taskid, nil
}
Expand Down Expand Up @@ -184,6 +186,7 @@ func (r *Reporter) ExecuteBackupClient2(fileName string) (taskid string, err err
}

// ReportBackupResult Report BackupResult info
// 会执行 ExecuteBackupClient upload
func (r *Reporter) ReportBackupResult(backupBaseResult BackupResult) error {
var backupResultArray []BackupResult

Expand Down
2 changes: 1 addition & 1 deletion helm-charts/bk-dbm/charts/dbconfig/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
apiVersion: v2
appVersion: 0.0.1-alpha.46
appVersion: 0.0.1-alpha.47
description: A Helm chart for dbconfig
name: dbconfig
type: application
Expand Down

0 comments on commit 4754ec6

Please sign in to comment.