Class: ChefProvisioningVsphere::CloneSpecBuilder
- Inherits:
-
Object
- Object
- ChefProvisioningVsphere::CloneSpecBuilder
- Defined in:
- lib/chef/provisioning/vsphere_driver/clone_spec_builder.rb
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
- #customization_options_from(vm_template, vm_name, options) ⇒ Object
- #hostname_from(options, vm_name) ⇒ Object
-
#initialize(vsphere_helper, action_handler) ⇒ CloneSpecBuilder
constructor
A new instance of CloneSpecBuilder.
- #relocate_spec_for(vm_template, options) ⇒ Object
- #windows_prep_for(options, vm_name) ⇒ Object
Constructor Details
#initialize(vsphere_helper, action_handler) ⇒ CloneSpecBuilder
Returns a new instance of CloneSpecBuilder.
4 5 6 7 |
# File 'lib/chef/provisioning/vsphere_driver/clone_spec_builder.rb', line 4 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.
10 11 12 |
# File 'lib/chef/provisioning/vsphere_driver/clone_spec_builder.rb', line 10 def action_handler @action_handler end |
#vsphere_helper ⇒ Object (readonly)
Returns the value of attribute vsphere_helper.
9 10 11 |
# File 'lib/chef/provisioning/vsphere_driver/clone_spec_builder.rb', line 9 def vsphere_helper @vsphere_helper end |
Instance Method Details
#build(vm_template, vm_name, options) ⇒ Object
12 13 14 15 16 17 18 19 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 |
# File 'lib/chef/provisioning/vsphere_driver/clone_spec_builder.rb', line 12 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
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 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 |
# File 'lib/chef/provisioning/vsphere_driver/clone_spec_builder.rb', line 88 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
157 158 159 160 161 162 163 164 |
# File 'lib/chef/provisioning/vsphere_driver/clone_spec_builder.rb', line 157 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 =~ 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
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/chef/provisioning/vsphere_driver/clone_spec_builder.rb', line 55 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
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/chef/provisioning/vsphere_driver/clone_spec_builder.rb', line 166 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 ) # puts "my env passwd is: #{ENV['domainAdminPassword']}" 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 |