fog-libvirt 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3f53f3776984eb45a0cff0a08140127ac0690c0c
4
- data.tar.gz: 25533737c8816f30167b33bd5beca51c35c80da6
3
+ metadata.gz: e949af7e23f8db3d43dbecbce7a73ea53f160778
4
+ data.tar.gz: 938426cdef480045883dcfd1095a4e9f881eea2c
5
5
  SHA512:
6
- metadata.gz: 3aa1d4a11bb73f6670e64f8d7a37aa94dcb7d1c1e624a3f27f0142b6f15c391a5431feb45cd0c27b950bfc59aa622102fcee4073eabc1890abc3395fdf14dab1
7
- data.tar.gz: 3e8d6c2df13c613f8151158a901722f51a643f0a25dd77060afebaba3c8f7f6f97855fcd2e8dfcecc11cf11f3163ccb5d30e933f91416db0b13565c06f1457f1
6
+ metadata.gz: f6ab2968b8c425d307fe2d8af1d5797b7521fdad80c5d1baa413b04148e1e6ca694a67e5fb6c8c9897b6c918facd4b1854a9e13709696e62a90b2b7a7141e04e
7
+ data.tar.gz: 3cd19bac7d5ef0de6e9080ab1f9fb2a4497d5cc1d6988b4db19717f35a47809e3e85246b79a919b2e5294939e99bf033e1932da63a700bd8372e1a55f3bc942e
@@ -40,6 +40,7 @@ module Fog
40
40
  request :clone_volume
41
41
  request :list_networks
42
42
  request :destroy_network
43
+ request :dhcp_leases
43
44
  request :list_interfaces
44
45
  request :destroy_interface
45
46
  request :get_node_info
@@ -16,6 +16,10 @@ module Fog
16
16
  super
17
17
  end
18
18
 
19
+ def dhcp_leases(mac, flags = 0)
20
+ service.dhcp_leases(uuid, mac, flags)
21
+ end
22
+
19
23
  def save
20
24
  raise Fog::Errors::Error.new('Creating a new network is not yet implemented. Contributions welcome!')
21
25
  end
@@ -263,88 +263,20 @@ module Fog
263
263
  def addresses(service_arg=service, options={})
264
264
  mac=self.mac
265
265
 
266
- # Aug 24 17:34:41 juno arpwatch: new station 10.247.4.137 52:54:00:88:5a:0a eth0.4
267
- # Aug 24 17:37:19 juno arpwatch: changed ethernet address 10.247.4.137 52:54:00:27:33:00 (52:54:00:88:5a:0a) eth0.4
268
- # Check if another ip_command string was provided
269
- ip_command_global=service_arg.ip_command.nil? ? 'grep $mac /var/log/arpwatch.log|sed -e "s/new station//"|sed -e "s/changed ethernet address//g" |sed -e "s/reused old ethernet //" |tail -1 |cut -d ":" -f 4-| cut -d " " -f 3' : service_arg.ip_command
270
- ip_command_local=options[:ip_command].nil? ? ip_command_global : options[:ip_command]
271
-
272
- ip_command="mac=#{mac}; server_name=#{name}; "+ip_command_local
273
-
274
- ip_address=nil
275
-
276
- if service_arg.uri.ssh_enabled?
277
-
278
- # Retrieve the parts we need from the service to setup our ssh options
279
- user=service_arg.uri.user #could be nil
280
- host=service_arg.uri.host
281
- keyfile=service_arg.uri.keyfile
282
- port=service_arg.uri.port
283
-
284
- # Setup the options
285
- ssh_options={}
286
- ssh_options[:keys]=[ keyfile ] unless keyfile.nil?
287
- ssh_options[:port]=port unless keyfile.nil?
288
- ssh_options[:paranoid]=true if service_arg.uri.no_verify?
289
-
290
- begin
291
- result=Fog::SSH.new(host, user, ssh_options).run(ip_command)
292
- rescue Errno::ECONNREFUSED
293
- raise Fog::Errors::Error.new("Connection was refused to host #{host} to retrieve the ip_address for #{mac}")
294
- rescue Net::SSH::AuthenticationFailed
295
- raise Fog::Errors::Error.new("Error authenticating over ssh to host #{host} and user #{user}")
296
- end
297
-
298
- # Check for a clean exit code
299
- if result.first.status == 0
300
- ip_address=result.first.stdout.strip
301
- else
302
- # We got a failure executing the command
303
- raise Fog::Errors::Error.new("The command #{ip_command} failed to execute with a clean exit code")
304
- end
305
-
306
- else
307
- # It's not ssh enabled, so we assume it is
308
- if service_arg.uri.transport=="tls"
309
- raise Fog::Errors::Error.new("TlS remote transport is not currently supported, only ssh")
310
- end
311
-
312
- # Execute the ip_command locally
313
- # Initialize empty ip_address string
314
- ip_address=""
315
-
316
- IO.popen("#{ip_command}") do |p|
317
- p.each_line do |l|
318
- ip_address+=l
319
- end
320
- status=Process.waitpid2(p.pid)[1].exitstatus
321
- if status!=0
322
- raise Fog::Errors::Error.new("The command #{ip_command} failed to execute with a clean exit code")
266
+ ip_address = nil
267
+ nic = self.nics.find {|nic| nic.mac==mac}
268
+ if !nic.nil?
269
+ Fog::Compute[:libvirt].networks.all.each do |net|
270
+ if net.name == nic.network
271
+ leases = net.dhcp_leases(mac, 0)
272
+ # Assume the lease expiring last is the current IP address
273
+ ip_address = leases.sort_by { |lse| lse["expirytime"] }.last["ipaddr"] if !leases.empty?
274
+ break
323
275
  end
324
276
  end
325
-
326
- #Strip any new lines from the string
327
- ip_address=ip_address.chomp
328
- end
329
-
330
- # The Ip-address command has been run either local or remote now
331
-
332
- if ip_address==""
333
- #The grep didn't find an ip address result"
334
- ip_address=nil
335
- else
336
- # To be sure that the command didn't return another random string
337
- # We check if the result is an actual ip-address
338
- # otherwise we return nil
339
- unless ip_address=~/^(\d{1,3}\.){3}\d{1,3}$/
340
- raise Fog::Errors::Error.new(
341
- "The result of #{ip_command} does not have valid ip-address format\n"+
342
- "Result was: #{ip_address}\n"
343
- )
344
- end
345
277
  end
346
278
 
347
- return { :public => [ip_address], :private => [ip_address]}
279
+ return { :public => [ip_address], :private => [ip_address] }
348
280
  end
349
281
 
350
282
  def ip_address(key)
@@ -0,0 +1,37 @@
1
+ require 'socket'
2
+
3
+ module Fog
4
+ module Compute
5
+ class Libvirt
6
+ class Real
7
+ def dhcp_leases(uuid, mac, flags = 0)
8
+ client.lookup_network_by_uuid(uuid).dhcp_leases(mac, flags)
9
+ end
10
+ end
11
+
12
+ class Mock
13
+ def dhcp_leases(uuid, mac, flags = 0)
14
+ leases1 = {
15
+ 'aa:bb:cc:dd:ee:ff' => [
16
+ { 'type' => Socket::AF_INET, 'ipaddr' => '1.2.3.4', 'prefix' => 24, 'expirytime' => 5000 },
17
+ { 'type' => Socket::AF_INET, 'ipaddr' => '1.2.5.6', 'prefix' => 24, 'expirytime' => 5005 }
18
+ ]
19
+ }
20
+ leases2 = {
21
+ '99:88:77:66:55:44' => [
22
+ { 'type' => Socket::AF_INET, 'ipaddr' => '10.1.1.5', 'prefix' => 24, 'expirytime' => 50 }
23
+ ]
24
+ }
25
+ networks = {
26
+ # should match mock net uuid from list_networks.rb
27
+ 'a29146ea-39b2-412d-8f53-239eef117a32' => leases1,
28
+ 'fbd4ac68-cbea-4f95-86ed-22953fd92384' => leases2
29
+ }
30
+ if !networks[uuid].nil?
31
+ return networks[uuid][mac]
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -13,7 +13,7 @@ module Fog
13
13
  client.list_defined_domains.each { |name| data << client.lookup_domain_by_name(name) } unless filter[:defined] == false
14
14
  client.list_domains.each { |id| data << client.lookup_domain_by_id(id) } unless filter[:active] == false
15
15
  end
16
- data.compact.map { |d| domain_to_attributes d }
16
+ data.compact.map { |d| domain_to_attributes d }.compact
17
17
  end
18
18
  end
19
19
 
@@ -51,23 +51,30 @@ module Fog
51
51
 
52
52
  def domain_to_attributes(dom)
53
53
  states= %w(nostate running blocked paused shutting-down shutoff crashed)
54
- {
55
- :id => dom.uuid,
56
- :uuid => dom.uuid,
57
- :name => dom.name,
58
- :max_memory_size => dom.info.max_mem,
59
- :cputime => dom.info.cpu_time,
60
- :memory_size => dom.info.memory,
61
- :cpus => dom.info.nr_virt_cpu,
62
- :autostart => dom.autostart?,
63
- :os_type => dom.os_type,
64
- :active => dom.active?,
65
- :display => domain_display(dom.xml_desc),
66
- :boot_order => boot_order(dom.xml_desc),
67
- :nics => domain_interfaces(dom.xml_desc),
68
- :volumes_path => domain_volumes(dom.xml_desc),
69
- :state => states[dom.info.state]
70
- }
54
+
55
+ begin
56
+ {
57
+ :id => dom.uuid,
58
+ :uuid => dom.uuid,
59
+ :name => dom.name,
60
+ :max_memory_size => dom.info.max_mem,
61
+ :cputime => dom.info.cpu_time,
62
+ :memory_size => dom.info.memory,
63
+ :cpus => dom.info.nr_virt_cpu,
64
+ :autostart => dom.autostart?,
65
+ :os_type => dom.os_type,
66
+ :active => dom.active?,
67
+ :display => domain_display(dom.xml_desc),
68
+ :boot_order => boot_order(dom.xml_desc),
69
+ :nics => domain_interfaces(dom.xml_desc),
70
+ :volumes_path => domain_volumes(dom.xml_desc),
71
+ :state => states[dom.info.state]
72
+ }
73
+ rescue ::Libvirt::RetrieveError, ::Libvirt::Error
74
+ # Catch libvirt exceptions to avoid race conditions involving
75
+ # concurrent libvirt operations (like from another process)
76
+ return nil
77
+ end
71
78
  end
72
79
  end
73
80
 
@@ -37,17 +37,17 @@ module Fog
37
37
 
38
38
  class Mock
39
39
  def list_networks(filters={ })
40
- net1 = mock_network 'net1'
41
- net2 = mock_network 'net2'
42
- [net1, net2]
43
- end
44
-
45
- def mock_network name
46
- {
47
- :uuid => 'net.uuid',
48
- :name => name,
49
- :bridge_name => 'net.bridge_name'
50
- }
40
+ [ {
41
+ :uuid => 'a29146ea-39b2-412d-8f53-239eef117a32',
42
+ :name => 'net1',
43
+ :bridge_name => 'virbr0'
44
+ },
45
+ {
46
+ :uuid => 'fbd4ac68-cbea-4f95-86ed-22953fd92384',
47
+ :name => 'net2',
48
+ :bridge_name => 'virbr1'
49
+ }
50
+ ]
51
51
  end
52
52
  end
53
53
  end
@@ -7,7 +7,13 @@ module Fog
7
7
  if filter.keys.empty?
8
8
  raw_volumes do |pool|
9
9
  pool.list_volumes.each do |volume_name|
10
- data << volume_to_attributes(pool.lookup_volume_by_name(volume_name))
10
+ begin
11
+ data << volume_to_attributes(pool.lookup_volume_by_name(volume_name))
12
+ rescue ::Libvirt::RetrieveError
13
+ # Catch libvirt exceptions to avoid race conditions involving
14
+ # concurrent libvirt operations (like from another process)
15
+ next
16
+ end
11
17
  end
12
18
  end
13
19
  else
@@ -22,16 +28,20 @@ module Fog
22
28
  format_type = xml_element(vol.xml_desc, "/volume/target/format", "type") rescue nil # not all volumes have types, e.g. LVM
23
29
  return nil if format_type == "dir"
24
30
 
25
- {
26
- :pool_name => vol.pool.name,
27
- :key => vol.key,
28
- :id => vol.key,
29
- :path => vol.path,
30
- :name => vol.name,
31
- :format_type => format_type,
32
- :allocation => bytes_to_gb(vol.info.allocation),
33
- :capacity => bytes_to_gb(vol.info.capacity),
34
- }
31
+ begin
32
+ {
33
+ :pool_name => vol.pool.name,
34
+ :key => vol.key,
35
+ :id => vol.key,
36
+ :path => vol.path,
37
+ :name => vol.name,
38
+ :format_type => format_type,
39
+ :allocation => bytes_to_gb(vol.info.allocation),
40
+ :capacity => bytes_to_gb(vol.info.capacity),
41
+ }
42
+ rescue ::Libvirt::RetrieveError, ::Libvirt::Error
43
+ return nil # If there are issues during stat of volume file
44
+ end
35
45
  end
36
46
 
37
47
  def bytes_to_gb bytes
@@ -1,5 +1,5 @@
1
1
  module Fog
2
2
  module Libvirt
3
- VERSION = '0.0.3'
3
+ VERSION = '0.0.4'
4
4
  end
5
5
  end
@@ -10,7 +10,8 @@ Shindo.tests('Fog::Compute[:libvirt]', ['libvirt']) do
10
10
 
11
11
  tests("Compute requests") do
12
12
  %w{ create_domain create_volume define_domain define_pool destroy_interface destroy_network get_node_info list_domains
13
- list_interfaces list_networks list_pools list_pool_volumes list_volumes pool_action vm_action volume_action }.each do |request|
13
+ list_interfaces list_networks list_pools list_pool_volumes list_volumes pool_action vm_action volume_action
14
+ dhcp_leases }.each do |request|
14
15
  test("it should respond to #{request}") { compute.respond_to? request }
15
16
  end
16
17
  end
@@ -6,6 +6,10 @@ Shindo.tests('Fog::Compute[:libvirt] | network model', ['libvirt']) do
6
6
  tests('The network model should') do
7
7
  tests('have the action') do
8
8
  test('reload') { network.respond_to? 'reload' }
9
+ test('dhcp_leases') { network.respond_to? 'dhcp_leases' }
10
+ end
11
+ tests('have a dhcp_leases action that') do
12
+ test('returns an array') { network.dhcp_leases('99:88:77:66:55:44', 0).kind_of? Array }
9
13
  end
10
14
  tests('have attributes') do
11
15
  model_attribute_hash = network.attributes
@@ -20,6 +20,9 @@ Shindo.tests('Fog::Compute[:libvirt] | server model', ['libvirt']) do
20
20
  }
21
21
  end
22
22
  end
23
+ tests('have an ip_address action that') do
24
+ test('returns the latest IP address lease') { server.public_ip_address() == '1.2.5.6' }
25
+ end
23
26
  tests('have attributes') do
24
27
  model_attribute_hash = server.attributes
25
28
  attributes = [ :id,
@@ -0,0 +1,15 @@
1
+ Shindo.tests("Fog::Compute[:libvirt] | dhcp_leases request", 'libvirt') do
2
+
3
+ compute = Fog::Compute[:libvirt]
4
+
5
+ tests("DHCP leases response") do
6
+ response = compute.dhcp_leases("fbd4ac68-cbea-4f95-86ed-22953fd92384", "99:88:77:66:55:44", 0)
7
+ test("should be an array") { response.kind_of? Array }
8
+ test("should have one element") { response.length == 1 }
9
+ test("should have dict elements") { response[0].kind_of? Hash }
10
+ ["ipaddr", "prefix", "expirytime", "type"].each {
11
+ |k| test("should have dict elements with required key #{k}") { !response[0][k].nil? }
12
+ }
13
+ end
14
+
15
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fog-libvirt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - geemus (Wesley Beary)
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-24 00:00:00.000000000 Z
11
+ date: 2016-03-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fog-core
@@ -263,6 +263,7 @@ files:
263
263
  - lib/fog/libvirt/requests/compute/define_pool.rb
264
264
  - lib/fog/libvirt/requests/compute/destroy_interface.rb
265
265
  - lib/fog/libvirt/requests/compute/destroy_network.rb
266
+ - lib/fog/libvirt/requests/compute/dhcp_leases.rb
266
267
  - lib/fog/libvirt/requests/compute/get_node_info.rb
267
268
  - lib/fog/libvirt/requests/compute/list_domains.rb
268
269
  - lib/fog/libvirt/requests/compute/list_interfaces.rb
@@ -299,6 +300,7 @@ files:
299
300
  - tests/libvirt/models/compute/volumes_tests.rb
300
301
  - tests/libvirt/requests/compute/create_domain_tests.rb
301
302
  - tests/libvirt/requests/compute/define_domain_tests.rb
303
+ - tests/libvirt/requests/compute/dhcp_leases_tests.rb
302
304
  - tests/libvirt/requests/compute/update_display.rb
303
305
  homepage: http://github.com/fog/fog-libvirt
304
306
  licenses:
@@ -346,5 +348,6 @@ test_files:
346
348
  - tests/libvirt/models/compute/volumes_tests.rb
347
349
  - tests/libvirt/requests/compute/create_domain_tests.rb
348
350
  - tests/libvirt/requests/compute/define_domain_tests.rb
351
+ - tests/libvirt/requests/compute/dhcp_leases_tests.rb
349
352
  - tests/libvirt/requests/compute/update_display.rb
350
353
  has_rdoc: