-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathnode.go
180 lines (147 loc) · 4.54 KB
/
node.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package redisnode
import (
"fmt"
"net"
"os"
"path/filepath"
"strconv"
"github.com/amadeusitgroup/redis-operator/pkg/config"
"github.com/amadeusitgroup/redis-operator/pkg/redis"
"github.com/golang/glog"
)
const (
dataFolder = "/redis-data"
)
// Node struct that represent a RedisNodeWrapper
type Node struct {
config *Config
Addr string
RedisAdmin redis.AdminInterface
}
// NewNode return a instance of a Node
func NewNode(c *Config, admin redis.AdminInterface) *Node {
n := &Node{
config: c,
RedisAdmin: admin,
Addr: net.JoinHostPort(c.Redis.ServerIP, c.Redis.ServerPort),
}
return n
}
// Clear clear possible initialize ressource
func (n *Node) Clear() {
if n.RedisAdmin != nil {
n.RedisAdmin.Close()
}
}
// UpdateNodeConfigFile update the redis config file with node information: ip, port
func (n *Node) UpdateNodeConfigFile() error {
if n.config.Redis.ConfigFileName != config.RedisConfigFileDefault {
if err := n.addSettingInConfigFile("include " + config.RedisConfigFileDefault); err != nil {
return err
}
}
if err := n.addSettingInConfigFile("port " + n.config.Redis.ServerPort); err != nil {
return err
}
if err := n.addSettingInConfigFile("cluster-enabled yes"); err != nil {
return err
}
if n.config.Redis.MaxMemory > 0 {
if err := n.addSettingInConfigFile(fmt.Sprintf("maxmemory %d", n.config.Redis.MaxMemory)); err != nil {
return err
}
}
if n.config.Redis.MaxMemoryPolicy != config.RedisMaxMemoryPolicyDefault {
if err := n.addSettingInConfigFile(fmt.Sprintf("maxmemory-policy %s", n.config.Redis.MaxMemoryPolicy)); err != nil {
return err
}
}
if err := n.addSettingInConfigFile("bind 0.0.0.0"); err != nil {
return err
}
if err := n.addSettingInConfigFile("cluster-config-file /redis-data/node.conf"); err != nil {
return err
}
if err := n.addSettingInConfigFile("dir /redis-data"); err != nil {
return err
}
if err := n.addSettingInConfigFile("cluster-node-timeout " + strconv.Itoa(n.config.Redis.ClusterNodeTimeout)); err != nil {
return err
}
if n.config.Redis.GetRenameCommandsFile() != "" {
if err := n.addSettingInConfigFile("include " + n.config.Redis.GetRenameCommandsFile()); err != nil {
return err
}
}
// Add at the end any configuration file provided as redis-node's arguments.
for _, file := range n.config.Redis.ConfigFiles {
if err := n.addSettingInConfigFile("include " + file); err != nil {
return err
}
}
return nil
}
// addSettingInConfigFile add a line in the redis configuration file
func (n *Node) addSettingInConfigFile(line string) error {
f, err := os.OpenFile(n.config.Redis.ConfigFileName, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
return fmt.Errorf("unable to set '%s' in config file, openfile error %s err:%v", line, n.config.Redis.ConfigFileName, err)
}
defer f.Close()
_, err = f.WriteString(line + "\n")
if err != nil {
return fmt.Errorf("unable to set '%s' in config file, err:%v", line, err)
}
return nil
}
// InitRedisCluster used to init a redis cluster with the current node
func (n *Node) InitRedisCluster(addr string) error {
glog.Info("InitRedis Cluster... starting")
err := n.RedisAdmin.InitRedisCluster(addr)
glog.Info("InitRedis Cluster... done")
return err
}
// AttachNodeToCluster used to attach the current node to a redis cluster
func (n *Node) AttachNodeToCluster(addr string) error {
glog.Info("AttachNodeToCluster... starting")
return n.RedisAdmin.AttachNodeToCluster(addr)
}
// ForgetNode used to remove a node for a cluster
func (n *Node) ForgetNode() error {
glog.Info("ForgetNode... starting")
return n.RedisAdmin.ForgetNodeByAddr(n.Addr)
}
// StartFailover start Failover if needed
func (n *Node) StartFailover() error {
glog.Info("StartFailover... starting")
return n.RedisAdmin.StartFailover(n.Addr)
}
// ClearDataFolder completely erase all files in the /data folder
func (n *Node) ClearDataFolder() error {
return clearFolder(dataFolder)
}
// ClearFolder remover all files and folder in a given folder
func clearFolder(folder string) error {
glog.Infof("Clearing '%s' folder... ", folder)
d, err := os.Open(folder)
if err != nil {
glog.Infof("Cannot access folder %s: %v", folder, err)
return err
}
defer d.Close()
names, err := d.Readdirnames(-1)
if err != nil {
glog.Infof("Cannot read files in %s: %v", folder, err)
return err
}
for _, name := range names {
file := filepath.Join(folder, name)
glog.V(2).Infof("Removing %s", file)
err = os.RemoveAll(file)
if err != nil {
glog.Errorf("Error while removing %s: %v", file, err)
return err
}
}
return nil
}