opennebula-cli 5.11.90.pre → 5.12.3

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
@@ -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
@@ -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
@@ -580,10 +603,6 @@ module CLIHelper
580
603
  filter_data!(res_data, options) if options[:filter]
581
604
  end
582
605
 
583
- return res_data unless options[:list]
584
-
585
- @default_columns = options[:list].collect {|o| o.upcase.to_sym }
586
-
587
606
  res_data
588
607
  end
589
608
 
@@ -612,7 +631,7 @@ module CLIHelper
612
631
  def config_expand_data
613
632
  ret = []
614
633
 
615
- @default_columns.each do |column|
634
+ @cli_columns.each do |column|
616
635
  expand_c = @columns[column][:expand]
617
636
 
618
637
  next unless expand_c
@@ -633,7 +652,7 @@ module CLIHelper
633
652
  def config_adjust_data
634
653
  ret = []
635
654
 
636
- @default_columns.each do |column|
655
+ @cli_columns.each do |column|
637
656
  next unless @columns[column][:adjust]
638
657
 
639
658
  ret << column.to_s.downcase
@@ -649,7 +668,9 @@ module CLIHelper
649
668
  def expand_columns(expand_columns, all = false)
650
669
  return if expand_columns.empty?
651
670
 
652
- 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?
653
674
  terminal_size = IO.console.winsize[1]
654
675
  else
655
676
  terminal_size = nil
@@ -657,13 +678,13 @@ module CLIHelper
657
678
 
658
679
  return if terminal_size.nil?
659
680
 
660
- default_columns = columns_info(@default_columns)
681
+ default_columns = columns_info(@cli_columns)
661
682
  expand_columns = columns_info(expand_columns)
662
683
 
663
684
  total_size = total_columns_size(default_columns)
664
685
  columns_size = total_columns_size(expand_columns)
665
686
 
666
- terminal_size -= (@default_columns.size - 1)
687
+ terminal_size -= (@cli_columns.size - 1)
667
688
  left_size = terminal_size - total_size
668
689
  remaining_size = left_size
669
690
 
@@ -735,7 +756,7 @@ module CLIHelper
735
756
  expand_data = []
736
757
 
737
758
  if expand_all
738
- expand_data = @default_columns
759
+ expand_data = @cli_columns
739
760
  elsif expand
740
761
  expand_data += options[:expand]
741
762
  end
@@ -749,7 +770,7 @@ module CLIHelper
749
770
  unless expand_all
750
771
  adjust.each do |column|
751
772
  column = column.upcase.to_sym
752
- size = max_size(@default_columns.index(column))
773
+ size = max_size(@cli_columns.index(column))
753
774
 
754
775
  if size && size > @columns[column][:size]
755
776
  @columns[column][:adjust] = true
@@ -821,7 +842,7 @@ module CLIHelper
821
842
  m = s.match(/^(.*?)#{operators}(.*?)$/)
822
843
 
823
844
  if m
824
- index = @columns.keys.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
@@ -115,7 +115,7 @@ class OneAclHelper < OpenNebulaHelper::OneHelper
115
115
  def format_pool(_options)
116
116
  config_file = self.class.table_conf
117
117
 
118
- table = CLIHelper::ShowTable.new(config_file, self) do
118
+ CLIHelper::ShowTable.new(config_file, self) do
119
119
  column :ID,
120
120
  'Rule Identifier',
121
121
  :size => 5 do |d|
@@ -157,8 +157,6 @@ class OneAclHelper < OpenNebulaHelper::OneHelper
157
157
 
158
158
  default :ID, :USER, :RES_VHNIUTGDCOZSvRMAPt, :RID, :OPE_UMAC, :ZONE
159
159
  end
160
-
161
- table
162
160
  end
163
161
  # rubocop:enable Lint/IneffectiveAccessModifier
164
162
 
@@ -406,7 +406,11 @@ class OneFlowHelper < OpenNebulaHelper::OneHelper
406
406
 
407
407
  column :TIME, '', :left, :size => 67 do |d|
408
408
  if d['start_time']
409
- Time.parse(d['start_time']).to_s
409
+ if !d['start_time'].match(/^\d+$/)
410
+ Time.parse(d['start_time']).to_s
411
+ else
412
+ d['start_time']
413
+ end
410
414
  else
411
415
  d['recurrence']
412
416
  end
@@ -94,7 +94,7 @@ class OneHookHelper < OpenNebulaHelper::OneHelper
94
94
  def format_pool(_options)
95
95
  config_file = self.class.table_conf
96
96
 
97
- table = CLIHelper::ShowTable.new(config_file, self) do
97
+ CLIHelper::ShowTable.new(config_file, self) do
98
98
  column :ID, 'ONE identifier for the Hook', :size => 5 do |d|
99
99
  d['ID']
100
100
  end
@@ -109,8 +109,6 @@ class OneHookHelper < OpenNebulaHelper::OneHelper
109
109
 
110
110
  default :ID, :NAME, :TYPE
111
111
  end
112
-
113
- table
114
112
  end
115
113
 
116
114
  # Function to print Execution Log records as sent by oned using:
@@ -163,7 +161,11 @@ class OneHookHelper < OpenNebulaHelper::OneHelper
163
161
  end
164
162
  end
165
163
 
166
- default :HOOK, :ID, :TIMESTAMP, :RC, :EXECUTION
164
+ if !header
165
+ default :HOOK, :ID, :TIMESTAMP, :RC, :EXECUTION
166
+ else
167
+ default :ID, :TIMESTAMP, :RC, :EXECUTION
168
+ end
167
169
  end
168
170
 
169
171
  table.show(execs, :stat_column => :EXECUTION)