2013-06-11 18:12:45 -04:00
|
|
|
package virtualbox
|
|
|
|
|
|
|
|
import (
|
2013-06-11 23:00:30 -04:00
|
|
|
"errors"
|
2013-06-11 18:45:52 -04:00
|
|
|
"fmt"
|
2013-06-11 18:12:45 -04:00
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
2013-06-11 18:45:52 -04:00
|
|
|
"log"
|
2013-06-11 23:00:30 -04:00
|
|
|
"net/url"
|
|
|
|
"os"
|
2013-06-11 18:47:10 -04:00
|
|
|
"os/exec"
|
2013-06-11 23:00:30 -04:00
|
|
|
"strings"
|
2013-06-11 18:12:45 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const BuilderId = "mitchellh.virtualbox"
|
|
|
|
|
|
|
|
type Builder struct {
|
|
|
|
config config
|
2013-06-11 18:45:52 -04:00
|
|
|
driver Driver
|
2013-06-11 18:12:45 -04:00
|
|
|
runner multistep.Runner
|
|
|
|
}
|
|
|
|
|
|
|
|
type config struct {
|
2013-06-11 18:57:20 -04:00
|
|
|
GuestOSType string `mapstructure:"guest_os_type"`
|
2013-06-11 23:00:30 -04:00
|
|
|
ISOMD5 string `mapstructure:"iso_md5"`
|
|
|
|
ISOUrl string `mapstructure:"iso_url"`
|
2013-06-11 18:12:45 -04:00
|
|
|
OutputDir string `mapstructure:"output_directory"`
|
2013-06-11 19:12:19 -04:00
|
|
|
VMName string `mapstructure:"vm_name"`
|
2013-06-11 18:12:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Builder) Prepare(raw interface{}) error {
|
2013-06-11 18:45:52 -04:00
|
|
|
var err error
|
2013-06-11 18:12:45 -04:00
|
|
|
if err := mapstructure.Decode(raw, &b.config); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-06-11 18:57:20 -04:00
|
|
|
if b.config.GuestOSType == "" {
|
|
|
|
b.config.GuestOSType = "Other"
|
|
|
|
}
|
|
|
|
|
2013-06-11 18:45:52 -04:00
|
|
|
if b.config.OutputDir == "" {
|
|
|
|
b.config.OutputDir = "virtualbox"
|
|
|
|
}
|
|
|
|
|
2013-06-11 19:12:19 -04:00
|
|
|
if b.config.VMName == "" {
|
|
|
|
b.config.VMName = "packer"
|
|
|
|
}
|
|
|
|
|
2013-06-11 18:45:52 -04:00
|
|
|
errs := make([]error, 0)
|
|
|
|
|
2013-06-11 23:00:30 -04:00
|
|
|
if b.config.ISOMD5 == "" {
|
|
|
|
errs = append(errs, errors.New("Due to large file sizes, an iso_md5 is required"))
|
|
|
|
} else {
|
|
|
|
b.config.ISOMD5 = strings.ToLower(b.config.ISOMD5)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.ISOUrl == "" {
|
|
|
|
errs = append(errs, errors.New("An iso_url must be specified."))
|
|
|
|
} else {
|
|
|
|
url, err := url.Parse(b.config.ISOUrl)
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, fmt.Errorf("iso_url is not a valid URL: %s", err))
|
|
|
|
} else {
|
|
|
|
if url.Scheme == "" {
|
|
|
|
url.Scheme = "file"
|
|
|
|
}
|
|
|
|
|
|
|
|
if url.Scheme == "file" {
|
|
|
|
if _, err := os.Stat(b.config.ISOUrl); err != nil {
|
|
|
|
errs = append(errs, fmt.Errorf("iso_url points to bad file: %s", err))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
supportedSchemes := []string{"file", "http", "https"}
|
|
|
|
scheme := strings.ToLower(url.Scheme)
|
|
|
|
|
|
|
|
found := false
|
|
|
|
for _, supported := range supportedSchemes {
|
|
|
|
if scheme == supported {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
errs = append(errs, fmt.Errorf("Unsupported URL scheme in iso_url: %s", scheme))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errs) == 0 {
|
|
|
|
// Put the URL back together since we may have modified it
|
|
|
|
b.config.ISOUrl = url.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-11 18:45:52 -04:00
|
|
|
b.driver, err = b.newDriver()
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, fmt.Errorf("Failed creating VirtualBox driver: %s", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errs) > 0 {
|
|
|
|
return &packer.MultiError{errs}
|
|
|
|
}
|
|
|
|
|
2013-06-11 18:12:45 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) packer.Artifact {
|
2013-06-11 18:45:52 -04:00
|
|
|
steps := []multistep.Step{
|
2013-06-11 23:00:30 -04:00
|
|
|
new(stepDownloadISO),
|
2013-06-11 18:45:52 -04:00
|
|
|
new(stepPrepareOutputDir),
|
|
|
|
new(stepSuppressMessages),
|
2013-06-11 19:12:19 -04:00
|
|
|
new(stepCreateVM),
|
2013-06-11 19:34:44 -04:00
|
|
|
new(stepCreateDisk),
|
2013-06-11 18:45:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Setup the state bag
|
|
|
|
state := make(map[string]interface{})
|
|
|
|
state["cache"] = cache
|
|
|
|
state["config"] = &b.config
|
|
|
|
state["driver"] = b.driver
|
|
|
|
state["hook"] = hook
|
|
|
|
state["ui"] = ui
|
|
|
|
|
|
|
|
// Run
|
|
|
|
b.runner = &multistep.BasicRunner{Steps: steps}
|
|
|
|
b.runner.Run(state)
|
|
|
|
|
2013-06-11 18:12:45 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Builder) Cancel() {
|
2013-06-11 18:45:52 -04:00
|
|
|
if b.runner != nil {
|
|
|
|
log.Println("Cancelling the step runner...")
|
|
|
|
b.runner.Cancel()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Builder) newDriver() (Driver, error) {
|
2013-06-11 18:47:10 -04:00
|
|
|
vboxmanagePath, err := exec.LookPath("VBoxManage")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2013-06-11 18:52:46 -04:00
|
|
|
log.Printf("VBoxManage path: %s", vboxmanagePath)
|
2013-06-11 18:45:52 -04:00
|
|
|
driver := &VBox42Driver{vboxmanagePath}
|
|
|
|
if err := driver.Verify(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return driver, nil
|
2013-06-11 18:12:45 -04:00
|
|
|
}
|