This repository was archived by the owner on Sep 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcanary.go
More file actions
85 lines (66 loc) · 1.57 KB
/
canary.go
File metadata and controls
85 lines (66 loc) · 1.57 KB
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
package main
import (
"bytes"
"encoding/xml"
"fmt"
"github.com/coreos/core-admin/admin/types"
"io/ioutil"
"net/http"
"net/url"
"os"
)
var cmdCanary = &Command{
UsageLine: "canary [bootid]",
Short: "add a canary to the database",
Long: `
Takes a bootid and adds it to the canary machine list
`,
}
var canaryK = cmdCanary.Flag.String("k", "", "api key for the admin user")
var canaryD = cmdCanary.Flag.Bool("d", false, "dry run, print out the xml payload")
func init() {
cmdCanary.Run = runCanary
}
func newCanaryRequestBody(bootid string) []byte {
cm := types.CanaryMachine{BootId: fmt.Sprintf("{%s}", bootid)}
raw, err := xml.MarshalIndent(cm, "", " ")
if err != nil {
panic(err)
}
body := []byte(xml.Header)
return append(body, raw...)
}
func runCanary(cmd *Command, args []string) {
id := args[0]
dryRun := *canaryD
key := *canaryK
if dryRun == false && key == "" {
panic("key or dry-run required")
}
body := newCanaryRequestBody(id)
adminURL, _ := url.Parse(updateURL.String())
adminURL.Path = "/admin/v1/canaryMachine"
req, _ := http.NewRequest("POST", adminURL.String(), bytes.NewBuffer(body))
req.Header.Set("Content-Type", "text/xml")
req.SetBasicAuth("admin", key)
if dryRun || *debug {
req.Write(os.Stdout)
}
if dryRun {
return
}
client := &http.Client{
Transport: tlsTransport,
}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
body, _ = ioutil.ReadAll(resp.Body)
os.Stdout.Write(body)
fmt.Printf("\n")
if resp.StatusCode != 200 {
panic(fmt.Sprintf("Error: bad return code %s\n", resp.Status))
}
return
}