2011-08-31 16:52:53 -04:00
|
|
|
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'libvirt'))
|
|
|
|
require 'fog/compute'
|
|
|
|
|
2011-08-29 11:24:39 -04:00
|
|
|
require 'fog/libvirt/models/compute/uri'
|
2011-08-01 03:49:09 -04:00
|
|
|
|
2011-06-28 09:47:16 -04:00
|
|
|
module Fog
|
|
|
|
module Compute
|
|
|
|
class Libvirt < Fog::Service
|
|
|
|
|
|
|
|
requires :libvirt_uri
|
|
|
|
|
2011-08-29 11:24:39 -04:00
|
|
|
model_path 'fog/libvirt/models/compute'
|
2011-06-28 09:47:16 -04:00
|
|
|
model :server
|
|
|
|
collection :servers
|
|
|
|
model :network
|
|
|
|
collection :networks
|
|
|
|
model :interface
|
|
|
|
collection :interfaces
|
|
|
|
model :volume
|
|
|
|
collection :volumes
|
|
|
|
model :pool
|
|
|
|
collection :pools
|
2011-08-22 02:51:47 -04:00
|
|
|
model :node
|
|
|
|
collection :nodes
|
2011-06-28 09:47:16 -04:00
|
|
|
|
|
|
|
class Mock
|
|
|
|
|
|
|
|
def initialize(options={})
|
|
|
|
Fog::Mock.not_implemented
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
class Real
|
|
|
|
|
2011-09-13 01:34:18 -04:00
|
|
|
attr_reader :raw
|
2011-08-01 03:49:09 -04:00
|
|
|
attr_reader :uri
|
2011-08-09 09:22:35 -04:00
|
|
|
attr_reader :ip_command
|
|
|
|
|
|
|
|
|
2011-06-28 09:47:16 -04:00
|
|
|
def initialize(options={})
|
2011-08-03 06:48:44 -04:00
|
|
|
@uri = ::Fog::Compute::LibvirtUtil::URI.new(enhance_uri(options[:libvirt_uri]))
|
2011-08-09 09:22:35 -04:00
|
|
|
@ip_command = options[:libvirt_ip_command]
|
2011-06-28 09:47:16 -04:00
|
|
|
|
2011-08-01 03:49:09 -04:00
|
|
|
# libvirt is part of the gem => ruby-libvirt
|
2011-06-28 09:47:16 -04:00
|
|
|
require 'libvirt'
|
2011-08-09 09:22:35 -04:00
|
|
|
|
|
|
|
begin
|
2011-09-12 10:10:12 -04:00
|
|
|
if options[:libvirt_username] and options[:libvirt_password]
|
2011-09-13 01:34:18 -04:00
|
|
|
@raw = ::Libvirt::open_auth(@uri.uri, [::Libvirt::CRED_AUTHNAME, ::Libvirt::CRED_PASSPHRASE]) do |cred|
|
2011-08-18 09:25:24 -04:00
|
|
|
if cred['type'] == ::Libvirt::CRED_AUTHNAME
|
2011-09-12 10:10:12 -04:00
|
|
|
res = options[:libvirt_username]
|
2011-08-18 09:25:24 -04:00
|
|
|
elsif cred["type"] == ::Libvirt::CRED_PASSPHRASE
|
|
|
|
res = options[:libvirt_password]
|
|
|
|
else
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
2011-09-13 01:34:18 -04:00
|
|
|
@raw = ::Libvirt::open(@uri.uri)
|
2011-08-18 09:25:24 -04:00
|
|
|
end
|
|
|
|
|
2011-08-09 09:22:35 -04:00
|
|
|
rescue ::Libvirt::ConnectionError
|
|
|
|
raise Fog::Errors::Error.new("Error making a connection to libvirt URI #{@uri.uri}:\n#{$!}")
|
|
|
|
end
|
|
|
|
|
2011-06-28 09:47:16 -04:00
|
|
|
end
|
|
|
|
|
2011-08-03 06:48:44 -04:00
|
|
|
def enhance_uri(uri)
|
|
|
|
require 'cgi'
|
|
|
|
append=""
|
|
|
|
|
|
|
|
# on macosx, chances are we are using libvirt through homebrew
|
|
|
|
# the client will default to a socket location based on it's own location (/opt)
|
|
|
|
# we conveniently point it to /var/run/libvirt/libvirt-sock
|
|
|
|
# if no socket option has been specified explicitly
|
|
|
|
|
|
|
|
if RUBY_PLATFORM =~ /darwin/
|
|
|
|
querystring=::URI.parse(uri).query
|
|
|
|
if querystring.nil?
|
|
|
|
append="?socket=/var/run/libvirt/libvirt-sock"
|
|
|
|
else
|
2011-08-09 09:22:35 -04:00
|
|
|
if !::CGI.parse(querystring).has_key?("socket")
|
|
|
|
append="&socket=/var/run/libvirt/libvirt-sock"
|
|
|
|
end
|
2011-08-03 06:48:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
newuri=uri+append
|
|
|
|
return newuri
|
|
|
|
end
|
2011-08-09 09:22:35 -04:00
|
|
|
|
|
|
|
|
2011-06-28 09:47:16 -04:00
|
|
|
# hack to provide 'requests'
|
|
|
|
def method_missing(method_sym, *arguments, &block)
|
2011-09-13 01:34:18 -04:00
|
|
|
if @raw.respond_to?(method_sym)
|
|
|
|
@raw.send(method_sym, *arguments)
|
2011-06-28 09:47:16 -04:00
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
2011-08-09 09:22:35 -04:00
|
|
|
|
2011-06-28 09:47:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|