2018-02-23 16:26:31 -05:00
|
|
|
package shell_local
|
2015-06-19 18:06:06 -04:00
|
|
|
|
|
|
|
import (
|
2019-04-03 11:14:55 -04:00
|
|
|
"context"
|
2015-06-19 18:06:06 -04:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2018-03-01 13:56:30 -05:00
|
|
|
"log"
|
2015-06-19 18:06:06 -04:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"syscall"
|
|
|
|
|
2020-11-19 18:10:00 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2015-06-19 18:06:06 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type Communicator struct {
|
|
|
|
ExecuteCommand []string
|
|
|
|
}
|
|
|
|
|
2020-11-19 18:10:00 -05:00
|
|
|
func (c *Communicator) Start(ctx context.Context, cmd *packersdk.RemoteCmd) error {
|
2018-02-23 16:26:31 -05:00
|
|
|
if len(c.ExecuteCommand) == 0 {
|
2018-02-28 17:35:42 -05:00
|
|
|
return fmt.Errorf("Error launching command via shell-local communicator: No ExecuteCommand provided")
|
2015-06-19 18:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build the local command to execute
|
2018-05-21 14:25:51 -04:00
|
|
|
log.Printf("[INFO] (shell-local communicator): Executing local shell command %s", c.ExecuteCommand)
|
2019-04-03 11:14:55 -04:00
|
|
|
localCmd := exec.CommandContext(ctx, c.ExecuteCommand[0], c.ExecuteCommand[1:]...)
|
2015-06-19 18:06:06 -04:00
|
|
|
localCmd.Stdin = cmd.Stdin
|
|
|
|
localCmd.Stdout = cmd.Stdout
|
|
|
|
localCmd.Stderr = cmd.Stderr
|
|
|
|
|
|
|
|
// Start it. If it doesn't work, then error right away.
|
|
|
|
if err := localCmd.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// We've started successfully. Start a goroutine to wait for
|
|
|
|
// it to complete and track exit status.
|
|
|
|
go func() {
|
|
|
|
var exitStatus int
|
|
|
|
err := localCmd.Wait()
|
|
|
|
if err != nil {
|
|
|
|
if exitErr, ok := err.(*exec.ExitError); ok {
|
|
|
|
exitStatus = 1
|
|
|
|
|
|
|
|
// There is no process-independent way to get the REAL
|
|
|
|
// exit status so we just try to go deeper.
|
|
|
|
if status, ok := exitErr.Sys().(syscall.WaitStatus); ok {
|
|
|
|
exitStatus = status.ExitStatus()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.SetExited(exitStatus)
|
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Communicator) Upload(string, io.Reader, *os.FileInfo) error {
|
|
|
|
return fmt.Errorf("upload not supported")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Communicator) UploadDir(string, string, []string) error {
|
|
|
|
return fmt.Errorf("uploadDir not supported")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Communicator) Download(string, io.Writer) error {
|
|
|
|
return fmt.Errorf("download not supported")
|
|
|
|
}
|
|
|
|
|
2015-11-02 06:22:52 -05:00
|
|
|
func (c *Communicator) DownloadDir(string, string, []string) error {
|
|
|
|
return fmt.Errorf("downloadDir not supported")
|
|
|
|
}
|