Skip to content

Commit

Permalink
Refactored code and added command line parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
darxkies committed Dec 14, 2018
1 parent a1390d1 commit ba4efef
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 67 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
virtual-ip
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
[![Build Status](https://travis-ci.org/darxkies/virtual-ip.svg?branch=master)](https://travis-ci.org/darxkies/virtual-ip)
[![Go Report Card](https://goreportcard.com/badge/github.com/darxkies/virtual-ip)](https://goreportcard.com/report/github.com/darxkies/virtual-ip)
[![GitHub release](https://img.shields.io/github/release/darxkies/virtual-ip.svg)](https://github.com/darxkies/virtual-ip/releases/latest)
[![GitHub](https://img.shields.io/github/license/darxkies/virtual-ip.svg)]
![GitHub](https://img.shields.io/github/license/darxkies/virtual-ip.svg)

48 changes: 48 additions & 0 deletions cmd/virtual-ip/main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,60 @@
package main

import (
"flag"
"fmt"
"os"
"os/signal"
"strings"
"time"

"github.com/darxkies/virtual-ip/pkg"
"github.com/darxkies/virtual-ip/version"
log "github.com/sirupsen/logrus"
)

func main() {
fmt.Printf("Version: %s\n", version.Version)

id := flag.String("id", "vip", "ID of this node")
bind := flag.String("bind", "0.0.0.0", "RAFT bind addreess")
virtualIP := flag.String("virtual-ip", "192.168.0.25", "Virtual/Floating IP")
timeout := flag.Int("timeout", 10, "Shell command timeout")
peersList := flag.String("peers", "", "Peers as a comma separated list of peer-id=peer-address:peer-port including the id and the bind of this instance")
_interface := flag.String("interface", "enp3s0:1", "Network interface")
flag.Parse()

commandRunner := pkg.NewShellCommandRunner(time.Duration(*timeout))

peers := pkg.Peers{}

if len(*peersList) > 0 {
for _, peer := range strings.Split(*peersList, ",") {
peerTokens := strings.Split(peer, "=")

if len(peerTokens) != 2 {
log.WithFields(log.Fields{"peer": peer}).Error("Peers malformated")

os.Exit(-1)
}

peers[peerTokens[0]] = peerTokens[1]
}
}

logger := pkg.Logger{}

vipManager := pkg.NewVIPManager(*id, *bind, *virtualIP, peers, logger, *_interface, commandRunner)
if error := vipManager.Start(); error != nil {
log.WithFields(log.Fields{"error": error}).Error("Start failed")

os.Exit(-1)
}

signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt)

<-signalChan

vipManager.Stop()
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ require (
github.com/fsnotify/fsnotify v1.4.7 // indirect
github.com/hashicorp/go-immutable-radix v1.0.0 // indirect
github.com/hashicorp/go-msgpack v0.0.0-20150518234257-fa3f63826f7c // indirect
github.com/hashicorp/raft v1.0.0 // indirect
github.com/hashicorp/raft v1.0.0
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/kr/pty v1.1.3 // indirect
github.com/ogier/pflag v0.0.1 // indirect
github.com/sirupsen/logrus v1.2.0 // indirect
github.com/sirupsen/logrus v1.2.0
golang.org/x/sys v0.0.0-20181213200352-4d1cda033e06 // indirect
)
42 changes: 42 additions & 0 deletions pkg/command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package pkg

import (
"context"
"fmt"
"os/exec"
"time"

log "github.com/sirupsen/logrus"
)

type CommandRunner interface {
Run(command string) error
}

type ShellCommandRunner struct {
timeout time.Duration
}

func NewShellCommandRunner(timeout time.Duration) ShellCommandRunner {
return ShellCommandRunner{timeout: timeout}
}

func (runner ShellCommandRunner) Run(command string) error {
_context, cancel := context.WithTimeout(context.Background(), runner.timeout*time.Second)
defer cancel()

log.WithFields(log.Fields{"command": command}).Debug("Command started")

cmd := exec.CommandContext(_context, "sh", "-c", command)

output, error := cmd.CombinedOutput()
if error != nil {
log.WithFields(log.Fields{"command": command, "error": error}).Debug("Command failed")

return fmt.Errorf("Command '%s' failed with error '%s' (Output: %s)", command, error, output)
}

log.WithFields(log.Fields{"command": command, "output": string(output)}).Debug("Command ended")

return nil
}
22 changes: 22 additions & 0 deletions pkg/fsm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package pkg

import (
"io"

"github.com/hashicorp/raft"
)

type FSM struct {
}

func (fsm FSM) Apply(log *raft.Log) interface{} {
return nil
}

func (fsm FSM) Restore(snap io.ReadCloser) error {
return nil
}

func (fsm FSM) Snapshot() (raft.FSMSnapshot, error) {
return Snapshot{}, nil
}
8 changes: 8 additions & 0 deletions pkg/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package pkg

type Logger struct {
}

func (logger Logger) Write(data []byte) (count int, error error) {
return len(data), nil
}
86 changes: 22 additions & 64 deletions pkg/vip_manager.go → pkg/manager.go
Original file line number Diff line number Diff line change
@@ -1,96 +1,50 @@
package servers
package pkg

import (
"fmt"
"io"
"net"
"time"

"github.com/hashicorp/raft"
log "github.com/sirupsen/logrus"
)

type Peer struct {
ID string
Bind string
}

type Peers map[string]string

type Logger struct {
}

func (logger Logger) Write(data []byte) (count int, error error) {
return len(data), nil
}

type FSM struct {
}

func (fsm FSM) Apply(log *raft.Log) interface{} {
return nil
}

func (fsm FSM) Restore(snap io.ReadCloser) error {
return nil
}

func (fsm FSM) Snapshot() (raft.FSMSnapshot, error) {
return Snapshot{}, nil
}

type Snapshot struct {
}

func (snapshot Snapshot) Persist(sink raft.SnapshotSink) error {
return nil
}

func (snapshot Snapshot) Release() {
}

type VIPManager struct {
_type string
id string
bind string
virtualIP string
fsm FSM
peers Peers
logger Logger
_interface string
stop chan bool
id string
bind string
virtualIP string
fsm FSM
peers Peers
logger Logger
_interface string
stop chan bool
commandRunner CommandRunner
}

func NewVIPManager(_type, id, bind string, virtualIP string, peers Peers, logger Logger, _interface string) *VIPManager {
return &VIPManager{_type: _type, id: id, peers: peers, bind: bind, virtualIP: virtualIP, fsm: FSM{}, logger: logger, _interface: _interface}
}

func (manager *VIPManager) Name() string {
return "vip-manager-" + manager._type
func NewVIPManager(id, bind string, virtualIP string, peers Peers, logger Logger, _interface string, commandRunner CommandRunner) *VIPManager {
return &VIPManager{id: id, peers: peers, bind: bind, virtualIP: virtualIP, fsm: FSM{}, logger: logger, _interface: _interface, commandRunner: commandRunner}
}

func (manager *VIPManager) updateNetworkConfiguration(action string) error {
command := fmt.Sprintf("ip addr %s %s/32 dev %s", action, manager.virtualIP, manager._interface)

/*
if error := utils.RunCommand(command); error != nil {
log.WithFields(log.Fields{"action": action, "name": manager.Name(), "error": error}).Error("Network update failed")
if error := manager.commandRunner.Run(command); error != nil {
log.WithFields(log.Fields{"action": action, "error": error}).Error("Network update failed")

return error
}
*/
return error
}

return nil
}

func (manager *VIPManager) addIP() error {
log.WithFields(log.Fields{"name": manager.Name()}).Info("Add virtual ip")
log.Info("Add virtual ip")

return manager.updateNetworkConfiguration("add")
}

func (manager *VIPManager) deleteIP() error {
log.WithFields(log.Fields{"name": manager.Name()}).Info("Delete virtual ip")
log.Info("Delete virtual ip")

return manager.updateNetworkConfiguration("delete")
}
Expand Down Expand Up @@ -156,9 +110,13 @@ func (manager *VIPManager) Start() error {
}
}()

log.Info("Started")

return nil
}

func (manager *VIPManager) Stop() {
close(manager.stop)

log.Info("Stopped")
}
8 changes: 8 additions & 0 deletions pkg/peers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package pkg

type Peer struct {
ID string
Bind string
}

type Peers map[string]string
13 changes: 13 additions & 0 deletions pkg/snapshot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package pkg

import "github.com/hashicorp/raft"

type Snapshot struct {
}

func (snapshot Snapshot) Persist(sink raft.SnapshotSink) error {
return nil
}

func (snapshot Snapshot) Release() {
}

0 comments on commit ba4efef

Please sign in to comment.