Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why not execute the shell command described by the RemoteCmd but the Communicator? #99

Open
qia01ng opened this issue Mar 4, 2022 · 0 comments
Labels
question Further information is requested

Comments

@qia01ng
Copy link

qia01ng commented Mar 4, 2022

There is one ExecuteCommand field in Communicator struct and trigger the ExecuteCommand execution in the Start() method; But what confused me is that why the command to be executed is not the command described by the RemoteCmd?

// https://github.com/hashicorp/packer-plugin-sdk/blob/main/shell-local/communicator.go
type Communicator struct {
	ExecuteCommand []string
}
func (c *Communicator) Start(ctx context.Context, cmd *packersdk.RemoteCmd) error {
	if len(c.ExecuteCommand) == 0 {
		return fmt.Errorf("Error launching command via shell-local communicator: No ExecuteCommand provided")
	}

	// Build the local command to execute
	log.Printf("[INFO] (shell-local communicator): Executing local shell command %s", c.ExecuteCommand)
	localCmd := exec.CommandContext(ctx, c.ExecuteCommand[0], c.ExecuteCommand[1:]...)
	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
}
@qia01ng qia01ng added the question Further information is requested label Mar 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant