knife-stats 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-stats.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "knife-stats/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "knife-stats"
7
+ s.version = Knife::Stats::VERSION
8
+ s.authors = ["Stephen Nelson-Smith", "Fletcher Nichol"]
9
+ s.email = ["support@atalanta-systems.com"]
10
+ s.homepage = "https://github.com/Atalanta/knife-stats"
11
+ s.summary = %q{Provides usage stats for Opscode Chef roles, cookbooks and environments}
12
+ s.description = %q{Provides usage stats for Opscode Chef roles, cookbooks and environments}
13
+
14
+ s.rubyforge_project = "knife-stats"
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_runtime_dependency "chef", ">= 0.10.0"
22
+ end
@@ -0,0 +1,80 @@
1
+ require 'yaml'
2
+
3
+ class Chef
4
+ class Knife
5
+ class CookbookStats < Chef::Knife
6
+
7
+ banner "knife cookbook stats -E ENVIRONMENT [-r ROLE]"
8
+
9
+ option :environment,
10
+ :short => "-E ENVIRONMENT",
11
+ :long => "--environment ENVIRONMENT",
12
+ :require => true
13
+
14
+ option :role,
15
+ :short => "-r ROLE",
16
+ :long => "--role ROLE"
17
+
18
+ deps do
19
+ require 'chef/shef/ext'
20
+ end
21
+
22
+ def run
23
+ Shef::Extensions.extend_context_object(self)
24
+ validate_options
25
+
26
+ cookbook_hash = build_cookbook_hash
27
+ cookbook_hash = filter_cookbook_hash(cookbook_hash) if config[:role]
28
+
29
+ puts cookbook_hash.to_yaml
30
+ end
31
+
32
+ private
33
+
34
+ def validate_options
35
+ unless api.get("/environments").keys.include?(config[:environment])
36
+ ui.fatal "Environment #{config[:environment]} not found."
37
+ exit 1
38
+ end
39
+
40
+ if config[:role] && !api.get("/roles").keys.include?(config[:role])
41
+ ui.fatal "Role #{config[:role]} not found."
42
+ exit 1
43
+ end
44
+ end
45
+
46
+ def filter_cookbook_hash(cookbook_hash)
47
+ cookbooks_from_role = calculate_cookbooks_from_role
48
+ selected_cookbooks = cookbooks_from_role & cookbook_hash.keys
49
+
50
+ filtered_cookbook_hash = Hash.new
51
+ selected_cookbooks.each do |cb|
52
+ filtered_cookbook_hash[cb] = cookbook_hash[cb]
53
+ end
54
+ filtered_cookbook_hash
55
+ end
56
+
57
+ def build_cookbook_hash
58
+ node_names = search(:node, "chef_environment:#{config[:environment]}").map { |n| n.name }
59
+
60
+ cookbook = Hash.new(0)
61
+ node_names.each do |node|
62
+ node_cookbooks = api.get("/nodes/#{node}/cookbooks").keys
63
+ node_cookbooks.each { |cb| cookbook[cb] += 1 }
64
+ end
65
+ cookbook
66
+ end
67
+
68
+ def calculate_cookbooks_from_role
69
+ rl = Chef::RunList.new("role[#{config[:role]}]")
70
+
71
+ recipes = rl.expand(Chef::Environment.load(config[:environment])).recipes
72
+
73
+ cookbooks = recipes.map do |recipe|
74
+ Chef::Recipe.parse_recipe_name(recipe).first
75
+ end
76
+ cookbooks.uniq!.map { |c| c.to_s }
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,27 @@
1
+ require 'yaml'
2
+
3
+ class Chef
4
+ class Knife
5
+ class EnvironmentStats < Chef::Knife
6
+
7
+ banner "knife environment stats"
8
+
9
+ deps do
10
+ require 'chef/shef/ext'
11
+ end
12
+
13
+ def run
14
+ Shef::Extensions.extend_context_object(self)
15
+
16
+ environment_names = api.get("/environments").keys
17
+
18
+ environment = Hash.new
19
+ environment_names.sort.each do |env|
20
+ environment[env] = search(:node, "chef_environment:#{env}").size
21
+ end
22
+
23
+ puts environment.to_yaml
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,50 @@
1
+ require 'yaml'
2
+
3
+ class Chef
4
+ class Knife
5
+ class RoleStats < Chef::Knife
6
+
7
+ banner "knife role stats [-E ENVIRONMENT]"
8
+
9
+ option :environment,
10
+ :short => "-E ENVIRONMENT",
11
+ :long => "--environment ENVIRONMENT"
12
+
13
+ deps do
14
+ require 'chef/shef/ext'
15
+ end
16
+
17
+ def run
18
+ Shef::Extensions.extend_context_object(self)
19
+ validate_options
20
+
21
+ role_names = api.get("/roles").keys
22
+
23
+ role = Hash.new
24
+ role_names.sort.each do |r|
25
+ query = "roles:#{r}#{environment_query}"
26
+ role[r] = search(:node, query).size
27
+ end
28
+
29
+ puts role.reject! { |k,v| v == 0 }.to_yaml
30
+ end
31
+
32
+ private
33
+
34
+ def validate_options
35
+ if config[:environment] && !api.get("/environments").keys.include?(config[:environment])
36
+ ui.fatal "Environment #{config[:environment]} not found."
37
+ exit 1
38
+ end
39
+ end
40
+
41
+ def environment_query
42
+ if config[:environment]
43
+ " AND chef_environment:#{config[:environment]}"
44
+ else
45
+ ""
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,5 @@
1
+ require "knife-stats/version"
2
+
3
+ require "chef/knife/environemt_stats"
4
+ require "chef/knife/role_stats"
5
+ require "chef/knife/coobook_stats"
@@ -0,0 +1,5 @@
1
+ module Knife
2
+ module Stats
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: knife-stats
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Stephen Nelson-Smith
9
+ - Fletcher Nichol
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2011-11-12 00:00:00.000000000Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: chef
17
+ requirement: &70268480 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: 0.10.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *70268480
26
+ description: Provides usage stats for Opscode Chef roles, cookbooks and environments
27
+ email:
28
+ - support@atalanta-systems.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - .gitignore
34
+ - Gemfile
35
+ - Rakefile
36
+ - knife-stats.gemspec
37
+ - lib/chef/knife/cookbook_stats.rb
38
+ - lib/chef/knife/environment_stats.rb
39
+ - lib/chef/knife/role_stats.rb
40
+ - lib/knife-stats.rb
41
+ - lib/knife-stats/version.rb
42
+ homepage: https://github.com/Atalanta/knife-stats
43
+ licenses: []
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubyforge_project: knife-stats
62
+ rubygems_version: 1.8.10
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: Provides usage stats for Opscode Chef roles, cookbooks and environments
66
+ test_files: []