Class: OpenNebula::Client

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

Overview

The client class, represents the connection with the core and handles the xml-rpc calls.

Constant Summary collapse

NO_ONE_AUTH_ERROR =
"ONE_AUTH file not present"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(secret = nil, endpoint = nil, options = {}) ⇒ OpenNebula::Client

Creates a new client object that will be used to call OpenNebula functions.

Parameters:

  • secret (String, nil) (defaults to: nil)

    user credentials (“user:password”) or nil to get the credentials from user auth file

  • endpoint (String, nil) (defaults to: nil)

    OpenNebula server endpoint (host:2633/RPC2) or nil to get it form the environment variable ONE_XMLRPC or use the default endpoint

  • options (Hash) (defaults to: {})
  • params (Hash)

    a customizable set of options



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/opennebula/client.rb', line 123

def initialize(secret=nil, endpoint=nil, options={})
    if secret
        @one_auth = secret
    elsif ENV["ONE_AUTH"] and !ENV["ONE_AUTH"].empty? and
            File.file?(ENV["ONE_AUTH"])
        @one_auth = File.read(ENV["ONE_AUTH"])
    elsif ENV["HOME"] and File.file?(ENV["HOME"]+"/.one/one_auth")
        @one_auth = File.read(ENV["HOME"]+"/.one/one_auth")
    elsif File.file?("/var/lib/one/.one/one_auth")
        @one_auth = File.read("/var/lib/one/.one/one_auth")
    else
        raise NO_ONE_AUTH_ERROR
    end

    @one_auth = @one_auth.rstrip

    if endpoint
        @one_endpoint = endpoint
    elsif ENV["ONE_XMLRPC"]
        @one_endpoint = ENV["ONE_XMLRPC"]
    elsif ENV['HOME'] and File.exist?(ENV['HOME']+"/.one/one_endpoint")
        @one_endpoint = File.read(ENV['HOME']+"/.one/one_endpoint")
    elsif File.exist?("/var/lib/one/.one/one_endpoint")
        @one_endpoint = File.read("/var/lib/one/.one/one_endpoint")
    else
        @one_endpoint = "http://localhost:2633/RPC2"
    end

    @one_endpoint= @one_endpoint.rstrip

    @async = !options[:sync]

    if options[:subscriber_endpoint]
        @one_zmq = options[:subscriber_endpoint]
    elsif ENV["ONE_ZMQ"]
        @one_zmq = ENV["ONE_ZMQ"]
    else
        @one_zmq = 'tcp://localhost:2101'
    end

    timeout=nil
    if options[:timeout]
        timeout = options[:timeout]
    elsif ENV['ONE_XMLRPC_TIMEOUT']
        timeout = ENV['ONE_XMLRPC_TIMEOUT'].to_i
    end

    http_proxy=nil
    http_proxy=options[:http_proxy] if options[:http_proxy]

    @server = XMLRPC::Client.new2(@one_endpoint, http_proxy, timeout)
    @server.http_header_extra = {'accept-encoding' => 'identity'}

    http = @server.instance_variable_get("@http")

    if options[:cert_dir] || ENV['ONE_CERT_DIR']
        raise "SSL options don't work in async mode" if @async

        cert_dir = options[:cert_dir] || ENV['ONE_CERT_DIR']
        cert_files = Dir["#{cert_dir}/*"]

        cert_store = OpenSSL::X509::Store.new
        cert_store.set_default_paths
        cert_files.each {|cert| cert_store.add_file(cert) }

        http.cert_store = cert_store
    end

    if options[:disable_ssl_verify] || ENV['ONE_DISABLE_SSL_VERIFY']
        raise "SSL options don't work in async mode" if @async

        http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    end

    if defined?(OxStreamParser)
        @server.set_parser(OxStreamParser.new)
    elsif OpenNebula::NOKOGIRI
        @server.set_parser(NokogiriStreamParser.new)
    elsif XMLPARSER
        @server.set_parser(XMLRPC::XMLParser::XMLStreamParser.new)
    end
end

Instance Attribute Details

#one_authObject

Returns the value of attribute one_auth.



90
91
92
# File 'lib/opennebula/client.rb', line 90

def one_auth
  @one_auth
end

#one_endpointObject (readonly)

Returns the value of attribute one_endpoint.



91
92
93
# File 'lib/opennebula/client.rb', line 91

def one_endpoint
  @one_endpoint
end

#one_zmqObject (readonly)

Returns the value of attribute one_zmq.



91
92
93
# File 'lib/opennebula/client.rb', line 91

def one_zmq
  @one_zmq
end

Instance Method Details

#call(action, *args) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/opennebula/client.rb', line 206

def call(action, *args)
    begin
        if @async
            response = @server.call_async("one."+action, @one_auth, *args)
        else
            response = @server.call("one."+action, @one_auth, *args)
        end

        if response[0] == false
            Error.new(response[1], response[2])
        else
            response[1] #response[1..-1]
        end
    rescue Exception => e
        Error.new(e.message, Error::EXML_RPC_CALL)
    end
end

#get_versionObject



224
225
226
# File 'lib/opennebula/client.rb', line 224

def get_version()
    call("system.version")
end