health_inspector 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/HISTORY.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 0.3.0 ( 2012-09-26 )
2
+
3
+ * Add new check for cookbooks: checksum comparison for each file
4
+
1
5
  ## 0.2.1 ( 2012-09-26 )
2
6
 
3
7
  * Fix 1.8.7 incompatibility introduced in last release (String#prepend).
@@ -11,6 +11,8 @@ require "health_inspector/checklists/data_bag_items"
11
11
  require "health_inspector/checklists/environments"
12
12
  require "health_inspector/checklists/roles"
13
13
  require "health_inspector/cli"
14
+ require 'chef/rest'
15
+ require 'chef/checksum_cache'
14
16
  require "json"
15
17
 
16
18
  module HealthInspector
@@ -30,7 +30,7 @@ module HealthInspector
30
30
  def run
31
31
  banner "Inspecting #{self.class.title}"
32
32
 
33
- items.each do |item|
33
+ each_item do |item|
34
34
  failures = run_checks(item)
35
35
 
36
36
  if failures.empty?
@@ -77,6 +77,11 @@ module HealthInspector
77
77
  end
78
78
  end
79
79
 
80
+ def chef_rest
81
+ @rest ||= Chef::REST.new(@context.configure[:chef_server_url], @context.configure[:node_name],
82
+ @context.configure[:client_key])
83
+ end
84
+
80
85
  def run_check(check, item)
81
86
  check_context = CheckContext.new(check, item, @context)
82
87
  check_context.call
@@ -13,7 +13,7 @@ module HealthInspector
13
13
 
14
14
  add_check "versions" do
15
15
  if item.local_version && item.server_version &&
16
- item.local_version != item.server_version
16
+ item.local_version != item.server_version
17
17
  failure "chef server has #{item.server_version} but local version is #{item.local_version}"
18
18
  end
19
19
  end
@@ -38,7 +38,15 @@ module HealthInspector
38
38
  end
39
39
  end
40
40
 
41
- class Cookbook < Struct.new(:name, :path, :server_version, :local_version)
41
+ add_check "changes on the server not in the repo" do
42
+ if item.server_version == item.local_version && !item.bad_files.empty?
43
+ fail_message = "has a checksum mismatch between server and repo in\n"
44
+ fail_message << item.bad_files.map { |f| " #{f}" }.join("\n")
45
+ failure fail_message
46
+ end
47
+ end
48
+
49
+ class Cookbook < Struct.new(:name, :path, :server_version, :local_version, :bad_files)
42
50
  def git_repo?
43
51
  self.path && File.exist?("#{self.path}/.git")
44
52
  end
@@ -46,18 +54,21 @@ module HealthInspector
46
54
 
47
55
  title "cookbooks"
48
56
 
49
- def items
57
+ def each_item
50
58
  server_cookbooks = cookbooks_on_server
51
59
  local_cookbooks = cookbooks_in_repo
52
60
  all_cookbook_names = ( server_cookbooks.keys + local_cookbooks.keys ).uniq.sort
53
61
 
54
- all_cookbook_names.map do |name|
55
- Cookbook.new.tap do |cookbook|
62
+ all_cookbook_names.each do |name|
63
+ item = Cookbook.new.tap do |cookbook|
56
64
  cookbook.name = name
57
65
  cookbook.path = cookbook_path(name)
58
66
  cookbook.server_version = server_cookbooks[name]
59
67
  cookbook.local_version = local_cookbooks[name]
68
+ cookbook.bad_files = checksum_compare(name, cookbook.server_version)
60
69
  end
70
+
71
+ yield item
61
72
  end
62
73
  end
63
74
 
@@ -76,18 +87,43 @@ module HealthInspector
76
87
  select { |path| File.exists?("#{path}/metadata.rb") }.
77
88
  inject({}) do |hsh, path|
78
89
 
79
- name = File.basename(path)
80
- version = (`grep '^version' #{path}/metadata.rb`).split.last[1...-1]
90
+ name = File.basename(path)
91
+ version = (`grep '^version' #{path}/metadata.rb`).split.last[1...-1]
81
92
 
82
- hsh[name] = version
83
- hsh
84
- end
93
+ hsh[name] = version
94
+ hsh
95
+ end
85
96
  end
86
97
 
87
98
  def cookbook_path(name)
88
99
  path = @context.cookbook_path.find { |f| File.exist?("#{f}/#{name}") }
89
100
  path ? File.join(path, name) : nil
90
101
  end
102
+
103
+ def checksum_compare(name,version)
104
+ begin
105
+ cookbook = chef_rest.get_rest("/cookbooks/#{name}/#{version}")
106
+ rescue Net::HTTPServerException => e
107
+ return ["Could not find cookbook #{name} on the server"]
108
+ end
109
+
110
+ cookbook.manifest.inject([]) do |memo, (key,value)|
111
+ if value.kind_of? Array
112
+ value.each do |file|
113
+ path = cookbook_path("#{name}/#{file["path"]}")
114
+
115
+ if path
116
+ checksum = Chef::ChecksumCache.generate_md5_checksum_for_file(path)
117
+ memo << "#{file['path']}" if checksum != file['checksum']
118
+ else
119
+ memo << "#{file['path']} does not exist in the repo"
120
+ end
121
+ end
122
+ end
123
+ memo
124
+ end
125
+ end
126
+
91
127
  end
92
128
  end
93
129
  end
@@ -22,17 +22,19 @@ module HealthInspector
22
22
 
23
23
  DataBagItem = Struct.new(:name, :server, :local)
24
24
 
25
- def items
25
+ def each_item
26
26
  server_data_bag_items = data_bag_items_on_server
27
27
  local_data_bag_items = data_bag_items_in_repo
28
28
  all_data_bag_item_names = ( server_data_bag_items + local_data_bag_items ).uniq.sort
29
29
 
30
- all_data_bag_item_names.map do |name|
31
- DataBagItem.new.tap do |data_bag_item|
30
+ all_data_bag_item_names.each do |name|
31
+ item = DataBagItem.new.tap do |data_bag_item|
32
32
  data_bag_item.name = name
33
33
  data_bag_item.server = load_item_from_server(name)
34
34
  data_bag_item.local = load_item_from_local(name)
35
35
  end
36
+
37
+ yield item
36
38
  end
37
39
  end
38
40
 
@@ -15,17 +15,19 @@ module HealthInspector
15
15
 
16
16
  DataBag = Struct.new(:name, :exists_on_server, :exists_locally)
17
17
 
18
- def items
18
+ def each_item
19
19
  server_data_bags = data_bags_on_server
20
20
  local_data_bags = data_bags_in_repo
21
21
  all_data_bag_names = ( server_data_bags + local_data_bags ).uniq.sort
22
22
 
23
- all_data_bag_names.map do |name|
24
- DataBag.new.tap do |data_bag|
23
+ all_data_bag_names.each do |name|
24
+ item = DataBag.new.tap do |data_bag|
25
25
  data_bag.name = name
26
26
  data_bag.exists_on_server = data_bags_on_server.include?(name)
27
27
  data_bag.exists_locally = data_bags_in_repo.include?(name)
28
28
  end
29
+
30
+ yield item
29
31
  end
30
32
  end
31
33
 
@@ -22,17 +22,19 @@ module HealthInspector
22
22
 
23
23
  Environment = Struct.new(:name, :server, :local)
24
24
 
25
- def items
25
+ def each_item
26
26
  server_items = items_on_server
27
27
  local_items = items_in_repo
28
28
  all_item_names = ( server_items + local_items ).uniq.sort
29
29
 
30
- all_item_names.map do |name|
31
- Environment.new.tap do |item|
30
+ all_item_names.each do |name|
31
+ item = Environment.new.tap do |item|
32
32
  item.name = name
33
33
  item.server = load_item_from_server(name)
34
34
  item.local = load_item_from_local(name)
35
35
  end
36
+
37
+ yield item
36
38
  end
37
39
  end
38
40
 
@@ -24,17 +24,19 @@ module HealthInspector
24
24
 
25
25
  Role = Struct.new(:name, :server, :local)
26
26
 
27
- def items
27
+ def each_item
28
28
  server_items = items_on_server
29
29
  local_items = items_in_repo
30
30
  all_item_names = ( server_items + local_items ).uniq.sort
31
31
 
32
- all_item_names.map do |name|
33
- Role.new.tap do |item|
32
+ all_item_names.each do |name|
33
+ item = Role.new.tap do |item|
34
34
  item.name = name
35
35
  item.server = load_item_from_server(name)
36
36
  item.local = load_item_from_local(name)
37
37
  end
38
+
39
+ yield item
38
40
  end
39
41
  end
40
42
 
@@ -1,3 +1,3 @@
1
1
  module HealthInspector
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: health_inspector
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-09-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: minitest
16
- requirement: &70151008460860 !ruby/object:Gem::Requirement
16
+ requirement: &70140548345560 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70151008460860
24
+ version_requirements: *70140548345560
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: mocha
27
- requirement: &70151008459940 !ruby/object:Gem::Requirement
27
+ requirement: &70140548344640 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70151008459940
35
+ version_requirements: *70140548344640
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: thor
38
- requirement: &70151008459400 !ruby/object:Gem::Requirement
38
+ requirement: &70140548344100 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70151008459400
46
+ version_requirements: *70140548344100
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: chef
49
- requirement: &70151008458860 !ruby/object:Gem::Requirement
49
+ requirement: &70140548343560 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '10.14'
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *70151008458860
57
+ version_requirements: *70140548343560
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: yajl-ruby
60
- requirement: &70151008458440 !ruby/object:Gem::Requirement
60
+ requirement: &70140548343140 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,7 +65,7 @@ dependencies:
65
65
  version: '0'
66
66
  type: :runtime
67
67
  prerelease: false
68
- version_requirements: *70151008458440
68
+ version_requirements: *70140548343140
69
69
  description: A tool to inspect your chef repo as is compares to what is on your chef
70
70
  server
71
71
  email: