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

@ -39,7 +39,10 @@ type Config struct {
// By default this is "packer-BUILDNAME", where "BUILDNAME" is the name of the build. // By default this is "packer-BUILDNAME", where "BUILDNAME" is the name of the build.
OutputDir string `mapstructure:"output_dir"` OutputDir string `mapstructure:"output_dir"`
SourceBox string `mapstructure:"source_box"` SourceBox string `mapstructure:"source_box"`
SourceBoxName string `mapstructure:"source_box_name"` Checksum string `mapstructure:"checksum"`
ChecksumType string `mapstructure:"checksum_type"`
BoxName string `mapstructure:"box_name"`
Provider string `mapstructure:"provider"` Provider string `mapstructure:"provider"`
Communicator string `mapstructure:"communicator"` Communicator string `mapstructure:"communicator"`
@ -104,6 +107,26 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
errs = packer.MultiErrorAppend(errs, errs = packer.MultiErrorAppend(errs,
fmt.Errorf(`The Vagrant builder currently only supports the ssh communicator"`)) 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 == "" { if b.config.TeardownMethod == "" {
b.config.TeardownMethod = "destroy" 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. // Build the steps.
steps := []multistep.Step{} steps := []multistep.Step{}
if !b.config.SkipPackage { // 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, steps = append(steps,
&common.StepOutputDir{ &common.StepOutputDir{
Force: b.config.PackerForce, Force: b.config.PackerForce,
Path: b.config.OutputDir, Path: b.config.OutputDir,
}) },
}
steps = append(steps,
&StepInitializeVagrant{ &StepInitializeVagrant{
BoxName: b.config.SourceBoxName,
BoxVersion: b.config.BoxVersion, BoxVersion: b.config.BoxVersion,
Minimal: b.config.Minimal, Minimal: b.config.Minimal,
Template: b.config.Template, Template: b.config.Template,
SourceBox: b.config.SourceBox, SourceBox: b.config.SourceBox,
OutputDir: b.config.OutputDir, OutputDir: b.config.OutputDir,
BoxName: b.config.BoxName,
}, },
&StepAddBox{ &StepAddBox{
BoxVersion: b.config.BoxVersion, 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, Insecure: b.config.AddInsecure,
Provider: b.config.Provider, Provider: b.config.Provider,
SourceBox: b.config.SourceBox, SourceBox: b.config.SourceBox,
BoxName: b.config.SourceBoxName, BoxName: b.config.BoxName,
}, },
&StepUp{ &StepUp{
b.config.TeardownMethod, b.config.TeardownMethod,

View File

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

View File

@ -9,58 +9,70 @@ builder that you have already installed what you need.
Required: Required:
`source_box` (string) - URL of the vagrant box to use, or the name of the - `source_box` (string) - URL of the vagrant box to use, or the name of the
vagrant box. For example, `hashicorp/precise64` or vagrant box. `hashicorp/precise64`, `./mylocalbox.box` and
`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`. `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: Optional:
`output_dir` (string) - The directory to create that will contain - `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 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 prevent Vagrant init collisions. If unset, it will be set to `packer-` plus
your buildname. your buildname.
`box_name` (string) - if your source\_box is a boxfile that we need to add to - `box_name` (string) - if your source\_box is a boxfile that we need to add
Vagrant, this is the name to give it. 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 - `checksum` (string) - The checksum for the .box file. The type of the
checksum is specified with `checksum_type`, documented below.
- `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.
- `vagrantfile_template` (string) - a path to an ERB template to use for the
vagrantfile when calling `vagrant init`. See the blog post vagrantfile when calling `vagrant init`. See the blog post
[here](https://www.hashicorp.com/blog/hashicorp-vagrant-2-0-2#customized-vagrantfile-templates) [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`, for some more details on how this works. Available variables are `box_name`,
`box_url`, and `box_version`. `box_url`, and `box_version`.
`teardown_method` (string) - Whether to halt, suspend, or destroy the box when - `teardown_method` (string) - Whether to halt, suspend, or destroy the box when
the build has completed. Defaults to "halt" the build has completed. Defaults to "halt"
`box_version` (string) - What box version to use when initializing Vagrant. - `box_version` (string) - What box version to use when initializing Vagrant.
`init_minimal` (bool) - If true, will add the --minimal flag to the Vagrant - `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 init command, creating a minimal vagrantfile instead of one filled with helpful
comments. comments.
`add_cacert` (string) - Equivalent to setting the - `add_cacert` (string) - Equivalent to setting the
[`--cacert`](https://www.vagrantup.com/docs/cli/box.html#cacert-certfile) [`--cacert`](https://www.vagrantup.com/docs/cli/box.html#cacert-certfile)
option in `vagrant add`; defaults to unset. option in `vagrant add`; defaults to unset.
`add_capath` (string) - Equivalent to setting the - `add_capath` (string) - Equivalent to setting the
[`--capath`](https://www.vagrantup.com/docs/cli/box.html#capath-certdir) option [`--capath`](https://www.vagrantup.com/docs/cli/box.html#capath-certdir) option
in `vagrant add`; defaults to unset. in `vagrant add`; defaults to unset.
`add_cert` (string) - Equivalent to setting the - `add_cert` (string) - Equivalent to setting the
[`--cert`](https://www.vagrantup.com/docs/cli/box.html#cert-certfile) option in [`--cert`](https://www.vagrantup.com/docs/cli/box.html#cert-certfile) option in
`vagrant add`; defaults to unset. `vagrant add`; defaults to unset.
`add_clean` (bool) - Equivalent to setting the - `add_clean` (bool) - Equivalent to setting the
[`--clean`](https://www.vagrantup.com/docs/cli/box.html#clean) flag in [`--clean`](https://www.vagrantup.com/docs/cli/box.html#clean) flag in
`vagrant add`; defaults to unset. `vagrant add`; defaults to unset.
`add_force` (bool) - Equivalent to setting the - `add_force` (bool) - Equivalent to setting the
[`--force`](https://www.vagrantup.com/docs/cli/box.html#force) flag in [`--force`](https://www.vagrantup.com/docs/cli/box.html#force) flag in
`vagrant add`; defaults to unset. `vagrant add`; defaults to unset.
`add_insecure` (bool) - Equivalent to setting the - `add_insecure` (bool) - Equivalent to setting the
[`--force`](https://www.vagrantup.com/docs/cli/box.html#insecure) flag in [`--force`](https://www.vagrantup.com/docs/cli/box.html#insecure) flag in
`vagrant add`; defaults to unset. `vagrant add`; defaults to unset.
`skip_package` (bool) - if true, Packer will not call `vagrant package` to - `skip_package` (bool) - if true, Packer will not call `vagrant package` to
package your base box into its own standalone .box file. package your base box into its own standalone .box file.