remove command gunk from main()
Jes Olson j3s@c3f.net
Mon, 20 Feb 2023 15:20:32 -0800
3 files changed,
5 insertions(+),
182 deletions(-)
M
agent/config.go
→
agent/config.go
@@ -1,7 +1,6 @@
package agent import ( - "fmt" "net" "os" )@@ -30,27 +29,3 @@ ClientAddr *net.TCPAddr
NodeName string StartJoin []string } - -// BindAddrParts returns the parts of the BindAddr that should be -// used to configure Serf. -func (c *Config) AddrParts(address string) (string, int, error) { - checkAddr := address - -START: - _, _, err := net.SplitHostPort(checkAddr) - if ae, ok := err.(*net.AddrError); ok && ae.Err == "missing port in address" { - checkAddr = fmt.Sprintf("%s:%d", checkAddr, DefaultSerfPort) - goto START - } - if err != nil { - return "", 0, err - } - - // Get the address - addr, err := net.ResolveTCPAddr("tcp", checkAddr) - if err != nil { - return "", 0, err - } - - return addr.IP.String(), addr.Port, nil -}
D
ipaddr/ipaddr.go
@@ -1,34 +0,0 @@
-package ipaddr - -import ( - "log" - "net" - "strconv" - "strings" -) - -// resolveHost will take a single host string and convert it to a list of TCPAddrs -// This will process any port in the input as well as looking up the hostname using -// normal DNS resolution. -func ParseIPPort(IPPort string) *net.TCPAddr { - var addr net.TCPAddr - ip, portStr, err := net.SplitHostPort(IPPort) - if err != nil { - if strings.Contains(err.Error(), "missing port in address") { - ip = IPPort - } else { - log.Printf("error splitting ip+port '%s' into IP and port: %s\n", IPPort, err) - return &addr - } - } else { - addr.Port, err = strconv.Atoi(portStr) - if err != nil { - log.Printf("Parsed port '%s' is not an integer: %s\n", portStr, err) - return &addr - } - } - - addr.IP = net.ParseIP(ip) - - return &addr -}
M
main.go
→
main.go
@@ -2,15 +2,9 @@ package main
import ( "fmt" - "log" "os" - "os/signal" - "strings" - "syscall" - "time" - "git.j3s.sh/cascade/agent" - "git.j3s.sh/cascade/ipaddr" + "git.j3s.sh/cascade/command/agent" "git.j3s.sh/cascade/list" )@@ -20,10 +14,6 @@ cascade members show serf cluster members
cascade rtt estimate latency between nodes ` -// gracefulTimeout controls how long we wait before forcefully terminating -// note that this value interacts with serf's LeavePropagateDelay config -const gracefulTimeout = 10 * time.Second - // TODO: rename agent to something cooler func main() { if len(os.Args) == 1 {@@ -31,13 +21,11 @@ fmt.Fprintf(os.Stderr, "%s", usage)
os.Exit(1) } - run(os.Args[1], os.Args[2:]) -} - -func run(command string, args []string) { - switch command { + command := os.Args[1] + args := os.Args[2:] + switch os.Args[1] { case "agent": - handleAgent() + agent.Run(args) case "list", "ls": list.Run(args) default:@@ -45,109 +33,3 @@ fmt.Fprintf(os.Stderr, "'%s' is not a valid command\n\n%s", command, usage)
os.Exit(1) } } - -func handleAgent() { - config := getAgentConfig() - agent := agent.New(config) - if err := agent.Start(); err != nil { - fmt.Println(err) - os.Exit(1) - } - defer agent.Shutdown() - // join any specified startup nodes - if err := startupJoin(agent); err != nil { - fmt.Println(err) - os.Exit(1) - } - if err := handleSignals(agent); err != nil { - fmt.Println(err) - os.Exit(1) - } -} - -// handleSignals blocks until we get an exit-causing signal -func handleSignals(agent *agent.Agent) error { - signalCh := make(chan os.Signal, 4) - signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM) - - // Wait for a signal - var sig os.Signal - select { - case s := <-signalCh: - sig = s - case <-agent.ShutdownCh(): - // Agent is already shutdown! - return nil - } - fmt.Fprintf(os.Stderr, "caught signal %s", sig) - - // Check if we should do a graceful leave - graceful := false - if sig == os.Interrupt || sig == syscall.SIGTERM { - graceful = true - } - - // Bail fast if not doing a graceful leave - if !graceful { - fmt.Fprintf(os.Stderr, "leave cluster with zero grace") - return nil - } - - // Attempt a graceful leave - gracefulCh := make(chan struct{}) - fmt.Printf("shutting down gracefully") - go func() { - if err := agent.Leave(); err != nil { - fmt.Println("Error: ", err) - return - } - close(gracefulCh) - }() - - // Wait for leave or another signal - select { - case <-signalCh: - return fmt.Errorf("idfk") - case <-time.After(gracefulTimeout): - return fmt.Errorf("leave timed out") - case <-gracefulCh: - return nil - } -} - -func startupJoin(a *agent.Agent) error { - if len(a.Config.StartJoin) == 0 { - return nil - } - - n, err := a.Join(a.Config.StartJoin) - if err != nil { - return err - } - if n > 0 { - log.Printf("issue join request nodes=%d\n", n) - } - - return nil -} - -func getAgentConfig() *agent.Config { - config := agent.DefaultConfig() - // CASCADE_BIND=192.168.0.15:12345 - if os.Getenv("CASCADE_BIND") != "" { - addr := ipaddr.ParseIPPort(os.Getenv("CASCADE_BIND")) - config.BindAddr.IP = addr.IP - if addr.Port != 0 { - config.BindAddr.Port = addr.Port - } - } - // CASCADE_JOIN=127.0.0.1,127.0.0.5 - if os.Getenv("CASCADE_JOIN") != "" { - config.StartJoin = strings.Split(os.Getenv("CASCADE_JOIN"), ",") - } - // CASCADE_NAME=nostromo.j3s.sh - if os.Getenv("CASCADE_NAME") != "" { - config.NodeName = os.Getenv("CASCADE_NAME") - } - return config -}