Skip to content

Commit

Permalink
[wip]Figuring out broken ssh
Browse files Browse the repository at this point in the history
  • Loading branch information
deven96 committed Mar 21, 2022
1 parent 8caa04f commit 76aed4a
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 46 deletions.
13 changes: 6 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@ import (
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/bisohns/saido/charts"
"github.com/bisohns/saido/config"
)

var (
cfgFile string
cfg *config.Config

// Verbose : Should display verbose logs
verbose bool
verbose bool
dashboardInfo *config.DashboardInfo
)

const appName = "saido"
Expand All @@ -54,9 +56,7 @@ var rootCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
log.Info("Saido is running ...")

_ = config.GetConfig()
// log.Infof("%v", cfg)
// charts.Main()
// charts.Main(cfg)
},
}

Expand All @@ -70,7 +70,6 @@ func Execute() {
}

func init() {
charts.Main()
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "Path to config file")

Expand All @@ -79,5 +78,5 @@ func init() {

// initConfig reads in config file and ENV variables if set.
func initConfig() {
_ = config.LoadConfig(cfgFile)
cfg = config.LoadConfig(cfgFile)
}
54 changes: 34 additions & 20 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ import (
"gopkg.in/yaml.v2"
)

var config = &Config{}
var allHosts []Host
type DashboardInfo struct {
Hosts []Host
Metrics []string
Title string
}

type Connection struct {
Type string `mapstructure:"type"`
Username string `mapstructure:"type"`
Password string `mapstructure:"type"`
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
PrivateKeyPath string `mapstructure:"private_key_path"`
Port int32 `mapstructure:"port"`
}
Expand All @@ -30,9 +33,11 @@ type Host struct {
type Config struct {
Hosts map[interface{}]interface{} `yaml:"hosts"`
Metrics []string `yaml:"metrics"`
Title string `yaml:"title"`
}

func LoadConfig(configPath string) *Config {
var config = &Config{}
confYaml, err := ioutil.ReadFile(configPath)
if err != nil {
log.Errorf("yamlFile.Get err %v ", err)
Expand All @@ -41,32 +46,37 @@ func LoadConfig(configPath string) *Config {
if err != nil {
log.Fatalf("error: %v", err)
}

parseConfig("root", "", config.Hosts, &Connection{})

for _, i := range allHosts {
log.Infof("Name: %s, Connection: %+v", i.Address, i.Connection)
}

return config
}

func (cf *Config) parse() {

}
func GetDashboardInfoConfig(config *Config) *DashboardInfo {
dashboardInfo := &DashboardInfo{
Title: "Saido",
}
if config.Title != "" {
dashboardInfo.Title = config.Title
}

func GetConfig() *Config {
return config
dashboardInfo.Hosts = parseConfig("root", "", config.Hosts, &Connection{})
dashboardInfo.Metrics = config.Metrics
for _, host := range dashboardInfo.Hosts {
log.Debugf("%s: %v", host.Address, host.Connection)
}
return dashboardInfo
}

func parseConnection(conn map[interface{}]interface{}) *Connection {
var c Connection
mapstructure.Decode(conn, &c)
if c.Type == "ssh" && c.Port == 0 {
c.Port = 22
}
return &c
}

func parseConfig(name string, host string, group map[interface{}]interface{}, currentConnection *Connection) {
func parseConfig(name string, host string, group map[interface{}]interface{}, currentConnection *Connection) []Host {
currentConn := currentConnection
allHosts := []Host{}
log.Infof("Loading config for %s and host: %s with Connection: %+v", name, host, currentConn)
isParent := false // Set to true for groups that contain just children data i.e children
if conn, ok := group["connection"]; ok {
Expand All @@ -83,16 +93,16 @@ func parseConfig(name string, host string, group map[interface{}]interface{}, cu
parsedChildren, ok := children.(map[interface{}]interface{})
if !ok {
log.Fatalf("Failed to parse children of %s", name)
return
return nil
}

for k, v := range parsedChildren {
host := make(map[interface{}]interface{})
host, ok := v.(map[interface{}]interface{})
if !ok && v != nil { // some leaf nodes do not contain extra data under
log.Errorf("Failed to parse children of %s", name)
log.Errorf("Faled to parse children of %s", name)
}
parseConfig(fmt.Sprintf("%s:%s", name, k), fmt.Sprintf("%s", k), host, currentConn)
allHosts = append(allHosts, parseConfig(fmt.Sprintf("%s:%s", name, k), fmt.Sprintf("%s", k), host, currentConn)...)
}
}

Expand All @@ -101,7 +111,11 @@ func parseConfig(name string, host string, group map[interface{}]interface{}, cu
Address: host,
Connection: currentConn,
}
if alias, ok := group["alias"]; ok {
newHost.Alias = alias.(string)
}

allHosts = append(allHosts, newHost)
}
return allHosts
}
1 change: 0 additions & 1 deletion driver/local_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

func TestUnixLocalRunCommand(t *testing.T) {
d := Local{}
d.Supported = []string{}
output, err := d.RunCommand(`ps -A`)
if err != nil || !strings.Contains(output, "PID") {
t.Error(err)
Expand Down
21 changes: 14 additions & 7 deletions driver/ssh_test.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
package driver

import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"

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

func NewSSHForTest() Driver {
workingDir, _ := os.Getwd()
workingDir = filepath.Dir(workingDir)
yamlPath := fmt.Sprintf("%s/%s", workingDir, "config-test.yaml")
conf := config.LoadConfig(yamlPath)
dashboardInfo := config.GetDashboardInfoConfig(conf)
return &SSH{
User: "dev",
Host: "127.0.0.1",
Port: 2222,
KeyFile: "/home/diretnan/.ssh/id_rsa",
User: dashboardInfo.Hosts[0].Connection.Username,
Host: dashboardInfo.Hosts[0].Address,
Port: int(dashboardInfo.Hosts[0].Connection.Port),
KeyFile: dashboardInfo.Hosts[0].Connection.PrivateKeyPath,
KeyPass: "",
CheckKnownHosts: false,
driverBase: driverBase{
PollInterval: 5,
},
}
}

Expand Down
9 changes: 9 additions & 0 deletions inspector/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ func (i *DF) Parse(output string) {
if err != nil {
log.Fatalf(`Error Parsing Percent Full: %s `, err)
}
// find size
for index := range columns {
_, err := strconv.ParseInt(columns[index], 0, 64)
if err == nil {
columns[0] = strings.Join(columns[:index], " ")
columns[1] = columns[index]
break
}
}
if strings.HasPrefix(columns[0], i.DeviceStartsWith) {
values = append(values, i.createMetric(columns, percentInt))
} else {
Expand Down
17 changes: 13 additions & 4 deletions inspector/disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,26 @@
package inspector

import (
"fmt"
"os"
"path/filepath"
"testing"

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

func NewSSHForTest() driver.Driver {
workingDir, _ := os.Getwd()
workingDir = filepath.Dir(workingDir)
yamlPath := fmt.Sprintf("%s/%s", workingDir, "config-test.yaml")
conf := config.LoadConfig(yamlPath)
dashboardInfo := config.GetDashboardInfoConfig(conf)
return &driver.SSH{
User: "dev",
Host: "127.0.0.1",
Port: 2222,
KeyFile: "/home/diretnan/.ssh/id_rsa",
User: dashboardInfo.Hosts[0].Connection.Username,
Host: dashboardInfo.Hosts[0].Address,
Port: int(dashboardInfo.Hosts[0].Connection.Port),
KeyFile: dashboardInfo.Hosts[0].Connection.PrivateKeyPath,
KeyPass: "",
CheckKnownHosts: false,
}
Expand Down
12 changes: 5 additions & 7 deletions scripts/prep-test-ssh.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,18 @@ rm -rf $SSH_KEY_PATH
mkdir -p $SSH_KEY_PATH
ssh-keygen -f "$SSH_KEY_PATH/${SSH_KEY_NAME}" -N ""
docker kill saido-linux-sshserver | true
docker rm saido-linux-sshserver || echo 'could not remove container'
docker run -d -p 2222:2222 -e USER_NAME=$SSH_USER --name saido-linux-sshserver -v ${SSH_KEY_PATH}/${SSH_KEY_NAME}.pub:/config/.ssh/authorized_keys linuxserver/openssh-server
SSH_HOST="$(docker inspect -f "{{ .NetworkSettings.IPAddress }}" saido-linux-sshserver)"
cat <<EOF > config-test.yaml
hosts:
children:
"$(docker inspect -f "{{ .NetworkSettings.IPAddress }}" saido-linux-sshserver)":
"${SSH_HOST}":
connection:
type: ssh
port: 2222
username: ${SSH_USER}
private_key_path: $(pwd)/${SSH_KEY_NAME}
"127.0.0.1":
connection:
type: local
port: 2222
private_key_path: "$SSH_KEY_PATH/${SSH_KEY_NAME}"
metrics:
- memory
- cpu
Expand Down

0 comments on commit 76aed4a

Please sign in to comment.