-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored code and added command line parameters
- Loading branch information
Showing
10 changed files
with
167 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
virtual-ip |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
} |