Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikolay Sturm committed Mar 9, 2012
0 parents commit 2f3cbb0
Show file tree
Hide file tree
Showing 6 changed files with 379 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License

Copyright (c) Nikolay Sturm <github@erisiandiscord.de>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
63 changes: 63 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
Guard::Knife
============

Knife guard allows to update cookbooks, data bags, environments, and roles
automatically when files are modified.

Install
-------

Please be sure to have [Guard](https://github.com/guard/guard) installed before
continueing.

Install the gem:

$ gem install guard-knife

Add it to your Gemfile

``` ruby
gem 'guard-knife'
```

Add guard definition to your Guardfile by running this command:

$ guard init knife

Guardfile
---------

``` ruby
guard 'knife' do
watch(%r{^cookbooks/.+$})
watch(%r{^data_bags/.+$})
watch(%r{^environments/.+$})
watch(%r{^roles/.+$})
end
```

Options
-------

By default Guard::Knife uses your `~/.chef/knife.rb`. To use a different
configuration file, use the `:config` option:

``` ruby
guard 'knife', :config => '~/.chef/other_knife_config.rb' do
# ...
end
```

Development
-----------

* Source hosted at [GitHub](https://github.com/nistude/guard-knife)
* Report Issues/Questions/Feature requests on [GitHub Issues](https://github.com/nistude/guard-knife/issues)

Pull requests are very welcome! Make sure your patches are well tested. Please
create a topic branch for every separate change you make.

Author
------

[Nikolay Sturm](http://blog.nistude.de/)
154 changes: 154 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
require 'rubygems'
require 'rake'
require 'date'

#############################################################################
#
# Helper functions
#
#############################################################################

def name
@name ||= Dir['*.gemspec'].first.split('.').first
end

def shortname
@shortname ||= name.split('-')[1]
end

def version
line = File.read("lib/guard/#{shortname}.rb")[/^\s*VERSION\s*=\s*.*/]
line.match(/.*VERSION\s*=\s*['"](.*)['"]/)[1]
end

def date
Date.today.to_s
end

def rubyforge_project
name
end

def gemspec_file
"#{name}.gemspec"
end

def gem_file
"#{name}-#{version}.gem"
end

def replace_header(head, header_name)
head.sub!(/(\.#{header_name}\s*= ').*'/) { "#{$1}#{send(header_name)}'"}
end

#############################################################################
#
# Standard tasks
#
#############################################################################

task :default => :test

require 'rake/testtask'
Rake::TestTask.new(:test) do |test|
test.libs << 'lib' << 'test'
test.pattern = 'test/**/test_*.rb'
test.verbose = true
end

desc "Generate RCov test coverage and open in your browser"
task :coverage do
require 'rcov'
sh "rm -fr coverage"
sh "rcov test/test_*.rb"
sh "open coverage/index.html"
end

require 'rake/rdoctask'
Rake::RDocTask.new do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = "#{name} #{version}"
rdoc.rdoc_files.include('README*')
rdoc.rdoc_files.include('lib/**/*.rb')
end

desc "Open an irb session preloaded with this library"
task :console do
sh "irb -rubygems -r ./lib/guard/#{shortname}.rb"
end

#############################################################################
#
# Custom tasks (add your own tasks here)
#
#############################################################################



#############################################################################
#
# Packaging tasks
#
#############################################################################

desc "Create tag v#{version} and build and push #{gem_file} to Rubygems"
task :release => :build do
unless `git branch` =~ /^\* master$/
puts "You must be on the master branch to release!"
exit!
end
sh "git commit --allow-empty -a -m 'Release #{version}'"
sh "git tag v#{version}"
sh "git push origin master"
sh "git push origin v#{version}"
sh "gem push pkg/#{name}-#{version}.gem"
end

desc "Build #{gem_file} into the pkg directory"
task :build => :gemspec do
sh "mkdir -p pkg"
sh "gem build #{gemspec_file}"
sh "mv #{gem_file} pkg"
end

desc "Generate #{gemspec_file}"
task :gemspec => :validate do
# read spec file and split out manifest section
spec = File.read(gemspec_file)
head, manifest, tail = spec.split(" # = MANIFEST =\n")

# replace name version and date
replace_header(head, :name)
replace_header(head, :version)
replace_header(head, :date)
#comment this out if your rubyforge_project has a different name
#replace_header(head, :rubyforge_project)

# determine file list from git ls-files
files = `git ls-files`.
split("\n").
sort.
reject { |file| file =~ /^\./ }.
reject { |file| file =~ /^(rdoc|pkg)/ }.
map { |file| " #{file}" }.
join("\n")

# piece file back together and write
manifest = " s.files = %w[\n#{files}\n ]\n"
spec = [head, manifest, tail].join(" # = MANIFEST =\n")
File.open(gemspec_file, 'w') { |io| io.write(spec) }
puts "Updated #{gemspec_file}"
end

desc "Validate #{gemspec_file}"
task :validate do
libfiles = Dir['lib/guard/*'] - ["lib/guard/#{shortname}.rb", "lib/guard/#{shortname}"]
unless libfiles.empty?
puts "Directory `lib/guard` should only contain a `#{shortname}.rb` file and `#{shortname}` dir."
exit!
end
unless Dir['VERSION*'].empty?
puts "A `VERSION` file at root level violates Gem best practices."
exit!
end
end
36 changes: 36 additions & 0 deletions guard-knife.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
## http://docs.rubygems.org/read/chapter/20
Gem::Specification.new do |s|
s.specification_version = 2 if s.respond_to? :specification_version=
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.rubygems_version = '1.3.5'

s.name = 'guard-knife'
s.version = '0.0.1'
s.date = '2012-03-09'
s.rubyforge_project = ''

s.summary = "Guard for Chef using knife"
s.description = "Guard for Chef using knife to upload files"

s.authors = ["Nikolay Sturm"]
s.email = 'github@erisiandiscord.de'
s.homepage = 'https://github.com/nistude/guard-knife'

s.require_paths = %w[lib]

s.rdoc_options = ["--charset=UTF-8"]
s.extra_rdoc_files = %w[README LICENSE]

s.add_dependency('guard')
s.add_dependency('chef', '>= 0.10')

#s.add_development_dependency('DEVDEPNAME', [">= 1.1.0", "< 2.0.0"])

# = MANIFEST =
s.files = %w[

]
# = MANIFEST =

#s.test_files = s.files.select { |path| path =~ /^test\/test_.*\.rb/ }
end
99 changes: 99 additions & 0 deletions lib/guard/knife.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
require 'guard'
require 'guard/guard'

module Guard
class Knife < Guard
VERSION = '0.0.1'

# Initialize a Guard.
# @param [Array<Guard::Watcher>] watchers the Guard file watchers
# @param [Hash] options the custom Guard options
def initialize(watchers = [], options = {})
super
@options = {
:config => '~/.chef/knife.rb'
}.update(options)
end

# Call once when Guard starts. Please override initialize method to init stuff.
# @raise [:task_has_failed] when start has failed
def start
end

# Called when `stop|quit|exit|s|q|e + enter` is pressed (when Guard quits).
# @raise [:task_has_failed] when stop has failed
def stop
end

# Called when `reload|r|z + enter` is pressed.
# This method should be mainly used for "reload" (really!) actions like reloading passenger/spork/bundler/...
# @raise [:task_has_failed] when reload has failed
def reload
end

# Called when just `enter` is pressed
# This method should be principally used for long action like running all specs/tests/...
# @raise [:task_has_failed] when run_all has failed
def run_all
end

# Called on file(s) modifications that the Guard watches.
# @param [Array<String>] paths the changes files or paths
# @raise [:task_has_failed] when run_on_change has failed
def run_on_change(paths)
paths.each do |path|
next if path.match(/\.swp$/) # vim swap file

upload(path)
end
end

# Called on file(s) deletions that the Guard watches.
# @param [Array<String>] paths the deleted files or paths
# @raise [:task_has_failed] when run_on_change has failed
def run_on_deletion(paths)
end

def upload(path)
if path.match(/^cookbooks\/([^\/]*)\//)
upload_cookbook($1)
elsif path.match(/^data_bags\/(.*)\/(.*)$/)
data_bag = $1
item = $2
upload_databag(data_bag, item)
elsif path.match(/^(environments\/.*\.rb)$/)
upload_environment($1)
elsif path.match(/^(roles\/.*.rb)$/)
upload_role($1)
end
end

def knife_options
options = ''
@options.each do |key, value|
case key
when :config
options += "-c #{value} "
end
end

options
end

def upload_cookbook(cookbook)
system("knife cookbook upload #{cookbook} #{knife_options}")
end

def upload_databag(data_bag, item)
system("knife data bag from file #{data_bag} #{item} #{knife_options}")
end

def upload_environment(environment)
system("knife environment from file #{environment} #{knife_options}")
end

def upload_role(role)
system("knife role from file #{role} #{knife_options}")
end
end
end
6 changes: 6 additions & 0 deletions lib/guard/knife/templates/Guardfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
guard 'knife' do
watch(%r{^cookbooks/.+$})
watch(%r{^data_bags/.+$})
watch(%r{^environments/.+$})
watch(%r{^roles/.+$})
end

0 comments on commit 2f3cbb0

Please sign in to comment.