Class: Chef::Provider

Inherits:
Object show all
Extended by:
Mixin::ConvertToClassName
Includes:
Mixin::EnforceOwnershipAndPermissions, Mixin::RecipeDefinitionDSLCore, Mixin::WhyRun
Defined in:
lib/chef/provider.rb,
lib/chef/provider/env.rb,
lib/chef/provider/git.rb,
lib/chef/provider/log.rb,
lib/chef/provider/cron.rb,
lib/chef/provider/file.rb,
lib/chef/provider/link.rb,
lib/chef/provider/ohai.rb,
lib/chef/provider/user.rb,
lib/chef/provider/group.rb,
lib/chef/provider/mdadm.rb,
lib/chef/provider/mount.rb,
lib/chef/provider/deploy.rb,
lib/chef/provider/script.rb,
lib/chef/provider/execute.rb,
lib/chef/provider/package.rb,
lib/chef/provider/service.rb,
lib/chef/provider/user/pw.rb,
lib/chef/provider/erl_call.rb,
lib/chef/provider/group/pw.rb,
lib/chef/provider/ifconfig.rb,
lib/chef/provider/template.rb,
lib/chef/provider/directory.rb,
lib/chef/provider/group/aix.rb,
lib/chef/provider/user/dscl.rb,
lib/chef/provider/breakpoint.rb,
lib/chef/provider/group/dscl.rb,
lib/chef/provider/group/suse.rb,
lib/chef/provider/ruby_block.rb,
lib/chef/provider/subversion.rb,
lib/chef/provider/env/windows.rb,
lib/chef/provider/mount/mount.rb,
lib/chef/provider/package/apt.rb,
lib/chef/provider/package/ips.rb,
lib/chef/provider/package/rpm.rb,
lib/chef/provider/package/yum.rb,
lib/chef/provider/remote_file.rb,
lib/chef/provider/cron/solaris.rb,
lib/chef/provider/http_request.rb,
lib/chef/provider/package/dpkg.rb,
lib/chef/provider/service/init.rb,
lib/chef/provider/user/solaris.rb,
lib/chef/provider/user/useradd.rb,
lib/chef/provider/user/windows.rb,
lib/chef/provider/cookbook_file.rb,
lib/chef/provider/group/gpasswd.rb,
lib/chef/provider/group/usermod.rb,
lib/chef/provider/group/windows.rb,
lib/chef/provider/mount/windows.rb,
lib/chef/provider/group/groupadd.rb,
lib/chef/provider/group/groupmod.rb,
lib/chef/provider/package/pacman.rb,
lib/chef/provider/package/zypper.rb,
lib/chef/provider/service/debian.rb,
lib/chef/provider/service/macosx.rb,
lib/chef/provider/service/redhat.rb,
lib/chef/provider/service/simple.rb,
lib/chef/provider/deploy/revision.rb,
lib/chef/provider/package/freebsd.rb,
lib/chef/provider/package/portage.rb,
lib/chef/provider/package/smartos.rb,
lib/chef/provider/package/solaris.rb,
lib/chef/provider/resource_update.rb,
lib/chef/provider/service/freebsd.rb,
lib/chef/provider/service/insserv.rb,
lib/chef/provider/service/solaris.rb,
lib/chef/provider/service/upstart.rb,
lib/chef/provider/package/macports.rb,
lib/chef/provider/package/rubygems.rb,
lib/chef/provider/remote_directory.rb,
lib/chef/provider/service/invokercd.rb,
lib/chef/provider/deploy/timestamped.rb,
lib/chef/provider/package/easy_install.rb,
lib/chef/provider/whyrun_safe_ruby_block.rb

Defined Under Namespace

Classes: Breakpoint, CookbookFile, Cron, Deploy, Directory, Env, ErlCall, Execute, File, Git, Group, HttpRequest, Ifconfig, Link, Log, Mdadm, Mount, Ohai, Package, RemoteDirectory, RemoteFile, ResourceUpdate, Route, RubyBlock, Script, Service, Subversion, Template, User, WhyrunSafeRubyBlock

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixin::ConvertToClassName

convert_to_class_name, convert_to_snake_case, filename_to_qualified_string, snake_case_basename

Methods included from Mixin::EnforceOwnershipAndPermissions

#access_controls, #enforce_ownership_and_permissions

Methods included from Mixin::RecipeDefinitionDSLCore

#method_missing

Methods included from Mixin::Language

#data_bag, #data_bag_item, #platform?, #platform_family?, #search, #value_for_platform, #value_for_platform_family

Constructor Details

#initialize(new_resource, run_context) ⇒ Provider

Returns a new instance of Provider.



42
43
44
45
46
47
48
# File 'lib/chef/provider.rb', line 42

def initialize(new_resource, run_context)
  @new_resource = new_resource
  @action = action
  @current_resource = nil
  @run_context = run_context
  @converge_actions = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Chef::Mixin::RecipeDefinitionDSLCore

Instance Attribute Details

#actionObject

– TODO: this should be a reader, and the action should be passed in the constructor; however, many/most subclasses override the constructor so changing the arity would be a breaking change. Change this at the next break, e.g., Chef 11.



40
41
42
# File 'lib/chef/provider.rb', line 40

def action
  @action
end

#current_resourceObject

Returns the value of attribute current_resource.



32
33
34
# File 'lib/chef/provider.rb', line 32

def current_resource
  @current_resource
end

#new_resourceObject

Returns the value of attribute new_resource.



31
32
33
# File 'lib/chef/provider.rb', line 31

def new_resource
  @new_resource
end

#run_contextObject

Returns the value of attribute run_context.



33
34
35
# File 'lib/chef/provider.rb', line 33

def run_context
  @run_context
end

Class Method Details

.build_from_file(cookbook_name, filename, run_context) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/chef/provider.rb', line 181

def build_from_file(cookbook_name, filename, run_context)
  pname = filename_to_qualified_string(cookbook_name, filename)

  # Add log entry if we override an existing light-weight provider.
  class_name = convert_to_class_name(pname)
  overriding = Chef::Provider.const_defined?(class_name)
  Chef::Log.info("#{class_name} light-weight provider already initialized -- overriding!") if overriding

  new_provider_class = Class.new self do |cls|

    include Chef::Mixin::RecipeDefinitionDSLCore

    def load_current_resource
      # silence Chef::Exceptions::Override exception
    end

    class << cls
      include Chef::Mixin::FromFile

      # setup DSL's shortcut methods
      def action(name, &block)
        define_method("action_#{name.to_s}") do
          instance_eval(&block)
        end
      end
    end

    # load provider definition from file
    cls.class_from_file(filename)
  end

  # register new class as a Chef::Provider
  pname = filename_to_qualified_string(cookbook_name, filename)
  class_name = convert_to_class_name(pname)
  Chef::Provider.const_set(class_name, new_provider_class)
  Chef::Log.debug("Loaded contents of #{filename} into a provider named #{pname} defined in Chef::Provider::#{class_name}")

  new_provider_class
end

Instance Method Details

#action_nothingObject



81
82
83
84
# File 'lib/chef/provider.rb', line 81

def action_nothing
  Chef::Log.debug("Doing nothing for #{@new_resource.to_s}")
  true
end

#cleanup_after_convergeObject



78
79
# File 'lib/chef/provider.rb', line 78

def cleanup_after_converge
end

#cookbook_nameObject



67
68
69
# File 'lib/chef/provider.rb', line 67

def cookbook_name
  new_resource.cookbook_name
end

#define_resource_requirementsObject



75
76
# File 'lib/chef/provider.rb', line 75

def define_resource_requirements
end

#eventsObject



86
87
88
# File 'lib/chef/provider.rb', line 86

def events
  run_context.events
end

#load_current_resourceObject



71
72
73
# File 'lib/chef/provider.rb', line 71

def load_current_resource
  raise Chef::Exceptions::Override, "You must override load_current_resource in #{self.to_s}"
end

#nodeObject



58
59
60
# File 'lib/chef/provider.rb', line 58

def node
  run_context && run_context.node
end

#process_resource_requirementsObject



127
128
129
130
# File 'lib/chef/provider.rb', line 127

def process_resource_requirements
  requirements.run(:all_actions) unless @action == :nothing
  requirements.run(@action)
end

#requirementsObject



141
142
143
# File 'lib/chef/provider.rb', line 141

def requirements
  @requirements ||= ResourceRequirements.new(@new_resource, run_context)
end

#resource_collectionObject

Used by providers supporting embedded recipes



63
64
65
# File 'lib/chef/provider.rb', line 63

def resource_collection
  run_context && run_context.resource_collection
end

#run_action(action = nil) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/chef/provider.rb', line 90

def run_action(action=nil)
  @action = action unless action.nil?

  # TODO: it would be preferable to get the action to be executed in the
  # constructor...

  # user-defined LWRPs may include unsafe load_current_resource methods that cannot be run in whyrun mode
  if !whyrun_mode? || whyrun_supported?
    load_current_resource
    events.resource_current_state_loaded(@new_resource, @action, @current_resource)
  elsif whyrun_mode? && !whyrun_supported?
    events.resource_current_state_load_bypassed(@new_resource, @action, @current_resource)
  end

  define_resource_requirements
  process_resource_requirements

  # user-defined providers including LWRPs may 
  # not include whyrun support - if they don't support it
  # we can't execute any actions while we're running in
  # whyrun mode. Instead we 'fake' whyrun by documenting that
  # we can't execute the action.
  # in non-whyrun mode, this will still cause the action to be
  # executed normally.
  if whyrun_supported? && !requirements.action_blocked?(@action)
    send("action_#{@action}")
  elsif whyrun_mode?
    events.resource_bypassed(@new_resource, @action, self)
  else
    send("action_#{@action}")
  end

  set_updated_status

  cleanup_after_converge
end

#set_updated_statusObject



132
133
134
135
136
137
138
139
# File 'lib/chef/provider.rb', line 132

def set_updated_status
  if converge_actions.empty? && !@new_resource.updated_by_last_action?
    events.resource_up_to_date(@new_resource, @action)
  else
    events.resource_updated(@new_resource, @action)
    new_resource.updated_by_last_action(true) 
  end
end

#whyrun_mode?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/chef/provider.rb', line 50

def whyrun_mode?
  Chef::Config[:why_run]
end

#whyrun_supported?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/chef/provider.rb', line 54

def whyrun_supported?
  false
end