Module: Chef::Knife::StencilBase

Overview

This module is included everywhere, as per the conventions for knife plugins. It includes various helper methods.

Instance Method Summary collapse

Instance Method Details

#build_plugin_klass(plugin, command, action) ⇒ Object

Return a real Class built from the options given. Used to build EC2ServerCreate, for example



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/chef/knife/stencil_base.rb', line 65

def build_plugin_klass(plugin, command, action)
  begin
    klass = Object.const_get('Chef').const_get('Knife').const_get(plugin.to_s.capitalize + command.to_s.capitalize + action.to_s.capitalize)
    klass.respond_to?(:new)
  rescue NameError, NoMethodError => e
    puts("I can't find the correct gem for plugin #{config[:plugin]}, it does not declare class #{klas}, or that class does not repsond to 'new'. #{e}")
    puts("Try: gem install knife-#{config[:plugin]}")
  end

  return klass
end

#explain(string) ⇒ Object

Output method for explanation sub-command. Very basic at present.



78
79
80
81
82
# File 'lib/chef/knife/stencil_base.rb', line 78

def explain(string)
  if $*[2].to_s.downcase == "explain"
    puts "EXPLAIN: #{string}"
  end
end

#invoked_as_stencil?Boolean

Return true if the stencil plugin has been invoked

Returns:

  • (Boolean)


35
36
37
38
39
40
41
# File 'lib/chef/knife/stencil_base.rb', line 35

def invoked_as_stencil?
  if $*[0].downcase == 'stencil'
    return true
  end

  return false
end

#locate_config_value(key) ⇒ Object

This is used seemingly at random inside knife. Force our values.



29
30
31
32
# File 'lib/chef/knife/stencil_base.rb', line 29

def locate_config_value(key)
  key = key.to_sym
  config[key] || Chef::Config[:knife][key]
end

#normalize_path(path, root) ⇒ Object

Do exactly that



56
57
58
59
60
61
62
# File 'lib/chef/knife/stencil_base.rb', line 56

def normalize_path(path, root)
  unless path[0] == File::SEPARATOR	# FIXME: This will probably make this nasty on Windows
    return File.join(root, path).to_s
  else
    return path
  end
end

#stencil_rootObject

Determine where to look for stencils



44
45
46
47
48
49
50
51
52
53
# File 'lib/chef/knife/stencil_base.rb', line 44

def stencil_root
  stencil_root = '/'
  unless Chef::Config[:knife][:stencil_root] && Dir.exist?(Chef::Config[:knife][:stencil_root]) && stencil_root = Chef::Config[:knife][:stencil_root]
    [ '/etc/chef/stencils', "#{File.join(ENV['HOME'], '.chef/stencils')}" ].each do |directory|
      stencil_root = directory if Dir.exist?(directory)
    end
  end

  return stencil_root
end