mirrored from https://skia.googlesource.com/buildbot
-
Notifications
You must be signed in to change notification settings - Fork 65
/
autofix.go
63 lines (54 loc) · 1.64 KB
/
autofix.go
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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"go.skia.org/infra/go/httputils"
"go.skia.org/infra/go/util"
"go.skia.org/infra/power/go/gatherer"
)
// GetAutoFixCandidates polls the given url (e.g. power.skia.org) for a
// list of down bots and devices. The list will be filtered to only
// those that match this hostname and are not silenced. An error will
// be returned for any of the various steps that could go wrong.
func GetAutoFixCandidates(url string) ([]string, error) {
hostname, err := os.Hostname()
if err != nil {
return nil, fmt.Errorf("Could not get hostname: %s", err)
}
client := httputils.NewTimeoutClient()
resp, err := client.Get(url)
if err != nil {
return nil, fmt.Errorf("Problem with GET: %s", err)
}
defer util.Close(resp.Body)
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("Could not read http response: %s", err)
}
return getMatchingCandidates(body, hostname)
}
type downBotsResponse struct {
List []gatherer.DownBot `json:"list"`
}
// getMatchingCandidates parses the json returned by power-controller/main.go
// It then filters the list to only those that match this hostname and are
// not silenced.
func getMatchingCandidates(response []byte, hostname string) ([]string, error) {
r := downBotsResponse{}
if err := json.Unmarshal(response, &r); err != nil {
return nil, fmt.Errorf("Badly formatted json: %s", err)
}
rv := []string{}
for _, b := range r.List {
if b.HostID == hostname && !b.Silenced {
if b.Status == gatherer.STATUS_DEVICE_MISSING {
rv = append(rv, b.BotID+"-device")
} else {
rv = append(rv, b.BotID)
}
}
}
return rv, nil
}