Skip to content

Commit

Permalink
Adding a wrapper for cookbook deletion command and options
Browse files Browse the repository at this point in the history
  • Loading branch information
kdaniels committed Nov 15, 2016
1 parent 2d0a6b7 commit 72a3d3a
Show file tree
Hide file tree
Showing 11 changed files with 188 additions and 0 deletions.
132 changes: 132 additions & 0 deletions lib/chef/knife/spork-delete.rb
@@ -0,0 +1,132 @@
require 'chef/knife'

module KnifeSpork
class SporkDelete < Chef::Knife
deps do
require 'chef/exceptions'
require 'knife-spork/runner'
require 'socket'
end

ALL_NO_CONF = 'all_no_conf'.freeze
ALL_CONF = 'all_conf'.freeze
ONE_NO_CONF = 'one_no_conf'.freeze
ONE_CONF = 'one_conf'.freeze

banner 'knife spork delete [COOKBOOKS...] (options)'

option :cookbook_path,
:short => '-o PATH:PATH',
:long => '--cookbook-path PATH:PATH',
:description => 'A colon-separated path to look for cookbooks in',
:proc => lambda { |o| o.split(':') }

option :yes,
:short => '-y',
:long => '--yes',
:description => 'Say yes to all prompts for confirmation'

option :all,
:short => '-a',
:long => '--all',
:description => 'Delete all versions of the specified cookbooks'


def run
self.class.send(:include, KnifeSpork::Runner)
self.config = Chef::Config.merge!(config)
config[:cookbook_path] ||= Chef::Config[:cookbook_path]

if @name_args.empty?
show_usage
ui.error("You must specify the --all flag or at least one cookbook name")
exit 1
end

delete
@misc_output = @deleted_cookbook_string
run_plugins(:after_delete)
end

private

def printable_version_string(version)
# The version variable might be a string or an array. The array might even be empty.
# Based on what it is, we will return a string that says something like "versions 1, 2" or "version 1" or "ALL versions"
# for more human-readable output.
if (version.class == Array and version.size == 0) or version == "ALL"
return "ALL versions"
elsif version.class == Array
if version.size > 1
return "versions #{version.join(', ')}"
else
return "version #{version.join(', ')}"
end
else
return "version #{version}"
end
end

def run_knife_command(cookbook_name, command, version = [])
begin
ui.warn("Deleting cookbook #{cookbook_name}...")
case command
when ALL_NO_CONF
@knife.delete_all_without_confirmation
when ALL_CONF
@knife.delete_all_versions
when ONE_NO_CONF
@knife.delete_versions_without_confirmation(version)
when ONE_CONF
@knife.version = version
@knife.delete_explicit_version
end
ui.msg("Successfully deleted cookbook #{cookbook_name} #{printable_version_string(version)} from the Chef server")
true
rescue SystemExit
# The user said no at a confirmation prompt, just continue. But return false since we
# didn't actually delete anything.
false
rescue Exception => e
ui.error("Error deleting cookbook #{cookbook_name}: #{e}")
false
end
end

def delete
@deleted_cookbooks = []
name_args.each do |cookbook_name|
@knife = Chef::Knife::CookbookDelete.new
@knife.name_args = cookbook_name
@knife.cookbook_name = cookbook_name
if config[:all]
@knife.config[:all] = true
if config[:yes]
@deleted_cookbooks.push([cookbook_name, "ALL"]) if run_knife_command(cookbook_name, ALL_NO_CONF)
else
@deleted_cookbooks.push([cookbook_name, "ALL"]) if run_knife_command(cookbook_name, ALL_CONF)
end
else
begin
versions_to_delete = @knife.ask_which_versions_to_delete
rescue NoMethodError
# Rescuing this means the output from knife itself already gets printed, no need to duplicate that.
exit 1
end
if config[:yes]
@deleted_cookbooks.push([cookbook_name, versions_to_delete]) if run_knife_command(cookbook_name, ONE_NO_CONF, versions_to_delete)
else
versions_to_delete.each do |version|
@deleted_cookbooks.push([cookbook_name, version]) if run_knife_command(cookbook_name, ONE_CONF, version)
end
end
end
end
# This is the formatted string that the plugins will use to print.
@deleted_cookbook_string = ""
@deleted_cookbooks.each {|cookbook, version| @deleted_cookbook_string << "#{cookbook}: #{printable_version_string(version)}, " }
@deleted_cookbook_string.chop!.chop! # Get rid of the trailing , chop chop!
ui.msg("Cookbooks deleted from chef server: #{@deleted_cookbook_string}")
end
end
end
8 changes: 8 additions & 0 deletions lib/knife-spork/plugins/campfire.rb
Expand Up @@ -16,6 +16,14 @@ def after_upload
end
end

def after_delete
campfire do |rooms|
rooms.paste <<-EOH
"#{organization}#{current_user} deleted the following cookbooks: #{misc_output}"
EOH
end
end

def after_promote_remote
campfire do |rooms|
rooms.paste <<-EOH
Expand Down
12 changes: 12 additions & 0 deletions lib/knife-spork/plugins/eventinator.rb
Expand Up @@ -24,6 +24,18 @@ def after_upload
end
end

def after_delete
event_data = {
:tag => 'knife',
:username => current_user,
:status => "#{organization}#{current_user} deleted the following cookbooks: #{misc_output}",
:metadata => {
:deleted_cookbooks => misc_output
}.to_json
}
eventinate(event_data)
end

def after_promote_remote
environments.each do |environment|
cookbooks.each do |cookbook|
Expand Down
6 changes: 6 additions & 0 deletions lib/knife-spork/plugins/grove.rb
Expand Up @@ -13,6 +13,12 @@ def after_upload
EOH
end

def after_delete
grove <<-EOH
#{organization}#{current_user} deleted the following cookbooks: #{misc_output}
EOH
end

def after_promote_remote
grove <<-EOH
#{current_user} promoted #{cookbooks.collect{|c| "#{c.name}@#{c.version}"}.join(', ')} on #{environments.collect{|e| "#{e.name}"}.join(', ')}
Expand Down
4 changes: 4 additions & 0 deletions lib/knife-spork/plugins/hipchat.rb
Expand Up @@ -11,6 +11,10 @@ def after_upload
hipchat "#{organization}#{current_user} uploaded the following cookbooks:\n#{cookbooks.collect{ |c| " #{c.name}@#{c.version}" }.join("\n")}"
end

def after_delete
hipchat "#{organization}#{current_user} deleted the following cookbooks: #{misc_output}"
end

def after_promote_remote
environments.each do |environment|
diff = environment_diffs[environment.name]
Expand Down
9 changes: 9 additions & 0 deletions lib/knife-spork/plugins/irccat.rb
Expand Up @@ -7,6 +7,7 @@ class Irccat < Plugin

TEMPLATES = {
:upload => '#BOLD#PURPLECHEF:#NORMAL %{organization}%{current_user} uploaded #TEAL%{cookbooks}#NORMAL',
:delete => '#BOLD#PURPLECHEF:#NORMAL %{organization}%{current_user} deleted the following cookbooks: #TEAL%{misc_output}#NORMAL',
:promote => '#BOLD#PURPLECHEF:#NORMAL %{organization}%{current_user} promoted #TEAL%{cookbooks}#NORMAL to %{environment} %{gist}',
:environmentfromfile => '#BOLD#PURPLECHEF:#NORMAL %{organization}%{current_user} uploaded environment #TEAL%{object_name}#NORMAL %{gist}',
:environmentedit => '#BOLD#PURPLECHEF:#NORMAL %{organization}%{current_user} edited environment #TEAL%{object_name}#NORMAL %{gist}',
Expand Down Expand Up @@ -40,6 +41,14 @@ def after_upload
})
end

def after_delete
irccat(template(:delete) % {
:organization => organization,
:current_user => current_user,
:misc_output => misc_output
})
end

def after_promote_remote
environments.each do |environment|
diff = environment_diffs[environment.name]
Expand Down
4 changes: 4 additions & 0 deletions lib/knife-spork/plugins/jabber.rb
Expand Up @@ -11,6 +11,10 @@ def after_upload
jabber "#{organization}#{current_user} uploaded the following cookbooks:\n#{cookbooks.collect{ |c| " #{c.name}@#{c.version}" }.join("\n")}"
end

def after_delete
jabber "#{organization}#{current_user} deleted the following cookbooks: #{misc_output}"
end

def after_promote_remote
jabber "#{organization}#{current_user} promoted the following cookbooks:\n#{cookbooks.collect{ |c| " #{c.name}@#{c.version}" }.join("\n")} to #{environments.collect{ |e| "#{e.name}" }.join(", ")}"
end
Expand Down
4 changes: 4 additions & 0 deletions lib/knife-spork/plugins/plugin.rb
Expand Up @@ -91,6 +91,10 @@ def object_difference
@options[:object_difference]
end

def misc_output
@options[:misc_output]
end

def ui
@options[:ui]
end
Expand Down
4 changes: 4 additions & 0 deletions lib/knife-spork/plugins/slack.rb
Expand Up @@ -11,6 +11,10 @@ def after_upload
slack "#{organization}#{current_user} uploaded the following cookbooks:\n#{cookbooks.collect{ |c| " #{c.name}@#{c.version}" }.join("\n")}"
end

def after_delete
slack "#{organization}#{current_user} deleted the following cookbooks: #{misc_output}"
end

def after_promote_remote
slack "#{organization}#{current_user} promoted the following cookbooks:\n#{cookbooks.collect{ |c| " #{c.name}@#{c.version}" }.join("\n")} to #{environments.collect{ |e| "#{e.name}" }.join(", ")}"
end
Expand Down
4 changes: 4 additions & 0 deletions lib/knife-spork/plugins/statusnet.rb
Expand Up @@ -11,6 +11,10 @@ def after_upload
statusnet "#{organization}#{current_user} uploaded the following cookbooks:\n#{cookbooks.collect{ |c| " #{c.name}@#{c.version}" }.join("\n")}"
end

def after_delete
statusnet "#{organization}#{current_user} deleted the following cookbooks: #{misc_output}"
end

def after_promote_remote
statusnet "#{organization}#{current_user} promoted the following cookbooks:\n#{cookbooks.collect{ |c| " #{c.name}@#{c.version}" }.join("\n")} to #{environments.collect{ |e| "#{e.name}" }.join(", ")}"
end
Expand Down
1 change: 1 addition & 0 deletions lib/knife-spork/runner.rb
Expand Up @@ -50,6 +50,7 @@ def run_plugins(hook)
:object_name => @object_name,
:object_secondary_name => @object_secondary_name,
:object_difference => @object_difference,
:misc_output => @misc_output,
:ui => ui
)
end
Expand Down

0 comments on commit 72a3d3a

Please sign in to comment.