Skip to content

Commit

Permalink
Merge pull request #675 from nautsio/f-cli-short-ids
Browse files Browse the repository at this point in the history
Shorten CLI identifiers
  • Loading branch information
dadgar committed Jan 19, 2016
2 parents 5630dc3 + 4330157 commit 0d29e30
Show file tree
Hide file tree
Showing 33 changed files with 281 additions and 148 deletions.
4 changes: 2 additions & 2 deletions api/nodes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func TestNodes_Info(t *testing.T) {
nodes := c.Nodes()

// Retrieving a non-existent node returns error
_, _, err := nodes.Info("nope", nil)
_, _, err := nodes.Info("12345678-abcd-efab-cdef-123456789abc", nil)
if err == nil || !strings.Contains(err.Error(), "not found") {
t.Fatalf("expected not found error, got: %#v", err)
}
Expand Down Expand Up @@ -215,7 +215,7 @@ func TestNodes_ForceEvaluate(t *testing.T) {
nodes := c.Nodes()

// Force-eval on a non-existent node fails
_, _, err := nodes.ForceEvaluate("nope", nil)
_, _, err := nodes.ForceEvaluate("12345678-abcd-efab-cdef-123456789abc", nil)
if err == nil || !strings.Contains(err.Error(), "not found") {
t.Fatalf("expected not found error, got: %#v", err)
}
Expand Down
13 changes: 6 additions & 7 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,12 @@ func (c *Client) setupNode() error {
node = &structs.Node{}
c.config.Node = node
}
// Generate an iD for the node
var err error
node.ID, err = c.nodeID()
if err != nil {
return fmt.Errorf("node ID setup failed: %v", err)
}
if node.Attributes == nil {
node.Attributes = make(map[string]string)
}
Expand All @@ -462,13 +468,6 @@ func (c *Client) setupNode() error {
if node.Resources == nil {
node.Resources = &structs.Resources{}
}
if node.ID == "" {
id, err := c.nodeID()
if err != nil {
return fmt.Errorf("node ID setup failed: %v", err)
}
node.ID = id
}
if node.Datacenter == "" {
node.Datacenter = "dc1"
}
Expand Down
1 change: 0 additions & 1 deletion command/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@ func (a *Agent) setupClient() error {
conf.Node = new(structs.Node)
conf.Node.Datacenter = a.config.Datacenter
conf.Node.Name = a.config.NodeName
conf.Node.ID = a.config.Client.NodeID
conf.Node.Meta = a.config.Client.Meta
conf.Node.NodeClass = a.config.Client.NodeClass

Expand Down
1 change: 0 additions & 1 deletion command/agent/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ func (c *Command) readConfig() *Config {
// Client-only options
flags.StringVar(&cmdConfig.Client.StateDir, "state-dir", "", "")
flags.StringVar(&cmdConfig.Client.AllocDir, "alloc-dir", "", "")
flags.StringVar(&cmdConfig.Client.NodeID, "node-id", "", "")
flags.StringVar(&cmdConfig.Client.NodeClass, "node-class", "", "")
flags.StringVar(&servers, "servers", "", "")
flags.Var((*sliceflag.StringFlag)(&meta), "meta", "")
Expand Down
7 changes: 0 additions & 7 deletions command/agent/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,6 @@ type ClientConfig struct {
// Servers is a list of known server addresses. These are as "host:port"
Servers []string `hcl:"servers"`

// NodeID is the unique node identifier to use. A UUID is used
// if not provided, and stored in the data directory
NodeID string `hcl:"node_id"`

// NodeClass is used to group the node by class
NodeClass string `hcl:"node_class"`

Expand Down Expand Up @@ -492,9 +488,6 @@ func (a *ClientConfig) Merge(b *ClientConfig) *ClientConfig {
if b.AllocDir != "" {
result.AllocDir = b.AllocDir
}
if b.NodeID != "" {
result.NodeID = b.NodeID
}
if b.NodeClass != "" {
result.NodeClass = b.NodeClass
}
Expand Down
3 changes: 0 additions & 3 deletions command/agent/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ func TestConfig_Merge(t *testing.T) {
Enabled: false,
StateDir: "/tmp/state1",
AllocDir: "/tmp/alloc1",
NodeID: "node1",
NodeClass: "class1",
Options: map[string]string{
"foo": "bar",
Expand Down Expand Up @@ -96,7 +95,6 @@ func TestConfig_Merge(t *testing.T) {
Enabled: true,
StateDir: "/tmp/state2",
AllocDir: "/tmp/alloc2",
NodeID: "node2",
NodeClass: "class2",
Servers: []string{"server2"},
Meta: map[string]string{
Expand Down Expand Up @@ -413,7 +411,6 @@ func TestConfig_LoadConfigString(t *testing.T) {
StateDir: "/tmp/client-state",
AllocDir: "/tmp/alloc",
Servers: []string{"a.b.c:80", "127.0.0.1:1234"},
NodeID: "xyz123",
NodeClass: "linux-medium-64bit",
Meta: map[string]string{
"foo": "bar",
Expand Down
13 changes: 10 additions & 3 deletions command/agent/node_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,14 @@ func TestHTTP_NodesList(t *testing.T) {

func TestHTTP_NodesPrefixList(t *testing.T) {
httpTest(t, nil, func(s *TestServer) {
ids := []string{"aaaaa", "aaaab", "aaabb", "aabbb", "abbbb", "bbbbb"}
ids := []string{
"12345678-abcd-efab-cdef-123456789abc",
"12345678-aaaa-efab-cdef-123456789abc",
"1234aaaa-abcd-efab-cdef-123456789abc",
"1234bbbb-abcd-efab-cdef-123456789abc",
"1234cccc-abcd-efab-cdef-123456789abc",
"1234dddd-abcd-efab-cdef-123456789abc",
}
for i := 0; i < 5; i++ {
// Create the node
node := mock.Node()
Expand All @@ -74,7 +81,7 @@ func TestHTTP_NodesPrefixList(t *testing.T) {
}

// Make the HTTP request
req, err := http.NewRequest("GET", "/v1/nodes?prefix=aaa", nil)
req, err := http.NewRequest("GET", "/v1/nodes?prefix=12345678", nil)
if err != nil {
t.Fatalf("err: %v", err)
}
Expand All @@ -99,7 +106,7 @@ func TestHTTP_NodesPrefixList(t *testing.T) {

// Check the nodes
n := obj.([]*structs.NodeListStub)
if len(n) != 3 {
if len(n) != 2 {
t.Fatalf("bad: %#v", n)
}
})
Expand Down
28 changes: 19 additions & 9 deletions command/alloc_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ General Options:
` + generalOptionsUsage() + `
Alloc Status Options:
-short
Display short output. Shows only the most recent task event.
-verbose
Show full information.
`

return strings.TrimSpace(helpText)
Expand All @@ -40,11 +42,12 @@ func (c *AllocStatusCommand) Synopsis() string {
}

func (c *AllocStatusCommand) Run(args []string) int {
var short bool
var short, verbose bool

flags := c.Meta.FlagSet("alloc-status", FlagSetClient)
flags.Usage = func() { c.Ui.Output(c.Help()) }
flags.BoolVar(&short, "short", false, "")
flags.BoolVar(&verbose, "verbose", false, "")

if err := flags.Parse(args); err != nil {
return 1
Expand All @@ -65,6 +68,12 @@ func (c *AllocStatusCommand) Run(args []string) int {
return 1
}

// Truncate the id unless full length is requested
length := shortId
if verbose {
length = fullId
}

// Query the allocation info
alloc, _, err := client.Allocations().Info(allocID, nil)
if err != nil {
Expand All @@ -83,12 +92,13 @@ func (c *AllocStatusCommand) Run(args []string) int {
out[0] = "ID|EvalID|JobID|TaskGroup|DesiredStatus|ClientStatus"
for i, alloc := range allocs {
out[i+1] = fmt.Sprintf("%s|%s|%s|%s|%s|%s",
alloc.ID,
alloc.EvalID,
alloc.ID[:length],
alloc.EvalID[:length],
alloc.JobID,
alloc.TaskGroup,
alloc.DesiredStatus,
alloc.ClientStatus)
alloc.ClientStatus,
)
}
c.Ui.Output(fmt.Sprintf("Prefix matched multiple allocations\n\n%s", formatList(out)))
return 0
Expand All @@ -103,10 +113,10 @@ func (c *AllocStatusCommand) Run(args []string) int {

// Format the allocation data
basic := []string{
fmt.Sprintf("ID|%s", alloc.ID),
fmt.Sprintf("EvalID|%s", alloc.EvalID),
fmt.Sprintf("ID|%s", alloc.ID[:length]),
fmt.Sprintf("EvalID|%s", alloc.EvalID[:length]),
fmt.Sprintf("Name|%s", alloc.Name),
fmt.Sprintf("NodeID|%s", alloc.NodeID),
fmt.Sprintf("NodeID|%s", alloc.NodeID[:length]),
fmt.Sprintf("JobID|%s", alloc.JobID),
fmt.Sprintf("ClientStatus|%s", alloc.ClientStatus),
fmt.Sprintf("NodesEvaluated|%d", alloc.Metrics.NodesEvaluated),
Expand All @@ -126,7 +136,7 @@ func (c *AllocStatusCommand) Run(args []string) int {

// Format the detailed status
c.Ui.Output("\n==> Status")
dumpAllocStatus(c.Ui, alloc)
dumpAllocStatus(c.Ui, alloc, length)

return 0
}
Expand Down
20 changes: 18 additions & 2 deletions command/eval_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ Usage: nomad eval-monitor [options] <evaluation>
General Options:
` + generalOptionsUsage()
` + generalOptionsUsage() + `
Eval Monitor Options:
-verbose
Show full information.
`
return strings.TrimSpace(helpText)
}

Expand All @@ -38,12 +44,22 @@ func (c *EvalMonitorCommand) Synopsis() string {
}

func (c *EvalMonitorCommand) Run(args []string) int {
var verbose bool

flags := c.Meta.FlagSet("eval-monitor", FlagSetClient)
flags.Usage = func() { c.Ui.Output(c.Help()) }
flags.BoolVar(&verbose, "verbose", false, "")

if err := flags.Parse(args); err != nil {
return 1
}

// Truncate the id unless full length is requested
length := shortId
if verbose {
length = fullId
}

// Check that we got exactly one eval ID
args = flags.Args()
if len(args) != 1 {
Expand All @@ -60,6 +76,6 @@ func (c *EvalMonitorCommand) Run(args []string) int {
}

// Start monitoring
mon := newMonitor(c.Ui, client)
mon := newMonitor(c.Ui, client, length)
return mon.monitor(evalID, true)
}
2 changes: 1 addition & 1 deletion command/eval_monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestEvalMonitorCommand_Fails(t *testing.T) {
ui.ErrorWriter.Reset()

// Fails on connection failure
if code := cmd.Run([]string{"-address=nope", "nope"}); code != 1 {
if code := cmd.Run([]string{"-address=nope", "12345678-abcd-efab-cdef-123456789abc"}); code != 1 {
t.Fatalf("expected exit code 1, got: %d", code)
}
if out := ui.ErrorWriter.String(); !strings.Contains(out, "Error reading evaluation") {
Expand Down
4 changes: 4 additions & 0 deletions command/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ const (
// Names of environment variables used to supply various
// config options to the Nomad CLI.
EnvNomadAddress = "NOMAD_ADDR"

// Constants for CLI identifier length
shortId = 8
fullId = 36
)

// FlagSetFlags is an enum to define what flags are present in the
Expand Down
Loading

0 comments on commit 0d29e30

Please sign in to comment.