2019-10-14 10:43:59 -04:00
|
|
|
//go:generate mapstructure-to-hcl2 -type Config
|
|
|
|
|
2013-06-26 20:37:46 -04:00
|
|
|
// vagrant implements the packer.PostProcessor interface and adds a
|
|
|
|
// post-processor that turns artifacts of known builders into Vagrant
|
|
|
|
// boxes.
|
|
|
|
package vagrant
|
|
|
|
|
|
|
|
import (
|
2013-12-19 16:22:46 -05:00
|
|
|
"compress/flate"
|
2019-03-22 09:56:02 -04:00
|
|
|
"context"
|
2013-06-26 22:09:24 -04:00
|
|
|
"fmt"
|
2013-12-19 16:22:46 -05:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"text/template"
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
"github.com/hashicorp/hcl/v2/hcldec"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/common"
|
|
|
|
"github.com/hashicorp/packer/helper/config"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
2018-12-12 09:45:00 -05:00
|
|
|
"github.com/hashicorp/packer/packer/tmp"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
2015-05-27 17:36:15 -04:00
|
|
|
"github.com/mitchellh/mapstructure"
|
2013-06-26 20:37:46 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var builtins = map[string]string{
|
2018-07-06 18:11:24 -04:00
|
|
|
"mitchellh.amazonebs": "aws",
|
|
|
|
"mitchellh.amazon.instance": "aws",
|
|
|
|
"mitchellh.virtualbox": "virtualbox",
|
|
|
|
"mitchellh.vmware": "vmware",
|
|
|
|
"mitchellh.vmware-esx": "vmware",
|
|
|
|
"pearkes.digitalocean": "digitalocean",
|
|
|
|
"packer.googlecompute": "google",
|
|
|
|
"hashicorp.scaleway": "scaleway",
|
|
|
|
"packer.parallels": "parallels",
|
|
|
|
"MSOpenTech.hyperv": "hyperv",
|
|
|
|
"transcend.qemu": "libvirt",
|
|
|
|
"ustream.lxc": "lxc",
|
2018-08-08 11:04:28 -04:00
|
|
|
"Azure.ResourceManagement.VMImage": "azure",
|
2018-07-06 18:11:24 -04:00
|
|
|
"packer.post-processor.docker-import": "docker",
|
2018-07-16 16:35:02 -04:00
|
|
|
"packer.post-processor.docker-tag": "docker",
|
|
|
|
"packer.post-processor.docker-push": "docker",
|
2013-06-26 20:37:46 -04:00
|
|
|
}
|
|
|
|
|
2013-06-26 22:09:39 -04:00
|
|
|
type Config struct {
|
2013-08-22 21:19:03 -04:00
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
|
|
|
|
2019-08-05 13:35:01 -04:00
|
|
|
CompressionLevel int `mapstructure:"compression_level"`
|
|
|
|
Include []string `mapstructure:"include"`
|
|
|
|
OutputPath string `mapstructure:"output"`
|
|
|
|
Override map[string]interface{}
|
|
|
|
VagrantfileTemplate string `mapstructure:"vagrantfile_template"`
|
|
|
|
VagrantfileTemplateGenerated bool `mapstructure:"vagrantfile_template_generated"`
|
2013-12-19 16:22:46 -05:00
|
|
|
|
2015-05-27 17:36:15 -04:00
|
|
|
ctx interpolate.Context
|
2013-12-19 16:22:46 -05:00
|
|
|
}
|
|
|
|
|
2013-06-26 20:37:46 -04:00
|
|
|
type PostProcessor struct {
|
2020-01-14 05:13:16 -05:00
|
|
|
config Config
|
2013-12-19 16:22:46 -05:00
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
func (p *PostProcessor) ConfigSpec() hcldec.ObjectSpec {
|
2020-01-14 05:13:16 -05:00
|
|
|
return p.config.FlatMapstructure().HCL2Spec()
|
2019-12-17 05:25:56 -05:00
|
|
|
}
|
|
|
|
|
2013-07-01 17:59:23 -04:00
|
|
|
func (p *PostProcessor) Configure(raws ...interface{}) error {
|
2020-01-14 05:13:16 -05:00
|
|
|
if err := p.configureSingle(&p.config, raws...); err != nil {
|
2013-08-22 21:19:03 -04:00
|
|
|
return err
|
2013-06-26 22:09:24 -04:00
|
|
|
}
|
2013-06-26 20:37:46 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-10-17 05:57:24 -04:00
|
|
|
func (p *PostProcessor) PostProcessProvider(name string, provider Provider, ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) {
|
2020-01-14 05:13:16 -05:00
|
|
|
config, err := p.specificConfig(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
2013-12-19 17:44:15 -05:00
|
|
|
}
|
|
|
|
|
2020-01-14 05:13:16 -05:00
|
|
|
err = CreateDummyBox(ui, config.CompressionLevel)
|
2020-01-08 14:33:39 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
2013-12-19 16:22:46 -05:00
|
|
|
ui.Say(fmt.Sprintf("Creating Vagrant box for '%s' provider", name))
|
|
|
|
|
2020-01-30 05:27:58 -05:00
|
|
|
var generatedData map[interface{}]interface{}
|
|
|
|
stateData := artifact.State("generated_data")
|
|
|
|
if stateData != nil {
|
|
|
|
// Make sure it's not a nil map so we can assign to it later.
|
|
|
|
generatedData = stateData.(map[interface{}]interface{})
|
2015-06-23 11:39:57 -04:00
|
|
|
}
|
2020-01-30 05:27:58 -05:00
|
|
|
// If stateData has a nil map generatedData will be nil
|
|
|
|
// and we need to make sure it's not
|
|
|
|
if generatedData == nil {
|
|
|
|
generatedData = make(map[interface{}]interface{})
|
|
|
|
}
|
|
|
|
generatedData["ArtifactId"] = artifact.Id()
|
|
|
|
generatedData["BuildName"] = config.PackerBuildName
|
|
|
|
generatedData["Provider"] = name
|
|
|
|
config.ctx.Data = generatedData
|
|
|
|
|
2015-06-23 11:39:57 -04:00
|
|
|
outputPath, err := interpolate.Render(config.OutputPath, &config.ctx)
|
2013-12-19 16:22:46 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a temporary directory for us to build the contents of the box in
|
2018-12-12 09:45:00 -05:00
|
|
|
dir, err := tmp.Dir("packer")
|
2013-12-19 16:22:46 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
// Copy all of the includes files into the temporary directory
|
2013-12-19 17:44:15 -05:00
|
|
|
for _, src := range config.Include {
|
2013-12-19 16:22:46 -05:00
|
|
|
ui.Message(fmt.Sprintf("Copying from include: %s", src))
|
|
|
|
dst := filepath.Join(dir, filepath.Base(src))
|
|
|
|
if err := CopyContents(dst, src); err != nil {
|
|
|
|
err = fmt.Errorf("Error copying include file: %s\n\n%s", src, err)
|
2013-07-01 14:30:39 -04:00
|
|
|
return nil, false, err
|
2013-06-27 10:14:15 -04:00
|
|
|
}
|
2013-12-19 16:22:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run the provider processing step
|
|
|
|
vagrantfile, metadata, err := provider.Process(ui, artifact, dir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the metadata we got
|
|
|
|
if err := WriteMetadata(dir, metadata); err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
2013-09-18 18:01:06 -04:00
|
|
|
|
2013-12-19 16:22:46 -05:00
|
|
|
// Write our Vagrantfile
|
|
|
|
var customVagrantfile string
|
2013-12-19 17:44:15 -05:00
|
|
|
if config.VagrantfileTemplate != "" {
|
2014-11-25 01:04:17 -05:00
|
|
|
ui.Message(fmt.Sprintf("Using custom Vagrantfile: %s", config.VagrantfileTemplate))
|
|
|
|
customBytes, err := ioutil.ReadFile(config.VagrantfileTemplate)
|
2013-12-19 16:22:46 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
2013-09-18 18:01:06 -04:00
|
|
|
}
|
2013-12-19 16:22:46 -05:00
|
|
|
|
|
|
|
customVagrantfile = string(customBytes)
|
2013-06-26 22:09:24 -04:00
|
|
|
}
|
|
|
|
|
2013-12-19 16:22:46 -05:00
|
|
|
f, err := os.Create(filepath.Join(dir, "Vagrantfile"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
2013-06-27 10:14:15 -04:00
|
|
|
|
2013-12-19 16:22:46 -05:00
|
|
|
t := template.Must(template.New("root").Parse(boxVagrantfileContents))
|
2013-12-19 16:52:48 -05:00
|
|
|
err = t.Execute(f, &vagrantfileTemplate{
|
2013-12-19 16:22:46 -05:00
|
|
|
ProviderVagrantfile: vagrantfile,
|
|
|
|
CustomVagrantfile: customVagrantfile,
|
|
|
|
})
|
|
|
|
f.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
2013-09-18 18:01:06 -04:00
|
|
|
}
|
|
|
|
|
2013-12-19 16:22:46 -05:00
|
|
|
// Create the box
|
2013-12-19 17:44:15 -05:00
|
|
|
if err := DirToBox(outputPath, dir, ui, config.CompressionLevel); err != nil {
|
2013-12-19 16:22:46 -05:00
|
|
|
return nil, false, err
|
2013-09-18 18:01:06 -04:00
|
|
|
}
|
|
|
|
|
2014-02-21 23:02:40 -05:00
|
|
|
return NewArtifact(name, outputPath), provider.KeepInputArtifact(), nil
|
2013-09-18 18:01:06 -04:00
|
|
|
}
|
|
|
|
|
2019-04-08 13:59:42 -04:00
|
|
|
func (p *PostProcessor) PostProcess(ctx context.Context, ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, bool, error) {
|
2014-10-17 05:57:24 -04:00
|
|
|
|
|
|
|
name, ok := builtins[artifact.BuilderId()]
|
|
|
|
if !ok {
|
2019-04-02 19:51:58 -04:00
|
|
|
return nil, false, false, fmt.Errorf(
|
2014-10-17 05:57:24 -04:00
|
|
|
"Unknown artifact type, can't build box: %s", artifact.BuilderId())
|
|
|
|
}
|
|
|
|
|
|
|
|
provider := providerForName(name)
|
|
|
|
if provider == nil {
|
|
|
|
// This shouldn't happen since we hard code all of these ourselves
|
|
|
|
panic(fmt.Sprintf("bad provider name: %s", name))
|
|
|
|
}
|
|
|
|
|
2019-04-02 19:51:58 -04:00
|
|
|
artifact, keep, err := p.PostProcessProvider(name, provider, ui, artifact)
|
|
|
|
|
|
|
|
// In some cases, (e.g. AMI), deleting the input artifact would render the
|
|
|
|
// resulting vagrant box useless. Because of these cases, we want to
|
|
|
|
// forcibly set keep_input_artifact.
|
|
|
|
|
|
|
|
// TODO: rework all provisioners to only forcibly keep those where it matters
|
|
|
|
return artifact, keep, true, err
|
2014-10-17 05:57:24 -04:00
|
|
|
}
|
|
|
|
|
2015-05-27 17:36:15 -04:00
|
|
|
func (p *PostProcessor) configureSingle(c *Config, raws ...interface{}) error {
|
|
|
|
var md mapstructure.Metadata
|
|
|
|
err := config.Decode(c, &config.DecodeOpts{
|
2015-06-22 15:24:27 -04:00
|
|
|
Metadata: &md,
|
|
|
|
Interpolate: true,
|
|
|
|
InterpolateContext: &c.ctx,
|
2015-05-27 17:36:15 -04:00
|
|
|
InterpolateFilter: &interpolate.RenderFilter{
|
|
|
|
Exclude: []string{
|
|
|
|
"output",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, raws...)
|
2013-12-19 17:44:15 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Defaults
|
2015-05-27 17:36:15 -04:00
|
|
|
if c.OutputPath == "" {
|
|
|
|
c.OutputPath = "packer_{{ .BuildName }}_{{.Provider}}.box"
|
2013-12-19 17:44:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
found := false
|
|
|
|
for _, k := range md.Keys {
|
|
|
|
if k == "compression_level" {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
2015-05-27 17:36:15 -04:00
|
|
|
c.CompressionLevel = flate.DefaultCompression
|
2013-12-19 17:44:15 -05:00
|
|
|
}
|
|
|
|
|
2015-05-27 17:36:15 -04:00
|
|
|
var errs *packer.MultiError
|
2019-08-05 13:35:01 -04:00
|
|
|
if c.VagrantfileTemplate != "" && c.VagrantfileTemplateGenerated == false {
|
2015-05-27 17:36:15 -04:00
|
|
|
_, err := os.Stat(c.VagrantfileTemplate)
|
2014-11-25 01:04:17 -05:00
|
|
|
if err != nil {
|
2015-05-27 17:36:15 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, fmt.Errorf(
|
|
|
|
"vagrantfile_template '%s' does not exist", c.VagrantfileTemplate))
|
2013-12-19 17:44:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if errs != nil && len(errs.Errors) > 0 {
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-14 05:13:16 -05:00
|
|
|
func (p *PostProcessor) specificConfig(name string) (Config, error) {
|
|
|
|
config := p.config
|
|
|
|
if _, ok := config.Override[name]; ok {
|
|
|
|
if err := mapstructure.Decode(config.Override[name], &config); err != nil {
|
|
|
|
err = fmt.Errorf("Error overriding config for %s: %s", name, err)
|
|
|
|
return config, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
2013-12-19 16:22:46 -05:00
|
|
|
func providerForName(name string) Provider {
|
|
|
|
switch name {
|
2013-12-25 13:01:59 -05:00
|
|
|
case "aws":
|
|
|
|
return new(AWSProvider)
|
2017-04-06 05:19:17 -04:00
|
|
|
case "scaleway":
|
|
|
|
return new(ScalewayProvider)
|
2013-12-25 13:01:59 -05:00
|
|
|
case "digitalocean":
|
|
|
|
return new(DigitalOceanProvider)
|
2013-06-27 10:39:11 -04:00
|
|
|
case "virtualbox":
|
2013-12-19 16:22:46 -05:00
|
|
|
return new(VBoxProvider)
|
2013-12-25 13:01:59 -05:00
|
|
|
case "vmware":
|
|
|
|
return new(VMwareProvider)
|
2014-04-09 01:22:48 -04:00
|
|
|
case "parallels":
|
|
|
|
return new(ParallelsProvider)
|
2014-06-23 14:19:06 -04:00
|
|
|
case "hyperv":
|
|
|
|
return new(HypervProvider)
|
2014-01-05 20:02:30 -05:00
|
|
|
case "libvirt":
|
|
|
|
return new(LibVirtProvider)
|
2017-12-23 01:13:17 -05:00
|
|
|
case "google":
|
|
|
|
return new(GoogleProvider)
|
2018-03-06 15:02:19 -05:00
|
|
|
case "lxc":
|
|
|
|
return new(LXCProvider)
|
2018-08-08 11:04:28 -04:00
|
|
|
case "azure":
|
|
|
|
return new(AzureProvider)
|
2018-07-06 18:11:24 -04:00
|
|
|
case "docker":
|
|
|
|
return new(DockerProvider)
|
2013-06-27 10:14:15 -04:00
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2013-12-19 16:22:46 -05:00
|
|
|
|
2013-12-19 16:52:48 -05:00
|
|
|
type vagrantfileTemplate struct {
|
|
|
|
ProviderVagrantfile string
|
|
|
|
CustomVagrantfile string
|
|
|
|
}
|
|
|
|
|
2013-12-19 16:22:46 -05:00
|
|
|
const boxVagrantfileContents string = `
|
|
|
|
# The contents below were provided by the Packer Vagrant post-processor
|
|
|
|
{{ .ProviderVagrantfile }}
|
|
|
|
|
|
|
|
# The contents below (if any) are custom contents provided by the
|
|
|
|
# Packer template during image build.
|
|
|
|
{{ .CustomVagrantfile }}
|
|
|
|
`
|