2013-05-12 17:47:55 -04:00
|
|
|
package packer
|
|
|
|
|
2013-05-12 19:18:17 -04:00
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"time"
|
|
|
|
)
|
2013-05-12 17:47:55 -04:00
|
|
|
|
|
|
|
// A Communicator is the interface used to communicate with the machine
|
|
|
|
// that exists that will eventually be packaged into an image. Communicators
|
|
|
|
// allow you to execute remote commands, upload files, etc.
|
|
|
|
//
|
|
|
|
// Communicators must be safe for concurrency, meaning multiple calls to
|
|
|
|
// Start or any other method may be called at the same time.
|
|
|
|
type Communicator interface {
|
|
|
|
Start(string) (*RemoteCommand, error)
|
|
|
|
Upload(string, io.Reader)
|
|
|
|
Download(string, io.Writer)
|
|
|
|
}
|
|
|
|
|
|
|
|
// This struct contains some information about the remote command being
|
|
|
|
// executed and can be used to wait for it to complete.
|
2013-05-12 19:18:17 -04:00
|
|
|
//
|
|
|
|
// Stdin, Stdout, Stderr are readers and writers to varios IO streams for
|
|
|
|
// the remote command.
|
|
|
|
//
|
|
|
|
// Exited is false until Wait is called. It can be used to check if Wait
|
|
|
|
// has already been called.
|
|
|
|
//
|
|
|
|
// ExitStatus is the exit code of the remote process. It is only available
|
|
|
|
// once Wait is called.
|
2013-05-12 17:47:55 -04:00
|
|
|
type RemoteCommand struct {
|
|
|
|
Stdin io.Writer
|
|
|
|
Stdout io.Reader
|
|
|
|
Stderr io.Reader
|
2013-05-12 19:18:17 -04:00
|
|
|
Exited bool
|
2013-05-12 17:47:55 -04:00
|
|
|
ExitStatus int
|
|
|
|
}
|
2013-05-12 19:18:17 -04:00
|
|
|
|
|
|
|
// Wait waits for the command to exit.
|
|
|
|
func (r *RemoteCommand) Wait() {
|
|
|
|
// Busy wait on being exited. We put a sleep to be kind to the
|
|
|
|
// Go scheduler, and because we don't really need smaller granularity.
|
|
|
|
for !r.Exited {
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
}
|
|
|
|
}
|