summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2025-02-15 12:16:27 -0600
committerJeff Carr <[email protected]>2025-02-15 12:21:34 -0600
commit5e837be94b272f6b69b0b68ed070617adcf83ba2 (patch)
tree98c485ee2ef2814560da360f0351894cc6f814af
parent46e329f4190bb5b41b1ec5ecf89e319a626f8d63 (diff)
move some common things to forgepb
-rw-r--r--main.go6
-rw-r--r--post.go48
-rw-r--r--send.go12
-rw-r--r--structs.go16
-rw-r--r--watchdog.go2
5 files changed, 21 insertions, 63 deletions
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)