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
f4c9f960
Commit
f4c9f960
authored
Jun 26, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
post-processor/vagrant: Can make AWS boxes!
parent
8a609b67
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
205 additions
and
3 deletions
+205
-3
post-processor/vagrant/artifact.go
post-processor/vagrant/artifact.go
+40
-0
post-processor/vagrant/artifact_test.go
post-processor/vagrant/artifact_test.go
+14
-0
post-processor/vagrant/aws.go
post-processor/vagrant/aws.go
+75
-1
post-processor/vagrant/aws_test.go
post-processor/vagrant/aws_test.go
+0
-1
post-processor/vagrant/post-processor.go
post-processor/vagrant/post-processor.go
+1
-1
post-processor/vagrant/util.go
post-processor/vagrant/util.go
+75
-0
No files found.
post-processor/vagrant/artifact.go
0 → 100644
View file @
f4c9f960
package
vagrant
import
(
"fmt"
"os"
)
const
BuilderId
=
"mitchellh.post-processor.vagrant"
type
Artifact
struct
{
Path
string
Provider
string
}
func
NewArtifact
(
provider
,
path
string
)
*
Artifact
{
return
&
Artifact
{
Path
:
path
,
Provider
:
provider
,
}
}
func
(
*
Artifact
)
BuilderId
()
string
{
return
BuilderId
}
func
(
a
*
Artifact
)
Files
()
[]
string
{
return
[]
string
{
a
.
Path
}
}
func
(
a
*
Artifact
)
Id
()
string
{
return
""
}
func
(
a
*
Artifact
)
String
()
string
{
return
fmt
.
Sprintf
(
"'%s' provider box: %s"
,
a
.
Provider
,
a
.
Path
)
}
func
(
a
*
Artifact
)
Destroy
()
error
{
return
os
.
Remove
(
a
.
Path
)
}
post-processor/vagrant/artifact_test.go
0 → 100644
View file @
f4c9f960
package
vagrant
import
(
"github.com/mitchellh/packer/packer"
"testing"
)
func
TestArtifact_ImplementsArtifact
(
t
*
testing
.
T
)
{
var
raw
interface
{}
raw
=
&
Artifact
{}
if
_
,
ok
:=
raw
.
(
packer
.
Artifact
);
!
ok
{
t
.
Fatalf
(
"Artifact should be a Artifact"
)
}
}
post-processor/vagrant/aws.go
View file @
f4c9f960
package
vagrant
package
vagrant
import
(
import
(
"fmt"
"github.com/mitchellh/mapstructure"
"github.com/mitchellh/packer/packer"
"github.com/mitchellh/packer/packer"
"io/ioutil"
"os"
"path/filepath"
"strings"
"text/template"
)
)
type
AWSBoxConfig
struct
{
OutputPath
string
`mapstructure:"output"`
}
type
AWSVagrantfileTemplate
struct
{
Images
map
[
string
]
string
}
type
AWSBoxPostProcessor
struct
{
type
AWSBoxPostProcessor
struct
{
config
AWSBoxConfig
}
}
func
(
p
*
AWSBoxPostProcessor
)
Configure
(
raw
interface
{})
error
{
func
(
p
*
AWSBoxPostProcessor
)
Configure
(
raw
interface
{})
error
{
err
:=
mapstructure
.
Decode
(
raw
,
&
p
.
config
)
if
err
!=
nil
{
return
err
}
return
nil
return
nil
}
}
func
(
p
*
AWSBoxPostProcessor
)
PostProcess
(
ui
packer
.
Ui
,
artifact
packer
.
Artifact
)
(
packer
.
Artifact
,
error
)
{
func
(
p
*
AWSBoxPostProcessor
)
PostProcess
(
ui
packer
.
Ui
,
artifact
packer
.
Artifact
)
(
packer
.
Artifact
,
error
)
{
return
nil
,
nil
// Determine the regions...
tplData
:=
&
AWSVagrantfileTemplate
{
Images
:
make
(
map
[
string
]
string
),
}
for
_
,
regions
:=
range
strings
.
Split
(
artifact
.
Id
(),
","
)
{
parts
:=
strings
.
Split
(
regions
,
":"
)
if
len
(
parts
)
!=
2
{
return
nil
,
fmt
.
Errorf
(
"Poorly formatted artifact ID: %s"
,
artifact
.
Id
())
}
tplData
.
Images
[
parts
[
0
]]
=
parts
[
1
]
}
// Create a temporary directory for us to build the contents of the box in
dir
,
err
:=
ioutil
.
TempDir
(
""
,
"packer"
)
if
err
!=
nil
{
return
nil
,
err
}
defer
os
.
RemoveAll
(
dir
)
// Create the Vagrantfile from the template
vf
,
err
:=
os
.
Create
(
filepath
.
Join
(
dir
,
"Vagrantfile"
))
if
err
!=
nil
{
return
nil
,
err
}
defer
vf
.
Close
()
t
:=
template
.
Must
(
template
.
New
(
"vagrantfile"
)
.
Parse
(
defaultVagrantfile
))
t
.
Execute
(
vf
,
tplData
)
vf
.
Close
()
// Create the metadata
metadata
:=
map
[
string
]
string
{
"provider"
:
"aws"
}
if
err
:=
WriteMetadata
(
dir
,
metadata
);
err
!=
nil
{
return
nil
,
err
}
// Compress the directory to the given output path
if
err
:=
DirToBox
(
p
.
config
.
OutputPath
,
dir
);
err
!=
nil
{
return
nil
,
err
}
return
NewArtifact
(
"aws"
,
p
.
config
.
OutputPath
),
nil
}
}
var
defaultVagrantfile
=
`
Vagrant.configure("2") do |config|
config.vm.provider "aws" do |aws|
{{ range $region, $ami := .Images }}
aws.region_config "{{ $region }}", ami: "{{ $ami }}"
{{ end }}
end
end
`
post-processor/vagrant/aws_test.go
View file @
f4c9f960
...
@@ -12,4 +12,3 @@ func TestPostProcessor_ImplementsPostProcessor(t *testing.T) {
...
@@ -12,4 +12,3 @@ func TestPostProcessor_ImplementsPostProcessor(t *testing.T) {
t
.
Fatalf
(
"AWS PostProcessor should be a PostProcessor"
)
t
.
Fatalf
(
"AWS PostProcessor should be a PostProcessor"
)
}
}
}
}
post-processor/vagrant/post-processor.go
View file @
f4c9f960
...
@@ -11,7 +11,7 @@ var builtins = map[string]string{
...
@@ -11,7 +11,7 @@ var builtins = map[string]string{
"mitchellh.amazonebs"
:
"aws"
,
"mitchellh.amazonebs"
:
"aws"
,
}
}
type
Config
struct
{}
type
Config
struct
{}
type
PostProcessor
struct
{
type
PostProcessor
struct
{
config
Config
config
Config
...
...
post-processor/vagrant/util.go
0 → 100644
View file @
f4c9f960
package
vagrant
import
(
"archive/tar"
"compress/gzip"
"encoding/json"
"io"
"os"
"path/filepath"
)
// DirToBox takes the directory and compresses it into a Vagrant-compatible
// box. This function does not perform checks to verify that dir is
// actually a proper box. This is an expected precondition.
func
DirToBox
(
dst
,
dir
string
)
error
{
dstF
,
err
:=
os
.
Create
(
dst
)
if
err
!=
nil
{
return
err
}
defer
dstF
.
Close
()
gzipWriter
:=
gzip
.
NewWriter
(
dstF
)
defer
gzipWriter
.
Close
()
tarWriter
:=
tar
.
NewWriter
(
gzipWriter
)
defer
tarWriter
.
Close
()
// This is the walk func that tars each of the files in the dir
tarWalk
:=
func
(
path
string
,
info
os
.
FileInfo
,
prevErr
error
)
error
{
f
,
err
:=
os
.
Open
(
path
)
if
err
!=
nil
{
return
err
}
defer
f
.
Close
()
header
,
err
:=
tar
.
FileInfoHeader
(
info
,
""
)
if
err
!=
nil
{
return
err
}
// We have to set the Name explicitly because it is supposed to
// be a relative path to the root. Otherwise, the tar ends up
// being a bunch of files in the root, even if they're actually
// nested in a dir in the original "dir" param.
header
.
Name
,
err
=
filepath
.
Rel
(
dir
,
path
)
if
err
!=
nil
{
return
err
}
if
err
:=
tarWriter
.
WriteHeader
(
header
);
err
!=
nil
{
return
err
}
if
_
,
err
:=
io
.
Copy
(
tarWriter
,
f
);
err
!=
nil
{
return
err
}
return
nil
}
// Tar.gz everything up
return
filepath
.
Walk
(
dir
,
tarWalk
)
}
// WriteMetadata writes the "metadata.json" file for a Vagrant box.
func
WriteMetadata
(
dir
string
,
contents
interface
{})
error
{
f
,
err
:=
os
.
Create
(
filepath
.
Join
(
dir
,
"metadata.json"
))
if
err
!=
nil
{
return
err
}
defer
f
.
Close
()
enc
:=
json
.
NewEncoder
(
f
)
return
enc
.
Encode
(
contents
)
}
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