Class: ChefProvisioningVsphere::CloneSpecBuilder
- Inherits:
-
Object
- Object
- ChefProvisioningVsphere::CloneSpecBuilder
- Defined in:
- lib/chef/provisioning/vsphere_driver/clone_spec_builder.rb
Overview
Creates a cspec for VMware
Instance Attribute Summary collapse
-
#action_handler ⇒ Object
readonly
Returns the value of attribute action_handler.
-
#vsphere_helper ⇒ Object
readonly
Returns the value of attribute vsphere_helper.
Instance Method Summary collapse
-
#build(vm_template, vm_name, options) ⇒ Object
The main build method.
-
#customization_options_from(vm_template, vm_name, options) ⇒ Object
Verify and create all the options needed for Customization Specs.
-
#hostname_from(options, vm_name) ⇒ Object
Creates a hostname, and verifies that it fulfills the requirements.
-
#initialize(vsphere_helper, action_handler) ⇒ CloneSpecBuilder
constructor
A new instance of CloneSpecBuilder.
-
#relocate_spec_for(vm_template, options) ⇒ Object
Figure out or declare where you need to bootstrap the vm.
-
#windows_prep_for(options, vm_name) ⇒ Object
Preparation work for windows, sysprep and the like.
Constructor Details
#initialize(vsphere_helper, action_handler) ⇒ CloneSpecBuilder
Returns a new instance of CloneSpecBuilder.
7 8 9 10 |
# File 'lib/chef/provisioning/vsphere_driver/clone_spec_builder.rb', line 7 def initialize(vsphere_helper, action_handler) @vsphere_helper = vsphere_helper @action_handler = action_handler end |
Instance Attribute Details
#action_handler ⇒ Object (readonly)
Returns the value of attribute action_handler.
13 14 15 |
# File 'lib/chef/provisioning/vsphere_driver/clone_spec_builder.rb', line 13 def action_handler @action_handler end |
#vsphere_helper ⇒ Object (readonly)
Returns the value of attribute vsphere_helper.
12 13 14 |
# File 'lib/chef/provisioning/vsphere_driver/clone_spec_builder.rb', line 12 def vsphere_helper @vsphere_helper end |
Instance Method Details
#build(vm_template, vm_name, options) ⇒ Object
The main build method.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/chef/provisioning/vsphere_driver/clone_spec_builder.rb', line 20 def build(vm_template, vm_name, ) clone_spec = RbVmomi::VIM.VirtualMachineCloneSpec( location: relocate_spec_for(vm_template, ), powerOn: false, template: false, config: RbVmomi::VIM.VirtualMachineConfigSpec( cpuHotAddEnabled: true, memoryHotAddEnabled: true, cpuHotRemoveEnabled: true, deviceChange: [] ) ) unless [:annotation].to_s.nil? clone_spec.config.annotation = [:annotation] end unless [:num_cpus].to_s.nil? clone_spec.config.numCPUs = [:num_cpus] end unless [:memory_mb].to_s.nil? clone_spec.config.memoryMB = [:memory_mb] end unless [:network_name].nil? deviceAdditions, changes = vsphere_helper.network_device_changes( action_handler, vm_template, ) clone_spec.config.deviceChange = changes end clone_spec.customization = ( vm_template, vm_name, ) clone_spec end |
#customization_options_from(vm_template, vm_name, options) ⇒ Object
Verify and create all the options needed for Customization Specs
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/chef/provisioning/vsphere_driver/clone_spec_builder.rb', line 105 def (vm_template, vm_name, ) if .key?(:customization_spec) if [:customization_spec].is_a?(Hash) || [:customization_spec].is_a?(Cheffish::MergedConfig) = [:customization_spec] ip_settings = [:ipsettings] cust_domain = [:domain] raise ArgumentError, 'domain is required' unless cust_domain cust_ip_settings = nil if ip_settings && ip_settings.key?(:ip) unless [:ipsettings].key?(:subnetMask) raise ArgumentError, 'subnetMask is required for static ip' end cust_ip_settings = RbVmomi::VIM::CustomizationIPSettings.new( ip_settings ) action_handler.report_progress "customizing #{vm_name} \ with static IP #{ip_settings[:ip]}" cust_ip_settings.ip = RbVmomi::VIM::CustomizationFixedIp( ipAddress: ip_settings[:ip] ) end if cust_ip_settings.nil? cust_ip_settings = RbVmomi::VIM::CustomizationIPSettings.new( ip: RbVmomi::VIM::CustomizationDhcpIpGenerator.new ) end if ip_settings && ip_settings.key?(:dnsServerList) cust_ip_settings.dnsServerList = ip_settings[:dnsServerList] action_handler.report_progress "customizing #{vm_name} with / dynamic IP and DNS: #{ip_settings[:dnsServerList]}" end cust_ip_settings.dnsDomain = cust_domain global_ip_settings = RbVmomi::VIM::CustomizationGlobalIPSettings.new global_ip_settings.dnsServerList = cust_ip_settings.dnsServerList global_ip_settings.dnsSuffixList = [cust_domain] cust_hostname = hostname_from(, vm_name) cust_hwclockutc = [:hw_clock_utc] cust_timezone = [:time_zone] cust_prep = if vm_template.config.guestId.start_with?('win') windows_prep_for(, vm_name) else RbVmomi::VIM::CustomizationLinuxPrep.new( domain: cust_domain, hostName: cust_hostname, hwClockUTC: cust_hwclockutc, timeZone: cust_timezone ) end cust_adapter_mapping = [ RbVmomi::VIM::CustomizationAdapterMapping.new( adapter: cust_ip_settings ) ] RbVmomi::VIM::CustomizationSpec.new( identity: cust_prep, globalIPSettings: global_ip_settings, nicSettingMap: cust_adapter_mapping ) else vsphere_helper.find_customization_spec([:customization_spec]) end end end |
#hostname_from(options, vm_name) ⇒ Object
Creates a hostname, and verifies that it fulfills the requirements
178 179 180 181 182 183 184 185 |
# File 'lib/chef/provisioning/vsphere_driver/clone_spec_builder.rb', line 178 def hostname_from(, vm_name) hostname = [:hostname] || vm_name test = /^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])$/ unless hostname.match(test) raise 'Only letters, numbers or hyphens in hostnames allowed' end RbVmomi::VIM::CustomizationFixedName.new(name: hostname) end |
#relocate_spec_for(vm_template, options) ⇒ Object
Figure out or declare where you need to bootstrap the vm
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/chef/provisioning/vsphere_driver/clone_spec_builder.rb', line 67 def relocate_spec_for(vm_template, ) rspec = RbVmomi::VIM.VirtualMachineRelocateSpec host = nil if .key?(:host) host = vsphere_helper.find_host([:host]) rspec.host = host end if [:resource_pool] rspec.pool = vsphere_helper.find_pool([:resource_pool]) elsif vm_template.config.template && !host.nil? rspec.pool = host.parent.resourcePool # assign to the "invisible" pool root elsif vm_template.config.template raise 'either :host or :resource_pool must be specified when cloning from a VM Template' end if [:use_linked_clone] if vm_template.config.template Chef::Log.warn('Using a VM Template, ignoring use_linked_clone.') else vsphere_helper.create_delta_disk(vm_template) rspec.diskMoveType = :moveChildMostDiskBacking end end unless [:datastore].to_s.empty? rspec.datastore = vsphere_helper.find_datastore([:datastore]) end rspec end |
#windows_prep_for(options, vm_name) ⇒ Object
Preparation work for windows, sysprep and the like.
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/chef/provisioning/vsphere_driver/clone_spec_builder.rb', line 191 def windows_prep_for(, vm_name) = [:customization_spec] unless [:run_once].nil? cust_runonce = RbVmomi::VIM::CustomizationGuiRunOnce.new( commandList: [:run_once] ) end cust_login_password = RbVmomi::VIM::CustomizationPassword( plainText: true, value: [:ssh][:password] ) if .key?(:domain) && ([:domain] != 'local') cust_domain_password = RbVmomi::VIM::CustomizationPassword( plainText: true, value: ENV['domainAdminPassword'] || [:domainAdminPassword] ) cust_id = RbVmomi::VIM::CustomizationIdentification.new( joinDomain: [:domain], domainAdmin: [:domainAdmin], domainAdminPassword: cust_domain_password ) action_handler.report_progress "joining domain #{[:domain]} / with user: #{[:domainAdmin]}" else cust_id = RbVmomi::VIM::CustomizationIdentification.new( joinWorkgroup: 'WORKGROUP' ) end cust_gui_unattended = RbVmomi::VIM::CustomizationGuiUnattended.new( autoLogon: true, autoLogonCount: 1, password: cust_login_password, timeZone: [:win_time_zone] ) cust_userdata = RbVmomi::VIM::CustomizationUserData.new( computerName: hostname_from(, vm_name), fullName: [:org_name], orgName: [:org_name], productId: [:product_id] ) RbVmomi::VIM::CustomizationSysprep.new( guiRunOnce: cust_runonce, identification: cust_id, guiUnattended: cust_gui_unattended, userData: cust_userdata ) end |