From a2bf964f30894457a4eaffe0287f70ae98910bcb Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 11 May 2013 11:11:40 -0700 Subject: [PATCH] packer/rpc: Properly support hooks --- builder/amazonebs/builder.go | 3 +++ builder/amazonebs/hook.go | 4 ++++ config.go | 5 +++++ packer.go | 1 + packer/rpc/environment.go | 9 ++++----- 5 files changed, 17 insertions(+), 5 deletions(-) create mode 100644 builder/amazonebs/hook.go diff --git a/builder/amazonebs/builder.go b/builder/amazonebs/builder.go index 7305dfe84..ea62703e6 100644 --- a/builder/amazonebs/builder.go +++ b/builder/amazonebs/builder.go @@ -60,6 +60,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) { MaxCount: 0, } + hook.Run(HookPreLaunch, nil, ui) + return + ui.Say("Launching a source AWS instance...\n") runResp, err := ec2conn.RunInstances(runOpts) if err != nil { diff --git a/builder/amazonebs/hook.go b/builder/amazonebs/hook.go new file mode 100644 index 000000000..5e3d2a2b8 --- /dev/null +++ b/builder/amazonebs/hook.go @@ -0,0 +1,4 @@ +package amazonebs + +// This hook is fired prior to launching the EC2 instance. +const HookPreLaunch = "amazonebs_pre_launch" diff --git a/config.go b/config.go index 494686202..903ee6638 100644 --- a/config.go +++ b/config.go @@ -90,3 +90,8 @@ func (c *config) LoadCommand(name string) (packer.Command, error) { return plugin.Command(exec.Command(commandBin)) } + +func (c *config) LoadHook(name string) (packer.Hook, error) { + log.Printf("Loading hook: %s\n", name) + return plugin.Hook(exec.Command(name)) +} diff --git a/packer.go b/packer.go index 7237c4e14..40c4aa0eb 100644 --- a/packer.go +++ b/packer.go @@ -87,6 +87,7 @@ func main() { envConfig.Commands = config.CommandNames() envConfig.Components.Builder = config.LoadBuilder envConfig.Components.Command = config.LoadCommand + envConfig.Components.Hook = config.LoadHook env, err := packer.NewEnvironment(envConfig) if err != nil { diff --git a/packer/rpc/environment.go b/packer/rpc/environment.go index 302c716d2..03a020570 100644 --- a/packer/rpc/environment.go +++ b/packer/rpc/environment.go @@ -50,13 +50,12 @@ func (e *Environment) Hook(name string) (h packer.Hook, err error) { return } - _, err = rpc.Dial("tcp", reply) + client, err := rpc.Dial("tcp", reply) if err != nil { return } - // TODO: Hook - h = nil + h = Hook(client) return } @@ -89,14 +88,14 @@ func (e *EnvironmentServer) Cli(args *EnvironmentCliArgs, reply *int) (err error } func (e *EnvironmentServer) Hook(name *string, reply *string) error { - _, err := e.env.Hook(*name) + hook, err := e.env.Hook(*name) if err != nil { return err } // Wrap it - // TODO: Register hook server := rpc.NewServer() + RegisterHook(server, hook) *reply = serveSingleConn(server) return nil