Commit 9b406a90 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

packer/plugin: Support PostProcessor

parent d823d255
......@@ -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
......
......@@ -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...")
......
......@@ -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":
......
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)
}
}
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)
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment