Class: OpenNebulaHelper::OneHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/one_helper.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(secret = nil, endpoint = nil) ⇒ OneHelper

Returns a new instance of OneHelper.



543
544
545
546
547
# File 'lib/one_helper.rb', line 543

def initialize(secret=nil, endpoint=nil)
    @client=nil

    @translation_hash = nil
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



455
456
457
# File 'lib/one_helper.rb', line 455

def client
  @client
end

Class Method Details

.clientObject



496
497
498
499
500
501
502
# File 'lib/one_helper.rb', line 496

def self.client
    if defined?(@@client)
        @@client
    else
        self.get_client
    end
end

.filterflag_to_i_descObject



1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
# File 'lib/one_helper.rb', line 1129

def self.filterflag_to_i_desc
    desc="a, all            all the known \#{self.rname}s\nm, mine           the \#{self.rname} belonging to the user in ONE_AUTH\ng, group          'mine' plus the \#{self.rname} belonging to the groups\n          the user is member of\nG, primary group  the \#{self.rname} owned the user's primary group\nuid               \#{self.rname} of the user identified by this uid\nuser              \#{self.rname} of the user identified by the username\n"
end

.get_client(options = {}, force = false) ⇒ Object



457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
# File 'lib/one_helper.rb', line 457

def self.get_client(options={}, force=false)
    if !force && defined?(@@client)
        @@client
    else

        secret=nil
        password=nil

        if defined?(@@user)
            user=@@user
            password=@@password if defined?(@@password)
        else
            user=options[:user]
        end

        if user
            password=password||options[:password]||self.get_password
            secret="#{user}:#{password}"
        end

        if defined?(@@endpoint)
            endpoint=@@endpoint
        else
            endpoint=options[:endpoint]
        end

        # This breaks the CLI SSL support for Ruby 1.8.7, but is necessary
        # in order to do template updates, otherwise you get the broken pipe
        # error (bug #3341)
        if RUBY_VERSION < '1.9'
            sync = false
        else
            sync = true
        end
        options[:sync] = sync
        @@client=OpenNebula::Client.new(secret, endpoint, options)
    end
end

.get_passwordObject

This function is copied from ruby net/imap.rb



530
531
532
533
534
535
536
537
538
539
# File 'lib/one_helper.rb', line 530

def self.get_password
    print "Password: "
    pass=nil
    STDIN.noecho {|io| pass=io.gets }
    puts

    pass.chop! if pass
    @@password=pass
    pass
end

.list_to_id_descObject



1083
1084
1085
# File 'lib/one_helper.rb', line 1083

def self.list_to_id_desc
    "Comma-separated list of OpenNebula #{self.rname} names or ids"
end

.name_to_id(name, pool, ename) ⇒ Object



1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
# File 'lib/one_helper.rb', line 1087

def self.name_to_id(name, pool, ename)
    if ename=="CLUSTER" and name.upcase=="ALL"
        return 0, "ALL"
    end

    objects=pool.select {|object| object.name==name }

    if objects.length>0
        if objects.length>1
            return -1, "There are multiple #{ename}s with name #{name}."
        else
            result = objects.first.id
        end
    else
        return -1, "#{ename} named #{name} not found."
    end

    return 0, result
end

.set_endpoint(endpoint) ⇒ Object



512
513
514
# File 'lib/one_helper.rb', line 512

def self.set_endpoint(endpoint)
    @@endpoint=endpoint
end

.set_password(password) ⇒ Object



508
509
510
# File 'lib/one_helper.rb', line 508

def self.set_password(password)
    @@password=password
end

.set_user(user) ⇒ Object



504
505
506
# File 'lib/one_helper.rb', line 504

def self.set_user(user)
    @@user=user
end

.table_conf(conf_file = self.conf_file) ⇒ Object



1141
1142
1143
1144
1145
1146
1147
1148
1149
# File 'lib/one_helper.rb', line 1141

def self.table_conf(conf_file=self.conf_file)
    path = "#{ENV["HOME"]}/.one/cli/#{conf_file}"

    if File.exists?(path)
        return path
    else
        return "#{TABLE_CONF_PATH}/#{conf_file}"
    end
end

.to_id_descObject



1055
1056
1057
# File 'lib/one_helper.rb', line 1055

def self.to_id_desc
    "OpenNebula #{self.rname} name or id"
end

Instance Method Details

#check_orphan(pool, xpath, resource_name, attributes) ⇒ Object

Check if a resource defined by attributes is referenced in pool

and :uname

atributes => …, name => …, id => …

Parameters:

  • pool

    pool to search in

  • xpath

    xpath to search in pool

  • resource_name

    name of the resource to search (e.g IMAGE)



949
950
951
952
953
954
955
956
957
958
959
960
961
# File 'lib/one_helper.rb', line 949

def check_orphan(pool, xpath, resource_name, attributes)
    return false if attributes.empty?

    return false unless pool["#{xpath}[#{resource_name}_ID = "\
                             "#{attributes[:id]}]"].nil?

    return false unless pool["#{xpath}[#{resource_name} = "\
                             "'#{attributes[:name]}' and "\
                             "#{resource_name}_UNAME = "\
                             "'#{attributes[:uname]}']"].nil?

    true
end

#create_resource(options, &block) ⇒ Object



557
558
559
560
561
562
563
564
565
566
567
# File 'lib/one_helper.rb', line 557

def create_resource(options, &block)
    resource = factory

    rc = block.call(resource)
    if OpenNebula.is_error?(rc)
        return -1, rc.message
    else
        puts "ID: #{resource.id.to_s}"
        return 0
    end
end

#filterflag_to_i(str) ⇒ Object



1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
# File 'lib/one_helper.rb', line 1107

def filterflag_to_i(str)
    filter_flag = case str
    when "a", "all"   then OpenNebula::Pool::INFO_ALL
    when "m", "mine"  then OpenNebula::Pool::INFO_MINE
    when "g", "group" then OpenNebula::Pool::INFO_GROUP
    when "G", "primary group" then OpenNebula::Pool::INFO_PRIMARY_GROUP
    else
        if str.match(/^[0123456789]+$/)
            str.to_i
        else
            rc = OpenNebulaHelper.rname_to_id(str, "USER")
            if rc.first==-1
                return rc
            else
                rc[1]
            end
        end
    end

    return 0, filter_flag
end

#group_name(resource, options = {}) ⇒ Object



1032
1033
1034
1035
1036
1037
1038
# File 'lib/one_helper.rb', line 1032

def group_name(resource, options={})
    if options[:numeric]
        resource['GID']
    else
        resource['GNAME']
    end
end

#list_pool(options, top = false, filter_flag = nil) ⇒ Object



898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
# File 'lib/one_helper.rb', line 898

def list_pool(options, top=false, filter_flag=nil)
    # Capture Broken pipe
    Signal.trap('PIPE', 'EXIT')

    table = format_pool(options)

    if options[:describe]
        table.describe_columns

        return 0
    end

    filter_flag ||= OpenNebula::Pool::INFO_ALL

    pool  = factory_pool(filter_flag)
    pname = pool.pool_name
    ename = pool.element_name

    if top
        return list_pool_top(table, pool, options)
    elsif options[:xml]
        return list_pool_xml(pool, options, filter_flag)
    elsif options[:json]
        list_pool_format(pool, options, filter_flag) do |pool|
            hash        = check_resource_xsd(pool, pname)
            puts ::JSON.pretty_generate(hash)
        end
    elsif options[:yaml]
        list_pool_format(pool, options, filter_flag) do |pool|
            hash        = check_resource_xsd(pool, pname)
            puts hash.to_yaml(:indent => 4)
        end
    else
        return list_pool_table(table, pool, options, filter_flag)
    end

    return 0
rescue SystemExit, Interrupt
    # Rescue ctrl + c when paginated
    0
end

#list_pool_format(pool, options, filter_flag) ⇒ Object


List pool in JSON format, pagination is used in interactive output




809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
# File 'lib/one_helper.rb', line 809

def list_pool_format(pool, options, filter_flag)
    extended = options.include?(:extended) && options[:extended]

    if $stdout.isatty and (!options.key?:no_pager)
        size = $stdout.winsize[0] - 1

        # ----------- First page, check if pager is needed -------------
        rc = pool.get_page(size, 0, extended, options[:state])
        ps = ""

        return -1, rc.message if OpenNebula.is_error?(rc)

        elements = get_format_size(pool, options)
        ppid     = -1

        if elements >= size
            ppid = start_pager
        end

        yield(pool) if block_given?

        if elements < size
            return 0
        end

        if elements < size
            return 0
        elsif !pool.is_paginated?
            stop_pager(ppid)
            return 0
        end

        # ------- Rest of the pages in the pool, piped to pager --------
        current = size

        loop do
            rc = pool.get_page(size, current, extended, options[:state])

            return -1, rc.message if OpenNebula.is_error?(rc)

            current += size

            begin
                Process.waitpid(ppid, Process::WNOHANG)
            rescue Errno::ECHILD
                break
            end

            elements = get_format_size(pool, options)

            break if elements < size

            yield(pool) if block_given?

            $stdout.flush
        end

        stop_pager(ppid)
    else
        if pool.pool_name == "VM_POOL" && extended
            rc = pool.info_all_extended
        else
            rc = pool.info
        end

        return -1, rc.message if OpenNebula.is_error?(rc)

        yield(pool) if block_given?
    end

    return 0
end

#list_pool_table(table, pool, options, filter_flag) ⇒ Object


List the pool in table form, it uses pagination for interactive output




650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
# File 'lib/one_helper.rb', line 650

def list_pool_table(table, pool, options, filter_flag)
    if $stdout.isatty and (!options.key?:no_pager)
        size = $stdout.winsize[0] - 1

        # ----------- First page, check if pager is needed -------------
        rc = pool.get_page(size, 0, false, options[:state])
        ps = ""

        return -1, rc.message if OpenNebula.is_error?(rc)

        elements, hash = print_page(pool, options)

        ppid = -1

        if elements >= size
            ppid = start_pager
        end

        table.show(hash, options)

        if elements < size
            return 0
        elsif !pool.is_paginated?
            stop_pager(ppid)
            return 0
        end

        # ------- Rest of the pages in the pool, piped to pager --------
        current = size

        options[:no_header] = true

        loop do
            rc = pool.get_page(size, current, false, options[:state])

            return -1, rc.message if OpenNebula.is_error?(rc)

            current += size

            begin
                Process.waitpid(ppid, Process::WNOHANG)
            rescue Errno::ECHILD
                break
            end

            elements, hash = print_page(pool, options)

            table.show(hash, options)

            $stdout.flush

            break if elements < size
        end

        stop_pager(ppid)
    else
        rc = pool.info

        return -1, rc.message if OpenNebula.is_error?(rc)

        elements, hash = print_page(pool, options)

        if options[:ids] && elements
            hash = [hash[pool.pool_name][pool.element_name]].flatten
            hash.reject! do |element|
                !options[:ids].include?(element['ID'].to_i)
            end
        end

        table.show(hash, options)
    end

    return 0
end

#list_pool_top(table, pool, options) ⇒ Object


List pool table in top-like form




885
886
887
888
889
890
891
892
893
894
895
# File 'lib/one_helper.rb', line 885

def list_pool_top(table, pool, options)
    table.top(options) {
        array = pool.get_hash

        return -1, array.message if OpenNebula.is_error?(array)

        array
    }

    return 0
end

#list_pool_xml(pool, options, filter_flag) ⇒ Object


List pool in XML format, pagination is used in interactive output




728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
# File 'lib/one_helper.rb', line 728

def list_pool_xml(pool, options, filter_flag)
    extended = options.include?(:extended) && options[:extended]

    if $stdout.isatty and (!options.key?:no_pager)
        size = $stdout.winsize[0] - 1

        # ----------- First page, check if pager is needed -------------
        rc = pool.get_page(size, 0, extended, options[:state])
        ps = ""

        return -1, rc.message if OpenNebula.is_error?(rc)

        pname = pool.pool_name

        elements, page = print_page(pool, options)

        ppid = -1

        if elements >= size
            ppid = start_pager
        end

        puts page

        if elements < size
            return 0
        end

        if elements < size
            return 0
        elsif !pool.is_paginated?
            stop_pager(ppid)
            return 0
        end

        # ------- Rest of the pages in the pool, piped to pager --------
        current = size

        loop do
            rc = pool.get_page(size, current, extended, options[:state])

            return -1, rc.message if OpenNebula.is_error?(rc)

            current += size

            begin
                Process.waitpid(ppid, Process::WNOHANG)
            rescue Errno::ECHILD
                break
            end

            elements, page = print_page(pool, options)

            puts page

            $stdout.flush

            break if elements < size
        end

        puts "</#{pname}>"

        stop_pager(ppid)
    else
        if pool.pool_name == "VM_POOL" && extended
            rc = pool.info_all_extended
        else
            rc = pool.info
        end

        return -1, rc.message if OpenNebula.is_error?(rc)

        puts pool.to_xml(true)
    end

    return 0
end

#list_to_id(names) ⇒ Object



1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
# File 'lib/one_helper.rb', line 1059

def list_to_id(names)
    rc = get_pool
    return rc if rc.first != 0

    pool     = rc[1]
    poolname = self.class.rname

    result = names.split(',').collect { |name|
        if name.match(/^[0123456789]+$/)
            name.to_i
        else
            rc = OneHelper.name_to_id(name, pool, poolname)

            if rc.first == -1
                return rc[0], rc[1]
            end

            rc[1]
        end
    }

    return 0, result
end

#perform_action(id, options, verbose, &block) ⇒ Object



993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
# File 'lib/one_helper.rb', line 993

def perform_action(id, options, verbose, &block)
    resource = retrieve_resource(id)

    rc = block.call(resource)
    if OpenNebula.is_error?(rc)
        return -1, rc.message
    else
        if options[:verbose]
            puts "#{self.class.rname} #{id}: #{verbose}"
        end
        return 0
    end
end

#perform_actions(ids, options, verbose, &block) ⇒ Object



1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
# File 'lib/one_helper.rb', line 1007

def perform_actions(ids,options,verbose,&block)
    exit_code = 0
    ids.each do |id|
        rc = perform_action(id,options,verbose,&block)

        unless rc[0]==0
            STDERR.puts rc[1]
            exit_code=rc[0]
        end
    end

    exit_code
end


614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
# File 'lib/one_helper.rb', line 614

def print_page(pool, options)
    elements = 0
    page     = ""

    if options[:xml]
        elements += 1

        page << pool.to_xml(true)
    else
        pname = pool.pool_name
        ename = pool.element_name

        if options[:decrypt]
            page = pool.map do |element|
                element.info(true)
                element.to_hash[ename]
            end
        else
            page  = pool.to_hash
            elems = page[pname][ename]
        end

        if elems.class == Array
            elements = elems.length
        else
            elements = 1
        end
    end

    return elements, page
end

#retrieve_resource(id) ⇒ Object



1151
1152
1153
# File 'lib/one_helper.rb', line 1151

def retrieve_resource(id)
    factory(id)
end

#set_client(options, client = nil) ⇒ Object



549
550
551
552
553
554
555
# File 'lib/one_helper.rb', line 549

def set_client(options, client=nil)
    if client.nil?
        @client=OpenNebulaHelper::OneHelper.get_client(options, true)
    else
        @client = client
    end
end

#show_resource(id, options) ⇒ Object



963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
# File 'lib/one_helper.rb', line 963

def show_resource(id, options)
    resource = retrieve_resource(id)

    if !options.key? :decrypt
        rc = resource.info
    else
        rc = resource.info(true)
    end

    return -1, rc.message if OpenNebula.is_error?(rc)

    if options[:xml]
        return 0, resource.to_xml(true)
    elsif options[:json]
        # If body is set, the resource contains a JSON inside
        if options[:body]
            return 0, check_resource_xsd(resource)
        else
            return 0, ::JSON.pretty_generate(
                check_resource_xsd(resource)
            )
        end
    elsif options[:yaml]
        return 0, check_resource_xsd(resource).to_yaml(:indent => 4)
    else
        format_resource(resource, options)
        return 0
    end
end

#start_pagerObject


List pool functions



572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
# File 'lib/one_helper.rb', line 572

def start_pager
    pager = ENV['ONE_PAGER'] || 'more'

    # Start pager, defaults to less
    p_r, p_w = IO.pipe

    Signal.trap('PIPE', 'SIG_IGN')
    Signal.trap('PIPE', 'EXIT')

    lpid = fork do
        $stdin.reopen(p_r)

        p_r.close
        p_w.close

        Kernel.select [$stdin]

        exec([pager, pager])
    end

    # Send listing to pager pipe
    $stdout.close
    $stdout = p_w.dup

    p_w.close
    p_r.close

    return lpid
end

#stop_pager(lpid) ⇒ Object



602
603
604
605
606
607
608
609
610
611
612
# File 'lib/one_helper.rb', line 602

def stop_pager(lpid)
    $stdout.close

    begin
        Process.wait(lpid)
    rescue Interrupt
        Process.kill("TERM", lpid)
        Process.wait(lpid)
    rescue Errno::ECHILD
    end
end

#to_id(name) ⇒ Object

Formatters for arguments



1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
# File 'lib/one_helper.rb', line 1043

def to_id(name)
    return 0, name.to_i if name.match(/^[0123456789]+$/)

    rc = get_pool
    return rc if rc.first != 0

    pool     = rc[1]
    poolname = self.class.rname

    OneHelper.name_to_id(name, pool, poolname)
end

#user_name(resource, options = {}) ⇒ Object

Id translation



1024
1025
1026
1027
1028
1029
1030
# File 'lib/one_helper.rb', line 1024

def user_name(resource, options={})
    if options[:numeric]
        resource['UID']
    else
        resource['UNAME']
    end
end