make it work with a local vagrant box

This commit is contained in:
Megan Marsh 2019-01-24 16:56:57 -08:00
parent ad21367b21
commit 52892699ca
3 changed files with 102 additions and 52 deletions

View File

@ -37,10 +37,13 @@ type Config struct {
// This is the name of the new virtual machine.
// By default this is "packer-BUILDNAME", where "BUILDNAME" is the name of the build.
OutputDir string `mapstructure:"output_dir"`
SourceBox string `mapstructure:"source_box"`
SourceBoxName string `mapstructure:"source_box_name"`
Provider string `mapstructure:"provider"`
OutputDir string `mapstructure:"output_dir"`
SourceBox string `mapstructure:"source_box"`
Checksum string `mapstructure:"checksum"`
ChecksumType string `mapstructure:"checksum_type"`
BoxName string `mapstructure:"box_name"`
Provider string `mapstructure:"provider"`
Communicator string `mapstructure:"communicator"`
@ -104,6 +107,26 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
errs = packer.MultiErrorAppend(errs,
fmt.Errorf(`The Vagrant builder currently only supports the ssh communicator"`))
}
// The box isn't a namespace like you'd pull from vagrant cloud
if b.config.BoxName == "" {
b.config.BoxName = fmt.Sprintf("packer_%s", b.config.PackerBuildName)
}
if b.config.SourceBox == "" {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("source_path is required"))
} else {
if !strings.HasSuffix(b.config.SourceBox, ".box") {
b.config.SourceBox, err = common.ValidatedURL(b.config.SourceBox)
if err != nil {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("source_path is invalid: %s", err))
}
fileOK := common.FileExistsLocally(b.config.SourceBox)
if !fileOK {
packer.MultiErrorAppend(errs,
fmt.Errorf("Source file '%s' needs to exist at time of config validation!", b.config.SourceBox))
}
}
}
if b.config.TeardownMethod == "" {
b.config.TeardownMethod = "destroy"
@ -147,21 +170,30 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
// Build the steps.
steps := []multistep.Step{}
if !b.config.SkipPackage {
steps = append(steps,
&common.StepOutputDir{
Force: b.config.PackerForce,
Path: b.config.OutputDir,
})
// Download if source box isn't from vagrant cloud.
if !strings.HasSuffix(b.config.SourceBox, ".box") {
steps = append(steps, &common.StepDownload{
Checksum: b.config.Checksum,
ChecksumType: b.config.ChecksumType,
Description: "Box",
Extension: "box",
ResultKey: "box_path",
Url: []string{b.config.SourceBox},
})
}
steps = append(steps,
&common.StepOutputDir{
Force: b.config.PackerForce,
Path: b.config.OutputDir,
},
&StepInitializeVagrant{
BoxName: b.config.SourceBoxName,
BoxVersion: b.config.BoxVersion,
Minimal: b.config.Minimal,
Template: b.config.Template,
SourceBox: b.config.SourceBox,
OutputDir: b.config.OutputDir,
BoxName: b.config.BoxName,
},
&StepAddBox{
BoxVersion: b.config.BoxVersion,
@ -173,7 +205,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
Insecure: b.config.AddInsecure,
Provider: b.config.Provider,
SourceBox: b.config.SourceBox,
BoxName: b.config.SourceBoxName,
BoxName: b.config.BoxName,
},
&StepUp{
b.config.TeardownMethod,

View File

@ -2,6 +2,7 @@ package vagrant
import (
"context"
"fmt"
"log"
"strings"
@ -25,12 +26,17 @@ type StepAddBox struct {
func (s *StepAddBox) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
driver := state.Get("driver").(VagrantDriver)
ui := state.Get("ui").(packer.Ui)
config := state.Get("config").(*Config)
ui.Say("Adding box using vagrant box add..")
addArgs := []string{}
if strings.HasSuffix(s.SourceBox, ".box") {
// The box isn't a namespace like you'd pull from vagrant cloud
if s.BoxName == "" {
s.BoxName = fmt.Sprintf("packer_%s", config.PackerBuildName)
}
addArgs = append(addArgs, s.BoxName)
}

View File

@ -9,58 +9,70 @@ builder that you have already installed what you need.
Required:
`source_box` (string) - URL of the vagrant box to use, or the name of the
vagrant box. For example, `hashicorp/precise64` or
`https://boxes.company.com/my-company.box` are valid source boxes. If using a URL like the latter example above, you will also need to provide a `box_name`.
- `source_box` (string) - URL of the vagrant box to use, or the name of the
vagrant box. `hashicorp/precise64`, `./mylocalbox.box` and
`https://boxes.company.com/my-company.box` are all valid source boxes. If your
source is a .box file, whether locally or from a URL like the latter example
above, you will also need to provide a `box_name`.
Optional:
`output_dir` (string) - The directory to create that will contain
your output box. We always create this directory and run from inside of it to
prevent Vagrant init collisions. If unset, it will be set to `packer-` plus
your buildname.
- `output_dir` (string) - The directory to create that will contain
your output box. We always create this directory and run from inside of it to
prevent Vagrant init collisions. If unset, it will be set to `packer-` plus
your buildname.
`box_name` (string) - if your source\_box is a boxfile that we need to add to
Vagrant, this is the name to give it.
- `box_name` (string) - if your source\_box is a boxfile that we need to add
to Vagrant, this is the name to give it. If left blank, will default to
"packer_" plus your buildname.
`vagrantfile_template` (string) - a path to an ERB template to use for the
vagrantfile when calling `vagrant init`. See the blog post
[here](https://www.hashicorp.com/blog/hashicorp-vagrant-2-0-2#customized-vagrantfile-templates)
for some more details on how this works. Available variables are `box_name`,
`box_url`, and `box_version`.
- `checksum` (string) - The checksum for the .box file. The type of the
checksum is specified with `checksum_type`, documented below.
`teardown_method` (string) - Whether to halt, suspend, or destroy the box when
the build has completed. Defaults to "halt"
- `checksum_type` (string) - The type of the checksum specified in `checksum`.
Valid values are `none`, `md5`, `sha1`, `sha256`, or `sha512`. Although the
checksum will not be verified when `checksum_type` is set to "none", this is
not recommended since OVA files can be very large and corruption does happen
from time to time.
`box_version` (string) - What box version to use when initializing Vagrant.
- `vagrantfile_template` (string) - a path to an ERB template to use for the
vagrantfile when calling `vagrant init`. See the blog post
[here](https://www.hashicorp.com/blog/hashicorp-vagrant-2-0-2#customized-vagrantfile-templates)
for some more details on how this works. Available variables are `box_name`,
`box_url`, and `box_version`.
`init_minimal` (bool) - If true, will add the --minimal flag to the Vagrant
init command, creating a minimal vagrantfile instead of one filled with helpful
comments.
- `teardown_method` (string) - Whether to halt, suspend, or destroy the box when
the build has completed. Defaults to "halt"
`add_cacert` (string) - Equivalent to setting the
[`--cacert`](https://www.vagrantup.com/docs/cli/box.html#cacert-certfile)
option in `vagrant add`; defaults to unset.
- `box_version` (string) - What box version to use when initializing Vagrant.
`add_capath` (string) - Equivalent to setting the
[`--capath`](https://www.vagrantup.com/docs/cli/box.html#capath-certdir) option
in `vagrant add`; defaults to unset.
- `init_minimal` (bool) - If true, will add the --minimal flag to the Vagrant
init command, creating a minimal vagrantfile instead of one filled with helpful
comments.
`add_cert` (string) - Equivalent to setting the
[`--cert`](https://www.vagrantup.com/docs/cli/box.html#cert-certfile) option in
`vagrant add`; defaults to unset.
- `add_cacert` (string) - Equivalent to setting the
[`--cacert`](https://www.vagrantup.com/docs/cli/box.html#cacert-certfile)
option in `vagrant add`; defaults to unset.
`add_clean` (bool) - Equivalent to setting the
[`--clean`](https://www.vagrantup.com/docs/cli/box.html#clean) flag in
`vagrant add`; defaults to unset.
- `add_capath` (string) - Equivalent to setting the
[`--capath`](https://www.vagrantup.com/docs/cli/box.html#capath-certdir) option
in `vagrant add`; defaults to unset.
`add_force` (bool) - Equivalent to setting the
[`--force`](https://www.vagrantup.com/docs/cli/box.html#force) flag in
`vagrant add`; defaults to unset.
- `add_cert` (string) - Equivalent to setting the
[`--cert`](https://www.vagrantup.com/docs/cli/box.html#cert-certfile) option in
`vagrant add`; defaults to unset.
`add_insecure` (bool) - Equivalent to setting the
[`--force`](https://www.vagrantup.com/docs/cli/box.html#insecure) flag in
`vagrant add`; defaults to unset.
- `add_clean` (bool) - Equivalent to setting the
[`--clean`](https://www.vagrantup.com/docs/cli/box.html#clean) flag in
`vagrant add`; defaults to unset.
`skip_package` (bool) - if true, Packer will not call `vagrant package` to
package your base box into its own standalone .box file.
- `add_force` (bool) - Equivalent to setting the
[`--force`](https://www.vagrantup.com/docs/cli/box.html#force) flag in
`vagrant add`; defaults to unset.
- `add_insecure` (bool) - Equivalent to setting the
[`--force`](https://www.vagrantup.com/docs/cli/box.html#insecure) flag in
`vagrant add`; defaults to unset.
- `skip_package` (bool) - if true, Packer will not call `vagrant package` to
package your base box into its own standalone .box file.