Skip to content

Instantly share code, notes, and snippets.

@sbotman
Last active December 16, 2015 06:28
Show Gist options
  • Save sbotman/5391247 to your computer and use it in GitHub Desktop.
Save sbotman/5391247 to your computer and use it in GitHub Desktop.
A Knife plugin to list environment cookbooks and version constrains.
#
# Author:: Sander Botman (<[email protected]>)
# Copyright:: Copyright (c) 2013 Sander Botman.
# License:: Apache License, Version 2.0
#
# 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.
#
require 'chef/knife'
class Chef
class Knife
class EnvironmentConstraints < Knife
deps do
require 'chef/environment'
require 'chef/json_compat'
end
banner "knife environment constraints [ ENVIRONMENT ENVIRONMENT ]"
option :mismatch,
:short => "-m",
:long => "--mismatch",
:description => "Only show cookbooks where versions mismatch",
:boolean => true
def run
environments = []
unless @name_args.nil? || @name_args.empty?
@name_args.each { |name| environments << name }
else
environments = Chef::Environment.list
end
cookbook_hash = {}
cookbook_col = {}
object_list = [ ui.color('', :bold) ]
environments.each do |env,url|
unless "#{env}" == "_default"
envdata = Chef::Environment.load(env)
cookbook_ver = envdata::cookbook_versions
cookbook_col.merge!(cookbook_ver)
cookbook_hash["#{env}"] = cookbook_ver
object_list << ui.color("#{env}", :bold)
end
end
if cookbook_col.nil? || cookbook_col.empty?
ui.error "Cannot find any environment cookbook constraints"
exit 1
end
columns = object_list.count
cookbook_col.sort.each do |book,version|
result = []
environments.each do |env,url|
unless cookbook_hash["#{env}"].nil?
if cookbook_hash["#{env}"].include?("#{book}")
result << cookbook_hash["#{env}"]["#{book}"]
end
end
end
unless config[:mismatch] and result.uniq.count == 1
object_list << ui.color("#{book}", :bold)
environments.each do |env,url|
unless cookbook_hash["#{env}"].nil?
if cookbook_hash["#{env}"].include?("#{book}")
tag = cookbook_hash["#{env}"]["#{book}"]
version = ui.color(tag)
version = ui.color(tag, :yellow) if result.uniq.count != 1
version = ui.color('blocked', :red) if tag =~ /^\<.*0.0.0/
object_list << version
else
object_list << ui.color("latest", :green)
end
end
end
end
end
puts ui.list(object_list, :uneven_columns_across, columns)
end
end
end
end
@sbotman
Copy link
Author

sbotman commented Sep 14, 2013

....putting some color in the output.....

< 0.0.0 versions that are applied mean that the cookbook is blocked on the environment.
No the output shows blocked in red to quickly notify these blocks.

Cookbook versions that mismatch will be displayed in yellow.

Added the option -m / --mismatch to only display mismatching versions in the output.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment