diff options
author | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2006-04-04 22:23:08 +0000 |
---|---|---|
committer | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2006-04-04 22:23:08 +0000 |
commit | d8b4b0dbf35b2b183cd62adf591ebf4650448a0d (patch) | |
tree | d51d99be35d0702ffc08bd0e8a28e82d8daf2e6b | |
parent | c0a9e5f2e9df8e6e1aed74653ae675029af8a9bb (diff) | |
download | puppet-d8b4b0dbf35b2b183cd62adf591ebf4650448a0d.tar.gz puppet-d8b4b0dbf35b2b183cd62adf591ebf4650448a0d.tar.xz puppet-d8b4b0dbf35b2b183cd62adf591ebf4650448a0d.zip |
adding -e ability to puppet executable
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@1065 980ebf18-57e1-0310-9a29-db15c13687c0
-rwxr-xr-x | bin/puppet | 26 | ||||
-rwxr-xr-x | ext/module_puppet | 2 | ||||
-rw-r--r-- | lib/puppet.rb | 5 | ||||
-rw-r--r-- | lib/puppet/client/master.rb | 22 | ||||
-rw-r--r-- | lib/puppet/log.rb | 14 | ||||
-rw-r--r-- | lib/puppet/parser/interpreter.rb | 46 | ||||
-rw-r--r-- | lib/puppet/server/master.rb | 23 | ||||
-rwxr-xr-x | test/executables/puppetbin.rb | 33 |
8 files changed, 114 insertions, 57 deletions
diff --git a/bin/puppet b/bin/puppet index b39a5160b..e75e8913f 100755 --- a/bin/puppet +++ b/bin/puppet @@ -27,10 +27,6 @@ # debug:: # Enable full debugging. # -# extclassfile:: -# Specify the location of the class file to load. Only affects the -# +--loadclasses+ option. -# # help:: # Print this help message # @@ -74,10 +70,10 @@ end options = [ [ "--debug", "-d", GetoptLong::NO_ARGUMENT ], - [ "--extclassfile", "-e", GetoptLong::REQUIRED_ARGUMENT ], [ "--help", "-h", GetoptLong::NO_ARGUMENT ], [ "--logdest", "-l", GetoptLong::REQUIRED_ARGUMENT ], - [ "--loadclasses", GetoptLong::NO_ARGUMENT ], + [ "--execute", "-e", GetoptLong::REQUIRED_ARGUMENT ], + [ "--loadclasses", "-L", GetoptLong::NO_ARGUMENT ], [ "--verbose", "-v", GetoptLong::NO_ARGUMENT ], [ "--use-nodes", GetoptLong::NO_ARGUMENT ], [ "--version", "-V", GetoptLong::NO_ARGUMENT ] @@ -93,7 +89,8 @@ verbose = false noop = false logfile = false loadclasses = false -classfile = nil + +code = nil master = { :Local => true @@ -120,8 +117,8 @@ begin verbose = true when "--debug" debug = true - when "--extclassfile" - classfile = arg + when "--execute" + code = arg when "--loadclasses" loadclasses = true when "--logdest" @@ -170,17 +167,22 @@ end Puppet.genconfig Puppet.genmanifest -master[:File] = ARGV.shift +if code + master[:Code] = code +else + master[:Manifest] = ARGV.shift +end +# Allow users to load the classes that puppetd creates. if loadclasses - file = classfile || Puppet[:classfile] + file = Puppet[:classfile] if FileTest.exists?(file) unless FileTest.readable?(file) $stderr.puts "%s is not readable" % file exit(63) end - master[:Classes] = File.read(file).split(/[\s\n]/) + master[:Classes] = File.read(file).split(/[\s\n]+/) end end diff --git a/ext/module_puppet b/ext/module_puppet index 489cc37ba..ee377446b 100755 --- a/ext/module_puppet +++ b/ext/module_puppet @@ -158,7 +158,7 @@ unless setdest Puppet::Log.newdestination(:syslog) end -master[:File] = ARGV.shift +master[:Manifest] = ARGV.shift unless ENV.include?("CFALLCLASSES") $stderr.puts "Cfengine classes must be passed to the module" diff --git a/lib/puppet.rb b/lib/puppet.rb index 65a51458c..21747bdb5 100644 --- a/lib/puppet.rb +++ b/lib/puppet.rb @@ -143,6 +143,7 @@ module Puppet "Whether to just print a manifest to stdout and exit. Only makes sense when used interactively. Takes into account arguments specified on the CLI."], + :color => [true, "Whether to use ANSI colors when logging to the console."], :mkusers => [false, "Whether to create the necessary user and group that puppetd will run as."] @@ -189,7 +190,9 @@ module Puppet :owner => "root", :mode => 0644, :desc => "The file in which puppetd stores a list of the classes - associated with the retrieved configuratiion."}, + associated with the retrieved configuratiion. Can be loaded in + the separate ``puppet`` executable using the ``--loadclasses`` + option."}, :puppetdlog => { :default => "$logdir/puppetd.log", :owner => "root", :mode => 0640, diff --git a/lib/puppet/client/master.rb b/lib/puppet/client/master.rb index cb2d6f444..390c07220 100644 --- a/lib/puppet/client/master.rb +++ b/lib/puppet/client/master.rb @@ -26,7 +26,9 @@ class Puppet::Client::MasterClient < Puppet::Client # objects. For now, just descend into the tree and perform and # necessary manipulations. def apply - Puppet.notice "Beginning configuration run" + unless @local + Puppet.notice "Beginning configuration run" + end dostorage() unless defined? @objects raise Puppet::Error, "Cannot apply; objects not defined" @@ -65,7 +67,9 @@ class Puppet::Client::MasterClient < Puppet::Client Metric.store Metric.graph end - Puppet.notice "Finished configuration run" + unless @local + Puppet.notice "Finished configuration run" + end return transaction end @@ -228,11 +232,7 @@ class Puppet::Client::MasterClient < Puppet::Client "Invalid returned objects of type %s" % objects.class end - if classes = objects.classes - self.setclasses(classes) - else - Puppet.info "No classes to store" - end + self.setclasses(objects.classes) # Clear all existing objects, so we can recreate our stack. if defined? @objects @@ -280,7 +280,15 @@ class Puppet::Client::MasterClient < Puppet::Client end end + # Store the classes in the classfile, but only if we're not local. def setclasses(ary) + if @local + return + end + unless ary and ary.length > 0 + Puppet.info "No classes to store" + return + end begin File.open(Puppet[:classfile], "w") { |f| f.puts ary.join("\n") diff --git a/lib/puppet/log.rb b/lib/puppet/log.rb index 6b8a55c18..1a392ac64 100644 --- a/lib/puppet/log.rb +++ b/lib/puppet/log.rb @@ -195,14 +195,20 @@ module Puppet dest.puts("%s %s (%s): %s" % [msg.time, msg.source, msg.level, msg.to_s]) when :console + color = "" + reset = "" + if Puppet[:color] + color = @colors[msg.level] + reset = RESET + end if msg.source == "Puppet" - puts @colors[msg.level] + "%s: %s" % [ + puts color + "%s: %s" % [ msg.level, msg.to_s - ] + RESET + ] + reset else - puts @colors[msg.level] + "%s: %s: %s" % [ + puts color + "%s: %s: %s" % [ msg.level, msg.source, msg.to_s - ] + RESET + ] + reset end when Puppet::Client::LogClient unless msg.is_a?(String) or msg.remote diff --git a/lib/puppet/parser/interpreter.rb b/lib/puppet/parser/interpreter.rb index 4f5f1f69a..730b59e15 100644 --- a/lib/puppet/parser/interpreter.rb +++ b/lib/puppet/parser/interpreter.rb @@ -42,11 +42,11 @@ module Puppet # create our interpreter def initialize(hash) - unless hash.include?(:Manifest) - raise Puppet::DevError, "Interpreter was not passed a manifest" + if @code = hash[:Code] + @file = nil # to avoid warnings + elsif ! @file = hash[:Manifest] + raise Puppet::DevError, "You must provide code or a manifest" end - - @file = hash[:Manifest] @filetimeout = hash[:ParseCheck] || 15 @lastchecked = 0 @@ -258,24 +258,26 @@ module Puppet end def parsefiles - if defined? @parser - # Only check the files every 15 seconds or so, not on - # every single connection - if (Time.now - @lastchecked).to_i >= @filetimeout.to_i - unless @parser.reparse? - @lastchecked = Time.now - return false + if @file + if defined? @parser + # Only check the files every 15 seconds or so, not on + # every single connection + if (Time.now - @lastchecked).to_i >= @filetimeout.to_i + unless @parser.reparse? + @lastchecked = Time.now + return false + end + else + return end - else - return end - end - unless FileTest.exists?(@file) - if @ast - return - else - raise Puppet::Error, "Manifest %s must exist" % @file + unless FileTest.exists?(@file) + if @ast + return + else + raise Puppet::Error, "Manifest %s must exist" % @file + end end end @@ -284,7 +286,11 @@ module Puppet end # should i be creating a new parser each time...? @parser = Puppet::Parser::Parser.new() - @parser.file = @file + if @code + @parser.string = @code + else + @parser.file = @file + end @ast = @parser.parse # Mark when we parsed, so we can check freshness diff --git a/lib/puppet/server/master.rb b/lib/puppet/server/master.rb index 07a476efd..7e719452f 100644 --- a/lib/puppet/server/master.rb +++ b/lib/puppet/server/master.rb @@ -35,10 +35,14 @@ class Server end def initialize(hash = {}) - # FIXME this should all be s/:File/:Manifest/g or something - # build our AST - @file = hash[:File] || Puppet[:manifest] - hash.delete(:File) + args = {} + + # Allow specification of a code snippet or of a file + if code = hash[:Code] + args[:Code] = code + else + args[:Manifest] = hash[:Manifest] || Puppet[:manifest] + end if hash[:Local] @local = hash[:Local] @@ -52,19 +56,18 @@ class Server @ca = nil end - @parsecheck = hash[:FileTimeout] || 15 + args[:ParseCheck] = hash[:FileTimeout] || 15 Puppet.debug("Creating interpreter") - args = {:Manifest => @file, :ParseCheck => @parsecheck} - if hash.include?(:UseNodes) args[:UseNodes] = hash[:UseNodes] elsif @local args[:UseNodes] = false end - # This is only used by the cfengine module + # This is only used by the cfengine module, or if --loadclasses was specified + # in +puppet+. if hash.include?(:Classes) args[:Classes] = hash[:Classes] end @@ -110,7 +113,9 @@ class Server clientip = facts["ipaddress"] end - Puppet.notice("Compiling configuration for %s" % client) + unless @local + Puppet.notice("Compiling configuration for %s" % client) + end begin retobjects = @interpreter.run(client, facts) rescue Puppet::Error => detail diff --git a/test/executables/puppetbin.rb b/test/executables/puppetbin.rb index d787a704d..79fbd2358 100755 --- a/test/executables/puppetbin.rb +++ b/test/executables/puppetbin.rb @@ -22,7 +22,6 @@ class TestPuppetBin < Test::Unit::TestCase def test_execution file = mktestmanifest() - @@tmpfiles << tempfile() output = nil cmd = "puppet" @@ -32,7 +31,9 @@ class TestPuppetBin < Test::Unit::TestCase #cmd += " --fqdn %s" % fqdn cmd += " --confdir %s" % Puppet[:confdir] cmd += " --vardir %s" % Puppet[:vardir] - cmd += " --logdest %s" % "/dev/null" + unless Puppet[:debug] + cmd += " --logdest %s" % "/dev/null" + end assert_nothing_raised { system(cmd + " " + file) @@ -41,6 +42,32 @@ class TestPuppetBin < Test::Unit::TestCase assert(FileTest.exists?(@createdfile), "Failed to create config'ed file") end + + def test_inlineexecution + path = tempfile() + code = "file { '#{path}': ensure => file }" + + output = nil + cmd = "puppet" + if Puppet[:debug] + cmd += " --debug" + end + #cmd += " --fqdn %s" % fqdn + cmd += " --confdir %s" % Puppet[:confdir] + cmd += " --vardir %s" % Puppet[:vardir] + unless Puppet[:debug] + cmd += " --logdest %s" % "/dev/null" + end + + cmd += " -e \"#{code}\"" + + assert_nothing_raised { + system(cmd) + } + assert($? == 0, "Puppet exited with code %s" % $?.to_i) + + assert(FileTest.exists?(path), "Failed to create config'ed file") + end end -# $Id: $ +# $Id$ |