class ChefCore::Actions::Base

Derive new Actions from Action::Base “target_host” is a TargetHost that the action is being applied to. May be nil

if the action does not require a target.

“config” is hash containing any options that your command may need

Implement perform_action to perform whatever action your class is intended to do. Run time will be captured via telemetry and categorized under “:action” with the unqualified class name of your Action.

Attributes

config[R]
target_host[R]

Public Class Methods

new(config = {}) click to toggle source
# File lib/chef_core/actions/base.rb, line 34
def initialize(config = {})
  c = config.dup
  @target_host = c.delete :target_host
  # Remaining options are for child classes to make use of.
  @config = c
end

Public Instance Methods

name() click to toggle source
# File lib/chef_core/actions/base.rb, line 58
def name
  self.class.name.split("::").last
end
notify(event, *args) click to toggle source

Invokes the notification handler with notifications of progress or events that occur while the action his running. The notification handler is provided as a block to `#run`.

event - a symbol describing the thing currently being done by your

action , eg ":download", ":error", ":version_check", etc.

*args - any arguments that should be passed along with this notification to the

notification handler block.
# File lib/chef_core/actions/base.rb, line 94
def notify(event, *args)
  return if @notification_handler.nil?

  ChefCore::Log.debug("[#{name}] Event: #{event}, Event Data: #{args}")
  @notification_handler.call(event, args) if @notification_handler
end
perform_action() click to toggle source
# File lib/chef_core/actions/base.rb, line 62
def perform_action
  raise NotImplemented
end
run(&block) click to toggle source
# File lib/chef_core/actions/base.rb, line 41
def run(&block)
  @notification_handler = block
  timed_action_capture(self) do
    begin
      perform_action
    rescue StandardError => e
      # Give the caller a chance to clean up - if an exception is
      # raised it'll otherwise get routed through the executing thread,
      # providing no means of feedback for the caller's current task.
      notify(:error, e)
      @error = e
    end
  end
  # Raise outside the block to ensure that the telemetry cpature completes
  raise @error unless @error.nil?
end
timed_action_capture(action, &block) click to toggle source

TODO bootstrap 2019-02-07 - we'll need to find the right way to keep this in telemeter, there are a bunch of exposed details here that the caller shouldn't care about. I've moved it here temporarily to keep things running until we come back to this for telemetry updates.

# File lib/chef_core/actions/base.rb, line 70
def timed_action_capture(action, &block)
  # Note: we do not directly capture hostname for privacy concerns, but
  # using a sha1 digest will allow us to anonymously see
  # unique hosts to derive number of hosts affected by a command
  target = action.target_host
  target_data = { platform: {}, hostname_sha1: nil, transport_type: nil }
  if target
    target_data[:platform][:name] = target.base_os # :windows, :linux, eventually :macos
    target_data[:platform][:version] = target.version
    target_data[:platform][:architecture] = target.architecture
    target_data[:hostname_sha1] = Digest::SHA1.hexdigest(target.hostname.downcase)
    target_data[:transport_type] = target.transport_type
  end
  ChefCore::Telemeter.timed_capture(:action, { action: action.name, target: target_data }, &block)
end