opennebula-cli 5.11.80.pre → 5.12.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -27,8 +27,9 @@ else
27
27
  end
28
28
 
29
29
  if File.directory?(GEMS_LOCATION)
30
- Gem.use_paths(GEMS_LOCATION)
31
- $LOAD_PATH.reject! {|l| l =~ /(vendor|site)_ruby/ }
30
+ $LOAD_PATH.reject! {|l| l =~ /vendor_ruby/ }
31
+ require 'rubygems'
32
+ Gem.use_paths(File.realpath(GEMS_LOCATION))
32
33
  end
33
34
 
34
35
  $LOAD_PATH << RUBY_LIB_LOCATION
@@ -27,8 +27,9 @@ else
27
27
  end
28
28
 
29
29
  if File.directory?(GEMS_LOCATION)
30
- Gem.use_paths(GEMS_LOCATION)
31
- $LOAD_PATH.reject! {|l| l =~ /(vendor|site)_ruby/ }
30
+ $LOAD_PATH.reject! {|l| l =~ /vendor_ruby/ }
31
+ require 'rubygems'
32
+ Gem.use_paths(File.realpath(GEMS_LOCATION))
32
33
  end
33
34
 
34
35
  $LOAD_PATH << RUBY_LIB_LOCATION
@@ -350,43 +351,9 @@ CommandParser::CmdParser.new(ARGV) do
350
351
  Update Address Range variables. SIZE, IP, MAC and TYPE cannot be updated
351
352
  EOT
352
353
 
353
- command :updatear, update_ar_desc, :vnetid, :ar_id, [:file, nil] do
354
- helper.perform_action(args[0], options, 'modified') do |obj|
355
- rc = obj.info
356
-
357
- if OpenNebula.is_error?(rc)
358
- puts rc.message
359
- exit(-1)
360
- end
361
-
362
- obj.delete_element("AR_POOL/AR[AR_ID!=#{args[1]}]")
363
- obj.delete_element('AR_POOL/AR/LEASES')
364
- obj.delete_element('AR_POOL/AR/USED_LEASES')
365
- obj.delete_element('AR_POOL/AR/MAC_END')
366
- obj.delete_element('AR_POOL/AR/IP_END')
367
- obj.delete_element('AR_POOL/AR/IP6_ULA')
368
- obj.delete_element('AR_POOL/AR/IP6_ULA_END')
369
- obj.delete_element('AR_POOL/AR/IP6_GLOBAL')
370
- obj.delete_element('AR_POOL/AR/IP6_GLOBAL_END')
371
-
372
- if obj.template_like_str('AR_POOL').empty?
373
- puts "Address Range #{args[1]} does not exist for " \
374
- "Virtual Network #{args[0]}"
375
- exit(-1)
376
- end
377
-
378
- str = OpenNebulaHelper.update_template_helper(false,
379
- args[0],
380
- obj,
381
- args[2],
382
- 'AR_POOL',
383
- false)
384
-
385
- helper.set_client(options)
386
- obj = helper.retrieve_resource(obj.id)
387
-
388
- obj.update_ar(str)
389
- end
354
+ command :updatear, update_ar_desc, :vnetid, :ar_id, [:file, nil],
355
+ :options => OpenNebulaHelper::APPEND do
356
+ helper.update_ar(args[0], args[1], args[2], options)
390
357
  end
391
358
 
392
359
  rename_desc = <<-EOT.unindent
@@ -27,8 +27,9 @@ else
27
27
  end
28
28
 
29
29
  if File.directory?(GEMS_LOCATION)
30
- Gem.use_paths(GEMS_LOCATION)
31
- $LOAD_PATH.reject! {|l| l =~ /(vendor|site)_ruby/ }
30
+ $LOAD_PATH.reject! {|l| l =~ /vendor_ruby/ }
31
+ require 'rubygems'
32
+ Gem.use_paths(File.realpath(GEMS_LOCATION))
32
33
  end
33
34
 
34
35
  $LOAD_PATH << RUBY_LIB_LOCATION
@@ -27,8 +27,9 @@ else
27
27
  end
28
28
 
29
29
  if File.directory?(GEMS_LOCATION)
30
- Gem.use_paths(GEMS_LOCATION)
31
- $LOAD_PATH.reject! {|l| l =~ /(vendor|site)_ruby/ }
30
+ $LOAD_PATH.reject! {|l| l =~ /vendor_ruby/ }
31
+ require 'rubygems'
32
+ Gem.use_paths(File.realpath(GEMS_LOCATION))
32
33
  end
33
34
 
34
35
  $LOAD_PATH << RUBY_LIB_LOCATION
@@ -27,8 +27,9 @@ else
27
27
  end
28
28
 
29
29
  if File.directory?(GEMS_LOCATION)
30
- Gem.use_paths(GEMS_LOCATION)
31
- $LOAD_PATH.reject! {|l| l =~ /(vendor|site)_ruby/ }
30
+ $LOAD_PATH.reject! {|l| l =~ /vendor_ruby/ }
31
+ require 'rubygems'
32
+ Gem.use_paths(File.realpath(GEMS_LOCATION))
32
33
  end
33
34
 
34
35
  $LOAD_PATH << RUBY_LIB_LOCATION
@@ -231,6 +232,9 @@ CommandParser::CmdParser.new(ARGV) do
231
232
 
232
233
  command :serversync, sync_desc, :server, :options => [DATABASE] do
233
234
  begin
235
+ # Suppress augeas require warning message
236
+ $VERBOSE = nil
237
+
234
238
  gem 'augeas', '~> 0.6'
235
239
  require 'augeas'
236
240
  rescue Gem::LoadError
@@ -325,9 +325,10 @@ module CLIHelper
325
325
  column[:size] = 5
326
326
 
327
327
  conf.each do |c|
328
- if c.is_a? Symbol
328
+ case c
329
+ when Symbol
329
330
  column[c] = true
330
- elsif c.is_a? Hash
331
+ when Hash
331
332
  c.each do |key, value|
332
333
  column[key] = value
333
334
  end
@@ -356,6 +357,12 @@ module CLIHelper
356
357
  # @param options [Hash] Object with CLI user options
357
358
  # @param top [Boolean] True to not update columns again
358
359
  def show(data, options = {}, top = false)
360
+ if options[:list]
361
+ @cli_columns = options[:list].collect {|o| o.upcase.to_sym }
362
+ else
363
+ @cli_columns = @default_columns
364
+ end
365
+
359
366
  update_columns(options) unless top
360
367
 
361
368
  if data.is_a? Hash
@@ -477,7 +484,7 @@ module CLIHelper
477
484
 
478
485
  # Get header in string format
479
486
  def header_str
480
- @default_columns.collect do |c|
487
+ @cli_columns.collect do |c|
481
488
  if @columns[c]
482
489
  format_str(c, c.to_s)
483
490
  else
@@ -495,12 +502,12 @@ module CLIHelper
495
502
 
496
503
  update_columns_size(options)
497
504
 
505
+ options[:csv_del] ? del = options[:csv_del] : del = ','
506
+
498
507
  if !options[:csv] && (!options.key? :no_header)
499
508
  CLIHelper.print_header(header_str)
500
- end
501
-
502
- if options[:csv] && (!options.key? :no_header)
503
- print_csv_data([@default_columns], options[:csv_del])
509
+ elsif options[:csv] && (!options.key? :no_header)
510
+ puts CSV.generate_line(@cli_columns, :col_sep => del)
504
511
  end
505
512
 
506
513
  @res_data ? print_data(@res_data, options) : puts
@@ -528,7 +535,13 @@ module CLIHelper
528
535
  del ? del = del : del = ','
529
536
 
530
537
  data.each do |l|
531
- puts CSV.generate_line(l, :col_sep => del)
538
+ result = []
539
+
540
+ @cli_columns.each do |col|
541
+ result << l[@default_columns.index(col)]
542
+ end
543
+
544
+ puts CSV.generate_line(result, :col_sep => del)
532
545
  end
533
546
  end
534
547
 
@@ -537,25 +550,26 @@ module CLIHelper
537
550
  # @param data [Array] Array with data to show
538
551
  # @param stat_column [String] Name of the state column
539
552
  def print_normal_data(data, stat_column)
540
- ncolumns = @default_columns.length
541
-
542
553
  if stat_column
543
- stat = stat_column.upcase.to_sym
544
- stat_column = @default_columns.index(stat)
554
+ stat = stat_column.upcase.to_sym
545
555
  else
546
- stat_column = @default_columns.index(:STAT)
556
+ stat = :STAT
547
557
  end
548
558
 
549
559
  data.each do |l|
550
560
  result = []
551
561
 
552
- ncolumns.times do |i|
562
+ @cli_columns.each do |col|
563
+ i = @default_columns.index(col)
564
+
565
+ # Column might not exist
566
+ next unless i
567
+
553
568
  dat = l[i]
554
- col = @default_columns[i]
555
569
 
556
570
  str = format_str(col, dat)
557
571
 
558
- str = CLIHelper.color_state(str) if i == stat_column
572
+ str = CLIHelper.color_state(str) if col == stat
559
573
 
560
574
  result << str
561
575
  end
@@ -570,8 +584,17 @@ module CLIHelper
570
584
  #
571
585
  # @return [Array] Array with selected columns information
572
586
  def data_array(data, options)
587
+ # Take default table columns and desired ones by the user
588
+ cols = @default_columns
589
+
590
+ @cli_columns.each do |col|
591
+ next if @default_columns.include?(col)
592
+
593
+ @default_columns.insert(@cli_columns.index(col) + 1, col)
594
+ end
595
+
573
596
  res_data = data.collect do |d|
574
- @default_columns.collect do |c|
597
+ cols.collect do |c|
575
598
  @columns[c][:proc].call(d).to_s if @columns[c]
576
599
  end
577
600
  end
@@ -608,7 +631,7 @@ module CLIHelper
608
631
  def config_expand_data
609
632
  ret = []
610
633
 
611
- @default_columns.each do |column|
634
+ @cli_columns.each do |column|
612
635
  expand_c = @columns[column][:expand]
613
636
 
614
637
  next unless expand_c
@@ -629,7 +652,7 @@ module CLIHelper
629
652
  def config_adjust_data
630
653
  ret = []
631
654
 
632
- @default_columns.each do |column|
655
+ @cli_columns.each do |column|
633
656
  next unless @columns[column][:adjust]
634
657
 
635
658
  ret << column.to_s.downcase
@@ -645,7 +668,9 @@ module CLIHelper
645
668
  def expand_columns(expand_columns, all = false)
646
669
  return if expand_columns.empty?
647
670
 
648
- if $stdout.tty? || (IO.console && IO.console.tty?)
671
+ if $stdout.isatty
672
+ terminal_size = $stdout.winsize[1]
673
+ elsif IO.console && IO.console.tty?
649
674
  terminal_size = IO.console.winsize[1]
650
675
  else
651
676
  terminal_size = nil
@@ -653,13 +678,13 @@ module CLIHelper
653
678
 
654
679
  return if terminal_size.nil?
655
680
 
656
- default_columns = columns_info(@default_columns)
681
+ default_columns = columns_info(@cli_columns)
657
682
  expand_columns = columns_info(expand_columns)
658
683
 
659
684
  total_size = total_columns_size(default_columns)
660
685
  columns_size = total_columns_size(expand_columns)
661
686
 
662
- terminal_size -= (@default_columns.size - 1)
687
+ terminal_size -= (@cli_columns.size - 1)
663
688
  left_size = terminal_size - total_size
664
689
  remaining_size = left_size
665
690
 
@@ -731,7 +756,7 @@ module CLIHelper
731
756
  expand_data = []
732
757
 
733
758
  if expand_all
734
- expand_data = @default_columns
759
+ expand_data = @cli_columns
735
760
  elsif expand
736
761
  expand_data += options[:expand]
737
762
  end
@@ -745,7 +770,7 @@ module CLIHelper
745
770
  unless expand_all
746
771
  adjust.each do |column|
747
772
  column = column.upcase.to_sym
748
- size = max_size(@default_columns.index(column))
773
+ size = max_size(@cli_columns.index(column))
749
774
 
750
775
  if size && size > @columns[column][:size]
751
776
  @columns[column][:adjust] = true
@@ -797,10 +822,6 @@ module CLIHelper
797
822
  rescue StandardError => e
798
823
  CLIHelper.fail(e.message)
799
824
  end
800
-
801
- return unless options[:list]
802
-
803
- @default_columns = options[:list].collect {|o| o.upcase.to_sym }
804
825
  end
805
826
 
806
827
  # Filter data
@@ -821,7 +842,7 @@ module CLIHelper
821
842
  m = s.match(/^(.*?)#{operators}(.*?)$/)
822
843
 
823
844
  if m
824
- index = @default_columns.index(m[1].to_sym)
845
+ index = @default_columns.index(m[1].upcase.to_sym)
825
846
 
826
847
  if index
827
848
  {
@@ -295,6 +295,11 @@ module CommandParser
295
295
  cmd[:arity]+=1 unless args.include?(nil)
296
296
  cmd[:args_format] << args
297
297
  elsif args.instance_of?(Hash) && args[:options]
298
+ if args[:options].is_a? Array
299
+ args[:options].flatten!
300
+ args[:options] = args[:options].sort_by {|o| o[:name] }
301
+ end
302
+
298
303
  cmd[:options] << args[:options]
299
304
  else
300
305
  cmd[:arity]+=1
@@ -649,7 +654,7 @@ module CommandParser
649
654
  print_formatters
650
655
  puts
651
656
  if @version
652
- puts "## LICENSE"
657
+ puts "## VERSION"
653
658
  puts @version
654
659
  end
655
660
  end
@@ -679,7 +684,9 @@ module CommandParser
679
684
  def print_options
680
685
  puts "## OPTIONS"
681
686
 
682
- shown_opts = Array.new
687
+ shown_opts = []
688
+ options = []
689
+
683
690
  @command_list.each do |key|
684
691
  value = @commands[key]
685
692
 
@@ -688,14 +695,17 @@ module CommandParser
688
695
  next
689
696
  else
690
697
  shown_opts << o[:name]
691
-
692
- print_option(o)
698
+ options << o
693
699
  end
694
700
  end
695
701
  end
696
702
 
697
- @available_options.each do |o|
698
- print_option o
703
+ options << @available_options
704
+ options.flatten!
705
+ options = options.sort_by {|o| o[:name] }
706
+
707
+ options.each do |o|
708
+ print_option(o)
699
709
  end
700
710
  end
701
711
 
@@ -724,6 +734,8 @@ module CommandParser
724
734
  else
725
735
  puts "## COMMANDS"
726
736
 
737
+ @command_list.sort! if @command_list
738
+
727
739
  @command_list.each do |key|
728
740
  value = @commands[key]
729
741
  printf cmd_format5, "* #{key} "
@@ -766,6 +778,9 @@ module CommandParser
766
778
 
767
779
  cmd_format5 = "#{' '*3}%s"
768
780
  cmd_format10 = "#{' '*8}%s"
781
+
782
+ @formats = @formats.sort_by {|key, _| key } if @formats
783
+
769
784
  @formats.each{ |key,value|
770
785
  printf cmd_format5, "* #{key}"
771
786
  puts
@@ -31,10 +31,6 @@ module OpenNebulaHelper
31
31
  ONE_VERSION=<<-EOT
32
32
  OpenNebula #{OpenNebula::VERSION}
33
33
  Copyright 2002-2020, OpenNebula Project, OpenNebula Systems
34
-
35
- Licensed under the Apache License, Version 2.0 (the "License"); you may
36
- not use this file except in compliance with the License. You may obtain
37
- a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
38
34
  EOT
39
35
 
40
36
  if ONE_LOCATION
@@ -1203,6 +1199,18 @@ EOT
1203
1199
  end
1204
1200
  end
1205
1201
 
1202
+ def OpenNebulaHelper.bytes_to_unit(value, unit = 'K')
1203
+ j = 0
1204
+ i = BinarySufix.index(unit).to_i
1205
+
1206
+ while j < i do
1207
+ value /= 1024.0
1208
+ j += 1
1209
+ end
1210
+
1211
+ value
1212
+ end
1213
+
1206
1214
  # If the cluster name is empty, returns a '-' char.
1207
1215
  #
1208
1216
  # @param str [String || Hash] Cluster name, or empty Hash (when <CLUSTER/>)
@@ -1887,4 +1895,57 @@ EOT
1887
1895
  answers
1888
1896
  end
1889
1897
 
1898
+ # Returns plot object to print it on the CLI
1899
+ #
1900
+ # @param x [Array] Data to x axis (Time axis)
1901
+ # @param y [Array] Data to y axis
1902
+ # @param attr [String] Parameter to y axis
1903
+ # @param title [String] Plot title
1904
+ #
1905
+ # @return Gnuplot plot object
1906
+ def OpenNebulaHelper.get_plot(x, y, attr, title)
1907
+ # Require gnuplot gem only here
1908
+ begin
1909
+ require 'gnuplot'
1910
+ rescue LoadError, Gem::LoadError
1911
+ STDERR.puts(
1912
+ 'Gnuplot gem is not installed, run `gem install gnuplot` '\
1913
+ 'to install it'
1914
+ )
1915
+ exit(-1)
1916
+ end
1917
+
1918
+ # Check if gnuplot is installed on the system
1919
+ unless system('gnuplot --version')
1920
+ STDERR.puts(
1921
+ 'Gnuplot is not installed, install it depending on your distro'
1922
+ )
1923
+ exit(-1)
1924
+ end
1925
+
1926
+ Gnuplot.open do |gp|
1927
+ Gnuplot::Plot.new(gp) do |p|
1928
+ p.title title
1929
+
1930
+ p.xlabel 'Time'
1931
+ p.ylabel attr
1932
+
1933
+ p.xdata 'time'
1934
+ p.timefmt "'%H:%M'"
1935
+ p.format "x '%H:%M'"
1936
+
1937
+ p.style 'data lines'
1938
+ p.terminal 'dumb'
1939
+
1940
+ p.data << Gnuplot::DataSet.new([x, y]) do |ds|
1941
+ ds.with = 'linespoints'
1942
+ ds.linewidth = '3'
1943
+ ds.using = '1:2'
1944
+
1945
+ ds.notitle
1946
+ end
1947
+ end
1948
+ end
1949
+ end
1950
+
1890
1951
  end