From f217b40f9a884e07b4be2461fe181df1def74117 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Wed, 11 May 2016 18:43:24 -0700 Subject: [PATCH] CLI commands for listing agents, queue items --- client/client.go | 5 +++- client/client_impl.go | 13 +++++++++ drone/agent_list.go | 64 +++++++++++++++++++++++++++++++++++++++++++ drone/build_queue.go | 6 ++++ drone/main.go | 1 + 5 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 drone/agent_list.go diff --git a/client/client.go b/client/client.go index 51a48768..37b6e99e 100644 --- a/client/client.go +++ b/client/client.go @@ -86,8 +86,11 @@ type Client interface { // target environment. Deploy(string, string, int, string) (*model.Build, error) + // AgentList returns a list of build agents. + AgentList() ([]*model.Agent, error) + // - // queue functions + // below items for Queue (internal use only) // // Pull pulls work from the server queue. diff --git a/client/client_impl.go b/client/client_impl.go index ea83fd3a..96d6814e 100644 --- a/client/client_impl.go +++ b/client/client_impl.go @@ -43,6 +43,7 @@ const ( pathUsers = "%s/api/users" pathUser = "%s/api/users/%s" pathBuildQueue = "%s/api/builds" + pathAgent = "%s/api/agents" ) type client struct { @@ -265,6 +266,18 @@ func (c *client) Sign(owner, name string, in []byte) ([]byte, error) { return ioutil.ReadAll(rc) } +// AgentList returns a list of build agents. +func (c *client) AgentList() ([]*model.Agent, error) { + var out []*model.Agent + uri := fmt.Sprintf(pathAgent, c.base) + err := c.get(uri, &out) + return out, err +} + +// +// below items for Queue (internal use only) +// + // Pull pulls work from the server queue. func (c *client) Pull(os, arch string) (*queue.Work, error) { out := new(queue.Work) diff --git a/drone/agent_list.go b/drone/agent_list.go new file mode 100644 index 00000000..4bb435b4 --- /dev/null +++ b/drone/agent_list.go @@ -0,0 +1,64 @@ +package main + +import ( + "html/template" + "log" + "os" + "time" + + "github.com/codegangsta/cli" +) + +var agentsCmd = cli.Command{ + Name: "agents", + Usage: "manage agents", + Action: func(c *cli.Context) { + if err := agentList(c); err != nil { + log.Fatalln(err) + } + }, + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "format", + Usage: "format output", + Value: tmplAgentList, + }, + }, +} + +func agentList(c *cli.Context) error { + client, err := newClient(c) + if err != nil { + return err + } + + agents, err := client.AgentList() + if err != nil { + return err + } + + tmpl, err := template.New("_").Funcs(funcMap).Parse(c.String("format") + "\n") + if err != nil { + return err + } + + for _, agent := range agents { + tmpl.Execute(os.Stdout, agent) + } + return nil +} + +// template for build list information +var tmplAgentList = "\x1b[33m{{ .Address }} \x1b[0m" + ` +Platform: {{ .Platform }} +Capacity: {{ .Capacity }} concurrent build(s) +Pinged: {{ since .Updated }} ago +Uptime: {{ since .Created }} +` + +var funcMap = template.FuncMap{ + "since": func(t int64) string { + d := time.Now().Sub(time.Unix(t, 0)) + return d.String() + }, +} diff --git a/drone/build_queue.go b/drone/build_queue.go index e67f8029..c9450ab8 100644 --- a/drone/build_queue.go +++ b/drone/build_queue.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "log" "os" "text/template" @@ -37,6 +38,11 @@ func buildQueue(c *cli.Context) error { return err } + if len(builds) == 0 { + fmt.Println("there are no pending or running builds") + return nil + } + tmpl, err := template.New("_").Parse(c.String("format") + "\n") if err != nil { return err diff --git a/drone/main.go b/drone/main.go index d1847086..f50e7188 100644 --- a/drone/main.go +++ b/drone/main.go @@ -32,6 +32,7 @@ func main() { } app.Commands = []cli.Command{ agent.AgentCmd, + agentsCmd, buildCmd, deployCmd, execCmd,