Compare commits
18 Commits
Author | SHA1 | Date |
---|---|---|
|
31c4bacc90 | |
|
7f20ecc438 | |
|
3a786d60fd | |
|
aeae4ddf46 | |
|
1d7b1efa27 | |
|
022e411f48 | |
|
a641772159 | |
|
ac5da98149 | |
|
60248b5931 | |
|
611025c946 | |
|
a02078081e | |
|
9b4bbeea80 | |
|
812c10091e | |
|
3249c65169 | |
|
14ee66fe6a | |
|
560162d2b2 | |
|
58ad49600a | |
|
2152a6477f |
6
Makefile
6
Makefile
|
@ -9,14 +9,14 @@ REDOMOD = $(shell if [ -e go.sum ]; then echo go.sum exists; else GO111MODULE=
|
||||||
|
|
||||||
all: build
|
all: build
|
||||||
./zood --version
|
./zood --version
|
||||||
./zood
|
./zood test
|
||||||
|
|
||||||
build: goimports
|
build: goimports
|
||||||
GO111MODULE=off go build \
|
GO111MODULE=off go build -v -x \
|
||||||
-ldflags "-X main.VERSION=${VERSION} -X main.BUILDTIME=${BUILDTIME} -X gui.GUIVERSION=${VERSION}"
|
-ldflags "-X main.VERSION=${VERSION} -X main.BUILDTIME=${BUILDTIME} -X gui.GUIVERSION=${VERSION}"
|
||||||
|
|
||||||
install:
|
install:
|
||||||
GO111MODULE=off go install \
|
GO111MODULE=off go install -v -x \
|
||||||
-ldflags "-X main.VERSION=${VERSION} -X main.BUILDTIME=${BUILDTIME} -X gui.GUIVERSION=${VERSION}"
|
-ldflags "-X main.VERSION=${VERSION} -X main.BUILDTIME=${BUILDTIME} -X gui.GUIVERSION=${VERSION}"
|
||||||
|
|
||||||
sudo: build
|
sudo: build
|
||||||
|
|
2
README
2
README
|
@ -1 +1,3 @@
|
||||||
This is the zookeeper daemon.
|
This is the zookeeper daemon.
|
||||||
|
|
||||||
|
It is simple. It's just designed to keep packages up to date.
|
||||||
|
|
10
argv.go
10
argv.go
|
@ -16,9 +16,13 @@ import (
|
||||||
var argv args
|
var argv args
|
||||||
|
|
||||||
type args struct {
|
type args struct {
|
||||||
Daemon bool `arg:"--daemon" default:"false" help:"run in daemon mode"`
|
Test *EmptyCmd `arg:"subcommand:test" help:"New to forge? This is for you.'"`
|
||||||
Port int `arg:"--port" default:"2521" help:"port to run on"`
|
Daemon bool `arg:"--daemon" default:"false" help:"run in daemon mode"`
|
||||||
URL string `arg:"--url" help:"url to use"`
|
Port int `arg:"--port" default:"2521" help:"port to run on"`
|
||||||
|
URL string `arg:"--url" help:"url to use"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type EmptyCmd struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (args) Version() string {
|
func (args) Version() string {
|
||||||
|
|
|
@ -16,10 +16,26 @@ func TimeFunction(f func()) time.Duration {
|
||||||
return time.Since(startTime) // Calculate the elapsed time
|
return time.Since(startTime) // Calculate the elapsed time
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewWatchdog() {
|
// sends updates to zookeeper
|
||||||
|
func zood() {
|
||||||
me.dog = time.NewTicker(me.pollDelay)
|
me.dog = time.NewTicker(me.pollDelay)
|
||||||
defer me.dog.Stop()
|
defer me.dog.Stop()
|
||||||
done := make(chan bool)
|
done := make(chan bool)
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-done:
|
||||||
|
fmt.Println("Done!")
|
||||||
|
return
|
||||||
|
case _ = <-me.dog.C:
|
||||||
|
s := me.machine.UpdatePackages()
|
||||||
|
me.failcount += 1
|
||||||
|
sendMachine(s)
|
||||||
|
|
||||||
|
if me.failcount > 20 {
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
/*
|
/*
|
||||||
// this example would exit/destroy the ticker in 10 seconds
|
// this example would exit/destroy the ticker in 10 seconds
|
||||||
go func() {
|
go func() {
|
||||||
|
@ -27,23 +43,4 @@ func NewWatchdog() {
|
||||||
done <- true
|
done <- true
|
||||||
}()
|
}()
|
||||||
*/
|
*/
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-done:
|
|
||||||
fmt.Println("Done!")
|
|
||||||
return
|
|
||||||
case _ = <-me.dog.C:
|
|
||||||
// log.Info("Watchdog() ticked", me.zookeeper, "Current time: ", t)
|
|
||||||
s := me.forge.Machine.UpdatePackages()
|
|
||||||
// pingStatus()
|
|
||||||
me.failcount += 1
|
|
||||||
sendMachine(s)
|
|
||||||
|
|
||||||
if me.failcount > 20 {
|
|
||||||
os.Exit(0)
|
|
||||||
}
|
|
||||||
// h.pollHypervisor()
|
|
||||||
// h.Scan()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
119
http.go
119
http.go
|
@ -1,119 +0,0 @@
|
||||||
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
|
|
||||||
// Use of this source code is governed by the GPL 3.0
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"go.wit.com/log"
|
|
||||||
)
|
|
||||||
|
|
||||||
// remove '?' part and trailing '/'
|
|
||||||
func cleanURL(url string) string {
|
|
||||||
url = "/" + strings.Trim(url, "/")
|
|
||||||
return url
|
|
||||||
}
|
|
||||||
|
|
||||||
func okHandler(w http.ResponseWriter, r *http.Request) {
|
|
||||||
log.Info("Got URL Path: ", r.URL.Path)
|
|
||||||
route := cleanURL(r.URL.Path)
|
|
||||||
|
|
||||||
domname := r.URL.Query().Get("domain")
|
|
||||||
flag := r.URL.Query().Get("flag")
|
|
||||||
|
|
||||||
msg, err := ioutil.ReadAll(r.Body) // Read the body as []byte
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintln(w, "ReadAll() error =", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Info("Got URL msg:", string(msg))
|
|
||||||
if route == "/" {
|
|
||||||
fmt.Fprintln(w, "OK")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// exit the virtigo daemon & have systemd restart it
|
|
||||||
// this can happen & when it does, access to
|
|
||||||
// to libvirtd will hang (aka: virsh list will hang)
|
|
||||||
// One way to trigger this is to not properly close
|
|
||||||
// domain sockets opened from go-qemu/hypervisor
|
|
||||||
// it's a good idea in any case so leave it here
|
|
||||||
if route == "/kill" {
|
|
||||||
log.Warn("KILLED")
|
|
||||||
fmt.Fprintln(w, "KILLED")
|
|
||||||
os.Exit(-1)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// curl http://localhost:2520/import?domain=foo.bar.com
|
|
||||||
if route == "/import" {
|
|
||||||
fmt.Fprint(w, "import domain:", domname)
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// toggle logging flags
|
|
||||||
if route == "/flag" {
|
|
||||||
log.HttpMode(w)
|
|
||||||
defer log.HttpMode(nil)
|
|
||||||
log.Info("going to toggle flag:", flag)
|
|
||||||
switch flag {
|
|
||||||
case "PING":
|
|
||||||
if PING.Enabled() {
|
|
||||||
log.Log(NOW, "toogle PING false")
|
|
||||||
PING.SetBool(false)
|
|
||||||
} else {
|
|
||||||
log.Log(NOW, "toogle PING true")
|
|
||||||
PING.SetBool(true)
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
log.Info("unknown looging flag:", flag)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if route == "/favicon.ico" {
|
|
||||||
writeFile(w, "ipv6.png")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Warn("BAD URL =", route)
|
|
||||||
}
|
|
||||||
|
|
||||||
func writeFile(w http.ResponseWriter, filename string) {
|
|
||||||
// fmt.Fprintln(w, "GOT TEST?")
|
|
||||||
fullname := "resources/" + filename
|
|
||||||
pfile, err := resources.ReadFile(fullname)
|
|
||||||
if err != nil {
|
|
||||||
log.Println("ERROR:", err)
|
|
||||||
// w.Write(pfile)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var repohtml string
|
|
||||||
repohtml = string(pfile)
|
|
||||||
if filename == "goReference.svg" {
|
|
||||||
w.Header().Set("Content-Type", "image/svg+xml")
|
|
||||||
}
|
|
||||||
fmt.Fprintln(w, repohtml)
|
|
||||||
log.Println("writeFile() found internal file:", filename)
|
|
||||||
}
|
|
||||||
|
|
||||||
// starts and sits waiting for HTTP requests
|
|
||||||
func startHTTP() {
|
|
||||||
http.HandleFunc("/", okHandler)
|
|
||||||
|
|
||||||
p := fmt.Sprintf(":%d", argv.Port)
|
|
||||||
log.Println("Running on port", p)
|
|
||||||
|
|
||||||
err := http.ListenAndServe(p, nil)
|
|
||||||
if err != nil {
|
|
||||||
log.Println("Error starting server:", err)
|
|
||||||
}
|
|
||||||
}
|
|
97
main.go
97
main.go
|
@ -5,11 +5,13 @@ 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/protobuf/forgepb"
|
"go.wit.com/lib/hostname"
|
||||||
|
"go.wit.com/lib/protobuf/zoopb"
|
||||||
"go.wit.com/log"
|
"go.wit.com/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -20,35 +22,94 @@ var BUILDTIME string
|
||||||
var resources embed.FS
|
var resources embed.FS
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var pp *arg.Parser
|
me = new(zoodStruct)
|
||||||
pp = arg.MustParse(&argv)
|
me.pp = arg.MustParse(&argv)
|
||||||
|
|
||||||
if pp == nil {
|
if me.pp == nil {
|
||||||
pp.WriteHelp(os.Stdout)
|
me.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()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
me = new(stuff)
|
if err := testZoo(); err != nil {
|
||||||
me.urlbase = "http://zookeeper.grid.wit.com:8080"
|
log.Info("FAILED TO CONNECT TO ZOOKEEPER: ", err)
|
||||||
if argv.URL != "" {
|
log.Info("sleeping for 3 minutes")
|
||||||
log.Info("USING ARGV URL:", argv.URL)
|
time.Sleep(3 * time.Minute)
|
||||||
me.urlbase = argv.URL
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
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.ConfigLoad()
|
if argv.Test != nil {
|
||||||
|
testURL(me.urlbase, me.machine)
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
me.forge = forgepb.InitPB()
|
zood() // talks to zookeeper
|
||||||
me.forge.InitMachine()
|
}
|
||||||
|
|
||||||
go NewWatchdog()
|
func testZoo() error {
|
||||||
|
var err error
|
||||||
startHTTP()
|
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()
|
||||||
|
newpb.Dump()
|
||||||
|
log.Info("got error:", err)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
59
send.go
59
send.go
|
@ -13,14 +13,17 @@ import (
|
||||||
|
|
||||||
func pingStatus() error {
|
func pingStatus() error {
|
||||||
var url string
|
var url string
|
||||||
url = me.urlbase + "/status?hostname=" + me.forge.Machine.Hostname
|
url = me.urlbase + "/status?hostname=" + me.machine.Hostname
|
||||||
msg, err := me.forge.Machine.Packages.Marshal()
|
/*
|
||||||
if err != nil {
|
msg, err := me.machine.Packages.Marshal()
|
||||||
log.Info("proto.Marshal() failed:", err)
|
if err != nil {
|
||||||
return err
|
log.Info("proto.Marshal() failed:", err)
|
||||||
}
|
return err
|
||||||
log.Info("proto Marshal len =", len(msg))
|
}
|
||||||
body, err := me.forge.HttpPost(url, msg)
|
log.Info("proto Marshal len =", len(msg))
|
||||||
|
*/
|
||||||
|
|
||||||
|
body, err := me.machine.HttpPostMachine(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Info("httpPost() failed:", err)
|
log.Info("httpPost() failed:", err)
|
||||||
return err
|
return err
|
||||||
|
@ -42,35 +45,51 @@ func pingStatus() error {
|
||||||
func sendMachine(s string) error {
|
func sendMachine(s string) error {
|
||||||
var url string
|
var url string
|
||||||
url = me.urlbase + "/machine"
|
url = me.urlbase + "/machine"
|
||||||
msg, err := me.forge.Machine.Marshal()
|
body, err := me.machine.HttpPostMachine(url)
|
||||||
if err != nil {
|
|
||||||
log.Info("proto.Marshal() failed:", err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
body, err := me.forge.HttpPost(url, msg)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Info("httpPost() failed: url", url)
|
log.Info("httpPost() failed: url", url)
|
||||||
log.Info("httpPost() failed:", err)
|
log.Info("httpPost() failed:", err)
|
||||||
|
log.Info("httpPost() data:", string(body))
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
// out := strings.TrimSpace(string(body))
|
||||||
|
// log.Info("httpPost() data:", out, len(body))
|
||||||
|
|
||||||
test := strings.TrimSpace(string(body))
|
test := strings.TrimSpace(string(body))
|
||||||
// log.Info("virtigo returned body:", test)
|
// log.Info("virtigo returned body:", test)
|
||||||
for _, line := range strings.Split(test, "\n") {
|
for _, line := range strings.Split(test, "\n") {
|
||||||
if line == "upgrade" {
|
if line == "good" {
|
||||||
log.Info(s, "zookeeper is healthy", len(msg))
|
log.Info(s, "zookeeper is healthy")
|
||||||
me.failcount = 0
|
me.failcount = 0
|
||||||
} else if line == "kill" {
|
} else if line == "kill" {
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
} else if strings.HasPrefix(line, "apt update") {
|
} else if strings.HasPrefix(line, "apt update") {
|
||||||
log.Info("machine upgrade now", line)
|
log.Info("machine upgrade now", line)
|
||||||
shell.Run([]string{"apt", "update"})
|
shell.RunRealtime([]string{"mv", "-f", "/usr/bin/zood", "/usr/bin/zood.last"})
|
||||||
shell.Run([]string{"apt", "install", "zood"})
|
shell.RunRealtime([]string{"apt", "update"})
|
||||||
log.Sleep(1)
|
shell.RunRealtime([]string{"apt", "install", "zood"})
|
||||||
|
if shell.Exists("/usr/bin/zood") {
|
||||||
|
shell.RunRealtime([]string{"rm", "-f", "/usr/bin/zood.last"})
|
||||||
|
} else {
|
||||||
|
// there is not a new version of the binary. move the old one back
|
||||||
|
shell.RunRealtime([]string{"mv", "-f", "/usr/bin/zood.last", "/usr/bin/zood"})
|
||||||
|
}
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
|
} else if strings.HasPrefix(line, "apt install") {
|
||||||
|
log.Info("Got to apt install", line)
|
||||||
|
parts := strings.Fields(line)
|
||||||
|
if len(parts) > 1 {
|
||||||
|
cmd := []string{"apt"}
|
||||||
|
cmd = append(cmd, parts[1:]...)
|
||||||
|
log.Info("Got to parts =", parts)
|
||||||
|
log.Info("Got to cmd =", cmd)
|
||||||
|
shell.RunRealtime(cmd)
|
||||||
|
} else {
|
||||||
|
log.Info("nothing to install for line:", line)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
log.Info(me.urlbase, "is maybe not working GOT:", line)
|
log.Info(me.urlbase, "is maybe not working GOT:", line)
|
||||||
log.Info(me.urlbase, "fail count", me.failcount, "from hostname", me.forge.Machine.Hostname)
|
log.Info(me.urlbase, "fail count", me.failcount, "from hostname", me.machine.Hostname)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
20
structs.go
20
structs.go
|
@ -6,19 +6,21 @@ package main
|
||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"go.wit.com/lib/protobuf/forgepb"
|
"go.wit.com/dev/alexflint/arg"
|
||||||
|
"go.wit.com/lib/protobuf/zoopb"
|
||||||
)
|
)
|
||||||
|
|
||||||
var me *stuff
|
var me *zoodStruct
|
||||||
|
|
||||||
// this app's variables
|
// this app's variables
|
||||||
type stuff struct {
|
type zoodStruct 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
|
||||||
dog *time.Ticker // the watchdog timer
|
dog *time.Ticker // the watchdog timer
|
||||||
// machine zoopb.Machine // populated from protobuf based zoopb
|
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
|
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
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,14 @@
|
||||||
[Unit]
|
[Unit]
|
||||||
Description=zood
|
Description=zood
|
||||||
|
After=network.target
|
||||||
|
|
||||||
[Service]
|
[Service]
|
||||||
User=root
|
User=root
|
||||||
Type=simple
|
Type=simple
|
||||||
ExecStart=/usr/bin/zood --daemon
|
ExecStart=/usr/bin/zood --daemon
|
||||||
ExecStop=killall zood
|
Restart=always
|
||||||
Restart=on-failure
|
RestartSec=5s
|
||||||
RestartSec=5
|
KillMode=process
|
||||||
|
|
||||||
[Install]
|
[Install]
|
||||||
WantedBy=multi-user.target
|
WantedBy=multi-user.target
|
||||||
|
|
Loading…
Reference in New Issue