Skip to content

Commit

Permalink
feat: handle config file
Browse files Browse the repository at this point in the history
new optional field config in hostRun spec
  • Loading branch information
York Chen committed Aug 4, 2023
1 parent 98a9b61 commit 9900384
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
7 changes: 4 additions & 3 deletions pkg/apis/troubleshoot/v1beta2/hostcollector_shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,10 @@ type HostServices struct {

type HostRun struct {
HostCollectorMeta `json:",inline" yaml:",inline"`
Command string `json:"command"`
Args []string `json:"args"`
OutputDir string `json:"outputDir" yaml:"outputDir"`
Command string `json:"command"`
Args []string `json:"args"`
OutputDir string `json:"outputDir" yaml:"outputDir"`
Config map[string]string `json:"config" yaml:"config"`
}

type HostCollect struct {
Expand Down
40 changes: 32 additions & 8 deletions pkg/collect/host_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"gopkg.in/yaml.v2"
"os"
"os/exec"
"path"
Expand All @@ -19,6 +20,7 @@ type HostRunInfo struct {
ExitCode string `json:"exitCode"`
Error string `json:"error"`
OutputDir string `json:"outputDir"`
Config string `json:"config"`
}

type CollectHostRun struct {
Expand All @@ -40,29 +42,53 @@ func (c *CollectHostRun) Collect(progressChan chan<- interface{}) (map[string][]
if collectorName == "" {
collectorName = "run-host"
}
var bundleOutputPath string
var (
bundleOutputPath string
bundleOutputRelativePath string
cmdConfigFilePath string
cmdConfigFileRelativePath string
)

cmd := exec.Command(runHostCollector.Command, runHostCollector.Args...)

cmd.Env = append(os.Environ(),
fmt.Sprintf("BUNDLE_PATH=%s", c.BundlePath),
)
collectorRelativePath := filepath.Join("host-collectors/run-host", collectorName)

// if we choose to save result for the command run
if runHostCollector.OutputDir != "" {
bundleOutputPath = path.Join(c.BundlePath, "host-collectors/run-host", collectorName, runHostCollector.OutputDir)
bundleOutputRelativePath = filepath.Join(collectorRelativePath, runHostCollector.OutputDir)
bundleOutputPath = path.Join(c.BundlePath, bundleOutputRelativePath)
cmd.Env = append(cmd.Env,
fmt.Sprintf("WORKSPACE_DIR=%s", bundleOutputPath),
)
}

output := NewResult()

if runHostCollector.Config != nil {
cmdConfigFileRelativePath = filepath.Join(collectorRelativePath, "command-config.yaml")
cmdConfigFilePath = path.Join(c.BundlePath, cmdConfigFileRelativePath)
configBytes, err := yaml.Marshal(runHostCollector.Config)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("failed to marshal the command config in collector: %s", collectorName))
}
output.SaveResult(c.BundlePath, cmdConfigFileRelativePath, bytes.NewBuffer(configBytes))
cmd.Env = append(cmd.Env,
fmt.Sprintf("CONFIG=%s", cmdConfigFilePath),
)
}

var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr

runInfo := HostRunInfo{
Command: cmd.String(),
ExitCode: "0",
OutputDir: bundleOutputPath,
OutputDir: bundleOutputRelativePath,
Config: cmdConfigFilePath,
}

err := cmd.Run()
Expand All @@ -75,21 +101,19 @@ func (c *CollectHostRun) Collect(progressChan chan<- interface{}) (map[string][]
}
}

resultInfo := filepath.Join("host-collectors/run-host", collectorName, "collector-info.json")
result := filepath.Join("host-collectors/run-host", collectorName, "collector.txt")
resultInfo := filepath.Join(collectorRelativePath, "collector-info.json")
result := filepath.Join(collectorRelativePath, "collector.txt")

b, err := json.Marshal(runInfo)
if err != nil {
return nil, errors.Wrap(err, "failed to marshal run host result")
}

output := NewResult()
output.SaveResult(c.BundlePath, resultInfo, bytes.NewBuffer(b))
output.SaveResult(c.BundlePath, result, bytes.NewBuffer(stdout.Bytes()))
// walkthrough the output directory and save result for each file
if runHostCollector.OutputDir != "" {
output.SaveResults(c.BundlePath,
filepath.Join("host-collectors/run-host", collectorName, runHostCollector.OutputDir))
output.SaveResults(c.BundlePath, bundleOutputRelativePath)
}

return output, nil
Expand Down

0 comments on commit 9900384

Please sign in to comment.