Skip to content

Commit 598caa8

Browse files
committed
feat(backend): clone-mysql-priv-in-actor #12601
1 parent 5b27dcd commit 598caa8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+168324
-100
lines changed

dbm-services/mysql/db-tools/dbactuator/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/BurntSushi/toml v1.3.2
1010
github.com/IBM/sarama v1.42.1
1111
github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2
12+
github.com/antlr4-go/antlr/v4 v4.13.1
1213
github.com/cloudfoundry/gosigar v1.3.59
1314
github.com/dustin/go-humanize v1.0.1
1415
github.com/github/gh-ost v1.1.6

dbm-services/mysql/db-tools/dbactuator/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2 h1:ZBbLwSJqkH
1515
github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2/go.mod h1:VSw57q4QFiWDbRnjdX8Cb3Ow0SFncRw+bA/ofY6Q83w=
1616
github.com/agext/levenshtein v1.2.3 h1:YB2fHEn0UJagG8T1rrWknE3ZQzWM06O8AMAatNn7lmo=
1717
github.com/agext/levenshtein v1.2.3/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
18+
github.com/antlr4-go/antlr/v4 v4.13.1 h1:SqQKkuVZ+zWkMMNkjy5FZe5mr5WURWnlpmOuzYWrPrQ=
19+
github.com/antlr4-go/antlr/v4 v4.13.1/go.mod h1:GKmUxMtwp6ZgGwZSva4eWPC5mS6vUAmOABFgjdkM7Nw=
1820
github.com/apparentlymart/go-textseg/v15 v15.0.0 h1:uYvfpb3DyLSCGWnctWKGj857c6ew1u1fNQOlOtuGxQY=
1921
github.com/apparentlymart/go-textseg/v15 v15.0.0/go.mod h1:K8XmNZdhEBkdlyDdvbmmsvpAG721bKi0joRfFdHIWJ4=
2022
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package mysqlcmd
2+
3+
import (
4+
"dbm-services/common/go-pubpkg/logger"
5+
"dbm-services/mysql/db-tools/dbactuator/internal/subcmd"
6+
7+
"dbm-services/mysql/db-tools/dbactuator/pkg/components/mysql/grant/import_grants_file"
8+
"dbm-services/mysql/db-tools/dbactuator/pkg/util"
9+
"fmt"
10+
11+
"github.com/spf13/cobra"
12+
)
13+
14+
const ImportGrantsFileCmd = "import-grants-file"
15+
16+
type ImportGrantsFileAct struct {
17+
*subcmd.BaseOptions
18+
Service import_grants_file.ImportGrantsFile
19+
}
20+
21+
func NewImportGrantsFileCommand() *cobra.Command {
22+
act := ImportGrantsFileAct{
23+
BaseOptions: subcmd.GBaseOptions,
24+
}
25+
cmd := &cobra.Command{
26+
Use: ImportGrantsFileCmd,
27+
Short: "clone db grants",
28+
Example: fmt.Sprintf(
29+
`dbactuator mysql %s %s %s`,
30+
ImportGrantsFileCmd, subcmd.CmdBaseExampleStr, subcmd.ToPrettyJson(act.Service.Example()),
31+
),
32+
Run: func(cmd *cobra.Command, args []string) {
33+
util.CheckErr(act.Validate())
34+
util.CheckErr(act.Init())
35+
util.CheckErr(act.Run())
36+
},
37+
}
38+
return cmd
39+
}
40+
41+
func (c *ImportGrantsFileAct) Validate() error {
42+
return c.BaseOptions.Validate()
43+
}
44+
45+
func (c *ImportGrantsFileAct) Init() error {
46+
if err := c.Deserialize(&c.Service.Params); err != nil {
47+
logger.Error("DeserializeAndValidate err %s", err.Error())
48+
return err
49+
}
50+
c.Service.GeneralParam = subcmd.GeneralRuntimeParam
51+
logger.Info("extend params: %s", c.Service.Params)
52+
return nil
53+
}
54+
55+
func (c *ImportGrantsFileAct) Run() (err error) {
56+
defer util.LoggerErrorStack(logger.Error, err)
57+
steps := subcmd.Steps{
58+
{
59+
FunName: "初始化",
60+
Func: c.Service.Init,
61+
},
62+
{
63+
FunName: "修改原始权限",
64+
Func: c.Service.ModifyPrivs,
65+
},
66+
{
67+
FunName: "写入最终权限文件",
68+
Func: c.Service.GenerateFinalFile,
69+
},
70+
{
71+
FunName: "执行导入",
72+
Func: c.Service.Import,
73+
},
74+
}
75+
76+
if err = steps.Run(); err != nil {
77+
return err
78+
}
79+
80+
logger.Info("import grants file completed")
81+
return nil
82+
}

dbm-services/mysql/db-tools/dbactuator/internal/subcmd/mysqlcmd/mysqlcmd.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ func NewMysqlCommand() *cobra.Command {
7676
v2.NewGenPeripheralToolsConfigCommand(),
7777
v2.NewReloadPeripheralToolsConfigCommand(),
7878
v2.NewInitCommonConfigCommand(),
79+
NewImportGrantsFileCommand(),
7980
},
8081
},
8182
{

dbm-services/mysql/db-tools/dbactuator/internal/subcmd/proxycmd/clone_proxy_user.go

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,16 @@
66
package proxycmd
77

88
import (
9-
"fmt"
10-
119
"dbm-services/common/go-pubpkg/logger"
1210
"dbm-services/mysql/db-tools/dbactuator/internal/subcmd"
1311
"dbm-services/mysql/db-tools/dbactuator/pkg/components/mysql_proxy"
1412
"dbm-services/mysql/db-tools/dbactuator/pkg/util"
13+
"fmt"
1514

1615
"github.com/spf13/cobra"
1716
)
1817

1918
// CloneProxyUserAct TODO
20-
// extend payload
21-
/*
22-
{
23-
"source_proxy_host": "1.1.1.1",
24-
"source_proxy_port" 10000,
25-
"target_proxy_host" "2.2.2.2",
26-
"target_proxy_port" 10000
27-
28-
}
29-
*/
3019
type CloneProxyUserAct struct {
3120
*subcmd.BaseOptions
3221
Service mysql_proxy.CloneProxyUserComp
@@ -74,10 +63,6 @@ func (c *CloneProxyUserAct) Validate() (err error) {
7463
// Run TODO
7564
func (c *CloneProxyUserAct) Run() (err error) {
7665
steps := subcmd.Steps{
77-
{
78-
FunName: "初始化",
79-
Func: c.Service.Init,
80-
},
8166
{
8267
FunName: "Clone proxy user",
8368
Func: c.Service.CloneProxyUser,

dbm-services/mysql/db-tools/dbactuator/pkg/components/mysql/grant/clone.go

Lines changed: 0 additions & 1 deletion
This file was deleted.

dbm-services/mysql/db-tools/dbactuator/pkg/components/mysql/grant/clone_instance_priv.go

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package import_grants_file
2+
3+
import (
4+
"dbm-services/mysql/db-tools/dbactuator/pkg/components"
5+
"dbm-services/mysql/db-tools/dbactuator/pkg/components/mysql/common"
6+
)
7+
8+
func (c *ImportGrantsFile) Example() interface{} {
9+
return ImportGrantsFile{
10+
GeneralParam: &components.GeneralParam{
11+
RuntimeAccountParam: components.RuntimeAccountParam{
12+
MySQLAccountParam: common.AccountAdminExample,
13+
},
14+
},
15+
Params: &ImportGrantsFileParam{
16+
SourceIp: "1.1.1.1",
17+
SourceVersion: "5.7.20-tmysql-3.4.4-log",
18+
DestAddress: "2.2.2.2:20000",
19+
Filename: "source-grants.priv",
20+
SystemUsers: []string{"u1", "u2", "u3"},
21+
SourceMachineType: "backend",
22+
DestMachineType: "backend",
23+
},
24+
}
25+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package import_grants_file
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
"strings"
8+
)
9+
10+
func (c *ImportGrantsFile) GenerateFinalFile() (err error) {
11+
c.finalFilename = fmt.Sprintf("final-%s", c.Params.Filename)
12+
fp := filepath.Join(c.workDir, c.finalFilename)
13+
f, err := os.OpenFile(fp, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.ModePerm)
14+
if err != nil {
15+
return err
16+
}
17+
defer func() {
18+
_ = f.Close()
19+
}()
20+
21+
c.lineCount = 0
22+
for _, l := range c.finalListeners {
23+
_, err = f.WriteString(strings.Trim(l.ToSql(), ";") + ";\n")
24+
if err != nil {
25+
return err
26+
}
27+
c.lineCount++
28+
}
29+
30+
_, err = f.WriteString("FLUSH PRIVILEGES;\n")
31+
if err != nil {
32+
return err
33+
}
34+
c.lineCount++
35+
36+
// 写完文件把内存释放掉
37+
c.finalListeners = nil
38+
return nil
39+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package import_grants_file
2+
3+
import (
4+
"bufio"
5+
"context"
6+
"dbm-services/common/go-pubpkg/logger"
7+
"os"
8+
"path/filepath"
9+
"time"
10+
)
11+
12+
func (c *ImportGrantsFile) Import() (err error) {
13+
fp := filepath.Join(c.workDir, c.finalFilename)
14+
f, err := os.OpenFile(fp, os.O_RDONLY, os.ModePerm)
15+
if err != nil {
16+
return err
17+
}
18+
defer func() {
19+
_ = f.Close()
20+
}()
21+
22+
conn, err := c.db.Db.Conn(context.Background())
23+
if err != nil {
24+
return err
25+
}
26+
_, err = conn.ExecContext(
27+
context.Background(),
28+
"SET sql_log_bin=0",
29+
)
30+
if err != nil {
31+
return err
32+
}
33+
defer func() {
34+
_ = conn.Close()
35+
}()
36+
logger.Info("disable binlog")
37+
38+
doneCh := make(chan struct{})
39+
defer close(doneCh)
40+
counter := 1
41+
// 把日志放到异步打印, 要不然太多了
42+
go func() {
43+
ticker := time.NewTicker(1 * time.Second)
44+
defer ticker.Stop()
45+
for {
46+
select {
47+
case <-ticker.C:
48+
logger.Info("%d/%d imported", counter, c.lineCount)
49+
case <-doneCh:
50+
logger.Info("%d/%d imported", counter, c.lineCount)
51+
return
52+
}
53+
}
54+
}()
55+
56+
scanner := bufio.NewScanner(f)
57+
for scanner.Scan() {
58+
line := scanner.Text()
59+
_, err = conn.ExecContext(context.Background(), line)
60+
if err != nil {
61+
logger.Error("import failed: %s", line, err.Error())
62+
return err
63+
}
64+
counter++
65+
}
66+
if err := scanner.Err(); err != nil {
67+
return err
68+
}
69+
70+
doneCh <- struct{}{}
71+
72+
return nil
73+
}

0 commit comments

Comments
 (0)