2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2015-09-15 23:44:38 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2019-12-16 14:50:10 +00:00
|
|
|
"bufio"
|
2016-07-13 19:23:33 +00:00
|
|
|
"bytes"
|
2022-04-21 20:20:36 +00:00
|
|
|
"encoding/json"
|
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
|
|
|
"os"
|
2020-10-21 14:22:08 +00:00
|
|
|
"path/filepath"
|
2016-06-10 18:02:15 +00:00
|
|
|
"strconv"
|
2017-08-29 17:04:02 +00:00
|
|
|
"strings"
|
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"
|
2022-04-21 20:20:36 +00:00
|
|
|
flaghelper "github.com/hashicorp/nomad/helper/flags"
|
2016-08-10 14:30:19 +00:00
|
|
|
"github.com/hashicorp/nomad/jobspec"
|
2020-10-21 14:22:08 +00:00
|
|
|
"github.com/hashicorp/nomad/jobspec2"
|
2018-03-21 17:16:13 +00:00
|
|
|
"github.com/kr/text"
|
2019-12-16 14:50:10 +00:00
|
|
|
"github.com/mitchellh/cli"
|
2017-08-23 19:53:15 +00:00
|
|
|
"github.com/posener/complete"
|
2015-09-15 23:44:38 +00:00
|
|
|
"github.com/ryanuber/columnize"
|
|
|
|
)
|
|
|
|
|
2023-04-11 13:45:08 +00:00
|
|
|
const (
|
|
|
|
formatJSON = "json"
|
|
|
|
formatHCL1 = "hcl1"
|
|
|
|
formatHCL2 = "hcl2"
|
|
|
|
)
|
|
|
|
|
2018-03-21 17:16:13 +00:00
|
|
|
// maxLineLength is the maximum width of any line.
|
|
|
|
const maxLineLength int = 78
|
|
|
|
|
2015-09-15 23:44:38 +00:00
|
|
|
// 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
|
|
|
|
2023-03-23 22:28:26 +00:00
|
|
|
// indentString returns the string s padded with the given number of empty
|
|
|
|
// spaces before each line except for the first one.
|
|
|
|
func indentString(s string, pad int) string {
|
|
|
|
prefix := strings.Repeat(" ", pad)
|
|
|
|
return strings.Join(strings.Split(s, "\n"), fmt.Sprintf("\n%s", prefix))
|
|
|
|
}
|
|
|
|
|
2018-03-21 17:16:13 +00:00
|
|
|
// wrapAtLengthWithPadding wraps the given text at the maxLineLength, taking
|
|
|
|
// into account any provided left padding.
|
|
|
|
func wrapAtLengthWithPadding(s string, pad int) string {
|
|
|
|
wrapped := text.Wrap(s, maxLineLength-pad)
|
|
|
|
lines := strings.Split(wrapped, "\n")
|
|
|
|
for i, line := range lines {
|
|
|
|
lines[i] = strings.Repeat(" ", pad) + line
|
|
|
|
}
|
|
|
|
return strings.Join(lines, "\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
// wrapAtLength wraps the given text to maxLineLength.
|
|
|
|
func wrapAtLength(s string) string {
|
|
|
|
return wrapAtLengthWithPadding(s, 0)
|
|
|
|
}
|
|
|
|
|
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 ""
|
|
|
|
}
|
2018-01-30 08:57:07 +00:00
|
|
|
// Return ISO_8601 time format GH-3806
|
|
|
|
return t.Format("2006-01-02T15:04:05Z07:00")
|
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()
|
|
|
|
}
|
|
|
|
|
2017-10-27 22:24:42 +00:00
|
|
|
// fmtInt formats v into the tail of buf.
|
|
|
|
// It returns the index where the output begins.
|
|
|
|
func fmtInt(buf []byte, v uint64) int {
|
|
|
|
w := len(buf)
|
|
|
|
for v > 0 {
|
|
|
|
w--
|
|
|
|
buf[w] = byte(v%10) + '0'
|
|
|
|
v /= 10
|
|
|
|
}
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
|
|
|
// prettyTimeDiff prints a human readable time difference.
|
|
|
|
// It uses abbreviated forms for each period - s for seconds, m for minutes, h for hours,
|
|
|
|
// d for days, mo for months, and y for years. Time difference is rounded to the nearest second,
|
|
|
|
// and the top two least granular periods are returned. For example, if the time difference
|
|
|
|
// is 10 months, 12 days, 3 hours and 2 seconds, the string "10mo12d" is returned. Zero values return the empty string
|
|
|
|
func prettyTimeDiff(first, second time.Time) string {
|
|
|
|
// handle zero values
|
2017-12-11 23:58:24 +00:00
|
|
|
if first.IsZero() || first.UnixNano() == 0 {
|
2017-10-27 22:24:42 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
// round to the nearest second
|
|
|
|
first = first.Round(time.Second)
|
|
|
|
second = second.Round(time.Second)
|
|
|
|
|
|
|
|
// calculate time difference in seconds
|
2018-03-02 23:43:49 +00:00
|
|
|
var d time.Duration
|
|
|
|
messageSuffix := "ago"
|
|
|
|
if second.Equal(first) || second.After(first) {
|
|
|
|
d = second.Sub(first)
|
|
|
|
} else {
|
|
|
|
d = first.Sub(second)
|
|
|
|
messageSuffix = "from now"
|
|
|
|
}
|
|
|
|
|
2017-10-27 22:24:42 +00:00
|
|
|
u := uint64(d.Seconds())
|
|
|
|
|
|
|
|
var buf [32]byte
|
|
|
|
w := len(buf)
|
|
|
|
secs := u % 60
|
|
|
|
|
|
|
|
// track indexes of various periods
|
|
|
|
var indexes []int
|
|
|
|
|
|
|
|
if secs > 0 {
|
|
|
|
w--
|
|
|
|
buf[w] = 's'
|
|
|
|
// u is now seconds
|
|
|
|
w = fmtInt(buf[:w], secs)
|
|
|
|
indexes = append(indexes, w)
|
|
|
|
}
|
|
|
|
u /= 60
|
|
|
|
// u is now minutes
|
|
|
|
if u > 0 {
|
|
|
|
mins := u % 60
|
|
|
|
if mins > 0 {
|
|
|
|
w--
|
|
|
|
buf[w] = 'm'
|
|
|
|
w = fmtInt(buf[:w], mins)
|
|
|
|
indexes = append(indexes, w)
|
|
|
|
}
|
|
|
|
u /= 60
|
|
|
|
// u is now hours
|
|
|
|
if u > 0 {
|
|
|
|
hrs := u % 24
|
|
|
|
if hrs > 0 {
|
|
|
|
w--
|
|
|
|
buf[w] = 'h'
|
|
|
|
w = fmtInt(buf[:w], hrs)
|
|
|
|
indexes = append(indexes, w)
|
|
|
|
}
|
|
|
|
u /= 24
|
|
|
|
}
|
|
|
|
// u is now days
|
|
|
|
if u > 0 {
|
|
|
|
days := u % 30
|
|
|
|
if days > 0 {
|
|
|
|
w--
|
|
|
|
buf[w] = 'd'
|
|
|
|
w = fmtInt(buf[:w], days)
|
|
|
|
indexes = append(indexes, w)
|
|
|
|
}
|
|
|
|
u /= 30
|
|
|
|
}
|
|
|
|
// u is now months
|
|
|
|
if u > 0 {
|
|
|
|
months := u % 12
|
|
|
|
if months > 0 {
|
|
|
|
w--
|
|
|
|
buf[w] = 'o'
|
|
|
|
w--
|
|
|
|
buf[w] = 'm'
|
|
|
|
w = fmtInt(buf[:w], months)
|
|
|
|
indexes = append(indexes, w)
|
|
|
|
}
|
|
|
|
u /= 12
|
|
|
|
}
|
|
|
|
// u is now years
|
|
|
|
if u > 0 {
|
|
|
|
w--
|
|
|
|
buf[w] = 'y'
|
|
|
|
w = fmtInt(buf[:w], u)
|
|
|
|
indexes = append(indexes, w)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
start := w
|
|
|
|
end := len(buf)
|
|
|
|
|
|
|
|
// truncate to the first two periods
|
|
|
|
num_periods := len(indexes)
|
|
|
|
if num_periods > 2 {
|
|
|
|
end = indexes[num_periods-3]
|
|
|
|
}
|
2017-11-14 22:40:34 +00:00
|
|
|
if start == end { //edge case when time difference is less than a second
|
2018-03-02 23:43:49 +00:00
|
|
|
return "0s " + messageSuffix
|
2017-11-14 22:40:34 +00:00
|
|
|
} else {
|
2018-03-02 23:43:49 +00:00
|
|
|
return string(buf[start:end]) + " " + messageSuffix
|
2017-11-14 22:40:34 +00:00
|
|
|
}
|
2017-10-27 22:24:42 +00:00
|
|
|
|
2017-10-26 14:21:05 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2022-04-21 20:20:36 +00:00
|
|
|
// JobGetter provides helpers for retrieving and parsing a jobpsec.
|
2016-08-16 10:49:14 +00:00
|
|
|
type JobGetter struct {
|
2022-04-21 20:20:36 +00:00
|
|
|
HCL1 bool
|
|
|
|
Vars flaghelper.StringFlag
|
|
|
|
VarFiles flaghelper.StringFlag
|
|
|
|
Strict bool
|
|
|
|
JSON bool
|
2020-10-21 14:22:08 +00:00
|
|
|
|
2016-08-10 14:30:19 +00:00
|
|
|
// The fields below can be overwritten for tests
|
|
|
|
testStdin io.Reader
|
|
|
|
}
|
|
|
|
|
2022-04-21 20:20:36 +00:00
|
|
|
func (j *JobGetter) Validate() error {
|
|
|
|
if j.HCL1 && j.Strict {
|
|
|
|
return fmt.Errorf("cannot parse job file as HCLv1 and HCLv2 strict.")
|
|
|
|
}
|
|
|
|
if j.HCL1 && j.JSON {
|
|
|
|
return fmt.Errorf("cannot parse job file as HCL and JSON.")
|
|
|
|
}
|
|
|
|
if len(j.Vars) > 0 && j.JSON {
|
|
|
|
return fmt.Errorf("cannot use variables with JSON files.")
|
|
|
|
}
|
|
|
|
if len(j.VarFiles) > 0 && j.JSON {
|
|
|
|
return fmt.Errorf("cannot use variables with JSON files.")
|
|
|
|
}
|
|
|
|
if len(j.Vars) > 0 && j.HCL1 {
|
|
|
|
return fmt.Errorf("cannot use variables with HCLv1.")
|
|
|
|
}
|
|
|
|
if len(j.VarFiles) > 0 && j.HCL1 {
|
|
|
|
return fmt.Errorf("cannot use variables with HCLv1.")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-30 09:08:12 +00:00
|
|
|
// ApiJob returns the Job struct from jobfile.
|
2023-04-11 13:45:08 +00:00
|
|
|
func (j *JobGetter) ApiJob(jpath string) (*api.JobSubmission, *api.Job, error) {
|
2022-04-21 20:20:36 +00:00
|
|
|
return j.Get(jpath)
|
|
|
|
}
|
|
|
|
|
2023-04-11 13:45:08 +00:00
|
|
|
func (j *JobGetter) Get(jpath string) (*api.JobSubmission, *api.Job, error) {
|
2016-08-10 14:30:19 +00:00
|
|
|
var jobfile io.Reader
|
2020-10-21 14:22:08 +00:00
|
|
|
pathName := filepath.Base(jpath)
|
2016-08-10 14:30:19 +00:00
|
|
|
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
|
|
|
|
}
|
2022-04-21 20:20:36 +00:00
|
|
|
pathName = "stdin"
|
2016-08-10 14:30:19 +00:00
|
|
|
default:
|
|
|
|
if len(jpath) == 0 {
|
2023-04-11 13:45:08 +00:00
|
|
|
return nil, nil, fmt.Errorf("Error jobfile path has to be specified.")
|
2016-08-10 14:30:19 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 20:20:36 +00:00
|
|
|
jobFile, err := os.CreateTemp("", "jobfile")
|
2016-08-10 14:30:19 +00:00
|
|
|
if err != nil {
|
2023-04-11 13:45:08 +00:00
|
|
|
return nil, nil, err
|
2016-08-10 14:30:19 +00:00
|
|
|
}
|
2022-04-21 20:20:36 +00:00
|
|
|
defer os.Remove(jobFile.Name())
|
2016-08-10 14:30:19 +00:00
|
|
|
|
2022-04-21 20:20:36 +00:00
|
|
|
if err := jobFile.Close(); err != nil {
|
2023-04-11 13:45:08 +00:00
|
|
|
return nil, nil, err
|
2016-09-04 21:55:35 +00:00
|
|
|
}
|
|
|
|
|
2016-08-10 14:30:19 +00:00
|
|
|
// Get the pwd
|
|
|
|
pwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
2023-04-11 13:45:08 +00:00
|
|
|
return nil, nil, err
|
2016-08-10 14:30:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
client := &gg.Client{
|
|
|
|
Src: jpath,
|
|
|
|
Pwd: pwd,
|
2022-04-21 20:20:36 +00:00
|
|
|
Dst: jobFile.Name(),
|
2022-05-03 22:38:32 +00:00
|
|
|
|
|
|
|
// This will prevent copying or writing files through symlinks
|
|
|
|
DisableSymlinks: true,
|
2016-08-10 14:30:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := client.Get(); err != nil {
|
2023-04-11 13:45:08 +00:00
|
|
|
return nil, nil, fmt.Errorf("Error getting jobfile from %q: %v", jpath, err)
|
2016-08-10 14:30:19 +00:00
|
|
|
} else {
|
2022-04-21 20:20:36 +00:00
|
|
|
file, err := os.Open(jobFile.Name())
|
2016-08-10 14:30:19 +00:00
|
|
|
if err != nil {
|
2023-04-11 13:45:08 +00:00
|
|
|
return nil, nil, fmt.Errorf("Error opening file %q: %v", jpath, err)
|
2016-08-10 14:30:19 +00:00
|
|
|
}
|
2019-11-13 13:18:20 +00:00
|
|
|
defer file.Close()
|
2016-08-10 14:30:19 +00:00
|
|
|
jobfile = file
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the JobFile
|
2023-04-11 13:45:08 +00:00
|
|
|
var jobStruct *api.Job // deserialized destination
|
|
|
|
var source bytes.Buffer // tee the original
|
|
|
|
var jobSubmission *api.JobSubmission // store the original and format
|
|
|
|
jobfile = io.TeeReader(jobfile, &source)
|
2020-10-21 14:22:08 +00:00
|
|
|
var err error
|
2022-04-21 20:20:36 +00:00
|
|
|
switch {
|
|
|
|
case j.HCL1:
|
2020-10-21 14:22:08 +00:00
|
|
|
jobStruct, err = jobspec.Parse(jobfile)
|
2023-04-11 13:45:08 +00:00
|
|
|
|
|
|
|
// include the hcl1 source as the submission
|
|
|
|
jobSubmission = &api.JobSubmission{
|
|
|
|
Source: source.String(),
|
|
|
|
Format: formatHCL1,
|
|
|
|
}
|
2022-04-21 20:20:36 +00:00
|
|
|
case j.JSON:
|
2023-04-11 13:45:08 +00:00
|
|
|
|
2022-04-21 20:20:36 +00:00
|
|
|
// Support JSON files with both a top-level Job key as well as
|
|
|
|
// ones without.
|
|
|
|
eitherJob := struct {
|
|
|
|
NestedJob *api.Job `json:"Job"`
|
|
|
|
api.Job
|
|
|
|
}{}
|
|
|
|
|
|
|
|
if err := json.NewDecoder(jobfile).Decode(&eitherJob); err != nil {
|
2023-04-11 13:45:08 +00:00
|
|
|
return nil, nil, fmt.Errorf("Failed to parse JSON job: %w", err)
|
2022-04-21 20:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if eitherJob.NestedJob != nil {
|
|
|
|
jobStruct = eitherJob.NestedJob
|
|
|
|
} else {
|
|
|
|
jobStruct = &eitherJob.Job
|
|
|
|
}
|
2023-04-11 13:45:08 +00:00
|
|
|
|
|
|
|
// include the json source as the submission
|
|
|
|
jobSubmission = &api.JobSubmission{
|
|
|
|
Source: source.String(),
|
|
|
|
Format: formatJSON,
|
|
|
|
}
|
2022-04-21 20:20:36 +00:00
|
|
|
default:
|
2023-05-11 13:04:33 +00:00
|
|
|
// we are parsing HCL2
|
|
|
|
|
|
|
|
// make a copy of the job file (or stdio)
|
2023-04-11 13:45:08 +00:00
|
|
|
if _, err = io.Copy(&source, jobfile); err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("Failed to parse HCL job: %w", err)
|
2020-11-09 20:01:31 +00:00
|
|
|
}
|
2023-04-11 13:45:08 +00:00
|
|
|
|
|
|
|
// we are parsing HCL2, whether from a file or stdio
|
2020-11-09 20:01:31 +00:00
|
|
|
jobStruct, err = jobspec2.ParseWithConfig(&jobspec2.ParseConfig{
|
2020-12-09 19:12:48 +00:00
|
|
|
Path: pathName,
|
2023-04-11 13:45:08 +00:00
|
|
|
Body: source.Bytes(),
|
2022-04-21 20:20:36 +00:00
|
|
|
ArgVars: j.Vars,
|
2020-12-09 19:12:48 +00:00
|
|
|
AllowFS: true,
|
2022-04-21 20:20:36 +00:00
|
|
|
VarFiles: j.VarFiles,
|
2020-12-11 18:32:51 +00:00
|
|
|
Envs: os.Environ(),
|
2022-04-21 20:20:36 +00:00
|
|
|
Strict: j.Strict,
|
2020-11-09 20:01:31 +00:00
|
|
|
})
|
2020-11-10 19:48:29 +00:00
|
|
|
|
2023-05-11 13:04:33 +00:00
|
|
|
var varFileCat string
|
|
|
|
var readVarFileErr error
|
|
|
|
if err == nil {
|
|
|
|
// combine any -var-file data into one big blob
|
|
|
|
varFileCat, readVarFileErr = extractVarFiles([]string(j.VarFiles))
|
|
|
|
if readVarFileErr != nil {
|
|
|
|
return nil, nil, fmt.Errorf("Failed to read var file(s): %w", readVarFileErr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-11 13:45:08 +00:00
|
|
|
// submit the job with the submission with content from -var flags
|
|
|
|
jobSubmission = &api.JobSubmission{
|
|
|
|
VariableFlags: extractVarFlags(j.Vars),
|
2023-05-11 13:04:33 +00:00
|
|
|
Variables: varFileCat,
|
2023-04-11 13:45:08 +00:00
|
|
|
Source: source.String(),
|
|
|
|
Format: formatHCL2,
|
|
|
|
}
|
2020-11-10 19:48:29 +00:00
|
|
|
if err != nil {
|
2023-04-11 13:45:08 +00:00
|
|
|
if _, merr := jobspec.Parse(&source); merr == nil {
|
|
|
|
return nil, nil, fmt.Errorf("Failed to parse using HCL 2. Use the HCL 1 parser with `nomad run -hcl1`, or address the following issues:\n%v", err)
|
2020-11-10 19:48:29 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-21 14:22:08 +00:00
|
|
|
}
|
2020-11-10 19:48:29 +00:00
|
|
|
|
2016-08-10 14:30:19 +00:00
|
|
|
if err != nil {
|
2023-04-11 13:45:08 +00:00
|
|
|
return nil, nil, fmt.Errorf("Error parsing job file from %s:\n%v", jpath, err)
|
2016-08-10 14:30:19 +00:00
|
|
|
}
|
|
|
|
|
2023-04-11 13:45:08 +00:00
|
|
|
return jobSubmission, jobStruct, nil
|
|
|
|
}
|
|
|
|
|
2023-05-11 13:04:33 +00:00
|
|
|
// extractVarFiles concatenates the content of each file in filenames and
|
|
|
|
// returns it all as one big content blob
|
|
|
|
func extractVarFiles(filenames []string) (string, error) {
|
|
|
|
var sb strings.Builder
|
|
|
|
for _, filename := range filenames {
|
|
|
|
b, err := os.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
sb.WriteString(string(b))
|
|
|
|
sb.WriteString("\n")
|
|
|
|
}
|
|
|
|
return sb.String(), nil
|
|
|
|
}
|
|
|
|
|
2023-04-11 13:45:08 +00:00
|
|
|
// extractVarFlags is used to parse the values of -var command line arguments
|
|
|
|
// and turn them into a map to be used for submission. The result is never
|
|
|
|
// nil for convenience.
|
|
|
|
func extractVarFlags(slice []string) map[string]string {
|
|
|
|
m := make(map[string]string, len(slice))
|
|
|
|
for _, s := range slice {
|
|
|
|
if tokens := strings.SplitN(s, "=", 2); len(tokens) == 1 {
|
|
|
|
m[tokens[0]] = ""
|
|
|
|
} else {
|
|
|
|
m[tokens[0]] = tokens[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return m
|
2016-08-10 14:30:19 +00:00
|
|
|
}
|
2017-07-28 17:20:47 +00:00
|
|
|
|
2017-08-23 19:53:15 +00:00
|
|
|
// mergeAutocompleteFlags is used to join multiple flag completion sets.
|
|
|
|
func mergeAutocompleteFlags(flags ...complete.Flags) complete.Flags {
|
|
|
|
merged := make(map[string]complete.Predictor, len(flags))
|
|
|
|
for _, f := range flags {
|
|
|
|
for k, v := range f {
|
|
|
|
merged[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return merged
|
|
|
|
}
|
2017-08-29 17:04:02 +00:00
|
|
|
|
2018-03-11 18:52:59 +00:00
|
|
|
// sanitizeUUIDPrefix is used to sanitize a UUID prefix. The returned result
|
2017-08-29 17:04:02 +00:00
|
|
|
// will be a truncated version of the prefix if the prefix would not be
|
2018-03-11 18:40:07 +00:00
|
|
|
// queryable.
|
2018-03-11 18:52:59 +00:00
|
|
|
func sanitizeUUIDPrefix(prefix string) string {
|
2017-08-29 17:04:02 +00:00
|
|
|
hyphens := strings.Count(prefix, "-")
|
|
|
|
length := len(prefix) - hyphens
|
|
|
|
remainder := length % 2
|
|
|
|
return prefix[:len(prefix)-remainder]
|
|
|
|
}
|
2018-04-18 16:02:11 +00:00
|
|
|
|
2022-12-21 22:21:48 +00:00
|
|
|
// commandErrorText is used to easily render the same messaging across commands
|
2018-04-18 16:02:11 +00:00
|
|
|
// when an error is printed.
|
|
|
|
func commandErrorText(cmd NamedCommand) string {
|
|
|
|
return fmt.Sprintf("For additional help try 'nomad %s -help'", cmd.Name())
|
|
|
|
}
|
2019-12-16 14:50:10 +00:00
|
|
|
|
|
|
|
// uiErrorWriter is a io.Writer that wraps underlying ui.ErrorWriter().
|
|
|
|
// ui.ErrorWriter expects full lines as inputs and it emits its own line breaks.
|
|
|
|
//
|
|
|
|
// uiErrorWriter scans input for individual lines to pass to ui.ErrorWriter. If data
|
|
|
|
// doesn't contain a new line, it buffers result until next new line or writer is closed.
|
|
|
|
type uiErrorWriter struct {
|
|
|
|
ui cli.Ui
|
|
|
|
buf bytes.Buffer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *uiErrorWriter) Write(data []byte) (int, error) {
|
|
|
|
read := 0
|
|
|
|
for len(data) != 0 {
|
|
|
|
a, token, err := bufio.ScanLines(data, false)
|
|
|
|
if err != nil {
|
|
|
|
return read, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if a == 0 {
|
|
|
|
r, err := w.buf.Write(data)
|
|
|
|
return read + r, err
|
|
|
|
}
|
|
|
|
|
|
|
|
w.ui.Error(w.buf.String() + string(token))
|
|
|
|
data = data[a:]
|
|
|
|
w.buf.Reset()
|
|
|
|
read += a
|
|
|
|
}
|
|
|
|
|
|
|
|
return read, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *uiErrorWriter) Close() error {
|
|
|
|
// emit what's remaining
|
|
|
|
if w.buf.Len() != 0 {
|
|
|
|
w.ui.Error(w.buf.String())
|
|
|
|
w.buf.Reset()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-11-28 09:51:45 +00:00
|
|
|
|
|
|
|
func loadDataSource(data string, testStdin io.Reader) (string, error) {
|
|
|
|
// Handle empty quoted shell parameters
|
|
|
|
if len(data) == 0 {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch data[0] {
|
|
|
|
case '@':
|
|
|
|
return loadFromFile(data[1:])
|
|
|
|
case '-':
|
|
|
|
if len(data) > 1 {
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
return loadFromStdin(testStdin)
|
|
|
|
default:
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadFromFile(path string) (string, error) {
|
|
|
|
data, err := os.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Failed to read file: %v", err)
|
|
|
|
}
|
|
|
|
return string(data), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadFromStdin(testStdin io.Reader) (string, error) {
|
|
|
|
var stdin io.Reader = os.Stdin
|
|
|
|
if testStdin != nil {
|
|
|
|
stdin = testStdin
|
|
|
|
}
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
if _, err := io.Copy(&b, stdin); err != nil {
|
|
|
|
return "", fmt.Errorf("Failed to read stdin: %v", err)
|
|
|
|
}
|
|
|
|
return b.String(), nil
|
|
|
|
}
|