builder/vmware: Start it. Creates disks so far...

This commit is contained in:
Mitchell Hashimoto 2013-06-04 15:00:58 -07:00
parent 87e7f17aa7
commit 2e57496a82
5 changed files with 111 additions and 0 deletions

47
builder/vmware/builder.go Normal file
View File

@ -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() {
}

View File

@ -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{}) {}

View File

@ -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{}) {}

View File

@ -13,6 +13,7 @@ import (
const defaultConfig = ` const defaultConfig = `
[builders] [builders]
amazon-ebs = "packer-builder-amazon-ebs" amazon-ebs = "packer-builder-amazon-ebs"
vmware = "packer-builder-vmware"
[commands] [commands]
build = "packer-command-build" build = "packer-command-build"

View File

@ -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))
}