-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add command line interface to caplance and config file parsing
- Loading branch information
Showing
10 changed files
with
188 additions
and
93 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 |
---|---|---|
@@ -1,21 +1,7 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"os" | ||
|
||
"github.com/pwpon500/caplance/pkg/setups" | ||
) | ||
|
||
var ( | ||
setupName = flag.String("setup", "", "name of test setup to create") | ||
teardownFlag = flag.Bool("teardown", false, "run teardown instead of build setup") | ||
) | ||
import "github.com/pwpon500/caplance/cmd/caplance/cmd" | ||
|
||
func main() { | ||
flag.Parse() | ||
if *setupName != "" { | ||
setups.RunSetup(*setupName, *teardownFlag) | ||
os.Exit(0) | ||
} | ||
cmd.Execute() | ||
} |
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 cmd | ||
|
||
import ( | ||
"log" | ||
"net" | ||
|
||
"github.com/pwpon500/caplance/internal/client" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func init() { | ||
rootCmd.AddCommand(clientCmd) | ||
} | ||
|
||
var clientCmd = &cobra.Command{ | ||
Use: "client", | ||
Short: "Start caplance in client mode", | ||
Long: `Mark this host as a backend, allowing packets to be forwarded to it from the load balancer.`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
readConfig() | ||
vip := net.ParseIP(conf.VIP) | ||
if vip == nil { | ||
log.Fatal("Could not parse vip: " + conf.VIP) | ||
} | ||
dataIP := net.ParseIP(conf.Client.DataIP) | ||
if dataIP == nil { | ||
log.Fatal("Could not parse data ip: " + conf.Client.DataIP) | ||
} | ||
c := client.NewClient(vip, dataIP) | ||
connectIP := net.ParseIP(conf.Client.ConnectIP) | ||
if connectIP == nil { | ||
connectIP = net.ParseIP(conf.Server.MngIP) | ||
if connectIP == nil { | ||
log.Fatalf("Could not parse Client.ConnectIP (%v) or Server.MngIP (%v)\n", conf.Client.ConnectIP, conf.Server.MngIP) | ||
} | ||
} | ||
err := c.Start(connectIP) | ||
if err != nil { | ||
log.Fatal("Failed to start with error: " + err.Error()) | ||
} | ||
}, | ||
} |
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,82 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
type config struct { | ||
Client struct { | ||
ConnectIP string | ||
DataIP string | ||
} | ||
Server struct { | ||
MngIP string | ||
BackendCapacity int | ||
} | ||
VIP string | ||
Test bool | ||
|
||
HealthRate int | ||
RegisterTimeout int | ||
ReadTimeout int | ||
WriteTimeout int | ||
|
||
Sockaddr string | ||
} | ||
|
||
var ( | ||
conf *config | ||
configLocation string | ||
) | ||
|
||
// Execute starts the cobra chain | ||
func Execute() { | ||
if err := rootCmd.Execute(); err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func init() { | ||
viper.AddConfigPath(".") | ||
viper.AddConfigPath("/etc/caplance/") | ||
viper.SetConfigName("caplance.cfg") | ||
|
||
viper.SetDefault("Test", false) | ||
viper.SetDefault("HealthRate", 20) | ||
viper.SetDefault("RegisterTimeout", 10) | ||
viper.SetDefault("ReadTimeout", 30) | ||
viper.SetDefault("WriteTimeout", 10) | ||
viper.SetDefault("Sockaddr", "/var/run/caplance.sock") | ||
|
||
rootCmd.PersistentFlags().StringVarP(&configLocation, "file", "f", "", "choose a non-standard config location") | ||
|
||
} | ||
|
||
func readConfig() { | ||
if configLocation != "" { | ||
viper.SetConfigFile(configLocation) | ||
} | ||
|
||
err := viper.ReadInConfig() | ||
if err != nil { | ||
log.Fatal("Failed to read in config: " + err.Error()) | ||
} | ||
|
||
conf = &config{} | ||
err = viper.Unmarshal(conf) | ||
if err != nil { | ||
log.Fatal("Failed to unmarshal config into struct: " + err.Error()) | ||
} | ||
} | ||
|
||
var rootCmd = &cobra.Command{ | ||
Use: "caplancectl", | ||
Short: "caplancectl is the controller for caplance", | ||
Long: `For more information, visit https://github.com/Pwpon500/caplance`, | ||
} |
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,39 @@ | ||
package cmd | ||
|
||
import ( | ||
"log" | ||
"net" | ||
"strconv" | ||
|
||
"github.com/pwpon500/caplance/internal/balancer" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func init() { | ||
rootCmd.AddCommand(serverCmd) | ||
} | ||
|
||
var serverCmd = &cobra.Command{ | ||
Use: "server", | ||
Short: "Start caplance in server mode", | ||
Long: `Mark this host as the load balancer, forwarding packets to a set of backends.`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
readConfig() | ||
vip := net.ParseIP(conf.VIP) | ||
if vip == nil { | ||
log.Fatal("Could not parse vip: " + conf.VIP) | ||
} | ||
mngIP := net.ParseIP(conf.Server.MngIP) | ||
if mngIP == nil { | ||
log.Fatal("Could not parse management ip: " + conf.Server.MngIP) | ||
} | ||
if conf.Server.BackendCapacity <= 0 { | ||
log.Fatal("Backend capacity " + strconv.Itoa(conf.Server.BackendCapacity) + " must be postive.") | ||
} | ||
b, err := balancer.New(vip, mngIP, conf.Server.BackendCapacity) | ||
if err != nil { | ||
log.Fatal("Error when creating balancer: " + err.Error()) | ||
} | ||
b.Start() | ||
}, | ||
} |
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 @@ | ||
vip: 10.0.0.50 | ||
|
||
client: | ||
dataIP: 10.0.0.2 | ||
|
||
server: | ||
mngIP: 10.0.0.1 | ||
backendCapacity: 53 |