Skip to content

Commit

Permalink
Adding makefile and browser trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
deven96 committed Nov 10, 2022
1 parent b86fddb commit f484452
Show file tree
Hide file tree
Showing 10 changed files with 288 additions and 21 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ ssh-key/ci/
config-ci.yaml
config-test.yaml
tmp/
cmd/build/
*-packr.go
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ else
upgrade:
@echo "Version not set - use syntax \`make upgrade version=0.x.x\`"
endif

build-frontend:
cd web && yarn build && cd ..

2 changes: 1 addition & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (client *Client) Write() {
if err != nil {
log.Error("Error inside client write ", err)
// check socket connection has been closed and end writer
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure, websocket.CloseInternalServerErr) || err == websocket.ErrCloseSent {
return
}
}
Expand Down
11 changes: 7 additions & 4 deletions client/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ func (hosts *HostsController) sendMetric(host config.Host, client *Client) {
hosts.resetDriver(host)
}
for metric, custom := range hosts.Info.Metrics {
driver := hosts.getDriver(host.Address)
initializedMetric, err := inspector.Init(metric, driver, custom)
inspectorDriver := hosts.getDriver(host.Address)
initializedMetric, err := inspector.Init(metric, inspectorDriver, custom)
if err != nil {
log.Error(err)
}
Expand All @@ -86,7 +86,7 @@ func (hosts *HostsController) sendMetric(host config.Host, client *Client) {
message := &SendMessage{
Message: Message{
Host: host.Address,
Platform: (*driver).GetDetails().Name,
Platform: (*inspectorDriver).GetDetails().Name,
Name: metric,
Data: unmarsh,
},
Expand All @@ -104,7 +104,10 @@ func (hosts *HostsController) sendMetric(host config.Host, client *Client) {
errorContent = fmt.Sprintf("Command %s not found on driver %s", metric, host.Address)
}
log.Error(errorContent)
hosts.resetDriver(host)
//FIXME: what kind of errors do we especially want to reset driver for
if _, ok := err.(*driver.SSHError); ok {
hosts.resetDriver(host)
}
message := &SendMessage{
Message: ErrorMessage{
Error: errorContent,
Expand Down
40 changes: 40 additions & 0 deletions cmd/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,75 @@ limitations under the License.
package cmd

import (
"embed"
"fmt"
"io/fs"
"net/http"
"os"
"path"
"strconv"

"github.com/bisohns/saido/client"
"github.com/gorilla/handlers"
"github.com/pkg/browser"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

var (
port string
server = http.NewServeMux()

//go:embed build/*
build embed.FS
)

type fsFunc func(name string) (fs.File, error)

func (f fsFunc) Open(name string) (fs.File, error) {
return f(name)
}

func EmbedHandler(prefix, root string) http.Handler {
handler := fsFunc(func(name string) (fs.File, error) {
assetPath := path.Join(root, name)

// If we can't find the asset, return the default index.html
// build
f, err := build.Open(assetPath)
if os.IsNotExist(err) {
return build.Open(fmt.Sprintf("%s/index.html", root))
}

// Otherwise assume this is a legitimate request routed
// correctly
return f, err
})

return http.StripPrefix(prefix, http.FileServer(http.FS(handler)))
}

var apiCmd = &cobra.Command{
Use: "dashboard",
Short: "Run saido dashboard on a PORT env variable, fallback to set argument",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
hosts := client.NewHostsController(cfg)

frontendHandler := EmbedHandler("/", "build")
server.Handle("/", frontendHandler)
server.Handle("/metrics", hosts)
server.Handle("/*filepath", frontendHandler)
log.Info("listening on :", port)
_, err := strconv.Atoi(port)
if err != nil {
log.Fatal(err)
}
go hosts.Run()
loggedRouters := handlers.LoggingHandler(os.Stdout, server)
// Trigger browser open
url := fmt.Sprintf("http://localhost:%s", port)
browser.OpenURL(url)
if err := http.ListenAndServe(":"+port, loggedRouters); err != nil {
log.Fatal(err)
}
Expand Down
17 changes: 14 additions & 3 deletions driver/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,17 @@ import (
"golang.org/x/crypto/ssh"
)

var port = 22
var (
port = 22
)

type SSHError struct {
content string
}

func (e *SSHError) Error() string {
return fmt.Sprintf("SSH Error: %s", e.content)
}

// SSH : Driver for handling ssh executions
type SSH struct {
Expand Down Expand Up @@ -40,6 +50,7 @@ func (d *SSH) String() string {
// set the goph Client
func (d *SSH) Client() (*goph.Client, error) {
if d.SessionClient == nil {
log.Debugf("re-establishing connection with %s ...", d.Host)
var err error
var client *goph.Client
var auth goph.Auth
Expand Down Expand Up @@ -71,7 +82,7 @@ func (d *SSH) Client() (*goph.Client, error) {
Timeout: goph.DefaultTimeout,
Callback: callback,
})
if err != nil {
if err == nil {
d.SessionClient = client
}
return client, err
Expand All @@ -91,7 +102,7 @@ func (d *SSH) RunCommand(command string) (string, error) {
log.Debugf("Running remote command %s", command)
client, err := d.Client()
if err != nil {
return ``, err
return ``, &SSHError{content: err.Error()}
}
if len(d.EnvVars) != 0 {
// add env variable to command
Expand Down
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ require (
github.com/gorilla/websocket v1.5.0
github.com/melbahja/goph v1.3.0
github.com/mitchellh/mapstructure v1.5.0
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8
github.com/sirupsen/logrus v1.9.0
github.com/spf13/cobra v1.6.1
golang.org/x/crypto v0.1.0
golang.org/x/crypto v0.2.0
gopkg.in/yaml.v2 v2.4.0
)

Expand All @@ -21,6 +22,7 @@ require (
github.com/pkg/errors v0.9.1 // indirect
github.com/pkg/sftp v1.13.5 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.1.0 // indirect
github.com/stretchr/testify v1.8.0 // indirect
golang.org/x/sys v0.2.0 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
)
18 changes: 12 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ github.com/melbahja/goph v1.3.0 h1:RAIS7eL2tew/UrNmBpY2NZMxw6fWtOxki9nkrzw8mZY=
github.com/melbahja/goph v1.3.0/go.mod h1:04M6J+mKmwzAOWhO0ABTweHGU3cizOp90WdCoxrn9gQ=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 h1:KoWmjvw+nsYOo29YJK9vDA65RGE3NrOnUtO7a+RF9HU=
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8/go.mod h1:HKlIX3XHQyzLZPlr7++PzdhaXEj94dEiJgZDTsxEqUI=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/sftp v1.13.4/go.mod h1:LzqnAvaD5TWeNBsZpfKxSYn1MbjWwOsCIAFFJbpIsK8=
Expand All @@ -37,27 +39,31 @@ github.com/spf13/cobra v1.6.1/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUq
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.1.0 h1:MDRAIl0xIo9Io2xV565hzXHw3zVseKrJKodhohM5CjU=
golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw=
golang.org/x/crypto v0.2.0 h1:BRXPfhNivWL5Yq0BGQ39a2sW6t44aODpfxkWjYdzewE=
golang.org/x/crypto v0.2.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210616045830-e2b7044e8c71/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211031064116-611d5d643895/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A=
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.1.0 h1:g6Z6vPFA9dYBAF7DWcH6sCcOntplXsDKcliusYijMlw=
golang.org/x/term v0.2.0 h1:z85xZCsEl7bi/KwbNADeBYoOP0++7W1ipu+aGnpwzRM=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down
2 changes: 1 addition & 1 deletion web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"build": "BUILD_PATH='../cmd/build' react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
Expand Down
Loading

0 comments on commit f484452

Please sign in to comment.