Skip to content
This repository has been archived by the owner on Nov 29, 2017. It is now read-only.

Commit

Permalink
Merge branch 'ticket/master/6805'
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklewis committed Mar 21, 2011
2 parents 84ba21e + 63f33d0 commit 0f4a430
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/puppet/application/configurer.rb
@@ -0,0 +1,23 @@
require 'puppet/application'
require 'puppet/interface'

class Puppet::Application::Configurer < Puppet::Application
should_parse_config
run_mode :agent

option("--debug","-d")
option("--verbose","-v")

def setup
if options[:debug] or options[:verbose]
Puppet::Util::Log.level = options[:debug] ? :debug : :info
end

Puppet::Util::Log.newdestination(:console)
end

def run_command
report = Puppet::Interface::Configurer.synchronize(Puppet[:certname])
Puppet::Interface::Report.submit(report)
end
end
2 changes: 2 additions & 0 deletions lib/puppet/interface.rb
Expand Up @@ -7,6 +7,8 @@ class Puppet::Interface
include Puppet::Interface::ActionManager
extend Puppet::Interface::ActionManager

include Puppet::Util

# This is just so we can search for actions. We only use its
# list of directories to search.
# Can't we utilize an external autoloader, or simply use the $LOAD_PATH? -pvb
Expand Down
32 changes: 32 additions & 0 deletions lib/puppet/interface/catalog.rb
@@ -1,4 +1,36 @@
require 'puppet/interface/indirector'

Puppet::Interface::Indirector.new(:catalog) do
action(:apply) do |catalog|
report = Puppet::Transaction::Report.new("apply")
report.configuration_version = catalog.version

Puppet::Util::Log.newdestination(report)

begin
benchmark(:notice, "Finished catalog run") do
catalog.apply(:report => report)
end
rescue => detail
puts detail.backtrace if Puppet[:trace]
Puppet.err "Failed to apply catalog: #{detail}"
end

report.finalize_report
report
end

action(:download) do |certname,facts|
Puppet::Resource::Catalog.terminus_class = :rest
facts_to_upload = {:facts_format => :b64_zlib_yaml, :facts => CGI.escape(facts.render(:b64_zlib_yaml))}
catalog = nil
retrieval_duration = thinmark do
catalog = Puppet::Interface::Catalog.find(certname, facts_to_upload)
end
catalog = catalog.to_ral
catalog.finalize
catalog.retrieval_duration = retrieval_duration
catalog.write_class_file
catalog
end
end
13 changes: 13 additions & 0 deletions lib/puppet/interface/configurer.rb
@@ -0,0 +1,13 @@
require 'puppet/interface'

Puppet::Interface.new(:configurer) do
action(:synchronize) do |certname|
facts = Puppet::Interface::Facts.find(certname)

catalog = Puppet::Interface::Catalog.download(certname, facts)

report = Puppet::Interface::Catalog.apply(catalog)

report
end
end
9 changes: 9 additions & 0 deletions lib/puppet/interface/report.rb
@@ -1,4 +1,13 @@
require 'puppet/interface/indirector'

Puppet::Interface::Indirector.new(:report) do
action(:submit) do |report|
begin
Puppet::Transaction::Report.terminus_class = :rest
report.save
rescue => detail
puts detail.backtrace if Puppet[:trace]
Puppet.err "Could not send report: #{detail}"
end
end
end
20 changes: 20 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -8,6 +8,26 @@

RSpec.configure do |config|
config.mock_with :mocha

config.before :each do
# Set the confdir and vardir to gibberish so that tests
# have to be correctly mocked.
Puppet[:confdir] = "/dev/null"
Puppet[:vardir] = "/dev/null"

# Avoid opening ports to the outside world
Puppet.settings[:bindaddress] = "127.0.0.1"

@logs = []
Puppet::Util::Log.newdestination(@logs)
end

config.after :each do
Puppet.settings.clear

@logs.clear
Puppet::Util::Log.close_all
end
end

# We need this because the RAL uses 'should' as a method. This
Expand Down
31 changes: 31 additions & 0 deletions spec/unit/application/configurer_spec.rb
@@ -0,0 +1,31 @@
#!/usr/bin/env ruby

require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb')
require 'puppet/application/configurer'
require 'puppet/indirector/catalog/rest'
require 'puppet/indirector/report/rest'
require 'tempfile'

describe "Puppet::Application::Configurer" do
it "should retrieve and apply a catalog and submit a report" do
dirname = Dir.mktmpdir("puppetdir")
Puppet[:vardir] = dirname
Puppet[:confdir] = dirname
Puppet[:certname] = "foo"
@catalog = Puppet::Resource::Catalog.new
@file = Puppet::Resource.new(:file, File.join(dirname, "tmp_dir_resource"), :parameters => {:ensure => :present})
@catalog.add_resource(@file)

@report = Puppet::Transaction::Report.new("apply")
Puppet::Transaction::Report.stubs(:new).returns(@report)

Puppet::Resource::Catalog::Rest.any_instance.stubs(:find).returns(@catalog)
@report.expects(:save)

Puppet::Util::Log.stubs(:newdestination)

Puppet::Application::Configurer.new.run

@report.status.should == "changed"
end
end
25 changes: 25 additions & 0 deletions spec/unit/interface/configurer_spec.rb
@@ -0,0 +1,25 @@
#!/usr/bin/env ruby

require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb')
require 'puppet/interface/configurer'
require 'puppet/indirector/catalog/rest'
require 'tempfile'

describe Puppet::Interface::Configurer do
describe "#synchronize" do
it "should retrieve and apply a catalog and return a report" do
dirname = Dir.mktmpdir("puppetdir")
Puppet[:vardir] = dirname
Puppet[:confdir] = dirname
@catalog = Puppet::Resource::Catalog.new
@file = Puppet::Resource.new(:file, File.join(dirname, "tmp_dir_resource"), :parameters => {:ensure => :present})
@catalog.add_resource(@file)
Puppet::Resource::Catalog::Rest.any_instance.stubs(:find).returns(@catalog)

report = Puppet::Interface::Configurer.synchronize("foo")

report.kind.should == "apply"
report.status.should == "changed"
end
end
end

0 comments on commit 0f4a430

Please sign in to comment.