Skip to content
This repository has been archived by the owner on Sep 18, 2023. It is now read-only.

Add possibility to specify a resource pool #20

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
70 changes: 52 additions & 18 deletions post-processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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{
Expand All @@ -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
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -535,3 +568,4 @@ func doRegistration(ui packer.Ui, config Config, vmx string, clonerequired bool)

return nil
}