Merge pull request #934 from hashicorp/f-random-job-allocation-fs
Allow fs commands to use job-id
This commit is contained in:
commit
b516362855
|
@ -1,6 +1,12 @@
|
|||
package command
|
||||
|
||||
import "github.com/mitchellh/cli"
|
||||
import (
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/nomad/api"
|
||||
"github.com/mitchellh/cli"
|
||||
)
|
||||
|
||||
type FSCommand struct {
|
||||
Meta
|
||||
|
@ -17,3 +23,23 @@ func (f *FSCommand) Synopsis() string {
|
|||
func (f *FSCommand) Run(args []string) int {
|
||||
return cli.RunResultHelp
|
||||
}
|
||||
|
||||
// Get Random Allocation ID from a known jobID. Prefer to use a running allocation,
|
||||
// but use a dead allocation if no running allocations are found
|
||||
func getRandomJobAlloc(client *api.Client, jobID string) (string, error) {
|
||||
var runningAllocs []*api.AllocationListStub
|
||||
allocs, _, err := client.Jobs().Allocations(jobID, nil)
|
||||
for _, v := range allocs {
|
||||
if v.ClientStatus == "running" {
|
||||
runningAllocs = append(runningAllocs, v)
|
||||
}
|
||||
}
|
||||
// If we don't have any allocations running, use dead allocations
|
||||
if len(runningAllocs) < 1 {
|
||||
runningAllocs = allocs
|
||||
}
|
||||
|
||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
allocID := runningAllocs[r.Intn(len(runningAllocs))].ID
|
||||
return allocID, err
|
||||
}
|
||||
|
|
|
@ -26,6 +26,9 @@ Cat Options:
|
|||
|
||||
-verbose
|
||||
Show full information.
|
||||
|
||||
-job <job-id>
|
||||
Use a random allocation from a specified job-id.
|
||||
`
|
||||
return strings.TrimSpace(helpText)
|
||||
}
|
||||
|
@ -35,10 +38,11 @@ func (f *FSCatCommand) Synopsis() string {
|
|||
}
|
||||
|
||||
func (f *FSCatCommand) Run(args []string) int {
|
||||
var verbose bool
|
||||
var verbose, job bool
|
||||
flags := f.Meta.FlagSet("fs-list", FlagSetClient)
|
||||
flags.Usage = func() { f.Ui.Output(f.Help()) }
|
||||
flags.BoolVar(&verbose, "verbose", false, "")
|
||||
flags.BoolVar(&job, "job", false, "")
|
||||
|
||||
if err := flags.Parse(args); err != nil {
|
||||
return 1
|
||||
|
@ -50,7 +54,6 @@ func (f *FSCatCommand) Run(args []string) int {
|
|||
return 1
|
||||
}
|
||||
|
||||
allocID := args[0]
|
||||
path := "/"
|
||||
if len(args) == 2 {
|
||||
path = args[1]
|
||||
|
@ -58,10 +61,19 @@ func (f *FSCatCommand) Run(args []string) int {
|
|||
|
||||
client, err := f.Meta.Client()
|
||||
if err != nil {
|
||||
f.Ui.Error(fmt.Sprintf("Error inititalizing client: %v", err))
|
||||
f.Ui.Error(fmt.Sprintf("Error initializing client: %v", err))
|
||||
return 1
|
||||
}
|
||||
|
||||
// If -job is specified, use random allocation, otherwise use provided allocation
|
||||
allocID := args[0]
|
||||
if job {
|
||||
allocID, err = getRandomJobAlloc(client, args[0])
|
||||
if err != nil {
|
||||
f.Ui.Error(fmt.Sprintf("Error querying API: %v", err))
|
||||
}
|
||||
}
|
||||
|
||||
// Truncate the id unless full length is requested
|
||||
length := shortId
|
||||
if verbose {
|
||||
|
|
|
@ -4,7 +4,7 @@ import (
|
|||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/dustin/go-humanize"
|
||||
humanize "github.com/dustin/go-humanize"
|
||||
)
|
||||
|
||||
type FSListCommand struct {
|
||||
|
@ -30,6 +30,9 @@ Ls Options:
|
|||
-verbose
|
||||
Show full information.
|
||||
|
||||
-job <job-id>
|
||||
Use a random allocation from a specified job-id.
|
||||
|
||||
`
|
||||
return strings.TrimSpace(helpText)
|
||||
}
|
||||
|
@ -41,10 +44,12 @@ func (f *FSListCommand) Synopsis() string {
|
|||
func (f *FSListCommand) Run(args []string) int {
|
||||
var verbose bool
|
||||
var machine bool
|
||||
var job bool
|
||||
flags := f.Meta.FlagSet("fs-list", FlagSetClient)
|
||||
flags.Usage = func() { f.Ui.Output(f.Help()) }
|
||||
flags.BoolVar(&verbose, "verbose", false, "")
|
||||
flags.BoolVar(&machine, "H", false, "")
|
||||
flags.BoolVar(&job, "job", false, "")
|
||||
|
||||
if err := flags.Parse(args); err != nil {
|
||||
return 1
|
||||
|
@ -56,7 +61,6 @@ func (f *FSListCommand) Run(args []string) int {
|
|||
return 1
|
||||
}
|
||||
|
||||
allocID := args[0]
|
||||
path := "/"
|
||||
if len(args) == 2 {
|
||||
path = args[1]
|
||||
|
@ -64,10 +68,20 @@ func (f *FSListCommand) Run(args []string) int {
|
|||
|
||||
client, err := f.Meta.Client()
|
||||
if err != nil {
|
||||
f.Ui.Error(fmt.Sprintf("Error inititalizing client: %v", err))
|
||||
f.Ui.Error(fmt.Sprintf("Error initializing client: %v", err))
|
||||
return 1
|
||||
}
|
||||
|
||||
// If -job is specified, use random allocation, otherwise use provided allocation
|
||||
allocID := args[0]
|
||||
if job {
|
||||
allocID, err = getRandomJobAlloc(client, args[0])
|
||||
if err != nil {
|
||||
f.Ui.Error(fmt.Sprintf("Error fetching allocations: %v", err))
|
||||
return 1
|
||||
}
|
||||
}
|
||||
|
||||
// Truncate the id unless full length is requested
|
||||
length := shortId
|
||||
if verbose {
|
||||
|
|
|
@ -4,7 +4,7 @@ import (
|
|||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/dustin/go-humanize"
|
||||
humanize "github.com/dustin/go-humanize"
|
||||
)
|
||||
|
||||
type FSStatCommand struct {
|
||||
|
@ -29,6 +29,9 @@ Stat Options:
|
|||
|
||||
-verbose
|
||||
Show full information.
|
||||
|
||||
-job <job-id>
|
||||
Use a random allocation from a specified job-id.
|
||||
`
|
||||
return strings.TrimSpace(helpText)
|
||||
}
|
||||
|
@ -40,10 +43,12 @@ func (f *FSStatCommand) Synopsis() string {
|
|||
func (f *FSStatCommand) Run(args []string) int {
|
||||
var verbose bool
|
||||
var machine bool
|
||||
var job bool
|
||||
flags := f.Meta.FlagSet("fs-list", FlagSetClient)
|
||||
flags.Usage = func() { f.Ui.Output(f.Help()) }
|
||||
flags.BoolVar(&verbose, "verbose", false, "")
|
||||
flags.BoolVar(&machine, "H", false, "")
|
||||
flags.BoolVar(&job, "job", false, "")
|
||||
|
||||
if err := flags.Parse(args); err != nil {
|
||||
return 1
|
||||
|
@ -55,7 +60,6 @@ func (f *FSStatCommand) Run(args []string) int {
|
|||
return 1
|
||||
}
|
||||
|
||||
allocID := args[0]
|
||||
path := "/"
|
||||
if len(args) == 2 {
|
||||
path = args[1]
|
||||
|
@ -63,10 +67,18 @@ func (f *FSStatCommand) Run(args []string) int {
|
|||
|
||||
client, err := f.Meta.Client()
|
||||
if err != nil {
|
||||
f.Ui.Error(fmt.Sprintf("Error inititalizing client: %v", err))
|
||||
f.Ui.Error(fmt.Sprintf("Error initializing client: %v", err))
|
||||
return 1
|
||||
}
|
||||
|
||||
allocID := args[0]
|
||||
if job {
|
||||
allocID, err = getRandomJobAlloc(client, args[0])
|
||||
if err != nil {
|
||||
f.Ui.Error(fmt.Sprintf("Error querying API: %v", err))
|
||||
}
|
||||
}
|
||||
|
||||
// Truncate the id unless full length is requested
|
||||
length := shortId
|
||||
if verbose {
|
||||
|
|
|
@ -23,7 +23,7 @@ nomad fs stat <alloc-id> <path>
|
|||
nomad fs cat <alloc-id> <path>
|
||||
```
|
||||
|
||||
A valid allocation id is necessary and the path is relative to the root of the allocation directory.
|
||||
A valid allocation id is necessary unless `-job` is specified and the path is relative to the root of the allocation directory.
|
||||
The path is optional and it defaults to `/` of the allocation directory
|
||||
|
||||
## Examples
|
||||
|
@ -50,3 +50,14 @@ $ nomad fs cat redis/local/redis.stdout
|
|||
6710:C 27 Jan 22:04:03.794 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
|
||||
6710:M 27 Jan 22:04:03.795 * Increased maximum number of open files to 10032 (it was originally set to 256).
|
||||
|
||||
## Using Job-ID instead of Alloc-ID
|
||||
|
||||
Passing `-job` into one of the `fs` commands will allow the `fs` command to randomly select an allocation ID from the specified job.
|
||||
|
||||
```
|
||||
nomad fs ls -job <job-id> <path>
|
||||
```
|
||||
|
||||
Nomad will prefer to select a running allocation ID for the job, but if no running allocations for the job are found, Nomad will use a dead allocation.
|
||||
|
||||
This can be useful for debugging a job that has multiple allocations, and it's not really required to use a specific allocation ID.
|
Loading…
Reference in New Issue