Class: Chef::Provider

Inherits:
Object show all
Extended by:
Mixin::DescendantsTracker
Includes:
Mixin::ShellOut, 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/batch.rb,
lib/chef/provider/group.rb,
lib/chef/provider/mdadm.rb,
lib/chef/provider/mount.rb,
lib/chef/provider/deploy.rb,
lib/chef/provider/reboot.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/cron/aix.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/user/aix.rb,
lib/chef/provider/cron/unix.rb,
lib/chef/provider/directory.rb,
lib/chef/provider/group/aix.rb,
lib/chef/provider/lwrp_base.rb,
lib/chef/provider/mount/aix.rb,
lib/chef/provider/user/dscl.rb,
lib/chef/provider/breakpoint.rb,
lib/chef/provider/dsc_script.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/aix.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/service/aix.rb,
lib/chef/provider/file/content.rb,
lib/chef/provider/http_request.rb,
lib/chef/provider/ifconfig/aix.rb,
lib/chef/provider/package/dpkg.rb,
lib/chef/provider/registry_key.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/solaris.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/windows_script.rb,
lib/chef/provider/deploy/revision.rb,
lib/chef/provider/ifconfig/debian.rb,
lib/chef/provider/ifconfig/redhat.rb,
lib/chef/provider/package/paludis.rb,
lib/chef/provider/package/portage.rb,
lib/chef/provider/package/smartos.rb,
lib/chef/provider/package/solaris.rb,
lib/chef/provider/package/windows.rb,
lib/chef/provider/remote_file/ftp.rb,
lib/chef/provider/resource_update.rb,
lib/chef/provider/service/aixinit.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/template_finder.rb,
lib/chef/provider/package/homebrew.rb,
lib/chef/provider/package/macports.rb,
lib/chef/provider/package/rubygems.rb,
lib/chef/provider/remote_directory.rb,
lib/chef/provider/remote_file/http.rb,
lib/chef/provider/template/content.rb,
lib/chef/provider/powershell_script.rb,
lib/chef/provider/service/invokercd.rb,
lib/chef/provider/deploy/timestamped.rb,
lib/chef/provider/package/freebsd/pkg.rb,
lib/chef/provider/package/windows/msi.rb,
lib/chef/provider/remote_file/content.rb,
lib/chef/provider/remote_file/fetcher.rb,
lib/chef/provider/package/easy_install.rb,
lib/chef/provider/package/freebsd/base.rb,
lib/chef/provider/package/freebsd/port.rb,
lib/chef/provider/cookbook_file/content.rb,
lib/chef/provider/package/freebsd/pkgng.rb,
lib/chef/provider/remote_file/local_file.rb,
lib/chef/provider/whyrun_safe_ruby_block.rb,
lib/chef/provider/remote_file/cache_control_data.rb

Defined Under Namespace

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

Constant Summary

Constants included from Mixin::ShellOut

Mixin::ShellOut::DEPRECATED_OPTIONS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixin::DescendantsTracker

descendants, descendants, direct_descendants, direct_descendants, find_descendants_by_name, find_descendants_by_name, inherited, store_inherited

Methods included from Mixin::ShellOut

#run_command_compatible_options, #shell_out, #shell_out!, #shell_out_with_systems_locale, #shell_out_with_systems_locale!

Constructor Details

#initialize(new_resource, run_context) ⇒ Provider

Returns a new instance of Provider.



69
70
71
72
73
74
75
76
77
78
# File 'lib/chef/provider.rb', line 69

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

  @recipe_name = nil
  @cookbook_name = nil
end

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.



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

def action
  @action
end

#cookbook_nameObject (readonly)

Returns the value of attribute cookbook_name.



60
61
62
# File 'lib/chef/provider.rb', line 60

def cookbook_name
  @cookbook_name
end

#current_resourceObject

Returns the value of attribute current_resource.



56
57
58
# File 'lib/chef/provider.rb', line 56

def current_resource
  @current_resource
end

#new_resourceObject

Returns the value of attribute new_resource.



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

def new_resource
  @new_resource
end

#recipe_nameObject (readonly)

Returns the value of attribute recipe_name.



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

def recipe_name
  @recipe_name
end

#run_contextObject

Returns the value of attribute run_context.



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

def run_context
  @run_context
end

Class Method Details

.node_mapObject



36
37
38
# File 'lib/chef/provider.rb', line 36

def node_map
  @node_map ||= Chef::NodeMap.new
end

.provides(resource_name, opts = {}, &block) ⇒ Object



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

def provides(resource_name, opts={}, &block)
  node_map.set(resource_name.to_sym, true, opts, &block)
end

.provides?(node, resource) ⇒ Boolean

provides a node on the resource (early binding)

Returns:

  • (Boolean)


45
46
47
# File 'lib/chef/provider.rb', line 45

def provides?(node, resource)
  node_map.get(node, resource.resource_name)
end

.supports?(resource, action) ⇒ Boolean

supports the given resource and action (late binding)

Returns:

  • (Boolean)


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

def supports?(resource, action)
  true
end

Instance Method Details

#action_nothingObject



111
112
113
114
# File 'lib/chef/provider.rb', line 111

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

#cleanup_after_convergeObject



108
109
# File 'lib/chef/provider.rb', line 108

def cleanup_after_converge
end

#converge_by(descriptions, &block) ⇒ Object



179
180
181
# File 'lib/chef/provider.rb', line 179

def converge_by(descriptions, &block)
  converge_actions.add_action(descriptions, &block)
end

#define_resource_requirementsObject



105
106
# File 'lib/chef/provider.rb', line 105

def define_resource_requirements
end

#eventsObject



116
117
118
# File 'lib/chef/provider.rb', line 116

def events
  run_context.events
end

#load_current_resourceObject



101
102
103
# File 'lib/chef/provider.rb', line 101

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

#nodeObject



88
89
90
# File 'lib/chef/provider.rb', line 88

def node
  run_context && run_context.node
end

#process_resource_requirementsObject



157
158
159
160
# File 'lib/chef/provider.rb', line 157

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

#requirementsObject



175
176
177
# File 'lib/chef/provider.rb', line 175

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

#resource_collectionObject

Used by providers supporting embedded recipes



93
94
95
# File 'lib/chef/provider.rb', line 93

def resource_collection
  run_context && run_context.resource_collection
end

#resource_updated?Boolean

Returns:

  • (Boolean)


162
163
164
# File 'lib/chef/provider.rb', line 162

def resource_updated?
  !converge_actions.empty? || @new_resource.updated_by_last_action?
end

#run_action(action = nil) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/chef/provider.rb', line 120

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



166
167
168
169
170
171
172
173
# File 'lib/chef/provider.rb', line 166

def set_updated_status
  if !resource_updated?
    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)


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

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

#whyrun_supported?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/chef/provider.rb', line 84

def whyrun_supported?
  false
end