Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (4.91 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/Cluster'
39
require 'OpenNebula/ClusterPool'
40
require 'OpenNebula/Template'
41
require 'OpenNebula/TemplatePool'
42
require 'OpenNebula/Group'
43
require 'OpenNebula/GroupPool'
44

    
45
module OpenNebula
46

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

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

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

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

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

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

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

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

    
103

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

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

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

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

    
125
        def call(action, *args)
126

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

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

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