2019-05-31 08:27:41 -04:00
|
|
|
//go:generate struct-markdown
|
2019-10-14 10:43:59 -04:00
|
|
|
//go:generate mapstructure-to-hcl2 -type Config
|
2019-05-31 08:27:41 -04:00
|
|
|
|
2019-02-08 12:31:30 -05:00
|
|
|
package ovf
|
2013-12-21 20:38:06 -05:00
|
|
|
|
|
|
|
import (
|
2013-12-22 18:24:24 -05:00
|
|
|
"fmt"
|
2014-05-04 11:56:57 -04:00
|
|
|
"strings"
|
2013-12-22 20:02:22 -05:00
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
vboxcommon "github.com/hashicorp/packer/builder/virtualbox/common"
|
|
|
|
"github.com/hashicorp/packer/common"
|
2018-04-18 19:19:44 -04:00
|
|
|
"github.com/hashicorp/packer/common/bootcommand"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/helper/config"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
2013-12-21 20:38:06 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// Config is the configuration structure for the builder.
|
|
|
|
type Config struct {
|
2014-03-12 19:14:44 -04:00
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
2015-11-01 17:29:24 -05:00
|
|
|
common.HTTPConfig `mapstructure:",squash"`
|
2016-07-26 15:42:04 -04:00
|
|
|
common.FloppyConfig `mapstructure:",squash"`
|
2018-04-18 19:19:44 -04:00
|
|
|
bootcommand.BootConfig `mapstructure:",squash"`
|
2014-03-12 19:14:44 -04:00
|
|
|
vboxcommon.ExportConfig `mapstructure:",squash"`
|
|
|
|
vboxcommon.OutputConfig `mapstructure:",squash"`
|
|
|
|
vboxcommon.RunConfig `mapstructure:",squash"`
|
2020-01-09 11:37:30 -05:00
|
|
|
vboxcommon.CommConfig `mapstructure:",squash"`
|
2014-03-12 19:14:44 -04:00
|
|
|
vboxcommon.ShutdownConfig `mapstructure:",squash"`
|
|
|
|
vboxcommon.VBoxManageConfig `mapstructure:",squash"`
|
|
|
|
vboxcommon.VBoxVersionConfig `mapstructure:",squash"`
|
2019-03-16 12:15:27 -04:00
|
|
|
vboxcommon.GuestAdditionsConfig `mapstructure:",squash"`
|
2019-06-06 10:29:25 -04:00
|
|
|
// The checksum for the source_path file. The
|
|
|
|
// algorithm to use when computing the checksum can be optionally specified
|
|
|
|
// with checksum_type. When checksum_type is not set packer will guess the
|
|
|
|
// checksumming type based on checksum length. checksum can be also be a
|
|
|
|
// file or an URL, in which case checksum_type must be set to file; the
|
|
|
|
// go-getter will download it and use the first hash found.
|
|
|
|
Checksum string `mapstructure:"checksum" required:"true"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The type of the checksum specified in checksum.
|
2019-06-06 10:29:25 -04:00
|
|
|
// 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.
|
|
|
|
ChecksumType string `mapstructure:"checksum_type" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The method by which guest additions are
|
2019-06-06 10:29:25 -04:00
|
|
|
// made available to the guest for installation. Valid options are upload,
|
|
|
|
// attach, or disable. If the mode is attach the guest additions ISO will
|
|
|
|
// be attached as a CD device to the virtual machine. If the mode is upload
|
|
|
|
// the guest additions ISO will be uploaded to the path specified by
|
|
|
|
// guest_additions_path. The default value is upload. If disable is used,
|
|
|
|
// guest additions won't be downloaded, either.
|
|
|
|
GuestAdditionsMode string `mapstructure:"guest_additions_mode" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The path on the guest virtual machine
|
2019-06-06 10:29:25 -04:00
|
|
|
// where the VirtualBox guest additions ISO will be uploaded. By default this
|
|
|
|
// is VBoxGuestAdditions.iso which should upload into the login directory of
|
|
|
|
// the user. This is a configuration
|
|
|
|
// template where the Version
|
|
|
|
// variable is replaced with the VirtualBox version.
|
|
|
|
GuestAdditionsPath string `mapstructure:"guest_additions_path" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The interface type to use to mount
|
2019-06-06 10:29:25 -04:00
|
|
|
// guest additions when guest_additions_mode is set to attach. Will
|
|
|
|
// default to the value set in iso_interface, if iso_interface is set.
|
|
|
|
// Will default to "ide", if iso_interface is not set. Options are "ide" and
|
|
|
|
// "sata".
|
|
|
|
GuestAdditionsInterface string `mapstructure:"guest_additions_interface" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The SHA256 checksum of the guest
|
2019-06-06 10:29:25 -04:00
|
|
|
// additions ISO that will be uploaded to the guest VM. By default the
|
|
|
|
// checksums will be downloaded from the VirtualBox website, so this only needs
|
|
|
|
// to be set if you want to be explicit about the checksum.
|
|
|
|
GuestAdditionsSHA256 string `mapstructure:"guest_additions_sha256" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The URL to the guest additions ISO
|
2019-06-06 10:29:25 -04:00
|
|
|
// to upload. This can also be a file URL if the ISO is at a local path. By
|
|
|
|
// default, the VirtualBox builder will attempt to find the guest additions ISO
|
|
|
|
// on the local file system. If it is not available locally, the builder will
|
|
|
|
// download the proper guest additions ISO from the internet.
|
|
|
|
GuestAdditionsURL string `mapstructure:"guest_additions_url" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// Additional flags to pass to
|
2019-06-06 10:29:25 -04:00
|
|
|
// VBoxManage import. This can be used to add additional command-line flags
|
|
|
|
// such as --eula-accept to accept a EULA in the OVF.
|
|
|
|
ImportFlags []string `mapstructure:"import_flags" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// Additional options to pass to the
|
2019-06-06 10:29:25 -04:00
|
|
|
// VBoxManage import. This can be useful for passing keepallmacs or
|
|
|
|
// keepnatmacs options for existing ovf images.
|
|
|
|
ImportOpts string `mapstructure:"import_opts" required:"false"`
|
2019-12-16 10:50:21 -05:00
|
|
|
// The filepath or URL to an OVF or OVA file that acts as the
|
|
|
|
// source of this build.
|
2019-06-06 10:29:25 -04:00
|
|
|
SourcePath string `mapstructure:"source_path" required:"true"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The path where the OVA should be saved
|
2019-06-06 10:29:25 -04:00
|
|
|
// after download. By default, it will go in the packer cache, with a hash of
|
|
|
|
// the original filename as its name.
|
|
|
|
TargetPath string `mapstructure:"target_path" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// This is the name of the OVF file for the new virtual
|
2019-06-06 10:29:25 -04:00
|
|
|
// machine, without the file extension. By default this is packer-BUILDNAME,
|
|
|
|
// where "BUILDNAME" is the name of the build.
|
|
|
|
VMName string `mapstructure:"vm_name" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// Set this to true if you would like to keep
|
2019-06-06 10:29:25 -04:00
|
|
|
// the VM registered with virtualbox. Defaults to false.
|
|
|
|
KeepRegistered bool `mapstructure:"keep_registered" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// Defaults to false. When enabled, Packer will
|
2019-06-06 10:29:25 -04:00
|
|
|
// not export the VM. Useful if the build output is not the resultant image,
|
|
|
|
// but created inside the VM.
|
|
|
|
SkipExport bool `mapstructure:"skip_export" required:"false"`
|
2013-12-22 18:19:19 -05:00
|
|
|
|
2015-05-27 17:03:56 -04:00
|
|
|
ctx interpolate.Context
|
2013-12-21 20:38:06 -05:00
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
func (c *Config) Prepare(raws ...interface{}) ([]string, error) {
|
2015-06-12 17:02:09 -04:00
|
|
|
err := config.Decode(c, &config.DecodeOpts{
|
2015-06-22 12:22:42 -04:00
|
|
|
Interpolate: true,
|
|
|
|
InterpolateContext: &c.ctx,
|
2015-05-27 17:03:56 -04:00
|
|
|
InterpolateFilter: &interpolate.RenderFilter{
|
|
|
|
Exclude: []string{
|
|
|
|
"boot_command",
|
|
|
|
"guest_additions_path",
|
|
|
|
"guest_additions_url",
|
|
|
|
"vboxmanage",
|
|
|
|
"vboxmanage_post",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, raws...)
|
2013-12-21 20:38:06 -05:00
|
|
|
if err != nil {
|
2019-12-17 05:25:56 -05:00
|
|
|
return nil, err
|
2013-12-21 20:38:06 -05:00
|
|
|
}
|
|
|
|
|
2013-12-22 18:24:24 -05:00
|
|
|
// Defaults
|
2014-05-04 11:56:57 -04:00
|
|
|
if c.GuestAdditionsMode == "" {
|
|
|
|
c.GuestAdditionsMode = "upload"
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.GuestAdditionsPath == "" {
|
|
|
|
c.GuestAdditionsPath = "VBoxGuestAdditions.iso"
|
|
|
|
}
|
2019-02-08 12:31:30 -05:00
|
|
|
if c.GuestAdditionsInterface == "" {
|
|
|
|
c.GuestAdditionsInterface = "ide"
|
2019-02-08 12:28:10 -05:00
|
|
|
}
|
2015-06-22 12:25:15 -04:00
|
|
|
|
2013-12-22 18:24:24 -05:00
|
|
|
if c.VMName == "" {
|
2015-06-22 12:25:15 -04:00
|
|
|
c.VMName = fmt.Sprintf(
|
|
|
|
"packer-%s-%d", c.PackerBuildName, interpolate.InitTime.Unix())
|
2013-12-22 18:24:24 -05:00
|
|
|
}
|
|
|
|
|
2013-12-21 20:38:06 -05:00
|
|
|
// Prepare the errors
|
2015-05-27 17:03:56 -04:00
|
|
|
var errs *packer.MultiError
|
|
|
|
errs = packer.MultiErrorAppend(errs, c.ExportConfig.Prepare(&c.ctx)...)
|
2019-06-19 10:34:14 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, c.ExportConfig.Prepare(&c.ctx)...)
|
2015-05-27 17:03:56 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, c.FloppyConfig.Prepare(&c.ctx)...)
|
2015-11-01 17:29:24 -05:00
|
|
|
errs = packer.MultiErrorAppend(errs, c.HTTPConfig.Prepare(&c.ctx)...)
|
2015-05-27 17:03:56 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, c.OutputConfig.Prepare(&c.ctx, &c.PackerConfig)...)
|
|
|
|
errs = packer.MultiErrorAppend(errs, c.RunConfig.Prepare(&c.ctx)...)
|
|
|
|
errs = packer.MultiErrorAppend(errs, c.ShutdownConfig.Prepare(&c.ctx)...)
|
2020-01-09 11:37:30 -05:00
|
|
|
errs = packer.MultiErrorAppend(errs, c.CommConfig.Prepare(&c.ctx)...)
|
2015-05-27 17:03:56 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, c.VBoxManageConfig.Prepare(&c.ctx)...)
|
|
|
|
errs = packer.MultiErrorAppend(errs, c.VBoxVersionConfig.Prepare(&c.ctx)...)
|
2018-04-18 19:19:44 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, c.BootConfig.Prepare(&c.ctx)...)
|
2019-03-16 12:15:27 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, c.GuestAdditionsConfig.Prepare(&c.ctx)...)
|
2014-09-05 13:23:37 -04:00
|
|
|
|
2016-10-10 22:16:25 -04:00
|
|
|
c.ChecksumType = strings.ToLower(c.ChecksumType)
|
|
|
|
c.Checksum = strings.ToLower(c.Checksum)
|
|
|
|
|
2013-12-22 20:02:22 -05:00
|
|
|
if c.SourcePath == "" {
|
|
|
|
errs = packer.MultiErrorAppend(errs, fmt.Errorf("source_path is required"))
|
Use the hashicorp/go-getter to download files
* removed packer.Cache and references since packer.Cache is never used except in the download step. The download step now uses the new func packer.CachePath(targetPath) for this, the behavior is the same.
* removed download code from packer that was reimplemented into the go-getter library: progress bar, http download restart, checksuming from file, skip already downloaded files, symlinking, make a download cancellable by context.
* on windows if packer is running without symlinking rights and we are getting a local file, the file will be copied instead to avoid errors.
* added unit tests for step_download that are now CI tested on windows, mac & linux.
* files are now downloaded under cache dir `sha1(filename + "?checksum=" + checksum) + file_extension`
* since the output dir is based on the source url and the checksum, when the checksum fails, the file is auto deleted.
* a download file is protected and locked by a file lock,
* updated docs
* updated go modules and vendors
2019-03-13 07:11:58 -04:00
|
|
|
}
|
2018-01-03 19:53:47 -05:00
|
|
|
|
2014-05-04 11:56:57 -04:00
|
|
|
validMode := false
|
|
|
|
validModes := []string{
|
|
|
|
vboxcommon.GuestAdditionsModeDisable,
|
|
|
|
vboxcommon.GuestAdditionsModeAttach,
|
|
|
|
vboxcommon.GuestAdditionsModeUpload,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, mode := range validModes {
|
|
|
|
if c.GuestAdditionsMode == mode {
|
|
|
|
validMode = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !validMode {
|
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
fmt.Errorf("guest_additions_mode is invalid. Must be one of: %v", validModes))
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.GuestAdditionsSHA256 != "" {
|
|
|
|
c.GuestAdditionsSHA256 = strings.ToLower(c.GuestAdditionsSHA256)
|
|
|
|
}
|
|
|
|
|
2013-12-22 12:37:27 -05:00
|
|
|
// Warnings
|
|
|
|
var warnings []string
|
|
|
|
if c.ShutdownCommand == "" {
|
|
|
|
warnings = append(warnings,
|
|
|
|
"A shutdown_command was not specified. Without a shutdown command, Packer\n"+
|
|
|
|
"will forcibly halt the virtual machine, which may result in data loss.")
|
|
|
|
}
|
|
|
|
|
2013-12-21 20:38:06 -05:00
|
|
|
// Check for any errors.
|
|
|
|
if errs != nil && len(errs.Errors) > 0 {
|
2019-12-17 05:25:56 -05:00
|
|
|
return warnings, errs
|
2013-12-21 20:38:06 -05:00
|
|
|
}
|
|
|
|
|
2014-09-05 13:23:37 -04:00
|
|
|
// TODO: Write a packer fix and just remove import_opts
|
|
|
|
if c.ImportOpts != "" {
|
|
|
|
c.ImportFlags = append(c.ImportFlags, "--options", c.ImportOpts)
|
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
return warnings, nil
|
2013-12-21 20:38:06 -05:00
|
|
|
}
|