2016-01-27 00:07:59 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-01-27 20:30:27 +00:00
|
|
|
"io"
|
2016-01-27 00:07:59 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type FSCatCommand struct {
|
|
|
|
Meta
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FSCatCommand) Help() string {
|
|
|
|
helpText := `
|
2016-01-31 19:10:45 +00:00
|
|
|
Usage: nomad fs cat <alloc-id> <path>
|
2016-01-27 00:07:59 +00:00
|
|
|
|
|
|
|
Dispays a file in an allocation directory at the given path.
|
2016-01-27 20:41:33 +00:00
|
|
|
The path is relative to the allocation directory 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
|
|
|
Cat Options:
|
|
|
|
|
2016-01-27 20:52:52 +00:00
|
|
|
-verbose
|
|
|
|
Show full information.
|
2016-03-18 19:05:35 +00:00
|
|
|
|
|
|
|
-job <job-id>
|
|
|
|
Use a random allocation from a specified job-id.
|
2016-01-27 20:52:52 +00:00
|
|
|
`
|
2016-01-27 00:07:59 +00:00
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FSCatCommand) Synopsis() string {
|
2016-02-05 19:01:29 +00:00
|
|
|
return "Cat a file in an allocation directory"
|
2016-01-27 00:07:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FSCatCommand) Run(args []string) int {
|
2016-03-18 19:05:35 +00:00
|
|
|
var verbose, job bool
|
2016-01-27 00:07:59 +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-03-18 19:05:35 +00:00
|
|
|
flags.BoolVar(&job, "job", false, "")
|
2016-01-27 00:07:59 +00:00
|
|
|
|
|
|
|
if err := flags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
args = flags.Args()
|
|
|
|
|
|
|
|
if len(args) < 1 {
|
2016-01-27 20:41:33 +00:00
|
|
|
f.Ui.Error("allocation id is a required parameter")
|
2016-01-27 00:07:59 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
path := "/"
|
|
|
|
if len(args) == 2 {
|
|
|
|
path = args[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := f.Meta.Client()
|
|
|
|
if err != nil {
|
2016-03-18 19:05:35 +00:00
|
|
|
f.Ui.Error(fmt.Sprintf("Error initializing client: %v", err))
|
2016-01-27 00:07:59 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2016-03-18 19:05:35 +00:00
|
|
|
// 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))
|
2016-04-07 20:56:17 +00:00
|
|
|
return 1
|
2016-03-18 19:05:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-27 20:56:38 +00:00
|
|
|
// 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-03-17 23:48:45 +00:00
|
|
|
if len(allocID) == 1 {
|
|
|
|
f.Ui.Error(fmt.Sprintf("Alloc ID must contain at least two characters."))
|
|
|
|
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]
|
|
|
|
}
|
2016-01-27 00:34:01 +00:00
|
|
|
|
2016-03-17 23:48:45 +00:00
|
|
|
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),
|
|
|
|
alloc.JobID,
|
|
|
|
alloc.TaskGroup,
|
|
|
|
alloc.DesiredStatus,
|
|
|
|
alloc.ClientStatus,
|
|
|
|
)
|
2016-01-27 00:34:01 +00:00
|
|
|
}
|
2016-03-17 23:48:45 +00:00
|
|
|
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-27 00:07:59 +00:00
|
|
|
}
|
|
|
|
|
2016-03-10 21:55:45 +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
|
|
|
|
}
|
2016-01-27 00:07:59 +00:00
|
|
|
|
2016-01-27 00:37:26 +00:00
|
|
|
// Get the contents of the file
|
2016-03-28 18:06:22 +00:00
|
|
|
r, _, err := client.AllocFS().Cat(alloc, path, nil)
|
2016-01-27 20:30:27 +00:00
|
|
|
if err != nil {
|
2016-01-27 00:07:59 +00:00
|
|
|
f.Ui.Error(fmt.Sprintf("Error reading file: %v", err))
|
|
|
|
return 1
|
|
|
|
}
|
2016-01-27 20:30:27 +00:00
|
|
|
io.Copy(os.Stdout, r)
|
2016-01-27 00:07:59 +00:00
|
|
|
return 0
|
|
|
|
}
|