mirror of https://github.com/hashicorp/packer
commit
ee50657097
@ -0,0 +1,33 @@
|
||||
package docker
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// ImportArtifact is an Artifact implementation for when a container is
|
||||
// exported from docker into a single flat file.
|
||||
type ImportArtifact struct {
|
||||
BuilderIdValue string
|
||||
Driver Driver
|
||||
IdValue string
|
||||
}
|
||||
|
||||
func (a *ImportArtifact) BuilderId() string {
|
||||
return a.BuilderIdValue
|
||||
}
|
||||
|
||||
func (*ImportArtifact) Files() []string {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *ImportArtifact) Id() string {
|
||||
return a.IdValue
|
||||
}
|
||||
|
||||
func (a *ImportArtifact) String() string {
|
||||
return fmt.Sprintf("Imported Docker image: %s", a.Id())
|
||||
}
|
||||
|
||||
func (a *ImportArtifact) Destroy() error {
|
||||
return a.Driver.DeleteImage(a.Id())
|
||||
}
|
||||
@ -0,0 +1,57 @@
|
||||
package docker
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/mitchellh/packer/packer"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestImportArtifact_impl(t *testing.T) {
|
||||
var _ packer.Artifact = new(ImportArtifact)
|
||||
}
|
||||
|
||||
func TestImportArtifactBuilderId(t *testing.T) {
|
||||
a := &ImportArtifact{BuilderIdValue: "foo"}
|
||||
if a.BuilderId() != "foo" {
|
||||
t.Fatalf("bad: %#v", a.BuilderId())
|
||||
}
|
||||
}
|
||||
|
||||
func TestImportArtifactFiles(t *testing.T) {
|
||||
a := &ImportArtifact{}
|
||||
if a.Files() != nil {
|
||||
t.Fatalf("bad: %#v", a.Files())
|
||||
}
|
||||
}
|
||||
|
||||
func TestImportArtifactId(t *testing.T) {
|
||||
a := &ImportArtifact{IdValue: "foo"}
|
||||
if a.Id() != "foo" {
|
||||
t.Fatalf("bad: %#v", a.Id())
|
||||
}
|
||||
}
|
||||
|
||||
func TestImportArtifactDestroy(t *testing.T) {
|
||||
d := new(MockDriver)
|
||||
a := &ImportArtifact{
|
||||
Driver: d,
|
||||
IdValue: "foo",
|
||||
}
|
||||
|
||||
// No error
|
||||
if err := a.Destroy(); err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
if !d.DeleteImageCalled {
|
||||
t.Fatal("delete image should be called")
|
||||
}
|
||||
if d.DeleteImageId != "foo" {
|
||||
t.Fatalf("bad: %#v", d.DeleteImageId)
|
||||
}
|
||||
|
||||
// With an error
|
||||
d.DeleteImageErr = errors.New("foo")
|
||||
if err := a.Destroy(); err != d.DeleteImageErr {
|
||||
t.Fatalf("err: %#v", err)
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,15 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/mitchellh/packer/packer/plugin"
|
||||
"github.com/mitchellh/packer/post-processor/docker-import"
|
||||
)
|
||||
|
||||
func main() {
|
||||
server, err := plugin.Server()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
server.RegisterPostProcessor(new(dockerimport.PostProcessor))
|
||||
server.Serve()
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
package main
|
||||
@ -0,0 +1,15 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/mitchellh/packer/packer/plugin"
|
||||
"github.com/mitchellh/packer/post-processor/docker-push"
|
||||
)
|
||||
|
||||
func main() {
|
||||
server, err := plugin.Server()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
server.RegisterPostProcessor(new(dockerpush.PostProcessor))
|
||||
server.Serve()
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
package main
|
||||
@ -0,0 +1,98 @@
|
||||
package dockerimport
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/mitchellh/packer/builder/docker"
|
||||
"github.com/mitchellh/packer/common"
|
||||
"github.com/mitchellh/packer/packer"
|
||||
)
|
||||
|
||||
const BuilderId = "packer.post-processor.docker-import"
|
||||
|
||||
type Config struct {
|
||||
common.PackerConfig `mapstructure:",squash"`
|
||||
|
||||
Repository string `mapstructure:"repository"`
|
||||
Tag string `mapstructure:"tag"`
|
||||
|
||||
tpl *packer.ConfigTemplate
|
||||
}
|
||||
|
||||
type PostProcessor struct {
|
||||
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{
|
||||
"repository": &p.config.Repository,
|
||||
"tag": &p.config.Tag,
|
||||
}
|
||||
|
||||
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() != docker.BuilderId {
|
||||
err := fmt.Errorf(
|
||||
"Unknown artifact type: %s\nCan only import from Docker builder artifacts.",
|
||||
artifact.BuilderId())
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
importRepo := p.config.Repository
|
||||
if p.config.Tag != "" {
|
||||
importRepo += ":" + p.config.Tag
|
||||
}
|
||||
|
||||
driver := &docker.DockerDriver{Tpl: p.config.tpl, Ui: ui}
|
||||
|
||||
ui.Message("Importing image: " + artifact.Id())
|
||||
ui.Message("Repository: " + importRepo)
|
||||
id, err := driver.Import(artifact.Files()[0], importRepo)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
ui.Message("Imported ID: " + id)
|
||||
|
||||
// Build the artifact
|
||||
artifact = &docker.ImportArtifact{
|
||||
BuilderIdValue: BuilderId,
|
||||
Driver: driver,
|
||||
IdValue: importRepo,
|
||||
}
|
||||
|
||||
return artifact, false, nil
|
||||
}
|
||||
@ -0,0 +1,31 @@
|
||||
package dockerimport
|
||||
|
||||
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)
|
||||
}
|
||||
@ -0,0 +1,67 @@
|
||||
package dockerpush
|
||||
|
||||
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"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
common.PackerConfig `mapstructure:",squash"`
|
||||
|
||||
tpl *packer.ConfigTemplate
|
||||
}
|
||||
|
||||
type PostProcessor struct {
|
||||
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)
|
||||
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\nCan only import from docker-import artifacts.",
|
||||
artifact.BuilderId())
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
driver := &docker.DockerDriver{Tpl: p.config.tpl, Ui: ui}
|
||||
|
||||
// Get the name. We strip off any tags from the name because the
|
||||
// push doesn't use those.
|
||||
name := artifact.Id()
|
||||
if i := strings.Index(name, ":"); i >= 0 {
|
||||
name = name[:i]
|
||||
}
|
||||
|
||||
ui.Message("Pushing: " + name)
|
||||
if err := driver.Push(name); err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
return nil, false, nil
|
||||
}
|
||||
@ -0,0 +1,31 @@
|
||||
package dockerpush
|
||||
|
||||
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)
|
||||
}
|
||||
@ -0,0 +1,44 @@
|
||||
---
|
||||
layout: "docs"
|
||||
page_title: "docker-import Post-Processor"
|
||||
---
|
||||
|
||||
# Docker Import Post-Processor
|
||||
|
||||
Type: `docker-import`
|
||||
|
||||
The Docker import post-processor takes an artifact from the
|
||||
[docker builder](/docs/builders/docker.html) and imports it with Docker
|
||||
locally. This allows you to apply a repository and tag to the image
|
||||
and lets you use the other Docker post-processors such as
|
||||
[docker-push](/docs/post-processors/docker-push.html) to push the image
|
||||
to a registry.
|
||||
|
||||
## Configuration
|
||||
|
||||
The configuration for this post-processor is extremely simple. At least
|
||||
a repository is required. The tag is optional.
|
||||
|
||||
* `repository` (string) - The repository of the imported image.
|
||||
|
||||
* `tag` (string) - The tag for the imported image. By default this is not
|
||||
set.
|
||||
|
||||
## Example
|
||||
|
||||
An example is shown below, showing only the post-processor configuration:
|
||||
|
||||
<pre class="prettyprint">
|
||||
{
|
||||
"type": "docker-import",
|
||||
"repository": "mitchellh/packer",
|
||||
"tag": "0.7"
|
||||
}
|
||||
</pre>
|
||||
|
||||
This example would take the image created by the Docker builder
|
||||
and import it into the local Docker process with a name of `mitchellh/packer:0.7`.
|
||||
|
||||
Following this, you can use the
|
||||
[docker-push](/docs/post-processors/docker-push.html)
|
||||
post-processor to push it to a registry, if you want.
|
||||
@ -0,0 +1,28 @@
|
||||
---
|
||||
layout: "docs"
|
||||
page_title: "Docker Push Post-Processor"
|
||||
---
|
||||
|
||||
# Docker Push Post-Processor
|
||||
|
||||
Type: `docker-push`
|
||||
|
||||
The Docker push post-processor takes an artifact from the
|
||||
[docker-import](/docs/post-processors/docker-import.html) post-processor
|
||||
and pushes it to a Docker registry.
|
||||
|
||||
<div class="alert alert-info alert-block">
|
||||
<strong>Before you use this,</strong> you must manually <code>docker login</code>
|
||||
to the proper repository. A future version of Packer will automate this
|
||||
for you, but for now you must manually do this.
|
||||
</div>
|
||||
|
||||
## Configuration
|
||||
|
||||
This post-processor has no configuration! Simply add it to your chain
|
||||
of post-processors and the image will be uploaded.
|
||||
|
||||
## Example
|
||||
|
||||
For an example of using docker-push, see the section on using
|
||||
generated artifacts from the [docker builder](/docs/builders/docker.html).
|
||||
Loading…
Reference in new issue