packer-cn/post-processor/vagrant/post-processor.go
Adrien Delorme 0785c2f6fc
build using HCL2 (#8423)
This follows #8232 which added the code to generate the code required to parse
HCL files for each packer component.

All old config files of packer will keep on working the same. Packer takes one
argument. When a directory is passed, all files in the folder with a name
ending with  “.pkr.hcl” or “.pkr.json” will be parsed using the HCL2 format.
When a file ending with “.pkr.hcl” or “.pkr.json” is passed it will be parsed
using the HCL2 format. For every other case; the old packer style will be used.

## 1. the hcl2template pkg can create a packer.Build from a set of HCL (v2) files

I had to make the packer.coreBuild (which is our one and only packer.Build ) a public struct with public fields

## 2. Components interfaces get a new ConfigSpec Method to read a file from an HCL file.

  This is a breaking change for packer plugins.

a packer component can be a: builder/provisioner/post-processor

each component interface now gets a `ConfigSpec() hcldec.ObjectSpec`
which allows packer to tell what is the layout of the hcl2 config meant
to configure that specific component.

This ObjectSpec is sent through the wire (RPC) and a cty.Value is now
sent through the already existing configuration entrypoints:

 Provisioner.Prepare(raws ...interface{}) error
 Builder.Prepare(raws ...interface{}) ([]string, error)
 PostProcessor.Configure(raws ...interface{}) error

close #1768


Example hcl files:

```hcl
// file amazon-ebs-kms-key/run.pkr.hcl
build {
    sources = [
        "source.amazon-ebs.first",
    ]

    provisioner "shell" {
        inline = [
            "sleep 5"
        ]
    }

    post-processor "shell-local" {
        inline = [
            "sleep 5"
        ]
    }
}

// amazon-ebs-kms-key/source.pkr.hcl

source "amazon-ebs" "first" {

    ami_name = "hcl2-test"
    region = "us-east-1"
    instance_type = "t2.micro"

    kms_key_id = "c729958f-c6ba-44cd-ab39-35ab68ce0a6c"
    encrypt_boot = true
    source_ami_filter {
        filters {
          virtualization-type = "hvm"
          name =  "amzn-ami-hvm-????.??.?.????????-x86_64-gp2"
          root-device-type = "ebs"
        }
        most_recent = true
        owners = ["amazon"]
    }
    launch_block_device_mappings {
        device_name = "/dev/xvda"
        volume_size = 20
        volume_type = "gp2"
        delete_on_termination = "true"
    }
    launch_block_device_mappings {
        device_name = "/dev/xvdf"
        volume_size = 500
        volume_type = "gp2"
        delete_on_termination = true
        encrypted = true
    }

    ami_regions = ["eu-central-1"]
    run_tags {
        Name = "packer-solr-something"
        stack-name = "DevOps Tools"
    }
    
    communicator = "ssh"
    ssh_pty = true
    ssh_username = "ec2-user"
    associate_public_ip_address = true
}
```
2019-12-17 11:25:56 +01:00

296 lines
8.0 KiB
Go

//go:generate mapstructure-to-hcl2 -type Config
// vagrant implements the packer.PostProcessor interface and adds a
// post-processor that turns artifacts of known builders into Vagrant
// boxes.
package vagrant
import (
"compress/flate"
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"text/template"
"github.com/hashicorp/hcl/v2/hcldec"
"github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/config"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/packer/tmp"
"github.com/hashicorp/packer/template/interpolate"
"github.com/mitchellh/mapstructure"
)
var builtins = map[string]string{
"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",
"Azure.ResourceManagement.VMImage": "azure",
"packer.post-processor.docker-import": "docker",
"packer.post-processor.docker-tag": "docker",
"packer.post-processor.docker-push": "docker",
}
type Config struct {
common.PackerConfig `mapstructure:",squash"`
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"`
ctx interpolate.Context
}
type PostProcessor struct {
configs map[string]*Config
}
func (p *PostProcessor) ConfigSpec() hcldec.ObjectSpec {
panic("not implemented yet")
// return p.config.FlatMapstructure().HCL2Spec()
}
func (p *PostProcessor) Configure(raws ...interface{}) error {
p.configs = make(map[string]*Config)
p.configs[""] = new(Config)
if err := p.configureSingle(p.configs[""], raws...); err != nil {
return err
}
// Go over any of the provider-specific overrides and load those up.
for name, override := range p.configs[""].Override {
subRaws := make([]interface{}, len(raws)+1)
copy(subRaws, raws)
subRaws[len(raws)] = override
config := new(Config)
p.configs[name] = config
if err := p.configureSingle(config, subRaws...); err != nil {
return fmt.Errorf("Error configuring %s: %s", name, err)
}
}
return nil
}
func (p *PostProcessor) PostProcessProvider(name string, provider Provider, ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) {
config := p.configs[""]
if specificConfig, ok := p.configs[name]; ok {
config = specificConfig
}
ui.Say(fmt.Sprintf("Creating Vagrant box for '%s' provider", name))
config.ctx.Data = &outputPathTemplate{
ArtifactId: artifact.Id(),
BuildName: config.PackerBuildName,
Provider: name,
}
outputPath, err := interpolate.Render(config.OutputPath, &config.ctx)
if err != nil {
return nil, false, err
}
// Create a temporary directory for us to build the contents of the box in
dir, err := tmp.Dir("packer")
if err != nil {
return nil, false, err
}
defer os.RemoveAll(dir)
// Copy all of the includes files into the temporary directory
for _, src := range config.Include {
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)
return nil, false, err
}
}
// 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
}
// Write our Vagrantfile
var customVagrantfile string
if config.VagrantfileTemplate != "" {
ui.Message(fmt.Sprintf("Using custom Vagrantfile: %s", config.VagrantfileTemplate))
customBytes, err := ioutil.ReadFile(config.VagrantfileTemplate)
if err != nil {
return nil, false, err
}
customVagrantfile = string(customBytes)
}
f, err := os.Create(filepath.Join(dir, "Vagrantfile"))
if err != nil {
return nil, false, err
}
t := template.Must(template.New("root").Parse(boxVagrantfileContents))
err = t.Execute(f, &vagrantfileTemplate{
ProviderVagrantfile: vagrantfile,
CustomVagrantfile: customVagrantfile,
})
f.Close()
if err != nil {
return nil, false, err
}
// Create the box
if err := DirToBox(outputPath, dir, ui, config.CompressionLevel); err != nil {
return nil, false, err
}
return NewArtifact(name, outputPath), provider.KeepInputArtifact(), nil
}
func (p *PostProcessor) PostProcess(ctx context.Context, ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, bool, error) {
name, ok := builtins[artifact.BuilderId()]
if !ok {
return nil, false, false, fmt.Errorf(
"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))
}
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
}
func (p *PostProcessor) configureSingle(c *Config, raws ...interface{}) error {
var md mapstructure.Metadata
err := config.Decode(c, &config.DecodeOpts{
Metadata: &md,
Interpolate: true,
InterpolateContext: &c.ctx,
InterpolateFilter: &interpolate.RenderFilter{
Exclude: []string{
"output",
},
},
}, raws...)
if err != nil {
return err
}
// Defaults
if c.OutputPath == "" {
c.OutputPath = "packer_{{ .BuildName }}_{{.Provider}}.box"
}
found := false
for _, k := range md.Keys {
if k == "compression_level" {
found = true
break
}
}
if !found {
c.CompressionLevel = flate.DefaultCompression
}
var errs *packer.MultiError
if c.VagrantfileTemplate != "" && c.VagrantfileTemplateGenerated == false {
_, err := os.Stat(c.VagrantfileTemplate)
if err != nil {
errs = packer.MultiErrorAppend(errs, fmt.Errorf(
"vagrantfile_template '%s' does not exist", c.VagrantfileTemplate))
}
}
if errs != nil && len(errs.Errors) > 0 {
return errs
}
return nil
}
func providerForName(name string) Provider {
switch name {
case "aws":
return new(AWSProvider)
case "scaleway":
return new(ScalewayProvider)
case "digitalocean":
return new(DigitalOceanProvider)
case "virtualbox":
return new(VBoxProvider)
case "vmware":
return new(VMwareProvider)
case "parallels":
return new(ParallelsProvider)
case "hyperv":
return new(HypervProvider)
case "libvirt":
return new(LibVirtProvider)
case "google":
return new(GoogleProvider)
case "lxc":
return new(LXCProvider)
case "azure":
return new(AzureProvider)
case "docker":
return new(DockerProvider)
default:
return nil
}
}
// OutputPathTemplate is the structure that is available within the
// OutputPath variables.
type outputPathTemplate struct {
ArtifactId string
BuildName string
Provider string
}
type vagrantfileTemplate struct {
ProviderVagrantfile string
CustomVagrantfile string
}
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 }}
`