Compare commits

..

No commits in common. "master" and "v0.0.93" have entirely different histories.

2 changed files with 14 additions and 66 deletions

73
main.go
View File

@ -5,12 +5,10 @@ package main
import ( import (
"embed" "embed"
"fmt"
"os" "os"
"time" "time"
"go.wit.com/dev/alexflint/arg" "go.wit.com/dev/alexflint/arg"
"go.wit.com/lib/hostname"
"go.wit.com/lib/protobuf/zoopb" "go.wit.com/lib/protobuf/zoopb"
"go.wit.com/log" "go.wit.com/log"
) )
@ -22,32 +20,31 @@ var BUILDTIME string
var resources embed.FS var resources embed.FS
func main() { func main() {
me = new(zoodStruct) var pp *arg.Parser
me.pp = arg.MustParse(&argv) pp = arg.MustParse(&argv)
if me.pp == nil { if pp == nil {
me.pp.WriteHelp(os.Stdout) pp.WriteHelp(os.Stdout)
os.Exit(0) os.Exit(0)
} }
if argv.Daemon { if argv.Daemon {
// turn off timestamps for STDOUT (systemd adds them) // turn off timestamps for STDOUT (systemd adds them)
log.DaemonMode(true) log.DaemonMode(true)
me.machine, me.fullpath = zoopb.InitDaemon()
} else {
me.machine, me.fullpath = zoopb.InitMachine()
} }
if err := testZoo(); err != nil { me = new(stuff)
log.Info("FAILED TO CONNECT TO ZOOKEEPER: ", err) me.urlbase = "http://zookeeper.chap.wit.com:8080"
log.Info("sleeping for 3 minutes") if argv.URL != "" {
time.Sleep(3 * time.Minute) log.Info("USING ARGV URL:", argv.URL)
os.Exit(0) me.urlbase = argv.URL
} }
me.pollDelay = 3 * time.Second me.pollDelay = 3 * time.Second
me.failcountmax = 20 // die every minute if zookeeper can't be found me.failcountmax = 20 // die every minute if zookeeper can't be found
me.machine = zoopb.InitMachine()
if argv.Test != nil { if argv.Test != nil {
testURL(me.urlbase, me.machine) testURL(me.urlbase, me.machine)
os.Exit(0) os.Exit(0)
@ -56,52 +53,6 @@ func main() {
zood() // talks to zookeeper zood() // talks to zookeeper
} }
func testZoo() error {
var err error
if argv.URL != "" {
log.Info("USING ARGV URL:", argv.URL)
if err = testURL(argv.URL, me.machine); err == nil {
me.urlbase = argv.URL
return nil
}
}
zooname := findZookeeper()
url := fmt.Sprintf("http://%s:8080/", zooname)
if err = testURL(url, me.machine); err == nil {
me.urlbase = url
return nil
}
url = fmt.Sprintf("https://%s/", zooname)
if err = testURL(url, me.machine); err == nil {
me.urlbase = url
return nil
}
url = "http://zookeeper.wit.com:8080/"
if err = testURL(url, me.machine); err == nil {
me.urlbase = url
return nil
}
url = "https://zookeeper.wit.com/"
if err = testURL(url, me.machine); err == nil {
me.urlbase = url
return nil
}
return err
}
func findZookeeper() string {
hname, dname, err := hostname.GetDomainname()
if err != nil {
log.Info("error with os.Hostname()", hname, dname, err)
}
if dname == "" {
return "zookeeper.wit.com"
}
return hostname.Join("zookeeper", dname)
}
func testURL(urlbase string, pb *zoopb.Machine) error { func testURL(urlbase string, pb *zoopb.Machine) error {
newpb, wsPB, err := pb.HttpPost(urlbase, "test") newpb, wsPB, err := pb.HttpPost(urlbase, "test")
if err != nil { if err != nil {

View File

@ -6,14 +6,13 @@ package main
import ( import (
"time" "time"
"go.wit.com/dev/alexflint/arg"
"go.wit.com/lib/protobuf/zoopb" "go.wit.com/lib/protobuf/zoopb"
) )
var me *zoodStruct var me *stuff
// this app's variables // this app's variables
type zoodStruct struct { type stuff struct {
urlbase string // the dns name for the zookeeper urlbase string // the dns name for the zookeeper
hostname string // my hostname hostname string // my hostname
pollDelay time.Duration // how often to report our status pollDelay time.Duration // how often to report our status
@ -21,6 +20,4 @@ type zoodStruct struct {
machine *zoopb.Machine // populated from protobuf based zoopb machine *zoopb.Machine // populated from protobuf based zoopb
failcount int // how many times we've failed to contact the zookeeper failcount int // how many times we've failed to contact the zookeeper
failcountmax int // after this, exit and let systemd restart the daemon failcountmax int // after this, exit and let systemd restart the daemon
fullpath string // where to save the machine PB file
pp *arg.Parser // from go-args
} }