@@ -30,14 +30,7 @@ func GetRedisLoccalConfFile(port int) (confFile string, err error) {
30
30
return
31
31
}
32
32
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 ) {
41
34
cmd01 := fmt .Sprintf (`grep -E '^requirepass' %s|awk '{print $2}'|head -1` , confFile )
42
35
password , err = util .RunBashCmd (cmd01 , "" , nil , 10 * time .Second )
43
36
if err != nil {
@@ -48,6 +41,57 @@ func GetRedisPasswdFromConfFile(port int) (password string, err error) {
48
41
return
49
42
}
50
43
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
+
51
95
// GetTwemproxyLocalConfFile 本地获取twemproxy实例配置文件
52
96
func GetTwemproxyLocalConfFile (port int ) (confFile string , err error ) {
53
97
psCmd := fmt .Sprintf (`
0 commit comments