Parses `/usr/sbin/xm list` and returns a comma-separated list of
domains. Based on a patch submitted by Jonas Genannt.
---

 Jonas,

 Thanks for sending us your patch. I've made a few minor changes but kept the
 behavior mostly the same.

 I've modified the implementation to use Resolution.exec instead of %x{} and
 I've modified the spec to stub its call. I've also added a spec for the case
 when /usr/sbin/xm isn't available that states that it should return nil. This
 is a change in design but will have end-user behavior equivalent to the
 previous behavior of returning "".

 I've split the string returned by Resolution.exec on "\n" to give an array of
 lines because String#each has been removed in Ruby 1.9.

 I've also refactored the implementation of get_domains to make use of the
 map/reduce-like functionality of Ruby's #reject and #map.

 lib/facter/util/xendomains.rb |   10 ++++++++++
 lib/facter/xendomains.rb      |   10 ++++++++++
 spec/unit/data/xendomains     |    4 ++++
 spec/unit/util/xendomains.rb  |   23 +++++++++++++++++++++++
 4 files changed, 47 insertions(+), 0 deletions(-)
 create mode 100644 lib/facter/util/xendomains.rb
 create mode 100644 lib/facter/xendomains.rb
 create mode 100644 spec/unit/data/xendomains
 create mode 100644 spec/unit/util/xendomains.rb

diff --git a/lib/facter/util/xendomains.rb b/lib/facter/util/xendomains.rb
new file mode 100644
index 0000000..4f590a8
--- /dev/null
+++ b/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
diff --git a/lib/facter/xendomains.rb b/lib/facter/xendomains.rb
new file mode 100644
index 0000000..972ac90
--- /dev/null
+++ b/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
diff --git a/spec/unit/data/xendomains b/spec/unit/data/xendomains
new file mode 100644
index 0000000..9b112bc
--- /dev/null
+++ b/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
diff --git a/spec/unit/util/xendomains.rb b/spec/unit/util/xendomains.rb
new file mode 100644
index 0000000..a0fa345
--- /dev/null
+++ b/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
-- 
1.7.0.4

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/puppet-dev?hl=en.

Reply via email to