Browse Source

first commit

master
Erik Hollensbe 9 years ago
commit
30e5cd9ea8
  1. 17
      .gitignore
  2. 4
      Gemfile
  3. 22
      LICENSE.txt
  4. 29
      README.md
  5. 1
      Rakefile
  6. 21
      knife_cookbook_sync.gemspec
  7. 4
      lib/knife_cookbook_sync.rb
  8. 151
      lib/knife_cookbook_sync/chef/knife/cookbook_sync.rb
  9. 3
      lib/knife_cookbook_sync/version.rb

17
.gitignore

@ -0,0 +1,17 @@
*.gem
*.rbc
.bundle
.config
.yardoc
Gemfile.lock
InstalledFiles
_yardoc
coverage
doc/
lib/bundler/man
pkg
rdoc
spec/reports
test/tmp
test/version_tmp
tmp

4
Gemfile

@ -0,0 +1,4 @@
source 'https://rubygems.org'
# Specify your gem's dependencies in knife_cookbook_sync.gemspec
gemspec

22
LICENSE.txt

@ -0,0 +1,22 @@
Copyright (c) 2012 Erik Hollensbe
MIT License
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.

29
README.md

@ -0,0 +1,29 @@
# KnifeCookbookSync
TODO: Write a gem description
## Installation
Add this line to your application's Gemfile:
gem 'knife_cookbook_sync'
And then execute:
$ bundle
Or install it yourself as:
$ gem install knife_cookbook_sync
## Usage
TODO: Write usage instructions here
## Contributing
1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request

1
Rakefile

@ -0,0 +1 @@
require "bundler/gem_tasks"

21
knife_cookbook_sync.gemspec

@ -0,0 +1,21 @@
# -*- encoding: utf-8 -*-
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'knife_cookbook_sync/version'
Gem::Specification.new do |gem|
gem.name = "knife_cookbook_sync"
gem.version = KnifeCookbookSync::VERSION
gem.authors = ["Erik Hollensbe"]
gem.email = ["erik+github@hollensbe.org"]
gem.description = %q{Sync only what's changed -- faster than cookbook upload}
gem.summary = %q{Sync only what's changed -- faster than cookbook upload}
gem.homepage = ""
gem.files = `git ls-files`.split($/)
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.require_paths = ["lib"]
gem.add_dependency 'chef', '~> 10.0'
end

4
lib/knife_cookbook_sync.rb

@ -0,0 +1,4 @@
require "knife_cookbook_sync/version"
module KnifeCookbookSync
end

151
lib/knife_cookbook_sync/chef/knife/cookbook_sync.rb

@ -0,0 +1,151 @@
#!/usr/bin/env ruby
Thread.abort_on_exception = true
module Knife
class CookbookSync < Chef::Knife
deps do
require 'chef/knife/cookbook_metadata'
require 'chef/rest'
require 'chef/checksum_cache'
require 'chef/cookbook_loader'
require 'chef/cookbook_uploader'
require 'chef/cookbook_version'
end
banner "knife cookbook sync [COOKBOOKS...]"
option :dry_run,
:short => '-d',
:long => '--dry-run',
:boolean => true,
:description => "Do a dry run -- do not sync anything, just show what would be synced"
option :all,
:short => '-a',
:long => '--all',
:boolean => true,
:description => "Sync all cookbooks"
option :cookbook_path,
:short => '-o [COOKBOOK PATH]',
:long => '--cookbook-path [COOKBOOK PATH]',
:default => %w[cookbooks site-cookbooks],
:description => "The path that cookbooks should be loaded from (path:path)",
:proc => proc { |x| x.split(":") }
def distill_manifest(cookbook)
files = { }
cookbook.manifest.values.select { |x| x.kind_of?(Array) }.flatten.each { |f| files[f['path']] = f['checksum'] }
# don't check metadata.json since json output is indeterministic, metadata.rb should be all that's needed anw
files.delete('metadata.json')
return files
end
def sync_cookbooks(cookbooks, cl)
uploaded = false
print_mutex = Mutex.new
cookbooks.each do |cookbook|
Thread.new do
upload = false
print_mutex.synchronize do
ui.msg "Checking cookbook '#{cookbook}' for sync necessity"
end
remote_cookbook = Chef::CookbookVersion.load(cookbook.to_s)
local_cookbook = cl[cookbook.to_s] rescue nil
unless local_cookbook
print_mutex.synchronize do
ui.fatal "Cookbook '#{cookbook}' does not exist locally."
end
exit 1
end
if local_cookbook and !remote_cookbook
upload = true
else
remote_files = distill_manifest(remote_cookbook) rescue { }
local_files = distill_manifest(local_cookbook) rescue { }
if local_files.keys.length != remote_files.keys.length
upload = true
else
(local_files.keys + remote_files.keys).uniq.each do |filename|
if local_files[filename] != remote_files[filename]
upload = true
break
end
end
end
end
if upload
print_mutex.synchronize do
ui.msg "sync necessary; uploading '#{cookbook}'"
end
retries_left = 5
begin
#
# XXX
#
# For some godawful reason, if we use the local_cookbook referenced
# above, the MD5 sums are off.
#
# So we reload the cookbooks here because it seems to work, with an
# optional retry if the chef server is being pissy.
#
cl = Chef::CookbookLoader.new(Chef::Config[:cookbook_path])
if config[:dry_run]
print_mutex.synchronize do
ui.warn "dry run: would sync '#{cookbook}'"
end
else
Chef::CookbookUploader.new(cl[cookbook], Chef::Config[:cookbook_path]).upload_cookbook
end
uploaded = true
rescue Exception => e
print_mutex.synchronize do
ui.error "Failed to upload; retrying up to #{retries_left} times"
end
retries_left -= 1
if retries_left > 0
retry
else
raise e
end
end
end
end
end
Thread.list.reject { |x| x == Thread.current }.each(&:join)
# exit with an exit status of 5 if we've uploaded anything.
exit uploaded ? 5 : 0
end
def run
Chef::Config[:cookbook_path] = config[:cookbook_path]
Chef::Cookbook::FileVendor.on_create { |manifest| Chef::Cookbook::FileSystemFileVendor.new(manifest) }
cl = Chef::CookbookLoader.new(Chef::Config[:cookbook_path])
if config[:all]
sync_cookbooks cl.cookbooks.map(&:name), cl
else
sync_cookbooks name_args, cl
end
end
end
end

3
lib/knife_cookbook_sync/version.rb

@ -0,0 +1,3 @@
module KnifeCookbookSync
VERSION = "0.0.1"
end
Loading…
Cancel
Save