2016-02-05 19:01:29 +00:00
|
|
|
package command
|
|
|
|
|
2016-03-18 19:05:35 +00:00
|
|
|
import (
|
2016-04-07 18:22:09 +00:00
|
|
|
"fmt"
|
2016-05-05 06:59:38 +00:00
|
|
|
"io"
|
2016-03-18 19:05:35 +00:00
|
|
|
"math/rand"
|
2016-05-05 06:59:38 +00:00
|
|
|
"os"
|
2016-07-07 18:51:40 +00:00
|
|
|
"os/signal"
|
2016-05-05 06:59:38 +00:00
|
|
|
"strings"
|
2016-07-07 18:51:40 +00:00
|
|
|
"syscall"
|
2016-03-18 19:05:35 +00:00
|
|
|
"time"
|
|
|
|
|
2016-05-05 06:59:38 +00:00
|
|
|
humanize "github.com/dustin/go-humanize"
|
2016-03-18 19:05:35 +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-03-18 19:05:35 +00:00
|
|
|
)
|
2016-02-05 19:01:29 +00:00
|
|
|
|
2016-07-07 18:51:40 +00:00
|
|
|
const (
|
|
|
|
// bytesToLines is an estimation of how many bytes are in each log line.
|
2016-07-12 16:45:05 +00:00
|
|
|
// This is used to set the offset to read from when a user specifies how
|
|
|
|
// many lines to tail from.
|
2016-07-07 18:51:40 +00:00
|
|
|
bytesToLines int64 = 120
|
2016-07-12 16:45:05 +00:00
|
|
|
|
|
|
|
// defaultTailLines is the number of lines to tail by default if the value
|
2017-08-07 21:13:05 +00:00
|
|
|
// is not overridden.
|
2016-07-12 16:45:05 +00:00
|
|
|
defaultTailLines int64 = 10
|
2016-07-07 18:51:40 +00:00
|
|
|
)
|
|
|
|
|
2018-03-21 00:37:28 +00:00
|
|
|
type AllocFSCommand struct {
|
2016-02-05 19:01:29 +00:00
|
|
|
Meta
|
|
|
|
}
|
|
|
|
|
2018-03-21 00:37:28 +00:00
|
|
|
func (f *AllocFSCommand) Help() string {
|
2016-05-05 06:59:38 +00:00
|
|
|
helpText := `
|
2018-03-21 00:37:28 +00:00
|
|
|
Usage: nomad alloc fs [options] <allocation> <path>
|
2018-03-21 01:28:14 +00:00
|
|
|
Alias: nomad fs
|
2016-05-05 06:59:38 +00:00
|
|
|
|
2020-11-19 21:38:08 +00:00
|
|
|
fs displays either the contents of an allocation directory for the passed
|
|
|
|
allocation, or displays the file at the given path. The path is relative to
|
|
|
|
the root of the alloc dir and defaults to root if unspecified.
|
|
|
|
|
|
|
|
When ACLs are enabled, this command requires a token with the 'read-fs',
|
|
|
|
'read-job', and 'list-jobs' capabilities for the allocation's namespace.
|
2016-05-05 06:59:38 +00:00
|
|
|
|
2016-05-16 17:17:37 +00:00
|
|
|
General Options:
|
2016-05-05 06:59:38 +00:00
|
|
|
|
2020-11-19 16:15:23 +00:00
|
|
|
` + generalOptionsUsage(usageOptsDefault) + `
|
2016-05-05 06:59:38 +00:00
|
|
|
|
2016-05-25 21:11:14 +00:00
|
|
|
FS Specific Options:
|
|
|
|
|
2016-05-05 06:59:38 +00:00
|
|
|
-H
|
|
|
|
Machine friendly output.
|
|
|
|
|
|
|
|
-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-05-05 06:59:38 +00:00
|
|
|
|
|
|
|
-stat
|
|
|
|
Show file stat information instead of displaying the file, or listing the directory.
|
|
|
|
|
2016-07-13 22:15:07 +00:00
|
|
|
-f
|
2016-08-01 12:46:09 +00:00
|
|
|
Causes the output to not stop when the end of the file is reached, but rather to
|
|
|
|
wait for additional output.
|
2016-07-13 22:15:07 +00:00
|
|
|
|
2016-12-24 20:39:33 +00:00
|
|
|
-tail
|
2016-07-20 15:53:59 +00:00
|
|
|
Show the files contents with offsets relative to the end of the file. If no
|
|
|
|
offset is given, -n is defaulted to 10.
|
2016-07-07 18:51:40 +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
|
|
|
|
of the file.
|
2016-07-07 18:51:40 +00:00
|
|
|
|
|
|
|
-c
|
2016-08-01 12:46:09 +00:00
|
|
|
Sets the tail location in number of bytes relative to the end of the file.
|
2016-05-05 06:59:38 +00:00
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
2016-02-05 19:01:29 +00:00
|
|
|
}
|
|
|
|
|
2018-03-21 00:37:28 +00:00
|
|
|
func (f *AllocFSCommand) Synopsis() string {
|
2016-02-05 19:01:29 +00:00
|
|
|
return "Inspect the contents of an allocation directory"
|
|
|
|
}
|
|
|
|
|
2021-12-20 10:44:21 +00:00
|
|
|
func (f *AllocFSCommand) AutocompleteFlags() complete.Flags {
|
|
|
|
return mergeAutocompleteFlags(f.Meta.AutocompleteFlags(FlagSetClient),
|
2017-08-23 21:56:21 +00:00
|
|
|
complete.Flags{
|
|
|
|
"-H": complete.PredictNothing,
|
|
|
|
"-verbose": complete.PredictNothing,
|
|
|
|
"-job": complete.PredictAnything,
|
|
|
|
"-stat": complete.PredictNothing,
|
|
|
|
"-f": complete.PredictNothing,
|
|
|
|
"-tail": complete.PredictNothing,
|
|
|
|
"-n": complete.PredictAnything,
|
|
|
|
"-c": complete.PredictAnything,
|
|
|
|
})
|
2017-08-22 20:11:32 +00:00
|
|
|
}
|
|
|
|
|
2018-03-21 00:37:28 +00:00
|
|
|
func (f *AllocFSCommand) 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 := f.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-04-18 16:02:11 +00:00
|
|
|
func (f *AllocFSCommand) Name() string { return "alloc fs" }
|
|
|
|
|
2018-03-21 00:37:28 +00:00
|
|
|
func (f *AllocFSCommand) Run(args []string) int {
|
2016-07-07 18:51:40 +00:00
|
|
|
var verbose, machine, job, stat, tail, follow bool
|
|
|
|
var numLines, numBytes int64
|
|
|
|
|
2018-04-18 16:02:11 +00:00
|
|
|
flags := f.Meta.FlagSet(f.Name(), FlagSetClient)
|
2016-05-05 06:59:38 +00:00
|
|
|
flags.Usage = func() { f.Ui.Output(f.Help()) }
|
|
|
|
flags.BoolVar(&verbose, "verbose", false, "")
|
|
|
|
flags.BoolVar(&machine, "H", false, "")
|
|
|
|
flags.BoolVar(&job, "job", false, "")
|
|
|
|
flags.BoolVar(&stat, "stat", false, "")
|
2016-07-07 18:51:40 +00:00
|
|
|
flags.BoolVar(&follow, "f", false, "")
|
|
|
|
flags.BoolVar(&tail, "tail", false, "")
|
|
|
|
flags.Int64Var(&numLines, "n", -1, "")
|
|
|
|
flags.Int64Var(&numBytes, "c", -1, "")
|
2016-05-05 06:59:38 +00:00
|
|
|
|
|
|
|
if err := flags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
args = flags.Args()
|
|
|
|
|
|
|
|
if len(args) < 1 {
|
2016-05-16 18:08:28 +00:00
|
|
|
if job {
|
2018-04-18 16:02:11 +00:00
|
|
|
f.Ui.Error("A job ID is required")
|
2016-05-16 18:08:28 +00:00
|
|
|
} else {
|
2018-04-18 16:02:11 +00:00
|
|
|
f.Ui.Error("An allocation ID is required")
|
2016-05-16 18:08:28 +00:00
|
|
|
}
|
2018-04-18 16:02:11 +00:00
|
|
|
f.Ui.Error(commandErrorText(f))
|
2016-07-26 13:45:09 +00:00
|
|
|
return 1
|
|
|
|
}
|
2016-05-16 18:08:28 +00:00
|
|
|
|
2016-07-26 13:45:09 +00:00
|
|
|
if len(args) > 2 {
|
2018-04-18 17:55:51 +00:00
|
|
|
f.Ui.Error("This command takes one or two arguments: <allocation> [<path>]")
|
2018-04-18 16:02:11 +00:00
|
|
|
f.Ui.Error(commandErrorText(f))
|
2016-05-05 06:59:38 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
path := "/"
|
|
|
|
if len(args) == 2 {
|
|
|
|
path = args[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := f.Meta.Client()
|
|
|
|
if err != nil {
|
|
|
|
f.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 {
|
2021-01-05 17:33:04 +00:00
|
|
|
allocID, err = getRandomJobAllocID(client, args[0])
|
2016-05-05 06:59:38 +00:00
|
|
|
if err != nil {
|
|
|
|
f.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 {
|
2020-12-09 19:05:18 +00:00
|
|
|
f.Ui.Error("Alloc ID must contain at least two characters.")
|
2016-05-05 06:59:38 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2018-03-11 18:52:59 +00:00
|
|
|
allocID = sanitizeUUIDPrefix(allocID)
|
2016-05-05 06:59:38 +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
|
2017-07-07 04:51:13 +00:00
|
|
|
out := formatAllocListStubs(allocs, verbose, length)
|
2017-07-21 00:31:07 +00:00
|
|
|
f.Ui.Error(fmt.Sprintf("Prefix matched multiple allocations\n\n%s", out))
|
|
|
|
return 1
|
2016-05-05 06:59:38 +00:00
|
|
|
}
|
|
|
|
// Prefix lookup matched a single allocation
|
2020-06-17 13:12:20 +00:00
|
|
|
q := &api.QueryOptions{Namespace: allocs[0].Namespace}
|
|
|
|
alloc, _, err := client.Allocations().Info(allocs[0].ID, q)
|
2016-05-05 06:59:38 +00:00
|
|
|
if err != nil {
|
|
|
|
f.Ui.Error(fmt.Sprintf("Error querying allocation: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get file stat info
|
|
|
|
file, _, err := client.AllocFS().Stat(alloc, path, nil)
|
|
|
|
if err != nil {
|
|
|
|
f.Ui.Error(err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we want file stats, print those and exit.
|
|
|
|
if stat {
|
|
|
|
// Display the file information
|
|
|
|
out := make([]string, 2)
|
2019-06-29 01:31:28 +00:00
|
|
|
out[0] = "Mode|Size|Modified Time|Content Type|Name"
|
2016-05-05 06:59:38 +00:00
|
|
|
if file != nil {
|
|
|
|
fn := file.Name
|
|
|
|
if file.IsDir {
|
|
|
|
fn = fmt.Sprintf("%s/", fn)
|
|
|
|
}
|
|
|
|
var size string
|
|
|
|
if machine {
|
|
|
|
size = fmt.Sprintf("%d", file.Size)
|
|
|
|
} else {
|
2016-06-12 21:20:39 +00:00
|
|
|
size = humanize.IBytes(uint64(file.Size))
|
2016-05-05 06:59:38 +00:00
|
|
|
}
|
2019-06-29 01:31:28 +00:00
|
|
|
out[1] = fmt.Sprintf("%s|%s|%s|%s|%s", file.FileMode, size,
|
|
|
|
formatTime(file.ModTime), file.ContentType, fn)
|
2016-05-05 06:59:38 +00:00
|
|
|
}
|
|
|
|
f.Ui.Output(formatList(out))
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine if the path is a file or a directory.
|
|
|
|
if file.IsDir {
|
|
|
|
// We have a directory, list it.
|
|
|
|
files, _, err := client.AllocFS().List(alloc, path, nil)
|
|
|
|
if err != nil {
|
|
|
|
f.Ui.Error(fmt.Sprintf("Error listing alloc dir: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
// Display the file information in a tabular format
|
|
|
|
out := make([]string, len(files)+1)
|
2016-06-13 22:09:13 +00:00
|
|
|
out[0] = "Mode|Size|Modified Time|Name"
|
2016-05-05 06:59:38 +00:00
|
|
|
for i, file := range files {
|
|
|
|
fn := file.Name
|
|
|
|
if file.IsDir {
|
|
|
|
fn = fmt.Sprintf("%s/", fn)
|
|
|
|
}
|
|
|
|
var size string
|
|
|
|
if machine {
|
|
|
|
size = fmt.Sprintf("%d", file.Size)
|
|
|
|
} else {
|
2016-06-12 21:20:39 +00:00
|
|
|
size = humanize.IBytes(uint64(file.Size))
|
2016-05-05 06:59:38 +00:00
|
|
|
}
|
|
|
|
out[i+1] = fmt.Sprintf("%s|%s|%s|%s",
|
|
|
|
file.FileMode,
|
|
|
|
size,
|
|
|
|
formatTime(file.ModTime),
|
|
|
|
fn,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
f.Ui.Output(formatList(out))
|
2016-07-07 18:51:40 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have a file, output it.
|
2016-07-13 21:33:17 +00:00
|
|
|
var r io.ReadCloser
|
|
|
|
var readErr error
|
2016-07-07 18:51:40 +00:00
|
|
|
if !tail {
|
2016-07-13 21:33:17 +00:00
|
|
|
if follow {
|
|
|
|
r, readErr = f.followFile(client, alloc, path, api.OriginStart, 0, -1)
|
|
|
|
} else {
|
2016-07-28 21:24:01 +00:00
|
|
|
r, readErr = client.AllocFS().Cat(alloc, path, nil)
|
2016-07-13 21:33:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if readErr != nil {
|
|
|
|
readErr = fmt.Errorf("Error reading file: %v", readErr)
|
2016-05-05 06:59:38 +00:00
|
|
|
}
|
2016-07-07 18:51:40 +00:00
|
|
|
} else {
|
|
|
|
// Parse the offset
|
2016-07-12 16:45:05 +00:00
|
|
|
var offset int64 = defaultTailLines * bytesToLines
|
2016-07-07 18:51:40 +00:00
|
|
|
|
|
|
|
if nLines, nBytes := numLines != -1, numBytes != -1; nLines && nBytes {
|
2016-08-01 12:46:09 +00:00
|
|
|
f.Ui.Error("Both -n and -c are not allowed")
|
|
|
|
return 1
|
|
|
|
} else if numLines < -1 || numBytes < -1 {
|
|
|
|
f.Ui.Error("Invalid size is specified")
|
2016-07-07 18:51:40 +00:00
|
|
|
return 1
|
|
|
|
} else if nLines {
|
|
|
|
offset = numLines * bytesToLines
|
|
|
|
} else if nBytes {
|
|
|
|
offset = numBytes
|
2016-07-13 19:23:33 +00:00
|
|
|
} else {
|
|
|
|
numLines = defaultTailLines
|
2016-07-07 18:51:40 +00:00
|
|
|
}
|
|
|
|
|
2016-07-07 21:04:36 +00:00
|
|
|
if offset > file.Size {
|
|
|
|
offset = file.Size
|
2016-07-07 18:51:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if follow {
|
2016-07-13 21:33:17 +00:00
|
|
|
r, readErr = f.followFile(client, alloc, path, api.OriginEnd, offset, numLines)
|
2016-07-07 18:51:40 +00:00
|
|
|
} else {
|
2016-07-07 21:04:36 +00:00
|
|
|
// This offset needs to be relative from the front versus the follow
|
|
|
|
// is relative to the end
|
|
|
|
offset = file.Size - offset
|
2016-07-28 21:24:01 +00:00
|
|
|
r, readErr = client.AllocFS().ReadAt(alloc, path, offset, -1, nil)
|
2016-07-13 19:23:33 +00:00
|
|
|
|
|
|
|
// If numLines is set, wrap the reader
|
|
|
|
if numLines != -1 {
|
2016-08-12 02:07:13 +00:00
|
|
|
r = NewLineLimitReader(r, int(numLines), int(numLines*bytesToLines), 1*time.Second)
|
2016-07-13 19:23:33 +00:00
|
|
|
}
|
2016-07-07 18:51:40 +00:00
|
|
|
}
|
|
|
|
|
2016-07-13 21:33:17 +00:00
|
|
|
if readErr != nil {
|
|
|
|
readErr = fmt.Errorf("Error tailing file: %v", readErr)
|
2016-07-07 18:51:40 +00:00
|
|
|
}
|
2016-05-05 06:59:38 +00:00
|
|
|
}
|
|
|
|
|
2016-10-24 18:14:05 +00:00
|
|
|
if r != nil {
|
|
|
|
defer r.Close()
|
|
|
|
}
|
2016-07-13 21:33:17 +00:00
|
|
|
if readErr != nil {
|
|
|
|
f.Ui.Error(readErr.Error())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-09-19 12:59:05 +00:00
|
|
|
_, err = io.Copy(os.Stdout, r)
|
|
|
|
if err != nil {
|
|
|
|
f.Ui.Error(fmt.Sprintf("error tailing file: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2016-05-05 06:59:38 +00:00
|
|
|
return 0
|
2016-02-05 19:01:29 +00:00
|
|
|
}
|
2016-03-18 19:05:35 +00:00
|
|
|
|
2016-07-07 18:51:40 +00:00
|
|
|
// followFile outputs the contents of the file to stdout relative to the end of
|
2016-07-13 19:23:33 +00:00
|
|
|
// the file. If numLines does not equal -1, then tail -n behavior is used.
|
2018-03-21 00:37:28 +00:00
|
|
|
func (f *AllocFSCommand) followFile(client *api.Client, alloc *api.Allocation,
|
2016-07-13 21:33:17 +00:00
|
|
|
path, origin string, offset, numLines int64) (io.ReadCloser, error) {
|
2016-07-07 18:51:40 +00:00
|
|
|
|
|
|
|
cancel := make(chan struct{})
|
2017-09-19 12:59:05 +00:00
|
|
|
frames, errCh := client.AllocFS().Stream(alloc, path, origin, offset, cancel, nil)
|
|
|
|
select {
|
|
|
|
case err := <-errCh:
|
2016-07-13 21:33:17 +00:00
|
|
|
return nil, err
|
2017-09-19 12:59:05 +00:00
|
|
|
default:
|
2016-07-07 18:51:40 +00:00
|
|
|
}
|
2016-07-12 16:45:05 +00:00
|
|
|
signalCh := make(chan os.Signal, 1)
|
2016-07-07 18:51:40 +00:00
|
|
|
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM)
|
|
|
|
|
2016-07-12 23:29:18 +00:00
|
|
|
// Create a reader
|
2016-07-13 19:23:33 +00:00
|
|
|
var r io.ReadCloser
|
2017-09-19 12:59:05 +00:00
|
|
|
frameReader := api.NewFrameReader(frames, errCh, cancel)
|
2016-08-12 02:07:13 +00:00
|
|
|
frameReader.SetUnblockTime(500 * time.Millisecond)
|
2016-07-13 19:23:33 +00:00
|
|
|
r = frameReader
|
|
|
|
|
|
|
|
// If numLines is set, wrap the reader
|
|
|
|
if numLines != -1 {
|
2016-08-12 02:07:13 +00:00
|
|
|
r = NewLineLimitReader(r, int(numLines), int(numLines*bytesToLines), 1*time.Second)
|
2016-07-13 19:23:33 +00:00
|
|
|
}
|
2016-07-07 18:51:40 +00:00
|
|
|
|
2016-07-12 23:29:18 +00:00
|
|
|
go func() {
|
|
|
|
<-signalCh
|
2016-07-07 18:51:40 +00:00
|
|
|
|
2016-07-12 23:29:18 +00:00
|
|
|
// End the streaming
|
|
|
|
r.Close()
|
|
|
|
}()
|
2016-07-07 18:51:40 +00:00
|
|
|
|
2016-07-13 21:33:17 +00:00
|
|
|
return r, nil
|
2016-07-07 18:51:40 +00:00
|
|
|
}
|
|
|
|
|
2021-01-05 17:33:04 +00:00
|
|
|
// Get Random Allocation from a known jobID. Prefer to use a running allocation,
|
2016-03-18 19:05:35 +00:00
|
|
|
// but use a dead allocation if no running allocations are found
|
2021-01-05 17:33:04 +00:00
|
|
|
func getRandomJobAlloc(client *api.Client, jobID string) (*api.AllocationListStub, error) {
|
2016-03-18 19:05:35 +00:00
|
|
|
var runningAllocs []*api.AllocationListStub
|
2016-11-24 12:20:52 +00:00
|
|
|
allocs, _, err := client.Jobs().Allocations(jobID, false, nil)
|
2021-06-18 20:26:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error querying job %q: %w", jobID, err)
|
|
|
|
}
|
2016-04-07 18:22:09 +00:00
|
|
|
|
|
|
|
// Check that the job actually has allocations
|
|
|
|
if len(allocs) == 0 {
|
2021-01-05 17:33:04 +00:00
|
|
|
return nil, fmt.Errorf("job %q doesn't exist or it has no allocations", jobID)
|
2016-04-07 18:22:09 +00:00
|
|
|
}
|
|
|
|
|
2016-03-18 19:05:35 +00:00
|
|
|
for _, v := range allocs {
|
|
|
|
if v.ClientStatus == "running" {
|
|
|
|
runningAllocs = append(runningAllocs, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If we don't have any allocations running, use dead allocations
|
|
|
|
if len(runningAllocs) < 1 {
|
|
|
|
runningAllocs = allocs
|
|
|
|
}
|
|
|
|
|
|
|
|
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
2021-01-05 17:33:04 +00:00
|
|
|
alloc := runningAllocs[r.Intn(len(runningAllocs))]
|
|
|
|
|
|
|
|
return alloc, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func getRandomJobAllocID(client *api.Client, jobID string) (string, error) {
|
|
|
|
alloc, err := getRandomJobAlloc(client, jobID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return alloc.ID, nil
|
2016-03-18 19:05:35 +00:00
|
|
|
}
|