Module: KnifeWip::Helpers

Included in:
NodeUnwip, NodeWip, Plugin
Defined in:
lib/chef/knife/knife-wip.rb

Overview

Public: collection namespace for some helper functions and methods

Instance Method Summary collapse

Instance Method Details

#app_configObject

Public: get the configuration loaded from the yaml files

Returns the appconf object with config loaded



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/chef/knife/knife-wip.rb', line 37

def app_config
  return @app_config unless @app_config.nil?

  @app_config = ::AppConf.new
  load_paths = []
  # first look for configuration in the cookbooks folder
  load_paths << File.expand_path("#{cookbook_path.gsub('cookbooks','')}/config/knife-wip-config.yml")
  # or see if we are in the cookbooks repo
  load_paths << File.expand_path('config/knife-wip-config.yml')
  # global config in /etc has higher priority
  load_paths << '/etc/knife-wip-config.yml'
  # finally the user supplied config file is loaded
  load_paths << File.expand_path('~/.chef/knife-wip-config.yml')

  # load all the paths
  load_paths.each do |load_path|
    if File.exists?(load_path)
      @app_config.load(load_path)
    end
  end

  @app_config
end

#cookbook_pathObject



69
70
71
72
# File 'lib/chef/knife/knife-wip.rb', line 69

def cookbook_path
  ensure_cookbook_path!
  [config[:cookbook_path] ||= ::Chef::Config.cookbook_path].flatten[0]
end

#ensure_cookbook_path!Object



61
62
63
64
65
66
67
# File 'lib/chef/knife/knife-wip.rb', line 61

def ensure_cookbook_path!
  if config[:cookbook_path].nil?
    ui.fatal "No default cookbook_path; Specify with -o or fix your knife.rb."
    show_usage
    exit(1)
  end
end

#load_pluginsObject

Public: load all plugins that are configured

this method merely tries to require the plugin files, instantiates an object from them and then stores it in @plugins. That way the knife commands can just call all plugins however they want from there.

Returns nothing



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/chef/knife/knife-wip.rb', line 17

def load_plugins

  @plugins ||= []

  app_config[:plugins].to_hash.each do |plugin, plugin_config|
    begin
      require "knife-wip/plugins/#{plugin.downcase}"
      # apparently this is the way to dynamically instantiate ruby objects
      @plugins << KnifeWip::Plugins.const_get(plugin.capitalize).new(plugin_config)
    rescue LoadError
      ui.warn "Configured plugin '#{plugin}' doesn't exist."
    end

  end

end