Skip to content

Commit

Permalink
Merge pull request #14 from adyatlov/remove_check_command
Browse files Browse the repository at this point in the history
Remove boilerplate code. Remove check command.
  • Loading branch information
adyatlov authored Sep 9, 2018
2 parents 658bdae + e3999a0 commit 5467375
Show file tree
Hide file tree
Showing 22 changed files with 778 additions and 693 deletions.
29 changes: 17 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,29 @@ $ go get github.com/adyatlov/bun/bun
$ mkdir bundle
$ unzip bundle.zip -d bundle
$ cd bundle
bundle$ bun check
PROBLEM: dcos-version - Versions are different
Details:
master 172.20.0.22 has DC/OS version 1.11.0
bundle$ bun
[PROBLEM] "dcos-version" - Versions are different.
---------------
Problem details
---------------
master 172.20.0.23 has DC/OS version 1.11.0
master 172.20.0.24 has DC/OS version 1.11.0
agent 172.20.0.25 has DC/OS version 1.11.0
public agent 172.20.0.26 has DC/OS version 1.11.0
agent 172.20.0.27 has DC/OS version 1.11.0
agent 172.20.0.28 has DC/OS version 1.11.0
agent 172.20.0.29 has DC/OS version 1.11.0
agent 172.20.0.21 has DC/OS version 1.10.1
master 172.20.0.23 has DC/OS version 1.11.0
agent 172.20.0.28 has DC/OS version 1.11.0
agent 172.20.0.25 has DC/OS version 1.11.0
public agent 172.20.0.26 has DC/OS version 1.11.0

PROBLEM: health - Some DC/OS systemd units are not healthy.
Details:
172.20.0.21 dcos-docker-gc.service: health = 1
[PROBLEM] "health" - Problems were found.
---------------
Problem details
---------------
agent 172.20.0.21: The following components are not healthy:
dcos-docker-gc.service: health = 1

OK: node-count - Masters: 3, Agents: 5, Public Agents: 1, Total: 9
[OK] "mesos-actor-mailboxes" - All Mesos actors are fine.
[OK] "node-count" - Masters: 3, Agents: 5, Public Agents: 1, Total: 9
```

You can use the `-p` flag if you do not want to change a current directory:
Expand Down
100 changes: 0 additions & 100 deletions bun/cmd/check.go

This file was deleted.

6 changes: 4 additions & 2 deletions import/import.go → bun/cmd/import.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package bun
package cmd

import (
_ "github.com/adyatlov/bun/check/dcosversion"
_ "github.com/adyatlov/bun/check/health"
_ "github.com/adyatlov/bun/check/mesos/actormailboxes"
_ "github.com/adyatlov/bun/check/nodecount"
_ "github.com/adyatlov/bun/file"
_ "github.com/adyatlov/bun/file/dcosversionfile"
_ "github.com/adyatlov/bun/file/healthfile"
_ "github.com/adyatlov/bun/file/mesos/actormailboxesfile"
)
54 changes: 54 additions & 0 deletions bun/cmd/print_report.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package cmd

import (
"fmt"
"strings"

"github.com/adyatlov/bun"
)

func printReport(c bun.Check) {
printEmptyLine := false
fmt.Printf("[%v] \"%v\" - %v\n", c.Status, c.Name, c.Summary)
if printLong {
if len(c.Problems) > 0 {
fmt.Println("---------------")
fmt.Println("Problem details")
fmt.Println("---------------")
fmt.Println(strings.Join(c.Problems, "\n"))
printEmptyLine = true
}
if len(c.Errors) > 0 {
fmt.Println("------")
fmt.Println("Errors")
fmt.Println("------")
fmt.Println(strings.Join(c.Errors, "\n"))
printEmptyLine = true
}
if len(c.OKs) > 0 {
fmt.Println("-------")
fmt.Println("Details")
fmt.Println("-------")
fmt.Println(strings.Join(c.OKs, "\n"))
printEmptyLine = true
}
} else {
if len(c.Problems) > 0 {
fmt.Println("---------------")
fmt.Println("Problem details")
fmt.Println("---------------")
fmt.Println(strings.Join(c.Problems, "\n"))
printEmptyLine = true
}
if len(c.Errors) > 0 {
fmt.Println("------")
fmt.Println("Errors")
fmt.Println("------")
fmt.Println(strings.Join(c.Errors, "\n"))
printEmptyLine = true
}
}
if printEmptyLine {
fmt.Print("\n")
}
}
65 changes: 62 additions & 3 deletions bun/cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
package cmd

import (
"context"
"fmt"
"os"
"sort"

"github.com/adyatlov/bun"
"github.com/spf13/cobra"
)

var bundlePath string
var bundle *bun.Bundle
var printLong = false

var rootCmd = &cobra.Command{
Use: "bun",
Short: "DC/OS diagnostics bundle analysis tool",
Long: "Bun extracts useful facts from hundreds of files in a DC/OS diagnostics bundle" +
" and searches for some common problems of a DC/OS cluster." +
Long: "Bun extracts useful facts from hundreds of files in the DC/OS diagnostics bundle\n" +
"and searches for some common problems of the DC/OS cluster.\n" +
"\nSpecify a subcommand to run a specific check, e.g. `bun health`\n" +
"or run all the available checks by not specifying any, i.e. `bun`.\n" +
"\nMore information is available at https://github.com/adyatlov/bun",
PreRun: preRun,
Run: runCheck,
}

func init() {
Expand All @@ -23,9 +32,59 @@ func init() {
fmt.Printf("Error while detecting a working directory: %v\n", err.Error())
os.Exit(1)
}
rootCmd.PersistentFlags().StringVarP(&bundlePath, "path", "p", wd, "path to the bundle directory")
rootCmd.PersistentFlags().StringVarP(&bundlePath, "path", "p", wd,
"path to the bundle directory")
rootCmd.PersistentFlags().BoolVarP(&printLong, "long", "l", false,
"print details")
// Adding registered checks as commands.
for _, check := range bun.Checks() {
run := func(cmd *cobra.Command, args []string) {
check.Run(*bundle)
printReport(check)
return
}
var cmd = &cobra.Command{
Use: check.Name,
Short: check.Description,
Long: check.Description,
PreRun: preRun,
Run: run,
}
rootCmd.AddCommand(cmd)
rootCmd.ValidArgs = append(rootCmd.ValidArgs, check.Name)
rootCmd.PreRun = preRun
}
}

func preRun(cmd *cobra.Command, args []string) {
if bundle != nil {
return
}
b, err := bun.NewBundle(context.Background(), bundlePath)
if err != nil {
fmt.Printf("Cannot find a bundle: %v\n", err.Error())
os.Exit(1)
}
bundle = &b
}

func runCheck(cmd *cobra.Command, args []string) {
if err := cobra.OnlyValidArgs(cmd, args); err != nil {
fmt.Println(err.Error())
fmt.Printf("Run '%v --help' for usage.\n", cmd.CommandPath())
os.Exit(1)
}
checks := bun.Checks()
sort.Slice(checks, func(i, j int) bool {
return checks[i].Name < checks[j].Name
})
for _, check := range checks {
check.Run(*bundle)
printReport(check)
}
}

// Execute starts Bun.
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
Expand Down
1 change: 0 additions & 1 deletion bun/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"github.com/adyatlov/bun/bun/cmd"
_ "github.com/adyatlov/bun/import"
)

const printProgress = false
Expand Down
Loading

0 comments on commit 5467375

Please sign in to comment.