1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
  | 
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0
package main
import (
	"embed"
	"fmt"
	"os"
	"time"
	"go.wit.com/lib/debian"
	"go.wit.com/lib/hostname"
	"go.wit.com/lib/protobuf/argvpb"
	"go.wit.com/lib/protobuf/zoopb"
	"go.wit.com/log"
)
//go:embed resources/*
var resources embed.FS
func main() {
	me = new(zoodStruct)
	argvpb.Init(&argv, APPNAME, BUILDTIME, VERSION) // adds shell auto-complete
	if argv.Daemon {
		// turn off timestamps for STDOUT (systemd adds them)
		log.DaemonMode(true)
		me.machine = debian.InitDaemon()
	} else {
		me.machine = debian.InitMachine()
	}
	if argv.Status != nil {
		log.Info("todo: show status here")
		argvpb.GoodExit("says hi")
	}
	if err := testZoo(); err != nil {
		log.Info("failed to connect. sleeping for 3 minutes")
		time.Sleep(3 * time.Minute)
		argvpb.BadExit("failed to connect to zookeeper", err)
	}
	me.pollDelay = 3 * time.Second
	me.failcountmax = 20 // die every minute if zookeeper can't be found
	if argv.Test != nil {
		testURL(me.urlbase, me.machine)
		os.Exit(0)
	}
	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 {
	newpb, wsPB, err := pb.HttpPost(urlbase, "test")
	if err != nil {
		log.Info("got error:", err)
		return err
	}
	wsPB.DumpClient()
	log.Infof("mem=%d cpus=%d\n", newpb.Memory, newpb.Cpus)
	log.Info("got error:", err)
	return err
}
  |