opennebula 5.12.12 → 5.13.80.pre
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/lib/DriverExecHelper.rb +43 -27
- data/lib/OpenNebulaDriver.rb +7 -3
- data/lib/VirtualMachineDriver.rb +8 -1
- data/lib/cloud/CloudClient.rb +1 -1
- data/lib/datacenter.rb +1258 -0
- data/lib/datastore.rb +1025 -0
- data/lib/distributed_firewall.rb +280 -0
- data/lib/file_helper.rb +370 -0
- data/lib/host.rb +1517 -0
- data/lib/logical_port.rb +50 -0
- data/lib/logical_switch.rb +77 -0
- data/lib/memoize.rb +74 -0
- data/lib/models/role.rb +1126 -0
- data/lib/models/service.rb +709 -0
- data/lib/models.rb +32 -0
- data/lib/network.rb +635 -0
- data/lib/nsx_client.rb +144 -0
- data/lib/nsx_component.rb +28 -0
- data/lib/nsx_constants.rb +149 -0
- data/lib/nsx_driver.rb +78 -0
- data/lib/nsx_error.rb +77 -0
- data/lib/nsx_rule.rb +193 -0
- data/lib/nsxt_client.rb +176 -0
- data/lib/nsxt_dfw.rb +196 -0
- data/lib/nsxt_logical_port.rb +94 -0
- data/lib/nsxt_rule.rb +188 -0
- data/lib/nsxt_tz.rb +38 -0
- data/lib/nsxv_client.rb +176 -0
- data/lib/nsxv_dfw.rb +202 -0
- data/lib/nsxv_logical_port.rb +107 -0
- data/lib/nsxv_rule.rb +172 -0
- data/lib/nsxv_tz.rb +41 -0
- data/lib/opaque_network.rb +134 -0
- data/lib/opennebula/document.rb +7 -28
- data/lib/opennebula/document_json.rb +41 -11
- data/lib/opennebula/error.rb +3 -0
- data/lib/opennebula/flow/grammar.rb +1195 -0
- data/lib/opennebula/flow/service_pool.rb +190 -0
- data/lib/opennebula/flow/service_template.rb +572 -0
- data/lib/opennebula/flow/service_template_ext.rb +84 -0
- data/lib/opennebula/flow/service_template_pool.rb +32 -0
- data/lib/opennebula/flow/validator.rb +499 -0
- data/lib/opennebula/flow.rb +23 -0
- data/lib/opennebula/hook.rb +4 -11
- data/lib/opennebula/image.rb +16 -13
- data/lib/opennebula/lockable_ext.rb +163 -0
- data/lib/opennebula/marketplaceapp.rb +8 -118
- data/lib/opennebula/marketplaceapp_ext.rb +522 -0
- data/lib/opennebula/oneflow_client.rb +3 -2
- data/lib/opennebula/pool.rb +3 -2
- data/lib/opennebula/template.rb +3 -12
- data/lib/opennebula/template_ext.rb +325 -0
- data/lib/opennebula/user.rb +25 -1
- data/lib/opennebula/virtual_machine.rb +24 -206
- data/lib/opennebula/virtual_machine_ext.rb +469 -0
- data/lib/opennebula/virtual_machine_pool.rb +0 -4
- data/lib/opennebula/virtual_network.rb +3 -9
- data/lib/opennebula/virtual_router.rb +3 -11
- data/lib/opennebula/vm_group.rb +3 -10
- data/lib/opennebula/vntemplate.rb +3 -12
- data/lib/opennebula/wait_ext.rb +222 -0
- data/lib/opennebula.rb +4 -1
- data/lib/rest_client.rb +201 -0
- data/lib/scripts_common.rb +180 -0
- data/lib/transport_zone.rb +43 -0
- data/lib/vcenter_driver.rb +8 -21
- data/lib/vcenter_importer.rb +616 -0
- data/lib/vi_client.rb +281 -0
- data/lib/vi_helper.rb +312 -0
- data/lib/virtual_machine.rb +3477 -0
- data/lib/virtual_wire.rb +158 -0
- data/lib/vm_device.rb +80 -0
- data/lib/vm_disk.rb +202 -0
- data/lib/vm_folder.rb +69 -0
- data/lib/vm_helper.rb +30 -0
- data/lib/vm_monitor.rb +303 -0
- data/lib/vm_nic.rb +70 -0
- data/lib/vm_template.rb +1961 -0
- data/lib/vmm_importer.rb +121 -0
- metadata +140 -26
@@ -0,0 +1,325 @@
|
|
1
|
+
# -------------------------------------------------------------------------- #
|
2
|
+
# Copyright 2002-2021, OpenNebula Project, OpenNebula Systems #
|
3
|
+
# #
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License"); you may #
|
5
|
+
# not use this file except in compliance with the License. You may obtain #
|
6
|
+
# a copy of the License at #
|
7
|
+
# #
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0 #
|
9
|
+
# #
|
10
|
+
# Unless required by applicable law or agreed to in writing, software #
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS, #
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
|
13
|
+
# See the License for the specific language governing permissions and #
|
14
|
+
# limitations under the License. #
|
15
|
+
#--------------------------------------------------------------------------- #
|
16
|
+
|
17
|
+
require 'securerandom'
|
18
|
+
require 'opennebula/wait_ext'
|
19
|
+
|
20
|
+
# Module to decorate Template class with additional helpers not directly
|
21
|
+
# exposed through the OpenNebula XMLRPC API. The extensions include
|
22
|
+
# - mp_import helper that imports a template into a marketplace
|
23
|
+
#
|
24
|
+
# rubocop:disable Style/ClassAndModuleChildren
|
25
|
+
module OpenNebula::TemplateExt
|
26
|
+
|
27
|
+
def self.extend_object(obj)
|
28
|
+
if !obj.is_a?(OpenNebula::Template)
|
29
|
+
raise StandardError, "Cannot extended #{obj.class} with TemplateExt"
|
30
|
+
end
|
31
|
+
|
32
|
+
class << obj
|
33
|
+
|
34
|
+
####################################################################
|
35
|
+
# Public extended interface
|
36
|
+
####################################################################
|
37
|
+
|
38
|
+
# ------------------------------------------------------------------
|
39
|
+
# Imports template into marketplace
|
40
|
+
#
|
41
|
+
# @param market [Integer] Market to import the Template
|
42
|
+
# @param import_all [Bool] true to import images too,
|
43
|
+
# @param template_name [String] Virtual Machine Template app name
|
44
|
+
#
|
45
|
+
# @return [String, Array]
|
46
|
+
# - Error message in case of any or random generated template name
|
47
|
+
# - Objects IDs
|
48
|
+
# ------------------------------------------------------------------
|
49
|
+
def mp_import(market, import_all, template_name = nil, opts = {})
|
50
|
+
opts = {
|
51
|
+
:wait => false,
|
52
|
+
:logger => nil
|
53
|
+
}.merge(opts)
|
54
|
+
|
55
|
+
ids = []
|
56
|
+
images = []
|
57
|
+
logger = opts[:logger]
|
58
|
+
|
59
|
+
main_template = ''
|
60
|
+
|
61
|
+
#---------------------------------------------------------------
|
62
|
+
# Import all disks as IMAGE
|
63
|
+
#---------------------------------------------------------------
|
64
|
+
logger.info 'Processing VM disks' if logger
|
65
|
+
|
66
|
+
retrieve_xmlelements('TEMPLATE/DISK').each_with_index do
|
67
|
+
|disk, idx|
|
68
|
+
|
69
|
+
image = image_lookup(disk)
|
70
|
+
|
71
|
+
if OpenNebula.is_error?(image)
|
72
|
+
logger.fatal image.message if logger
|
73
|
+
|
74
|
+
rollback(ids)
|
75
|
+
return [image, ids]
|
76
|
+
end
|
77
|
+
|
78
|
+
logger.info "Adding disk with image #{image.id}" if logger
|
79
|
+
|
80
|
+
tmpl, main = create_app_template(image, idx)
|
81
|
+
|
82
|
+
if OpenNebula.is_error?(tmpl)
|
83
|
+
logger.fatal tmpl.message if logger
|
84
|
+
|
85
|
+
rollback(ids)
|
86
|
+
return [tmpl, ids]
|
87
|
+
end
|
88
|
+
|
89
|
+
main_template << main
|
90
|
+
|
91
|
+
next unless import_all
|
92
|
+
|
93
|
+
logger.info 'Importing image to market place' if logger
|
94
|
+
|
95
|
+
rc = create_app(tmpl, market)
|
96
|
+
|
97
|
+
if OpenNebula.is_error?(rc)
|
98
|
+
logger.fatal rc.message if logger
|
99
|
+
|
100
|
+
rollback(ids)
|
101
|
+
return [rc, ids]
|
102
|
+
end
|
103
|
+
|
104
|
+
images << image
|
105
|
+
|
106
|
+
ids << rc
|
107
|
+
end
|
108
|
+
|
109
|
+
delete_element('TEMPLATE/DISK')
|
110
|
+
|
111
|
+
#---------------------------------------------------------------
|
112
|
+
# Import VM template
|
113
|
+
#---------------------------------------------------------------
|
114
|
+
logger.info 'Processing VM NICs' if logger
|
115
|
+
|
116
|
+
# Replace all nics by auto nics
|
117
|
+
nic_xpath = '/VMTEMPLATE/TEMPLATE/NIC'
|
118
|
+
alias_xpath = '/VMTEMPLATE/TEMPLATE/NIC_ALIAS'
|
119
|
+
|
120
|
+
retrieve_xmlelements(nic_xpath).each do |nic|
|
121
|
+
if nic['NETWORK']
|
122
|
+
net = "[NETWORK=\"#{nic['NETWORK']}\"]"
|
123
|
+
elsif nic['NETWORK_ID']
|
124
|
+
net = "[NETWORK_ID=\"#{nic['NETWORK_ID']}\"]"
|
125
|
+
end
|
126
|
+
|
127
|
+
# NIC can be already in auto mode
|
128
|
+
next unless net
|
129
|
+
|
130
|
+
# If there are ALIAS the NIC can't be auto,
|
131
|
+
# because this combination isn't supported by the core
|
132
|
+
nic_alias = retrieve_xmlelements(alias_xpath)
|
133
|
+
|
134
|
+
if !nic_alias || nic_alias.empty?
|
135
|
+
nic.add_element("#{nic_xpath}#{net}",
|
136
|
+
'NETWORK_MODE' => 'auto')
|
137
|
+
else
|
138
|
+
# Add attribute to avoid empty elements
|
139
|
+
nic.add_element("#{nic_xpath}#{net}", 'MARKET' => 'YES')
|
140
|
+
end
|
141
|
+
|
142
|
+
delete_nic_attributes(nic)
|
143
|
+
end
|
144
|
+
|
145
|
+
retrieve_xmlelements(alias_xpath).each do |nic|
|
146
|
+
delete_nic_attributes(nic)
|
147
|
+
end
|
148
|
+
|
149
|
+
# Rename it to avoid clashing names
|
150
|
+
if template_name
|
151
|
+
name = template_name
|
152
|
+
else
|
153
|
+
name = "#{self['NAME']}-#{SecureRandom.hex[0..9]}"
|
154
|
+
end
|
155
|
+
|
156
|
+
main_template << <<-EOT
|
157
|
+
NAME ="#{name}"
|
158
|
+
ORIGIN_ID ="-1\"
|
159
|
+
TYPE ="VMTEMPLATE"
|
160
|
+
VERSION ="#{OpenNebula::VERSION}"
|
161
|
+
|
162
|
+
APPTEMPLATE64 ="#{Base64.strict_encode64(template_str)}"
|
163
|
+
EOT
|
164
|
+
|
165
|
+
logger.info 'Creating VM app' if logger
|
166
|
+
|
167
|
+
rc = create_app(main_template, market)
|
168
|
+
|
169
|
+
if OpenNebula.is_error?(rc)
|
170
|
+
logger.fatal rc.message if logger
|
171
|
+
|
172
|
+
rollback(ids)
|
173
|
+
return [rc, ids]
|
174
|
+
end
|
175
|
+
|
176
|
+
logger.info 'Waiting for image upload' if logger && opts[:wait]
|
177
|
+
|
178
|
+
images.each {|i| i.wait('READY') } if opts[:wait]
|
179
|
+
|
180
|
+
ids << rc
|
181
|
+
|
182
|
+
[name, ids]
|
183
|
+
end
|
184
|
+
|
185
|
+
####################################################################
|
186
|
+
# Private methods
|
187
|
+
####################################################################
|
188
|
+
private
|
189
|
+
|
190
|
+
#-------------------------------------------------------------------
|
191
|
+
# NIC and NIC Alias attributes to delete when importing template
|
192
|
+
# into marketplace.
|
193
|
+
# @param nic [XMLElement] to delete attributes from
|
194
|
+
#-------------------------------------------------------------------
|
195
|
+
def delete_nic_attributes(nic)
|
196
|
+
%w[NETWORK NETWORK_ID NETWORK_UNAME SECURITY_GROUPS].each do |a|
|
197
|
+
nic.delete_element(a)
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
#-------------------------------------------------------------------
|
202
|
+
# Create application template
|
203
|
+
#
|
204
|
+
# @param id [Image] OpenNebula Image
|
205
|
+
# @param idx [Integer] Image ID in VM template
|
206
|
+
#
|
207
|
+
# @return [String, String]
|
208
|
+
# - app template
|
209
|
+
# - content for VM template
|
210
|
+
#-------------------------------------------------------------------
|
211
|
+
def create_app_template(image, idx = 0)
|
212
|
+
# Wait until the image is READY to safe copy it to the MP
|
213
|
+
image.wait('READY') if Integer(image['STATE']) != 1
|
214
|
+
|
215
|
+
# Rename to avoid clashing names
|
216
|
+
app_name = "#{image['NAME']}-#{SecureRandom.hex[0..9]}"
|
217
|
+
|
218
|
+
dev_prefix = image['TEMPLATE/DEV_PREFIX']
|
219
|
+
img_type = image.type_str
|
220
|
+
|
221
|
+
template64 = "TYPE=#{img_type}\n"
|
222
|
+
template64 << "DEV_PREFIX=\"#{dev_prefix}\"\n" if dev_prefix
|
223
|
+
|
224
|
+
template = <<-EOT
|
225
|
+
NAME ="#{app_name}"
|
226
|
+
ORIGIN_ID ="#{image['ID']}"
|
227
|
+
TYPE ="IMAGE"
|
228
|
+
VERSION ="#{OpenNebula::VERSION}"
|
229
|
+
|
230
|
+
APPTEMPLATE64 = "#{Base64.strict_encode64(template64)}"
|
231
|
+
EOT
|
232
|
+
|
233
|
+
main_template = <<-EOT
|
234
|
+
DISK = [
|
235
|
+
NAME = "#{img_type}_#{idx}",
|
236
|
+
APP = "#{app_name}"
|
237
|
+
]
|
238
|
+
EOT
|
239
|
+
|
240
|
+
[template, main_template]
|
241
|
+
end
|
242
|
+
|
243
|
+
#-------------------------------------------------------------------
|
244
|
+
# Create application in marketplace
|
245
|
+
#
|
246
|
+
# @param template [String] APP template
|
247
|
+
# @param market_id [Integer] Marketplace to import it
|
248
|
+
#
|
249
|
+
# @return [Integer] APP ID
|
250
|
+
#-------------------------------------------------------------------
|
251
|
+
def create_app(template, market_id)
|
252
|
+
xml = OpenNebula::MarketPlaceApp.build_xml
|
253
|
+
app = OpenNebula::MarketPlaceApp.new(xml, @client)
|
254
|
+
|
255
|
+
rc = app.allocate(template, market_id)
|
256
|
+
|
257
|
+
return rc if OpenNebula.is_error?(rc)
|
258
|
+
|
259
|
+
app.id
|
260
|
+
end
|
261
|
+
|
262
|
+
#-------------------------------------------------------------------
|
263
|
+
# Delete IDS apps
|
264
|
+
#
|
265
|
+
# @param ids [Array] Apps IDs to delete
|
266
|
+
#-------------------------------------------------------------------
|
267
|
+
def rollback(ids)
|
268
|
+
ids.each do |id|
|
269
|
+
app = OpenNebula::MarketPlaceApp.new_with_id(id, @client)
|
270
|
+
|
271
|
+
app.delete
|
272
|
+
end
|
273
|
+
end
|
274
|
+
|
275
|
+
#-------------------------------------------------------------------
|
276
|
+
# Lookup OpenNebula Images by name or id. Lookup is made on a cached
|
277
|
+
# image pool.
|
278
|
+
#
|
279
|
+
# @param id [Integer, nil] Image id
|
280
|
+
# @param name [String] Image name
|
281
|
+
#
|
282
|
+
# @return [Image]
|
283
|
+
#-------------------------------------------------------------------
|
284
|
+
def image_lookup(disk)
|
285
|
+
# Image pool cache for image id lookup
|
286
|
+
if @image_lookup_cache.nil?
|
287
|
+
@image_lookup_cache = OpenNebula::ImagePool.new(@client)
|
288
|
+
@image_lookup_cache.info
|
289
|
+
end
|
290
|
+
|
291
|
+
# Find image information, from ID or NAME. NAME uses the
|
292
|
+
# specified user or template owner namespaces
|
293
|
+
image = nil
|
294
|
+
|
295
|
+
image_id = disk['IMAGE_ID']
|
296
|
+
|
297
|
+
if image_id
|
298
|
+
image = OpenNebula::Image.new_with_id(image_id, @client)
|
299
|
+
else
|
300
|
+
name = disk['IMAGE']
|
301
|
+
uname = disk['IMAGE_UNAME']
|
302
|
+
uname ||= self['UNAME']
|
303
|
+
|
304
|
+
name.gsub!('"', '')
|
305
|
+
image = @image_lookup_cache.find do |v|
|
306
|
+
v['NAME'] == name && v['UNAME'] == uname
|
307
|
+
end
|
308
|
+
|
309
|
+
return Error.new("Image #{image} not found") if image.nil?
|
310
|
+
end
|
311
|
+
|
312
|
+
rc = image.info
|
313
|
+
|
314
|
+
return rc if OpenNebula.is_error? rc
|
315
|
+
|
316
|
+
image.extend(OpenNebula::WaitExt)
|
317
|
+
|
318
|
+
image
|
319
|
+
end
|
320
|
+
|
321
|
+
end
|
322
|
+
end
|
323
|
+
|
324
|
+
end
|
325
|
+
# rubocop:enable Style/ClassAndModuleChildren
|
data/lib/opennebula/user.rb
CHANGED
@@ -34,7 +34,9 @@ module OpenNebula
|
|
34
34
|
:update => "user.update",
|
35
35
|
:chauth => "user.chauth",
|
36
36
|
:quota => "user.quota",
|
37
|
-
:login => "user.login"
|
37
|
+
:login => "user.login",
|
38
|
+
:enable => "user.enable",
|
39
|
+
:disable => "user.disable"
|
38
40
|
}
|
39
41
|
|
40
42
|
SELF = -1
|
@@ -128,6 +130,16 @@ module OpenNebula
|
|
128
130
|
super(USER_METHODS[:delete])
|
129
131
|
end
|
130
132
|
|
133
|
+
# Enable the User
|
134
|
+
def enable()
|
135
|
+
set_enabled(true)
|
136
|
+
end
|
137
|
+
|
138
|
+
# Disable the User
|
139
|
+
def disable()
|
140
|
+
set_enabled(false)
|
141
|
+
end
|
142
|
+
|
131
143
|
# Changes the password of the given User
|
132
144
|
#
|
133
145
|
# +password+ String containing the new password
|
@@ -230,5 +242,17 @@ module OpenNebula
|
|
230
242
|
all_groups = self.retrieve_elements("GROUPS/ID")
|
231
243
|
all_groups.collect! {|x| x.to_i}
|
232
244
|
end
|
245
|
+
|
246
|
+
private
|
247
|
+
|
248
|
+
def set_enabled(enabled)
|
249
|
+
return Error.new('ID not defined') if !@pe_id
|
250
|
+
|
251
|
+
rc = @client.call(USER_METHODS[:enable], @pe_id, enabled)
|
252
|
+
rc = nil if !OpenNebula.is_error?(rc)
|
253
|
+
|
254
|
+
return rc
|
255
|
+
end
|
256
|
+
|
233
257
|
end
|
234
258
|
end
|
@@ -14,6 +14,7 @@
|
|
14
14
|
# limitations under the License. #
|
15
15
|
#--------------------------------------------------------------------------- #
|
16
16
|
|
17
|
+
require 'opennebula/lockable_ext'
|
17
18
|
require 'opennebula/pool_element'
|
18
19
|
|
19
20
|
module OpenNebula
|
@@ -123,6 +124,9 @@ module OpenNebula
|
|
123
124
|
DISK_RESIZE_POWEROFF
|
124
125
|
DISK_RESIZE_UNDEPLOYED
|
125
126
|
HOTPLUG_NIC_POWEROFF
|
127
|
+
HOTPLUG_RESIZE
|
128
|
+
HOTPLUG_SAVEAS_UNDEPLOYED
|
129
|
+
HOTPLUG_SAVEAS_STOPPED
|
126
130
|
}
|
127
131
|
|
128
132
|
SHORT_VM_STATES={
|
@@ -204,7 +208,10 @@ module OpenNebula
|
|
204
208
|
"DISK_RESIZE" => "drsz",
|
205
209
|
"DISK_RESIZE_POWEROFF" => "drsz",
|
206
210
|
"DISK_RESIZE_UNDEPLOYED" => "drsz",
|
207
|
-
"HOTPLUG_NIC_POWEROFF" => "hotp"
|
211
|
+
"HOTPLUG_NIC_POWEROFF" => "hotp",
|
212
|
+
"HOTPLUG_RESIZE" => "hotp",
|
213
|
+
"HOTPLUG_SAVEAS_UNDEPLOYED" => "hotp",
|
214
|
+
"HOTPLUG_SAVEAS_STOPPED" => "hotp"
|
208
215
|
}
|
209
216
|
|
210
217
|
HISTORY_ACTION=%w{none migrate live-migrate shutdown shutdown-hard
|
@@ -271,6 +278,8 @@ module OpenNebula
|
|
271
278
|
|
272
279
|
# Class constructor
|
273
280
|
def initialize(xml, client)
|
281
|
+
LockableExt.make_lockable(self, VM_METHODS)
|
282
|
+
|
274
283
|
super(xml,client)
|
275
284
|
end
|
276
285
|
|
@@ -701,9 +710,9 @@ module OpenNebula
|
|
701
710
|
end
|
702
711
|
end
|
703
712
|
|
704
|
-
|
705
|
-
|
706
|
-
|
713
|
+
# Changes the attributes of a VM in power off, failure and undeploy
|
714
|
+
# states
|
715
|
+
# @param new_conf, string describing the new attributes. Each attribute
|
707
716
|
# will replace the existing ones or delete it if empty. Attributes that
|
708
717
|
# can be updated are: INPUT/{TYPE, BUS}; RAW/{TYPE, DATA, DATA_VMX},
|
709
718
|
# OS/{BOOT, BOOTLOADER, ARCH, MACHINE, KERNEL, INITRD},
|
@@ -711,20 +720,10 @@ module OpenNebula
|
|
711
720
|
# and GRAPHICS/{TYPE, LISTEN, PASSWD, KEYMAP}
|
712
721
|
# @return [nil, OpenNebula::Error] nil in case of success, Error
|
713
722
|
# otherwise
|
714
|
-
|
723
|
+
def updateconf(new_conf)
|
715
724
|
return call(VM_METHODS[:updateconf], @pe_id, new_conf)
|
716
725
|
end
|
717
726
|
|
718
|
-
# Lock a VM
|
719
|
-
def lock(level)
|
720
|
-
return call(VM_METHODS[:lock], @pe_id, level)
|
721
|
-
end
|
722
|
-
|
723
|
-
# Unlock a VM
|
724
|
-
def unlock()
|
725
|
-
return call(VM_METHODS[:unlock], @pe_id)
|
726
|
-
end
|
727
|
-
|
728
727
|
########################################################################
|
729
728
|
# Helpers to get VirtualMachine information
|
730
729
|
########################################################################
|
@@ -771,205 +770,24 @@ module OpenNebula
|
|
771
770
|
self['DEPLOY_ID']
|
772
771
|
end
|
773
772
|
|
774
|
-
|
775
|
-
|
776
|
-
#
|
777
|
-
# @param name [String] Name for the new Template
|
778
|
-
# @param name [true,false,nil] Optional, true to make the saved images
|
779
|
-
# persistent, false make them non-persistent
|
780
|
-
#
|
781
|
-
# @return [Integer, OpenNebula::Error] the new Template ID in case of
|
782
|
-
# success, error otherwise
|
783
|
-
REMOVE_VNET_ATTRS = %w{AR_ID BRIDGE CLUSTER_ID IP MAC TARGET NIC_ID
|
784
|
-
NETWORK_ID VN_MAD SECURITY_GROUPS VLAN_ID}
|
785
|
-
|
786
|
-
REMOVE_IMAGE_ATTRS = %w{DEV_PREFIX SOURCE ORIGINAL_SIZE SIZE
|
787
|
-
DISK_SNAPSHOT_TOTAL_SIZE DRIVER IMAGE_STATE SAVE CLONE READONLY
|
788
|
-
PERSISTENT TARGET ALLOW_ORPHANS CLONE_TARGET CLUSTER_ID DATASTORE
|
789
|
-
DATASTORE_ID DISK_ID DISK_TYPE IMAGE_ID IMAGE IMAGE_UNAME IMAGE_UID
|
790
|
-
LN_TARGET TM_MAD TYPE OPENNEBULA_MANAGED}
|
791
|
-
|
792
|
-
def save_as_template(name,description, persistent=nil)
|
793
|
-
img_ids = []
|
794
|
-
new_tid = nil
|
795
|
-
begin
|
796
|
-
rc = info()
|
797
|
-
raise if OpenNebula.is_error?(rc)
|
798
|
-
|
799
|
-
tid = self['TEMPLATE/TEMPLATE_ID']
|
800
|
-
if tid.nil? || tid.empty?
|
801
|
-
rc = Error.new('VM has no template to be saved')
|
802
|
-
raise
|
803
|
-
end
|
804
|
-
|
805
|
-
if state_str() != "POWEROFF"
|
806
|
-
rc = Error.new("VM state must be POWEROFF, "<<
|
807
|
-
"current state is #{state_str()}, #{lcm_state_str()}")
|
808
|
-
raise
|
809
|
-
end
|
810
|
-
|
811
|
-
# Clone the source template
|
812
|
-
rc = OpenNebula::Template.new_with_id(tid, @client).clone(name)
|
813
|
-
raise if OpenNebula.is_error?(rc)
|
814
|
-
|
815
|
-
new_tid = rc
|
773
|
+
def wait_state(state, timeout=120)
|
774
|
+
extend OpenNebula::WaitExt
|
816
775
|
|
817
|
-
|
818
|
-
replace = ""
|
819
|
-
if !description.nil?
|
820
|
-
replace << "DESCRIPTION = \"#{description}\"\n"
|
821
|
-
end
|
822
|
-
cpu = self['TEMPLATE/CPU']
|
823
|
-
if !cpu.nil? && !cpu.empty?
|
824
|
-
replace << "CPU = #{cpu}\n"
|
825
|
-
end
|
776
|
+
rc = wait2(state, 'LCM_INIT', timeout)
|
826
777
|
|
827
|
-
|
828
|
-
if !vcpu.nil? && !vcpu.empty?
|
829
|
-
replace << "VCPU = #{vcpu}\n"
|
830
|
-
end
|
831
|
-
|
832
|
-
mem = self['TEMPLATE/MEMORY']
|
833
|
-
if !mem.nil? && !mem.empty?
|
834
|
-
replace << "MEMORY = #{mem}\n"
|
835
|
-
end
|
778
|
+
return Error.new("Timeout expired for state #{state}.") unless rc
|
836
779
|
|
837
|
-
|
838
|
-
# While the previous snapshot is still in progress, we wait
|
839
|
-
# indefinitely
|
840
|
-
rc = info()
|
841
|
-
raise if OpenNebula.is_error?(rc)
|
842
|
-
|
843
|
-
steps = 0
|
844
|
-
while lcm_state_str() == "HOTPLUG_SAVEAS_POWEROFF"
|
845
|
-
if steps < 30
|
846
|
-
sleep 1
|
847
|
-
else
|
848
|
-
sleep 15
|
849
|
-
end
|
850
|
-
|
851
|
-
rc = info()
|
852
|
-
raise if OpenNebula.is_error?(rc)
|
853
|
-
|
854
|
-
steps += 1
|
855
|
-
end
|
856
|
-
|
857
|
-
# If the VM is not busy with a previous disk snapshot, we wait
|
858
|
-
# but this time with a timeout
|
859
|
-
rc = wait_state("POWEROFF")
|
860
|
-
raise if OpenNebula.is_error?(rc)
|
861
|
-
|
862
|
-
disk_id = disk["DISK_ID"]
|
863
|
-
if disk_id.nil? || disk_id.empty?
|
864
|
-
rc = Error.new('The DISK_ID is missing from the VM template')
|
865
|
-
raise
|
866
|
-
end
|
867
|
-
|
868
|
-
image_id = disk["IMAGE_ID"]
|
869
|
-
opennebula_managed = disk["OPENNEBULA_MANAGED"]
|
870
|
-
type = disk["TYPE"]
|
871
|
-
|
872
|
-
REMOVE_IMAGE_ATTRS.each do |attr|
|
873
|
-
disk.delete_element(attr)
|
874
|
-
end
|
875
|
-
|
876
|
-
if !image_id.nil? && !image_id.empty?
|
877
|
-
if type == 'CDROM'
|
878
|
-
replace << "DISK = [ IMAGE_ID = #{image_id}"
|
879
|
-
if opennebula_managed
|
880
|
-
replace << ", OPENNEBULA_MANAGED=#{opennebula_managed}"
|
881
|
-
end
|
882
|
-
replace << " ]\n"
|
883
|
-
else
|
884
|
-
rc = disk_saveas(disk_id.to_i,"#{name}-disk-#{disk_id}","",-1)
|
885
|
-
|
886
|
-
raise if OpenNebula.is_error?(rc)
|
887
|
-
|
888
|
-
if persistent == true
|
889
|
-
OpenNebula::Image.new_with_id(rc.to_i, @client).persistent()
|
890
|
-
end
|
891
|
-
|
892
|
-
img_ids << rc.to_i
|
893
|
-
|
894
|
-
disk_template = disk.template_like_str(".").tr("\n", ",\n")
|
895
|
-
|
896
|
-
if disk_template.empty?
|
897
|
-
replace << "DISK = [ IMAGE_ID = #{rc} ] \n"
|
898
|
-
else
|
899
|
-
replace << "DISK = [ IMAGE_ID = #{rc}, " <<
|
900
|
-
disk_template << " ] \n"
|
901
|
-
end
|
902
|
-
end
|
903
|
-
else
|
904
|
-
# Volatile disks cannot be saved, so the definition is copied
|
905
|
-
replace << self.template_like_str(
|
906
|
-
"TEMPLATE", true, "DISK[DISK_ID=#{disk_id}]") << "\n"
|
907
|
-
end
|
908
|
-
end
|
909
|
-
|
910
|
-
self.each('TEMPLATE/NIC') do |nic|
|
911
|
-
nic_id = nic["NIC_ID"]
|
912
|
-
|
913
|
-
if nic_id.nil? || nic_id.empty?
|
914
|
-
rc = Error.new('The NIC_ID is missing from the VM template')
|
915
|
-
raise
|
916
|
-
end
|
917
|
-
|
918
|
-
REMOVE_VNET_ATTRS.each do |attr|
|
919
|
-
# Remove every automatically generated value
|
920
|
-
# The vnet will be referenced via NAME + UNAME (if defined)
|
921
|
-
nic.delete_element(attr)
|
922
|
-
end
|
923
|
-
|
924
|
-
replace << "NIC = [ " << nic.template_like_str(".").tr("\n", ",\n") << " ] \n"
|
925
|
-
end
|
926
|
-
|
927
|
-
# Required by the Sunstone Cloud View
|
928
|
-
replace << "SAVED_TEMPLATE_ID = #{tid}\n"
|
929
|
-
|
930
|
-
new_tmpl = OpenNebula::Template.new_with_id(new_tid, @client)
|
931
|
-
|
932
|
-
rc = new_tmpl.update(replace, true)
|
933
|
-
raise if OpenNebula.is_error?(rc)
|
934
|
-
|
935
|
-
return new_tid
|
936
|
-
|
937
|
-
rescue
|
938
|
-
# Rollback. Delete the template and the images created
|
939
|
-
if !new_tid.nil?
|
940
|
-
new_tmpl = OpenNebula::Template.new_with_id(new_tid, @client)
|
941
|
-
new_tmpl.delete()
|
942
|
-
end
|
943
|
-
|
944
|
-
img_ids.each do |id|
|
945
|
-
img = OpenNebula::Image.new_with_id(id, @client)
|
946
|
-
img.delete()
|
947
|
-
end
|
948
|
-
|
949
|
-
return rc
|
950
|
-
end
|
780
|
+
true
|
951
781
|
end
|
952
782
|
|
953
|
-
def
|
954
|
-
|
955
|
-
lcm_state = ""
|
783
|
+
def wait_state2(state, lcm_state, timeout=120)
|
784
|
+
extend OpenNebula::WaitExt
|
956
785
|
|
957
|
-
timeout
|
958
|
-
rc = info()
|
959
|
-
return rc if OpenNebula.is_error?(rc)
|
960
|
-
|
961
|
-
vm_state = state_str()
|
962
|
-
lcm_state = lcm_state_str()
|
963
|
-
|
964
|
-
if vm_state == state
|
965
|
-
return true
|
966
|
-
end
|
786
|
+
rc = wait2(state, lcm_state, timeout)
|
967
787
|
|
968
|
-
|
969
|
-
end
|
788
|
+
return Error.new("Timeout expired for state #{state}.") unless rc
|
970
789
|
|
971
|
-
|
972
|
-
"VM is in state #{vm_state}, #{lcm_state}")
|
790
|
+
true
|
973
791
|
end
|
974
792
|
|
975
793
|
private
|