Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.19

require (
github.com/ahmetalpbalkan/go-cursor v0.0.0-20131010032410-8136607ea412
github.com/arduino/go-apt-client v0.0.0-20190812130613-5613f843fdc8
github.com/blang/semver v3.5.1+incompatible
github.com/containers/image/v5 v5.23.1
github.com/docker/distribution v2.8.1+incompatible
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ github.com/alexflint/go-filemutex v0.0.0-20171022225611-72bdc8eae2ae/go.mod h1:C
github.com/andybalholm/brotli v1.0.1 h1:KqhlKozYbRtJvsPrrEeXcO+N2l6NYT5A2QAFmSULpEc=
github.com/andybalholm/brotli v1.0.1/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/arduino/go-apt-client v0.0.0-20190812130613-5613f843fdc8 h1:HpmeqWCUoU+dPrz8V4KGDMDxvR+WyeJ0g6DSSqnptuY=
github.com/arduino/go-apt-client v0.0.0-20190812130613-5613f843fdc8/go.mod h1:U1gYCDLM1Kg0dG0PxUjlT09+l6/TdUZKx0FQ2CocJUU=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY=
Expand Down
6 changes: 6 additions & 0 deletions pkg/analyze/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ func HostAnalyze(hostAnalyzer *troubleshootv1beta2.HostAnalyze, getFile getColle
return NewAnalyzeResultError(analyzer, errors.New("invalid host analyzer"))
}

if tplanalyzer, ok := analyzer.(Templated); ok {
if err := tplanalyzer.ProcessTemplate(getFile); err != nil {
return NewAnalyzeResultError(analyzer, err)
}
}

isExcluded, _ := analyzer.IsExcluded()
if isExcluded {
return nil
Expand Down
6 changes: 6 additions & 0 deletions pkg/analyze/host_analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package analyzer

import troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"

type Templated interface {
ProcessTemplate(getFile func(string) ([]byte, error)) error
}

type HostAnalyzer interface {
Title() string
IsExcluded() (bool, error)
Expand Down Expand Up @@ -44,6 +48,8 @@ func GetHostAnalyzer(analyzer *troubleshootv1beta2.HostAnalyze) (HostAnalyzer, b
return &AnalyzeHostServices{analyzer.HostServices}, true
case analyzer.HostOS != nil:
return &AnalyzeHostOS{analyzer.HostOS}, true
case analyzer.InstalledPackage != nil:
return &AnalyzeInstalledPackage{analyzer.InstalledPackage}, true
default:
return nil, false
}
Expand Down
167 changes: 167 additions & 0 deletions pkg/analyze/host_installed_packages.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
package analyzer

import (
"bytes"
"encoding/json"
"fmt"
"path"
"text/template"

"github.com/hashicorp/go-multierror"

"github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
"github.com/replicatedhq/troubleshoot/pkg/collect"
)

type AnalyzeInstalledPackage struct {
hanalyzer *v1beta2.InstalledPackageAnalyze
}

func (a *AnalyzeInstalledPackage) Title() string {
return hostAnalyzerTitleOrDefault(a.hanalyzer.AnalyzeMeta, "Host Package")
}

// ProcessTemplate parses the content collect by the collector an then applies the result in the 'exclude'
// property from the analyzer.
func (a *AnalyzeInstalledPackage) ProcessTemplate(getFileContents func(string) ([]byte, error)) error {
fullPath := path.Join("host-collectors", "hostPackages", "hostPackages.json")
if a.hanalyzer.CollectorName != "" {
fname := fmt.Sprintf("%s.json", a.hanalyzer.CollectorName)
fullPath = path.Join("host-collectors", "hostPackages", fname)
}

data, err := getFileContents(fullPath)
if err != nil {
return fmt.Errorf("failed to read collector content: %w", err)
}

var info collect.HostInfo
if err := json.Unmarshal(data, &info); err != nil {
return fmt.Errorf("failed to unmarshal collector content: %w", err)
}

tpl := template.New("template")
tpl, err = tpl.Parse(a.hanalyzer.AnalyzeMeta.Exclude.StrVal)
if err != nil {
return fmt.Errorf("failed to parse exclude template: %w", err)
}

buf := bytes.NewBuffer(nil)
if err := tpl.Execute(buf, info.OSInfo); err != nil {
return fmt.Errorf("failed to execute exclude template: %w", err)
}

a.hanalyzer.AnalyzeMeta.Exclude.StrVal = buf.String()
return nil
}

func (a *AnalyzeInstalledPackage) IsExcluded() (bool, error) {
return isExcluded(a.hanalyzer.Exclude)
}

func (a *AnalyzeInstalledPackage) Analyze(getFileContents func(string) ([]byte, error)) ([]*AnalyzeResult, error) {
fullPath := path.Join("host-collectors", "hostPackages", "hostPackages.json")
if a.hanalyzer.CollectorName != "" {
fname := fmt.Sprintf("%s.json", a.hanalyzer.CollectorName)
fullPath = path.Join("host-collectors", "hostPackages", fname)
}

data, err := getFileContents(fullPath)
if err != nil {
return nil, fmt.Errorf("failed to read collected file name %s: %w", fullPath, err)
}

var ospkgs collect.HostInfo
if err := json.Unmarshal(data, &ospkgs); err != nil {
return nil, fmt.Errorf("failed to unmarshal collected data: %w", err)
}

pkg := ospkgs.PackageByName(a.hanalyzer.PackageName)
if pkg == nil {
res := &AnalyzeResult{
IsFail: true,
Title: fmt.Sprintf("Package %s not installed", a.hanalyzer.PackageName),
Message: fmt.Sprintf("Package %s was not found in the system", a.hanalyzer.PackageName),
}
return []*AnalyzeResult{res}, nil
}

// if no outcome has been provided then we are only checking if the package has been
// installed. on this case just return an ok.
if len(a.hanalyzer.Outcomes) == 0 {
res := &AnalyzeResult{
IsPass: true,
Title: fmt.Sprintf("Package %s installed", pkg.Name),
Message: fmt.Sprintf("Package %s is installed (version %s)", pkg.Name, pkg.Version),
}
return []*AnalyzeResult{res}, nil
}

res, err := a.validateOutcomes(pkg)
if err != nil {
return nil, fmt.Errorf("failed to analyze outcomes: %w", err)
}
return []*AnalyzeResult{res}, nil
}

func (a *AnalyzeInstalledPackage) prepareResult(outcome *v1beta2.Outcome) (*AnalyzeResult, string) {
title := a.hanalyzer.CheckName
if title == "" {
title = fmt.Sprintf("Package %s required version", a.hanalyzer.PackageName)
}
result := &AnalyzeResult{Title: title}

if outcome.Fail != nil {
result.IsFail = true
result.Message = outcome.Fail.Message
result.URI = outcome.Fail.URI
return result, outcome.Fail.When
}

if outcome.Warn != nil {
result.IsWarn = true
result.Message = outcome.Warn.Message
result.URI = outcome.Warn.URI
return result, outcome.Warn.When
}

if outcome.Pass != nil {
result.IsPass = true
result.Message = outcome.Pass.Message
result.URI = outcome.Pass.URI
return result, outcome.Pass.When
}

return nil, ""
}

func (a *AnalyzeInstalledPackage) validateOutcomes(pkg *collect.HostInstalledPackage) (*AnalyzeResult, error) {
for _, outcome := range a.hanalyzer.Outcomes {
result, when := a.prepareResult(outcome)
if result == nil {
return nil, fmt.Errorf("empty outcome")
} else if when == "" {
return result, nil
}

// if something went wrong when evaluating if the package is within the semantic
// version range then or the range is not valid or the package does not use semantic
// versions at all, on this case we move on and try to validate using regex.
var errs *multierror.Error
if matches, err := pkg.InRange(when); err != nil {
errs = multierror.Append(errs, err)
} else if matches {
return result, nil
} else {
continue
}

if matches, err := pkg.MatchesRegex(when); err != nil {
errs = multierror.Append(errs, err)
return nil, errs
} else if matches {
return result, nil
}
}
return &AnalyzeResult{}, nil
}
9 changes: 9 additions & 0 deletions pkg/apis/troubleshoot/v1beta2/hostanalyzer_shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ type TCPPortStatusAnalyze struct {
Outcomes []*Outcome `json:"outcomes" yaml:"outcomes"`
}

type InstalledPackageAnalyze struct {
AnalyzeMeta `json:",inline" yaml:",inline"`
CollectorName string `json:"collectorName,omitempty" yaml:"collectorName,omitempty"`
PackageName string `json:"packageName"`
Outcomes []*Outcome `json:"outcomes" yaml:"outcomes"`
}

type DiskUsageAnalyze struct {
AnalyzeMeta `json:",inline" yaml:",inline"`
CollectorName string `json:"collectorName,omitempty" yaml:"collectorName,omitempty"`
Expand Down Expand Up @@ -136,4 +143,6 @@ type HostAnalyze struct {
HostServices *HostServicesAnalyze `json:"hostServices,omitempty" yaml:"hostServices,omitempty"`

HostOS *HostOSAnalyze `json:"hostOS,omitempty" yaml:"hostOS,omitempty"`

InstalledPackage *InstalledPackageAnalyze `json:"installedPackage,omitempty" yaml:"installedPackage,omitempty"`
}
5 changes: 5 additions & 0 deletions pkg/apis/troubleshoot/v1beta2/hostcollector_shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ type HTTPLoadBalancer struct {
Timeout string `json:"timeout,omitempty"`
}

type InstalledPackages struct {
HostCollectorMeta `json:",inline" yaml:",inline"`
}

type TCPPortStatus struct {
HostCollectorMeta `json:",inline" yaml:",inline"`
Interface string `json:"interface,omitempty"`
Expand Down Expand Up @@ -176,6 +180,7 @@ type HostCollect struct {
HostServices *HostServices `json:"hostServices,omitempty" yaml:"hostServices,omitempty"`
HostOS *HostOS `json:"hostOS,omitempty" yaml:"hostOS,omitempty"`
HostRun *HostRun `json:"run,omitempty" yaml:"run,omitempty"`
InstalledPackages *InstalledPackages `json:"installedPackages,omitempty" yaml:"installedPackages,omitempty"`
}

func (c *HostCollect) GetName() string {
Expand Down
42 changes: 42 additions & 0 deletions pkg/apis/troubleshoot/v1beta2/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pkg/collect/host_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ func GetHostCollector(collector *troubleshootv1beta2.HostCollect, bundlePath str
return &CollectHostOS{collector.HostOS, bundlePath}, true
case collector.HostRun != nil:
return &CollectHostRun{collector.HostRun, bundlePath}, true
case collector.InstalledPackages != nil:
return &CollectInstalledPackages{collector.InstalledPackages, bundlePath}, true
default:
return nil, false
}
Expand Down
Loading