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
86350856
Commit
86350856
authored
Jan 19, 2014
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
builder/docker: make Import part of this Driver for reuse
parent
34dbf721
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
53 additions
and
33 deletions
+53
-33
builder/docker/driver.go
builder/docker/driver.go
+3
-0
builder/docker/driver_docker.go
builder/docker/driver_docker.go
+34
-0
builder/docker/driver_mock.go
builder/docker/driver_mock.go
+13
-0
post-processor/docker-import/post-processor.go
post-processor/docker-import/post-processor.go
+3
-33
No files found.
builder/docker/driver.go
View file @
86350856
...
...
@@ -14,6 +14,9 @@ type Driver interface {
// Export exports the container with the given ID to the given writer.
Export
(
id
string
,
dst
io
.
Writer
)
error
// Import imports a container from a tar file
Import
(
path
,
repo
string
)
(
string
,
error
)
// Pull should pull down the given image.
Pull
(
image
string
)
error
...
...
builder/docker/driver_docker.go
View file @
86350856
...
...
@@ -6,6 +6,7 @@ import (
"github.com/mitchellh/packer/packer"
"io"
"log"
"os"
"os/exec"
"strings"
)
...
...
@@ -54,6 +55,39 @@ func (d *DockerDriver) Export(id string, dst io.Writer) error {
return
nil
}
func
(
d
*
DockerDriver
)
Import
(
path
string
,
repo
string
)
(
string
,
error
)
{
var
stdout
bytes
.
Buffer
cmd
:=
exec
.
Command
(
"docker"
,
"import"
,
"-"
,
repo
)
cmd
.
Stdout
=
&
stdout
stdin
,
err
:=
cmd
.
StdinPipe
()
if
err
!=
nil
{
return
""
,
err
}
// There should be only one artifact of the Docker builder
file
,
err
:=
os
.
Open
(
path
)
if
err
!=
nil
{
return
""
,
err
}
defer
file
.
Close
()
if
err
:=
cmd
.
Start
();
err
!=
nil
{
return
""
,
err
}
go
func
()
{
defer
stdin
.
Close
()
io
.
Copy
(
stdin
,
file
)
}()
if
err
:=
cmd
.
Wait
();
err
!=
nil
{
err
=
fmt
.
Errorf
(
"Error importing container: %s"
,
err
)
return
""
,
err
}
return
strings
.
TrimSpace
(
stdout
.
String
()),
nil
}
func
(
d
*
DockerDriver
)
Pull
(
image
string
)
error
{
cmd
:=
exec
.
Command
(
"docker"
,
"pull"
,
image
)
return
runAndStream
(
cmd
,
d
.
Ui
)
...
...
builder/docker/driver_mock.go
View file @
86350856
...
...
@@ -10,6 +10,12 @@ type MockDriver struct {
DeleteImageId
string
DeleteImageErr
error
ImportCalled
bool
ImportPath
string
ImportRepo
string
ImportId
string
ImportErr
error
ExportReader
io
.
Reader
ExportError
error
PullError
error
...
...
@@ -49,6 +55,13 @@ func (d *MockDriver) Export(id string, dst io.Writer) error {
return
d
.
ExportError
}
func
(
d
*
MockDriver
)
Import
(
path
,
repo
string
)
(
string
,
error
)
{
d
.
ImportCalled
=
true
d
.
ImportPath
=
path
d
.
ImportRepo
=
repo
return
d
.
ImportId
,
d
.
ImportErr
}
func
(
d
*
MockDriver
)
Pull
(
image
string
)
error
{
d
.
PullCalled
=
true
d
.
PullImage
=
image
...
...
post-processor/docker-import/post-processor.go
View file @
86350856
package
dockerimport
import
(
"bytes"
"fmt"
"github.com/mitchellh/packer/builder/docker"
"github.com/mitchellh/packer/common"
"github.com/mitchellh/packer/packer"
"io"
"os"
"os/exec"
"strings"
)
const
BuilderId
=
"packer.post-processor.docker-import"
...
...
@@ -74,43 +69,18 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac
importRepo
+=
":"
+
p
.
config
.
Tag
}
var
stdout
bytes
.
Buffer
cmd
:=
exec
.
Command
(
"docker"
,
"import"
,
"-"
,
importRepo
)
cmd
.
Stdout
=
&
stdout
stdin
,
err
:=
cmd
.
StdinPipe
()
if
err
!=
nil
{
return
nil
,
false
,
err
}
// There should be only one artifact of the Docker builder
file
,
err
:=
os
.
Open
(
artifact
.
Files
()[
0
])
if
err
!=
nil
{
return
nil
,
false
,
err
}
defer
file
.
Close
()
driver
:=
&
docker
.
DockerDriver
{
Tpl
:
p
.
config
.
tpl
,
Ui
:
ui
}
ui
.
Message
(
"Importing image: "
+
artifact
.
Id
())
ui
.
Message
(
"Repository: "
+
importRepo
)
if
err
:=
cmd
.
Start
();
err
!=
nil
{
return
nil
,
false
,
err
}
go
func
()
{
defer
stdin
.
Close
()
io
.
Copy
(
stdin
,
file
)
}()
if
err
:=
cmd
.
Wait
();
err
!=
nil
{
err
=
fmt
.
Errorf
(
"Error importing container: %s"
,
err
)
id
,
err
:=
driver
.
Import
(
artifact
.
Files
()[
0
],
importRepo
)
if
err
!=
nil
{
return
nil
,
false
,
err
}
id
:=
strings
.
TrimSpace
(
stdout
.
String
())
ui
.
Message
(
"Imported ID: "
+
id
)
// Build the artifact
driver
:=
&
docker
.
DockerDriver
{
Tpl
:
p
.
config
.
tpl
,
Ui
:
ui
}
artifact
=
&
docker
.
ImportArtifact
{
BuilderIdValue
:
BuilderId
,
Driver
:
driver
,
...
...
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