2014-02-01 21:13:17 -05:00
|
|
|
require 'fog/libvirt/core'
|
2012-04-04 08:03:17 -04:00
|
|
|
require 'fog/libvirt/models/compute/util/util'
|
|
|
|
require 'fog/libvirt/models/compute/util/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
|
2012-04-04 08:03:17 -04:00
|
|
|
requires :libvirt_uri
|
2011-11-21 04:09:03 -05:00
|
|
|
recognizes :libvirt_username, :libvirt_password
|
2011-11-30 05:29:51 -05:00
|
|
|
recognizes :libvirt_ip_command
|
2011-06-28 09:47:16 -04:00
|
|
|
|
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
|
2012-04-04 08:03:17 -04:00
|
|
|
model :nic
|
|
|
|
collection :nics
|
|
|
|
|
|
|
|
request_path 'fog/libvirt/requests/compute'
|
|
|
|
request :list_domains
|
|
|
|
request :create_domain
|
|
|
|
request :define_domain
|
|
|
|
request :vm_action
|
|
|
|
request :list_pools
|
|
|
|
request :list_pool_volumes
|
|
|
|
request :define_pool
|
|
|
|
request :pool_action
|
|
|
|
request :list_volumes
|
|
|
|
request :volume_action
|
|
|
|
request :create_volume
|
2014-03-05 12:09:08 -05:00
|
|
|
request :clone_volume
|
2012-04-04 08:03:17 -04:00
|
|
|
request :list_networks
|
|
|
|
request :destroy_network
|
|
|
|
request :list_interfaces
|
|
|
|
request :destroy_interface
|
|
|
|
request :get_node_info
|
2012-04-08 09:36:06 -04:00
|
|
|
request :update_display
|
2011-06-28 09:47:16 -04:00
|
|
|
|
2012-04-05 10:02:45 -04:00
|
|
|
module Shared
|
|
|
|
include Fog::Compute::LibvirtUtil
|
|
|
|
end
|
|
|
|
|
2011-06-28 09:47:16 -04:00
|
|
|
class Mock
|
2012-04-05 10:02:45 -04:00
|
|
|
include Shared
|
|
|
|
def initialize(options={})
|
|
|
|
# libvirt is part of the gem => ruby-libvirt
|
|
|
|
require 'libvirt'
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2012-05-14 14:58:32 -04:00
|
|
|
|
|
|
|
def client
|
|
|
|
return @client if defined?(@client)
|
|
|
|
end
|
2012-04-05 10:02:45 -04:00
|
|
|
|
|
|
|
#read mocks xml
|
|
|
|
def read_xml(file_name)
|
|
|
|
file_path = File.join(File.dirname(__FILE__),"requests","compute","mock_files",file_name)
|
|
|
|
File.read(file_path)
|
|
|
|
end
|
2011-06-28 09:47:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class Real
|
2012-04-05 10:02:45 -04:00
|
|
|
include Shared
|
2012-04-04 08:03:17 -04:00
|
|
|
attr_reader :client
|
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
|
2012-12-28 16:39:43 -05:00
|
|
|
begin
|
|
|
|
require 'libvirt'
|
|
|
|
rescue LoadError => e
|
|
|
|
retry if require('rubygems')
|
|
|
|
raise e.message
|
|
|
|
end
|
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]
|
2012-04-04 08:03:17 -04:00
|
|
|
@client = ::Libvirt::open_auth(uri.uri, [::Libvirt::CRED_AUTHNAME, ::Libvirt::CRED_PASSPHRASE]) do |cred|
|
|
|
|
case cred['type']
|
|
|
|
when ::Libvirt::CRED_AUTHNAME
|
|
|
|
options[:libvirt_username]
|
|
|
|
when ::Libvirt::CRED_PASSPHRASE
|
|
|
|
options[:libvirt_password]
|
2011-08-18 09:25:24 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
2012-04-04 08:03:17 -04:00
|
|
|
@client = ::Libvirt::open(uri.uri)
|
2011-08-18 09:25:24 -04:00
|
|
|
end
|
|
|
|
|
2011-08-09 09:22:35 -04:00
|
|
|
rescue ::Libvirt::ConnectionError
|
2012-01-25 07:30:24 -05:00
|
|
|
raise Fog::Errors::Error.new("Error making a connection to libvirt URI #{uri.uri}:\n#{$!}")
|
2011-08-09 09:22:35 -04:00
|
|
|
end
|
2011-06-28 09:47:16 -04:00
|
|
|
end
|
|
|
|
|
2012-04-04 08:03:17 -04:00
|
|
|
def terminate
|
|
|
|
@client.close if @client and !@client.closed?
|
|
|
|
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
|
2014-05-21 10:28:51 -04:00
|
|
|
if !::CGI.parse(querystring).key?("socket")
|
2011-08-09 09:22:35 -04:00
|
|
|
append="&socket=/var/run/libvirt/libvirt-sock"
|
|
|
|
end
|
2011-08-03 06:48:44 -04:00
|
|
|
end
|
|
|
|
end
|
2012-01-25 07:30:24 -05:00
|
|
|
uri+append
|
2011-08-03 06:48:44 -04:00
|
|
|
end
|
2011-06-28 09:47:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|