class Object

Constants

CommandDeactivateNode
CommandStoreReport

Public Instance Methods

add_v4_fields_to_event(resource_status, event_hash) click to toggle source

Backwards compatibility with versions of Puppet prior to report format 4

@api private

# File lib/puppet/reports/puppetdb.rb, line 159
def add_v4_fields_to_event(resource_status, event_hash)
  if report_format >= 4
    event_hash.merge("containment-path" => resource_status.containment_path)
  else
    event_hash.merge("containment-path" => nil)
  end
end
add_v4_fields_to_report(report_hash) click to toggle source

Backwards compatibility with versions of Puppet prior to report format 4

@api private

# File lib/puppet/reports/puppetdb.rb, line 148
def add_v4_fields_to_report(report_hash)
  if report_format >= 4
    report_hash.merge("transaction-uuid" => transaction_uuid)
  else
    report_hash.merge("transaction-uuid" => nil)
  end
end
build_events_list() click to toggle source

@api private

# File lib/puppet/reports/puppetdb.rb, line 65
def build_events_list
  profile "Build events list (count: #{resource_statuses.count})" do
    filter_events(resource_statuses.inject([]) do |events, status_entry|
      _, status = *status_entry
      if ! (status.events.empty?)
        events.concat(status.events.map { |event| event_to_hash(status, event) })
      elsif status.skipped
        events.concat([fabricate_event(status, "skipped")])
      elsif status.failed
        # PP-254:
        #   We have to fabricate resource events here due to a bug/s in report providers
        #   that causes them not to include events on a resource status that has failed.
        #   When PuppetDB is able to make a hard break from older version of Puppet that
        #   have this bug, we can remove this behavior.
        events.concat([fabricate_event(status, "failure")])
      end
      events
    end)
  end
end
config() click to toggle source

Helper method for accessing the puppetdb configuration

@api private

# File lib/puppet/reports/puppetdb.rb, line 183
def config
  Puppet::Util::Puppetdb.config
end
destination_file(timestamp) click to toggle source

Returns the location to leave the output. This is really only here for testing. :/

# File lib/puppet/face/storeconfigs.rb, line 102
def destination_file(timestamp)
  File.expand_path("storeconfigs-#{timestamp.strftime('%Y%m%d%H%M%S')}.tar")
end
event_to_hash(resource_status, event) click to toggle source

Convert an instance of `Puppet::Transaction::Event` to a hash suitable for sending over the wire to PuppetDB

@api private

# File lib/puppet/reports/puppetdb.rb, line 108
def event_to_hash(resource_status, event)
  add_v4_fields_to_event(resource_status,
    {
      "status"            => event.status,
      "timestamp"         => Puppet::Util::Puppetdb.to_wire_time(event.time),
      "resource-type"     => resource_status.resource_type,
      "resource-title"    => resource_status.title,
      "property"          => event.property,
      "new-value"         => event.desired_value,
      "old-value"         => event.previous_value,
      "message"           => event.message,
      "file"              => resource_status.file,
      "line"              => resource_status.line
    })
end
execute(command) click to toggle source

Execute a command using Puppet's execution static method.

@param command [Array<String>, String] the command to execute. If it is

an Array the first element should be the executable and the rest of the
elements should be the individual arguments to that executable.

@return [Puppet::Util::Execution::ProcessOutput] output as specified by options @raise [Puppet::ExecutionFailure] if the executed chiled process did not exit with status == 0 and `failonfail` is

`true`.
# File lib/puppet/face/storeconfigs.rb, line 114
def execute(command)
  Puppet::Util::Execution.execute(command)
end
fabricate_event(resource_status, event_status) click to toggle source

Given an instance of `Puppet::Resource::Status` and a status string, this method fabricates a PuppetDB event object with the provided `“status”`.

@api private

# File lib/puppet/reports/puppetdb.rb, line 129
def fabricate_event(resource_status, event_status)
  add_v4_fields_to_event(resource_status,
    {
      "status"            => event_status,
      "timestamp"         => Puppet::Util::Puppetdb.to_wire_time(resource_status.time),
      "resource-type"     => resource_status.resource_type,
      "resource-title"    => resource_status.title,
      "property"          => nil,
      "new-value"         => nil,
      "old-value"         => nil,
      "message"           => nil,
      "file"              => resource_status.file,
      "line"              => resource_status.line
    })
end
filter_events(events) click to toggle source

Filter out blacklisted events, if we're configured to do so

@api private

# File lib/puppet/reports/puppetdb.rb, line 170
def filter_events(events)
  if config.ignore_blacklisted_events?
    profile "Filter blacklisted events" do
      events.select { |e| ! config.is_event_blacklisted?(e) }
    end
  else
    events
  end
end
headers() click to toggle source
# File lib/puppet/face/node/status.rb, line 77
def headers
  {
    "Accept" => "application/json",
    "Content-Type" => "application/x-www-form-urlencoded; charset=UTF-8",
  }
end
node_to_catalog_hash(node) click to toggle source
# File lib/puppet/face/storeconfigs.rb, line 118
def node_to_catalog_hash(node)
  resources = node.resources.map { |resource| resource_to_hash(resource) }
  edges = node.resources.map { |resource| resource_to_edge_hash(resource) }

  {
    :metadata => {
      :api_version => 1,
    },
    :data => {
      :name => node.name,
      :version => node.last_compile || Time.now,
      :edges => edges,
      :resources => resources + [stage_main_hash],
    },
  }
end
process() click to toggle source
# File lib/puppet/reports/puppetdb.rb, line 19
def process
  profile "report#process" do
    submit_command(self.host, report_to_hash, CommandStoreReport, 3)
  end
end
puppet_version() click to toggle source

@api private

# File lib/puppet/reports/puppetdb.rb, line 40
def puppet_version
  @puppet_version
end
report_format() click to toggle source

@api private

# File lib/puppet/reports/puppetdb.rb, line 35
def report_format
  @report_format
end
report_to_hash() click to toggle source

Convert `self` (an instance of `Puppet::Transaction::Report`) to a hash suitable for sending over the wire to PuppetDB

@api private

# File lib/puppet/reports/puppetdb.rb, line 48
def report_to_hash
  profile "Convert report to wire format hash" do
    add_v4_fields_to_report(
      {
        "certname"                => host,
        "puppet-version"          => puppet_version,
        "report-format"           => report_format,
        "configuration-version"   => configuration_version.to_s,
        "start-time"              => Puppet::Util::Puppetdb.to_wire_time(time),
        "end-time"                => Puppet::Util::Puppetdb.to_wire_time(time + run_duration),
        "resource-events"         => build_events_list,
        "environment"             => environment,
      })
  end
end
resource_to_edge_hash(resource) click to toggle source

The catalog must have edges, so everything is contained by Stage!

# File lib/puppet/face/storeconfigs.rb, line 162
def resource_to_edge_hash(resource)
  {
    'source' => {'type' => 'Stage', 'title' => 'main'},
    'target' => {'type' => resource.restype, 'title' => resource.title},
    'relationship' => 'contains',
  }
end
resource_to_hash(resource) click to toggle source
# File lib/puppet/face/storeconfigs.rb, line 135
def resource_to_hash(resource)
  parameters = resource.param_values.inject({}) do |params,param_value|
    if params.has_key?(param_value.param_name.name)
      value = [params[param_value.param_name.name],param_value.value].flatten
    else
      value = param_value.value
    end
    params.merge(param_value.param_name.name => value)
  end

  tags = resource.puppet_tags.map(&:name).uniq.sort

  hash = {
    :type       => resource.restype,
    :title      => resource.title,
    :exported   => true,
    :parameters => parameters,
    :tags       => tags,
  }

  hash[:file] = resource.file if resource.file
  hash[:line] = resource.line if resource.line

  hash
end
run_duration() click to toggle source

@api private

# File lib/puppet/reports/puppetdb.rb, line 87
def run_duration
  # TODO: this is wrong in puppet.  I am consistently seeing reports where
  # start-time + this value is less than the timestamp on the individual
  # resource events.  Not sure what the best short-term fix is yet; the long
  # term fix is obviously to make the correct data available in puppet.
  # I've filed a ticket against puppet here:
  #  http://projects.puppetlabs.com/issues/16480
  #
  # NOTE: failed reports have an empty metrics hash. Just send 0 for run time,
  #  since we don't have access to any better information.
  if metrics["time"] and metrics["time"]["total"]
    metrics["time"]["total"]
  else
    raise Puppet::Error, "Report from #{host} contained no metrics, which is often caused by a failed catalog compilation. Unable to process."
  end
end
stage_main_hash() click to toggle source
# File lib/puppet/face/storeconfigs.rb, line 170
def stage_main_hash
  {
    :type       => 'Stage',
    :title      => 'main',
    :exported   => false,
    :parameters => {},
    :tags       => ['stage', 'main'],
  }
end