Skip to content

Commit

Permalink
(#4989) Add xendomains fact
Browse files Browse the repository at this point in the history
Parses `/usr/sbin/xm list` and returns a comma-separated list of
domains. Based on a patch submitted by Jonas Genannt.
  • Loading branch information
Rein Henrichs authored and Paul Berry committed Nov 10, 2010
1 parent d4df963 commit f007a9d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/facter/util/xendomains.rb
@@ -0,0 +1,10 @@
# A module to gather running Xen Domains
#
module Facter::Util::Xendomains
def self.get_domains
if xm_list = Facter::Util::Resolution.exec('/usr/sbin/xm list')
domains = xm_list.split("\n").reject { |line| line =~ /^(Name|Domain-0)/ }
domains.map { |line| line.split(/\s/)[0] }.join(',')
end
end
end
10 changes: 10 additions & 0 deletions lib/facter/xendomains.rb
@@ -0,0 +1,10 @@
require 'facter/util/xendomains'

Facter.add("xendomains") do
confine :kernel => %w{Linux FreeBSD OpenBSD SunOS}
confine :virtual => 'xen0'

setcode do
Facter::Util::Xendomains.get_domains
end
end
4 changes: 4 additions & 0 deletions spec/unit/data/xendomains
@@ -0,0 +1,4 @@
Name ID Mem VCPUs State Time(s)
Domain-0 0 656 4 r----- 48140.9
web01 48 512 2 -b---- 97651.5
mailserver 53 512 4 -b---- 7536.1
23 changes: 23 additions & 0 deletions spec/unit/util/xendomains.rb
@@ -0,0 +1,23 @@
#!/usr/bin/env ruby

require File.dirname(__FILE__) + '/../../spec_helper'

require 'facter/util/xendomains'

describe Facter::Util::Xendomains do
describe ".get_domains" do
it "should return a list of running Xen Domains on Xen0" do
sample_output_file = File.dirname(__FILE__) + '/../data/xendomains'
xen0_domains = File.read(sample_output_file)
Facter::Util::Resolution.stubs(:exec).with('/usr/sbin/xm list').returns(xen0_domains)
Facter::Util::Xendomains.get_domains.should == %{web01,mailserver}
end

context "when xm list isn't executable" do
it "should be nil" do
Facter::Util::Resolution.stubs(:exec).with('/usr/sbin/xm list').returns(nil)
Facter::Util::Xendomains.get_domains.should == nil
end
end
end
end

0 comments on commit f007a9d

Please sign in to comment.