Module: CloudClient

Defined in:
lib/cloud/CloudClient.rb

Overview

The CloudClient module contains general functionality to implement a Cloud Client

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =

OpenNebula version

'6.10.1'
DEFAULT_AUTH_FILE =
"/var/lib/one/.one/one_auth"

Class Method Summary collapse

Class Method Details

.get_one_authObject

######################################################################### Gets authorization credentials from ONE_AUTH or default auth file.

Raises an error if authorization is not found #########################################################################



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/cloud/CloudClient.rb', line 73

def self.get_one_auth
    if ENV["ONE_AUTH"] and !ENV["ONE_AUTH"].empty? and
        File.file?(ENV["ONE_AUTH"])
        one_auth=File.read(ENV["ONE_AUTH"]).strip.split(':')
    elsif File.file?(DEFAULT_AUTH_FILE)
        one_auth=File.read(DEFAULT_AUTH_FILE).strip.split(':')
    else
        raise "No authorization data present"
    end

    raise "Authorization data malformed" if one_auth.length < 2

    one_auth
end

.http_start(url, timeout, &block) ⇒ Object

######################################################################### Starts an http connection and calls the block provided. SSL flag is set if needed. #########################################################################



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
# File 'lib/cloud/CloudClient.rb', line 92

def self.http_start(url, timeout, &block)
    host = nil
    port = nil

    if ENV['http_proxy']
        uri_proxy  = URI.parse(ENV['http_proxy'])
        flag = false

        # Check if we need to bypass the proxy
        if ENV['no_proxy']
            ENV['no_proxy'].split(',').each do |item|
                item = item.rstrip.lstrip

                unless (IPAddress(url.host) rescue nil).nil?
                    unless (IPAddress(item) rescue nil).nil?
                        flag |= IPAddress(item).include? IPAddress(url.host)
                    end
                else
                    if (IPAddress(item) rescue nil).nil?
                        flag |= (item == url.host)
                    end
                end
            end
        end

        unless flag
            host = uri_proxy.host
            port = uri_proxy.port
        end
    end

    http = Net::HTTP::Proxy(host, port).new(url.host, url.port)

    if timeout
        http.read_timeout = timeout.to_i
    end

    if url.scheme=='https'
        http.use_ssl = true
        http.verify_mode=OpenSSL::SSL::VERIFY_NONE
    end

    begin
        res = http.start do |connection|
            block.call(connection)
        end
    rescue Errno::ECONNREFUSED => e
        str =  "Error connecting to server (#{e.to_s}).\n"
        str << "Server: #{url.host}:#{url.port}"

        return CloudClient::Error.new(str,"503")
    rescue Errno::ETIMEDOUT => e
        str =  "Error timeout connecting to server (#{e.to_s}).\n"
        str << "Server: #{url.host}:#{url.port}"

        return CloudClient::Error.new(str,"504")
    rescue Timeout::Error => e
        str =  "Error timeout while connected to server (#{e.to_s}).\n"
        str << "Server: #{url.host}:#{url.port}"

        return CloudClient::Error.new(str,"504")
    rescue SocketError => e
        str =  "Error timeout while connected to server (#{e.to_s}).\n"

        return CloudClient::Error.new(str,"503")
    rescue
        return CloudClient::Error.new($!.to_s,"503")
    end

    if res.is_a?(Net::HTTPSuccess)
        res
    else
        CloudClient::Error.new(res.body, res.code)
    end
end

.is_error?(value) ⇒ Boolean

######################################################################### Returns true if the object returned by a method of the OpenNebula library is an Error #########################################################################

Returns:

  • (Boolean)


191
192
193
# File 'lib/cloud/CloudClient.rb', line 191

def self.is_error?(value)
    value.class==CloudClient::Error
end