small pixel drawing of a pufferfish cascade

Implement all agent flags
Jes Olson j3s@c3f.net
Mon, 20 Feb 2023 17:48:08 -0800
commit

d5f6f0e1717c95843e148cc6df98d08e9747ad49

parent

d1fd7dafe17d7c5df8b9cd8d5d6a929e7949643a

2 files changed, 57 insertions(+), 35 deletions(-)

jump to
M agent/config.goagent/config.go

@@ -5,8 +5,11 @@ "net"

"os" ) -const DefaultSerfPort int = 8301 -const DefaultAPIPort int = 8500 +const ( + DefaultAPIPort int = 8500 + DefaultDNSPort int = 8600 + DefaultSerfPort int = 8301 +) func DefaultConfig() *Config { hostname, err := os.Hostname()

@@ -16,16 +19,18 @@ }

// TODO: figure out how to default the listeners cfg := Config{} + cfg.APIBindAddr = &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: DefaultAPIPort} + cfg.DNSBindAddr = &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: DefaultDNSPort} cfg.SerfBindAddr = &net.TCPAddr{IP: net.ParseIP("0.0.0.0"), Port: DefaultSerfPort} - cfg.APIBindAddr = &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: DefaultAPIPort} cfg.NodeName = hostname return &cfg } type Config struct { + APIBindAddr *net.TCPAddr + DNSBindAddr *net.TCPAddr SerfBindAddr *net.TCPAddr - APIBindAddr *net.TCPAddr NodeName string StartJoin []string }
M command/agent/agent.gocommand/agent/agent.go

@@ -25,40 +25,46 @@ this command starts the cascade agent, which is responsible

for basically everything, including service registration, health checking, cluster membership, and hosting the API. -common options: +options: + -bind-api=<addr> + address the http api binds to (default = 127.0.0.1:8500) - -node=<name> - name of this node, must be globally unique (default = hostname) + -bind-dns=<addr> + address the DNS server binds to (default = 127.0.0.1:8600) -bind options: + -bind-serf=<addr> + address the serf agent binds to (default = 0.0.0.0:8301) - -api-bind=<addr> - address the http api binds to (default = 127.0.0.1:8500) - !!do not expose the api publicly!! + -join=<addrs> + comma-separated address of agents to join at start time (default = nil) - -dns-bind=<addr> - address the DNS resolver to (default = 127.0.0.1:8600) - !!do not expose the api publicly!! - - -serf-bind=<addr> - address the serf gossip cluster binds to (default = 0.0.0.0:8301) + -node=<name> + name of this node, must be globally unique (default = hostname) ` -var ( - apiBindAddr string - serfBindAddr string -) +type Flags struct { + bindAPI string + bindDNS string + bindSerf string + join string + node string +} + +var agentFlags Flags func Run(args []string) { flags := flag.NewFlagSet("agent", flag.ContinueOnError) flags.Usage = func() { fmt.Printf(usage) } - flags.StringVar(&serfBindAddr, "serf-bind", "", "") + flags.StringVar(&agentFlags.bindAPI, "bind-api", "", "") + flags.StringVar(&agentFlags.bindDNS, "bind-dns", "", "") + flags.StringVar(&agentFlags.bindSerf, "bind-serf", "", "") + flags.StringVar(&agentFlags.join, "join", "", "") + flags.StringVar(&agentFlags.node, "node", "", "") if err := flags.Parse(args); err != nil { - fmt.Println(err) os.Exit(1) } - config, err := configureAgent() + config, err := getAgentConfig() if err != nil { fmt.Println(err) os.Exit(1)

@@ -146,22 +152,33 @@

return nil } -func configureAgent() (*agent.Config, error) { +// getAgentConfig takes a default agent config and modifies it based +// on user specified flags. It also does some a little input validation. +func getAgentConfig() (*agent.Config, error) { config := agent.DefaultConfig() - // CASCADE_BIND=192.168.0.15:12345 - if os.Getenv("CASCADE_BIND") != "" { - err := parseFlagAddress(os.Getenv("CASCADE_BIND"), config.SerfBindAddr) - if err != nil { + if agentFlags.bindAPI != "" { + if err := parseFlagAddress(agentFlags.bindAPI, config.APIBindAddr); err != nil { + return nil, err + } + } + if agentFlags.bindDNS != "" { + if err := parseFlagAddress(agentFlags.bindDNS, config.DNSBindAddr); err != nil { + return nil, err + } + } + if agentFlags.bindSerf != "" { + if err := parseFlagAddress(agentFlags.bindSerf, config.SerfBindAddr); err != nil { return nil, err } } - // CASCADE_JOIN=127.0.0.1,127.0.0.5 - if os.Getenv("CASCADE_JOIN") != "" { - config.StartJoin = strings.Split(os.Getenv("CASCADE_JOIN"), ",") + + if agentFlags.join != "" { + // TODO: moar validation + config.StartJoin = strings.Split(agentFlags.join, ",") } - // CASCADE_NAME=nostromo.j3s.sh - if os.Getenv("CASCADE_NAME") != "" { - config.NodeName = os.Getenv("CASCADE_NAME") + + if agentFlags.node != "" { + config.NodeName = agentFlags.node } return config, nil }