-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(backend): dbconfig migrate 修复 close #1038
- Loading branch information
Showing
13 changed files
with
4,979 additions
and
2,408 deletions.
There are no files selected for viewing
4,039 changes: 2,750 additions & 1,289 deletions
4,039
dbm-services/common/db-config/docs/swagger.json
Large diffs are not rendered by default.
Oops, something went wrong.
3,103 changes: 1,998 additions & 1,105 deletions
3,103
dbm-services/common/db-config/docs/swagger.yaml
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
dbm-services/mysql/db-tools/dbactuator/internal/subcmd/mysqlcmd/oscmd_run.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
dbm-services/mysql/db-tools/dbactuator/pkg/components/mysql/os_command.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package mysql |
134 changes: 134 additions & 0 deletions
134
dbm-services/mysql/db-tools/dbactuator/pkg/components/mysql/oscmd_run.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters