/
info.go
95 lines (80 loc) · 2.25 KB
/
info.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
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
package main
import (
"encoding/json"
"flag"
"os"
"sync"
"time"
"github.com/couchbaselabs/cbfs/client"
"github.com/couchbaselabs/cbfs/config"
"github.com/couchbaselabs/cbfs/tools"
)
type Tasks map[string]map[string]struct {
State string
TS time.Time
}
type Backup struct {
Filename string
OID string
When time.Time
Conf cbfsconfig.CBFSConfig
}
type Nodes map[string]cbfsclient.StorageNode
var infoFlags = flag.NewFlagSet("info", flag.ExitOnError)
var infoTemplate = infoFlags.String("t", "", "Display template")
var infoTemplateFile = infoFlags.String("T", "", "Display template filename")
var infoJSON = infoFlags.Bool("json", false, "Dump as json")
const defaultInfoTemplate = `nodes:
{{ range $name, $info := .Nodes }} {{$name}} {{$info.Version}} up {{$info.UptimeStr}} (age: {{$info.HBAgeStr}})
{{ end }}
{{if .Tasks}}tasks:{{end}}{{ range $node, $tasks := .Tasks }}
{{$node}}
{{ range $task, $info := $tasks }} {{$task}} - {{$info.State}} - {{$info.TS}}
{{end}}{{end}}
backups:
Found {{len .Backups.Previous }} backups.
Current: {{.Backups.Latest.Filename}} ({{.Backups.Latest.OID}})
as of {{.Backups.Latest.When}}
config:
{{ range $k, $v := .Conf.ToMap}} {{$k}}: {{$v}}
{{end}}
`
func infoCommand(base string, args []string) {
tmpl := cbfstool.GetTemplate(*infoTemplate, *infoTemplateFile,
defaultInfoTemplate)
result := struct {
Nodes Nodes
Tasks Tasks
Backups struct {
Previous []Backup `json:"backups"`
Latest Backup
}
Conf cbfsconfig.CBFSConfig
}{}
todo := map[string]interface{}{
"/.cbfs/nodes/": &result.Nodes,
"/.cbfs/tasks/": &result.Tasks,
"/.cbfs/backup/": &result.Backups,
"/.cbfs/config/": &result.Conf,
}
u := cbfstool.ParseURL(base)
wg := sync.WaitGroup{}
for k, v := range todo {
u.Path = k
wg.Add(1)
go func(s string, to interface{}) {
defer wg.Done()
err := cbfstool.GetJsonData(s, to)
cbfstool.MaybeFatal(err, "Error getting node info: %v", err)
}(u.String(), v)
}
wg.Wait()
if *infoJSON {
data, err := json.MarshalIndent(result, "", " ")
cbfstool.MaybeFatal(err, "Error marshaling rseult: %v", err)
os.Stdout.Write(data)
} else {
err := tmpl.Execute(os.Stdout, result)
cbfstool.MaybeFatal(err, "Error executing template: %v", err)
}
}