Statistics
| Branch: | Tag: | Revision:

one / src / oca / ruby / OpenNebula.rb @ bfaabf35

History | View | Annotate | Download (4.87 KB)

1
# -------------------------------------------------------------------------- #
2
# Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org)             #
3
#                                                                            #
4
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
5
# not use this file except in compliance with the License. You may obtain    #
6
# a copy of the License at                                                   #
7
#                                                                            #
8
# http://www.apache.org/licenses/LICENSE-2.0                                 #
9
#                                                                            #
10
# Unless required by applicable law or agreed to in writing, software        #
11
# distributed under the License is distributed on an "AS IS" BASIS,          #
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
13
# See the License for the specific language governing permissions and        #
14
# limitations under the License.                                             #
15
#--------------------------------------------------------------------------- #
16

    
17
begin # require 'rubygems'
18
    require 'rubygems'
19
rescue Exception
20
end
21

    
22
require 'xmlrpc/client'
23
require 'digest/sha1'
24
require 'rexml/document'
25
require 'pp'
26

    
27
require 'OpenNebula/XMLUtils'
28
require 'OpenNebula/VirtualMachine'
29
require 'OpenNebula/VirtualMachinePool'
30
require 'OpenNebula/VirtualNetwork'
31
require 'OpenNebula/VirtualNetworkPool'
32
require 'OpenNebula/Image'
33
require 'OpenNebula/ImagePool'
34
require 'OpenNebula/User'
35
require 'OpenNebula/UserPool'
36
require 'OpenNebula/Host'
37
require 'OpenNebula/HostPool'
38
require 'OpenNebula/Template'
39
require 'OpenNebula/TemplatePool'
40
require 'OpenNebula/Group'
41
require 'OpenNebula/GroupPool'
42
require 'OpenNebula/Acl'
43

    
44
module OpenNebula
45

    
46
    # -------------------------------------------------------------------------
47
    # The Error Class represents a generic error in the OpenNebula
48
    # library. It contains a readable representation of the error.
49
    # Any function in the OpenNebula module will return an Error
50
    # object in case of error.
51
    # -------------------------------------------------------------------------
52
    class Error
53
        attr_reader :message
54

    
55
        # +message+ a description of the error
56
        def initialize(message=nil)
57
            @message=message
58
        end
59

    
60
        def to_str()
61
            @message
62
        end
63
    end
64

    
65
    # -------------------------------------------------------------------------
66
    # Returns true if the object returned by a method of the OpenNebula
67
    # library is an Error
68
    # -------------------------------------------------------------------------
69
    def self.is_error?(value)
70
        value.class==OpenNebula::Error
71
    end
72

    
73
    # -------------------------------------------------------------------------
74
    # The client class, represents the connection with the core and handles the
75
    # xml-rpc calls.
76
    # -------------------------------------------------------------------------
77
    class Client
78
        attr_accessor :one_auth
79

    
80
        begin
81
            require 'xmlparser'
82
            XMLPARSER=true
83
        rescue LoadError
84
            XMLPARSER=false
85
        end
86

    
87
        def initialize(secret=nil, endpoint=nil)
88
            if secret
89
                one_secret = secret
90
            elsif ENV["ONE_AUTH"] and !ENV["ONE_AUTH"].empty? and File.file?(ENV["ONE_AUTH"])
91
                one_secret=File.read(ENV["ONE_AUTH"])
92
            elsif File.file?(ENV["HOME"]+"/.one/one_auth")
93
                one_secret=File.read(ENV["HOME"]+"/.one/one_auth")
94
            else
95
                raise "ONE_AUTH file not present"
96
            end
97

    
98
            if !one_secret.match(".+:.+")
99
                raise "Authorization file malformed"
100
            end
101

    
102

    
103
            one_secret=~/^(.+?):(.+)$/
104
            user=$1
105
            password=$2
106

    
107
            if password.match(/^plain:/)
108
                @one_auth = "#{user}:#{password.split(':').last}"
109
            else
110
                @one_auth = "#{user}:#{Digest::SHA1.hexdigest(password)}"
111
            end
112

    
113
            if endpoint
114
                @one_endpoint=endpoint
115
            elsif ENV["ONE_XMLRPC"]
116
                @one_endpoint=ENV["ONE_XMLRPC"]
117
            else
118
                @one_endpoint="http://localhost:2633/RPC2"
119
            end
120

    
121
            @server=XMLRPC::Client.new2(@one_endpoint)
122
        end
123

    
124
        def call(action, *args)
125

    
126
            if XMLPARSER
127
                @server.set_parser(XMLRPC::XMLParser::XMLStreamParser.new)
128
            end
129

    
130
            begin
131
                response = @server.call_async("one."+action, @one_auth, *args)
132

    
133
                if response[0] == false
134
                    Error.new(response[1])
135
                else
136
                    response[1] #response[1..-1]
137
                end
138
            rescue Exception => e
139
                Error.new(e.message)
140
            end
141
        end
142
    end
143
end