2019-04-28 21:35:17 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/term"
|
|
|
|
"github.com/hashicorp/nomad/api"
|
|
|
|
"github.com/hashicorp/nomad/api/contexts"
|
2019-05-16 19:50:46 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/escapingio"
|
2019-04-28 21:35:17 +00:00
|
|
|
"github.com/posener/complete"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AllocExecCommand struct {
|
|
|
|
Meta
|
|
|
|
|
|
|
|
Stdin io.Reader
|
|
|
|
Stdout io.WriteCloser
|
|
|
|
Stderr io.WriteCloser
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *AllocExecCommand) Help() string {
|
|
|
|
helpText := `
|
|
|
|
Usage: nomad alloc exec [options] <allocation> <command>
|
|
|
|
|
|
|
|
Run command inside the environment of the given allocation and task.
|
|
|
|
|
|
|
|
General Options:
|
|
|
|
|
|
|
|
` + generalOptionsUsage() + `
|
|
|
|
|
|
|
|
Exec Specific Options:
|
|
|
|
|
|
|
|
-task <task-name>
|
|
|
|
Sets the task to exec command in
|
|
|
|
|
|
|
|
-job
|
|
|
|
Use a random allocation from the specified job ID.
|
|
|
|
|
|
|
|
-i
|
2019-05-15 14:35:12 +00:00
|
|
|
Pass stdin to the container, defaults to true. Pass -i=false to disable.
|
2019-04-28 21:35:17 +00:00
|
|
|
|
|
|
|
-t
|
2019-05-15 14:35:12 +00:00
|
|
|
Allocate a pseudo-tty, defaults to true if stdin is detected to be a tty session.
|
|
|
|
Pass -t=false to disable explicitly.
|
2019-05-16 19:50:46 +00:00
|
|
|
|
|
|
|
-e <escape_char>
|
|
|
|
Sets the escape character for sessions with a pty (default: '~'). The escape
|
|
|
|
character is only recognized at the beginning of a line. The escape character
|
|
|
|
followed by a dot ('.') closes the connection. Setting the character to
|
|
|
|
'none' disables any escapes and makes the session fully transparent.
|
2019-04-28 21:35:17 +00:00
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *AllocExecCommand) Synopsis() string {
|
|
|
|
return "Execute commands in task"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *AllocExecCommand) AutocompleteFlags() complete.Flags {
|
|
|
|
return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
|
|
|
|
complete.Flags{
|
|
|
|
"--task": complete.PredictAnything,
|
|
|
|
"-job": complete.PredictAnything,
|
|
|
|
"-i": complete.PredictNothing,
|
|
|
|
"-t": complete.PredictNothing,
|
2019-05-16 19:50:46 +00:00
|
|
|
"-e": complete.PredictSet("none", "~"),
|
2019-04-28 21:35:17 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *AllocExecCommand) AutocompleteArgs() complete.Predictor {
|
|
|
|
return complete.PredictFunc(func(a complete.Args) []string {
|
|
|
|
client, err := l.Meta.Client()
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, _, err := client.Search().PrefixSearch(a.Last, contexts.Allocs, nil)
|
|
|
|
if err != nil {
|
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
return resp.Matches[contexts.Allocs]
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *AllocExecCommand) Name() string { return "alloc exec" }
|
|
|
|
|
|
|
|
func (l *AllocExecCommand) Run(args []string) int {
|
|
|
|
var job, stdinOpt, ttyOpt bool
|
|
|
|
var task string
|
2019-05-16 19:50:46 +00:00
|
|
|
var escapeChar string
|
2019-04-28 21:35:17 +00:00
|
|
|
|
|
|
|
flags := l.Meta.FlagSet(l.Name(), FlagSetClient)
|
|
|
|
flags.Usage = func() { l.Ui.Output(l.Help()) }
|
|
|
|
flags.BoolVar(&job, "job", false, "")
|
|
|
|
flags.StringVar(&task, "task", "", "")
|
2019-05-16 19:50:46 +00:00
|
|
|
flags.StringVar(&escapeChar, "e", "~", "")
|
2019-04-28 21:35:17 +00:00
|
|
|
|
|
|
|
flags.BoolVar(&stdinOpt, "i", true, "")
|
|
|
|
|
2019-11-11 23:22:25 +00:00
|
|
|
inferredTty := isTty()
|
|
|
|
flags.BoolVar(&ttyOpt, "t", inferredTty, "")
|
2019-04-28 21:35:17 +00:00
|
|
|
|
|
|
|
if err := flags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
args = flags.Args()
|
|
|
|
|
|
|
|
if ttyOpt && !stdinOpt {
|
|
|
|
l.Ui.Error("-i must be enabled if running with tty")
|
2019-05-16 19:50:46 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if escapeChar == "none" {
|
|
|
|
escapeChar = ""
|
|
|
|
} else if len(escapeChar) > 1 {
|
|
|
|
l.Ui.Error("-e requires 'none' or a single character")
|
|
|
|
return 1
|
2019-04-28 21:35:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if numArgs := len(args); numArgs < 1 {
|
|
|
|
if job {
|
|
|
|
l.Ui.Error("A job ID is required")
|
|
|
|
} else {
|
|
|
|
l.Ui.Error("An allocation ID is required")
|
|
|
|
}
|
|
|
|
|
|
|
|
l.Ui.Error(commandErrorText(l))
|
|
|
|
return 1
|
|
|
|
} else if numArgs < 2 {
|
|
|
|
l.Ui.Error("A command is required")
|
|
|
|
l.Ui.Error(commandErrorText(l))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
command := args[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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
length := shortId
|
|
|
|
|
|
|
|
// Query the allocation info
|
|
|
|
if len(allocID) == 1 {
|
|
|
|
l.Ui.Error(fmt.Sprintf("Alloc ID must contain at least two characters."))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
allocID = sanitizeUUIDPrefix(allocID)
|
|
|
|
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
|
|
|
|
out := formatAllocListStubs(allocs, false, length)
|
|
|
|
l.Ui.Error(fmt.Sprintf("Prefix matched multiple allocations\n\n%s", out))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
if task == "" {
|
|
|
|
task, err = lookupAllocTask(alloc)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
l.Ui.Error(err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := validateTaskExistsInAllocation(task, alloc); err != nil {
|
|
|
|
l.Ui.Error(err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if l.Stdin == nil {
|
|
|
|
l.Stdin = os.Stdin
|
|
|
|
}
|
|
|
|
if l.Stdout == nil {
|
|
|
|
l.Stdout = os.Stdout
|
|
|
|
}
|
|
|
|
if l.Stderr == nil {
|
|
|
|
l.Stderr = os.Stderr
|
|
|
|
}
|
|
|
|
|
|
|
|
var stdin io.Reader = l.Stdin
|
|
|
|
if !stdinOpt {
|
|
|
|
stdin = bytes.NewReader(nil)
|
|
|
|
}
|
|
|
|
|
2019-05-16 19:50:46 +00:00
|
|
|
code, err := l.execImpl(client, alloc, task, ttyOpt, command, escapeChar, stdin, l.Stdout, l.Stderr)
|
2019-04-28 21:35:17 +00:00
|
|
|
if err != nil {
|
|
|
|
l.Ui.Error(fmt.Sprintf("failed to exec into task: %v", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return code
|
|
|
|
}
|
|
|
|
|
2019-05-15 14:37:37 +00:00
|
|
|
// execImpl invokes the Alloc Exec api call, it also prepares and restores terminal states as necessary.
|
|
|
|
func (l *AllocExecCommand) execImpl(client *api.Client, alloc *api.Allocation, task string, tty bool,
|
2019-05-16 19:50:46 +00:00
|
|
|
command []string, escapeChar string, stdin io.Reader, stdout, stderr io.WriteCloser) (int, error) {
|
2019-05-15 14:37:37 +00:00
|
|
|
|
|
|
|
sizeCh := make(chan api.TerminalSize, 1)
|
|
|
|
|
2019-05-16 19:50:46 +00:00
|
|
|
ctx, cancelFn := context.WithCancel(context.Background())
|
|
|
|
defer cancelFn()
|
|
|
|
|
2019-05-15 14:37:37 +00:00
|
|
|
// When tty, ensures we capture all user input and monitor terminal resizes.
|
|
|
|
if tty {
|
|
|
|
if stdin == nil {
|
|
|
|
return -1, fmt.Errorf("stdin is null")
|
|
|
|
}
|
|
|
|
|
|
|
|
inCleanup, err := setRawTerminal(stdin)
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
defer inCleanup()
|
|
|
|
|
|
|
|
outCleanup, err := setRawTerminalOutput(stdout)
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
defer outCleanup()
|
|
|
|
|
|
|
|
sizeCleanup, err := watchTerminalSize(stdout, sizeCh)
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
defer sizeCleanup()
|
|
|
|
|
2019-05-16 19:50:46 +00:00
|
|
|
if escapeChar != "" {
|
|
|
|
stdin = escapingio.NewReader(stdin, escapeChar[0], func(c byte) bool {
|
|
|
|
switch c {
|
|
|
|
case '.':
|
2019-05-17 15:52:36 +00:00
|
|
|
// need to restore tty state so error reporting here
|
|
|
|
// gets emitted at beginning of line
|
|
|
|
outCleanup()
|
|
|
|
inCleanup()
|
|
|
|
|
2019-05-16 19:50:46 +00:00
|
|
|
stderr.Write([]byte("\nConnection closed\n"))
|
|
|
|
cancelFn()
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-05-15 14:37:37 +00:00
|
|
|
|
|
|
|
signalCh := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM)
|
|
|
|
go func() {
|
|
|
|
for range signalCh {
|
|
|
|
cancelFn()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return client.Allocations().Exec(ctx,
|
|
|
|
alloc, task, tty, command, stdin, stdout, stderr, sizeCh, nil)
|
|
|
|
}
|
|
|
|
|
2019-11-11 23:22:25 +00:00
|
|
|
// isTty returns true if both stdin and stdout are a TTY
|
|
|
|
func isTty() bool {
|
|
|
|
_, isStdinTerminal := term.GetFdInfo(os.Stdin)
|
|
|
|
_, isStdoutTerminal := term.GetFdInfo(os.Stdout)
|
|
|
|
return isStdinTerminal && isStdoutTerminal
|
2019-04-28 21:35:17 +00:00
|
|
|
}
|
|
|
|
|
2019-05-15 14:35:12 +00:00
|
|
|
// setRawTerminal sets the stream terminal in raw mode, so process captures
|
|
|
|
// Ctrl+C and other commands to forward to remote process.
|
|
|
|
// It returns a cleanup function that restores terminal to original mode.
|
2019-04-28 21:35:17 +00:00
|
|
|
func setRawTerminal(stream interface{}) (cleanup func(), err error) {
|
|
|
|
fd, isTerminal := term.GetFdInfo(stream)
|
|
|
|
if !isTerminal {
|
|
|
|
return nil, errors.New("not a terminal")
|
|
|
|
}
|
|
|
|
|
|
|
|
state, err := term.SetRawTerminal(fd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return func() { term.RestoreTerminal(fd, state) }, nil
|
|
|
|
}
|
|
|
|
|
2019-05-15 14:37:37 +00:00
|
|
|
// setRawTerminalOutput sets the output stream in Windows to raw mode,
|
|
|
|
// so it disables LF -> CRLF translation.
|
|
|
|
// It's basically a no-op on unix.
|
|
|
|
func setRawTerminalOutput(stream interface{}) (cleanup func(), err error) {
|
|
|
|
fd, isTerminal := term.GetFdInfo(stream)
|
|
|
|
if !isTerminal {
|
|
|
|
return nil, errors.New("not a terminal")
|
|
|
|
}
|
|
|
|
|
|
|
|
state, err := term.SetRawTerminalOutput(fd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return func() { term.RestoreTerminal(fd, state) }, nil
|
|
|
|
}
|
|
|
|
|
2019-05-15 17:01:05 +00:00
|
|
|
// watchTerminalSize watches terminal size changes to propagate to remote tty.
|
2019-04-28 21:35:17 +00:00
|
|
|
func watchTerminalSize(out io.Writer, resize chan<- api.TerminalSize) (func(), error) {
|
|
|
|
fd, isTerminal := term.GetFdInfo(out)
|
|
|
|
if !isTerminal {
|
|
|
|
return nil, errors.New("not a terminal")
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
signalCh := make(chan os.Signal, 1)
|
|
|
|
setupWindowNotification(signalCh)
|
|
|
|
|
|
|
|
sendTerminalSize := func() {
|
|
|
|
s, err := term.GetWinsize(fd)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resize <- api.TerminalSize{
|
|
|
|
Height: int(s.Height),
|
|
|
|
Width: int(s.Width),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case <-signalCh:
|
|
|
|
sendTerminalSize()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
// send initial size
|
|
|
|
sendTerminalSize()
|
|
|
|
}()
|
|
|
|
|
|
|
|
return cancel, nil
|
|
|
|
}
|