packer-cn/packer/plugin/command.go

44 lines
740 B
Go
Raw Normal View History

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"
)
func Command(cmd *exec.Cmd) packer.Command {
env := []string{
"PACKER_PLUGIN_MIN_PORT=10000",
"PACKER_PLUGIN_MAX_PORT=25000",
}
2013-05-05 00:26:30 -04:00
out := new(bytes.Buffer)
cmd.Env = append(cmd.Env, env...)
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)
}
client, err := rpc.Dial("tcp", address)
if err != nil {
panic(err)
}
2013-05-05 00:26:30 -04:00
return packrpc.Command(client)
2013-05-05 00:26:30 -04:00
}