opennebula 6.1.90.pre → 6.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/vi_helper.rb CHANGED
@@ -118,6 +118,16 @@ module VCenterDriver
118
118
  return_if_error(rc, item, exit_if_fail)
119
119
  end
120
120
 
121
+ # Since https://github.com/OpenNebula/one/issues/5689
122
+ # there two deploy_ids allowed:
123
+ # * moref, eg: vm-567
124
+ # * moref +"_" + vcenter uuid, eg:
125
+ # 2499952a-6c85-480e-b7df-4cbd2137eb69_vm-456
126
+ # This function will always return the moref
127
+ def self.get_deploy_id(deploy_id)
128
+ deploy_id.split('_')[0]
129
+ end
130
+
121
131
  def self.find_by_name(the_class, name, pool = nil, raise_if_fail = true)
122
132
  pool = one_pool(the_class, raise_if_fail) if pool.nil?
123
133
  element = pool.find {|e| e['NAME'] == name.to_s }
@@ -251,7 +261,7 @@ module VCenterDriver
251
261
  # Let's try to find the VM object only by its name
252
262
  # Let's build the VM name
253
263
  vm_prefix = host['TEMPLATE/VM_PREFIX']
254
- vm_prefix = VM_PREFIX_DEFAULT if vm_prefix.nil? || vm_prefix.empty?
264
+ vm_prefix = VM_PREFIX_DEFAULT if vm_prefix.nil?
255
265
  vm_prefix.gsub!('$i', one_vm['ID'])
256
266
  vm_name = vm_prefix + one_vm['NAME']
257
267
 
@@ -103,6 +103,7 @@ end
103
103
 
104
104
  def initialize(vi_client, ref, one_id)
105
105
  if ref
106
+ ref = VCenterDriver::VIHelper.get_deploy_id(ref)
106
107
  @item = RbVmomi::VIM::VirtualMachine.new(vi_client.vim, ref)
107
108
  check_item(@item, RbVmomi::VIM::VirtualMachine)
108
109
  end
@@ -470,7 +471,7 @@ end
470
471
  # @return String vcenter name
471
472
  def vc_name
472
473
  vm_prefix = host['TEMPLATE/VM_PREFIX']
473
- vm_prefix = VM_PREFIX_DEFAULT if vm_prefix.nil? || vm_prefix.empty?
474
+ vm_prefix = VM_PREFIX_DEFAULT if vm_prefix.nil?
474
475
 
475
476
  if !one_item['USER_TEMPLATE/VM_PREFIX'].nil?
476
477
  vm_prefix = one_item['USER_TEMPLATE/VM_PREFIX']
@@ -579,8 +580,6 @@ end
579
580
  end
580
581
  end
581
582
 
582
- dc = cluster.datacenter
583
-
584
583
  vcenter_vm_folder = drv_action['USER_TEMPLATE/VCENTER_VM_FOLDER']
585
584
 
586
585
  if !vcenter_vm_folder.nil? && !vcenter_vm_folder.empty?
@@ -1715,8 +1714,13 @@ end
1715
1714
  def sync(deploy = {})
1716
1715
  extraconfig = []
1717
1716
  device_change = []
1717
+ sync_opt = nil
1718
1718
 
1719
- disks = sync_disks(:all, false)
1719
+ # Disk are only synced with :all option when VM is first created
1720
+ # NOTE: Detach actions are implemented through TM (not sync)
1721
+ sync_opt = :all if deploy[:new] == true
1722
+
1723
+ disks = sync_disks(sync_opt, false)
1720
1724
  resize_unmanaged_disks
1721
1725
 
1722
1726
  if deploy[:boot] && !deploy[:boot].empty?
@@ -1983,7 +1987,7 @@ end
1983
1987
  end
1984
1988
 
1985
1989
  card_spec = {
1986
- :key => Time.now.utc.strftime('%Y%m%d%M%S%L').to_i,
1990
+ :key => 0,
1987
1991
  :deviceInfo => {
1988
1992
  :label => 'net' + card_num.to_s,
1989
1993
  :summary => pg_name
@@ -1993,6 +1997,9 @@ end
1993
1997
  :macAddress => mac,
1994
1998
  :unitNumber => unumber
1995
1999
  }
2000
+ if @vi_client.vim.serviceContent.about.apiVersion.to_f >= 7.0
2001
+ card_spec[:key] = Time.now.utc.strftime('%m%d%M%S%L').to_i
2002
+ end
1996
2003
 
1997
2004
  if (limit || rsrv) && (limit > 0)
1998
2005
  ra_spec = {}
@@ -2129,7 +2136,7 @@ end
2129
2136
  end
2130
2137
 
2131
2138
  card_spec = {
2132
- :key => Time.now.utc.strftime('%Y%m%d%M%S%L').to_i,
2139
+ :key => 0,
2133
2140
  :deviceInfo => {
2134
2141
  :label => 'net' + card_num.to_s,
2135
2142
  :summary => pg_name
@@ -2137,6 +2144,9 @@ end
2137
2144
  :backing => backing,
2138
2145
  :addressType => 'generated'
2139
2146
  }
2147
+ if @vi_client.vim.serviceContent.about.apiVersion.to_f >= 7.0
2148
+ card_spec[:key] = Time.now.utc.strftime('%m%d%M%S%L').to_i
2149
+ end
2140
2150
 
2141
2151
  if (limit || rsrv) && (limit > 0)
2142
2152
  ra_spec = {}
@@ -2302,6 +2312,7 @@ end
2302
2312
  detach_disk_array = []
2303
2313
  extra_config = []
2304
2314
  keys = disk_keys.invert
2315
+
2305
2316
  ipool = VCenterDriver::VIHelper.one_pool(OpenNebula::ImagePool)
2306
2317
  disks_each(:detached?) do |d|
2307
2318
  key = d.key.to_s
@@ -2377,8 +2388,7 @@ end
2377
2388
  # @param option [symbol] if :all is provided the
2378
2389
  # method will try to sync
2379
2390
  # all the disks (detached and not existing ones)
2380
- # otherwishe it will only sync
2381
- # the disks that are not existing
2391
+ # otherwise it will only sync the disks that are not existing
2382
2392
  #
2383
2393
  # @param execute [boolean] indicates if the reconfigure operation
2384
2394
  # is going to
@@ -2428,8 +2438,8 @@ end
2428
2438
  # https://github.com/OpenNebula/one/issues/5409
2429
2439
  if snapshots? || one_snapshots?
2430
2440
  error_msg = 'Existing sytem snapshots, cannot change disks. '
2431
- error_msg << 'Please remove all snapshots and try again.'
2432
- raise error_message
2441
+ error_msg << 'Please remove all snapshots and try again'
2442
+ raise error_msg
2433
2443
  end
2434
2444
 
2435
2445
  spec_hash = {}
@@ -2615,7 +2625,7 @@ end
2615
2625
 
2616
2626
  if snapshots? || one_snapshots?
2617
2627
  error_message = 'Existing sytem snapshots, cannot change disks'
2618
- error_message << '. Please remove all snapshots and try again.'
2628
+ error_message << '. Please remove all snapshots and try again'
2619
2629
  raise error_message
2620
2630
  end
2621
2631
 
@@ -3066,7 +3076,7 @@ end
3066
3076
  @item.ReconfigVM_Task(:spec => vm_config_spec).wait_for_completion
3067
3077
 
3068
3078
  devices.each do |device|
3069
- next unless first_ &&
3079
+ next unless first_condition &&
3070
3080
  device.key == scsi_key
3071
3081
 
3072
3082
  controller = device.deviceInfo.label
@@ -3426,6 +3436,7 @@ end
3426
3436
  config[:esx_migration_list] = 'Selected_by_DRS'
3427
3437
  end
3428
3438
 
3439
+ vc_vm.reference_all_disks
3429
3440
  vc_vm.migrate(config)
3430
3441
 
3431
3442
  vm.replace({ 'VCENTER_CCR_REF' => ccr_ref })
data/lib/vm_template.rb CHANGED
@@ -96,6 +96,90 @@ module VCenterDriver
96
96
  @vi_client.vim.serviceContent.about.instanceUuid rescue nil
97
97
  end
98
98
 
99
+ def save_as_linked_clones(name)
100
+ error = nil
101
+
102
+ disks = @item.config.hardware.device.grep(
103
+ RbVmomi::VIM::VirtualMachine
104
+ )
105
+ disks.select {|x| x.backing.parent.nil? }.each do |disk|
106
+ spec = {
107
+ :deviceChange => [
108
+ {
109
+ :operation => :remove,
110
+ :device => disk
111
+ },
112
+ {
113
+ :operation => :add,
114
+ :fileOperation => :create,
115
+ :device => disk.dup.tap do |x|
116
+ x.backing = x.backing.dup
117
+ x.backing.fileName =
118
+ "[#{disk.backing.datastore.name}]"
119
+ x.backing.parent = disk.backing
120
+ end
121
+ }
122
+ ]
123
+ }
124
+ @item.ReconfigVM_Task(
125
+ :spec => spec
126
+ ).wait_for_completion
127
+ end
128
+
129
+ relocateSpec = RbVmomi::VIM.VirtualMachineRelocateSpec(
130
+ :diskMoveType => :moveChildMostDiskBacking
131
+ )
132
+
133
+ spec = RbVmomi::VIM.VirtualMachineCloneSpec(
134
+ :location => relocateSpec,
135
+ :powerOn => false,
136
+ :template => true
137
+ )
138
+
139
+ new_template = @item.CloneVM_Task(
140
+ :folder => @item.parent,
141
+ :name => name,
142
+ :spec => spec
143
+ ).wait_for_completion
144
+
145
+ new_vm_template_ref = new_template._ref
146
+
147
+ one_client = OpenNebula::Client.new
148
+ importer = VCenterDriver::VmImporter.new(
149
+ one_client,
150
+ @vi_client
151
+ )
152
+
153
+ importer.retrieve_resources({})
154
+ importer.get_indexes(new_vm_template_ref)
155
+
156
+ importer.process_import(
157
+ new_vm_template_ref,
158
+ {
159
+ new_vm_template_ref.to_s => {
160
+ :type => 'default',
161
+ :linked_clone => '1',
162
+ :copy => '0',
163
+ :name => '',
164
+ :folder => ''
165
+ }
166
+ }
167
+ )
168
+
169
+ begin
170
+ importer.output[:success][0][:id][0]
171
+ rescue StandardError => e
172
+ error = 'Creating linked clone VM Template' \
173
+ " failed due to \"#{e.message}\".\n"
174
+
175
+ if VCenterDriver::CONFIG[:debug_information]
176
+ error += " #{e.backtrace}\n"
177
+ end
178
+ end
179
+
180
+ [error, new_vm_template_ref]
181
+ end
182
+
99
183
  def create_template_copy(template_name)
100
184
  error = nil
101
185
  template_ref = nil
@@ -539,6 +623,23 @@ module VCenterDriver
539
623
 
540
624
  network.info
541
625
 
626
+ if nic[:ipv4] || nic[:ipv6]
627
+ ar_array = network.to_hash['VNET']['AR_POOL']['AR']
628
+ ar_array = [ar_array] if ar_array.is_a?(Hash)
629
+
630
+ ipv4, _ipv6, _arid = find_ip_in_ar(
631
+ IPAddr.new(nic[:ipv4]),
632
+ ar_array
633
+ ) if ar_array && nic[:ipv4]
634
+
635
+ _ipv4, ipv6, _arid = find_ip_in_ar(
636
+ IPAddr.new(nic[:ipv6]),
637
+ ar_array
638
+ ) if ar_array && nic[:ipv6]
639
+
640
+ return [ipv4, ipv6]
641
+ end
642
+
542
643
  # Iterate over Retrieve vCenter VM NICs
543
644
  unless vm_object.item.guest.net.empty?
544
645
  vm_object.item.guest.net.each do |net|
@@ -671,9 +772,10 @@ module VCenterDriver
671
772
  nic_tmp = "NIC=[\n"
672
773
  nic_tmp << "NETWORK_ID=\"#{one_vn.id}\",\n"
673
774
  nic_tmp << "NAME =\"VC_NIC#{nic_index}\",\n"
775
+ nic_tmp << "IP = \"#{nic[:ipv4]}\",\n" if nic[:ipv4]
674
776
 
675
777
  if vm?
676
- if nic[:mac]
778
+ if nic[:mac] && !nic[:ipv4]
677
779
  nic_tmp << "MAC=\"#{nic[:mac]}\",\n"
678
780
  end
679
781
  if nic[:ipv4_additionals]
@@ -729,6 +831,7 @@ module VCenterDriver
729
831
  ar_tmp = create_ar(nic)
730
832
  network_found.add_ar(ar_tmp)
731
833
  end
834
+
732
835
  ipv4, ipv6 = find_ips_in_network(network_found, vm_object,
733
836
  nic, true)
734
837
  network_found.info
@@ -740,8 +843,10 @@ module VCenterDriver
740
843
  if nic[:mac] && ipv4.empty? && ipv6.empty?
741
844
  nic_tmp << "MAC=\"#{nic[:mac]}\",\n"
742
845
  end
846
+
743
847
  nic_tmp << "IP=\"#{ipv4}\"," unless ipv4.empty?
744
848
  nic_tmp << "IP6=\"#{ipv6}\"," unless ipv6.empty?
849
+
745
850
  if nic[:ipv4_additionals]
746
851
  nic_tmp <<
747
852
  'VCENTER_ADDITIONALS_IP4'\
@@ -969,7 +1074,13 @@ module VCenterDriver
969
1074
  ar_tmp << "]\n"
970
1075
 
971
1076
  if vm?
972
- ar_tmp << create_ar(nic, true)
1077
+ ar_tmp << create_ar(nic, false, nic[:ipv4]) if nic[:ipv4]
1078
+
1079
+ if nic[:ipv6]
1080
+ ar_tmp << create_ar(nic, false, nil, nic[:ipv6])
1081
+ end
1082
+
1083
+ ar_tmp << create_ar(nic, true) if !nic[:ipv4] && !nic[:ipv6]
973
1084
  end
974
1085
 
975
1086
  one_vnet[:one] << ar_tmp
@@ -1024,6 +1135,23 @@ module VCenterDriver
1024
1135
  nic_index = 0
1025
1136
 
1026
1137
  vc_nics.each do |nic|
1138
+ [:ipv4, :ipv6].each do |type|
1139
+ if nic[type]
1140
+ opts[type].shift if opts[type]
1141
+ next
1142
+ end
1143
+
1144
+ begin
1145
+ ip = opts[type].shift if opts[type]
1146
+
1147
+ # Check if it is a valid IP
1148
+ IPAddr.new(ip)
1149
+
1150
+ nic[type] = ip
1151
+ rescue StandardError
1152
+ end
1153
+ end
1154
+
1027
1155
  # Check if the network already exists
1028
1156
  network_found =
1029
1157
  VCenterDriver::VIHelper
@@ -1835,12 +1963,10 @@ module VCenterDriver
1835
1963
  # the template if something go wrong
1836
1964
  if copy
1837
1965
  error, template_copy_ref =
1838
- selected[:template]
1839
- .create_template_copy(
1840
- opts[:name]
1841
- )
1966
+ selected[:template].save_as_linked_clones(opts[:name])
1967
+
1842
1968
  unless template_copy_ref
1843
- raise 'There is a problem creating creating' \
1969
+ raise 'There is a problem creating ' \
1844
1970
  "your copy: #{error}"
1845
1971
  end
1846
1972
 
@@ -1893,68 +2019,70 @@ module VCenterDriver
1893
2019
  working_template[:one] <<
1894
2020
  "VCENTER_TEMPLATE_NAME=\"#{selected[:name]}\"\n"
1895
2021
 
1896
- create(working_template[:one]) do |one_object, id|
1897
- res[:id] << id
2022
+ unless copy
2023
+ create(working_template[:one]) do |one_object, id|
2024
+ res[:id] << id
1898
2025
 
1899
- type = { :object => 'template', :id => id }
1900
- error, template_disks, allocated_images =
1901
- template
1902
- .import_vcenter_disks(
1903
- vc_uuid,
1904
- dpool,
1905
- ipool,
1906
- type
1907
- )
2026
+ type = { :object => 'template', :id => id }
2027
+ error, template_disks, allocated_images =
2028
+ template
2029
+ .import_vcenter_disks(
2030
+ vc_uuid,
2031
+ dpool,
2032
+ ipool,
2033
+ type
2034
+ )
1908
2035
 
1909
- if allocated_images
1910
- # rollback stack
1911
- allocated_images.reverse.each do |i|
1912
- @rollback.unshift(Raction.new(i, :delete))
2036
+ if allocated_images
2037
+ # rollback stack
2038
+ allocated_images.reverse.each do |i|
2039
+ @rollback.unshift(Raction.new(i, :delete))
2040
+ end
1913
2041
  end
1914
- end
1915
- raise error unless error.empty?
2042
+ raise error unless error.empty?
1916
2043
 
1917
- working_template[:one] << template_disks
2044
+ working_template[:one] << template_disks
1918
2045
 
1919
- if template_copy_ref
1920
- template_moref = template_copy_ref
1921
- else
1922
- template_moref = selected[:vcenter_ref]
1923
- end
2046
+ if template_copy_ref
2047
+ template_moref = template_copy_ref
2048
+ else
2049
+ template_moref = selected[:vcenter_ref]
2050
+ end
1924
2051
 
1925
- opts_nics = {
1926
- :vi_client => @vi_client,
1927
- :vc_uuid => vc_uuid,
1928
- :npool => npool,
1929
- :hpool => hpool,
1930
- :vcenter => vcenter,
1931
- :template_moref => template_moref,
1932
- :vm_object => nil
1933
- }
2052
+ opts_nics = {
2053
+ :vi_client => @vi_client,
2054
+ :vc_uuid => vc_uuid,
2055
+ :npool => npool,
2056
+ :hpool => hpool,
2057
+ :vcenter => vcenter,
2058
+ :template_moref => template_moref,
2059
+ :vm_object => nil
2060
+ }
1934
2061
 
1935
- error, template_nics, _ar_ids, allocated_nets =
1936
- template
1937
- .import_vcenter_nics(
1938
- opts_nics,
1939
- id,
1940
- dc
1941
- )
2062
+ error, template_nics, _ar_ids, allocated_nets =
2063
+ template
2064
+ .import_vcenter_nics(
2065
+ opts_nics,
2066
+ id,
2067
+ dc
2068
+ )
1942
2069
 
1943
- if allocated_nets
1944
- # rollback stack
1945
- allocated_nets.reverse.each do |n|
1946
- @rollback.unshift(Raction.new(n, :delete))
2070
+ if allocated_nets
2071
+ # rollback stack
2072
+ allocated_nets.reverse.each do |n|
2073
+ @rollback.unshift(Raction.new(n, :delete))
2074
+ end
1947
2075
  end
1948
- end
1949
- raise error unless error.empty?
2076
+ raise error unless error.empty?
1950
2077
 
1951
- working_template[:one] << template_nics
1952
- working_template[:one] << rp_opts(
1953
- opts[:type],
1954
- opts[:resourcepool]
1955
- )
2078
+ working_template[:one] << template_nics
2079
+ working_template[:one] << rp_opts(
2080
+ opts[:type],
2081
+ opts[:resourcepool]
2082
+ )
1956
2083
 
1957
- one_object.update(working_template[:one])
2084
+ one_object.update(working_template[:one])
2085
+ end
1958
2086
  end
1959
2087
 
1960
2088
  res
data/lib/vmm_importer.rb CHANGED
@@ -46,7 +46,8 @@ module VCenterDriver
46
46
  vm_ref = selected['DEPLOY_ID'] || selected[:wild]['DEPLOY_ID']
47
47
  vm = selected[:one_item] || build
48
48
  template = selected[:template] || import_tmplt
49
- host_id = selected[:host] || @list.keys[0]
49
+ template = "DEPLOY_ID = #{vm_ref}\n" + template
50
+ host_id = selected[:host] || @list.keys[0]
50
51
 
51
52
  vc_uuid = @vi_client.vim.serviceContent.about.instanceUuid
52
53
  vc_name = @vi_client.vim.host
@@ -72,13 +73,15 @@ module VCenterDriver
72
73
  template << template_disks
73
74
 
74
75
  opts = {
75
- :vi_client => @vi_client,
76
- :vc_uuid => vc_uuid,
77
- :npool => npool,
78
- :hpool => hpool,
79
- :vcenter => vc_name,
76
+ :vi_client => @vi_client,
77
+ :vc_uuid => vc_uuid,
78
+ :npool => npool,
79
+ :hpool => hpool,
80
+ :vcenter => vc_name,
80
81
  :template_moref => vm_ref,
81
- :vm_object => vc_vm
82
+ :vm_object => vc_vm,
83
+ :ipv4 => selected[:ipv4],
84
+ :ipv6 => selected[:ipv6]
82
85
  }
83
86
 
84
87
  # Create images or get nics information for template