Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Config #8

Merged
merged 2 commits into from
Mar 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion charts/charts.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ const (
tcellTerminal = "tcell"
)

func charts_main() {
func Main() {
log.Debug("Starting Saido Main")
t, err := tcell.New(tcell.ColorMode(terminalapi.ColorMode256))
if err != nil {
Expand Down
8 changes: 5 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ var rootCmd = &cobra.Command{
if verbose {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
// log.SetLevel(log.InfoLevel)
log.SetLevel(log.DebugLevel)
}
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
Expand All @@ -52,8 +53,9 @@ var rootCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
log.Info("Saido is running ...")

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

Expand Down
36 changes: 18 additions & 18 deletions config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,28 @@ hosts:
type: ssh
username: root
password: somethingSecret
x.example.net:
y.example.net:
z.example.net:
"192.0.1.5":
alias: home-server
connection:
type: ssh
username: root
password: somethingSecret
port: 33
"192.0.1.4":
connection:
type: local
children:
x.example.net:
y.example.net:
z.example.net:
"192.0.1.5":
alias: home-server
connection:
type: ssh
username: root
password: somethingSecret
port: 33
"192.0.1.4":
connection:
type: local
eu-west1:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's eu-west1 supposed to be in this context?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@deven96 a group. You can create groups that share similar ssh configuration.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it different from the group that you just put them directly under children

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol... nevermind na shayo I shayo that night of this comment

hosts:
"192.0.10.3":
"192.0.10.5":
connection:
type: ssh
privateKeyPath: /path/to/private/key
port: 2222
private_key_path: /path/to/private/key
port: 2222
children:
"192.0.10.3":
"192.0.10.5":

metrics:
- memory
Expand Down
94 changes: 65 additions & 29 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,42 +1,35 @@
package config

import (
"fmt"
"io/ioutil"
"strings"

"github.com/mitchellh/mapstructure"
log "github.com/sirupsen/logrus"

"gopkg.in/yaml.v2"
)

var config = &Config{}
var allHosts []Host

type Connection struct {
Type string
Username string
Password string
PrivateKeyPath string
}

// Group represents group specified under children
type Group struct {
Name string
Hosts map[string]*Host
Children map[string]*Group
Connection *Connection
Type string `mapstructure:"type"`
Username string `mapstructure:"type"`
Password string `mapstructure:"type"`
PrivateKeyPath string `mapstructure:"private_key_path"`
Port int32 `mapstructure:"port"`
}

type Host struct {
Alias string
Port int32
Connection *Connection
Hosts map[string]*Host
directGroups map[string]*Group
Address string
Alias string
Connection *Connection
}

type Config struct {
Hosts map[string]interface{} `yaml:"hosts"`
Metrics []string `yaml:"metrics"`
Hosts map[interface{}]interface{} `yaml:"hosts"`
Metrics []string `yaml:"metrics"`
}

func LoadConfig(configPath string) *Config {
Expand All @@ -49,23 +42,66 @@ func LoadConfig(configPath string) *Config {
log.Fatalf("error: %v", err)
}

for k, v := range config.Hosts {
// Parser Children
if strings.ToLower(k) == "children" {

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

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

return config
}

func (cf *Config) parse() {

}

func GetConfig() *Config {
return config
}

func parseHosts(h interface{}) []*Host {
return []*Host{}
func parseConnection(conn map[interface{}]interface{}) *Connection {
var c Connection
mapstructure.Decode(conn, &c)
return &c
}

func parseConfig(name string, host string, group map[interface{}]interface{}, currentConnection *Connection) {
currentConn := currentConnection
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 {
v, ok := conn.(map[interface{}]interface{})
if !ok {
log.Errorf("Failed to parse connection for %s", name)
}

currentConn = parseConnection(v)
}

if children, ok := group["children"]; ok {
isParent = true
parsedChildren, ok := children.(map[interface{}]interface{})
if !ok {
log.Fatalf("Failed to parse children of %s", name)
return
}

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)
}
parseConfig(fmt.Sprintf("%s:%s", name, k), fmt.Sprintf("%s", k), host, currentConn)
}
}

if !isParent {
newHost := Host{
Address: host,
Connection: currentConn,
}

allHosts = append(allHosts, newHost)
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.14
require (
github.com/kr/pretty v0.2.0 // indirect
github.com/melbahja/goph v1.2.1
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/mum4k/termdash v0.16.0
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.2.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0Qu
github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/mapstructure v1.4.3 h1:OVowDSCllw/YjdLkam3/sm7wEtOy59d8ndGgCcyj8cs=
github.com/mitchellh/mapstructure v1.4.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
Expand Down