Compare commits
31 Commits
Author | SHA1 | Date |
---|---|---|
|
31c4bacc90 | |
|
7f20ecc438 | |
|
3a786d60fd | |
|
aeae4ddf46 | |
|
1d7b1efa27 | |
|
022e411f48 | |
|
a641772159 | |
|
ac5da98149 | |
|
60248b5931 | |
|
611025c946 | |
|
a02078081e | |
|
9b4bbeea80 | |
|
812c10091e | |
|
3249c65169 | |
|
14ee66fe6a | |
|
560162d2b2 | |
|
58ad49600a | |
|
2152a6477f | |
|
94b1881d41 | |
|
5e837be94b | |
|
46e329f419 | |
|
f6b9e6d86a | |
|
66dc8d1f61 | |
|
22d49578c9 | |
|
f5144e4b8a | |
|
afc7fedbe1 | |
|
b71fec22c8 | |
|
769c9757b7 | |
|
4d5c10676b | |
|
3f10b49f04 | |
|
68933a8ec7 |
19
Makefile
19
Makefile
|
@ -9,24 +9,22 @@ REDOMOD = $(shell if [ -e go.sum ]; then echo go.sum exists; else GO111MODULE=
|
|||
|
||||
all: build
|
||||
./zood --version
|
||||
./zood
|
||||
./zood test
|
||||
|
||||
build:
|
||||
GO111MODULE=off go build \
|
||||
build: goimports
|
||||
GO111MODULE=off go build -v -x \
|
||||
-ldflags "-X main.VERSION=${VERSION} -X main.BUILDTIME=${BUILDTIME} -X gui.GUIVERSION=${VERSION}"
|
||||
|
||||
install:
|
||||
GO111MODULE=off go install \
|
||||
GO111MODULE=off go install -v -x \
|
||||
-ldflags "-X main.VERSION=${VERSION} -X main.BUILDTIME=${BUILDTIME} -X gui.GUIVERSION=${VERSION}"
|
||||
|
||||
# this is for release builds using the go.mod files
|
||||
release-build:
|
||||
@echo ${REDOMOD}
|
||||
go build -v -ldflags "-X main.VERSION=${VERSION} -X gui.GUIVERSION=${VERSION}"
|
||||
sudo: build
|
||||
sudo ./zood
|
||||
|
||||
# makes a .deb package
|
||||
debian:
|
||||
go-deb --no-gui --repo go.wit.com/lib/daemons/zood
|
||||
go-deb --auto --repo go.wit.com/lib/daemons/zood
|
||||
|
||||
goimports:
|
||||
goimports -w *.go
|
||||
|
@ -47,3 +45,6 @@ git-clone:
|
|||
|
||||
http-list-packages:
|
||||
curl --silent http://localhost:2521/list?hostname=zookeeper.wit.com
|
||||
|
||||
localhost:
|
||||
./zood --port 2232 --url "http://localhost:2233"
|
||||
|
|
|
@ -7,15 +7,11 @@ all:
|
|||
log:
|
||||
journalctl -f -xeu zood.service
|
||||
|
||||
curl-vms:
|
||||
curl http://localhost:2520/vms
|
||||
curl-toggle-PING-output:
|
||||
curl "http://localhost:2521/flag?flag-PING"
|
||||
|
||||
curl-kill:
|
||||
curl http://localhost:2520/kill
|
||||
|
||||
curl-dumpdomain-coriolis:
|
||||
# information about libvirt domain for vm 'coriolis':
|
||||
curl --silent http://localhost:2520/dumpdomain?domain=coriolis
|
||||
curl http://localhost:2521/kill
|
||||
|
||||
status:
|
||||
dpkg -s zood
|
||||
|
|
2
README
2
README
|
@ -1 +1,3 @@
|
|||
This is the zookeeper daemon.
|
||||
|
||||
It is simple. It's just designed to keep packages up to date.
|
||||
|
|
73
apt.go
73
apt.go
|
@ -1,73 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"go.wit.com/lib/protobuf/zoopb"
|
||||
"go.wit.com/log"
|
||||
)
|
||||
|
||||
// init the installed package list
|
||||
func initPackages() {
|
||||
// Get the list of installed packages for the detected distro
|
||||
newP, err := getPackageList(me.distro)
|
||||
if err != nil {
|
||||
fmt.Println("Error:", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Print the installed packages and their versions
|
||||
for pkg, version := range newP {
|
||||
new1 := new(zoopb.Package)
|
||||
new1.Name = pkg
|
||||
new1.Version = version
|
||||
if me.machine.Packages.Append(new1) {
|
||||
// log.Info("added", new1.Name, "ok")
|
||||
} else {
|
||||
log.Info("added", new1.Name, "failed")
|
||||
}
|
||||
}
|
||||
|
||||
log.Info(me.hostname, "has distro", me.distro, "with", me.machine.Packages.Len(), "packages installed")
|
||||
}
|
||||
|
||||
func addNew(name string, version string) bool {
|
||||
new1 := new(zoopb.Package)
|
||||
new1.Name = name
|
||||
new1.Version = version
|
||||
return me.machine.Packages.Append(new1)
|
||||
}
|
||||
|
||||
func updatePackages() {
|
||||
// Get the list of installed packages for the detected distro
|
||||
newP, err := getPackageList(me.distro)
|
||||
if err != nil {
|
||||
fmt.Println("Error:", err)
|
||||
return
|
||||
}
|
||||
|
||||
var newCounter, changeCounter int
|
||||
// Print the installed packages and their versions
|
||||
for pkg, version := range newP {
|
||||
found := me.machine.Packages.FindByName(pkg)
|
||||
if found == nil {
|
||||
log.Info("adding new", pkg, version)
|
||||
addNew(pkg, version)
|
||||
newCounter += 1
|
||||
} else {
|
||||
found.Version = version
|
||||
if me.machine.Packages.Update(found) {
|
||||
changeCounter += 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
footer := fmt.Sprintf("%s has distro %s with %d packages installed", me.hostname, me.distro, me.machine.Packages.Len())
|
||||
if changeCounter != 0 {
|
||||
footer += fmt.Sprintf(" (%d changed)", changeCounter)
|
||||
}
|
||||
if newCounter != 0 {
|
||||
footer += fmt.Sprintf(" (%d new)", newCounter)
|
||||
}
|
||||
log.Info(footer)
|
||||
}
|
72
apt_linux.go
72
apt_linux.go
|
@ -1,72 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
||||
"go.wit.com/log"
|
||||
)
|
||||
|
||||
// getPackageList returns the list of installed packages based on the distro
|
||||
func getPackageList(distro string) (map[string]string, error) {
|
||||
var cmd *exec.Cmd
|
||||
|
||||
// Run the appropriate command based on the detected distribution
|
||||
switch distro {
|
||||
case "ubuntu", "debian":
|
||||
return dpkgQuery()
|
||||
case "fedora", "centos", "rhel":
|
||||
cmd = exec.Command("rpm", "-qa")
|
||||
case "arch", "manjaro":
|
||||
cmd = exec.Command("pacman", "-Q")
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported distribution: %s", distro)
|
||||
}
|
||||
|
||||
// Capture the command's output
|
||||
output, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error running command: %v", err)
|
||||
}
|
||||
|
||||
// todo: Split the output into lines and return
|
||||
lines := strings.Split(string(output), "\n")
|
||||
log.Info("output had", len(lines), "lines")
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func dpkgQuery() (map[string]string, error) {
|
||||
// Run the dpkg-query command to list installed packages and versions
|
||||
cmd := exec.Command("dpkg-query", "-W", "-f=${Package} ${Version}\n")
|
||||
stdout, err := cmd.StdoutPipe()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Start the command execution
|
||||
if err := cmd.Start(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer cmd.Wait()
|
||||
|
||||
// Create a map to store package names and versions
|
||||
installedPackages := make(map[string]string)
|
||||
|
||||
// Use a scanner to read the output of the command line by line
|
||||
scanner := bufio.NewScanner(stdout)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
// Split each line into package name and version
|
||||
parts := strings.SplitN(line, " ", 2)
|
||||
if len(parts) == 2 {
|
||||
packageName := parts[0]
|
||||
version := parts[1]
|
||||
installedPackages[packageName] = version
|
||||
}
|
||||
}
|
||||
|
||||
// Return the map with package names and versions
|
||||
return installedPackages, scanner.Err()
|
||||
}
|
24
argv.go
24
argv.go
|
@ -1,3 +1,6 @@
|
|||
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
|
||||
// Use of this source code is governed by the GPL 3.0
|
||||
|
||||
package main
|
||||
|
||||
/*
|
||||
|
@ -7,13 +10,19 @@ package main
|
|||
|
||||
import (
|
||||
"go.wit.com/dev/alexflint/arg"
|
||||
"go.wit.com/log"
|
||||
)
|
||||
|
||||
var argv args
|
||||
|
||||
type args struct {
|
||||
Test *EmptyCmd `arg:"subcommand:test" help:"New to forge? This is for you.'"`
|
||||
Daemon bool `arg:"--daemon" default:"false" help:"run in daemon mode"`
|
||||
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 {
|
||||
|
@ -29,3 +38,18 @@ func (a args) Description() string {
|
|||
this daemon talks to zookeeper
|
||||
`
|
||||
}
|
||||
|
||||
var NOW *log.LogFlag
|
||||
var INFO *log.LogFlag
|
||||
var PING *log.LogFlag
|
||||
var WARN *log.LogFlag
|
||||
|
||||
func init() {
|
||||
full := "go.wit.com/apps/zood"
|
||||
short := "zood"
|
||||
|
||||
NOW = log.NewFlag("NOW", true, full, short, "useful while doing debugging")
|
||||
INFO = log.NewFlag("INFO", false, full, short, "general zood")
|
||||
PING = log.NewFlag("PING", false, full, short, "show pings to the zookeeper")
|
||||
WARN = log.NewFlag("WARN", true, full, short, "bad things")
|
||||
}
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
// 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"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"go.wit.com/log"
|
||||
)
|
||||
|
||||
// timeFunction takes a function as an argument and returns the execution time.
|
||||
|
@ -14,10 +16,26 @@ func TimeFunction(f func()) time.Duration {
|
|||
return time.Since(startTime) // Calculate the elapsed time
|
||||
}
|
||||
|
||||
func NewWatchdog() {
|
||||
// sends updates to zookeeper
|
||||
func zood() {
|
||||
me.dog = time.NewTicker(me.pollDelay)
|
||||
defer me.dog.Stop()
|
||||
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
|
||||
go func() {
|
||||
|
@ -25,18 +43,4 @@ func NewWatchdog() {
|
|||
done <- true
|
||||
}()
|
||||
*/
|
||||
for {
|
||||
select {
|
||||
case <-done:
|
||||
fmt.Println("Done!")
|
||||
return
|
||||
case t := <-me.dog.C:
|
||||
log.Info("Watchdog() ticked", me.zookeeper, "Current time: ", t)
|
||||
updatePackages()
|
||||
// pingStatus()
|
||||
sendMachine()
|
||||
// h.pollHypervisor()
|
||||
// h.Scan()
|
||||
}
|
||||
}
|
||||
}
|
68
distro.go
68
distro.go
|
@ -1,68 +0,0 @@
|
|||
// Copyright 2016 The go-qemu Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func initDistro() string {
|
||||
switch runtime.GOOS {
|
||||
case "windows":
|
||||
return "windows"
|
||||
case "macos":
|
||||
return "macos"
|
||||
case "linux":
|
||||
// Detect the Linux distribution
|
||||
distro := detectDistro()
|
||||
if distro == "" {
|
||||
fmt.Println("Unable to detect Linux distribution.")
|
||||
distro = "fixme"
|
||||
}
|
||||
|
||||
fmt.Printf("Detected distribution: %s\n", distro)
|
||||
return distro
|
||||
default:
|
||||
return runtime.GOOS
|
||||
}
|
||||
}
|
||||
|
||||
// detectDistro returns the Linux distribution name (if possible)
|
||||
func detectDistro() string {
|
||||
// Check if we're on Linux
|
||||
|
||||
// Try to read /etc/os-release to determine the distro
|
||||
file, err := os.Open("/etc/os-release")
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
if strings.HasPrefix(line, "ID=") {
|
||||
parts := strings.SplitN(line, "=", 2)
|
||||
if len(parts) == 2 {
|
||||
return strings.Trim(parts[1], `"`)
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
95
http.go
95
http.go
|
@ -1,95 +0,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")
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
118
main.go
118
main.go
|
@ -1,25 +1,16 @@
|
|||
// Copyright 2016 The go-qemu Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
// 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/dev/alexflint/arg"
|
||||
"go.wit.com/lib/hostname"
|
||||
"go.wit.com/lib/protobuf/zoopb"
|
||||
"go.wit.com/log"
|
||||
)
|
||||
|
@ -31,37 +22,94 @@ var BUILDTIME string
|
|||
var resources embed.FS
|
||||
|
||||
func main() {
|
||||
var pp *arg.Parser
|
||||
pp = arg.MustParse(&argv)
|
||||
me = new(zoodStruct)
|
||||
me.pp = arg.MustParse(&argv)
|
||||
|
||||
if pp == nil {
|
||||
pp.WriteHelp(os.Stdout)
|
||||
if me.pp == nil {
|
||||
me.pp.WriteHelp(os.Stdout)
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
if argv.Daemon {
|
||||
// turn off timestamps for STDOUT (systemd adds them)
|
||||
log.DaemonMode(true)
|
||||
me.machine, me.fullpath = zoopb.InitDaemon()
|
||||
} else {
|
||||
me.machine, me.fullpath = zoopb.InitMachine()
|
||||
|
||||
}
|
||||
|
||||
me = new(stuff)
|
||||
me.zookeeper = "zookeeper.wit.com"
|
||||
me.hostname, _ = os.Hostname()
|
||||
if err := testZoo(); err != nil {
|
||||
log.Info("FAILED TO CONNECT TO ZOOKEEPER: ", err)
|
||||
log.Info("sleeping for 3 minutes")
|
||||
time.Sleep(3 * time.Minute)
|
||||
os.Exit(0)
|
||||
}
|
||||
me.pollDelay = 3 * time.Second
|
||||
me.failcountmax = 20 // die every minute if zookeeper can't be found
|
||||
|
||||
// what OS?
|
||||
me.distro = initDistro()
|
||||
|
||||
// init my machine protobuf
|
||||
me.machine = new(zoopb.Machine)
|
||||
me.machine.Packages = new(zoopb.Packages)
|
||||
me.machine.Hostname = me.hostname
|
||||
|
||||
// init the installed package list
|
||||
// me.packages = new(zoopb.Packages)
|
||||
initPackages()
|
||||
|
||||
go NewWatchdog()
|
||||
|
||||
startHTTP()
|
||||
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()
|
||||
newpb.Dump()
|
||||
log.Info("got error:", err)
|
||||
return err
|
||||
}
|
||||
|
|
48
post.go
48
post.go
|
@ -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
|
||||
}
|
70
send.go
70
send.go
|
@ -1,28 +1,29 @@
|
|||
// Copyright 2024 WIT.COM Inc Licensed GPL 3.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 (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"go.wit.com/lib/gui/shell"
|
||||
"go.wit.com/log"
|
||||
)
|
||||
|
||||
var urlbase string = "http://localhost:8080"
|
||||
|
||||
func send() {
|
||||
}
|
||||
|
||||
func pingStatus() error {
|
||||
var url string
|
||||
url = urlbase + "/status?hostname=" + me.hostname
|
||||
url = me.urlbase + "/status?hostname=" + me.machine.Hostname
|
||||
/*
|
||||
msg, err := me.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.machine.HttpPostMachine(url)
|
||||
if err != nil {
|
||||
log.Info("httpPost() failed:", err)
|
||||
return err
|
||||
|
@ -41,29 +42,54 @@ func pingStatus() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func sendMachine() error {
|
||||
func sendMachine(s string) error {
|
||||
var url string
|
||||
url = urlbase + "/machine"
|
||||
msg, err := me.machine.Marshal()
|
||||
if err != nil {
|
||||
log.Info("proto.Marshal() failed:", err)
|
||||
return err
|
||||
}
|
||||
log.Info("proto Marshal len =", len(msg))
|
||||
body, err := httpPost(url, msg)
|
||||
url = me.urlbase + "/machine"
|
||||
body, err := me.machine.HttpPostMachine(url)
|
||||
if err != nil {
|
||||
log.Info("httpPost() failed: url", url)
|
||||
log.Info("httpPost() failed:", err)
|
||||
log.Info("httpPost() data:", string(body))
|
||||
return err
|
||||
}
|
||||
// out := strings.TrimSpace(string(body))
|
||||
// log.Info("httpPost() data:", out, len(body))
|
||||
|
||||
test := strings.TrimSpace(string(body))
|
||||
// log.Info("virtigo returned body:", test)
|
||||
for _, line := range strings.Split(test, "\n") {
|
||||
switch line {
|
||||
case "upgrade":
|
||||
log.Info("machine upgrade now")
|
||||
default:
|
||||
log.Info("GOT:", line)
|
||||
if line == "good" {
|
||||
log.Info(s, "zookeeper is healthy")
|
||||
me.failcount = 0
|
||||
} else if line == "kill" {
|
||||
os.Exit(0)
|
||||
} else if strings.HasPrefix(line, "apt update") {
|
||||
log.Info("machine upgrade now", line)
|
||||
shell.RunRealtime([]string{"mv", "-f", "/usr/bin/zood", "/usr/bin/zood.last"})
|
||||
shell.RunRealtime([]string{"apt", "update"})
|
||||
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)
|
||||
} 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 {
|
||||
log.Info(me.urlbase, "is maybe not working GOT:", line)
|
||||
log.Info(me.urlbase, "fail count", me.failcount, "from hostname", me.machine.Hostname)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
|
20
structs.go
20
structs.go
|
@ -1,20 +1,26 @@
|
|||
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
|
||||
// Use of this source code is governed by the GPL 3.0
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"go.wit.com/dev/alexflint/arg"
|
||||
"go.wit.com/lib/protobuf/zoopb"
|
||||
)
|
||||
|
||||
var me *stuff
|
||||
var me *zoodStruct
|
||||
|
||||
// this app's variables
|
||||
type stuff struct {
|
||||
hostname string // my hostname to send to zookeeper
|
||||
zookeeper string // the dns name for the zookeeper
|
||||
type zoodStruct struct {
|
||||
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
|
||||
distro string // debian,redhat,gentoo,macos,wincrap
|
||||
machine *zoopb.Machine // my protobuf
|
||||
// packages *zoopb.Packages // installed packages and versions
|
||||
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
|
||||
fullpath string // where to save the machine PB file
|
||||
pp *arg.Parser // from go-args
|
||||
}
|
||||
|
|
|
@ -1,13 +1,14 @@
|
|||
[Unit]
|
||||
Description=zood
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
User=root
|
||||
Type=simple
|
||||
ExecStart=/usr/bin/zood
|
||||
ExecStop=killall zood
|
||||
Restart=on-failure
|
||||
RestartSec=5
|
||||
ExecStart=/usr/bin/zood --daemon
|
||||
Restart=always
|
||||
RestartSec=5s
|
||||
KillMode=process
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
|
|
Loading…
Reference in New Issue