Commit e5c5f685 authored by Mark Aaron Shirley's avatar Mark Aaron Shirley

Add compression_level option to vagrant post-processors

parent ff970483
package vagrant package vagrant
import ( import (
"compress/flate"
"fmt" "fmt"
"github.com/mitchellh/packer/common" "github.com/mitchellh/packer/common"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
...@@ -8,6 +9,7 @@ import ( ...@@ -8,6 +9,7 @@ import (
"log" "log"
"os" "os"
"path/filepath" "path/filepath"
"strconv"
"strings" "strings"
) )
...@@ -16,6 +18,7 @@ type AWSBoxConfig struct { ...@@ -16,6 +18,7 @@ type AWSBoxConfig struct {
OutputPath string `mapstructure:"output"` OutputPath string `mapstructure:"output"`
VagrantfileTemplate string `mapstructure:"vagrantfile_template"` VagrantfileTemplate string `mapstructure:"vagrantfile_template"`
CompressionLevel string `mapstructure:"compression_level"`
tpl *packer.ConfigTemplate tpl *packer.ConfigTemplate
} }
...@@ -46,6 +49,7 @@ func (p *AWSBoxPostProcessor) Configure(raws ...interface{}) error { ...@@ -46,6 +49,7 @@ func (p *AWSBoxPostProcessor) Configure(raws ...interface{}) error {
validates := map[string]*string{ validates := map[string]*string{
"output": &p.config.OutputPath, "output": &p.config.OutputPath,
"vagrantfile_template": &p.config.VagrantfileTemplate, "vagrantfile_template": &p.config.VagrantfileTemplate,
"compression_level": &p.config.CompressionLevel,
} }
for n, ptr := range validates { for n, ptr := range validates {
...@@ -127,6 +131,14 @@ func (p *AWSBoxPostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact ...@@ -127,6 +131,14 @@ func (p *AWSBoxPostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact
vf.Write([]byte(vagrantfileContents)) vf.Write([]byte(vagrantfileContents))
vf.Close() 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 // Create the metadata
metadata := map[string]string{"provider": "aws"} metadata := map[string]string{"provider": "aws"}
if err := WriteMetadata(dir, metadata); err != nil { if err := WriteMetadata(dir, metadata); err != nil {
...@@ -134,7 +146,7 @@ func (p *AWSBoxPostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact ...@@ -134,7 +146,7 @@ func (p *AWSBoxPostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact
} }
// Compress the directory to the given output path // 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) err = fmt.Errorf("error creating box: %s", err)
return nil, false, err return nil, false, err
} }
......
...@@ -44,7 +44,7 @@ func CopyContents(dst, src string) error { ...@@ -44,7 +44,7 @@ func CopyContents(dst, src string) error {
// DirToBox takes the directory and compresses it into a Vagrant-compatible // DirToBox takes the directory and compresses it into a Vagrant-compatible
// box. This function does not perform checks to verify that dir is // box. This function does not perform checks to verify that dir is
// actually a proper box. This is an expected precondition. // 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) log.Printf("Turning dir into box: %s => %s", dir, dst)
dstF, err := os.Create(dst) dstF, err := os.Create(dst)
if err != nil { if err != nil {
...@@ -52,7 +52,10 @@ func DirToBox(dst, dir string, ui packer.Ui) error { ...@@ -52,7 +52,10 @@ func DirToBox(dst, dir string, ui packer.Ui) error {
} }
defer dstF.Close() defer dstF.Close()
gzipWriter := gzip.NewWriter(dstF) gzipWriter, err := gzip.NewWriterLevel(dstF, level)
if err != nil {
return err
}
defer gzipWriter.Close() defer gzipWriter.Close()
tarWriter := tar.NewWriter(gzipWriter) tarWriter := tar.NewWriter(gzipWriter)
......
...@@ -2,6 +2,7 @@ package vagrant ...@@ -2,6 +2,7 @@ package vagrant
import ( import (
"archive/tar" "archive/tar"
"compress/flate"
"errors" "errors"
"fmt" "fmt"
"github.com/mitchellh/packer/common" "github.com/mitchellh/packer/common"
...@@ -12,6 +13,7 @@ import ( ...@@ -12,6 +13,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strconv"
) )
type VBoxBoxConfig struct { type VBoxBoxConfig struct {
...@@ -19,6 +21,7 @@ type VBoxBoxConfig struct { ...@@ -19,6 +21,7 @@ type VBoxBoxConfig struct {
OutputPath string `mapstructure:"output"` OutputPath string `mapstructure:"output"`
VagrantfileTemplate string `mapstructure:"vagrantfile_template"` VagrantfileTemplate string `mapstructure:"vagrantfile_template"`
CompressionLevel string `mapstructure:"compression_level"`
tpl *packer.ConfigTemplate tpl *packer.ConfigTemplate
} }
...@@ -49,6 +52,7 @@ func (p *VBoxBoxPostProcessor) Configure(raws ...interface{}) error { ...@@ -49,6 +52,7 @@ func (p *VBoxBoxPostProcessor) Configure(raws ...interface{}) error {
validates := map[string]*string{ validates := map[string]*string{
"output": &p.config.OutputPath, "output": &p.config.OutputPath,
"vagrantfile_template": &p.config.VagrantfileTemplate, "vagrantfile_template": &p.config.VagrantfileTemplate,
"compression_level": &p.config.CompressionLevel,
} }
for n, ptr := range validates { for n, ptr := range validates {
...@@ -141,6 +145,14 @@ func (p *VBoxBoxPostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifac ...@@ -141,6 +145,14 @@ func (p *VBoxBoxPostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifac
vf.Write([]byte(vagrantfileContents)) vf.Write([]byte(vagrantfileContents))
vf.Close() 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 // Create the metadata
metadata := map[string]string{"provider": "virtualbox"} metadata := map[string]string{"provider": "virtualbox"}
if err := WriteMetadata(dir, metadata); err != nil { if err := WriteMetadata(dir, metadata); err != nil {
...@@ -155,7 +167,7 @@ func (p *VBoxBoxPostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifac ...@@ -155,7 +167,7 @@ func (p *VBoxBoxPostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifac
// Compress the directory to the given output path // Compress the directory to the given output path
ui.Message(fmt.Sprintf("Compressing box...")) 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 return nil, false, err
} }
......
package vagrant package vagrant
import ( import (
"compress/flate"
"fmt" "fmt"
"github.com/mitchellh/packer/common" "github.com/mitchellh/packer/common"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strconv"
) )
type VMwareBoxConfig struct { type VMwareBoxConfig struct {
...@@ -14,6 +16,7 @@ type VMwareBoxConfig struct { ...@@ -14,6 +16,7 @@ type VMwareBoxConfig struct {
OutputPath string `mapstructure:"output"` OutputPath string `mapstructure:"output"`
VagrantfileTemplate string `mapstructure:"vagrantfile_template"` VagrantfileTemplate string `mapstructure:"vagrantfile_template"`
CompressionLevel string `mapstructure:"compression_level"`
tpl *packer.ConfigTemplate tpl *packer.ConfigTemplate
} }
...@@ -40,6 +43,7 @@ func (p *VMwareBoxPostProcessor) Configure(raws ...interface{}) error { ...@@ -40,6 +43,7 @@ func (p *VMwareBoxPostProcessor) Configure(raws ...interface{}) error {
validates := map[string]*string{ validates := map[string]*string{
"output": &p.config.OutputPath, "output": &p.config.OutputPath,
"vagrantfile_template": &p.config.VagrantfileTemplate, "vagrantfile_template": &p.config.VagrantfileTemplate,
"compression_level": &p.config.CompressionLevel,
} }
for n, ptr := range validates { for n, ptr := range validates {
...@@ -111,6 +115,14 @@ func (p *VMwareBoxPostProcessor) PostProcess(ui packer.Ui, artifact packer.Artif ...@@ -111,6 +115,14 @@ func (p *VMwareBoxPostProcessor) PostProcess(ui packer.Ui, artifact packer.Artif
vf.Close() 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 // Create the metadata
metadata := map[string]string{"provider": "vmware_desktop"} metadata := map[string]string{"provider": "vmware_desktop"}
if err := WriteMetadata(dir, metadata); err != nil { if err := WriteMetadata(dir, metadata); err != nil {
...@@ -119,7 +131,7 @@ func (p *VMwareBoxPostProcessor) PostProcess(ui packer.Ui, artifact packer.Artif ...@@ -119,7 +131,7 @@ func (p *VMwareBoxPostProcessor) PostProcess(ui packer.Ui, artifact packer.Artif
// Compress the directory to the given output path // Compress the directory to the given output path
ui.Message(fmt.Sprintf("Compressing box...")) 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 return nil, false, err
} }
......
...@@ -68,6 +68,11 @@ The AWS provider itself can be configured with specific options: ...@@ -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 this is a template that simply sets the AMIs for the various regions
of the AWS build. 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 The `vagrantfile_template` has the `Images` variable which is a map
of region (string) to AMI ID (string). An example Vagrantfile template for 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. AWS is shown below. The example simply sets the AMI for each region.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment