summaryrefslogtreecommitdiff
path: root/event.go
blob: a2406b960eb0eb4f806f2c458f125fe7466e6243 (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
package main

import (
	"fmt"
	"time"

	"go.wit.com/lib/gui/shell"
	pb "go.wit.com/lib/protobuf/virtbuf"
	"go.wit.com/log"
)

// restarts the virtigod daemon on a hypervisor via http
func (h *HyperT) RestartVirtigod() {
	url := "http://" + h.pb.Hostname + ":2520/kill"
	s := shell.Wget(url)
	log.Info("EVENT RestartVirtigod", url, s)
	h.lastpoll = time.Now()
	h.killcount += 1

	dur := time.Since(h.lastpoll) // Calculate the elapsed time
	log.Info("KILLED DAEMON", h.pb.Hostname, shell.FormatDuration(dur), "curl", url)
	me.killcount += 1

	// mark the cluster as unstable so droplet starts can be throttled
	me.unstable = time.Now()
}

// checks if the cluster is ready and stable
func clusterReady() (bool, string) {
	last := time.Since(me.unstable)
	if last > me.unstableTimeout {
		// the cluster has not been stable for 133 seconds
		log.Warn("clusterReady() is stable for ", shell.FormatDuration(me.unstableTimeout), " secs")
		return true, fmt.Sprintln("clusterReady() is stable ", shell.FormatDuration(me.unstableTimeout), " secs")
	}
	log.Warn("clusterReady() is unstable for", shell.FormatDuration(last))
	return false, "clusterReady() is unstable for " + shell.FormatDuration(last)
}

func dropletReady(d *pb.Droplet) (bool, string) {
	if d.CurrentState == pb.DropletState_ON {
		return false, "EVENT start droplet is already ON"
	}
	if d.Starts > 2 {
		// reason := "EVENT start droplet has already been started " + d.starts + " times"
		return false, fmt.Sprintln("EVENT start droplet has already been started ", d.Starts, " times")
	}
	return true, ""
}

// this must be bool in string because accumulated output is sometimes
// written to STDOUT, sometimes to http
func (h *HyperT) start(d *pb.Droplet) (bool, string) {
	ready, result := clusterReady()
	if !ready {
		return false, result
	}
	ready, result = dropletReady(d)
	if !ready {
		return false, result
	}

	url := "http://" + h.pb.Hostname + ":2520/start?start=" + d.Hostname
	var msg string
	var data []byte
	msg = d.FormatJSON()
	data = []byte(msg) // Convert the string to []byte
	req, err := httpPost(url, data)
	if err != nil {
		return false, fmt.Sprintln("error:", err)
	}
	log.Info("http post url:", url)
	log.Info("http post data:", msg)

	result = "EVENT start droplet url: " + url + "\n"
	result += "EVENT start droplet response: " + string(req)

	// increment the counter for a start attempt working
	d.Starts += 1

	// mark the cluster as unstable so droplet starts can be throttled
	me.unstable = time.Now()

	return true, result
}

func findDroplet(name string) *pb.Droplet {
	for _, d := range me.cluster.Droplets {
		if d.Hostname == name {
			return d
		}
	}
	return nil
}