knife-cleanup 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in knife-cleanup.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "knife-cleanup/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "knife-cleanup"
7
+ s.version = Knife::Cleanup::VERSION
8
+ s.authors = ["Marius Ducea"]
9
+ s.email = ["marius.ducea@gmail.com"]
10
+ s.homepage = "https://github.com/mdxp/knife-cleanup"
11
+ s.summary = %q{Chef knife plugin to help cleanup unused versions of cookbooks from a chef server}
12
+ s.description = s.summary
13
+
14
+ s.rubyforge_project = "knife-cleanup"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_dependency "chef", ">= 0.10.10"
22
+
23
+ s.add_development_dependency "rspec", "~> 2.10"
24
+ end
@@ -0,0 +1,97 @@
1
+ #
2
+ # Author:: Marius Ducea (<marius.ducea@gmail.com>)
3
+ # Copyright:: Copyright (c) 2013 Marius Ducea
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+ module ServerCleanup
19
+ class CleanupVersions < Chef::Knife
20
+
21
+ deps do
22
+ require 'chef/api_client'
23
+ end
24
+
25
+ banner "knife cleanup versions (options)"
26
+
27
+ option :delete,
28
+ :short => "-D",
29
+ :long => "--delete",
30
+ :description => "Delete the unused versions of the cookbooks",
31
+ :boolean => true
32
+
33
+ def run
34
+ cookbooks
35
+ end
36
+
37
+ def cookbooks
38
+ ui.msg "Searching for unused cookboks versions..."
39
+ all_cookbooks = rest.get_rest("/cookbooks?num_versions=all")
40
+ latest_cookbooks = rest.get_rest("/cookbooks?latest")
41
+
42
+ # All cookbooks
43
+ cbv = all_cookbooks.inject({}) do |collected, ( cookbook, versions )|
44
+ collected[cookbook] = versions["versions"].map {|v| v['version']}
45
+ collected
46
+ end
47
+
48
+ # Get the latest cookbooks
49
+ latest = latest_cookbooks.inject({}) do |collected, ( cookbook, versions )|
50
+ collected[cookbook] = versions["versions"].map {|v| v['version']}
51
+ collected
52
+ end
53
+
54
+ latest.each_key do |cb|
55
+ cbv[cb].delete(latest[cb][0])
56
+ end
57
+
58
+ # Let see what cookbooks we have in use in all environments
59
+ Chef::Environment.list.each_key do |env_list|
60
+ env = Chef::Environment.load(env_list)
61
+ next unless !env.cookbook_versions.empty?
62
+ env.cookbook_versions.each_key do |cb|
63
+ cb_ver = env.cookbook_versions[cb].split(" ").last
64
+ begin
65
+ cbv[cb].delete(cb_ver)
66
+ rescue
67
+ "Skipping..."
68
+ end
69
+ end
70
+ end
71
+
72
+ confirm("Do you really want to delete unused cookbook versions from the server") if config[:delete]
73
+ ui.msg "Cookbook Versions:"
74
+ key_length = cbv.empty? ? 0 : cbv.keys.map {|name| name.size }.max + 2
75
+ cbv.each_key do |cb|
76
+ print " #{cb.ljust(key_length)}"
77
+ cbv[cb].each do |cb_ver|
78
+ print "#{cb_ver} "
79
+ if config[:delete]
80
+ delete_cookbook(cb, cb_ver)
81
+ end
82
+ end
83
+ print "\n"
84
+ end
85
+
86
+ if !config[:delete]
87
+ ui.msg "Not deleting unused cookbook versions; use --delete if you want to remove them"
88
+ end
89
+
90
+ end
91
+
92
+ def delete_cookbook(cb, cb_ver)
93
+ rest.delete_rest("cookbooks/#{cb}/#{cb_ver}")
94
+ print ". "
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,5 @@
1
+ module Knife
2
+ module Cleanup
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1 @@
1
+ require "knife-cleanup/version"
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: knife-cleanup
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Marius Ducea
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-24 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: chef
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.10.10
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 0.10.10
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '2.10'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '2.10'
46
+ description: Chef knife plugin to help cleanup unused versions of cookbooks from a
47
+ chef server
48
+ email:
49
+ - marius.ducea@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - Gemfile
56
+ - Rakefile
57
+ - knife-cleanup.gemspec
58
+ - lib/chef/knife/cleanup_versions.rb
59
+ - lib/knife-cleanup.rb
60
+ - lib/knife-cleanup/version.rb
61
+ homepage: https://github.com/mdxp/knife-cleanup
62
+ licenses: []
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project: knife-cleanup
81
+ rubygems_version: 1.8.24
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: Chef knife plugin to help cleanup unused versions of cookbooks from a chef
85
+ server
86
+ test_files: []
87
+ has_rdoc: