opennebula-cli 3.8.0 → 3.9.0.beta

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.
@@ -20,60 +20,6 @@ class OneQuotaHelper
20
20
 
21
21
  EDITOR_PATH='/usr/bin/vi'
22
22
 
23
- #---------------------------------------------------------------------------
24
- # Tables to format user quotas
25
- #---------------------------------------------------------------------------
26
- TABLE_DS = CLIHelper::ShowTable.new(nil, self) do
27
- column :"DATASTORE ID", "", :size=>12 do |d|
28
- d["ID"] if !d.nil?
29
- end
30
-
31
- column :"IMAGES", "", :right, :size=>20 do |d|
32
- "%8d / %8d" % [d["IMAGES_USED"], d["IMAGES"]] if !d.nil?
33
- end
34
-
35
- column :"SIZE", "", :right, :size=>19 do |d|
36
- "%8s / %8s" % [OpenNebulaHelper.unit_to_str(d["SIZE_USED"].to_i,{},"M"),
37
- OpenNebulaHelper.unit_to_str(d["SIZE"].to_i,{},"M")] if !d.nil?
38
- end
39
- end
40
-
41
- TABLE_NET = CLIHelper::ShowTable.new(nil, self) do
42
- column :"NETWORK ID", "", :size=>12 do |d|
43
- d["ID"] if !d.nil?
44
- end
45
-
46
- column :"LEASES", "", :right, :size=>20 do |d|
47
- "%8d / %8d" % [d["LEASES_USED"], d["LEASES"]] if !d.nil?
48
- end
49
- end
50
-
51
- TABLE_VM = CLIHelper::ShowTable.new(nil, self) do
52
-
53
- column :"NUMBER OF VMS", "", :right, :size=>20 do |d|
54
- "%8d / %8d" % [d["VMS_USED"], d["VMS"]] if !d.nil?
55
- end
56
-
57
- column :"MEMORY", "", :right, :size=>20 do |d|
58
- "%8s / %8s" % [OpenNebulaHelper.unit_to_str(d["MEMORY_USED"].to_i,{},"M"),
59
- OpenNebulaHelper.unit_to_str(d["MEMORY"].to_i,{},"M")] if !d.nil?
60
- end
61
-
62
- column :"CPU", "", :right, :size=>20 do |d|
63
- "%8.2f / %8.2f" % [d["CPU_USED"], d["CPU"]] if !d.nil?
64
- end
65
- end
66
-
67
- TABLE_IMG = CLIHelper::ShowTable.new(nil, self) do
68
- column :"IMAGE ID", "", :size=>12 do |d|
69
- d["ID"] if !d.nil?
70
- end
71
-
72
- column :"RUNNING VMS", "", :right, :size=>20 do |d|
73
- "%8d / %8d" % [d["RVMS_USED"], d["RVMS"]] if !d.nil?
74
- end
75
- end
76
-
77
23
  HELP_QUOTA = <<-EOT.unindent
78
24
  #-----------------------------------------------------------------------
79
25
  # Supported quota limits:
@@ -96,18 +42,22 @@ class OneQuotaHelper
96
42
  # ]
97
43
  #
98
44
  # IMAGE = [
99
- # ID = <ID of the image>
100
- # RVMS = <Max. number of VMs using the image>
45
+ # ID = <ID of the image>
46
+ # RVMS = <Max. number of VMs using the image>
101
47
  # ]
102
48
  #
103
- # In any quota 0 means unlimited. The usage counters "*_USED" are
104
- # shown for information purposes and will NOT be modified.
49
+ # In any quota:
50
+ # -1 means use the default limit ('defaultquota' command)
51
+ # 0 means unlimited.
52
+ #
53
+ # The usage counters "*_USED" are shown for information
54
+ # purposes and will NOT be modified.
105
55
  #-----------------------------------------------------------------------
106
56
  EOT
107
57
 
108
58
  # Edits the quota template of a resource
109
- # @param resource [PoolElement] to get the current info from
110
- # @param path [String] path to the new contents. If nil a editor will be
59
+ # @param [XMLElement] resource to get the current info from
60
+ # @param [String] path to the new contents. If nil a editor will be
111
61
  # used
112
62
  # @return [String] contents of the new quotas
113
63
  def self.set_quota(resource, path)
@@ -119,13 +69,6 @@ class OneQuotaHelper
119
69
  tmp = Tempfile.new('one-cli')
120
70
  path = tmp.path
121
71
 
122
- rc = resource.info
123
-
124
- if OpenNebula.is_error?(rc)
125
- puts rc.message
126
- exit -1
127
- end
128
-
129
72
  tmp << HELP_QUOTA
130
73
  tmp << resource.template_like_str("DATASTORE_QUOTA") << "\n"
131
74
  tmp << resource.template_like_str("VM_QUOTA") << "\n"
@@ -220,8 +163,9 @@ class OneQuotaHelper
220
163
 
221
164
  # Outputs formated quota information to stdout
222
165
  # @param qh [Hash] with the quotas for a given resource
166
+ # @param default_quotas_hash [XMLElement] with the default quota limits
223
167
  #
224
- def self.format_quota(qh)
168
+ def format_quota(qh, default_quotas)
225
169
  str_h1="%-80s"
226
170
 
227
171
  puts
@@ -230,27 +174,145 @@ class OneQuotaHelper
230
174
 
231
175
  puts
232
176
 
177
+ @default_quotas = default_quotas
178
+
233
179
  vm_quotas = [qh['VM_QUOTA']['VM']].flatten
180
+
234
181
  if !vm_quotas[0].nil?
235
- TABLE_VM.show(vm_quotas, {})
182
+ CLIHelper::ShowTable.new(nil, self) do
183
+ column :"NUMBER OF VMS", "", :right, :size=>20 do |d|
184
+ if !d.nil?
185
+ elem = 'VMS'
186
+ limit = d[elem]
187
+ limit = helper.get_default_limit(
188
+ limit, "VM_QUOTA/VM/#{elem}")
189
+
190
+ "%8d / %8d" % [d["VMS_USED"], limit]
191
+ end
192
+ end
193
+
194
+ column :"MEMORY", "", :right, :size=>20 do |d|
195
+ if !d.nil?
196
+ elem = 'MEMORY'
197
+ limit = d[elem]
198
+ limit = helper.get_default_limit(
199
+ limit, "VM_QUOTA/VM/#{elem}")
200
+
201
+ "%8s / %8s" % [
202
+ OpenNebulaHelper.unit_to_str(d["MEMORY_USED"].to_i,{},"M"),
203
+ OpenNebulaHelper.unit_to_str(limit.to_i,{},"M")
204
+ ]
205
+ end
206
+ end
207
+
208
+ column :"CPU", "", :right, :size=>20 do |d|
209
+ if !d.nil?
210
+ elem = 'CPU'
211
+ limit = d[elem]
212
+ limit = helper.get_default_limit(
213
+ limit, "VM_QUOTA/VM/#{elem}")
214
+
215
+ "%8.2f / %8.2f" % [d["CPU_USED"], limit]
216
+ end
217
+ end
218
+ end.show(vm_quotas, {})
219
+
236
220
  puts
237
221
  end
238
222
 
239
223
  ds_quotas = [qh['DATASTORE_QUOTA']['DATASTORE']].flatten
224
+
240
225
  if !ds_quotas[0].nil?
241
- TABLE_DS.show(ds_quotas, {})
226
+ CLIHelper::ShowTable.new(nil, self) do
227
+ column :"DATASTORE ID", "", :size=>12 do |d|
228
+ d["ID"] if !d.nil?
229
+ end
230
+
231
+ column :"IMAGES", "", :right, :size=>20 do |d|
232
+ if !d.nil?
233
+ elem = 'IMAGES'
234
+ limit = d[elem]
235
+ limit = helper.get_default_limit(
236
+ limit, "DATASTORE_QUOTA/DATASTORE[ID=#{d['ID']}]/#{elem}")
237
+
238
+ "%8d / %8d" % [d["IMAGES_USED"], limit]
239
+ end
240
+ end
241
+
242
+ column :"SIZE", "", :right, :size=>19 do |d|
243
+ if !d.nil?
244
+ elem = 'SIZE'
245
+ limit = d[elem]
246
+ limit = helper.get_default_limit(
247
+ limit, "DATASTORE_QUOTA/DATASTORE[ID=#{d['ID']}]/#{elem}")
248
+
249
+ "%8s / %8s" % [
250
+ OpenNebulaHelper.unit_to_str(d["SIZE_USED"].to_i,{},"M"),
251
+ OpenNebulaHelper.unit_to_str(limit.to_i,{},"M")
252
+ ]
253
+ end
254
+ end
255
+ end.show(ds_quotas, {})
256
+
242
257
  puts
243
258
  end
244
259
 
245
260
  net_quotas = [qh['NETWORK_QUOTA']['NETWORK']].flatten
261
+
246
262
  if !net_quotas[0].nil?
247
- TABLE_NET.show(net_quotas, {})
263
+ CLIHelper::ShowTable.new(nil, self) do
264
+ column :"NETWORK ID", "", :size=>12 do |d|
265
+ d["ID"] if !d.nil?
266
+ end
267
+
268
+ column :"LEASES", "", :right, :size=>20 do |d|
269
+ if !d.nil?
270
+ elem = 'LEASES'
271
+ limit = d[elem]
272
+ limit = helper.get_default_limit(
273
+ limit, "NETWORK_QUOTA/NETWORK[ID=#{d['ID']}]/#{elem}")
274
+
275
+ "%8d / %8d" % [d["LEASES_USED"], limit]
276
+ end
277
+ end
278
+ end.show(net_quotas, {})
279
+
248
280
  puts
249
281
  end
250
282
 
251
283
  image_quotas = [qh['IMAGE_QUOTA']['IMAGE']].flatten
284
+
252
285
  if !image_quotas[0].nil?
253
- TABLE_IMG.show(image_quotas, {})
286
+ CLIHelper::ShowTable.new(nil, self) do
287
+ column :"IMAGE ID", "", :size=>12 do |d|
288
+ d["ID"] if !d.nil?
289
+ end
290
+
291
+ column :"RUNNING VMS", "", :right, :size=>20 do |d|
292
+ if !d.nil?
293
+ elem = 'RVMS'
294
+ limit = d[elem]
295
+ limit = helper.get_default_limit(
296
+ limit, "IMAGE_QUOTA/IMAGE[ID=#{d['ID']}]/RVMS")
297
+
298
+ "%8d / %8d" % [d["RVMS_USED"], limit]
299
+ end
300
+ end
301
+ end.show(image_quotas, {})
254
302
  end
255
303
  end
304
+
305
+ def get_default_limit(limit, xpath)
306
+ if limit == "-1"
307
+ if !@default_quotas.nil?
308
+ limit = @default_quotas[xpath]
309
+
310
+ limit = "0" if limit.nil? || limit == ""
311
+ else
312
+ limit = "0"
313
+ end
314
+ end
315
+
316
+ return limit
317
+ end
256
318
  end
@@ -60,10 +60,10 @@ class OneUserHelper < OpenNebulaHelper::OneHelper
60
60
  return -1, "You have to specify the --key option"
61
61
  end
62
62
 
63
- require 'ssh_auth'
63
+ require 'opennebula/ssh_auth'
64
64
 
65
65
  begin
66
- auth = SshAuth.new(:private_key=>options[:key])
66
+ auth = OpenNebula::SshAuth.new(:private_key=>options[:key])
67
67
  rescue Exception => e
68
68
  return -1, e.message
69
69
  end
@@ -74,11 +74,11 @@ class OneUserHelper < OpenNebulaHelper::OneHelper
74
74
  return -1, "You have to specify the --cert option"
75
75
  end
76
76
 
77
- require 'x509_auth'
77
+ require 'opennebula/x509_auth'
78
78
 
79
79
  begin
80
80
  cert = [File.read(options[:cert])]
81
- auth = X509Auth.new(:certs_pem=>cert)
81
+ auth = OpenNebula::X509Auth.new(:certs_pem=>cert)
82
82
  rescue Exception => e
83
83
  return -1, e.message
84
84
  end
@@ -92,17 +92,17 @@ class OneUserHelper < OpenNebulaHelper::OneHelper
92
92
  def self.login(username, options)
93
93
  case options[:driver]
94
94
  when OpenNebula::User::SSH_AUTH
95
- require 'ssh_auth'
95
+ require 'opennebula/ssh_auth'
96
96
 
97
97
  options[:key] ||= ENV['HOME']+'/.ssh/id_rsa'
98
98
 
99
99
  begin
100
- auth = SshAuth.new(:private_key=>options[:key])
100
+ auth = OpenNebula::SshAuth.new(:private_key=>options[:key])
101
101
  rescue Exception => e
102
102
  return -1, e.message
103
103
  end
104
104
  when OpenNebula::User::X509_AUTH
105
- require 'x509_auth'
105
+ require 'opennebula/x509_auth'
106
106
 
107
107
  options[:cert] ||= ENV['X509_USER_CERT']
108
108
  options[:key] ||= ENV['X509_USER_KEY']
@@ -111,12 +111,12 @@ class OneUserHelper < OpenNebulaHelper::OneHelper
111
111
  certs = [File.read(options[:cert])]
112
112
  key = File.read(options[:key])
113
113
 
114
- auth = X509Auth.new(:certs_pem=>certs, :key_pem=>key)
114
+ auth = OpenNebula::X509Auth.new(:certs_pem=>certs, :key_pem=>key)
115
115
  rescue Exception => e
116
116
  return -1, e.message
117
117
  end
118
118
  when OpenNebula::User::X509_PROXY_AUTH
119
- require 'x509_auth'
119
+ require 'opennebula/x509_auth'
120
120
 
121
121
  options[:proxy] ||= ENV['X509_PROXY_CERT']
122
122
 
@@ -129,7 +129,7 @@ class OneUserHelper < OpenNebulaHelper::OneHelper
129
129
  rc = proxy.match(/(-+BEGIN RSA PRIVATE KEY-+\n[^-]*\n-+END RSA PRIVATE KEY-+)/)
130
130
  key= rc[1]
131
131
 
132
- auth = X509Auth.new(:certs_pem=>certs, :key_pem=>key)
132
+ auth = OpenNebula::X509Auth.new(:certs_pem=>certs, :key_pem=>key)
133
133
  rescue => e
134
134
  return -1, e.message
135
135
  end
@@ -147,6 +147,13 @@ class OneUserHelper < OpenNebulaHelper::OneHelper
147
147
  def format_pool(options)
148
148
  config_file = self.class.table_conf
149
149
 
150
+ system = System.new(@client)
151
+ default_quotas = system.get_user_quotas()
152
+
153
+ if OpenNebula::is_error?(default_quotas)
154
+ raise "Error retrieving the default user quotas: #{default_quotas.message}"
155
+ end
156
+
150
157
  table = CLIHelper::ShowTable.new(config_file, self) do
151
158
  column :ID, "ONE identifier for the User", :size=>4 do |d|
152
159
  d["ID"]
@@ -166,7 +173,14 @@ class OneUserHelper < OpenNebulaHelper::OneHelper
166
173
 
167
174
  column :VMS , "Number of VMS", :size=>9 do |d|
168
175
  if d.has_key?('VM_QUOTA') and d['VM_QUOTA'].has_key?('VM')
169
- "%3d / %3d" % [d['VM_QUOTA']['VM']["VMS_USED"], d['VM_QUOTA']['VM']["VMS"]]
176
+ limit = d['VM_QUOTA']['VM']["VMS"]
177
+
178
+ if limit == "-1"
179
+ limit = default_quotas['VM_QUOTA/VM/VMS']
180
+ limit = "0" if limit.nil? || limit == ""
181
+ end
182
+
183
+ "%3d / %3d" % [d['VM_QUOTA']['VM']["VMS_USED"], limit]
170
184
  else
171
185
  "-"
172
186
  end
@@ -174,9 +188,16 @@ class OneUserHelper < OpenNebulaHelper::OneHelper
174
188
 
175
189
  column :MEMORY, "Total memory allocated to user VMs", :size=>17 do |d|
176
190
  if d.has_key?('VM_QUOTA') and d['VM_QUOTA'].has_key?('VM')
191
+ limit = d['VM_QUOTA']['VM']["MEMORY"]
192
+
193
+ if limit == "-1"
194
+ limit = default_quotas['VM_QUOTA/VM/MEMORY']
195
+ limit = "0" if limit.nil? || limit == ""
196
+ end
197
+
177
198
  d['VM_QUOTA']['VM']['MEMORY_USED']
178
199
  "%7s / %7s" % [OpenNebulaHelper.unit_to_str(d['VM_QUOTA']['VM']["MEMORY_USED"].to_i,{},"M"),
179
- OpenNebulaHelper.unit_to_str(d['VM_QUOTA']['VM']["MEMORY"].to_i,{},"M")]
200
+ OpenNebulaHelper.unit_to_str(limit.to_i,{},"M")]
180
201
  else
181
202
  "-"
182
203
  end
@@ -184,7 +205,14 @@ class OneUserHelper < OpenNebulaHelper::OneHelper
184
205
 
185
206
  column :CPU, "Total CPU allocated to user VMs", :size=>11 do |d|
186
207
  if d.has_key?('VM_QUOTA') and d['VM_QUOTA'].has_key?('VM')
187
- "%4.0f / %4.0f" % [d['VM_QUOTA']['VM']["CPU_USED"], d['VM_QUOTA']['VM']["CPU"]]
208
+ limit = d['VM_QUOTA']['VM']["CPU"]
209
+
210
+ if limit == "-1"
211
+ limit = default_quotas['VM_QUOTA/VM/CPU']
212
+ limit = "0" if limit.nil? || limit == ""
213
+ end
214
+
215
+ "%4.0f / %4.0f" % [d['VM_QUOTA']['VM']["CPU_USED"], limit]
188
216
  else
189
217
  "-"
190
218
  end
@@ -217,6 +245,13 @@ class OneUserHelper < OpenNebulaHelper::OneHelper
217
245
  end
218
246
 
219
247
  def format_resource(user)
248
+ system = System.new(@client)
249
+ default_quotas = system.get_user_quotas()
250
+
251
+ if OpenNebula::is_error?(default_quotas)
252
+ raise "Error retrieving the default user quotas: #{default_quotas.message}"
253
+ end
254
+
220
255
  str="%-15s: %-20s"
221
256
  str_h1="%-80s"
222
257
 
@@ -237,6 +272,7 @@ class OneUserHelper < OpenNebulaHelper::OneHelper
237
272
 
238
273
  user_hash = user.to_hash
239
274
 
240
- OneQuotaHelper.format_quota(user_hash['USER'])
275
+ helper = OneQuotaHelper.new
276
+ helper.format_quota(user_hash['USER'], default_quotas)
241
277
  end
242
278
  end