Skip to content

Commit

Permalink
Merge pull request #334 from replicatedhq/host-preflgihts-ux
Browse files Browse the repository at this point in the history
Host preflight ux improvements
  • Loading branch information
emosbaugh authored Mar 2, 2021
2 parents bdf843f + 4b78c43 commit 7c4135d
Show file tree
Hide file tree
Showing 48 changed files with 640 additions and 357 deletions.
5 changes: 5 additions & 0 deletions cmd/preflight/cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func runPreflights(v *viper.Viper, arg string) error {

s := spin.New()
go func() {
lastMsg := ""
for {
select {
case msg, ok := <-progressCh:
Expand All @@ -104,6 +105,10 @@ func runPreflights(v *viper.Viper, arg string) error {
c := color.New(color.FgHiRed)
c.Println(fmt.Sprintf("%s\r * %v", cursor.ClearEntireLine(), msg))
case string:
if lastMsg == msg {
break
}
lastMsg = msg
c := color.New(color.FgCyan)
c.Println(fmt.Sprintf("%s\r * %s", cursor.ClearEntireLine(), msg))
}
Expand Down
111 changes: 21 additions & 90 deletions pkg/analyze/analyzer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package analyzer

import (
"fmt"
"strconv"

"github.com/pkg/errors"
Expand Down Expand Up @@ -40,100 +41,30 @@ func isExcluded(excludeVal multitype.BoolOrString) (bool, error) {
return parsed, nil
}

func HostAnalyze(hostAnalyzer *troubleshootv1beta2.HostAnalyze, getFile getCollectedFileContents, findFiles getChildCollectedFileContents) ([]*AnalyzeResult, error) {
if hostAnalyzer.CPU != nil {
result, err := analyzeHostCPU(hostAnalyzer.CPU, getFile)
if err != nil {
return nil, err
}
return []*AnalyzeResult{result}, nil
}
if hostAnalyzer.TCPLoadBalancer != nil {
result, err := analyzeHostTCPLoadBalancer(hostAnalyzer.TCPLoadBalancer, getFile)
if err != nil {
return nil, err
}
return []*AnalyzeResult{result}, nil
}
if hostAnalyzer.HTTPLoadBalancer != nil {
result, err := analyzeHostHTTPLoadBalancer(hostAnalyzer.HTTPLoadBalancer, getFile)
if err != nil {
return nil, err
}
return []*AnalyzeResult{result}, nil
}
if hostAnalyzer.DiskUsage != nil {
result, err := analyzeHostDiskUsage(hostAnalyzer.DiskUsage, getFile)
if err != nil {
return nil, err
}
return []*AnalyzeResult{result}, nil
}
if hostAnalyzer.Memory != nil {
result, err := analyzeHostMemory(hostAnalyzer.Memory, getFile)
if err != nil {
return nil, err
}
return []*AnalyzeResult{result}, nil
}
if hostAnalyzer.TCPPortStatus != nil {
result, err := analyzeHostTCPPortStatus(hostAnalyzer.TCPPortStatus, getFile)
if err != nil {
return nil, err
}
return []*AnalyzeResult{result}, nil
}
if hostAnalyzer.HTTP != nil {
result, err := analyzeHostHTTP(hostAnalyzer.HTTP, getFile)
if err != nil {
return nil, err
}
return []*AnalyzeResult{result}, nil
func HostAnalyze(hostAnalyzer *troubleshootv1beta2.HostAnalyze, getFile getCollectedFileContents, findFiles getChildCollectedFileContents) []*AnalyzeResult {
analyzer, ok := GetHostAnalyzer(hostAnalyzer)
if !ok {
return NewAnalyzeResultError(analyzer, errors.New("invalid analyzer"))
}
if hostAnalyzer.Time != nil {
result, err := analyzeHostTime(hostAnalyzer.Time, getFile)
if err != nil {
return nil, err
}
return []*AnalyzeResult{result}, nil
}
if hostAnalyzer.BlockDevices != nil {
result, err := analyzeHostBlockDevices(hostAnalyzer.BlockDevices, getFile)
if err != nil {
return nil, err
}
return []*AnalyzeResult{result}, nil
}
if hostAnalyzer.TCPConnect != nil {
result, err := analyzeHostTCPConnect(hostAnalyzer.TCPConnect, getFile)
if err != nil {
return nil, err
}
return []*AnalyzeResult{result}, nil
}
if hostAnalyzer.IPV4Interfaces != nil {
result, err := analyzeHostIPV4Interfaces(hostAnalyzer.IPV4Interfaces, getFile)
if err != nil {
return nil, err
}
return []*AnalyzeResult{result}, nil
}
if hostAnalyzer.FilesystemPerformance != nil {
result, err := analyzeHostFilesystemPerformance(hostAnalyzer.FilesystemPerformance, getFile)
if err != nil {
return nil, err
}
return []*AnalyzeResult{result}, nil

isExcluded, _ := analyzer.IsExcluded()
if isExcluded {
return nil
}
if hostAnalyzer.Certificate != nil {
result, err := analyzeHostCertificate(hostAnalyzer.Certificate, getFile)
if err != nil {
return nil, err
}
return []*AnalyzeResult{result}, nil

result, err := analyzer.Analyze(getFile)
if err != nil {
return NewAnalyzeResultError(analyzer, errors.Wrap(err, "analyze"))
}
return []*AnalyzeResult{result}
}

return nil, errors.New("invalid analyzer")
func NewAnalyzeResultError(analyzer HostAnalyzer, err error) []*AnalyzeResult {
return []*AnalyzeResult{{
IsFail: true,
Title: analyzer.Title(),
Message: fmt.Sprintf("Analyzer Failed: %v", err),
}}
}

func Analyze(analyzer *troubleshootv1beta2.Analyze, getFile getCollectedFileContents, findFiles getChildCollectedFileContents) ([]*AnalyzeResult, error) {
Expand Down
49 changes: 49 additions & 0 deletions pkg/analyze/host_analyzer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package analyzer

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

type HostAnalyzer interface {
Title() string
IsExcluded() (bool, error)
Analyze(getFile func(string) ([]byte, error)) (*AnalyzeResult, error)
}

func GetHostAnalyzer(analyzer *troubleshootv1beta2.HostAnalyze) (HostAnalyzer, bool) {
switch {
case analyzer.CPU != nil:
return &AnalyzeHostCPU{analyzer.CPU}, true
case analyzer.Memory != nil:
return &AnalyzeHostMemory{analyzer.Memory}, true
case analyzer.TCPLoadBalancer != nil:
return &AnalyzeHostTCPLoadBalancer{analyzer.TCPLoadBalancer}, true
case analyzer.HTTPLoadBalancer != nil:
return &AnalyzeHostHTTPLoadBalancer{analyzer.HTTPLoadBalancer}, true
case analyzer.DiskUsage != nil:
return &AnalyzeHostDiskUsage{analyzer.DiskUsage}, true
case analyzer.TCPPortStatus != nil:
return &AnalyzeHostTCPPortStatus{analyzer.TCPPortStatus}, true
case analyzer.HTTP != nil:
return &AnalyzeHostHTTP{analyzer.HTTP}, true
case analyzer.Time != nil:
return &AnalyzeHostTime{analyzer.Time}, true
case analyzer.BlockDevices != nil:
return &AnalyzeHostBlockDevices{analyzer.BlockDevices}, true
case analyzer.TCPConnect != nil:
return &AnalyzeHostTCPConnect{analyzer.TCPConnect}, true
case analyzer.IPV4Interfaces != nil:
return &AnalyzeHostIPV4Interfaces{analyzer.IPV4Interfaces}, true
case analyzer.FilesystemPerformance != nil:
return &AnalyzeHostFilesystemPerformance{analyzer.FilesystemPerformance}, true
case analyzer.Certificate != nil:
return &AnalyzeHostCertificate{analyzer.Certificate}, true
default:
return nil, false
}
}

func hostAnalyzerTitleOrDefault(meta troubleshootv1beta2.AnalyzeMeta, defaultTitle string) string {
if meta.CheckName != "" {
return meta.CheckName
}
return defaultTitle
}
22 changes: 16 additions & 6 deletions pkg/analyze/host_block_devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,21 @@ import (
"github.com/replicatedhq/troubleshoot/pkg/collect"
)

func analyzeHostBlockDevices(hostAnalyzer *troubleshootv1beta2.BlockDevicesAnalyze, getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
type AnalyzeHostBlockDevices struct {
hostAnalyzer *troubleshootv1beta2.BlockDevicesAnalyze
}

func (a *AnalyzeHostBlockDevices) Title() string {
return hostAnalyzerTitleOrDefault(a.hostAnalyzer.AnalyzeMeta, "Block Devices")
}

func (a *AnalyzeHostBlockDevices) IsExcluded() (bool, error) {
return isExcluded(a.hostAnalyzer.Exclude)
}

func (a *AnalyzeHostBlockDevices) Analyze(getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
hostAnalyzer := a.hostAnalyzer

contents, err := getCollectedFileContents("system/block_devices.json")
if err != nil {
return nil, errors.Wrap(err, "failed to get collected file")
Expand All @@ -25,11 +39,7 @@ func analyzeHostBlockDevices(hostAnalyzer *troubleshootv1beta2.BlockDevicesAnaly

result := AnalyzeResult{}

title := hostAnalyzer.CheckName
if title == "" {
title = "Block Devices"
}
result.Title = title
result.Title = a.Title()

for _, outcome := range hostAnalyzer.Outcomes {
if outcome.Fail != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/analyze/host_block_devices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func TestAnalyzeBlockDevices(t *testing.T) {
return b, nil
}

result, err := analyzeHostBlockDevices(test.hostAnalyzer, getCollectedFileContents)
result, err := (&AnalyzeHostBlockDevices{test.hostAnalyzer}).Analyze(getCollectedFileContents)
if test.expectErr {
req.Error(err)
} else {
Expand Down
22 changes: 16 additions & 6 deletions pkg/analyze/host_certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,21 @@ import (
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
)

func analyzeHostCertificate(hostAnalyzer *troubleshootv1beta2.CertificateAnalyze, getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
type AnalyzeHostCertificate struct {
hostAnalyzer *troubleshootv1beta2.CertificateAnalyze
}

func (a *AnalyzeHostCertificate) Title() string {
return hostAnalyzerTitleOrDefault(a.hostAnalyzer.AnalyzeMeta, "Certificate Key Pair")
}

func (a *AnalyzeHostCertificate) IsExcluded() (bool, error) {
return isExcluded(a.hostAnalyzer.Exclude)
}

func (a *AnalyzeHostCertificate) Analyze(getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
hostAnalyzer := a.hostAnalyzer

collectorName := hostAnalyzer.CollectorName
if collectorName == "" {
collectorName = "certificate"
Expand All @@ -21,11 +35,7 @@ func analyzeHostCertificate(hostAnalyzer *troubleshootv1beta2.CertificateAnalyze

result := AnalyzeResult{}

title := hostAnalyzer.CheckName
if title == "" {
title = "Certificate Key Pair"
}
result.Title = title
result.Title = a.Title()

for _, outcome := range hostAnalyzer.Outcomes {
if outcome.Fail != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/analyze/host_certificate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ func TestAnalyzeCertificate(t *testing.T) {
return []byte(test.status), nil
}

result, err := analyzeHostCertificate(test.hostAnalyzer, getCollectedFileContents)
result, err := (&AnalyzeHostCertificate{test.hostAnalyzer}).Analyze(getCollectedFileContents)
if test.expectErr {
req.Error(err)
} else {
Expand Down
22 changes: 16 additions & 6 deletions pkg/analyze/host_cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,21 @@ import (
"github.com/replicatedhq/troubleshoot/pkg/collect"
)

func analyzeHostCPU(hostAnalyzer *troubleshootv1beta2.CPUAnalyze, getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
type AnalyzeHostCPU struct {
hostAnalyzer *troubleshootv1beta2.CPUAnalyze
}

func (a *AnalyzeHostCPU) Title() string {
return hostAnalyzerTitleOrDefault(a.hostAnalyzer.AnalyzeMeta, "Number of CPUs")
}

func (a *AnalyzeHostCPU) IsExcluded() (bool, error) {
return isExcluded(a.hostAnalyzer.Exclude)
}

func (a *AnalyzeHostCPU) Analyze(getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
hostAnalyzer := a.hostAnalyzer

contents, err := getCollectedFileContents("system/cpu.json")
if err != nil {
return nil, errors.Wrap(err, "failed to get collected file")
Expand All @@ -23,11 +37,7 @@ func analyzeHostCPU(hostAnalyzer *troubleshootv1beta2.CPUAnalyze, getCollectedFi

result := AnalyzeResult{}

title := hostAnalyzer.CheckName
if title == "" {
title = "Number of CPUs"
}
result.Title = title
result.Title = a.Title()

for _, outcome := range hostAnalyzer.Outcomes {
if outcome.Fail != nil {
Expand Down
22 changes: 16 additions & 6 deletions pkg/analyze/host_disk_usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,21 @@ import (
"k8s.io/apimachinery/pkg/api/resource"
)

func analyzeHostDiskUsage(hostAnalyzer *troubleshootv1beta2.DiskUsageAnalyze, getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
type AnalyzeHostDiskUsage struct {
hostAnalyzer *troubleshootv1beta2.DiskUsageAnalyze
}

func (a *AnalyzeHostDiskUsage) Title() string {
return hostAnalyzerTitleOrDefault(a.hostAnalyzer.AnalyzeMeta, fmt.Sprintf("Disk Usage %s", a.hostAnalyzer.CollectorName))
}

func (a *AnalyzeHostDiskUsage) IsExcluded() (bool, error) {
return isExcluded(a.hostAnalyzer.Exclude)
}

func (a *AnalyzeHostDiskUsage) Analyze(getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
hostAnalyzer := a.hostAnalyzer

key := collect.HostDiskUsageKey(hostAnalyzer.CollectorName)
contents, err := getCollectedFileContents(key)
if err != nil {
Expand All @@ -26,11 +40,7 @@ func analyzeHostDiskUsage(hostAnalyzer *troubleshootv1beta2.DiskUsageAnalyze, ge

result := AnalyzeResult{}

title := hostAnalyzer.CheckName
if title == "" {
title = fmt.Sprintf("Disk Usage %s", hostAnalyzer.CollectorName)
}
result.Title = title
result.Title = a.Title()

for _, outcome := range hostAnalyzer.Outcomes {
if outcome.Fail != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/analyze/host_disk_usage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ func TestAnalyzeHostDiskUsage(t *testing.T) {
return b, nil
}

result, err := analyzeHostDiskUsage(test.hostAnalyzer, getCollectedFileContents)
result, err := (&AnalyzeHostDiskUsage{test.hostAnalyzer}).Analyze(getCollectedFileContents)
if test.expectErr {
req.Error(err)
} else {
Expand Down
Loading

0 comments on commit 7c4135d

Please sign in to comment.