2015-09-15 23:44:38 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2016-07-13 19:23:33 +00:00
|
|
|
"bytes"
|
2016-04-11 22:20:49 +00:00
|
|
|
"fmt"
|
2016-07-13 19:23:33 +00:00
|
|
|
"io"
|
2016-08-10 14:30:19 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2016-06-10 18:02:15 +00:00
|
|
|
"strconv"
|
2016-01-27 22:56:17 +00:00
|
|
|
"time"
|
|
|
|
|
2016-08-10 14:30:19 +00:00
|
|
|
gg "github.com/hashicorp/go-getter"
|
2016-04-11 22:20:49 +00:00
|
|
|
"github.com/hashicorp/nomad/api"
|
2016-08-10 14:30:19 +00:00
|
|
|
"github.com/hashicorp/nomad/jobspec"
|
|
|
|
|
2015-09-15 23:44:38 +00:00
|
|
|
"github.com/ryanuber/columnize"
|
|
|
|
)
|
|
|
|
|
|
|
|
// formatKV takes a set of strings and formats them into properly
|
|
|
|
// aligned k = v pairs using the columnize library.
|
|
|
|
func formatKV(in []string) string {
|
|
|
|
columnConf := columnize.DefaultConfig()
|
2015-09-27 21:04:53 +00:00
|
|
|
columnConf.Empty = "<none>"
|
2015-09-15 23:44:38 +00:00
|
|
|
columnConf.Glue = " = "
|
|
|
|
return columnize.Format(in, columnConf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// formatList takes a set of strings and formats them into properly
|
|
|
|
// aligned output, replacing any blank fields with a placeholder
|
|
|
|
// for awk-ability.
|
|
|
|
func formatList(in []string) string {
|
|
|
|
columnConf := columnize.DefaultConfig()
|
|
|
|
columnConf.Empty = "<none>"
|
|
|
|
return columnize.Format(in, columnConf)
|
|
|
|
}
|
2016-01-27 18:42:10 +00:00
|
|
|
|
2016-03-11 00:20:51 +00:00
|
|
|
// formatListWithSpaces takes a set of strings and formats them into properly
|
|
|
|
// aligned output. It should be used sparingly since it doesn't replace empty
|
|
|
|
// values and hence not awk/sed friendly
|
|
|
|
func formatListWithSpaces(in []string) string {
|
|
|
|
columnConf := columnize.DefaultConfig()
|
|
|
|
return columnize.Format(in, columnConf)
|
|
|
|
}
|
|
|
|
|
2016-01-27 18:42:10 +00:00
|
|
|
// Limits the length of the string.
|
|
|
|
func limit(s string, length int) string {
|
|
|
|
if len(s) < length {
|
2016-03-11 03:02:39 +00:00
|
|
|
return s
|
2016-01-27 18:42:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return s[:length]
|
|
|
|
}
|
2016-01-27 22:56:17 +00:00
|
|
|
|
|
|
|
// formatTime formats the time to string based on RFC822
|
|
|
|
func formatTime(t time.Time) string {
|
2017-07-20 22:43:00 +00:00
|
|
|
if t.Unix() < 1 {
|
|
|
|
// It's more confusing to display the UNIX epoch or a zero value than nothing
|
|
|
|
return ""
|
|
|
|
}
|
2016-06-15 20:34:45 +00:00
|
|
|
return t.Format("01/02/06 15:04:05 MST")
|
2016-01-27 22:56:17 +00:00
|
|
|
}
|
2016-04-11 22:20:49 +00:00
|
|
|
|
2016-08-09 02:24:38 +00:00
|
|
|
// formatUnixNanoTime is a helper for formatting time for output.
|
|
|
|
func formatUnixNanoTime(nano int64) string {
|
|
|
|
t := time.Unix(0, nano)
|
|
|
|
return formatTime(t)
|
|
|
|
}
|
|
|
|
|
2016-06-15 22:26:58 +00:00
|
|
|
// formatTimeDifference takes two times and determines their duration difference
|
|
|
|
// truncating to a passed unit.
|
|
|
|
// E.g. formatTimeDifference(first=1m22s33ms, second=1m28s55ms, time.Second) -> 6s
|
|
|
|
func formatTimeDifference(first, second time.Time, d time.Duration) string {
|
|
|
|
return second.Truncate(d).Sub(first.Truncate(d)).String()
|
|
|
|
}
|
|
|
|
|
2016-04-11 22:20:49 +00:00
|
|
|
// getLocalNodeID returns the node ID of the local Nomad Client and an error if
|
|
|
|
// it couldn't be determined or the Agent is not running in Client mode.
|
|
|
|
func getLocalNodeID(client *api.Client) (string, error) {
|
|
|
|
info, err := client.Agent().Self()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Error querying agent info: %s", err)
|
|
|
|
}
|
2017-03-08 14:50:54 +00:00
|
|
|
clientStats, ok := info.Stats["client"]
|
2016-04-11 22:20:49 +00:00
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("Nomad not running in client mode")
|
|
|
|
}
|
|
|
|
|
2017-03-08 14:50:54 +00:00
|
|
|
nodeID, ok := clientStats["node_id"]
|
2016-04-11 22:20:49 +00:00
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("Failed to determine node ID")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodeID, nil
|
|
|
|
}
|
2016-06-10 18:02:15 +00:00
|
|
|
|
|
|
|
// evalFailureStatus returns whether the evaluation has failures and a string to
|
|
|
|
// display when presenting users with whether there are failures for the eval
|
|
|
|
func evalFailureStatus(eval *api.Evaluation) (string, bool) {
|
|
|
|
if eval == nil {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
hasFailures := len(eval.FailedTGAllocs) != 0
|
|
|
|
text := strconv.FormatBool(hasFailures)
|
|
|
|
if eval.Status == "blocked" {
|
|
|
|
text = "N/A - In Progress"
|
|
|
|
}
|
|
|
|
|
|
|
|
return text, hasFailures
|
|
|
|
}
|
2016-07-13 19:23:33 +00:00
|
|
|
|
|
|
|
// LineLimitReader wraps another reader and provides `tail -n` like behavior.
|
|
|
|
// LineLimitReader buffers up to the searchLimit and returns `-n` number of
|
|
|
|
// lines. After those lines have been returned, LineLimitReader streams the
|
|
|
|
// underlying ReadCloser
|
|
|
|
type LineLimitReader struct {
|
|
|
|
io.ReadCloser
|
|
|
|
lines int
|
|
|
|
searchLimit int
|
|
|
|
|
2016-07-20 02:48:16 +00:00
|
|
|
timeLimit time.Duration
|
|
|
|
lastRead time.Time
|
|
|
|
|
2016-07-13 19:23:33 +00:00
|
|
|
buffer *bytes.Buffer
|
|
|
|
bufFiled bool
|
|
|
|
foundLines bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewLineLimitReader takes the ReadCloser to wrap, the number of lines to find
|
2016-07-20 02:48:16 +00:00
|
|
|
// searching backwards in the first searchLimit bytes. timeLimit can optionally
|
|
|
|
// be specified by passing a non-zero duration. When set, the search for the
|
|
|
|
// last n lines is aborted if no data has been read in the duration. This
|
|
|
|
// can be used to flush what is had if no extra data is being received. When
|
|
|
|
// used, the underlying reader must not block forever and must periodically
|
|
|
|
// unblock even when no data has been read.
|
|
|
|
func NewLineLimitReader(r io.ReadCloser, lines, searchLimit int, timeLimit time.Duration) *LineLimitReader {
|
2016-07-13 19:23:33 +00:00
|
|
|
return &LineLimitReader{
|
|
|
|
ReadCloser: r,
|
|
|
|
searchLimit: searchLimit,
|
2016-07-20 02:48:16 +00:00
|
|
|
timeLimit: timeLimit,
|
2016-07-13 19:23:33 +00:00
|
|
|
lines: lines,
|
|
|
|
buffer: bytes.NewBuffer(make([]byte, 0, searchLimit)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *LineLimitReader) Read(p []byte) (n int, err error) {
|
|
|
|
// Fill up the buffer so we can find the correct number of lines.
|
|
|
|
if !l.bufFiled {
|
2016-07-20 02:48:16 +00:00
|
|
|
b := make([]byte, len(p))
|
|
|
|
n, err := l.ReadCloser.Read(b)
|
|
|
|
if n > 0 {
|
|
|
|
if _, err := l.buffer.Write(b[:n]); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-13 19:23:33 +00:00
|
|
|
if err != nil {
|
2016-07-20 02:48:16 +00:00
|
|
|
if err != io.EOF {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
l.bufFiled = true
|
|
|
|
goto READ
|
|
|
|
}
|
|
|
|
|
|
|
|
if l.buffer.Len() >= l.searchLimit {
|
|
|
|
l.bufFiled = true
|
|
|
|
goto READ
|
|
|
|
}
|
|
|
|
|
|
|
|
if l.timeLimit.Nanoseconds() > 0 {
|
|
|
|
if l.lastRead.IsZero() {
|
|
|
|
l.lastRead = time.Now()
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
if n == 0 {
|
|
|
|
// We hit the limit
|
|
|
|
if l.lastRead.Add(l.timeLimit).Before(now) {
|
|
|
|
l.bufFiled = true
|
|
|
|
goto READ
|
|
|
|
} else {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
l.lastRead = now
|
|
|
|
}
|
2016-07-13 19:23:33 +00:00
|
|
|
}
|
|
|
|
|
2016-07-20 02:48:16 +00:00
|
|
|
return 0, nil
|
2016-07-13 19:23:33 +00:00
|
|
|
}
|
|
|
|
|
2016-07-20 02:48:16 +00:00
|
|
|
READ:
|
2016-07-13 19:23:33 +00:00
|
|
|
if l.bufFiled && l.buffer.Len() != 0 {
|
|
|
|
b := l.buffer.Bytes()
|
|
|
|
|
|
|
|
// Find the lines
|
|
|
|
if !l.foundLines {
|
|
|
|
found := 0
|
|
|
|
i := len(b) - 1
|
|
|
|
sep := byte('\n')
|
|
|
|
lastIndex := len(b) - 1
|
|
|
|
for ; found < l.lines && i >= 0; i-- {
|
|
|
|
if b[i] == sep {
|
|
|
|
lastIndex = i
|
|
|
|
|
|
|
|
// Skip the first one
|
|
|
|
if i != len(b)-1 {
|
|
|
|
found++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We found them all
|
|
|
|
if found == l.lines {
|
|
|
|
// Clear the buffer until the last index
|
|
|
|
l.buffer.Next(lastIndex + 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
l.foundLines = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read from the buffer
|
|
|
|
n := copy(p, l.buffer.Next(len(p)))
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Just stream from the underlying reader now
|
|
|
|
return l.ReadCloser.Read(p)
|
|
|
|
}
|
2016-08-10 14:30:19 +00:00
|
|
|
|
2016-08-16 10:49:14 +00:00
|
|
|
type JobGetter struct {
|
2016-08-10 14:30:19 +00:00
|
|
|
// The fields below can be overwritten for tests
|
|
|
|
testStdin io.Reader
|
|
|
|
}
|
|
|
|
|
|
|
|
// StructJob returns the Job struct from jobfile.
|
2017-02-06 19:48:28 +00:00
|
|
|
func (j *JobGetter) ApiJob(jpath string) (*api.Job, error) {
|
2016-08-10 14:30:19 +00:00
|
|
|
var jobfile io.Reader
|
|
|
|
switch jpath {
|
|
|
|
case "-":
|
2016-08-16 10:49:14 +00:00
|
|
|
if j.testStdin != nil {
|
|
|
|
jobfile = j.testStdin
|
2016-08-10 14:30:19 +00:00
|
|
|
} else {
|
|
|
|
jobfile = os.Stdin
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
if len(jpath) == 0 {
|
|
|
|
return nil, fmt.Errorf("Error jobfile path has to be specified.")
|
|
|
|
}
|
|
|
|
|
|
|
|
job, err := ioutil.TempFile("", "jobfile")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer os.Remove(job.Name())
|
|
|
|
|
2016-09-04 21:55:35 +00:00
|
|
|
if err := job.Close(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-08-10 14:30:19 +00:00
|
|
|
// Get the pwd
|
|
|
|
pwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
client := &gg.Client{
|
|
|
|
Src: jpath,
|
|
|
|
Pwd: pwd,
|
|
|
|
Dst: job.Name(),
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := client.Get(); err != nil {
|
|
|
|
return nil, fmt.Errorf("Error getting jobfile from %q: %v", jpath, err)
|
|
|
|
} else {
|
|
|
|
file, err := os.Open(job.Name())
|
|
|
|
defer file.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error opening file %q: %v", jpath, err)
|
|
|
|
}
|
|
|
|
jobfile = file
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the JobFile
|
|
|
|
jobStruct, err := jobspec.Parse(jobfile)
|
|
|
|
if err != nil {
|
2017-02-28 00:00:19 +00:00
|
|
|
return nil, fmt.Errorf("Error parsing job file from %s: %v", jpath, err)
|
2016-08-10 14:30:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return jobStruct, nil
|
|
|
|
}
|
2017-07-28 17:20:47 +00:00
|
|
|
|
|
|
|
// COMPAT: Remove in 0.7.0
|
|
|
|
// Nomad 0.6.0 introduces the submit time field so CLI's interacting with
|
|
|
|
// older versions of Nomad would SEGFAULT as reported here:
|
|
|
|
// https://github.com/hashicorp/nomad/issues/2918
|
|
|
|
// getSubmitTime returns a submit time of the job converting to time.Time
|
|
|
|
func getSubmitTime(job *api.Job) time.Time {
|
|
|
|
if job.SubmitTime != nil {
|
|
|
|
return time.Unix(0, *job.SubmitTime)
|
|
|
|
}
|
|
|
|
|
|
|
|
return time.Time{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// COMPAT: Remove in 0.7.0
|
|
|
|
// Nomad 0.6.0 introduces job Versions so CLI's interacting with
|
|
|
|
// older versions of Nomad would SEGFAULT as reported here:
|
|
|
|
// https://github.com/hashicorp/nomad/issues/2918
|
|
|
|
// getVersion returns a version of the job in safely.
|
|
|
|
func getVersion(job *api.Job) uint64 {
|
|
|
|
if job.Version != nil {
|
|
|
|
return *job.Version
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|