CLI commands for listing agents, queue items

This commit is contained in:
Brad Rydzewski 2016-05-11 18:43:24 -07:00
parent 752ce496d5
commit f217b40f9a
5 changed files with 88 additions and 1 deletions

View file

@ -86,8 +86,11 @@ type Client interface {
// target environment. // target environment.
Deploy(string, string, int, string) (*model.Build, error) 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. // Pull pulls work from the server queue.

View file

@ -43,6 +43,7 @@ const (
pathUsers = "%s/api/users" pathUsers = "%s/api/users"
pathUser = "%s/api/users/%s" pathUser = "%s/api/users/%s"
pathBuildQueue = "%s/api/builds" pathBuildQueue = "%s/api/builds"
pathAgent = "%s/api/agents"
) )
type client struct { type client struct {
@ -265,6 +266,18 @@ func (c *client) Sign(owner, name string, in []byte) ([]byte, error) {
return ioutil.ReadAll(rc) 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. // Pull pulls work from the server queue.
func (c *client) Pull(os, arch string) (*queue.Work, error) { func (c *client) Pull(os, arch string) (*queue.Work, error) {
out := new(queue.Work) out := new(queue.Work)

64
drone/agent_list.go Normal file
View file

@ -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()
},
}

View file

@ -1,6 +1,7 @@
package main package main
import ( import (
"fmt"
"log" "log"
"os" "os"
"text/template" "text/template"
@ -37,6 +38,11 @@ func buildQueue(c *cli.Context) error {
return err 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") tmpl, err := template.New("_").Parse(c.String("format") + "\n")
if err != nil { if err != nil {
return err return err

View file

@ -32,6 +32,7 @@ func main() {
} }
app.Commands = []cli.Command{ app.Commands = []cli.Command{
agent.AgentCmd, agent.AgentCmd,
agentsCmd,
buildCmd, buildCmd,
deployCmd, deployCmd,
execCmd, execCmd,