class HealthInspector::Checklists::Base

Public Class Methods

new(knife) click to toggle source
# File lib/health_inspector/checklists/base.rb, line 30
def initialize(knife)
  @context = Context.new(knife)
end
option() click to toggle source
# File lib/health_inspector/checklists/base.rb, line 22
def self.option
  Inflecto.dasherize(underscored_class).to_sym
end
run(knife) click to toggle source
# File lib/health_inspector/checklists/base.rb, line 18
def self.run(knife)
  new(knife).run
end
title() click to toggle source
# File lib/health_inspector/checklists/base.rb, line 13
def title
  Inflecto.humanize(underscored_class).downcase
end
underscored_class() click to toggle source
# File lib/health_inspector/checklists/base.rb, line 26
def self.underscored_class
  Inflecto.underscore(Inflecto.demodulize(self.to_s))
end

Public Instance Methods

all_item_names() click to toggle source
# File lib/health_inspector/checklists/base.rb, line 46
def all_item_names
  (server_items + local_items).uniq.sort
end
banner(message) click to toggle source
indent(string, depth) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/health_inspector/checklists/base.rb, line 50
def load_validate(name)
  item = load_item(name)
  validate_item(item)
end
local_items() click to toggle source
# File lib/health_inspector/checklists/base.rb, line 42
def local_items
  fail NotImplementedError, 'You must implement this method in a subclass'
end
print_failures(subject, failures) click to toggle source
print_failures_from_hash(message, depth = 2) click to toggle source
print_key(key, depth) click to toggle source
print_success(subject) click to toggle source
print_value_diff(value, depth) click to toggle source
run() click to toggle source
# 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_items() click to toggle source
# File lib/health_inspector/checklists/base.rb, line 38
def server_items
  fail NotImplementedError, 'You must implement this method in a subclass'
end
ui() click to toggle source
# File lib/health_inspector/checklists/base.rb, line 34
def ui
  @context.knife.ui
end
validate_item(item) click to toggle source
# 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

Private Instance Methods

load_instance_from(parsed_json, chef_class) click to toggle source

This supports both Chef 11 and 12+ (assuming the public API still has from_hash

# File lib/health_inspector/checklists/base.rb, line 164
def load_instance_from(parsed_json, chef_class)
  if Gem::Version.new(Chef::VERSION) < Gem::Version.new('12.0.0')
    chef_class.json_create(parsed_json)
  else
    chef_class.from_hash(parsed_json)
  end
end