-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add the initial destruction of cookbooks out of basic retention #1
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,106 @@ class Knife | |
class RetentionCookbook < Knife | ||
banner "knife retention cookbook [COOKBOOK] (options)" | ||
|
||
deps do | ||
require "chef/search/query" | ||
require "chef/knife/search" | ||
require "chef/cookbook_version" | ||
end | ||
|
||
option :clobber, | ||
long: "--clobber", | ||
description: "Actually delete cookbooks from the Chef Server forever", | ||
boolean: true, | ||
default: false | ||
|
||
def run | ||
ui.info "TODO: Implement this" | ||
cookbook_name = name_args[0] if name_args.length.positive? | ||
clobber = config[:clobber] | ||
|
||
ui.info "Running in Evaluation Mode no cookbooks will be deleted" unless clobber | ||
|
||
if cookbook_name | ||
cleanup_cookbook(cookbook_name, clobber) | ||
else | ||
cleanup_all_cookbooks(clobber) | ||
end | ||
end | ||
|
||
private | ||
|
||
def cleanup_all_cookbooks(clobber) | ||
Chef::CookbookVersion.list.keys.each do |cookbook_name| | ||
ui.info "Evaluating #{cookbook_name}" | ||
cleanup_cookbook(cookbook_name, clobber) | ||
ui.info "" | ||
end | ||
end | ||
|
||
def cleanup_cookbook(cookbook, clobber) | ||
latest_version = Chef::CookbookVersion.load(cookbook) | ||
ui.info "Latest Version: #{latest_version.version}" | ||
|
||
# Lets get all the cookbook versions that we could care less about | ||
find_unused_versions(cookbook).each do |version| | ||
destroy_cookbook_version(cookbook, version[:version], clobber) | ||
end | ||
end | ||
|
||
def destroy_cookbook_version(cookbook, version, clobber) | ||
if clobber | ||
delete_object(Chef::CookbookVersion, "#{cookbook} version #{version}", "cookbook") do | ||
rest.delete("cookbooks/#{cookbook}/#{version}") | ||
end | ||
else | ||
ui.info "Unused Version: #{version}" | ||
end | ||
end | ||
|
||
def max_results | ||
Chef::Node.list.count || 1000 | ||
end | ||
|
||
def search_args | ||
{ | ||
rows: max_results, | ||
filter_result: { | ||
name: ["name"], | ||
cookbooks: ["cookbooks"], | ||
ohai_time: ["ohai_time"] | ||
} | ||
} | ||
end | ||
|
||
def find_unused_versions(cookbook) | ||
# get all the version information for the cookbook | ||
version_info_for_cookbook(cookbook).take_while do |version| | ||
# These are package that are not used and are considered old as they | ||
# are after the first used version still | ||
version unless version[:used] | ||
end | ||
end | ||
|
||
def version_info_for_cookbook(cookbook_name) | ||
nodes = all_nodes_for_cookbook(cookbook_name) | ||
versions = Chef::CookbookVersion.available_versions(cookbook_name).map! do |version| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to assign to unused variable There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is used |
||
used_by_node = nodes.any? { |n| n["cookbooks"][cookbook_name]["version"] == version } | ||
|
||
{ | ||
version: Chef::Version.new(version), | ||
used: used_by_node | ||
} | ||
end | ||
|
||
# Sort and take out the latests version so we do not delete it | ||
versions.sort_by { |v| v[:version] }[0..-2] | ||
end | ||
|
||
def search_nodes(query) | ||
Chef::Search::Query.new.search(:node, query, search_args).first | ||
end | ||
|
||
def all_nodes_for_cookbook(cookbook_name) | ||
search_nodes("cookbooks_#{cookbook_name}:*") | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
# frozen_string_literal: true | ||
module Knife | ||
module ChefRetention | ||
VERSION = "0.0.1" | ||
VERSION = "0.0.2" | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why
map!
, compared tomap
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is what happens when you refactor and miss something