Class: ChefProvisioningVsphere::CloneSpecBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/provisioning/vsphere_driver/clone_spec_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_handlerObject (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_helperObject (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, options)
  clone_spec = RbVmomi::VIM.VirtualMachineCloneSpec(
    location: relocate_spec_for(vm_template, options),
    powerOn: false,
    template: false,
    config: RbVmomi::VIM.VirtualMachineConfigSpec(
      cpuHotAddEnabled: true,
      memoryHotAddEnabled: true,
      cpuHotRemoveEnabled: true,
      deviceChange: []
    )
  )

  unless options[:annotation].to_s.nil?
    clone_spec.config.annotation = options[:annotation]
  end

  unless options[:num_cpus].to_s.nil?
    clone_spec.config.numCPUs = options[:num_cpus]
  end

  unless options[:memory_mb].to_s.nil?
    clone_spec.config.memoryMB = options[:memory_mb]
  end

  unless options[:network_name].nil?
    deviceAdditions, changes = vsphere_helper.network_device_changes(
      action_handler,
      vm_template,
      options
    )
    clone_spec.config.deviceChange = changes
  end

  clone_spec.customization = customization_options_from(
    vm_template,
    vm_name,
    options
  )

  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 customization_options_from(vm_template, vm_name, options)
  if options.key?(:customization_spec)
    if options[:customization_spec].is_a?(Hash) ||
       options[:customization_spec].is_a?(Cheffish::MergedConfig)
      cust_options = options[:customization_spec]
      ip_settings = cust_options[:ipsettings]
      cust_domain = cust_options[:domain]

      raise ArgumentError, 'domain is required' unless cust_domain
      cust_ip_settings = nil
      if ip_settings && ip_settings.key?(:ip)
        unless cust_options[: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(cust_options, vm_name)
      cust_hwclockutc = cust_options[:hw_clock_utc]
      cust_timezone = cust_options[:time_zone]

      cust_prep = if vm_template.config.guestId.start_with?('win')
                    windows_prep_for(options, 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(options[: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(options, vm_name)
  hostname = options[: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, options)
  rspec = RbVmomi::VIM.VirtualMachineRelocateSpec
  host = nil

  if options.key?(:host)
    host = vsphere_helper.find_host(options[:host])
    rspec.host = host
  end

  if options[:resource_pool]
    rspec.pool = vsphere_helper.find_pool(options[: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 options[: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 options[:datastore].to_s.empty?
    rspec.datastore = vsphere_helper.find_datastore(options[: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(options, vm_name)
  cust_options = options[:customization_spec]
  unless cust_options[:run_once].nil?
    cust_runonce = RbVmomi::VIM::CustomizationGuiRunOnce.new(
      commandList: cust_options[:run_once]
    )
  end

   = RbVmomi::VIM::CustomizationPassword(
    plainText: true,
    value: options[:ssh][:password]
  )
  if cust_options.key?(:domain) && (cust_options[:domain] != 'local')
    cust_domain_password = RbVmomi::VIM::CustomizationPassword(
      plainText: true,
      value: ENV['domainAdminPassword'] || cust_options[:domainAdminPassword]
    )
    cust_id = RbVmomi::VIM::CustomizationIdentification.new(
      joinDomain: cust_options[:domain],
      domainAdmin: cust_options[:domainAdmin],
      domainAdminPassword: cust_domain_password
    )
    # puts "my env passwd is: #{ENV['domainAdminPassword']}"
    action_handler.report_progress "joining domain #{cust_options[:domain]} /
      with user: #{cust_options[:domainAdmin]}"
  else
    cust_id = RbVmomi::VIM::CustomizationIdentification.new(
      joinWorkgroup: 'WORKGROUP'
    )
  end
  cust_gui_unattended = RbVmomi::VIM::CustomizationGuiUnattended.new(
    autoLogon: true,
    autoLogonCount: 1,
    password: ,
    timeZone: cust_options[:win_time_zone]
  )
  cust_userdata = RbVmomi::VIM::CustomizationUserData.new(
    computerName: hostname_from(cust_options, vm_name),
    fullName: cust_options[:org_name],
    orgName: cust_options[:org_name],
    productId: cust_options[:product_id]
  )
  RbVmomi::VIM::CustomizationSysprep.new(
    guiRunOnce: cust_runonce,
    identification: cust_id,
    guiUnattended: cust_gui_unattended,
    userData: cust_userdata
  )
end