Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
packer
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kristopher Ruzic
packer
Commits
cb1e0cba
Commit
cb1e0cba
authored
May 11, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
packer/plugin: Support hooks
parent
d926b987
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
106 additions
and
0 deletions
+106
-0
packer/plugin/hook.go
packer/plugin/hook.go
+66
-0
packer/plugin/hook_test.go
packer/plugin/hook_test.go
+26
-0
packer/plugin/plugin.go
packer/plugin/plugin.go
+12
-0
packer/plugin/plugin_test.go
packer/plugin/plugin_test.go
+2
-0
No files found.
packer/plugin/hook.go
0 → 100644
View file @
cb1e0cba
package
plugin
import
(
"github.com/mitchellh/packer/packer"
packrpc
"github.com/mitchellh/packer/packer/rpc"
"log"
"net/rpc"
"os/exec"
)
type
cmdHook
struct
{
hook
packer
.
Hook
client
*
client
}
func
(
c
*
cmdHook
)
Run
(
name
string
,
data
interface
{},
ui
packer
.
Ui
)
{
defer
func
()
{
r
:=
recover
()
c
.
checkExit
(
r
,
nil
)
}()
c
.
hook
.
Run
(
name
,
data
,
ui
)
}
func
(
c
*
cmdHook
)
checkExit
(
p
interface
{},
cb
func
())
{
if
c
.
client
.
Exited
()
{
cb
()
}
else
if
p
!=
nil
{
log
.
Panic
(
p
)
}
}
// Returns a valid packer.Hook where the hook is executed via RPC
// to a plugin that is within a subprocess.
//
// This method will start the given exec.Cmd, which should point to
// the plugin binary to execute. Some configuration will be done to
// the command, such as overriding Stdout and some environmental variables.
//
// This function guarantees the subprocess will end in a timely manner.
func
Hook
(
cmd
*
exec
.
Cmd
)
(
result
packer
.
Hook
,
err
error
)
{
cmdClient
:=
NewManagedClient
(
cmd
)
address
,
err
:=
cmdClient
.
Start
()
if
err
!=
nil
{
return
}
defer
func
()
{
// Make sure the command is properly killed in the case of an error
if
err
!=
nil
{
cmdClient
.
Kill
()
}
}()
client
,
err
:=
rpc
.
Dial
(
"tcp"
,
address
)
if
err
!=
nil
{
return
}
result
=
&
cmdHook
{
packrpc
.
Hook
(
client
),
cmdClient
,
}
return
}
packer/plugin/hook_test.go
0 → 100644
View file @
cb1e0cba
package
plugin
import
(
"cgl.tideland.biz/asserts"
"github.com/mitchellh/packer/packer"
"os/exec"
"testing"
)
type
helperHook
byte
func
(
helperHook
)
Run
(
string
,
interface
{},
packer
.
Ui
)
{}
func
TestHook_NoExist
(
t
*
testing
.
T
)
{
assert
:=
asserts
.
NewTestingAsserts
(
t
,
true
)
_
,
err
:=
Hook
(
exec
.
Command
(
"i-should-never-ever-ever-exist"
))
assert
.
NotNil
(
err
,
"should have an error"
)
}
func
TestHook_Good
(
t
*
testing
.
T
)
{
assert
:=
asserts
.
NewTestingAsserts
(
t
,
true
)
_
,
err
:=
Hook
(
helperProcess
(
"hook"
))
assert
.
Nil
(
err
,
"should start hook properly"
)
}
packer/plugin/plugin.go
View file @
cb1e0cba
...
...
@@ -98,3 +98,15 @@ func ServeCommand(command packer.Command) {
log
.
Panic
(
err
)
}
}
// Serves a hook from a plugin.
func
ServeHook
(
hook
packer
.
Hook
)
{
log
.
Println
(
"Preparing to serve a hook plugin..."
)
server
:=
rpc
.
NewServer
()
packrpc
.
RegisterHook
(
server
,
hook
)
if
err
:=
serve
(
server
);
err
!=
nil
{
log
.
Panic
(
err
)
}
}
packer/plugin/plugin_test.go
View file @
cb1e0cba
...
...
@@ -52,6 +52,8 @@ func TestHelperProcess(*testing.T) {
ServeBuilder
(
new
(
helperBuilder
))
case
"command"
:
ServeCommand
(
new
(
helperCommand
))
case
"hook"
:
ServeHook
(
new
(
helperHook
))
case
"invalid-rpc-address"
:
fmt
.
Println
(
"lolinvalid"
)
case
"start-timeout"
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment