summaryrefslogtreecommitdiff
path: root/send.go
blob: 727cbc9786d321ead7666fae317bf3635f8aed79 (plain)
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
// 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"
)

func pingStatus() error {
	var url string
	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 := me.machine.HttpPostMachine(url)
	if err != nil {
		log.Info("httpPost() failed:", err)
		return err
	}

	test := strings.TrimSpace(string(body))
	// log.Info("virtigo returned body:", test)
	for _, line := range strings.Split(test, "\n") {
		switch line {
		case "upgrade":
			log.Info("should upgrade now")
		default:
			log.Info("GOT:", line)
		}
	}
	return nil
}

func sendMachine(s string) error {
	var url string
	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") {
		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
}