These two patches are a basic attempt to enhance Puppetrun to support
shell globs.

This is still rough.   There are some obvious things wrong yet that I
need to fix:

* Hard coded path to /var/lib/puppet
* Only supports one database source (the filesystem)
* -a still doesn't know how to use the filesystem.

In other words, a simple refactor/cleanup of puppetrun is something
I'm probably going to do as a course of this.
I want to abstract out the sources of 'what nodes we have' in a better way.

Assuming we make 'puppetrun' more part of the app, is there a better
place to put things than 'util' or are there resources I should
be using to do things like 'get the list of available nodes that have
checked in' versus globbing the directory?

(Early patches attached, don't apply them yet)

Another thing I'd like to do is also fix "ralsh -h" so we can query
things via ralsh remotely using the puppetrun infrastructure.
If we do that, and also allow ralsh to use the "Exec" resource, we
have essentially recreated Func in the context of Puppet, and can
do clever things like rapid inventory collection (live) even without
storeconfigs.

--Michael

-- 
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.

From 17f5530ca2b83b8d67c202d6929f7c0f86f129ee Mon Sep 17 00:00:00 2001
From: Michael DeHaan <[email protected]>
Date: Mon, 8 Mar 2010 10:10:11 -0500
Subject: [PATCH 1/2] Initial round of work on puppetrun being able to understand shell globs.

---
 lib/puppet/daemon.rb         |    2 +-
 lib/puppet/network/server.rb |    2 +-
 lib/puppet/util/runner.rb    |   24 ++++++++++++++++++++++++
 3 files changed, 26 insertions(+), 2 deletions(-)
 create mode 100644 lib/puppet/util/runner.rb

diff --git a/lib/puppet/daemon.rb b/lib/puppet/daemon.rb
index 0f538fe..3c7b194 100755
--- a/lib/puppet/daemon.rb
+++ b/lib/puppet/daemon.rb
@@ -44,7 +44,7 @@ class Puppet::Daemon
     def create_pidfile
         Puppet::Util.sync(Puppet[:name]).synchronize(Sync::EX) do
             unless Puppet::Util::Pidlock.new(pidfile).lock
-                raise "Could not create PID file: %s" % [pidfile]
+                raise "Could not create PID file, already running?: %s" % [pidfile]
             end
         end
     end
diff --git a/lib/puppet/network/server.rb b/lib/puppet/network/server.rb
index 01a55df..f926fad 100644
--- a/lib/puppet/network/server.rb
+++ b/lib/puppet/network/server.rb
@@ -34,7 +34,7 @@ class Puppet::Network::Server
     def create_pidfile
         Puppet::Util.sync(Puppet[:name]).synchronize(Sync::EX) do
             unless Puppet::Util::Pidlock.new(pidfile).lock
-                raise "Could not create PID file: %s" % [pidfile]
+                raise "Could not create PID file, already running?: %s" % [pidfile]
             end
         end
     end
diff --git a/lib/puppet/util/runner.rb b/lib/puppet/util/runner.rb
new file mode 100644
index 0000000..e6bd458
--- /dev/null
+++ b/lib/puppet/util/runner.rb
@@ -0,0 +1,24 @@
+# code to support features of the 'puppetrun' application
+
+class Puppet::Util::Runner
+    include Puppet::Util
+
+    def initialize(host_patterns)
+        @host_list=host_patterns
+    end
+
+    def expand_hosts()
+        results = @host_list.inject([]) do |result, element|
+            # FIXME: don't hardcode this path node-path 
+            # TODO:  support more than one 'database' type for sourcing node locations
+            #        for example:  LDAP, Dashoard DB, etc
+            files = Dir.glob("/var/lib/puppet/yaml/node/%s.yml" % element)
+            hostnames = files.map { |f| f.basename.replace(".yml","") }
+            result.extend(hostnames)
+        end
+        puts "expanded host list: %s" % @results
+        results
+    end
+
+end
+
-- 
1.6.3.3

From 46d4bc14efbf65b24f818ba28c9932d67a1df163 Mon Sep 17 00:00:00 2001
From: Michael DeHaan <[email protected]>
Date: Mon, 8 Mar 2010 13:37:07 -0500
Subject: [PATCH 2/2] Puppetrun now understands shell globs.

Use --host '*.example.com' for an example'

Use --host '*' to target all hosts (does not require -a).

Note yaml path is still hardcoded and we'll want this to support multiple 'database' types.
---
 lib/puppet/application/puppetrun.rb |    9 +++++++--
 lib/puppet/util/runner.rb           |   31 +++++++++++++++++++++----------
 2 files changed, 28 insertions(+), 12 deletions(-)

diff --git a/lib/puppet/application/puppetrun.rb b/lib/puppet/application/puppetrun.rb
index 4febcf5..66a614f 100644
--- a/lib/puppet/application/puppetrun.rb
+++ b/lib/puppet/application/puppetrun.rb
@@ -1,5 +1,6 @@
 require 'puppet'
 require 'puppet/application'
+require 'puppet/util/runner'
 
 Puppet.warning "RubyGems not installed" unless Puppet.features.rubygems?
 Puppet.warning "Failed to load ruby LDAP library. LDAP functionality will not be available" unless Puppet.features.ldap?
@@ -41,7 +42,6 @@ Puppet::Application.new(:puppetrun) do
         end
     end
 
-
     dispatch do
         options[:test] ? :test : :main
     end
@@ -55,6 +55,11 @@ Puppet::Application.new(:puppetrun) do
         require 'puppet/network/client'
         require 'puppet/util/ldap/connection'
 
+        # the host list may contain globs/regexes, expand it to get
+        # the actual list of hosts to operate on
+        runner_lib = Puppet::Util::Runner.new(@hosts)
+        @hosts = runner_lib.expand_hosts()
+
         todo = @hosts.dup
 
         failures = []
@@ -108,7 +113,7 @@ Puppet::Application.new(:puppetrun) do
         if options[:ping]
             out = %x{ping -c 1 #{host}}
             unless $? == 0
-                $stderr.print "Could not contact %s\n" % host
+                $stderr.print "Ping failed, could not contact %s\n" % host
                 next
             end
         end
diff --git a/lib/puppet/util/runner.rb b/lib/puppet/util/runner.rb
index e6bd458..6a35e64 100644
--- a/lib/puppet/util/runner.rb
+++ b/lib/puppet/util/runner.rb
@@ -1,23 +1,34 @@
 # code to support features of the 'puppetrun' application
+#
+# Michael DeHaan <[email protected]>
 
 class Puppet::Util::Runner
-    include Puppet::Util
+
+
+    # Constructed with a list of host patterns    
 
     def initialize(host_patterns)
         @host_list=host_patterns
     end
 
+    # Return the host list converting any shell globs to full hostnames
+    # in the future, this may do regexes as with other data sources
+    # fnmatch style globbing makes less sense.  If host name
+    # does not look like a glob, use directly, in case there is not
+    # a file in yaml/node yet (is that possible?)
+    #
+    # FIXME: remove hardcoded /var/lib/puppet node path
+
     def expand_hosts()
-        results = @host_list.inject([]) do |result, element|
-            # FIXME: don't hardcode this path node-path 
-            # TODO:  support more than one 'database' type for sourcing node locations
-            #        for example:  LDAP, Dashoard DB, etc
-            files = Dir.glob("/var/lib/puppet/yaml/node/%s.yml" % element)
-            hostnames = files.map { |f| f.basename.replace(".yml","") }
-            result.extend(hostnames)
+        @host_list.inject([]) do |result, element|
+            if (element =~ /\*|\?|\[\{/)
+                files = Dir.glob("/var/lib/puppet/yaml/node/#{element}.yaml")
+                hostnames = files.map { |filename| File.basename(filename, ".yaml") }
+                result.concat(hostnames)
+            else
+                result.concat(element)
+            end
         end
-        puts "expanded host list: %s" % @results
-        results
     end
 
 end
-- 
1.6.3.3

Reply via email to