opennebula 6.6.2 → 6.7.80.pre

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -21,6 +21,7 @@ require 'cloud/CloudClient'
21
21
  include CloudCLI
22
22
 
23
23
  module Role
24
+
24
25
  # Actions that can be performed on the VMs of a given Role
25
26
  SCHEDULE_ACTIONS = [
26
27
  'terminate',
@@ -78,8 +79,9 @@ module Role
78
79
  # @param [String] state String number representing the state
79
80
  # @return the state string
80
81
  def self.state_str(state_number)
81
- return STATE_STR[state_number.to_i]
82
+ STATE_STR[state_number.to_i]
82
83
  end
84
+
83
85
  end
84
86
 
85
87
  module Service
@@ -126,19 +128,19 @@ module Service
126
128
  # @param [String] state String number representing the state
127
129
  # @return the state string
128
130
  def self.state_str(state_number)
129
- return STATE_STR[state_number.to_i]
131
+ STATE_STR[state_number.to_i]
130
132
  end
131
133
 
132
134
  # Build a json specifying an action
133
135
  # @param [String] perform action to be performed (e.g.: shutdown)
134
136
  # @param [Hash, nil] params contains the params for the action
135
137
  # @return [String] json representing the action
136
- def self.build_json_action(perform, params=nil)
137
- body = Hash.new
138
+ def self.build_json_action(perform, params = nil)
139
+ body = {}
138
140
  body['perform'] = perform
139
141
  body['params'] = params if params
140
142
 
141
- action = Hash.new
143
+ action = {}
142
144
  action['action'] = body
143
145
 
144
146
  JSON.pretty_generate action
@@ -148,83 +150,83 @@ module Service
148
150
 
149
151
  DEFAULT_OPTIONS = [
150
152
  ENDPOINT = {
151
- :name => "server",
152
- :short => "-s url",
153
- :large => "--server url",
153
+ :name => 'server',
154
+ :short => '-s url',
155
+ :large => '--server url',
154
156
  :format => String,
155
- :description => "Service endpoint"
157
+ :description => 'Service endpoint'
156
158
  },
157
159
  USERNAME={
158
- :name => "username",
159
- :short => "-u name",
160
- :large => "--username name",
160
+ :name => 'username',
161
+ :short => '-u name',
162
+ :large => '--username name',
161
163
  :format => String,
162
- :description => "User name"
164
+ :description => 'User name'
163
165
  },
164
166
  PASSWORD={
165
- :name => "password",
166
- :short => "-p pass",
167
- :large => "--password pass",
167
+ :name => 'password',
168
+ :short => '-p pass',
169
+ :large => '--password pass',
168
170
  :format => String,
169
- :description => "User password"
171
+ :description => 'User password'
170
172
  }
171
173
  ]
172
174
 
173
175
  JSON_FORMAT = {
174
- :name => "json",
175
- :short => "-j",
176
- :large => "--json",
177
- :description => "Print the resource in JSON"
176
+ :name => 'json',
177
+ :short => '-j',
178
+ :large => '--json',
179
+ :description => 'Print the resource in JSON'
178
180
  }
179
181
 
180
182
  TOP = {
181
- :name => "top",
182
- :short => "-t",
183
- :large => "--top",
184
- :description => "Top for the command"
183
+ :name => 'top',
184
+ :short => '-t',
185
+ :large => '--top',
186
+ :description => 'Top for the command'
185
187
  }
186
188
 
187
189
  PERIOD = {
188
- :name => "period",
189
- :short => "-p x",
190
- :large => "--period x",
190
+ :name => 'period',
191
+ :short => '-p x',
192
+ :large => '--period x',
191
193
  :format => Integer,
192
- :description => "Seconds between each group of actions"
194
+ :description => 'Seconds between each group of actions'
193
195
  }
194
196
 
195
197
  NUMBER = {
196
- :name => "number",
197
- :short => "-n x",
198
- :large => "--number x",
198
+ :name => 'number',
199
+ :short => '-n x',
200
+ :large => '--number x',
199
201
  :format => Integer,
200
- :description => "Number of VMs to apply the action to each period"
202
+ :description => 'Number of VMs to apply the action to each period'
201
203
  }
202
204
 
203
205
  FORCE = {
204
- :name => "force",
205
- :short => "-f",
206
- :large => "--force",
207
- :description => "Force the new cardinality even if it is outside the limits"
206
+ :name => 'force',
207
+ :short => '-f',
208
+ :large => '--force',
209
+ :description => 'Force the new cardinality even if it is outside the limits'
208
210
  }
209
211
 
210
212
  # Format helpers
211
213
 
212
- # def self.rname_to_id(name, poolname, options)
214
+ # def self.rname_to_id(name, poolname, options)
213
215
  def self.rname_to_id(name, poolname)
214
216
  return 0, name.to_i if name.match(/^[0123456789]+$/)
215
217
 
216
- client = Service::Client.new()
218
+ client = Service::Client.new
217
219
 
218
220
  resource_path = case poolname
219
- when "SERVICE" then "/service"
220
- when "SERVICE TEMPLATE" then "/service_template"
221
- end
221
+ when 'SERVICE' then '/service'
222
+ when 'SERVICE TEMPLATE' then '/service_template'
223
+ end
222
224
 
223
225
  response = client.get(resource_path)
224
226
 
225
- if CloudClient::is_error?(response)
227
+ if CloudClient.is_error?(response)
226
228
  return -1, "OpenNebula #{poolname} name not found," <<
227
- " use the ID instead"
229
+ ' use the ID instead'
228
230
  end
229
231
 
230
232
  pool = JSON.parse(response.body)
@@ -242,38 +244,32 @@ module Service
242
244
 
243
245
  objects = pool['DOCUMENT_POOL']['DOCUMENT'].select {|object| object['NAME'] == name }
244
246
 
245
- if objects.length>0
246
- if objects.length>1
247
- return -1, "There are multiple #{ename}s with name #{name}."
248
- else
249
- result = objects.first['ID']
250
- end
251
- else
252
- return -1, "#{ename} named #{name} not found."
253
- end
247
+ return -1, "#{ename} named #{name} not found." unless objects.length>0
248
+ return -1, "There are multiple #{ename}s with name #{name}." if objects.length>1
249
+
250
+ result = objects.first['ID']
254
251
 
255
- return 0, result
252
+ [0, result]
256
253
  end
257
254
 
258
255
  def self.list_to_id(names, poolname)
259
-
260
- client = Service::Client.new()
256
+ client = Service::Client.new
261
257
 
262
258
  resource_path = case poolname
263
- when "SERVICE" then "/service"
264
- when "SERVICE TEMPLATE" then "/service_template"
265
- end
259
+ when 'SERVICE' then '/service'
260
+ when 'SERVICE TEMPLATE' then '/service_template'
261
+ end
266
262
 
267
263
  response = client.get(resource_path)
268
264
 
269
- if CloudClient::is_error?(response)
265
+ if CloudClient.is_error?(response)
270
266
  return -1, "OpenNebula #{poolname} name not found," <<
271
- " use the ID instead"
267
+ ' use the ID instead'
272
268
  end
273
269
 
274
270
  pool = JSON.parse(response.body)
275
271
 
276
- result = names.split(',').collect { |name|
272
+ result = names.split(',').collect do |name|
277
273
  if name.match(/^[0123456789]+$/)
278
274
  name.to_i
279
275
  else
@@ -285,9 +281,9 @@ module Service
285
281
 
286
282
  rc[1]
287
283
  end
288
- }
284
+ end
289
285
 
290
- return 0, result
286
+ [0, result]
291
287
  end
292
288
 
293
289
  def self.list_to_id_desc(poolname)
@@ -304,8 +300,8 @@ module Service
304
300
  ids.each do |id|
305
301
  response = block.call(id) if block_given?
306
302
 
307
- if CloudClient::is_error?(response)
308
- puts response.to_s
303
+ if CloudClient.is_error?(response)
304
+ puts response
309
305
  exit_code = response.code.to_i
310
306
  end
311
307
  end
@@ -321,16 +317,29 @@ module Service
321
317
  exit_code = 0
322
318
  response = block.call(id) if block_given?
323
319
 
324
- if CloudClient::is_error?(response)
325
- puts response.to_s
320
+ if CloudClient.is_error?(response)
321
+ puts response
326
322
  exit_code = response.code.to_i
327
323
  end
328
324
 
329
325
  exit_code
330
326
  end
331
327
 
328
+ #
329
+ # Interface to OneFlow REST API through a Ruby client
330
+ #
332
331
  class Client
333
- def initialize(opts={})
332
+
333
+ #
334
+ # The options are read from ENV and FS if not passed
335
+ #
336
+ # @param [Hash] opts Required configuration to interact with OneFlow
337
+ # @option opts [String] :url Endpoint where OneFlow is running. Defaults to 'http://localhost:2474'
338
+ # @option opts [String] :username OpenNebula user
339
+ # @option opts [String] :password OpenNebula user password
340
+ # @option opts [String] :user_agent Defaults to Ruby. Oneflow will behave accordingly.
341
+ #
342
+ def initialize(opts = {})
334
343
  endpoint = '/.one/oneflow_endpoint'
335
344
  @username = opts[:username] || ENV['ONEFLOW_USER']
336
345
  @password = opts[:password] || ENV['ONEFLOW_PASSWORD']
@@ -339,23 +348,23 @@ module Service
339
348
  url = opts[:url]
340
349
  elsif ENV['ONEFLOW_URL']
341
350
  url = ENV['ONEFLOW_URL']
342
- elsif ENV['HOME'] && File.exists?(ENV['HOME'] + endpoint)
351
+ elsif ENV['HOME'] && File.exist?(ENV['HOME'] + endpoint)
343
352
  url = File.read(ENV['HOME'] + endpoint).strip
344
- elsif File.exists?('/var/lib/one/.one/oneflow_endpoint')
353
+ elsif File.exist?('/var/lib/one/.one/oneflow_endpoint')
345
354
  url = File.read('/var/lib/one/.one/oneflow_endpoint').strip
346
355
  else
347
356
  url = 'http://localhost:2474'
348
357
  end
349
358
 
350
359
  if @username.nil? && @password.nil?
351
- if ENV["ONE_AUTH"] and !ENV["ONE_AUTH"].empty? and File.file?(ENV["ONE_AUTH"])
352
- one_auth = File.read(ENV["ONE_AUTH"])
353
- elsif ENV["HOME"] and File.file?(ENV["HOME"]+"/.one/one_auth")
354
- one_auth = File.read(ENV["HOME"]+"/.one/one_auth")
355
- elsif File.file?("/var/lib/one/.one/one_auth")
356
- one_auth = File.read("/var/lib/one/.one/one_auth")
360
+ if ENV['ONE_AUTH'] and !ENV['ONE_AUTH'].empty? and File.file?(ENV['ONE_AUTH'])
361
+ one_auth = File.read(ENV['ONE_AUTH'])
362
+ elsif ENV['HOME'] and File.file?(ENV['HOME']+'/.one/one_auth')
363
+ one_auth = File.read(ENV['HOME']+'/.one/one_auth')
364
+ elsif File.file?('/var/lib/one/.one/one_auth')
365
+ one_auth = File.read('/var/lib/one/.one/one_auth')
357
366
  else
358
- raise "ONE_AUTH file not present"
367
+ raise 'ONE_AUTH file not present'
359
368
  end
360
369
 
361
370
  one_auth = one_auth.rstrip
@@ -366,37 +375,37 @@ module Service
366
375
  @uri = URI.parse(url)
367
376
 
368
377
  @user_agent = "OpenNebula #{CloudClient::VERSION} " <<
369
- "(#{opts[:user_agent]||"Ruby"})"
378
+ "(#{opts[:user_agent]||'Ruby'})"
370
379
 
371
380
  @host = nil
372
381
  @port = nil
373
382
 
374
- if ENV['http_proxy']
375
- uri_proxy = URI.parse(ENV['http_proxy'])
376
- flag = false
377
-
378
- # Check if we need to bypass the proxy
379
- if ENV['no_proxy']
380
- ENV['no_proxy'].split(',').each do |item|
381
- item = item.rstrip.lstrip
382
-
383
- unless (IPAddress @uri.host rescue nil).nil?
384
- unless (IPAddress item rescue nil).nil?
385
- flag |= IPAddress(item).include? IPAddress(@uri.host)
386
- end
387
- else
388
- if (IPAddress(item) rescue nil).nil?
389
- flag |= (item == @uri.host)
390
- end
383
+ return unless ENV['http_proxy']
384
+
385
+ uri_proxy = URI.parse(ENV['http_proxy'])
386
+ flag = false
387
+
388
+ #  Check if we need to bypass the proxy
389
+ if ENV['no_proxy']
390
+ ENV['no_proxy'].split(',').each do |item|
391
+ item = item.strip
392
+
393
+ if (IPAddress @uri.host rescue nil).nil?
394
+ if (IPAddress(item) rescue nil).nil?
395
+ flag |= (item == @uri.host)
396
+ end
397
+ else
398
+ unless (IPAddress item rescue nil).nil?
399
+ flag |= IPAddress(item).include? IPAddress(@uri.host)
391
400
  end
392
401
  end
393
402
  end
394
-
395
- unless flag
396
- @host = uri_proxy.host
397
- @port = uri_proxy.port
398
- end
399
403
  end
404
+
405
+ return if flag
406
+
407
+ @host = uri_proxy.host
408
+ @port = uri_proxy.port
400
409
  end
401
410
 
402
411
  def set_content_type(content_type)
@@ -420,10 +429,8 @@ module Service
420
429
  req = Net::HTTP::Proxy(@host, @port)::Post.new(path)
421
430
  req.body = body
422
431
 
423
- if path.start_with?('/service_template')
424
- unless @content_type.nil?
425
- req.content_type = @content_type
426
- end
432
+ if path.start_with?('/service_template') && !@content_type.nil?
433
+ req.content_type = @content_type
427
434
  end
428
435
  do_request(req)
429
436
  end
@@ -454,11 +461,11 @@ module Service
454
461
 
455
462
  req['User-Agent'] = @user_agent
456
463
 
457
- res = CloudClient::http_start(@uri, @timeout) do |http|
464
+ CloudClient.http_start(@uri, @timeout) do |http|
458
465
  http.request(req)
459
466
  end
460
-
461
- res
462
467
  end
468
+
463
469
  end
470
+
464
471
  end
@@ -17,11 +17,13 @@
17
17
  require 'opennebula/pool'
18
18
 
19
19
  module OpenNebula
20
+
20
21
  # The PoolElement Class represents a generic element of a Pool in
21
22
  # XML format
22
23
  class PoolElement < XMLElement
23
24
 
24
- protected
25
+ protected
26
+
25
27
  # node:: _XML_is a XML element that represents the Pool element
26
28
  # client:: _Client_ represents a XML-RPC connection
27
29
  def initialize(node, client)
@@ -49,12 +51,12 @@ module OpenNebula
49
51
  # @return [nil, OpenNebula::Error] nil in case of success, Error
50
52
  # otherwise
51
53
  def call(xml_method, *args)
52
- return Error.new('ID not defined') if !@pe_id
54
+ return Error.new('ID not defined') unless @pe_id
53
55
 
54
56
  rc = @client.call(xml_method, *args)
55
- rc = nil if !OpenNebula.is_error?(rc)
57
+ rc = nil unless OpenNebula.is_error?(rc)
56
58
 
57
- return rc
59
+ rc
58
60
  end
59
61
 
60
62
  # Calls to the corresponding info method to retreive the element
@@ -66,19 +68,19 @@ module OpenNebula
66
68
  # @return [nil, OpenNebula::Error] nil in case of success, Error
67
69
  # otherwise
68
70
  def info(xml_method, root_element, decrypt = false)
69
- return Error.new('ID not defined') if !@pe_id
71
+ return Error.new('ID not defined') unless @pe_id
70
72
 
71
73
  rc = @client.call(xml_method, @pe_id, decrypt)
72
74
 
73
75
  if !OpenNebula.is_error?(rc)
74
76
  initialize_xml(rc, root_element)
75
- rc = nil
77
+ rc = nil
76
78
 
77
79
  @pe_id = self['ID'].to_i if self['ID']
78
80
  @name = self['NAME'] if self['NAME']
79
81
  end
80
82
 
81
- return rc
83
+ rc
82
84
  end
83
85
 
84
86
  # Calls to the corresponding allocate method to create a new element
@@ -97,7 +99,7 @@ module OpenNebula
97
99
  rc = nil
98
100
  end
99
101
 
100
- return rc
102
+ rc
101
103
  end
102
104
 
103
105
  # Calls to the corresponding update method to modify
@@ -111,10 +113,10 @@ module OpenNebula
111
113
  # otherwise
112
114
  def update(xml_method, new_template, *args)
113
115
  if new_template.nil?
114
- return Error.new("Wrong argument", Error::EXML_RPC_CALL)
116
+ return Error.new('Wrong argument', Error::EXML_RPC_CALL)
115
117
  end
116
118
 
117
- return call(xml_method, @pe_id, new_template, *args)
119
+ call(xml_method, @pe_id, new_template, *args)
118
120
  end
119
121
 
120
122
  # Calls to the corresponding delete method to remove this element
@@ -125,7 +127,7 @@ module OpenNebula
125
127
  # @return [nil, OpenNebula::Error] nil in case of success, Error
126
128
  # otherwise
127
129
  def delete(xml_method)
128
- return call(xml_method,@pe_id)
130
+ call(xml_method, @pe_id)
129
131
  end
130
132
 
131
133
  # Calls to the corresponding chown method to modify
@@ -138,7 +140,7 @@ module OpenNebula
138
140
  # @return [nil, OpenNebula::Error] nil in case of success, Error
139
141
  # otherwise
140
142
  def chown(xml_method, uid, gid)
141
- return call(xml_method, @pe_id, uid, gid)
143
+ call(xml_method, @pe_id, uid, gid)
142
144
  end
143
145
 
144
146
  # Calls to the corresponding chmod method to modify
@@ -149,7 +151,7 @@ module OpenNebula
149
151
  #
150
152
  # @return [nil, OpenNebula::Error] nil in case of success, Error
151
153
  # otherwise
152
- def chmod_octet(xml_method, octet)
154
+ def chmod_octet(_xml_method, octet)
153
155
  owner_u = octet[0..0].to_i & 4 != 0 ? 1 : 0
154
156
  owner_m = octet[0..0].to_i & 2 != 0 ? 1 : 0
155
157
  owner_a = octet[0..0].to_i & 1 != 0 ? 1 : 0
@@ -161,7 +163,7 @@ module OpenNebula
161
163
  other_a = octet[2..2].to_i & 1 != 0 ? 1 : 0
162
164
 
163
165
  chmod(owner_u, owner_m, owner_a, group_u, group_m, group_a, other_u,
164
- other_m, other_a)
166
+ other_m, other_a)
165
167
  end
166
168
 
167
169
  # Calls to the corresponding chmod method to modify
@@ -173,13 +175,12 @@ module OpenNebula
173
175
  # @return [nil, OpenNebula::Error] nil in case of success, Error
174
176
  # otherwise
175
177
  def chmod(xml_method, owner_u, owner_m, owner_a, group_u, group_m, group_a, other_u,
176
- other_m, other_a)
177
- return call(xml_method, @pe_id, owner_u, owner_m,
178
- owner_a, group_u, group_m, group_a, other_u,
179
- other_m, other_a)
178
+ other_m, other_a)
179
+ call(xml_method, @pe_id, owner_u, owner_m,
180
+ owner_a, group_u, group_m, group_a, other_u,
181
+ other_m, other_a)
180
182
  end
181
183
 
182
-
183
184
  # Retrieves this Element's monitoring data from OpenNebula
184
185
  #
185
186
  # @param [String] xml_method the name of the XML-RPC method
@@ -189,28 +190,27 @@ module OpenNebula
189
190
  # @return [Hash<String, Array<Array<int>>, OpenNebula::Error] Hash with
190
191
  # the requested xpath expressions, and an Array of [timestamp, value].
191
192
  def monitoring(xml_method, xpaths)
192
- return Error.new('ID not defined') if !@pe_id
193
+ return Error.new('ID not defined') unless @pe_id
193
194
 
194
195
  rc = @client.call(xml_method, @pe_id)
195
196
 
196
- if ( OpenNebula.is_error?(rc) )
197
+ if OpenNebula.is_error?(rc)
197
198
  return rc
198
199
  end
199
200
 
200
201
  xmldoc = XMLElement.new
201
202
  xmldoc.initialize_xml(rc, 'MONITORING_DATA')
202
203
 
203
-
204
- return OpenNebula.process_monitoring(xmldoc, @pe_id, xpaths)
204
+ OpenNebula.process_monitoring(xmldoc, @pe_id, xpaths)
205
205
  end
206
206
 
207
- public
207
+ public
208
208
 
209
209
  # Creates new element specifying its id
210
210
  # id:: identifyier of the element
211
211
  # client:: initialized OpenNebula::Client object
212
- def self.new_with_id(id, client=nil)
213
- self.new(self.build_xml(id), client)
212
+ def self.new_with_id(id, client = nil)
213
+ new(build_xml(id), client)
214
214
  end
215
215
 
216
216
  # Returns element identifier
@@ -221,16 +221,14 @@ module OpenNebula
221
221
 
222
222
  # Gets element name
223
223
  # [return] _String_ the PoolElement name
224
- def name
225
- @name
226
- end
224
+ attr_reader :name
227
225
 
228
226
  # DO NOT USE - ONLY REXML BACKEND
229
227
  def to_str
230
- str = ""
231
- REXML::Formatters::Pretty.new(1).write(@xml,str)
228
+ str = ''
229
+ REXML::Formatters::Pretty.new(1).write(@xml, str)
232
230
 
233
- return str
231
+ str
234
232
  end
235
233
 
236
234
  # Replace the xml pointed by xpath using a Hash object
@@ -240,18 +238,19 @@ module OpenNebula
240
238
  # @param [Hash] options object containing pair key-value
241
239
  #
242
240
  # @returns the new xml representation
243
- def replace(opts, xpath = "TEMPLATE")
244
- if self[xpath]
245
- opts.each do |att, value|
246
- xpath_u = xpath+"/#{att}"
247
- docs = retrieve_xmlelements(xpath_u)
248
- if docs.size == 1
249
- docs[0].set_content(value)
250
- end
241
+ def replace(opts, xpath = 'TEMPLATE')
242
+ return unless self[xpath]
243
+
244
+ opts.each do |att, value|
245
+ xpath_u = xpath+"/#{att}"
246
+ docs = retrieve_xmlelements(xpath_u)
247
+ if docs.size == 1
248
+ docs[0].set_content(value)
251
249
  end
252
- update(template_like_str(xpath))
253
250
  end
251
+ update(template_like_str(xpath))
254
252
  end
253
+
255
254
  end
256
255
 
257
256
  # Processes the monitoring data in XML returned by OpenNebula
@@ -266,20 +265,22 @@ module OpenNebula
266
265
  def self.process_monitoring(xmldoc, oid, xpath_expressions)
267
266
  hash = {}
268
267
  timestamps = xmldoc.retrieve_elements(
269
- "/MONITORING_DATA/MONITORING[ID=#{oid}]/TIMESTAMP")
268
+ "/MONITORING_DATA/MONITORING[ID=#{oid}]/TIMESTAMP"
269
+ )
270
270
 
271
- xpath_expressions.each { |xpath|
271
+ xpath_expressions.each do |xpath|
272
272
  xpath_values = xmldoc.retrieve_elements(
273
- "/MONITORING_DATA/MONITORING[ID=#{oid}]/#{xpath}")
273
+ "/MONITORING_DATA/MONITORING[ID=#{oid}]/#{xpath}"
274
+ )
274
275
 
275
- if ( xpath_values.nil? )
276
+ if xpath_values.nil?
276
277
  hash[xpath] = []
277
278
  else
278
279
  hash[xpath] = timestamps.zip(xpath_values)
279
280
  end
280
- }
281
+ end
281
282
 
282
- return hash
283
+ hash
283
284
  end
284
285
 
285
286
  end