-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathfleet_client_cli_status.go
146 lines (120 loc) · 2.99 KB
/
fleet_client_cli_status.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
// Parse for `fleet status` output
import (
"bufio"
"fmt"
"io"
"os/exec"
"strings"
)
func (this *ClientCLI) StatusAll() ([]UnitStatus, error) {
cmd := exec.Command(FLEETCTL, ENDPOINT_OPTION, this.etcdPeer, "list-units", "--full=true", "-l=true", "--fields=unit,load,active,sub,machine")
stdout, err := execCmd(cmd)
if err != nil {
return []UnitStatus{}, err
}
return parseFleetStatusOutput(stdout)
}
func parseFleetStatusOutput(output string) ([]UnitStatus, error) {
result := make([]UnitStatus, 0)
scanner := bufio.NewScanner(strings.NewReader(output))
// Scan each line of input.
lineCount := 0
for scanner.Scan() {
line := scanner.Text()
lineCount++
if lineCount == 1 {
continue
}
words := filterEmpty(strings.Split(line, "\t"))
unitStatus := UnitStatus{
Unit: words[0],
Load: words[1],
Active: words[2],
Sub: words[3],
Machine: words[4],
}
result = append(result, unitStatus)
}
if err := scanner.Err(); err != nil {
return result, scanner.Err()
}
return result, nil
}
func (this *ClientCLI) StatusUnit(name string) (UnitStatus, error) {
status, err := this.StatusAll()
if err != nil {
return UnitStatus{}, err
}
for _, s := range status {
if s.Unit == name {
return s, nil
}
}
return UnitStatus{}, NewFleetClientError(ERROR_TYPE_NOT_FOUND, fmt.Sprintf("Cannot fetch status. Unit (%s) not found. Aborting...", name))
}
func (this *ClientCLI) JournalF(name string) (outc, errc chan string, err error) {
var stdout, stderr io.ReadCloser
cmdY := exec.Command("echo", "y")
cmd := exec.Command(FLEETCTL, ENDPOINT_OPTION, this.etcdPeer, "journal", "-f", name)
cmd.Stdin, _ = cmdY.StdoutPipe()
stdout, err = cmd.StdoutPipe()
if err != nil {
return
}
stderr, err = cmd.StderrPipe()
if err != nil {
return
}
if err = cmd.Start(); err != nil {
return
}
cmdY.Run()
outc = make(chan string)
errc = make(chan string)
go func() {
scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
outc <- scanner.Text()
}
}()
go func() {
scanner := bufio.NewScanner(stderr)
for scanner.Scan() {
errc <- scanner.Text()
}
}()
return
}
func (this *ClientCLI) MachineAll() ([]MachineStatus, error) {
cmd := exec.Command(FLEETCTL, ENDPOINT_OPTION, this.etcdPeer, "list-machines", "--full=true")
stdout, err := execCmd(cmd)
if err != nil {
return []MachineStatus{}, err
}
return parseMachineStatusOutput(stdout)
}
func parseMachineStatusOutput(output string) ([]MachineStatus, error) {
result := make([]MachineStatus, 0)
scanner := bufio.NewScanner(strings.NewReader(output))
// Scan each line of input.
lineCount := 0
for scanner.Scan() {
line := scanner.Text()
lineCount++
if lineCount == 1 {
continue
}
words := filterEmpty(strings.Split(line, "\t"))
unitStatus := MachineStatus{
Machine: words[0],
IPAddress: words[1],
Metadata: words[2],
}
result = append(result, unitStatus)
}
if err := scanner.Err(); err != nil {
return result, scanner.Err()
}
return result, nil
}