knife-chef-retention 0.0.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +2 -0
- data/CHANGELOG.md +16 -0
- data/Gemfile +7 -0
- data/README.md +24 -4
- data/knife-chef-retention.gemspec +0 -5
- data/lib/chef/knife/retention_cookbook.rb +115 -3
- data/lib/knife-chef-retention/version.rb +1 -1
- metadata +3 -58
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2ff54c639881e87fffe4a593253d850625f5e642
|
4
|
+
data.tar.gz: aaee45b42f3fa620baaa97e2577e612a9fc8f283
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a57915b45f7229c37813a8d63425f844354f2421da19db823889885e171369f66d72405501c732b89363d8e38b41fff90c87002a6425f6cb6cab0c37f294ec70
|
7
|
+
data.tar.gz: 07ffeaddb0a8396269e83ecc9120ea4f1b35b930bfad2c491e67dacc3e60cd12e114203fdcaed0c01fa7909e6b8a80a4c6a24b07d693ce594cc66be3da61c37c
|
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# Change Log
|
2
|
+
|
3
|
+
## [v0.2.0](https://github.com/cvent/knife-chef-retention/tree/v0.2.0) (2016-10-03)
|
4
|
+
[Full Changelog](https://github.com/cvent/knife-chef-retention/compare/v0.0.1...v0.2.0)
|
5
|
+
|
6
|
+
**Merged pull requests:**
|
7
|
+
|
8
|
+
- Fix regression when very few cookbook versions exist [\#5](https://github.com/cvent/knife-chef-retention/pull/5) ([brentm5](https://github.com/brentm5))
|
9
|
+
- Adds a way to keep old unused versions [\#4](https://github.com/cvent/knife-chef-retention/pull/4) ([brentm5](https://github.com/brentm5))
|
10
|
+
- Move the retention command to be under the resource it modifies [\#3](https://github.com/cvent/knife-chef-retention/pull/3) ([brentm5](https://github.com/brentm5))
|
11
|
+
- Add the initial destruction of cookbooks out of basic retention [\#1](https://github.com/cvent/knife-chef-retention/pull/1) ([brentm5](https://github.com/brentm5))
|
12
|
+
|
13
|
+
## [v0.0.1](https://github.com/cvent/knife-chef-retention/tree/v0.0.1) (2016-09-28)
|
14
|
+
|
15
|
+
|
16
|
+
\* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)*
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -23,16 +23,36 @@ gem install knife-chef-retention
|
|
23
23
|
## Usage
|
24
24
|
|
25
25
|
Everything suppose to be organized in an easy to navigate way. Everything is
|
26
|
-
nested under
|
26
|
+
nested under its corresponding resource. For example cookbook retentions are
|
27
|
+
under `knife cookbook` and the commands should be self documenting.
|
28
|
+
|
27
29
|
Below are some simple examples
|
28
30
|
|
29
31
|
```bash
|
30
|
-
knife
|
32
|
+
knife cookbook
|
33
|
+
|
34
|
+
** COOKBOOK COMMANDS **
|
35
|
+
...
|
36
|
+
knife cookbook retention [COOKBOOK] (options)
|
37
|
+
...
|
38
|
+
|
31
39
|
|
32
|
-
|
33
|
-
|
40
|
+
knife cookbook retention windows
|
41
|
+
Running in Evaluation Mode no cookbooks will be deleted
|
42
|
+
Latest Version: 2.0.2
|
43
|
+
Unused Version: 1.34.8
|
44
|
+
Unused Version: 1.36.0
|
45
|
+
...
|
46
|
+
Unused Version: 1.38.3
|
34
47
|
```
|
35
48
|
|
49
|
+
### Options
|
50
|
+
|
51
|
+
- `--clobber` - This will actually delete cookbooks while prompting
|
52
|
+
- `-y` - This will not ask when deleting cookbooks
|
53
|
+
- `--extra-versions` - Specifies the total number of extra unused versions to keep around (Default 1)
|
54
|
+
|
55
|
+
|
36
56
|
License and Author
|
37
57
|
==================
|
38
58
|
|
@@ -20,9 +20,4 @@ Gem::Specification.new do |s|
|
|
20
20
|
s.required_ruby_version = "~> 2.3"
|
21
21
|
|
22
22
|
s.add_dependency "chef", "~> 12.11"
|
23
|
-
|
24
|
-
s.add_development_dependency "bundler", "~> 1.6"
|
25
|
-
s.add_development_dependency "rake", "~> 11.1"
|
26
|
-
s.add_development_dependency "rubocop", "~> 0.38"
|
27
|
-
s.add_development_dependency "github_changelog_generator", "~> 1.13"
|
28
23
|
end
|
@@ -3,11 +3,123 @@ require "chef/knife"
|
|
3
3
|
|
4
4
|
class Chef
|
5
5
|
class Knife
|
6
|
-
class
|
7
|
-
banner "knife retention
|
6
|
+
class CookbookRetention < Knife
|
7
|
+
banner "knife cookbook retention [COOKBOOK] (options)"
|
8
|
+
|
9
|
+
deps do
|
10
|
+
require "chef/search/query"
|
11
|
+
require "chef/knife/search"
|
12
|
+
require "chef/cookbook_version"
|
13
|
+
end
|
14
|
+
|
15
|
+
option :clobber,
|
16
|
+
long: "--clobber",
|
17
|
+
description: "Actually delete cookbooks from the Chef Server forever",
|
18
|
+
boolean: true,
|
19
|
+
default: false
|
20
|
+
|
21
|
+
option :extra_versions,
|
22
|
+
short: "-e VALUE",
|
23
|
+
long: "--extra-versions VALUE",
|
24
|
+
description: "The number of extra versions to keep (Default: 1)",
|
25
|
+
default: 1
|
8
26
|
|
9
27
|
def run
|
10
|
-
|
28
|
+
cookbook_name = name_args[0] if name_args.length.positive?
|
29
|
+
clobber = config[:clobber]
|
30
|
+
extra_versions = config[:extra_versions].to_i
|
31
|
+
|
32
|
+
ui.info "Running in Evaluation Mode no cookbooks will be deleted" unless clobber
|
33
|
+
ui.info "Keeping the top #{extra_versions} unused versions" if extra_versions.positive?
|
34
|
+
|
35
|
+
if cookbook_name
|
36
|
+
cleanup_cookbook(cookbook_name, extra_versions, clobber)
|
37
|
+
else
|
38
|
+
cleanup_all_cookbooks(extra_versions, clobber)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def cleanup_all_cookbooks(extra_versions, clobber)
|
45
|
+
Chef::CookbookVersion.list.keys.each do |cookbook_name|
|
46
|
+
ui.info "Evaluating #{cookbook_name}"
|
47
|
+
cleanup_cookbook(cookbook_name, extra_versions, clobber)
|
48
|
+
ui.info ""
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def cleanup_cookbook(cookbook, extra_versions, clobber)
|
53
|
+
latest_version = Chef::CookbookVersion.load(cookbook)
|
54
|
+
ui.info "Latest Version: #{latest_version.version}"
|
55
|
+
|
56
|
+
# Lets get all the cookbook versions that we could care less about
|
57
|
+
out_of_retention_cookbooks(cookbook, extra_versions).each do |version|
|
58
|
+
destroy_cookbook_version(cookbook, version[:version], clobber)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def destroy_cookbook_version(cookbook, version, clobber)
|
63
|
+
if clobber
|
64
|
+
delete_object(Chef::CookbookVersion, "#{cookbook} version #{version}", "cookbook") do
|
65
|
+
rest.delete("cookbooks/#{cookbook}/#{version}")
|
66
|
+
end
|
67
|
+
else
|
68
|
+
ui.info "Unused Version: #{version}"
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def max_results
|
73
|
+
Chef::Node.list.count || 1000
|
74
|
+
end
|
75
|
+
|
76
|
+
def search_args
|
77
|
+
{
|
78
|
+
rows: max_results,
|
79
|
+
filter_result: {
|
80
|
+
name: ["name"],
|
81
|
+
cookbooks: ["cookbooks"],
|
82
|
+
ohai_time: ["ohai_time"]
|
83
|
+
}
|
84
|
+
}
|
85
|
+
end
|
86
|
+
|
87
|
+
def out_of_retention_cookbooks(cookbook, extra_versions)
|
88
|
+
# get all the version information for the cookbook
|
89
|
+
unused_versions = cookbook_versions(cookbook).take_while do |version|
|
90
|
+
# These are package that are not used and are considered old as they
|
91
|
+
# are after the first used version still
|
92
|
+
version unless version[:used]
|
93
|
+
end
|
94
|
+
|
95
|
+
save_some_versions(unused_versions, extra_versions)
|
96
|
+
end
|
97
|
+
|
98
|
+
def save_some_versions(versions, extra_versions)
|
99
|
+
# Just removes the top X which since we sort is the top X newest versions
|
100
|
+
Array(versions.slice(0, versions.length - extra_versions))
|
101
|
+
end
|
102
|
+
|
103
|
+
def cookbook_versions(cookbook_name)
|
104
|
+
nodes = all_nodes_for_cookbook(cookbook_name)
|
105
|
+
versions = Chef::CookbookVersion.available_versions(cookbook_name).map do |version|
|
106
|
+
used_by_node = nodes.any? { |n| n["cookbooks"][cookbook_name]["version"] == version }
|
107
|
+
|
108
|
+
{
|
109
|
+
version: Chef::Version.new(version),
|
110
|
+
used: used_by_node
|
111
|
+
}
|
112
|
+
end
|
113
|
+
|
114
|
+
versions.sort_by { |v| v[:version] }
|
115
|
+
end
|
116
|
+
|
117
|
+
def search_nodes(query)
|
118
|
+
Chef::Search::Query.new.search(:node, query, search_args).first
|
119
|
+
end
|
120
|
+
|
121
|
+
def all_nodes_for_cookbook(cookbook_name)
|
122
|
+
search_nodes("cookbooks_#{cookbook_name}:*")
|
11
123
|
end
|
12
124
|
end
|
13
125
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: knife-chef-retention
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brent Montague
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-10-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: chef
|
@@ -24,62 +24,6 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '12.11'
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: bundler
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - "~>"
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '1.6'
|
34
|
-
type: :development
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - "~>"
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: '1.6'
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: rake
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - "~>"
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: '11.1'
|
48
|
-
type: :development
|
49
|
-
prerelease: false
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
51
|
-
requirements:
|
52
|
-
- - "~>"
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: '11.1'
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: rubocop
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
58
|
-
requirements:
|
59
|
-
- - "~>"
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: '0.38'
|
62
|
-
type: :development
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - "~>"
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
version: '0.38'
|
69
|
-
- !ruby/object:Gem::Dependency
|
70
|
-
name: github_changelog_generator
|
71
|
-
requirement: !ruby/object:Gem::Requirement
|
72
|
-
requirements:
|
73
|
-
- - "~>"
|
74
|
-
- !ruby/object:Gem::Version
|
75
|
-
version: '1.13'
|
76
|
-
type: :development
|
77
|
-
prerelease: false
|
78
|
-
version_requirements: !ruby/object:Gem::Requirement
|
79
|
-
requirements:
|
80
|
-
- - "~>"
|
81
|
-
- !ruby/object:Gem::Version
|
82
|
-
version: '1.13'
|
83
27
|
description: A knife plugin to handle deleting of items in a safe manor
|
84
28
|
email: bmontague@cvent.com
|
85
29
|
executables: []
|
@@ -91,6 +35,7 @@ files:
|
|
91
35
|
- ".rubocop.yml"
|
92
36
|
- ".ruby-version"
|
93
37
|
- ".travis.yml"
|
38
|
+
- CHANGELOG.md
|
94
39
|
- Gemfile
|
95
40
|
- LICENSE
|
96
41
|
- README.md
|