Class: Puppet::Application::Inspect

Inherits:
Puppet::Application show all
Defined in:
lib/vendor/puppet/application/inspect.rb

Constant Summary

Constants inherited from Puppet::Application

DOCPATTERN

Constants included from Util

Util::AbsolutePathPosix, Util::AbsolutePathWindows

Instance Attribute Summary

Attributes inherited from Puppet::Application

#command_line, #options

Instance Method Summary collapse

Methods inherited from Puppet::Application

[], banner, clear!, clear?, #configure_indirector_routes, controlled_run, exit, find, #handlearg, #initialize, interrupted?, #main, #name, option, option_parser_commands, #parse_options, restart!, restart_requested?, #run, run_mode, #set_run_mode, #setup_logs, should_not_parse_config, should_parse_config, #should_parse_config?, should_parse_config?, stop!, stop_requested?

Methods included from Util

absolute_path?, activerecord_version, benchmark, binread, chuser, classproxy, #execfail, #execpipe, execute, execute_posix, execute_windows, logmethods, memory, path_to_uri, proxy, replace_file, safe_posix_fork, symbolize, symbolizehash, symbolizehash!, synchronize_on, thinmark, #threadlock, uri_to_path, wait_for_output, which, withumask

Methods included from Util::POSIX

#get_posix_field, #gid, #idfield, #methodbyid, #methodbyname, #search_posix_field, #uid

Constructor Details

This class inherits a constructor from Puppet::Application

Instance Method Details

#helpObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/vendor/puppet/application/inspect.rb', line 20

def help
  <<-'HELP'

puppet-inspect(8) -- Send an inspection report
========

SYNOPSIS
--------

Prepares and submits an inspection report to the puppet master.


USAGE
-----
puppet inspect [--archive_files] [--archive_file_server]


DESCRIPTION
-----------

This command uses the cached catalog from the previous run of 'puppet
agent' to determine which attributes of which resources have been
marked as auditable with the 'audit' metaparameter. It then examines
the current state of the system, writes the state of the specified
resource attributes to a report, and submits the report to the puppet
master.

Puppet inspect does not run as a daemon, and must be run manually or
from cron.


OPTIONS
-------

Any configuration setting which is valid in the configuration file is
also a valid long argument, e.g. '--server=master.domain.com'. See the
configuration file documentation at
http://docs.puppetlabs.com/references/latest/configuration.html for
the full list of acceptable settings.

* --archive_files:
During an inspect run, whether to archive files whose contents are audited to
a file bucket.

* --archive_file_server:
During an inspect run, the file bucket server to archive files to if
archive_files is set.  The default value is '$server'.


AUTHOR
------

Puppet Labs


COPYRIGHT
---------
Copyright (c) 2011 Puppet Labs, LLC Licensed under the Apache 2.0 License

  HELP
end

#preinitObject



107
108
109
110
# File 'lib/vendor/puppet/application/inspect.rb', line 107

def preinit
  require 'puppet'
  require 'puppet/file_bucket/dipper'
end

#run_commandObject



112
113
114
115
116
117
118
119
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/vendor/puppet/application/inspect.rb', line 112

def run_command
  benchmark(:notice, "Finished inspection") do
    retrieval_starttime = Time.now

    unless catalog = Puppet::Resource::Catalog.indirection.find(Puppet[:certname])
      raise "Could not find catalog for #{Puppet[:certname]}"
    end

    @report.configuration_version = catalog.version
    @report.environment = Puppet[:environment]

    inspect_starttime = Time.now
    @report.add_times("config_retrieval", inspect_starttime - retrieval_starttime)

    if Puppet[:archive_files]
      dipper = Puppet::FileBucket::Dipper.new(:Server => Puppet[:archive_file_server])
    end

    catalog.to_ral.resources.each do |ral_resource|
      audited_attributes = ral_resource[:audit]
      next unless audited_attributes

      status = Puppet::Resource::Status.new(ral_resource)

      begin
        audited_resource = ral_resource.to_resource
      rescue StandardError => detail
        puts detail.backtrace if Puppet[:trace]
        ral_resource.err "Could not inspect #{ral_resource}; skipping: #{detail}"
        audited_attributes.each do |name|
          event = ral_resource.event(
                                     :property => name,
                                     :status   => "failure",
                                     :audited  => true,
                                     :message  => "failed to inspect #{name}"
                                     )
          status.add_event(event)
        end
      else
        audited_attributes.each do |name|
          next if audited_resource[name].nil?
          # Skip :absent properties of :absent resources. Really, it would be nicer if the RAL returned nil for those, but it doesn't. ~JW
          if name == :ensure or audited_resource[:ensure] != :absent or audited_resource[name] != :absent
            event = ral_resource.event(
                                       :previous_value => audited_resource[name],
                                       :property       => name,
                                       :status         => "audit",
                                       :audited        => true,
                                       :message        => "inspected value is #{audited_resource[name].inspect}"
                                       )
            status.add_event(event)
          end
        end
      end
      if Puppet[:archive_files] and ral_resource.type == :file and audited_attributes.include?(:content)
        path = ral_resource[:path]
        if ::File.readable?(path)
          begin
            dipper.backup(path)
          rescue StandardError => detail
            Puppet.warning detail
          end
        end
      end
      @report.add_resource_status(status)
    end

    finishtime = Time.now
    @report.add_times("inspect", finishtime - inspect_starttime)
    @report.finalize_report

    begin
      Puppet::Transaction::Report.indirection.save(@report)
    rescue => detail
      puts detail.backtrace if Puppet[:trace]
      Puppet.err "Could not send report: #{detail}"
    end
  end
end

#setupObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/vendor/puppet/application/inspect.rb', line 82

def setup
  exit(Puppet.settings.print_configs ? 0 : 1) if Puppet.settings.print_configs?

  raise "Inspect requires reporting to be enabled. Set report=true in puppet.conf to enable reporting." unless Puppet[:report]

  @report = Puppet::Transaction::Report.new("inspect")

  Puppet::Util::Log.newdestination(@report)
  Puppet::Util::Log.newdestination(:console) unless options[:logset]

  Signal.trap(:INT) do
    $stderr.puts "Exiting"
    exit(1)
  end

  if options[:debug]
    Puppet::Util::Log.level = :debug
  elsif options[:verbose]
    Puppet::Util::Log.level = :info
  end

  Puppet::Transaction::Report.indirection.terminus_class = :rest
  Puppet::Resource::Catalog.indirection.terminus_class = :yaml
end