From 2e57496a821a17fe0de72fb1e98cb35f604b8963 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 4 Jun 2013 15:00:58 -0700 Subject: [PATCH] builder/vmware: Start it. Creates disks so far... --- builder/vmware/builder.go | 47 +++++++++++++++++++++++ builder/vmware/step_create_disk.go | 33 ++++++++++++++++ builder/vmware/step_prepare_output_dir.go | 20 ++++++++++ config.go | 1 + plugin/builder-vmware/main.go | 10 +++++ 5 files changed, 111 insertions(+) create mode 100644 builder/vmware/builder.go create mode 100644 builder/vmware/step_create_disk.go create mode 100644 builder/vmware/step_prepare_output_dir.go create mode 100644 plugin/builder-vmware/main.go diff --git a/builder/vmware/builder.go b/builder/vmware/builder.go new file mode 100644 index 000000000..9603eee8e --- /dev/null +++ b/builder/vmware/builder.go @@ -0,0 +1,47 @@ +package vmware + +import ( + "github.com/mitchellh/multistep" + "github.com/mitchellh/packer/packer" +) + +const BuilderId = "mitchellh.vmware" + +type Builder struct { + config config + runner multistep.Runner +} + +type config struct { + OutputDir string `mapstructure:"output_directory"` +} + +func (b *Builder) Prepare(raw interface{}) (err error) { + if b.config.OutputDir == "" { + b.config.OutputDir = "vmware" + } + + return nil +} + +func (b *Builder) Run(ui packer.Ui, hook packer.Hook) packer.Artifact { + steps := []multistep.Step{ + &stepPrepareOutputDir{}, + &stepCreateDisk{}, + } + + // Setup the state bag + state := make(map[string]interface{}) + state["config"] = &b.config + state["hook"] = hook + state["ui"] = ui + + // Run! + b.runner = &multistep.BasicRunner{Steps: steps} + b.runner.Run(state) + + return nil +} + +func (b *Builder) Cancel() { +} diff --git a/builder/vmware/step_create_disk.go b/builder/vmware/step_create_disk.go new file mode 100644 index 000000000..4187af6c6 --- /dev/null +++ b/builder/vmware/step_create_disk.go @@ -0,0 +1,33 @@ +package vmware + +import ( + "fmt" + "github.com/mitchellh/multistep" + "github.com/mitchellh/packer/packer" + "os/exec" + "path/filepath" +) + +type stepCreateDisk struct{} + +func (stepCreateDisk) Run(state map[string]interface{}) multistep.StepAction { + // TODO(mitchellh): Configurable disk size + // TODO(mitchellh): Capture error output in case things go wrong to report it + + config := state["config"].(*config) + ui := state["ui"].(packer.Ui) + + vdisk_manager := "/Applications/VMware Fusion.app/Contents/Library/vmware-vdiskmanager" + output := filepath.Join(config.OutputDir, "disk.vmdk") + + ui.Say("Creating virtual machine disk") + cmd := exec.Command(vdisk_manager, "-c", "-s", "40000M", "-a", "lsilogic", "-t", "1", output) + if err := cmd.Run(); err != nil { + ui.Error(fmt.Sprintf("Error creating VMware disk: %s", err)) + return multistep.ActionHalt + } + + return multistep.ActionContinue +} + +func (stepCreateDisk) Cleanup(map[string]interface{}) {} diff --git a/builder/vmware/step_prepare_output_dir.go b/builder/vmware/step_prepare_output_dir.go new file mode 100644 index 000000000..6068e5bb7 --- /dev/null +++ b/builder/vmware/step_prepare_output_dir.go @@ -0,0 +1,20 @@ +package vmware + +import ( + "github.com/mitchellh/multistep" + "os" +) + +type stepPrepareOutputDir struct{} + +func (stepPrepareOutputDir) Run(state map[string]interface{}) multistep.StepAction { + config := state["config"].(*config) + + if err := os.MkdirAll(config.OutputDir, 0755); err != nil { + return multistep.ActionHalt + } + + return multistep.ActionContinue +} + +func (stepPrepareOutputDir) Cleanup(map[string]interface{}) {} diff --git a/config.go b/config.go index 0b97ce09e..0e48c96eb 100644 --- a/config.go +++ b/config.go @@ -13,6 +13,7 @@ import ( const defaultConfig = ` [builders] amazon-ebs = "packer-builder-amazon-ebs" +vmware = "packer-builder-vmware" [commands] build = "packer-command-build" diff --git a/plugin/builder-vmware/main.go b/plugin/builder-vmware/main.go new file mode 100644 index 000000000..a449751b8 --- /dev/null +++ b/plugin/builder-vmware/main.go @@ -0,0 +1,10 @@ +package main + +import ( + "github.com/mitchellh/packer/builder/vmware" + "github.com/mitchellh/packer/packer/plugin" +) + +func main() { + plugin.ServeBuilder(new(vmware.Builder)) +}