2013-05-05 00:26:30 -04:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"net/rpc"
|
|
|
|
"os/exec"
|
|
|
|
packrpc "github.com/mitchellh/packer/packer/rpc"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2013-05-05 19:25:32 -04:00
|
|
|
func Command(cmd *exec.Cmd) packer.Command {
|
2013-05-07 14:50:42 -04:00
|
|
|
env := []string{
|
|
|
|
"PACKER_PLUGIN_MIN_PORT=10000",
|
|
|
|
"PACKER_PLUGIN_MAX_PORT=25000",
|
|
|
|
}
|
|
|
|
|
2013-05-05 00:26:30 -04:00
|
|
|
out := new(bytes.Buffer)
|
2013-05-07 14:50:42 -04:00
|
|
|
cmd.Env = append(cmd.Env, env...)
|
2013-05-05 19:25:32 -04:00
|
|
|
cmd.Stdout = out
|
|
|
|
cmd.Start()
|
2013-05-05 00:26:30 -04:00
|
|
|
|
|
|
|
// TODO: timeout
|
|
|
|
// TODO: check that command is even running
|
|
|
|
address := ""
|
|
|
|
for {
|
|
|
|
line, err := out.ReadBytes('\n')
|
|
|
|
if err == nil {
|
|
|
|
address = strings.TrimSpace(string(line))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
2013-05-05 19:25:32 -04:00
|
|
|
client, err := rpc.Dial("tcp", address)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2013-05-05 00:26:30 -04:00
|
|
|
|
2013-05-05 19:25:32 -04:00
|
|
|
return packrpc.Command(client)
|
2013-05-05 00:26:30 -04:00
|
|
|
}
|