Skip to content

Commit c9c3055

Browse files
Host Run Collector (#606)
Host Run Collector
1 parent 601bf75 commit c9c3055

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

pkg/apis/troubleshoot/v1beta2/hostcollector_shared.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@ type HostServices struct {
150150
HostCollectorMeta `json:",inline" yaml:",inline"`
151151
}
152152

153+
type HostRun struct {
154+
HostCollectorMeta `json:",inline" yaml:",inline"`
155+
Command string `json:"command"`
156+
Args []string `json:"args"`
157+
}
158+
153159
type HostCollect struct {
154160
CPU *CPU `json:"cpu,omitempty" yaml:"cpu,omitempty"`
155161
Memory *Memory `json:"memory,omitempty" yaml:"memory,omitempty"`
@@ -169,6 +175,7 @@ type HostCollect struct {
169175
Certificate *Certificate `json:"certificate,omitempty" yaml:"certificate,omitempty"`
170176
HostServices *HostServices `json:"hostServices,omitempty" yaml:"hostServices,omitempty"`
171177
HostOS *HostOS `json:"hostOS,omitempty" yaml:"hostOS,omitempty"`
178+
HostRun *HostRun `json:"run,omitempty" yaml:"run,omitempty"`
172179
}
173180

174181
func (c *HostCollect) GetName() string {

pkg/collect/host_collector.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ func GetHostCollector(collector *troubleshootv1beta2.HostCollect, bundlePath str
5151
return &CollectHostServices{collector.HostServices, bundlePath}, true
5252
case collector.HostOS != nil:
5353
return &CollectHostOS{collector.HostOS, bundlePath}, true
54+
case collector.HostRun != nil:
55+
return &CollectHostRun{collector.HostRun, bundlePath}, true
5456
default:
5557
return nil, false
5658
}

pkg/collect/host_run.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package collect
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"os/exec"
7+
"path/filepath"
8+
"strings"
9+
10+
"github.com/pkg/errors"
11+
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
12+
)
13+
14+
type HostRunInfo struct {
15+
Command string `json:"command"`
16+
ExitCode string `json:"exitCode"`
17+
Error string `json:"error"`
18+
}
19+
20+
type CollectHostRun struct {
21+
hostCollector *troubleshootv1beta2.HostRun
22+
BundlePath string
23+
}
24+
25+
func (c *CollectHostRun) Title() string {
26+
return hostCollectorTitleOrDefault(c.hostCollector.HostCollectorMeta, "Run Host")
27+
}
28+
29+
func (c *CollectHostRun) IsExcluded() (bool, error) {
30+
return isExcluded(c.hostCollector.Exclude)
31+
}
32+
33+
func (c *CollectHostRun) Collect(progressChan chan<- interface{}) (map[string][]byte, error) {
34+
runHostCollector := c.hostCollector
35+
36+
cmd := exec.Command(runHostCollector.Command, runHostCollector.Args...)
37+
38+
var stdout, stderr bytes.Buffer
39+
cmd.Stdout = &stdout
40+
cmd.Stderr = &stderr
41+
42+
runInfo := HostRunInfo{
43+
Command: cmd.String(),
44+
ExitCode: "0",
45+
}
46+
47+
err := cmd.Run()
48+
if err != nil {
49+
if werr, ok := err.(*exec.ExitError); ok {
50+
runInfo.ExitCode = strings.TrimPrefix(werr.Error(), "exit status ")
51+
runInfo.Error = stderr.String()
52+
} else {
53+
return nil, errors.Wrap(err, "failed to run")
54+
}
55+
}
56+
57+
collectorName := c.hostCollector.CollectorName
58+
if collectorName == "" {
59+
collectorName = "run-host"
60+
}
61+
resultInfo := filepath.Join("host-collectors/run-host", collectorName+"-info.json")
62+
result := filepath.Join("host-collectors/run-host", collectorName+".txt")
63+
64+
b, err := json.Marshal(runInfo)
65+
if err != nil {
66+
return nil, errors.Wrap(err, "failed to marshal run host result")
67+
}
68+
69+
output := NewResult()
70+
output.SaveResult(c.BundlePath, resultInfo, bytes.NewBuffer(b))
71+
output.SaveResult(c.BundlePath, result, bytes.NewBuffer(stdout.Bytes()))
72+
73+
runHostOutput := map[string][]byte{
74+
resultInfo: b,
75+
result: stdout.Bytes(),
76+
}
77+
78+
return runHostOutput, nil
79+
}

0 commit comments

Comments
 (0)