|
|
@ -1,6 +1,7 @@ |
|
|
module Lxd::API |
|
|
module Lxd::API |
|
|
def self.get host, certificate = nil |
|
|
def self.get host, certificate = nil |
|
|
certificate ||= host.certificate |
|
|
certificate ||= host.certificate |
|
|
|
|
|
|
|
|
uri = URI.parse host.uri |
|
|
uri = URI.parse host.uri |
|
|
con = Net::HTTP.new uri.host, uri.port ? uri.port : 8443 |
|
|
con = Net::HTTP.new uri.host, uri.port ? uri.port : 8443 |
|
|
con.use_ssl = true |
|
|
con.use_ssl = true |
|
|
@ -8,14 +9,33 @@ module Lxd::API |
|
|
con.key = OpenSSL::PKey::RSA.new certificate.key |
|
|
con.key = OpenSSL::PKey::RSA.new certificate.key |
|
|
con.verify_mode = OpenSSL::SSL::VERIFY_NONE |
|
|
con.verify_mode = OpenSSL::SSL::VERIFY_NONE |
|
|
|
|
|
|
|
|
resp = self.call con, Net::HTTP::Get.new('/') |
|
|
|
|
|
return Lxd::API::V1_0.new con if resp['metadata'].include? '/1.0' |
|
|
|
|
|
raise "unsupported api version" |
|
|
|
|
|
|
|
|
resp = call con, Net::HTTP::Get.new('/') |
|
|
|
|
|
api = Lxd::API::V1_0.new con if resp['metadata'].include? '/1.0' |
|
|
|
|
|
raise Lxd::API::Exception 'unsupported api version' unless api |
|
|
|
|
|
return api unless block_given? |
|
|
|
|
|
yield api |
|
|
|
|
|
rescue Lxd::API::Exception => e |
|
|
|
|
|
Rails.logger.error { "#{e.message} #{e.backtrace.join("\n")}" } |
|
|
|
|
|
nil |
|
|
|
|
|
rescue => e |
|
|
|
|
|
Rails.logger.error { |
|
|
|
|
|
format( |
|
|
|
|
|
'Error connecting: %s, %s %s', |
|
|
|
|
|
host.uri, e.message, e.backtrace.join("\n") |
|
|
|
|
|
) |
|
|
|
|
|
} |
|
|
|
|
|
nil |
|
|
|
|
|
ensure |
|
|
|
|
|
con.close if block_given? |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
def self.call con, req |
|
|
def self.call con, req |
|
|
resp = con.request req |
|
|
resp = con.request req |
|
|
raise "request failure: " + resp.code unless resp.code != 200 |
|
|
|
|
|
|
|
|
unless resp.code != 200 |
|
|
|
|
|
raise Lxd::API::Exception( |
|
|
|
|
|
"request failure: (#{resp.code}) #{resp.message}" |
|
|
|
|
|
) |
|
|
|
|
|
end |
|
|
JSON.parse resp.body |
|
|
JSON.parse resp.body |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
@ -25,6 +45,17 @@ module Lxd::API |
|
|
|
|
|
|
|
|
def call req |
|
|
def call req |
|
|
handle_response(Lxd::API.call @con, req) |
|
|
handle_response(Lxd::API.call @con, req) |
|
|
|
|
|
rescue Lxd::API::Exception => e |
|
|
|
|
|
Rails.logger.error { "#{e.message} #{e.backtrace.join("\n")}" } |
|
|
|
|
|
nil |
|
|
|
|
|
rescue => e |
|
|
|
|
|
Rails.logger.error { |
|
|
|
|
|
format( |
|
|
|
|
|
'Error connecting: %s, %s %s', |
|
|
|
|
|
host.uri, e.message, e.backtrace.join("\n") |
|
|
|
|
|
) |
|
|
|
|
|
} |
|
|
|
|
|
nil |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
def get uri |
|
|
def get uri |
|
|
|