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

add support for node list #16

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
66 changes: 49 additions & 17 deletions gladius.go → main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"encoding/json"
"fmt"
"github.com/davecgh/go-spew/spew"
"github.com/docopt/docopt-go"
"github.com/go-chef/chef"
"github.com/kdar/factorlog"
"github.com/spf13/cobra"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"runtime"
"strings"
)

Expand All @@ -20,6 +21,7 @@ const VERSION = "0.0.1"
type config struct {
ServerURL string `json:"server_url"`
ClientName string `json:"client_name"`
SkipSSL bool `json:"skip_ssl"`
KeyPath string `json:"client_key"`
CookPaths []string `json:"cook_paths"`
}
Expand All @@ -31,20 +33,56 @@ var Config = config{}
var stderr = factorlog.New(os.Stderr, factorlog.NewStdFormatter(`%{Color "red" "ERROR"}%{Color "yellow" "WARN"}%{Color "green" "INFO"}%{Color "cyan" "DEBUG"}%{Color "blue" "TRACE"} %{SEVERITY}: %{Message}%{Color "reset"}`))

func main() {
gladiusMain()
}

// Debatable weather this is worth doing or not.
// TODO: benchmark/profile setting and leaving GOMAXPROCS default
if os.Getenv("GOMAXPROCS") == "" {
runtime.GOMAXPROCS(runtime.NumCPU())
func chefClient() (client *chef.Client, err error) {
key, err := ioutil.ReadFile(Config.KeyPath)
if err != nil {
fmt.Println("Couldn't read key.pem:", err)
os.Exit(1)
}
return chef.NewClient(&chef.Config{
Name: Config.ClientName,
Key: string(key),
BaseURL: Config.ServerURL,
SkipSSL: Config.SkipSSL,
})
}

func nodeList(cmd *cobra.Command, args []string) {
client, err := chefClient()
nodes, err := client.Nodes.List()
if err != nil {
fmt.Println("Issue listing nodes:", err)
}
for nodeName, _ := range nodes {
fmt.Println(nodeName)
}
}

args, _ := docopt.Parse(usage(), nil, true, VERSION, false, true)
setlog(args["--loglevel"].(string))
func gladiusMain() {
var cmdNode = &cobra.Command{
Use: "node",
Short: "Node related operations",
Long: "create, retrieve, update and delete node(s)",
}
var cmdNodeList = &cobra.Command{
Use: "list",
Short: "List nodes",
Long: "List chef nodes present",
Run: nodeList,
}

configure(args["--config"].([]string))
stderr.Debug("Config: ", spew.Sdump(Config))
cmdNode.AddCommand(cmdNodeList)
var rootCmd = &cobra.Command{Use: "gladius"}
rootCmd.PersistentFlags().StringVarP(&Config.ServerURL, "server-url", "s", "http://localhost:8080", "Chef server URL")
rootCmd.PersistentFlags().StringVarP(&Config.ClientName, "user", "u", "admin", "User name, aka node name aka api client name")
rootCmd.PersistentFlags().StringVarP(&Config.KeyPath, "key", "k", "/etc/chef/client.pem", "Client key")
rootCmd.PersistentFlags().BoolVarP(&Config.SkipSSL, "skip-ssl", "", false, "Skip SSL verification")

dispatch(args)
rootCmd.AddCommand(cmdNode)
rootCmd.Execute()
}

// Configure finds, parses, and loads the config.json presented by the cli args. last file loaded wins.
Expand All @@ -64,7 +102,6 @@ func configure(files []string) {
continue
}
defer file.Close()

stderr.Info("Loading Config: ", path)
err = json.NewDecoder(file).Decode(&Config)
if err != nil {
Expand Down Expand Up @@ -121,8 +158,3 @@ Objects:

return usage
}

// dispatch maps the arg commands into their subcommands
func dispatch(args map[string]interface{}) {

}
File renamed without changes.