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
ed446782
Commit
ed446782
authored
Jul 20, 2014
by
Andy Thompson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add a docker save post processor
parent
bf166831
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
194 additions
and
0 deletions
+194
-0
builder/docker/driver.go
builder/docker/driver.go
+3
-0
builder/docker/driver_docker.go
builder/docker/driver_docker.go
+20
-0
builder/docker/driver_mock.go
builder/docker/driver_mock.go
+19
-0
config.go
config.go
+1
-0
plugin/post-processor-docker-save/main.go
plugin/post-processor-docker-save/main.go
+15
-0
plugin/post-processor-docker-save/main_test.go
plugin/post-processor-docker-save/main_test.go
+1
-0
post-processor/docker-save/post-processor.go
post-processor/docker-save/post-processor.go
+104
-0
post-processor/docker-save/post-processor_test.go
post-processor/docker-save/post-processor_test.go
+31
-0
No files found.
builder/docker/driver.go
View file @
ed446782
...
...
@@ -26,6 +26,9 @@ type Driver interface {
// Push pushes an image to a Docker index/registry.
Push
(
name
string
)
error
// Save an image with the given ID to the given writer.
SaveImage
(
id
string
,
dst
io
.
Writer
)
error
// StartContainer starts a container and returns the ID for that container,
// along with a potential error.
StartContainer
(
*
ContainerConfig
)
(
string
,
error
)
...
...
builder/docker/driver_docker.go
View file @
ed446782
...
...
@@ -115,6 +115,26 @@ func (d *DockerDriver) Push(name string) error {
return
runAndStream
(
cmd
,
d
.
Ui
)
}
func
(
d
*
DockerDriver
)
SaveImage
(
id
string
,
dst
io
.
Writer
)
error
{
var
stderr
bytes
.
Buffer
cmd
:=
exec
.
Command
(
"docker"
,
"save"
,
id
)
cmd
.
Stdout
=
dst
cmd
.
Stderr
=
&
stderr
log
.
Printf
(
"Exporting image: %s"
,
id
)
if
err
:=
cmd
.
Start
();
err
!=
nil
{
return
err
}
if
err
:=
cmd
.
Wait
();
err
!=
nil
{
err
=
fmt
.
Errorf
(
"Error exporting: %s
\n
Stderr: %s"
,
err
,
stderr
.
String
())
return
err
}
return
nil
}
func
(
d
*
DockerDriver
)
StartContainer
(
config
*
ContainerConfig
)
(
string
,
error
)
{
// Build up the template data
var
tplData
startContainerTemplate
...
...
builder/docker/driver_mock.go
View file @
ed446782
...
...
@@ -25,6 +25,11 @@ type MockDriver struct {
PushName
string
PushErr
error
SaveImageCalled
bool
SaveImageId
string
SaveImageReader
io
.
Reader
SaveImageError
error
TagImageCalled
bool
TagImageImageId
string
TagImageRepo
string
...
...
@@ -94,6 +99,20 @@ func (d *MockDriver) Push(name string) error {
return
d
.
PushErr
}
func
(
d
*
MockDriver
)
SaveImage
(
id
string
,
dst
io
.
Writer
)
error
{
d
.
SaveImageCalled
=
true
d
.
SaveImageId
=
id
if
d
.
SaveImageReader
!=
nil
{
_
,
err
:=
io
.
Copy
(
dst
,
d
.
SaveImageReader
)
if
err
!=
nil
{
return
err
}
}
return
d
.
SaveImageError
}
func
(
d
*
MockDriver
)
StartContainer
(
config
*
ContainerConfig
)
(
string
,
error
)
{
d
.
StartCalled
=
true
d
.
StartConfig
=
config
...
...
config.go
View file @
ed446782
...
...
@@ -48,6 +48,7 @@ const defaultConfig = `
"vsphere": "packer-post-processor-vsphere",
"docker-push": "packer-post-processor-docker-push",
"docker-import": "packer-post-processor-docker-import",
"docker-save": "packer-post-processor-docker-save",
"docker-tag": "packer-post-processor-docker-tag",
"vagrant-cloud": "packer-post-processor-vagrant-cloud"
},
...
...
plugin/post-processor-docker-save/main.go
0 → 100644
View file @
ed446782
package
main
import
(
"github.com/mitchellh/packer/packer/plugin"
"github.com/mitchellh/packer/post-processor/docker-save"
)
func
main
()
{
server
,
err
:=
plugin
.
Server
()
if
err
!=
nil
{
panic
(
err
)
}
server
.
RegisterPostProcessor
(
new
(
dockersave
.
PostProcessor
))
server
.
Serve
()
}
plugin/post-processor-docker-save/main_test.go
0 → 100644
View file @
ed446782
package
main
post-processor/docker-save/post-processor.go
0 → 100644
View file @
ed446782
package
dockersave
import
(
"fmt"
"github.com/mitchellh/packer/builder/docker"
"github.com/mitchellh/packer/common"
"github.com/mitchellh/packer/packer"
"github.com/mitchellh/packer/post-processor/docker-import"
"os"
)
const
BuilderId
=
"packer.post-processor.docker-save"
type
Config
struct
{
common
.
PackerConfig
`mapstructure:",squash"`
Path
string
`mapstructure:"path"`
tpl
*
packer
.
ConfigTemplate
}
type
PostProcessor
struct
{
Driver
docker
.
Driver
config
Config
}
func
(
p
*
PostProcessor
)
Configure
(
raws
...
interface
{})
error
{
_
,
err
:=
common
.
DecodeConfig
(
&
p
.
config
,
raws
...
)
if
err
!=
nil
{
return
err
}
p
.
config
.
tpl
,
err
=
packer
.
NewConfigTemplate
()
if
err
!=
nil
{
return
err
}
p
.
config
.
tpl
.
UserVars
=
p
.
config
.
PackerUserVars
// Accumulate any errors
errs
:=
new
(
packer
.
MultiError
)
templates
:=
map
[
string
]
*
string
{
"path"
:
&
p
.
config
.
Path
,
}
for
key
,
ptr
:=
range
templates
{
if
*
ptr
==
""
{
errs
=
packer
.
MultiErrorAppend
(
errs
,
fmt
.
Errorf
(
"%s must be set"
,
key
))
}
*
ptr
,
err
=
p
.
config
.
tpl
.
Process
(
*
ptr
,
nil
)
if
err
!=
nil
{
errs
=
packer
.
MultiErrorAppend
(
errs
,
fmt
.
Errorf
(
"Error processing %s: %s"
,
key
,
err
))
}
}
if
len
(
errs
.
Errors
)
>
0
{
return
errs
}
return
nil
}
func
(
p
*
PostProcessor
)
PostProcess
(
ui
packer
.
Ui
,
artifact
packer
.
Artifact
)
(
packer
.
Artifact
,
bool
,
error
)
{
if
artifact
.
BuilderId
()
!=
dockerimport
.
BuilderId
{
err
:=
fmt
.
Errorf
(
"Unknown artifact type: %s
\n
Can only save Docker builder artifacts."
,
artifact
.
BuilderId
())
return
nil
,
false
,
err
}
path
:=
p
.
config
.
Path
// Open the file that we're going to write to
f
,
err
:=
os
.
Create
(
path
)
if
err
!=
nil
{
err
:=
fmt
.
Errorf
(
"Error creating output file: %s"
,
err
)
return
nil
,
false
,
err
}
driver
:=
p
.
Driver
if
driver
==
nil
{
// If no driver is set, then we use the real driver
driver
=
&
docker
.
DockerDriver
{
Tpl
:
p
.
config
.
tpl
,
Ui
:
ui
}
}
ui
.
Message
(
"Saving image: "
+
artifact
.
Id
())
if
err
:=
driver
.
SaveImage
(
artifact
.
Id
(),
f
);
err
!=
nil
{
f
.
Close
()
os
.
Remove
(
f
.
Name
())
return
nil
,
false
,
err
}
f
.
Close
()
ui
.
Message
(
"Saved to: "
+
path
)
return
artifact
,
true
,
nil
}
post-processor/docker-save/post-processor_test.go
0 → 100644
View file @
ed446782
package
dockersave
import
(
"bytes"
"github.com/mitchellh/packer/packer"
"testing"
)
func
testConfig
()
map
[
string
]
interface
{}
{
return
map
[
string
]
interface
{}{}
}
func
testPP
(
t
*
testing
.
T
)
*
PostProcessor
{
var
p
PostProcessor
if
err
:=
p
.
Configure
(
testConfig
());
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
return
&
p
}
func
testUi
()
*
packer
.
BasicUi
{
return
&
packer
.
BasicUi
{
Reader
:
new
(
bytes
.
Buffer
),
Writer
:
new
(
bytes
.
Buffer
),
}
}
func
TestPostProcessor_ImplementsPostProcessor
(
t
*
testing
.
T
)
{
var
_
packer
.
PostProcessor
=
new
(
PostProcessor
)
}
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