opennebula-cli 4.4.0 → 4.5.80.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.
data/lib/cli_helper.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # -------------------------------------------------------------------------- #
2
- # Copyright 2002-2013, OpenNebula Project (OpenNebula.org), C12G Labs #
2
+ # Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs #
3
3
  # #
4
4
  # Licensed under the Apache License, Version 2.0 (the "License"); you may #
5
5
  # not use this file except in compliance with the License. You may obtain #
@@ -14,6 +14,8 @@
14
14
  # limitations under the License. #
15
15
  #--------------------------------------------------------------------------- #
16
16
 
17
+ require 'csv'
18
+
17
19
  module CLIHelper
18
20
  LIST = {
19
21
  :name => "list",
@@ -23,6 +25,12 @@ module CLIHelper
23
25
  :description => "Selects columns to display with list command"
24
26
  }
25
27
 
28
+ CSV = {
29
+ :name => "csv",
30
+ :large => "--csv",
31
+ :description => "Write table in csv format"
32
+ }
33
+
26
34
  #ORDER = {
27
35
  # :name => "order",
28
36
  # :short => "-o x,y,z",
@@ -56,7 +64,7 @@ module CLIHelper
56
64
  }
57
65
 
58
66
  #OPTIONS = [LIST, ORDER, FILTER, HEADER, DELAY]
59
- OPTIONS = [LIST, DELAY, FILTER]
67
+ OPTIONS = [LIST, DELAY, FILTER, CSV]
60
68
 
61
69
  # Sets bold font
62
70
  def CLIHelper.scr_bold
@@ -126,9 +134,36 @@ module CLIHelper
126
134
  puts
127
135
  end
128
136
 
137
+ module HashWithSearch
138
+ def dsearch(path)
139
+ stems=path.split('/')
140
+ hash=self
141
+
142
+ stems.delete_if {|s| s.nil? || s.empty? }
143
+
144
+ stems.each do |stem|
145
+ if Hash===hash
146
+ if hash[stem]
147
+ hash=hash[stem]
148
+ else
149
+ hash=nil
150
+ break
151
+ end
152
+ else
153
+ hash=nil
154
+ break
155
+ end
156
+ end
157
+
158
+ hash
159
+ end
160
+ end
161
+
129
162
  class ShowTable
130
163
  require 'yaml'
131
164
 
165
+ attr_reader :default_columns
166
+
132
167
  def initialize(conf=nil, ext=nil, &block)
133
168
  @columns = Hash.new
134
169
  @default_columns = Array.new
@@ -168,7 +203,23 @@ module CLIHelper
168
203
 
169
204
  def show(data, options={})
170
205
  update_columns(options)
171
- print_table(data, options)
206
+
207
+ if Hash===data
208
+ @data=data
209
+ @data.extend(HashWithSearch)
210
+
211
+ pool=@data.keys.first
212
+ return print_table(nil, options) if !pool
213
+
214
+ element=pool.split('_').first
215
+
216
+ pool_data=@data.dsearch("#{pool}/#{element}")
217
+ pool_data=[pool_data].flatten if pool_data
218
+
219
+ print_table(pool_data, options)
220
+ else
221
+ print_table(data, options)
222
+ end
172
223
  end
173
224
 
174
225
  def top(options={}, &block)
@@ -176,11 +227,11 @@ module CLIHelper
176
227
 
177
228
  begin
178
229
  while true
230
+ data = block.call
231
+
179
232
  CLIHelper.scr_cls
180
233
  CLIHelper.scr_move(0,0)
181
234
 
182
- data = block.call
183
-
184
235
  show(data, options)
185
236
  sleep delay
186
237
  end
@@ -200,7 +251,7 @@ module CLIHelper
200
251
  private
201
252
 
202
253
  def print_table(data, options)
203
- CLIHelper.print_header(header_str)
254
+ CLIHelper.print_header(header_str) if !options[:csv]
204
255
  data ? print_data(data, options) : puts
205
256
  end
206
257
 
@@ -216,21 +267,26 @@ module CLIHelper
216
267
  end
217
268
 
218
269
  begin
219
- print res_data.collect{|l|
220
- (0..ncolumns-1).collect{ |i|
221
- dat=l[i]
222
- col=@default_columns[i]
270
+ if options[:csv]
271
+ CSV($stdout, :write_headers => true,
272
+ :headers => @default_columns) do |csv|
273
+ res_data.each {|l| csv << l }
274
+ end
275
+ else
276
+ res_data.each{|l|
277
+ puts (0..ncolumns-1).collect{ |i|
278
+ dat=l[i]
279
+ col=@default_columns[i]
223
280
 
224
- str=format_str(col, dat)
225
- str=CLIHelper.color_state(str) if i==stat_column
281
+ str=format_str(col, dat)
282
+ str=CLIHelper.color_state(str) if i==stat_column
226
283
 
227
- str
228
- }.join(' ')
229
- }.join("\n").gsub(/ *$/, '')
284
+ str
285
+ }.join(' ').rstrip
286
+ }
287
+ end
230
288
  rescue Errno::EPIPE
231
289
  end
232
-
233
- puts
234
290
  end
235
291
 
236
292
  def data_array(data, options)
@@ -1,5 +1,5 @@
1
1
  # -------------------------------------------------------------------------- #
2
- # Copyright 2002-2013, OpenNebula Project (OpenNebula.org), C12G Labs #
2
+ # Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs #
3
3
  # #
4
4
  # Licensed under the Apache License, Version 2.0 (the "License"); you may #
5
5
  # not use this file except in compliance with the License. You may obtain #
data/lib/one_helper.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # -------------------------------------------------------------------------- #
2
- # Copyright 2002-2013, OpenNebula Project (OpenNebula.org), C12G Labs #
2
+ # Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs #
3
3
  # #
4
4
  # Licensed under the Apache License, Version 2.0 (the "License"); you may #
5
5
  # not use this file except in compliance with the License. You may obtain #
@@ -16,13 +16,19 @@
16
16
 
17
17
  require 'cli_helper'
18
18
 
19
- require 'opennebula'
19
+ begin
20
+ require 'opennebula'
21
+ rescue Exception => e
22
+ puts "Error: "+e.message.to_s
23
+ exit(-1)
24
+ end
25
+
20
26
  include OpenNebula
21
27
 
22
28
  module OpenNebulaHelper
23
29
  ONE_VERSION=<<-EOT
24
30
  OpenNebula #{OpenNebula::VERSION}
25
- Copyright 2002-2013, OpenNebula Project (OpenNebula.org), C12G Labs
31
+ Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs
26
32
 
27
33
  Licensed under the Apache License, Version 2.0 (the "License"); you may
28
34
  not use this file except in compliance with the License. You may obtain
@@ -94,18 +100,91 @@ EOT
94
100
  :name => 'user',
95
101
  :large => '--user name',
96
102
  :description => 'User name used to connect to OpenNebula',
97
- :format => String
103
+ :format => String,
104
+ :proc => lambda do |o, options|
105
+ OneHelper.set_user(o)
106
+ [0, o]
107
+ end
98
108
  },
99
109
  {
100
110
  :name => 'password',
101
111
  :large => '--password password',
102
112
  :description => 'Password to authenticate with OpenNebula',
103
- :format => String
113
+ :format => String,
114
+ :proc => lambda do |o, options|
115
+ OneHelper.set_password(o)
116
+ [0, o]
117
+ end
104
118
  },
105
119
  {
106
120
  :name => 'endpoint',
107
121
  :large => '--endpoint endpoint',
108
122
  :description => 'URL of OpenNebula xmlrpc frontend',
123
+ :format => String,
124
+ :proc => lambda do |o, options|
125
+ OneHelper.set_endpoint(o)
126
+ [0, o]
127
+ end
128
+ }
129
+ ]
130
+
131
+ GROUP_OPTIONS=[
132
+ {
133
+ :name => 'name',
134
+ :large => '--name name',
135
+ :short => "-n",
136
+ :description =>
137
+ 'Name for the new group',
138
+ :format => String
139
+ },
140
+ {
141
+ :name => 'admin_group',
142
+ :large => '--admin_group name',
143
+ :short => "-a",
144
+ :description =>
145
+ 'Creates an admin group with name',
146
+ :format => String
147
+ },
148
+ {
149
+ :name => 'admin_user',
150
+ :large => '--admin_user name',
151
+ :short => "-u",
152
+ :description =>
153
+ 'Creates an admin user for the group with name',
154
+ :format => String
155
+ },
156
+ {
157
+ :name => 'admin_password',
158
+ :large => '--admin_password password',
159
+ :short => "-p",
160
+ :description =>
161
+ 'Password for the admin user of the group',
162
+ :format => String
163
+ },
164
+ {
165
+ :name => 'admin_driver',
166
+ :large => '--admin_driver auth_driver',
167
+ :short => "-d",
168
+ :description =>
169
+ 'Auth driver for the admin user of the group',
170
+ :format => String
171
+ },
172
+ {
173
+ :name => 'resources',
174
+ :large => '--resources resources_str',
175
+ :short => "-r",
176
+ :description =>
177
+ 'Which resources can be created by group users "\
178
+ "(VM+NET+IMAGE+TEMPLATE by default)',
179
+ :format => String
180
+ },
181
+ {
182
+ :name => 'admin_resources',
183
+ :large => '--admin_resources resources_str',
184
+ :short => "-o",
185
+ :description =>
186
+ 'Which resources can be created by group users "\
187
+ "(VM+NET+IMAGE+TEMPLATE by default)',
109
188
  :format => String
110
189
  }
111
190
  ]
@@ -273,18 +352,31 @@ EOT
273
352
  class OneHelper
274
353
  attr_accessor :client
275
354
 
276
- def self.get_client(options)
277
- if defined?(@@client)
355
+ def self.get_client(options={}, force=false)
356
+ if !force && defined?(@@client)
278
357
  @@client
279
358
  else
359
+
280
360
  secret=nil
281
- user=options[:user]
361
+ password=nil
362
+
363
+ if defined?(@@user)
364
+ user=@@user
365
+ password=@@password if defined?(@@password)
366
+ else
367
+ user=options[:user]
368
+ end
369
+
282
370
  if user
283
- password=options[:password]||self.get_password
371
+ password=password||options[:password]||self.get_password
284
372
  secret="#{user}:#{password}"
285
373
  end
286
374
 
287
- endpoint=options[:endpoint]
375
+ if defined?(@@endpoint)
376
+ endpoint=@@endpoint
377
+ else
378
+ endpoint=options[:endpoint]
379
+ end
288
380
 
289
381
  @@client=OpenNebula::Client.new(secret, endpoint)
290
382
  end
@@ -294,10 +386,22 @@ EOT
294
386
  if defined?(@@client)
295
387
  @@client
296
388
  else
297
- self.get_client({})
389
+ self.get_client
298
390
  end
299
391
  end
300
392
 
393
+ def self.set_user(user)
394
+ @@user=user
395
+ end
396
+
397
+ def self.set_password(password)
398
+ @@password=password
399
+ end
400
+
401
+ def self.set_endpoint(endpoint)
402
+ @@endpoint=endpoint
403
+ end
404
+
301
405
  if RUBY_VERSION>="1.9.3"
302
406
  require 'io/console'
303
407
  def self.get_password
@@ -307,6 +411,7 @@ EOT
307
411
  puts
308
412
 
309
413
  pass.chop! if pass
414
+ @@password=pass
310
415
  pass
311
416
  end
312
417
  else
@@ -314,8 +419,9 @@ EOT
314
419
  def self.get_password
315
420
  print "Password: "
316
421
  system("stty", "-echo")
422
+ @@password=gets.chop
317
423
  begin
318
- return gets.chop
424
+ return @@password
319
425
  ensure
320
426
  system("stty", "echo")
321
427
  print "\n"
@@ -330,7 +436,7 @@ EOT
330
436
  end
331
437
 
332
438
  def set_client(options)
333
- @client=OpenNebulaHelper::OneHelper.get_client(options)
439
+ @client=OpenNebulaHelper::OneHelper.get_client(options, true)
334
440
  end
335
441
 
336
442
  def create_resource(options, &block)
@@ -357,25 +463,29 @@ EOT
357
463
 
358
464
  pool = factory_pool(filter_flag)
359
465
 
360
- rc = pool.info
361
- return -1, rc.message if OpenNebula.is_error?(rc)
362
-
363
466
  if options[:xml]
467
+ # TODO: use paginated functions
468
+ rc=pool.info
469
+ return -1, rc.message if OpenNebula.is_error?(rc)
364
470
  return 0, pool.to_xml(true)
365
471
  else
366
472
  table = format_pool(options)
367
473
 
368
474
  if top
369
475
  table.top(options) {
370
- pool.info
371
- pool_to_array(pool)
476
+ array=pool.get_hash
477
+ return -1, array.message if OpenNebula.is_error?(array)
478
+
479
+ array
372
480
  }
373
481
  else
374
- array=pool_to_array(pool)
482
+ array=pool.get_hash
483
+ return -1, array.message if OpenNebula.is_error?(array)
375
484
 
376
485
  if options[:ids]
377
- array=array.select do |element|
378
- options[:ids].include? element['ID'].to_i
486
+ rname=self.class.rname
487
+ array["#{rname}_POOL"][rname].reject! do |element|
488
+ !options[:ids].include?(element['ID'].to_i)
379
489
  end
380
490
  end
381
491
 
@@ -495,6 +605,10 @@ EOT
495
605
  end
496
606
 
497
607
  def self.name_to_id(name, pool, ename)
608
+ if ename=="CLUSTER" and name.upcase=="ALL"
609
+ return 0, "ALL"
610
+ end
611
+
498
612
  objects=pool.select {|object| object.name==name }
499
613
 
500
614
  if objects.length>0
@@ -610,6 +724,7 @@ EOT
610
724
  when "IMAGE" then OpenNebula::ImagePool.new(client)
611
725
  when "VMTEMPLATE" then OpenNebula::TemplatePool.new(client)
612
726
  when "VM" then OpenNebula::VirtualMachinePool.new(client)
727
+ when "ZONE" then OpenNebula::ZonePool.new(client)
613
728
  end
614
729
 
615
730
  rc = pool.info
@@ -1,5 +1,5 @@
1
1
  # -------------------------------------------------------------------------- #
2
- # Copyright 2002-2013, OpenNebula Project (OpenNebula.org), C12G Labs #
2
+ # Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs #
3
3
  # #
4
4
  # Licensed under the Apache License, Version 2.0 (the "License"); you may #
5
5
  # not use this file except in compliance with the License. You may obtain #
@@ -87,7 +87,7 @@ class AcctHelper < OpenNebulaHelper::OneHelper
87
87
  :name => "json",
88
88
  :short => "-j",
89
89
  :large => "--json",
90
- :description => "Show the resource in xml format"
90
+ :description => "Show the resource in json format"
91
91
  }
92
92
 
93
93
  SPLIT={
@@ -100,6 +100,10 @@ class AcctHelper < OpenNebulaHelper::OneHelper
100
100
 
101
101
 
102
102
  ACCT_TABLE = CLIHelper::ShowTable.new("oneacct.yaml", nil) do
103
+ column :UID, "User ID", :size=>4 do |d|
104
+ d["UID"]
105
+ end
106
+
103
107
  column :VID, "Virtual Machine ID", :size=>4 do |d|
104
108
  d["OID"]
105
109
  end
@@ -181,4 +185,4 @@ class AcctHelper < OpenNebulaHelper::OneHelper
181
185
  CLIHelper.scr_restore
182
186
  puts
183
187
  end
184
- end
188
+ end