From b33bdc4a2e947be8a8c680d53809e60c5679e053 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Thu, 30 Jun 2011 22:48:18 -0400 Subject: [PATCH] initial commit --- .gitignore | 4 +++ Gemfile | 9 ++++++ Guardfile | 9 ++++++ Rakefile | 1 + guard-puppet.gemspec | 26 +++++++++++++++ lib/guard/puppet.rb | 30 ++++++++++++++++++ lib/guard/puppet/runner.rb | 32 +++++++++++++++++++ lib/guard/puppet/version.rb | 5 +++ spec/lib/guard/puppet/runner_spec.rb | 47 ++++++++++++++++++++++++++++ spec/lib/guard/puppet_spec.rb | 32 +++++++++++++++++++ spec/spec_helper.rb | 4 +++ 11 files changed, 199 insertions(+) create mode 100644 .gitignore create mode 100644 Gemfile create mode 100644 Guardfile create mode 100644 Rakefile create mode 100644 guard-puppet.gemspec create mode 100644 lib/guard/puppet.rb create mode 100644 lib/guard/puppet/runner.rb create mode 100644 lib/guard/puppet/version.rb create mode 100644 spec/lib/guard/puppet/runner_spec.rb create mode 100644 spec/lib/guard/puppet_spec.rb create mode 100644 spec/spec_helper.rb diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4040c6c --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.gem +.bundle +Gemfile.lock +pkg/* diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..00a84d5 --- /dev/null +++ b/Gemfile @@ -0,0 +1,9 @@ +source "http://rubygems.org" + +# Specify your gem's dependencies in guard-puppet.gemspec +gemspec + +require 'rbconfig' +if RbConfig::CONFIG['host_os'] =~ /linux/ + gem 'libnotify' +end diff --git a/Guardfile b/Guardfile new file mode 100644 index 0000000..767287d --- /dev/null +++ b/Guardfile @@ -0,0 +1,9 @@ +# A sample Guardfile +# More info at https://github.com/guard/guard#readme + +guard 'rspec', :version => 2 do + watch(%r{^spec/.+_spec\.rb$}) + watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" } + watch('spec/spec_helper.rb') { "spec" } +end + diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..c702cfc --- /dev/null +++ b/Rakefile @@ -0,0 +1 @@ +require 'bundler/gem_tasks' diff --git a/guard-puppet.gemspec b/guard-puppet.gemspec new file mode 100644 index 0000000..6ca6f11 --- /dev/null +++ b/guard-puppet.gemspec @@ -0,0 +1,26 @@ +# -*- encoding: utf-8 -*- +$:.push File.expand_path("../lib", __FILE__) +require "guard/puppet/version" + +Gem::Specification.new do |s| + s.name = "guard-puppet" + s.version = Guard::PuppetVersion::VERSION + s.authors = ["John Bintz"] + s.email = ["john@coswellproductions.com"] + s.homepage = "" + s.summary = %q{TODO: Write a gem summary} + s.description = %q{TODO: Write a gem description} + + s.rubyforge_project = "guard-puppet" + + s.files = `git ls-files`.split("\n") + s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") + s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } + s.require_paths = ["lib"] + + s.add_dependency 'guard' + s.add_dependency 'puppet' + + s.add_development_dependency 'rspec', '~> 2.6.0' + s.add_development_dependency 'mocha' +end diff --git a/lib/guard/puppet.rb b/lib/guard/puppet.rb new file mode 100644 index 0000000..9238afa --- /dev/null +++ b/lib/guard/puppet.rb @@ -0,0 +1,30 @@ +require 'guard/guard' + +module ::Guard + class Puppet < ::Guard::Guard + class << self + attr_accessor :is_wrapping_exit + end + + def initialize(watchers = [], options = {}) + super + @options = options + end + + def run_all + UI.info msg = "Applying Puppet configuration..." + Notifier.notify msg, :title => "Puppet Config", :image => :pending + if Runner.new(@options).run != 0 + Notifier.notify "Puppet config failure!", :title => "Puppet Config", :image => :failed + else + Notifier.notify "Puppet config reapplied successfully!", :title => "Puppet Config" + end + end + + def run_on_change(files = []) + run_all + end + end +end + +require 'guard/puppet/runner' diff --git a/lib/guard/puppet/runner.rb b/lib/guard/puppet/runner.rb new file mode 100644 index 0000000..bef3e44 --- /dev/null +++ b/lib/guard/puppet/runner.rb @@ -0,0 +1,32 @@ +require 'guard/puppet' +require 'puppet/util/command_line' +require 'puppet/application/apply' + +module Guard + class Puppet + class Runner + attr_reader :options + + def initialize(options) + @options = options + end + + def run + ::Puppet::Util::CommandLine.new('puppet', command_line_params) + rescue SystemExit => e + e.status + end + + def command_line_params + command = [ "apply" ] + command << "-v" if @options[:verbose] + if @options[:config_dir] + @options[:config_dir] = Dir.pwd if @options[:config_dir] == true + command << %{--confdir="#{@options[:config_dir]}"} if @options[:config_dir] + end + command << @options[:manifest] if @options[:manifest] + command + end + end + end +end diff --git a/lib/guard/puppet/version.rb b/lib/guard/puppet/version.rb new file mode 100644 index 0000000..b627561 --- /dev/null +++ b/lib/guard/puppet/version.rb @@ -0,0 +1,5 @@ +module Guard + module PuppetVersion + VERSION = '0.0.1' + end +end diff --git a/spec/lib/guard/puppet/runner_spec.rb b/spec/lib/guard/puppet/runner_spec.rb new file mode 100644 index 0000000..d5ca8d6 --- /dev/null +++ b/spec/lib/guard/puppet/runner_spec.rb @@ -0,0 +1,47 @@ +require 'spec_helper' +require 'guard/puppet/runner' + +describe Guard::Puppet::Runner do + let(:options) { {} } + let(:runner) { described_class.new(options) } + + describe '#command_line_params' do + subject { runner.command_line_params } + + context 'default' do + it { should == [ 'apply' ] } + end + + context 'verbose' do + let(:options) { { :verbose => true } } + + it { should == [ 'apply', '-v' ] } + end + + context 'config dir with path' do + let(:options) { { :config_dir => '/123' } } + + it { should == [ 'apply', '--confdir="/123"' ] } + end + + context 'config dir with true' do + let(:options) { { :config_dir => true } } + + it { should == [ 'apply', %{--confdir="#{Dir.pwd}"} ] } + end + + context 'manifest' do + let(:options) { { :manifest => '123' } } + + it { should == [ 'apply', '123' ] } + end + end + + describe '#run' do + it 'should return the result of an exit call' do + ::Puppet::Util::CommandLine.expects(:new).raises(SystemExit.new(10)) + + runner.run.should == 10 + end + end +end diff --git a/spec/lib/guard/puppet_spec.rb b/spec/lib/guard/puppet_spec.rb new file mode 100644 index 0000000..d58f519 --- /dev/null +++ b/spec/lib/guard/puppet_spec.rb @@ -0,0 +1,32 @@ +require 'spec_helper' +require 'guard/puppet' + +describe Guard::Puppet do + let(:guard) { described_class.new } + + describe '#run_all' do + before do + Guard::UI.expects(:info) + Guard::Notifier.expects(:notify).with("Applying Puppet configuration...", { :image => :pending, :title => "Puppet Config" }) + Guard::Puppet::Runner.any_instance.expects(:run).returns(return_value) + end + + context 'fails' do + let(:return_value) { 1 } + + it 'should show the failure message' do + Guard::Notifier.expects(:notify).with("Puppet config failure!", :image => :failed, :title => "Puppet Config") + guard.run_all + end + end + + context 'succeeds' do + let(:return_value) { 0 } + + it 'should show the success message' do + Guard::Notifier.expects(:notify).with("Puppet config reapplied successfully!", :title => "Puppet Config") + guard.run_all + end + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..d472384 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,4 @@ +RSpec.configure do |c| + c.mock_with :mocha +end +