Class: OpenNebulaHelper::OneHelper
- Inherits:
-
Object
- Object
- OpenNebulaHelper::OneHelper
- Defined in:
- lib/one_helper.rb
Direct Known Subclasses
AcctHelper, OneAclHelper, OneClusterHelper, OneDatastoreHelper, OneFlowHelper, OneFlowTemplateHelper, OneGroupHelper, OneHookHelper, OneHostHelper, OneImageHelper, OneMarketPlaceAppHelper, OneMarketPlaceHelper, OneSecurityGroupHelper, OneTemplateHelper, OneUserHelper, OneVMGroupHelper, OneVMHelper, OneVNetHelper, OneVcenterHelper, OneVdcHelper, OneVirtualRouterHelper, OneZoneHelper
Instance Attribute Summary collapse
-
#client ⇒ Object
Returns the value of attribute client.
Class Method Summary collapse
- .client ⇒ Object
- .filterflag_to_i_desc ⇒ Object
- .get_client(options = {}, force = false) ⇒ Object
-
.get_password ⇒ Object
This function is copied from ruby net/imap.rb.
- .list_to_id_desc ⇒ Object
- .name_to_id(name, pool, ename) ⇒ Object
- .set_endpoint(endpoint) ⇒ Object
- .set_password(password) ⇒ Object
- .set_user(user) ⇒ Object
- .table_conf(conf_file = self.conf_file) ⇒ Object
- .to_id_desc ⇒ Object
Instance Method Summary collapse
-
#check_orphan(pool, xpath, resource_name, attributes) ⇒ Object
Check if a resource defined by attributes is referenced in pool.
- #create_resource(options, &block) ⇒ Object
- #filterflag_to_i(str) ⇒ Object
- #group_name(resource, options = {}) ⇒ Object
-
#initialize(secret = nil, endpoint = nil) ⇒ OneHelper
constructor
A new instance of OneHelper.
- #list_pool(options, top = false, filter_flag = nil) ⇒ Object
-
#list_pool_format(pool, options, filter_flag) ⇒ Object
———————————————————————– List pool in JSON format, pagination is used in interactive output ———————————————————————–.
-
#list_pool_table(table, pool, options, filter_flag) ⇒ Object
———————————————————————– List the pool in table form, it uses pagination for interactive output ———————————————————————–.
-
#list_pool_top(table, pool, options) ⇒ Object
———————————————————————– List pool table in top-like form ———————————————————————–.
-
#list_pool_xml(pool, options, filter_flag) ⇒ Object
———————————————————————– List pool in XML format, pagination is used in interactive output ———————————————————————–.
- #list_to_id(names) ⇒ Object
- #perform_action(id, options, verbose, &block) ⇒ Object
- #perform_actions(ids, options, verbose, &block) ⇒ Object
- #print_page(pool, options) ⇒ Object
- #retrieve_resource(id) ⇒ Object
- #set_client(options, client = nil) ⇒ Object
- #show_resource(id, options) ⇒ Object
-
#start_pager ⇒ Object
———————————————————————– List pool functions ———————————————————————–.
- #stop_pager(lpid) ⇒ Object
-
#to_id(name) ⇒ Object
Formatters for arguments.
-
#user_name(resource, options = {}) ⇒ Object
Id translation.
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
#client ⇒ Object
Returns the value of attribute client.
455 456 457 |
# File 'lib/one_helper.rb', line 455 def client @client end |
Class Method Details
.client ⇒ Object
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_desc ⇒ Object
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(={}, force=false) if !force && defined?(@@client) @@client else secret=nil password=nil if defined?(@@user) user=@@user password=@@password if defined?(@@password) else user=[:user] end if user password=password||[:password]||self.get_password secret="#{user}:#{password}" end if defined?(@@endpoint) endpoint=@@endpoint else endpoint=[: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 [:sync] = sync @@client=OpenNebula::Client.new(secret, endpoint, ) end end |
.get_password ⇒ Object
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_desc ⇒ Object
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_desc ⇒ Object
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 => …
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(, &block) resource = factory rc = block.call(resource) if OpenNebula.is_error?(rc) return -1, rc. 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, ={}) if [: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(, top=false, filter_flag=nil) # Capture Broken pipe Signal.trap('PIPE', 'EXIT') table = format_pool() if [: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, ) elsif [:xml] return list_pool_xml(pool, , filter_flag) elsif [:json] list_pool_format(pool, , filter_flag) do |pool| hash = check_resource_xsd(pool, pname) puts ::JSON.pretty_generate(hash) end elsif [:yaml] list_pool_format(pool, , filter_flag) do |pool| hash = check_resource_xsd(pool, pname) puts hash.to_yaml(:indent => 4) end else return list_pool_table(table, pool, , 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, , filter_flag) extended = .include?(:extended) && [:extended] if $stdout.isatty and (!.key?:no_pager) size = $stdout.winsize[0] - 1 # ----------- First page, check if pager is needed ------------- rc = pool.get_page(size, 0, extended, [:state]) ps = "" return -1, rc. if OpenNebula.is_error?(rc) elements = get_format_size(pool, ) 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, [:state]) return -1, rc. if OpenNebula.is_error?(rc) current += size begin Process.waitpid(ppid, Process::WNOHANG) rescue Errno::ECHILD break end elements = get_format_size(pool, ) 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. 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, , filter_flag) if $stdout.isatty and (!.key?:no_pager) size = $stdout.winsize[0] - 1 # ----------- First page, check if pager is needed ------------- rc = pool.get_page(size, 0, false, [:state]) ps = "" return -1, rc. if OpenNebula.is_error?(rc) elements, hash = print_page(pool, ) ppid = -1 if elements >= size ppid = start_pager end table.show(hash, ) 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 [:no_header] = true loop do rc = pool.get_page(size, current, false, [:state]) return -1, rc. if OpenNebula.is_error?(rc) current += size begin Process.waitpid(ppid, Process::WNOHANG) rescue Errno::ECHILD break end elements, hash = print_page(pool, ) table.show(hash, ) $stdout.flush break if elements < size end stop_pager(ppid) else rc = pool.info return -1, rc. if OpenNebula.is_error?(rc) elements, hash = print_page(pool, ) if [:ids] && elements hash = [hash[pool.pool_name][pool.element_name]].flatten hash.reject! do |element| ![:ids].include?(element['ID'].to_i) end end table.show(hash, ) 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, ) table.top() { array = pool.get_hash return -1, array. 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, , filter_flag) extended = .include?(:extended) && [:extended] if $stdout.isatty and (!.key?:no_pager) size = $stdout.winsize[0] - 1 # ----------- First page, check if pager is needed ------------- rc = pool.get_page(size, 0, extended, [:state]) ps = "" return -1, rc. if OpenNebula.is_error?(rc) pname = pool.pool_name elements, page = print_page(pool, ) 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, [:state]) return -1, rc. if OpenNebula.is_error?(rc) current += size begin Process.waitpid(ppid, Process::WNOHANG) rescue Errno::ECHILD break end elements, page = print_page(pool, ) 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. 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, , verbose, &block) resource = retrieve_resource(id) rc = block.call(resource) if OpenNebula.is_error?(rc) return -1, rc. else if [: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,,verbose,&block) exit_code = 0 ids.each do |id| rc = perform_action(id,,verbose,&block) unless rc[0]==0 STDERR.puts rc[1] exit_code=rc[0] end end exit_code end |
#print_page(pool, options) ⇒ Object
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, ) elements = 0 page = "" if [:xml] elements += 1 page << pool.to_xml(true) else pname = pool.pool_name ename = pool.element_name if [: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(, client=nil) if client.nil? @client=OpenNebulaHelper::OneHelper.get_client(, 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, ) resource = retrieve_resource(id) if !.key? :decrypt rc = resource.info else rc = resource.info(true) end return -1, rc. if OpenNebula.is_error?(rc) if [:xml] return 0, resource.to_xml(true) elsif [:json] # If body is set, the resource contains a JSON inside if [:body] return 0, check_resource_xsd(resource) else return 0, ::JSON.pretty_generate( check_resource_xsd(resource) ) end elsif [:yaml] return 0, check_resource_xsd(resource).to_yaml(:indent => 4) else format_resource(resource, ) return 0 end end |
#start_pager ⇒ Object
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, ={}) if [:numeric] resource['UID'] else resource['UNAME'] end end |