open-nomad/command/fs_ls.go

162 lines
3.7 KiB
Go
Raw Normal View History

2016-01-26 22:31:52 +00:00
package command
import (
"fmt"
"strings"
2016-02-12 23:26:11 +00:00
"github.com/dustin/go-humanize"
2016-01-26 22:31:52 +00:00
)
type FSListCommand struct {
Meta
}
func (f *FSListCommand) Help() string {
helpText := `
2016-01-31 19:10:45 +00:00
Usage: nomad fs ls <alloc-id> <path>
2016-01-26 22:31:52 +00:00
2016-01-28 00:29:29 +00:00
ls displays the contents of the allocation directory for the passed allocation. The path
is relative to the root of the alloc dir and defaults to root if unspecified.
2016-01-27 20:52:52 +00:00
General Options:
` + generalOptionsUsage() + `
2016-02-12 23:26:11 +00:00
Ls Options:
-H
Machine friendly output.
2016-01-27 20:52:52 +00:00
-verbose
Show full information.
2016-01-26 22:31:52 +00:00
`
return strings.TrimSpace(helpText)
}
func (f *FSListCommand) Synopsis() string {
return "List files in an allocation directory"
2016-01-26 22:31:52 +00:00
}
2016-01-26 23:03:26 +00:00
func (f *FSListCommand) Run(args []string) int {
2016-01-27 21:26:12 +00:00
var verbose bool
2016-02-12 23:26:11 +00:00
var machine bool
2016-01-26 23:03:26 +00:00
flags := f.Meta.FlagSet("fs-list", FlagSetClient)
flags.Usage = func() { f.Ui.Output(f.Help()) }
2016-01-27 20:52:52 +00:00
flags.BoolVar(&verbose, "verbose", false, "")
2016-02-12 23:26:11 +00:00
flags.BoolVar(&machine, "H", false, "")
2016-01-26 22:31:52 +00:00
if err := flags.Parse(args); err != nil {
return 1
}
args = flags.Args()
if len(args) < 1 {
f.Ui.Error("allocation id is a required parameter")
2016-01-26 22:31:52 +00:00
return 1
}
allocID := args[0]
path := "/"
if len(args) == 2 {
path = args[1]
}
2016-01-26 23:03:26 +00:00
client, err := f.Meta.Client()
2016-01-26 22:31:52 +00:00
if err != nil {
2016-01-26 23:03:26 +00:00
f.Ui.Error(fmt.Sprintf("Error inititalizing client: %v", err))
2016-01-26 22:31:52 +00:00
return 1
}
// Truncate the id unless full length is requested
length := shortId
if verbose {
length = fullId
}
2016-01-27 00:34:01 +00:00
// Query the allocation info
2016-01-26 22:31:52 +00:00
alloc, _, err := client.Allocations().Info(allocID, nil)
if err != nil {
2016-01-27 00:34:01 +00:00
if len(allocID) == 1 {
f.Ui.Error(fmt.Sprintf("Alloc ID must contain at least two characters."))
2016-01-27 00:34:01 +00:00
return 1
}
if len(allocID)%2 == 1 {
// Identifiers must be of even length, so we strip off the last byte
// to provide a consistent user experience.
allocID = allocID[:len(allocID)-1]
}
allocs, _, err := client.Allocations().PrefixList(allocID)
if err != nil {
f.Ui.Error(fmt.Sprintf("Error querying allocation: %v", err))
return 1
}
if len(allocs) == 0 {
f.Ui.Error(fmt.Sprintf("No allocation(s) with prefix or id %q found", allocID))
return 1
}
if len(allocs) > 1 {
// Format the allocs
out := make([]string, len(allocs)+1)
out[0] = "ID|Eval ID|Job ID|Task Group|Desired Status|Client Status"
for i, alloc := range allocs {
out[i+1] = fmt.Sprintf("%s|%s|%s|%s|%s|%s",
limit(alloc.ID, length),
limit(alloc.EvalID, length),
2016-01-27 00:34:01 +00:00
alloc.JobID,
alloc.TaskGroup,
alloc.DesiredStatus,
alloc.ClientStatus,
)
}
f.Ui.Output(fmt.Sprintf("Prefix matched multiple allocations\n\n%s", formatList(out)))
return 0
}
// Prefix lookup matched a single allocation
alloc, _, err = client.Allocations().Info(allocs[0].ID, nil)
if err != nil {
f.Ui.Error(fmt.Sprintf("Error querying allocation: %s", err))
return 1
}
2016-01-26 22:31:52 +00:00
}
if alloc.DesiredStatus == "failed" {
allocID := limit(alloc.ID, length)
msg := fmt.Sprintf(`The allocation %q failed to be placed. To see the cause, run:
nomad alloc-status %s`, allocID, allocID)
f.Ui.Error(msg)
return 0
}
// Get the file at the given path
2016-01-26 22:31:52 +00:00
files, _, err := client.AllocFS().List(alloc, path, nil)
if err != nil {
2016-01-26 23:03:26 +00:00
f.Ui.Error(fmt.Sprintf("Error listing alloc dir: %v", err))
2016-01-26 22:31:52 +00:00
return 1
}
2016-01-27 00:37:26 +00:00
// Display the file information in a tabular format
2016-01-26 22:31:52 +00:00
out := make([]string, len(files)+1)
2016-01-27 22:20:10 +00:00
out[0] = "Mode|Size|Modfied Time|Name"
2016-01-26 22:31:52 +00:00
for i, file := range files {
fn := file.Name
if file.IsDir {
fn = fmt.Sprintf("%s/", fn)
}
2016-02-12 23:26:11 +00:00
var size string
if machine {
size = fmt.Sprintf("%d", file.Size)
} else {
size = humanize.Bytes(uint64(file.Size))
}
out[i+1] = fmt.Sprintf("%s|%s|%s|%s",
2016-01-27 22:20:10 +00:00
file.FileMode,
2016-02-12 23:26:11 +00:00
size,
2016-01-27 22:56:17 +00:00
formatTime(file.ModTime),
2016-01-26 22:31:52 +00:00
fn,
2016-01-27 22:20:10 +00:00
)
2016-01-26 22:31:52 +00:00
}
2016-01-26 23:03:26 +00:00
f.Ui.Output(formatList(out))
2016-01-26 22:31:52 +00:00
return 0
}