diff --git a/README.md b/README.md index 9ecd3d1..3ebad44 100644 --- a/README.md +++ b/README.md @@ -93,3 +93,6 @@ Add ```"remove_floppy": "true", "remove_ethernet": "true", "remove_optical_drive NOTE: This makes the ```"vm_network": "vmware_network_name"``` parameter optional. +### Specifying a Resource Pool when Uploading to Vsphere + +Add ```"resource_pool": "my_pool"``` to the post-processor config in your packer template. If not specified, the default Resource Pool will be used. diff --git a/post-processor.go b/post-processor.go index ba68bf4..506a8dd 100644 --- a/post-processor.go +++ b/post-processor.go @@ -4,6 +4,14 @@ import ( "bytes" "crypto/tls" "fmt" + "io/ioutil" + "net/http" + "net/url" + "os" + "os/exec" + "strings" + "time" + "github.com/cheggaaa/pb" vmwarecommon "github.com/hashicorp/packer/builder/vmware/common" "github.com/hashicorp/packer/common" @@ -14,13 +22,6 @@ import ( "github.com/vmware/govmomi/find" "github.com/vmware/govmomi/vim25/types" "golang.org/x/net/context" - "io/ioutil" - "net/http" - "net/url" - "os" - "os/exec" - "strings" - "time" ) var builtins = map[string]string{ @@ -42,6 +43,8 @@ type Config struct { RemoveFloppy string `mapstructure:"remove_floppy"` RemoveOpticalDrive string `mapstructure:"remove_optical_drive"` VirtualHardwareVer string `mapstructure:"virtual_hardware_version"` + ResourcePool string `mapstructure:"resource_pool"` + VMWareGuestOsType string `mapstructure:"vm_guest_os_type"` ctx interpolate.Context } @@ -129,7 +132,7 @@ func (p *PostProcessor) RemoveFloppy(vmx string, ui packer.Ui) error { } func (p *PostProcessor) RemoveEthernet(vmx string, ui packer.Ui) error { - ui.Message(fmt.Sprintf("Removing ethernet0 intercace from %s", vmx)) + ui.Message(fmt.Sprintf("Removing ethernet0 interface from %s", vmx)) vmxData, err := vmwarecommon.ReadVMX(vmx) if err != nil { return err @@ -168,6 +171,31 @@ func (p *PostProcessor) SetVHardwareVersion(vmx string, ui packer.Ui, hwversion return nil } +func (p *PostProcessor) SetGuestOs(vmx string, ui packer.Ui, os string) error { + vmxContent, err := ioutil.ReadFile(vmx) + lines := strings.Split(string(vmxContent), "\n") + updated := false + guestOsLine := fmt.Sprintf("guestos = \"%s\"", os) + for i, line := range lines { + if strings.Contains(line, "guestos") { + lines[i] = guestOsLine + ui.Message(fmt.Sprintf("Updated the OS type in the vmx to '%s'", os)) + updated = true + } + } + if updated == false { + lines = append(lines, guestOsLine) + ui.Message(fmt.Sprintf("Append the OS type in the vmx to '%s'", os)) + } + output := strings.Join(lines, "\n") + err = ioutil.WriteFile(vmx, []byte(output), 0644) + if err != nil { + return err + } + + return nil +} + func (p *PostProcessor) RemoveOpticalDrive(vmx string, ui packer.Ui) error { ui.Message(fmt.Sprintf("Removing optical drive from %s", vmx)) vmxData, err := vmwarecommon.ReadVMX(vmx) @@ -215,12 +243,12 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac if ova != "" { // Sweet, we've got an OVA, Now it's time to make that baby something we can work with. var args []string - if p.config.RemoveEthernet != "true" { - args = append(args, - "--allowExtraConfig", - fmt.Sprintf("--extraConfig:ethernet0.networkName=%s", p.config.VMNetwork), - ) - } + //if p.config.RemoveEthernet != "true" { + // args = append(args, + // "--allowExtraConfig", + // fmt.Sprintf("--extraConfig:ethernet0.networkName=%s", p.config.VMNetwork), + // ) + //} args = append(args, "--lax", ova, @@ -264,6 +292,12 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac } } + if p.config.VMWareGuestOsType != "" { + if err := p.SetGuestOs(vmx, ui, p.config.VMWareGuestOsType); err != nil { + return nil, false, fmt.Errorf("Setting the Guest OS in VMX failed!") + } + } + ui.Message(fmt.Sprintf("Uploading %s and %s to Datastore %s on host %s", vmdk, vmx, p.config.Datastore, p.config.Host)) clonerequired := false @@ -385,19 +419,18 @@ func doRegistration(ui packer.Ui, config Config, vmx string, clonerequired bool) } finder := find.NewFinder(client.Client, false) - datacenter, err := finder.DatacenterOrDefault(context.TODO(), config.Datacenter) + datacenter, err := finder.DatacenterOrDefault(context.TODO(), config.Datacenter) if err != nil { return err } - finder.SetDatacenter(datacenter) - + finder.SetDatacenter(datacenter) folders, err := datacenter.Folders(context.TODO()) if err != nil { return err } - resourcePool, err := finder.DefaultResourcePool(context.TODO()) + resourcePool, err := finder.ResourcePoolOrDefault(context.TODO(), config.ResourcePool) if err != nil { return err @@ -535,3 +568,4 @@ func doRegistration(ui packer.Ui, config Config, vmx string, clonerequired bool) return nil } +