Skip to content

Commit 286d5be

Browse files
OMG-ByiSecloud
authored andcommitted
fix(redis): redis主从下架获取密码失败 #7988
1 parent aef65f0 commit 286d5be

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed

dbm-services/redis/db-tools/dbactuator/models/myredis/myredis.go

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,7 @@ func GetRedisLoccalConfFile(port int) (confFile string, err error) {
3030
return
3131
}
3232

33-
// GetRedisPasswdFromConfFile (从配置文件中)获取本地redis实例密码
34-
func GetRedisPasswdFromConfFile(port int) (password string, err error) {
35-
confFile, err := GetRedisLoccalConfFile(port)
36-
if err != nil {
37-
err = fmt.Errorf("get redis local config file failed,err:%v,port:%d", err, port)
38-
mylog.Logger.Error(err.Error())
39-
return
40-
}
33+
func GetRedisPasswd(confFile string) (password string, err error) {
4134
cmd01 := fmt.Sprintf(`grep -E '^requirepass' %s|awk '{print $2}'|head -1`, confFile)
4235
password, err = util.RunBashCmd(cmd01, "", nil, 10*time.Second)
4336
if err != nil {
@@ -48,6 +41,57 @@ func GetRedisPasswdFromConfFile(port int) (password string, err error) {
4841
return
4942
}
5043

44+
// GetRedisPasswdFromConfFile (从配置文件中)获取本地redis实例密码
45+
func GetRedisPasswdFromConfFile(port int) (password string, err error) {
46+
// config rewrite的时候,重写的东西是写进redis.conf, 原来的实现方式如果存在instance.conf,就会获取到老的配置。
47+
// 默认先从redis.conf中获取密码
48+
dataDir := consts.GetRedisDataDir()
49+
redisConf := filepath.Join(dataDir, "redis", strconv.Itoa(port), "redis.conf")
50+
instConf := filepath.Join(dataDir, "redis", strconv.Itoa(port), "instance.conf")
51+
if util.FileExists(redisConf) {
52+
password, err = GetRedisPasswd(redisConf)
53+
if err == nil && password != "" {
54+
mylog.Logger.Info(fmt.Sprintf("get pwd success from redis.conf"))
55+
return
56+
}
57+
}
58+
// 尝试从instance.conf中获取密码
59+
if util.FileExists(instConf) {
60+
password, err = GetRedisPasswd(instConf)
61+
if err == nil && password != "" {
62+
mylog.Logger.Info(fmt.Sprintf("get pwd success from instance.conf"))
63+
return
64+
}
65+
}
66+
// 文件都不存在
67+
if !util.FileExists(instConf) && !util.FileExists(redisConf) {
68+
err = fmt.Errorf("[%s,%s] not exists", instConf, redisConf)
69+
mylog.Logger.Error(err.Error())
70+
return
71+
}
72+
// 有可能存在redis实例密码确实为空的情况。。
73+
mylog.Logger.Info(fmt.Sprintf("get pwd success. but pwd is empty"))
74+
return
75+
}
76+
77+
// GetRedisPasswdFromConfFile (从配置文件中)获取本地redis实例密码
78+
//func GetRedisPasswdFromConfFile(port int) (password string, err error) {
79+
// confFile, err := GetRedisLoccalConfFile(port)
80+
// if err != nil {
81+
// err = fmt.Errorf("get redis local config file failed,err:%v,port:%d", err, port)
82+
// mylog.Logger.Error(err.Error())
83+
// return
84+
// }
85+
// cmd01 := fmt.Sprintf(`grep -E '^requirepass' %s|awk '{print $2}'|head -1`, confFile)
86+
// password, err = util.RunBashCmd(cmd01, "", nil, 10*time.Second)
87+
// if err != nil {
88+
// return
89+
// }
90+
// password = strings.TrimPrefix(password, "\"")
91+
// password = strings.TrimSuffix(password, "\"")
92+
// return
93+
//}
94+
5195
// GetTwemproxyLocalConfFile 本地获取twemproxy实例配置文件
5296
func GetTwemproxyLocalConfFile(port int) (confFile string, err error) {
5397
psCmd := fmt.Sprintf(`

dbm-services/redis/db-tools/dbactuator/pkg/atomjobs/atomsys/redis_capturer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func (job *RedisCapturer) Monitor(port int) {
131131
var err error
132132
running, err := job.IsRedisRunning(port)
133133
if err != nil || !running {
134-
job.errChan <- fmt.Errorf("port[%d] is not running", port)
134+
// 如果端口运行异常,那没必要去做抓包,直接返回
135135
return
136136
}
137137

0 commit comments

Comments
 (0)