Skip to content

Commit

Permalink
Add compression_level option to vagrant post-processors
Browse files Browse the repository at this point in the history
  • Loading branch information
maspwr committed Oct 8, 2013
1 parent 9b501b9 commit 4f10ff2
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 5 deletions.
14 changes: 13 additions & 1 deletion post-processor/vagrant/aws.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package vagrant

import (
"compress/flate"
"fmt"
"github.com/mitchellh/packer/common"
"github.com/mitchellh/packer/packer"
"io/ioutil"
"log"
"os"
"path/filepath"
"strconv"
"strings"
)

Expand All @@ -16,6 +18,7 @@ type AWSBoxConfig struct {

OutputPath string `mapstructure:"output"`
VagrantfileTemplate string `mapstructure:"vagrantfile_template"`
CompressionLevel string `mapstructure:"compression_level"`

tpl *packer.ConfigTemplate
}
Expand Down Expand Up @@ -46,6 +49,7 @@ func (p *AWSBoxPostProcessor) Configure(raws ...interface{}) error {
validates := map[string]*string{
"output": &p.config.OutputPath,
"vagrantfile_template": &p.config.VagrantfileTemplate,
"compression_level": &p.config.CompressionLevel,
}

for n, ptr := range validates {
Expand Down Expand Up @@ -127,14 +131,22 @@ func (p *AWSBoxPostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact
vf.Write([]byte(vagrantfileContents))
vf.Close()

var level int = flate.DefaultCompression
if p.config.CompressionLevel != "" {
level, err = strconv.Atoi(p.config.CompressionLevel)
if err != nil {
return nil, false, err
}
}

// Create the metadata
metadata := map[string]string{"provider": "aws"}
if err := WriteMetadata(dir, metadata); err != nil {
return nil, false, err
}

// Compress the directory to the given output path
if err := DirToBox(outputPath, dir, ui); err != nil {
if err := DirToBox(outputPath, dir, ui, level); err != nil {
err = fmt.Errorf("error creating box: %s", err)
return nil, false, err
}
Expand Down
7 changes: 5 additions & 2 deletions post-processor/vagrant/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,18 @@ func CopyContents(dst, src string) error {
// 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, ui packer.Ui) error {
func DirToBox(dst, dir string, ui packer.Ui, level int) error {
log.Printf("Turning dir into box: %s => %s", dir, dst)
dstF, err := os.Create(dst)
if err != nil {
return err
}
defer dstF.Close()

gzipWriter := gzip.NewWriter(dstF)
gzipWriter, err := gzip.NewWriterLevel(dstF, level)
if err != nil {
return err
}
defer gzipWriter.Close()

tarWriter := tar.NewWriter(gzipWriter)
Expand Down
14 changes: 13 additions & 1 deletion post-processor/vagrant/virtualbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package vagrant

import (
"archive/tar"
"compress/flate"
"errors"
"fmt"
"github.com/mitchellh/packer/common"
Expand All @@ -12,13 +13,15 @@ import (
"os"
"path/filepath"
"regexp"
"strconv"
)

type VBoxBoxConfig struct {
common.PackerConfig `mapstructure:",squash"`

OutputPath string `mapstructure:"output"`
VagrantfileTemplate string `mapstructure:"vagrantfile_template"`
CompressionLevel string `mapstructure:"compression_level"`

tpl *packer.ConfigTemplate
}
Expand Down Expand Up @@ -49,6 +52,7 @@ func (p *VBoxBoxPostProcessor) Configure(raws ...interface{}) error {
validates := map[string]*string{
"output": &p.config.OutputPath,
"vagrantfile_template": &p.config.VagrantfileTemplate,
"compression_level": &p.config.CompressionLevel,
}

for n, ptr := range validates {
Expand Down Expand Up @@ -141,6 +145,14 @@ func (p *VBoxBoxPostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifac
vf.Write([]byte(vagrantfileContents))
vf.Close()

var level int = flate.DefaultCompression
if p.config.CompressionLevel != "" {
level, err = strconv.Atoi(p.config.CompressionLevel)
if err != nil {
return nil, false, err
}
}

// Create the metadata
metadata := map[string]string{"provider": "virtualbox"}
if err := WriteMetadata(dir, metadata); err != nil {
Expand All @@ -155,7 +167,7 @@ func (p *VBoxBoxPostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifac

// Compress the directory to the given output path
ui.Message(fmt.Sprintf("Compressing box..."))
if err := DirToBox(outputPath, dir, ui); err != nil {
if err := DirToBox(outputPath, dir, ui, level); err != nil {
return nil, false, err
}

Expand Down
14 changes: 13 additions & 1 deletion post-processor/vagrant/vmware.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package vagrant

import (
"compress/flate"
"fmt"
"github.com/mitchellh/packer/common"
"github.com/mitchellh/packer/packer"
"io/ioutil"
"os"
"path/filepath"
"strconv"
)

type VMwareBoxConfig struct {
common.PackerConfig `mapstructure:",squash"`

OutputPath string `mapstructure:"output"`
VagrantfileTemplate string `mapstructure:"vagrantfile_template"`
CompressionLevel string `mapstructure:"compression_level"`

tpl *packer.ConfigTemplate
}
Expand All @@ -40,6 +43,7 @@ func (p *VMwareBoxPostProcessor) Configure(raws ...interface{}) error {
validates := map[string]*string{
"output": &p.config.OutputPath,
"vagrantfile_template": &p.config.VagrantfileTemplate,
"compression_level": &p.config.CompressionLevel,
}

for n, ptr := range validates {
Expand Down Expand Up @@ -111,6 +115,14 @@ func (p *VMwareBoxPostProcessor) PostProcess(ui packer.Ui, artifact packer.Artif
vf.Close()
}

var level int = flate.DefaultCompression
if p.config.CompressionLevel != "" {
level, err = strconv.Atoi(p.config.CompressionLevel)
if err != nil {
return nil, false, err
}
}

// Create the metadata
metadata := map[string]string{"provider": "vmware_desktop"}
if err := WriteMetadata(dir, metadata); err != nil {
Expand All @@ -119,7 +131,7 @@ func (p *VMwareBoxPostProcessor) PostProcess(ui packer.Ui, artifact packer.Artif

// Compress the directory to the given output path
ui.Message(fmt.Sprintf("Compressing box..."))
if err := DirToBox(outputPath, dir, ui); err != nil {
if err := DirToBox(outputPath, dir, ui, level); err != nil {
return nil, false, err
}

Expand Down
5 changes: 5 additions & 0 deletions website/source/docs/post-processors/vagrant.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ The AWS provider itself can be configured with specific options:
this is a template that simply sets the AMIs for the various regions
of the AWS build.

* `compression_level` (integer) - An integer repesenting the
compression level to use when creating the Vagrant box. Valid
values range from 0 to 9, with 0 being no compression and 9 being
the best compression.

The `vagrantfile_template` has the `Images` variable which is a map
of region (string) to AMI ID (string). An example Vagrantfile template for
AWS is shown below. The example simply sets the AMI for each region.
Expand Down

0 comments on commit 4f10ff2

Please sign in to comment.