2016-07-18 18:39:38 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
2016-07-20 02:48:16 +00:00
|
|
|
"time"
|
2016-07-18 18:39:38 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/api"
|
2017-08-22 20:11:32 +00:00
|
|
|
"github.com/hashicorp/nomad/api/contexts"
|
|
|
|
"github.com/posener/complete"
|
2016-07-18 18:39:38 +00:00
|
|
|
)
|
|
|
|
|
2018-03-21 00:37:28 +00:00
|
|
|
type AllocLogsCommand struct {
|
2016-07-18 18:39:38 +00:00
|
|
|
Meta
|
|
|
|
}
|
|
|
|
|
2018-03-21 00:37:28 +00:00
|
|
|
func (l *AllocLogsCommand) Help() string {
|
2016-07-18 18:39:38 +00:00
|
|
|
helpText := `
|
2018-03-21 00:37:28 +00:00
|
|
|
Usage: nomad alloc logs [options] <allocation> <task>
|
2018-03-21 01:28:14 +00:00
|
|
|
Alias: nomad logs
|
2016-07-18 18:39:38 +00:00
|
|
|
|
2016-07-20 15:53:59 +00:00
|
|
|
Streams the stdout/stderr of the given allocation and task.
|
2016-07-18 18:39:38 +00:00
|
|
|
|
|
|
|
General Options:
|
|
|
|
|
2016-07-20 22:18:54 +00:00
|
|
|
` + generalOptionsUsage() + `
|
2016-07-18 18:39:38 +00:00
|
|
|
|
|
|
|
Logs Specific Options:
|
|
|
|
|
2016-12-25 01:12:16 +00:00
|
|
|
-stderr
|
2016-07-25 20:08:39 +00:00
|
|
|
Display stderr logs.
|
|
|
|
|
2016-07-18 18:39:38 +00:00
|
|
|
-verbose
|
|
|
|
Show full information.
|
|
|
|
|
|
|
|
-job <job-id>
|
2016-07-20 22:18:54 +00:00
|
|
|
Use a random allocation from the specified job ID.
|
2016-07-18 18:39:38 +00:00
|
|
|
|
2016-07-20 17:18:05 +00:00
|
|
|
-f
|
|
|
|
Causes the output to not stop when the end of the logs are reached, but
|
|
|
|
rather to wait for additional output.
|
|
|
|
|
2016-07-20 02:48:16 +00:00
|
|
|
-tail
|
2016-07-25 20:08:39 +00:00
|
|
|
Show the logs contents with offsets relative to the end of the logs. If no
|
2016-07-20 15:53:59 +00:00
|
|
|
offset is given, -n is defaulted to 10.
|
2016-07-18 18:39:38 +00:00
|
|
|
|
|
|
|
-n
|
2016-07-20 15:53:59 +00:00
|
|
|
Sets the tail location in best-efforted number of lines relative to the end
|
2016-07-25 20:08:39 +00:00
|
|
|
of the logs.
|
2016-07-18 18:39:38 +00:00
|
|
|
|
|
|
|
-c
|
2016-07-25 20:08:39 +00:00
|
|
|
Sets the tail location in number of bytes relative to the end of the logs.
|
2016-12-24 20:39:33 +00:00
|
|
|
`
|
2016-07-18 18:39:38 +00:00
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
2018-03-21 00:37:28 +00:00
|
|
|
func (l *AllocLogsCommand) Synopsis() string {
|
2016-07-20 15:53:59 +00:00
|
|
|
return "Streams the logs of a task."
|
2016-07-18 18:39:38 +00:00
|
|
|
}
|
|
|
|
|
2018-03-21 00:37:28 +00:00
|
|
|
func (c *AllocLogsCommand) AutocompleteFlags() complete.Flags {
|
2017-08-23 21:56:21 +00:00
|
|
|
return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
|
|
|
|
complete.Flags{
|
|
|
|
"-stderr": complete.PredictNothing,
|
|
|
|
"-verbose": complete.PredictNothing,
|
|
|
|
"-job": complete.PredictAnything,
|
|
|
|
"-f": complete.PredictNothing,
|
|
|
|
"-tail": complete.PredictAnything,
|
|
|
|
"-n": complete.PredictAnything,
|
|
|
|
"-c": complete.PredictAnything,
|
|
|
|
})
|
2017-08-22 20:11:32 +00:00
|
|
|
}
|
|
|
|
|
2018-03-21 00:37:28 +00:00
|
|
|
func (l *AllocLogsCommand) AutocompleteArgs() complete.Predictor {
|
2017-08-22 20:11:32 +00:00
|
|
|
return complete.PredictFunc(func(a complete.Args) []string {
|
2017-08-29 21:29:32 +00:00
|
|
|
client, err := l.Meta.Client()
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-28 05:17:51 +00:00
|
|
|
resp, _, err := client.Search().PrefixSearch(a.Last, contexts.Allocs, nil)
|
2017-08-22 20:11:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
return resp.Matches[contexts.Allocs]
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-03-21 00:37:28 +00:00
|
|
|
func (l *AllocLogsCommand) Run(args []string) int {
|
2016-07-20 17:18:05 +00:00
|
|
|
var verbose, job, tail, stderr, follow bool
|
2016-07-18 18:39:38 +00:00
|
|
|
var numLines, numBytes int64
|
|
|
|
|
2018-03-21 00:37:28 +00:00
|
|
|
flags := l.Meta.FlagSet("alloc logs", FlagSetClient)
|
2016-07-18 18:39:38 +00:00
|
|
|
flags.Usage = func() { l.Ui.Output(l.Help()) }
|
|
|
|
flags.BoolVar(&verbose, "verbose", false, "")
|
|
|
|
flags.BoolVar(&job, "job", false, "")
|
|
|
|
flags.BoolVar(&tail, "tail", false, "")
|
2016-07-20 17:18:05 +00:00
|
|
|
flags.BoolVar(&follow, "f", false, "")
|
2016-07-18 18:39:38 +00:00
|
|
|
flags.BoolVar(&stderr, "stderr", false, "")
|
|
|
|
flags.Int64Var(&numLines, "n", -1, "")
|
|
|
|
flags.Int64Var(&numBytes, "c", -1, "")
|
|
|
|
|
|
|
|
if err := flags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
args = flags.Args()
|
|
|
|
|
2016-07-20 22:18:54 +00:00
|
|
|
if numArgs := len(args); numArgs < 1 {
|
2016-07-18 18:39:38 +00:00
|
|
|
if job {
|
2016-07-20 22:18:54 +00:00
|
|
|
l.Ui.Error("Job ID required. See help:\n")
|
2016-07-18 18:39:38 +00:00
|
|
|
} else {
|
2016-07-20 22:18:54 +00:00
|
|
|
l.Ui.Error("Allocation ID required. See help:\n")
|
2016-07-18 18:39:38 +00:00
|
|
|
}
|
|
|
|
|
2016-07-20 22:18:54 +00:00
|
|
|
l.Ui.Error(l.Help())
|
|
|
|
return 1
|
|
|
|
} else if numArgs > 2 {
|
|
|
|
l.Ui.Error(l.Help())
|
2016-07-18 18:39:38 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := l.Meta.Client()
|
|
|
|
if err != nil {
|
|
|
|
l.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 {
|
|
|
|
l.Ui.Error(fmt.Sprintf("Error fetching allocations: %v", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Truncate the id unless full length is requested
|
|
|
|
length := shortId
|
|
|
|
if verbose {
|
|
|
|
length = fullId
|
|
|
|
}
|
|
|
|
// Query the allocation info
|
|
|
|
if len(allocID) == 1 {
|
|
|
|
l.Ui.Error(fmt.Sprintf("Alloc ID must contain at least two characters."))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2018-03-11 18:52:59 +00:00
|
|
|
allocID = sanitizeUUIDPrefix(allocID)
|
2016-07-18 18:39:38 +00:00
|
|
|
allocs, _, err := client.Allocations().PrefixList(allocID)
|
|
|
|
if err != nil {
|
|
|
|
l.Ui.Error(fmt.Sprintf("Error querying allocation: %v", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
if len(allocs) == 0 {
|
|
|
|
l.Ui.Error(fmt.Sprintf("No allocation(s) with prefix or id %q found", allocID))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
if len(allocs) > 1 {
|
|
|
|
// Format the allocs
|
2017-07-07 04:51:13 +00:00
|
|
|
out := formatAllocListStubs(allocs, verbose, length)
|
2017-07-21 00:31:07 +00:00
|
|
|
l.Ui.Error(fmt.Sprintf("Prefix matched multiple allocations\n\n%s", out))
|
|
|
|
return 1
|
2016-07-18 18:39:38 +00:00
|
|
|
}
|
|
|
|
// Prefix lookup matched a single allocation
|
|
|
|
alloc, _, err := client.Allocations().Info(allocs[0].ID, nil)
|
|
|
|
if err != nil {
|
|
|
|
l.Ui.Error(fmt.Sprintf("Error querying allocation: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2016-07-20 16:13:48 +00:00
|
|
|
var task string
|
|
|
|
if len(args) >= 2 {
|
|
|
|
task = args[1]
|
|
|
|
if task == "" {
|
|
|
|
l.Ui.Error("Task name required")
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// Try to determine the tasks name from the allocation
|
|
|
|
var tasks []*api.Task
|
|
|
|
for _, tg := range alloc.Job.TaskGroups {
|
2017-02-06 19:48:28 +00:00
|
|
|
if *tg.Name == alloc.TaskGroup {
|
2016-07-20 16:13:48 +00:00
|
|
|
if len(tg.Tasks) == 1 {
|
|
|
|
task = tg.Tasks[0].Name
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
tasks = tg.Tasks
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if task == "" {
|
|
|
|
l.Ui.Error(fmt.Sprintf("Allocation %q is running the following tasks:", limit(alloc.ID, length)))
|
|
|
|
for _, t := range tasks {
|
|
|
|
l.Ui.Error(fmt.Sprintf(" * %s", t.Name))
|
|
|
|
}
|
|
|
|
l.Ui.Error("\nPlease specify the task.")
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-18 18:39:38 +00:00
|
|
|
logType := "stdout"
|
|
|
|
if stderr {
|
|
|
|
logType = "stderr"
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have a file, output it.
|
|
|
|
var r io.ReadCloser
|
|
|
|
var readErr error
|
|
|
|
if !tail {
|
2016-07-20 17:18:05 +00:00
|
|
|
r, readErr = l.followFile(client, alloc, follow, task, logType, api.OriginStart, 0)
|
2016-07-18 18:39:38 +00:00
|
|
|
if readErr != nil {
|
|
|
|
readErr = fmt.Errorf("Error reading file: %v", readErr)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Parse the offset
|
|
|
|
var offset int64 = defaultTailLines * bytesToLines
|
|
|
|
|
|
|
|
if nLines, nBytes := numLines != -1, numBytes != -1; nLines && nBytes {
|
|
|
|
l.Ui.Error("Both -n and -c set")
|
|
|
|
return 1
|
|
|
|
} else if nLines {
|
|
|
|
offset = numLines * bytesToLines
|
|
|
|
} else if nBytes {
|
|
|
|
offset = numBytes
|
|
|
|
} else {
|
|
|
|
numLines = defaultTailLines
|
|
|
|
}
|
|
|
|
|
2016-07-20 17:18:05 +00:00
|
|
|
r, readErr = l.followFile(client, alloc, follow, task, logType, api.OriginEnd, offset)
|
2016-07-18 18:39:38 +00:00
|
|
|
|
|
|
|
// If numLines is set, wrap the reader
|
|
|
|
if numLines != -1 {
|
2016-07-20 02:48:16 +00:00
|
|
|
r = NewLineLimitReader(r, int(numLines), int(numLines*bytesToLines), 1*time.Second)
|
2016-07-18 18:39:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if readErr != nil {
|
|
|
|
readErr = fmt.Errorf("Error tailing file: %v", readErr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if readErr != nil {
|
|
|
|
l.Ui.Error(readErr.Error())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2016-07-28 21:24:01 +00:00
|
|
|
defer r.Close()
|
2017-09-19 12:59:05 +00:00
|
|
|
_, err = io.Copy(os.Stdout, r)
|
|
|
|
if err != nil {
|
|
|
|
l.Ui.Error(fmt.Sprintf("error following logs: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2016-07-18 18:39:38 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// followFile outputs the contents of the file to stdout relative to the end of
|
2016-07-19 22:58:02 +00:00
|
|
|
// the file.
|
2018-03-21 00:37:28 +00:00
|
|
|
func (l *AllocLogsCommand) followFile(client *api.Client, alloc *api.Allocation,
|
2016-07-20 17:18:05 +00:00
|
|
|
follow bool, task, logType, origin string, offset int64) (io.ReadCloser, error) {
|
2016-07-18 18:39:38 +00:00
|
|
|
|
|
|
|
cancel := make(chan struct{})
|
2017-09-19 12:59:05 +00:00
|
|
|
frames, errCh := client.AllocFS().Logs(alloc, follow, task, logType, origin, offset, cancel, nil)
|
|
|
|
select {
|
|
|
|
case err := <-errCh:
|
2016-07-18 18:39:38 +00:00
|
|
|
return nil, err
|
2017-09-19 12:59:05 +00:00
|
|
|
default:
|
2016-07-18 18:39:38 +00:00
|
|
|
}
|
|
|
|
signalCh := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM)
|
|
|
|
|
|
|
|
// Create a reader
|
|
|
|
var r io.ReadCloser
|
2017-09-19 12:59:05 +00:00
|
|
|
frameReader := api.NewFrameReader(frames, errCh, cancel)
|
2016-07-20 02:48:16 +00:00
|
|
|
frameReader.SetUnblockTime(500 * time.Millisecond)
|
2016-07-18 18:39:38 +00:00
|
|
|
r = frameReader
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
<-signalCh
|
|
|
|
|
|
|
|
// End the streaming
|
|
|
|
r.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
return r, nil
|
|
|
|
}
|