-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathcompute.rb
More file actions
130 lines (113 loc) · 3.65 KB
/
compute.rb
File metadata and controls
130 lines (113 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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
require 'fog/libvirt/models/compute/util/util'
require 'fog/libvirt/models/compute/util/uri'
module Fog
module Libvirt
class Compute < Fog::Service
requires :libvirt_uri
recognizes :libvirt_username, :libvirt_password
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
model :tpm
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 :upload_volume
request :clone_volume
request :list_networks
request :destroy_network
request :dhcp_leases
request :list_interfaces
request :destroy_interface
request :get_node_info
request :update_autostart
request :update_display
request :libversion
module Shared
include Fog::Libvirt::Util
attr_reader :client
attr_reader :uri
def initialize(options={})
super()
@uri = ::Fog::Libvirt::Util::URI.new(enhance_uri(options[:libvirt_uri]))
# 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
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 and
# if the socket exists
socketpath="/var/run/libvirt/libvirt-sock"
if RUBY_PLATFORM =~ /darwin/ && File.exist?(socketpath)
querystring=::URI.parse(uri).query
if querystring.nil?
append="?socket=#{socketpath}"
elsif !::CGI.parse(querystring).key?("socket")
append="&socket=#{socketpath}"
end
end
uri+append
end
end
class Real
include Shared
end
class Mock
include Shared
def enhance_uri(uri)
uri = 'test:///default' unless ::URI.parse(uri).scheme == 'test'
super(uri)
end
end
end
end
end