Skip to content

Commit

Permalink
NewInspector Interface enforces sanity across Interface initializers
Browse files Browse the repository at this point in the history
  • Loading branch information
deven96 committed Mar 17, 2022
1 parent 624e205 commit dfaa340
Show file tree
Hide file tree
Showing 16 changed files with 118 additions and 71 deletions.
57 changes: 30 additions & 27 deletions driver/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ type SSH struct {
// Check known hosts (only disable for tests
CheckKnownHosts bool
// set environmental vars for server e.g []string{"DEBUG=1", "FAKE=echo"}
Vars []string
Vars []string
SessionClient *goph.Client
}

func (d *SSH) String() string {
Expand All @@ -36,33 +37,37 @@ func (d *SSH) String() string {

// set the goph Client
func (d *SSH) Client() (*goph.Client, error) {
var err error
var client *goph.Client
var callback ssh.HostKeyCallback
if d.Port != 0 {
port = d.Port
}
auth, err := goph.Key(d.KeyFile, d.KeyPass)
if err != nil {
return nil, err
}
if d.CheckKnownHosts {
callback, err = goph.DefaultKnownHosts()
if d.SessionClient == nil {
var err error
var client *goph.Client
var callback ssh.HostKeyCallback
if d.Port != 0 {
port = d.Port
}
auth, err := goph.Key(d.KeyFile, d.KeyPass)
if err != nil {
return nil, err
}
} else {
callback = ssh.InsecureIgnoreHostKey()
if d.CheckKnownHosts {
callback, err = goph.DefaultKnownHosts()
if err != nil {
return nil, err
}
} else {
callback = ssh.InsecureIgnoreHostKey()
}
client, err = goph.NewConn(&goph.Config{
User: d.User,
Addr: d.Host,
Port: uint(port),
Auth: auth,
Timeout: goph.DefaultTimeout,
Callback: callback,
})
d.SessionClient = client
return client, err
}
client, err = goph.NewConn(&goph.Config{
User: d.User,
Addr: d.Host,
Port: uint(port),
Auth: auth,
Timeout: goph.DefaultTimeout,
Callback: callback,
})
return client, err
return d.SessionClient, nil
}

func (d *SSH) ReadFile(path string) (string, error) {
Expand All @@ -72,7 +77,7 @@ func (d *SSH) ReadFile(path string) (string, error) {
}

func (d *SSH) RunCommand(command string) (string, error) {
// FIXME: Do we retain client across all command runs?
// FIXME: provide refresh interface to somehow refresh d.Client if d.SessionClient somehow gets dropped
log.Debugf("Running remote command %s", command)
client, err := d.Client()
if err != nil {
Expand Down Expand Up @@ -112,8 +117,6 @@ func (d *SSH) GetDetails() SystemDetails {
details.IsLinux = true
case "darwin":
details.IsDarwin = true
default:
details.IsLinux = true
}
d.Info = details
}
Expand Down
9 changes: 5 additions & 4 deletions inspector/custom.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package inspector

import (
"errors"
"fmt"

"github.com/bisohns/saido/driver"
Expand Down Expand Up @@ -51,15 +52,15 @@ func (i *Custom) Execute() {
}

// NewCustom : Initialize a new Custom instance
func NewCustom(custom string, driver *driver.Driver) Inspector {
func NewCustom(driver *driver.Driver, custom ...string) (Inspector, error) {
var customInspector Inspector
details := (*driver).GetDetails()
if details.IsWeb {
panic(fmt.Sprintf("Cannot use Custom(%s) on web", custom))
return nil, errors.New(fmt.Sprintf("Cannot use Custom(%s) on web", custom))
}
customInspector = &Custom{
Command: custom,
Command: custom[0],
}
customInspector.SetDriver(driver)
return customInspector
return customInspector, nil
}
7 changes: 4 additions & 3 deletions inspector/disk.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package inspector

import (
"errors"
"strconv"
"strings"

Expand Down Expand Up @@ -97,11 +98,11 @@ type WMIC struct {
}

// NewDF : Initialize a new DF instance
func NewDF(driver *driver.Driver) Inspector {
func NewDF(driver *driver.Driver, _ ...string) (Inspector, error) {
var df Inspector
details := (*driver).GetDetails()
if !(details.IsLinux || details.IsDarwin || details.IsWindows) {
panic("Cannot use 'df' command on drivers outside (linux, darwin, windows)")
return nil, errors.New("Cannot use 'df' command on drivers outside (linux, darwin, windows)")
}
if details.IsLinux || details.IsDarwin {
df = &DF{
Expand All @@ -114,5 +115,5 @@ func NewDF(driver *driver.Driver) Inspector {
}
}
df.SetDriver(driver)
return df
return df, nil
}
4 changes: 2 additions & 2 deletions inspector/disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func NewSSHForTest() driver.Driver {

func TestDFOnLocal(t *testing.T) {
driver := NewLocalForTest()
d := NewDF(&driver)
d, _ := NewDF(&driver)
d.Execute()
dfConcrete, _ := d.(*DF)
if len(dfConcrete.Values) == 0 {
Expand All @@ -31,7 +31,7 @@ func TestDFOnLocal(t *testing.T) {

func TestDFOnSSH(t *testing.T) {
driver := NewSSHForTest()
d := NewDF(&driver)
d, _ := NewDF(&driver)
d.Execute()
dfConcrete, _ := d.(*DF)
if len(dfConcrete.Values) == 0 {
Expand Down
7 changes: 4 additions & 3 deletions inspector/docker_stats.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package inspector

import (
"errors"
"strconv"
"strings"

Expand Down Expand Up @@ -109,15 +110,15 @@ func (i *DockerStats) Execute() {
}

// NewDockerStats : Initialize a new DockerStats instance
func NewDockerStats(driver *driver.Driver) Inspector {
func NewDockerStats(driver *driver.Driver, _ ...string) (Inspector, error) {
var dockerstats Inspector
details := (*driver).GetDetails()
if !(details.IsLinux || details.IsDarwin || details.IsWindows) {
panic("Cannot use LoadAvgDarwin on drivers outside (linux, darwin, windows)")
return nil, errors.New("Cannot use LoadAvgDarwin on drivers outside (linux, darwin, windows)")
}
dockerstats = &DockerStats{
Command: `docker stats --no-stream`,
}
dockerstats.SetDriver(driver)
return dockerstats
return dockerstats, nil
}
28 changes: 28 additions & 0 deletions inspector/inspector.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package inspector

import (
"errors"
"fmt"

"github.com/bisohns/saido/driver"
)

Expand All @@ -11,3 +14,28 @@ type Inspector interface {
Execute()
driverExec() driver.Command
}

type NewInspector func(driver *driver.Driver, custom ...string) (Inspector, error)

var inspectorMap = map[string]NewInspector{
`disk`: NewDF,
`docker`: NewDockerStats,
`uptime`: NewUptime,
`responsetime`: NewResponseTime,
`memory`: NewMemInfo,
`process`: NewProcess,
`custom`: NewCustom,
}

// Init : initializes the specified inspector using name and driver
func Init(name string, driver *driver.Driver, custom ...string) (Inspector, error) {
val, ok := inspectorMap[name]
if ok {
inspector, err := val(driver, custom...)
if err != nil {
return nil, err
}
return inspector, nil
}
return nil, errors.New(fmt.Sprintf("Cannot find inspector with name %s", name))
}
7 changes: 4 additions & 3 deletions inspector/loadavg.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package inspector

import (
"errors"
"strconv"
"strings"

Expand Down Expand Up @@ -98,11 +99,11 @@ func (i *LoadAvg) Execute() {
// of LoadAvg

// NewLoadAvg : Initialize a new LoadAvg instance
func NewLoadAvg(driver *driver.Driver) Inspector {
func NewLoadAvg(driver *driver.Driver, _ ...string) (Inspector, error) {
var loadavg Inspector
details := (*driver).GetDetails()
if !(details.IsLinux || details.IsDarwin) {
panic("Cannot use LoadAvg on drivers outside (linux, darwin)")
return nil, errors.New("Cannot use LoadAvg on drivers outside (linux, darwin)")
}
if details.IsLinux {
loadavg = &LoadAvg{
Expand All @@ -115,6 +116,6 @@ func NewLoadAvg(driver *driver.Driver) Inspector {
}
}
loadavg.SetDriver(driver)
return loadavg
return loadavg, nil

}
2 changes: 1 addition & 1 deletion inspector/loadavg_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func NewLocalForTest() driver.Driver {

func TestLoadAvg(t *testing.T) {
testDriver := NewLocalForTest()
loadavg := NewLoadAvg(&testDriver)
loadavg, _ := NewLoadAvg(&testDriver)
loadavg.Execute()
loadavgConcreteLinux, ok := loadavg.(*LoadAvg)
if ok {
Expand Down
7 changes: 4 additions & 3 deletions inspector/meminfo.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package inspector

import (
"errors"
"fmt"
"strconv"
"strings"
Expand Down Expand Up @@ -166,11 +167,11 @@ func (i *MemInfoDarwin) Execute() {
// TODO: Windows Equivalents of MemInfo

// NewMemInfoLinux : Initialize a new MemInfoLinux instance
func NewMemInfo(driver *driver.Driver) Inspector {
func NewMemInfo(driver *driver.Driver, _ ...string) (Inspector, error) {
var meminfo Inspector
details := (*driver).GetDetails()
if !(details.IsLinux || details.IsDarwin) {
panic("Cannot use MemInfo on drivers outside (linux, darwin)")
return nil, errors.New("Cannot use MemInfo on drivers outside (linux, darwin)")
}
if details.IsLinux {
meminfo = &MemInfoLinux{
Expand All @@ -187,5 +188,5 @@ func NewMemInfo(driver *driver.Driver) Inspector {
}
}
meminfo.SetDriver(driver)
return meminfo
return meminfo, nil
}
2 changes: 1 addition & 1 deletion inspector/meminfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func TestMemInfoOnLocal(t *testing.T) {
driver := NewLocalForTest()
d := NewMemInfo(&driver)
d, _ := NewMemInfo(&driver)
d.Execute()
iConcreteLinux, ok := d.(*MemInfoLinux)
if ok {
Expand Down
7 changes: 4 additions & 3 deletions inspector/process.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package inspector

import (
"errors"
"strconv"
"strings"

Expand Down Expand Up @@ -185,11 +186,11 @@ func (i *ProcessWin) Execute() {
}

// NewProcess : Initialize a new Process instance
func NewProcess(driver *driver.Driver) Inspector {
func NewProcess(driver *driver.Driver, _ ...string) (Inspector, error) {
var process Inspector
details := (*driver).GetDetails()
if !(details.IsLinux || details.IsDarwin || details.IsWindows) {
panic("Cannot use Process on drivers outside (linux, darwin, windows)")
return nil, errors.New("Cannot use Process on drivers outside (linux, darwin, windows)")
}
if details.IsLinux || details.IsDarwin {
process = &Process{
Expand All @@ -201,5 +202,5 @@ func NewProcess(driver *driver.Driver) Inspector {
}
}
process.SetDriver(driver)
return process
return process, nil
}
7 changes: 4 additions & 3 deletions inspector/rt.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package inspector

import (
"errors"
"strconv"

"github.com/bisohns/saido/driver"
Expand Down Expand Up @@ -49,15 +50,15 @@ func (i *ResponseTime) Execute() {
}

// NewResponseTime : Initialize a new ResponseTime instance
func NewResponseTime(driver *driver.Driver) Inspector {
func NewResponseTime(driver *driver.Driver, _ ...string) (Inspector, error) {
var responsetime Inspector
details := (*driver).GetDetails()
if !(details.IsWeb) {
panic("Cannot use response time outside driver (web)")
return nil, errors.New("Cannot use response time outside driver (web)")
}
responsetime = &ResponseTime{
Command: `response`,
}
responsetime.SetDriver(driver)
return responsetime
return responsetime, nil
}
7 changes: 4 additions & 3 deletions inspector/uptime.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package inspector

import (
"errors"
"fmt"
"strconv"
"strings"
Expand Down Expand Up @@ -115,11 +116,11 @@ func (i *UptimeDarwin) Execute() {
//TODO: Windows equivalent of uptime

// NewUptime : Initialize a new Uptime instance
func NewUptime(driver *driver.Driver) Inspector {
func NewUptime(driver *driver.Driver, _ ...string) (Inspector, error) {
var uptime Inspector
details := (*driver).GetDetails()
if !(details.IsDarwin || details.IsLinux) {
panic("Cannot use Uptime on drivers outside (linux, darwin)")
return nil, errors.New("Cannot use Uptime on drivers outside (linux, darwin)")
}
if details.IsLinux {
uptime = &UptimeLinux{
Expand All @@ -132,5 +133,5 @@ func NewUptime(driver *driver.Driver) Inspector {
}
}
uptime.SetDriver(driver)
return uptime
return uptime, nil
}
Loading

0 comments on commit dfaa340

Please sign in to comment.