Class: HealthInspector::Checklists::Base

Inherits:
Object
  • Object
show all
Includes:
HealthInspector::Color
Defined in:
lib/health_inspector/checklists/base.rb

Direct Known Subclasses

Cookbooks, DataBagItems, DataBags, Environments, Roles

Defined Under Namespace

Classes: CheckContext

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HealthInspector::Color

#color

Constructor Details

#initialize(context) ⇒ Base

Returns a new instance of Base.



26
27
28
# File 'lib/health_inspector/checklists/base.rb', line 26

def initialize(context)
  @context = context
end

Class Attribute Details

.checksObject (readonly)

Returns the value of attribute checks.



10
11
12
# File 'lib/health_inspector/checklists/base.rb', line 10

def checks
  @checks
end

.title(val = nil) ⇒ Object (readonly)

Returns the value of attribute title.



10
11
12
# File 'lib/health_inspector/checklists/base.rb', line 10

def title
  @title
end

Class Method Details

.add_check(name, &block) ⇒ Object



12
13
14
15
# File 'lib/health_inspector/checklists/base.rb', line 12

def add_check(name, &block)
  @checks ||= []
  @checks << block
end

.run(context) ⇒ Object



22
23
24
# File 'lib/health_inspector/checklists/base.rb', line 22

def self.run(context)
  new(context).run
end

Instance Method Details



99
100
101
102
103
# File 'lib/health_inspector/checklists/base.rb', line 99

def banner(message)
  puts
  puts message
  puts "-" * 80
end

#checksObject



56
57
58
# File 'lib/health_inspector/checklists/base.rb', line 56

def checks
  self.class.checks
end

#chef_restObject



89
90
91
# File 'lib/health_inspector/checklists/base.rb', line 89

def chef_rest
  @context.chef_rest
end

#each_itemObject

Subclasses should collect all items from the server and the local repo, and for each item pair, yield an object that contains a reference to the server item, and the local repo item. A reference can be nil if it does not exist in one of the locations.

Raises:

  • (NotImplementedError)


34
35
36
# File 'lib/health_inspector/checklists/base.rb', line 34

def each_item
  raise NotImplementedError, "You must implement this method in a subclass"
end

#indent(string, depth) ⇒ Object



169
170
171
# File 'lib/health_inspector/checklists/base.rb', line 169

def indent(string, depth)
  (' ' * 2 * depth) + string
end

#load_ruby_or_json_from_local(chef_class, folder, name) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/health_inspector/checklists/base.rb', line 149

def load_ruby_or_json_from_local(chef_class, folder, name)
  path_template = "#{@context.repo_path}/#{folder}/#{name}.%s"
  ruby_pathname = Pathname.new(path_template % "rb")
  json_pathname = Pathname.new(path_template % "json")
  js_pathname   = Pathname.new(path_template % "js")

  if ruby_pathname.exist?
    instance = chef_class.new
    instance.from_file(ruby_pathname.to_s)
  elsif json_pathname.exist?
    instance = chef_class.json_create( Yajl::Parser.parse( json_pathname.read ) )
  elsif js_pathname.exist?
    instance = chef_class.json_create( Yajl::Parser.parse( js_pathname.read ) )
  end

  instance ? instance.to_hash : nil
rescue IOError
  nil
end


109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/health_inspector/checklists/base.rb', line 109

def print_failures(subject, failures)
  puts color('bright fail', "- #{subject}")

  failures.each do |message|
    if message.kind_of? Hash
      puts color('bright yellow',"  has the following values mismatched on the server and repo\n")
      print_failures_from_hash(message)
    else
      puts color('bright yellow', "  #{message}")
    end
  end
end


122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/health_inspector/checklists/base.rb', line 122

def print_failures_from_hash(message, depth=2)
  message.keys.each do |key|
    print_key(key,depth)

    if message[key].include? "server"
      print_value_diff(message[key],depth)
      message[key].delete_if { |k,v| k == "server" || "local" }
      print_failures_from_hash(message[key], depth + 1) unless message[key].empty?
    else
      print_failures_from_hash(message[key], depth + 1)
    end
  end
end


136
137
138
# File 'lib/health_inspector/checklists/base.rb', line 136

def print_key(key, depth)
  puts indent( color('bright yellow',"#{key} : "), depth )
end


105
106
107
# File 'lib/health_inspector/checklists/base.rb', line 105

def print_success(subject)
  puts color('bright pass', "") + " #{subject}"
end


140
141
142
143
144
145
146
147
# File 'lib/health_inspector/checklists/base.rb', line 140

def print_value_diff(value, depth)
  print indent( color('bright fail',"server value = "), depth + 1 )
  print value["server"]
  print "\n"
  print indent( color('bright fail',"local value  = "), depth + 1 )
  print value["local"] 
  print "\n\n"
end

#runObject



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/health_inspector/checklists/base.rb', line 38

def run
  banner "Inspecting #{self.class.title}"

  each_item do |item|
    failures = run_checks(item)

    if failures.empty?
      print_success(item.name) unless @context.quiet_success
    else
      print_failures(item.name, failures)
    end
  end
end

#run_check(check, item) ⇒ Object



93
94
95
96
97
# File 'lib/health_inspector/checklists/base.rb', line 93

def run_check(check, item)
  check_context = CheckContext.new(check, item, @context)
  check_context.call
  return check_context.failure
end

#run_checks(item) ⇒ Object



52
53
54
# File 'lib/health_inspector/checklists/base.rb', line 52

def run_checks(item)
  checks.map { |check| run_check(check, item) }.compact
end