Skip to content

Commit

Permalink
first
Browse files Browse the repository at this point in the history
  • Loading branch information
John Goulah committed Jan 26, 2012
0 parents commit eb41766
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 0 deletions.
33 changes: 33 additions & 0 deletions README.md
@@ -0,0 +1,33 @@
# knife-env-diff

A plugin for Chef::Knife which will diff the cookbook versions of two or more environments.

## Usage

Supply two or more environments to get a diff of their cookbook versions

```
% knife env-diff development production
diffing environment development against production
cookbook: hadoop
development version: = 0.1.0
production version: = 0.1.8
cookbook: mysql
development version: = 0.2.4
production version: = 0.2.5
```

## Installation

#### Script install

Copy the knife-env-diff script from lib/chef/knife/env-diff.rb to your ~/.chef/plugins/knife directory.

#### Gem install

knife-env-diff is available on rubygems.org - if you have that source in your gemrc, you can simply use:

gem install knife-env-diff
18 changes: 18 additions & 0 deletions knife-env-diff.gemspec
@@ -0,0 +1,18 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
require "knife-env-diff/version"

Gem::Specification.new do |s|
s.name = 'knife-env-diff'
s.version = Knife::EnvDiff::VERSION
s.date = '2012-01-02'
s.summary = "A plugin for Chef::Knife which displays the roles that are included recursively within a role and optionally displays all the roles that include it."
s.description = s.summary
s.authors = ["John Goulah"]
s.email = ["jgoulah@gmail.com"]
s.homepage = "https://github.com/jgoulah/knife-env-diff"
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"]
end
86 changes: 86 additions & 0 deletions lib/chef/knife/env-diff.rb
@@ -0,0 +1,86 @@
#
# Author:: John Goulah (<jgoulah@gmail.com>)
# Copyright:: Copyright (c) 2011 John Goulah
#
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
#

module GoulahKnifePlugins
class EnvDiff < Chef::Knife

banner "knife env-diff [ENVIRONMENTS...]"

def run
if name_args.size < 2
ui.error("You need to supply at least two environments")
show_usage
exit 1
end

firstenv = name_args.first
otherenvs = name_args.slice(1, name_args.length - 1)

from_env = {}
env = load_environment(firstenv)
from_env[firstenv] = env.cookbook_versions

to_env = {}
otherenvs.each do |env_name|
env = load_environment(env_name)
to_env[env_name] = env.cookbook_versions
end

ui.msg "diffing environment " + firstenv + " against " + otherenvs.join(', ') + "\n\n"

from_env.each_value do |from_cookbooks|
from_cookbooks.each do |from_cookbook, from_version|

diff_versions = {}

to_env.each do |to_env, to_cookbooks|
to_version = to_cookbooks[from_cookbook] || "missing"
if from_version != to_version
diff_versions[to_env] = to_version
end
end

unless diff_versions.empty?
ui.msg "cookbook: "+ from_cookbook
ui.msg " #{firstenv} version: "+ from_version
diff_versions.each do |env, version|
ui.msg " #{env} version: "+ version
end
ui.msg "\n"
end

end
end

end

def load_environment(env)
e = Chef::Environment.load(env)
return e
rescue Net::HTTPServerException => e
if e.response.code.to_s == "404"
ui.error "The environment #{env} does not exist on the server, aborting."
Chef::Log.debug(e)
exit 1
else
raise
end
end

end
end
5 changes: 5 additions & 0 deletions lib/knife-env-diff/version.rb
@@ -0,0 +1,5 @@
module Knife
module EnvDiff
VERSION = "0.0.1"
end
end

0 comments on commit eb41766

Please sign in to comment.