-
Notifications
You must be signed in to change notification settings - Fork 9.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Vsphere windows support #6087
Merged
Merged
Vsphere windows support #6087
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c40f739
Support for Linked Cloning in vsphere, based off of https://github.co…
aheeren 5f4a3ec
Creating different config spec based on template OS ID
aheeren 7dfc0a6
Added windows clone options in vsphere and documented them
aheeren 338cb95
Rearranging code to clean up git diff
aheeren f04298f
Renaming linkedClone to linked_clone in config spec
aheeren 0b97c0a
Adding default time logic for windows clones
aheeren File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import ( | |
"fmt" | ||
"log" | ||
"net" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
|
@@ -43,24 +44,35 @@ type hardDisk struct { | |
initType string | ||
} | ||
|
||
//Additional options Vsphere can use clones of windows machines | ||
type windowsOptConfig struct { | ||
productKey string | ||
adminPassword string | ||
domainUser string | ||
domain string | ||
domainUserPassword string | ||
} | ||
|
||
type virtualMachine struct { | ||
name string | ||
folder string | ||
datacenter string | ||
cluster string | ||
resourcePool string | ||
datastore string | ||
vcpu int | ||
memoryMb int64 | ||
template string | ||
networkInterfaces []networkInterface | ||
hardDisks []hardDisk | ||
gateway string | ||
domain string | ||
timeZone string | ||
dnsSuffixes []string | ||
dnsServers []string | ||
customConfigurations map[string](types.AnyType) | ||
name string | ||
folder string | ||
datacenter string | ||
cluster string | ||
resourcePool string | ||
datastore string | ||
vcpu int | ||
memoryMb int64 | ||
template string | ||
networkInterfaces []networkInterface | ||
hardDisks []hardDisk | ||
gateway string | ||
domain string | ||
timeZone string | ||
dnsSuffixes []string | ||
dnsServers []string | ||
linkedClone bool | ||
windowsOptionalConfig windowsOptConfig | ||
customConfigurations map[string](types.AnyType) | ||
} | ||
|
||
func (v virtualMachine) Path() string { | ||
|
@@ -124,6 +136,12 @@ func resourceVSphereVirtualMachine() *schema.Resource { | |
ForceNew: true, | ||
}, | ||
|
||
"linkedClone": &schema.Schema{ | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
Default: false, | ||
ForceNew: true, | ||
}, | ||
"gateway": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
|
@@ -163,6 +181,44 @@ func resourceVSphereVirtualMachine() *schema.Resource { | |
Optional: true, | ||
ForceNew: true, | ||
}, | ||
"windows_opt_config": &schema.Schema{ | ||
Type: schema.TypeList, | ||
Optional: true, | ||
ForceNew: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"product_key": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"admin_password": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"domain_user": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"domain": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"domain_user_password": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
|
||
"network_interface": &schema.Schema{ | ||
Type: schema.TypeList, | ||
|
@@ -318,6 +374,10 @@ func resourceVSphereVirtualMachineCreate(d *schema.ResourceData, meta interface{ | |
vm.timeZone = v.(string) | ||
} | ||
|
||
if v, ok := d.GetOk("linkedClone"); ok { | ||
vm.linkedClone = v.(bool) | ||
} | ||
|
||
if raw, ok := d.GetOk("dns_suffixes"); ok { | ||
for _, v := range raw.([]interface{}) { | ||
vm.dnsSuffixes = append(vm.dnsSuffixes, v.(string)) | ||
|
@@ -374,6 +434,28 @@ func resourceVSphereVirtualMachineCreate(d *schema.ResourceData, meta interface{ | |
log.Printf("[DEBUG] network_interface init: %v", networks) | ||
} | ||
|
||
if vL, ok := d.GetOk("windows_opt_config"); ok { | ||
var winOpt windowsOptConfig | ||
custom_configs := (vL.([]interface{}))[0].(map[string]interface{}) | ||
if v, ok := custom_configs["admin_password"].(string); ok && v != "" { | ||
winOpt.adminPassword = v | ||
} | ||
if v, ok := custom_configs["domain"].(string); ok && v != "" { | ||
winOpt.domain = v | ||
} | ||
if v, ok := custom_configs["domain_user"].(string); ok && v != "" { | ||
winOpt.domainUser = v | ||
} | ||
if v, ok := custom_configs["product_key"].(string); ok && v != "" { | ||
winOpt.productKey = v | ||
} | ||
if v, ok := custom_configs["domain_user_password"].(string); ok && v != "" { | ||
winOpt.domainUserPassword = v | ||
} | ||
vm.windowsOptionalConfig = winOpt | ||
log.Printf("[DEBUG] windows config init: %v", winOpt) | ||
} | ||
|
||
if vL, ok := d.GetOk("disk"); ok { | ||
disks := make([]hardDisk, len(vL.([]interface{}))) | ||
for i, v := range vL.([]interface{}) { | ||
|
@@ -707,8 +789,15 @@ func buildNetworkDevice(f *find.Finder, label, adapterType string) (*types.Virtu | |
} | ||
|
||
// buildVMRelocateSpec builds VirtualMachineRelocateSpec to set a place for a new VirtualMachine. | ||
func buildVMRelocateSpec(rp *object.ResourcePool, ds *object.Datastore, vm *object.VirtualMachine, initType string) (types.VirtualMachineRelocateSpec, error) { | ||
func buildVMRelocateSpec(rp *object.ResourcePool, ds *object.Datastore, vm *object.VirtualMachine, linkedClone bool, initType string) (types.VirtualMachineRelocateSpec, error) { | ||
var key int | ||
var moveType string | ||
if linkedClone { | ||
moveType = "createNewChildDiskBacking" | ||
} else { | ||
moveType = "moveAllDiskBackingsAndDisallowSharing" | ||
} | ||
log.Printf("[DEBUG] relocate type: [%s]", moveType) | ||
|
||
devices, err := vm.Device(context.TODO()) | ||
if err != nil { | ||
|
@@ -724,8 +813,9 @@ func buildVMRelocateSpec(rp *object.ResourcePool, ds *object.Datastore, vm *obje | |
rpr := rp.Reference() | ||
dsr := ds.Reference() | ||
return types.VirtualMachineRelocateSpec{ | ||
Datastore: &dsr, | ||
Pool: &rpr, | ||
Datastore: &dsr, | ||
Pool: &rpr, | ||
DiskMoveType: moveType, | ||
Disk: []types.VirtualMachineRelocateSpecDiskLocator{ | ||
types.VirtualMachineRelocateSpecDiskLocator{ | ||
Datastore: dsr, | ||
|
@@ -1099,7 +1189,7 @@ func (vm *virtualMachine) deployVirtualMachine(c *govmomi.Client) error { | |
} | ||
log.Printf("[DEBUG] datastore: %#v", datastore) | ||
|
||
relocateSpec, err := buildVMRelocateSpec(resourcePool, datastore, template, vm.hardDisks[0].initType) | ||
relocateSpec, err := buildVMRelocateSpec(resourcePool, datastore, template, vm.linkedClone, vm.hardDisks[0].initType) | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -1179,16 +1269,69 @@ func (vm *virtualMachine) deployVirtualMachine(c *govmomi.Client) error { | |
log.Printf("[DEBUG] virtual machine Extra Config spec: %v", configSpec.ExtraConfig) | ||
} | ||
|
||
// create CustomizationSpec | ||
customSpec := types.CustomizationSpec{ | ||
Identity: &types.CustomizationLinuxPrep{ | ||
var template_mo mo.VirtualMachine | ||
err = template.Properties(context.TODO(), template.Reference(), []string{"parent", "config.template", "config.guestId", "resourcePool", "snapshot", "guest.toolsVersionStatus2", "config.guestFullName"}, &template_mo) | ||
|
||
var identity_options types.BaseCustomizationIdentitySettings | ||
if strings.HasPrefix(template_mo.Config.GuestId, "win") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dig it - this is the same path we were going down - yours is much cleaner. We may include the pieces to domain join in a second round once we can show that basic provisioning of a Win box works. |
||
var timeZone int | ||
timeZone, err := strconv.Atoi(vm.timeZone) | ||
if err != nil { | ||
return fmt.Errorf("Error converting TimeZone: %s", err) | ||
} | ||
|
||
guiUnattended := types.CustomizationGuiUnattended{ | ||
AutoLogon: false, | ||
AutoLogonCount: 1, | ||
TimeZone: timeZone, | ||
} | ||
|
||
customIdentification := types.CustomizationIdentification{} | ||
|
||
userData := types.CustomizationUserData{ | ||
ComputerName: &types.CustomizationFixedName{ | ||
Name: strings.Split(vm.name, ".")[0], | ||
}, | ||
ProductId: vm.windowsOptionalConfig.productKey, | ||
FullName: "terraform", | ||
OrgName: "terraform", | ||
} | ||
|
||
if vm.windowsOptionalConfig.domainUserPassword != "" && vm.windowsOptionalConfig.domainUser != "" && vm.windowsOptionalConfig.domain != "" { | ||
customIdentification.DomainAdminPassword = &types.CustomizationPassword{ | ||
PlainText: true, | ||
Value: vm.windowsOptionalConfig.domainUserPassword, | ||
} | ||
customIdentification.DomainAdmin = vm.windowsOptionalConfig.domainUser | ||
customIdentification.JoinDomain = vm.windowsOptionalConfig.domain | ||
} | ||
|
||
if vm.windowsOptionalConfig.adminPassword != "" { | ||
guiUnattended.Password = &types.CustomizationPassword{ | ||
PlainText: true, | ||
Value: vm.windowsOptionalConfig.adminPassword, | ||
} | ||
} | ||
|
||
identity_options = &types.CustomizationSysprep{ | ||
GuiUnattended: guiUnattended, | ||
Identification: customIdentification, | ||
UserData: userData, | ||
} | ||
} else { | ||
identity_options = &types.CustomizationLinuxPrep{ | ||
HostName: &types.CustomizationFixedName{ | ||
Name: strings.Split(vm.name, ".")[0], | ||
}, | ||
Domain: vm.domain, | ||
TimeZone: vm.timeZone, | ||
HwClockUTC: types.NewBool(true), | ||
}, | ||
} | ||
} | ||
|
||
// create CustomizationSpec | ||
customSpec := types.CustomizationSpec{ | ||
Identity: identity_options, | ||
GlobalIPSettings: types.CustomizationGlobalIPSettings{ | ||
DnsSuffixList: vm.dnsSuffixes, | ||
DnsServerList: vm.dnsServers, | ||
|
@@ -1204,6 +1347,15 @@ func (vm *virtualMachine) deployVirtualMachine(c *govmomi.Client) error { | |
Config: &configSpec, | ||
PowerOn: false, | ||
} | ||
if vm.linkedClone { | ||
if err != nil { | ||
return fmt.Errorf("Error reading base VM properties: %s", err) | ||
} | ||
if template_mo.Snapshot == nil { | ||
return fmt.Errorf("`linkedClone=true`, but image VM has no snapshots") | ||
} | ||
cloneSpec.Snapshot = template_mo.Snapshot.CurrentSnapshot | ||
} | ||
log.Printf("[DEBUG] clone spec: %v", cloneSpec) | ||
|
||
task, err := template.Clone(context.TODO(), folder, vm.name, cloneSpec) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aheeren I think this will be asked to change since it doesn't meet naming convention standards. Probably asked for
linked_clone
and this may conflict with other PRs that are currently out there.