1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00
fog--fog/lib/fog/libvirt/compute.rb

138 lines
3.9 KiB
Ruby
Raw Normal View History

require 'fog/libvirt/core'
require 'fog/libvirt/models/compute/util/util'
require 'fog/libvirt/models/compute/util/uri'
module Fog
module Compute
class Libvirt < Fog::Service
requires :libvirt_uri
2011-11-21 04:09:03 -05:00
recognizes :libvirt_username, :libvirt_password
recognizes :libvirt_ip_command
model_path 'fog/libvirt/models/compute'
model :server
collection :servers
model :network
collection :networks
model :interface
collection :interfaces
model :volume
collection :volumes
model :pool
collection :pools
model :node
collection :nodes
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
request :clone_volume
request :list_networks
request :destroy_network
request :list_interfaces
request :destroy_interface
request :get_node_info
request :update_display
2012-04-05 10:02:45 -04:00
module Shared
include Fog::Compute::LibvirtUtil
end
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
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
end
class Real
2012-04-05 10:02:45 -04:00
include Shared
attr_reader :client
attr_reader :uri
attr_reader :ip_command
def initialize(options={})
@uri = ::Fog::Compute::LibvirtUtil::URI.new(enhance_uri(options[:libvirt_uri]))
@ip_command = options[:libvirt_ip_command]
# libvirt is part of the gem => ruby-libvirt
begin
require 'libvirt'
rescue LoadError => e
retry if require('rubygems')
raise e.message
end
begin
if options[:libvirt_username] and options[:libvirt_password]
@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]
end
end
else
@client = ::Libvirt::open(uri.uri)
end
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#{$!}")
end
end
def terminate
@client.close if @client and !@client.closed?
end
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
if !::CGI.parse(querystring).key?("socket")
append="&socket=/var/run/libvirt/libvirt-sock"
end
end
end
2012-01-25 07:30:24 -05:00
uri+append
end
end
end
end
end