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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HealthInspector::Color

#color

Constructor Details

#initialize(knife) ⇒ Base

Returns a new instance of Base.



30
31
32
# File 'lib/health_inspector/checklists/base.rb', line 30

def initialize(knife)
  @context = Context.new(knife)
end

Class Method Details

.optionObject



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

def self.option
  Inflecto.dasherize(underscored_class).to_sym
end

.run(knife) ⇒ Object



18
19
20
# File 'lib/health_inspector/checklists/base.rb', line 18

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

.titleObject



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

def title
  Inflecto.humanize(underscored_class).downcase
end

.underscored_classObject



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

def self.underscored_class
  Inflecto.underscore(Inflecto.demodulize(self.to_s))
end

Instance Method Details

#all_item_namesObject



46
47
48
# File 'lib/health_inspector/checklists/base.rb', line 46

def all_item_names
  (server_items + local_items).uniq.sort
end


80
81
82
83
84
# File 'lib/health_inspector/checklists/base.rb', line 80

def banner(message)
  ui.msg ''
  ui.msg message
  ui.msg '-' * 80
end

#indent(string, depth) ⇒ Object



156
157
158
# File 'lib/health_inspector/checklists/base.rb', line 156

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

#load_ruby_or_json_from_local(chef_class, folder, name) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/health_inspector/checklists/base.rb', line 134

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

  if !ruby_pathname.empty?
    instance = chef_class.new
    instance.from_file(ruby_pathname.first.to_s)
  elsif !json_pathname.empty?
    parsed_json = FFI_Yajl::Parser.parse(json_pathname.first.read)
    instance = load_instance_from(parsed_json, chef_class)
  elsif !js_pathname.empty?
    parsed_json = FFI_Yajl::Parser.parse(js_pathname.first.read)
    instance = load_instance_from(parsed_json, chef_class)
  end

  instance ? instance.to_hash : nil
rescue IOError
  nil
end

#load_validate(name) ⇒ Object



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

def load_validate(name)
  item = load_item(name)
  validate_item(item)
end

#local_itemsObject



42
43
44
# File 'lib/health_inspector/checklists/base.rb', line 42

def local_items
  fail NotImplementedError, 'You must implement this method in a subclass'
end


94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/health_inspector/checklists/base.rb', line 94

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

  failures.each do |message|
    if message.is_a? 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


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

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, _| 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


121
122
123
# File 'lib/health_inspector/checklists/base.rb', line 121

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


86
87
88
89
90
91
92
# File 'lib/health_inspector/checklists/base.rb', line 86

def print_success(subject)
  if $stdout.tty?
    ui.msg color('bright pass', "") + " #{subject}"
  else
    ui.msg "Success #{subject}"
  end
end


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

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



55
56
57
58
59
60
61
62
63
# File 'lib/health_inspector/checklists/base.rb', line 55

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

  results = Parallel.map(all_item_names) do |name|
    load_validate(name)
  end

  !results.include?(false)
end

#server_itemsObject



38
39
40
# File 'lib/health_inspector/checklists/base.rb', line 38

def server_items
  fail NotImplementedError, 'You must implement this method in a subclass'
end

#uiObject



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

def ui
  @context.knife.ui
end

#validate_item(item) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/health_inspector/checklists/base.rb', line 65

def validate_item(item)
  item.validate
  failures = item.errors

  if failures.empty?
    print_success(item.name)

    true
  else
    print_failures(item.name, failures)

    false
  end
end