Skip to content

Commit

Permalink
Use config file (the first argument when redis-go-to-master is run) i…
Browse files Browse the repository at this point in the history
…nstead of CLI params
  • Loading branch information
Chupaka committed Jan 13, 2023
1 parent 29956ef commit b4619f0
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 20 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,14 @@ Small command-line utility (based on the ideas from <https://github.com/flant/re
Usage
-----

`./redis-go-to-master --ports 6379 --hosts redis1,redis2 --auth MyAuthKey`
Create a config file in YAML:

ports:
- 6379
nodes:
- redis1
- redis2
# auth: "Your-Redis-Auth-Key"

Run redis-go-to-master:
`./redis-go-to-master /path/to/config.yaml`
59 changes: 41 additions & 18 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ package main

import (
"bytes"
"flag"
"fmt"
"io"
"log"
"net"
"os"
"path/filepath"
"strings"
"sync"
"sync/atomic"
"time"

systemdnotify "github.com/iguanesolutions/go-systemd/v5/notify"
"gopkg.in/yaml.v2"
)

type RedisPort struct {
Expand All @@ -26,12 +28,14 @@ type Stats struct {
pipesActive uint32
}

var (
nodes []string
type ConfigStruct struct {
Ports []string `yaml:"ports"`
Nodes []string `yaml:"nodes"`
Auth string `yaml:"auth"`
}

configPorts = flag.String("ports", "", "comma-eparated list of listening ports")
configNodes = flag.String("nodes", "", "comma-separated list of redis nodes hostnames")
configAuth = flag.String("auth", "", "redis auth string")
var (
config ConfigStruct

globalStats Stats
)
Expand All @@ -41,23 +45,42 @@ func main() {
log.SetFlags(log.Flags() &^ (log.Ldate | log.Ltime))
}

flag.Parse()
if len(os.Args) != 2 {
log.Fatalln("A single parameter is expected: config file name")
}

fn, err := filepath.Abs(os.Args[1])
if err != nil {
log.Fatalf("Can't get config file absolute path: %s\n", err)
}

log.Printf("Using onfiguration file %s\n", fn)

f, err := os.Open(fn)
if err != nil {
log.Fatalf("Can't open config file: %s\n", err)
}

err = yaml.NewDecoder(f).Decode(&config)
if err != nil {
log.Fatalf("Can't read config: %s\n", err)
}

f.Close()

if *configPorts == "" {
if len(config.Ports) < 1 {
log.Fatalln("Must specify at least one listening port!")
}

if *configNodes == "" {
if len(config.Nodes) < 1 {
log.Fatalln("Must specify at least one redis node!")
}

nodes = strings.Split(*configNodes, ",")
log.Printf("Watching the following redis servers: %s", strings.Join(nodes, ", "))
log.Printf("Watching the following redis servers: %s", strings.Join(config.Nodes, ", "))

ports := strings.Split(*configPorts, ",")
log.Printf("Serving the following ports: %s", strings.Join(ports, ", "))
log.Printf("Serving the following ports: %s", strings.Join(config.Ports, ", "))

for _, port := range ports {
for _, port := range config.Ports {
go ServePort(port)
}

Expand Down Expand Up @@ -167,7 +190,7 @@ func pipe(r io.Reader, w io.WriteCloser) {
}

func getMasterAddr(port string) *net.TCPAddr {
for _, node := range nodes {
for _, node := range config.Nodes {
d := net.Dialer{Timeout: 1 * time.Second}
conn, err := d.Dial("tcp", node+":"+port)
if err != nil {
Expand All @@ -177,8 +200,8 @@ func getMasterAddr(port string) *net.TCPAddr {

defer conn.Close()

if *configAuth != "" {
conn.Write([]byte(fmt.Sprintf("AUTH %s\r\ninfo replication\r\n", *configAuth)))
if config.Auth != "" {
conn.Write([]byte(fmt.Sprintf("AUTH %s\r\ninfo replication\r\n", config.Auth)))
} else {
conn.Write([]byte("info replication\r\n"))
}
Expand All @@ -187,7 +210,7 @@ func getMasterAddr(port string) *net.TCPAddr {

l, err := conn.Read(b)
if err != nil {
log.Fatal(err)
log.Printf("Can't read Redis response: %s\n", err)
}

if bytes.Contains(b[:l], []byte("role:master")) {
Expand Down
2 changes: 1 addition & 1 deletion redis-go-to-master.service
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ After=multi-user.target

[Service]
Type=notify
ExecStart=/usr/bin/redis-go-to-master /etc/redis-go-to-master.conf
ExecStart=/usr/bin/redis-go-to-master /etc/redis-go-to-master.yaml
Restart=on-abnormal

[Install]
Expand Down

0 comments on commit b4619f0

Please sign in to comment.