packer/plugin: Support PostProcessor

This commit is contained in:
Mitchell Hashimoto 2013-06-18 13:49:07 -07:00
parent d823d2550a
commit 9b406a9010
5 changed files with 91 additions and 3 deletions

View File

@ -143,13 +143,12 @@ func (c *Client) Hook() (packer.Hook, error) {
// Returns a post-processor implementation that is communicating over
// this client. If the client hasn't been started, this will start it.
func (c *Client) PostProcessor() (packer.PostProcessor, error) {
_, err := c.rpcClient()
client, err := c.rpcClient()
if err != nil {
return nil, err
}
// TODO
return nil, nil
return &cmdPostProcessor{packrpc.PostProcessor(client), c}, nil
}
// Returns a provisioner implementation that is communicating over this

View File

@ -137,6 +137,19 @@ func ServeHook(hook packer.Hook) {
}
}
// Serves a post-processor from a plugin.
func ServePostProcessor(p packer.PostProcessor) {
log.Println("Preparing to serve a post-processor plugin...")
server := rpc.NewServer()
packrpc.RegisterPostProcessor(server, p)
swallowInterrupts()
if err := serve(server); err != nil {
log.Panic(err)
}
}
// Serves a provisioner from a plugin.
func ServeProvisioner(p packer.Provisioner) {
log.Println("Preparing to serve a provisioner plugin...")

View File

@ -59,6 +59,8 @@ func TestHelperProcess(*testing.T) {
case "mock":
fmt.Println(":1234")
<-make(chan int)
case "post-processor":
ServePostProcessor(new(helperPostProcessor))
case "provisioner":
ServeProvisioner(new(helperProvisioner))
case "start-timeout":

View File

@ -0,0 +1,37 @@
package plugin
import (
"github.com/mitchellh/packer/packer"
"log"
)
type cmdPostProcessor struct {
p packer.PostProcessor
client *Client
}
func (c *cmdPostProcessor) Configure(config interface{}) error {
defer func() {
r := recover()
c.checkExit(r, nil)
}()
return c.p.Configure(config)
}
func (c *cmdPostProcessor) PostProcess(a packer.Artifact) (packer.Artifact, error) {
defer func() {
r := recover()
c.checkExit(r, nil)
}()
return c.p.PostProcess(a)
}
func (c *cmdPostProcessor) checkExit(p interface{}, cb func()) {
if c.client.Exited() {
cb()
} else if p != nil {
log.Panic(p)
}
}

View File

@ -0,0 +1,37 @@
package plugin
import (
"github.com/mitchellh/packer/packer"
"os/exec"
"testing"
)
type helperPostProcessor byte
func (helperPostProcessor) Configure(interface{}) error {
return nil
}
func (helperPostProcessor) PostProcess(packer.Artifact) (packer.Artifact, error) {
return nil, nil
}
func TestPostProcessor_NoExist(t *testing.T) {
c := NewClient(&ClientConfig{Cmd: exec.Command("i-should-not-exist")})
defer c.Kill()
_, err := c.PostProcessor()
if err == nil {
t.Fatal("should have error")
}
}
func TestPostProcessor_Good(t *testing.T) {
c := NewClient(&ClientConfig{Cmd: helperProcess("post-processor")})
defer c.Kill()
_, err := c.PostProcessor()
if err != nil {
t.Fatalf("should not have error: %s", err)
}
}