From 5e837be94b272f6b69b0b68ed070617adcf83ba2 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sat, 15 Feb 2025 12:16:27 -0600 Subject: [PATCH] move some common things to forgepb --- main.go | 6 +++++- post.go | 48 ------------------------------------------------ send.go | 12 ++++++------ structs.go | 16 +++++++++------- watchdog.go | 2 +- 5 files changed, 21 insertions(+), 63 deletions(-) delete mode 100644 post.go diff --git a/main.go b/main.go index d7ec9c9..81b3bc3 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "time" "go.wit.com/dev/alexflint/arg" + "go.wit.com/lib/protobuf/forgepb" "go.wit.com/log" ) @@ -41,7 +42,10 @@ func main() { me.pollDelay = 3 * time.Second me.failcountmax = 20 // die every minute if zookeeper can't be found - me.machine.ConfigLoad() + // me.machine.ConfigLoad() + + me.forge = forgepb.InitPB() + me.forge.InitMachine() go NewWatchdog() diff --git a/post.go b/post.go deleted file mode 100644 index 08c487d..0000000 --- a/post.go +++ /dev/null @@ -1,48 +0,0 @@ -package main - -import ( - "bytes" - "io/ioutil" - "net/http" - "os" - "os/user" - - "go.wit.com/log" -) - -func httpPost(url string, data []byte) ([]byte, error) { - var err error - var req *http.Request - - // data := []byte("some junk") - // url := "https://go.wit.com/register/" - - req, err = http.NewRequest(http.MethodPost, url, bytes.NewBuffer(data)) - - usr, _ := user.Current() - req.Header.Set("author", usr.Username) - hostname, _ := os.Hostname() - req.Header.Set("hostname", hostname) - - client := &http.Client{} - resp, err := client.Do(req) - if err != nil { - log.Error(err) - return []byte("client.Do(req) error"), err - } - defer resp.Body.Close() - - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - log.Error(err) - return body, err - } - - // test := strings.TrimSpace(string(body)) - // log.Info("go.wit.com returned body:", test) - // if test == "OK" { - // return body, nil - // } - - return body, nil -} diff --git a/send.go b/send.go index 10ca056..e0e7ec5 100644 --- a/send.go +++ b/send.go @@ -12,14 +12,14 @@ import ( func pingStatus() error { var url string - url = me.urlbase + "/status?hostname=" + me.machine.Hostname - msg, err := me.machine.Packages.Marshal() + url = me.urlbase + "/status?hostname=" + me.forge.Machine.Hostname + msg, err := me.forge.Machine.Packages.Marshal() if err != nil { log.Info("proto.Marshal() failed:", err) return err } log.Info("proto Marshal len =", len(msg)) - body, err := httpPost(url, msg) + body, err := me.forge.HttpPost(url, msg) if err != nil { log.Info("httpPost() failed:", err) return err @@ -41,12 +41,12 @@ func pingStatus() error { func sendMachine(s string) error { var url string url = me.urlbase + "/machine" - msg, err := me.machine.Marshal() + msg, err := me.forge.Machine.Marshal() if err != nil { log.Info("proto.Marshal() failed:", err) return err } - body, err := httpPost(url, msg) + body, err := me.forge.HttpPost(url, msg) if err != nil { log.Info("httpPost() failed: url", url) log.Info("httpPost() failed:", err) @@ -69,7 +69,7 @@ func sendMachine(s string) error { os.Exit(0) } else { log.Info(me.urlbase, "is maybe not working GOT:", line) - log.Info(me.urlbase, "fail count", me.failcount, "from hostname", me.machine.Hostname) + log.Info(me.urlbase, "fail count", me.failcount, "from hostname", me.forge.Machine.Hostname) } } return nil diff --git a/structs.go b/structs.go index fe96980..dcdf5c6 100644 --- a/structs.go +++ b/structs.go @@ -3,17 +3,19 @@ package main import ( "time" - "go.wit.com/lib/protobuf/zoopb" + "go.wit.com/lib/protobuf/forgepb" ) var me *stuff // this app's variables type stuff struct { - urlbase string // the dns name for the zookeeper - pollDelay time.Duration // how often to report our status - dog *time.Ticker // the watchdog timer - machine zoopb.Machine // populated from protobuf based zoopb - failcount int // how many times we've failed to contact the zookeeper - failcountmax int // after this, exit and let systemd restart the daemon + urlbase string // the dns name for the zookeeper + hostname string // my hostname + pollDelay time.Duration // how often to report our status + dog *time.Ticker // the watchdog timer + // machine zoopb.Machine // populated from protobuf based zoopb + forge *forgepb.Forge // handle to forge + failcount int // how many times we've failed to contact the zookeeper + failcountmax int // after this, exit and let systemd restart the daemon } diff --git a/watchdog.go b/watchdog.go index e6587a8..5d1b4a2 100644 --- a/watchdog.go +++ b/watchdog.go @@ -31,7 +31,7 @@ func NewWatchdog() { return case _ = <-me.dog.C: // log.Info("Watchdog() ticked", me.zookeeper, "Current time: ", t) - s := me.machine.UpdatePackages() + s := me.forge.Machine.UpdatePackages() // pingStatus() me.failcount += 1 sendMachine(s)