class Puppet::Application::Script

Public Instance Methods

app_defaults() click to toggle source
Calls superclass method Puppet::Application#app_defaults
    # File lib/puppet/application/script.rb
115 def app_defaults
116   super.merge({
117     :default_file_terminus => :file_server,
118   })
119 end
help() click to toggle source
    # File lib/puppet/application/script.rb
 24   def help
 25     <<-HELP
 26 
 27 puppet-script(8) -- #{summary}
 28 ========
 29 
 30 SYNOPSIS
 31 --------
 32 Runs a puppet language script without compiling a catalog.
 33 
 34 
 35 USAGE
 36 -----
 37 puppet script [-h|--help] [-V|--version] [-d|--debug] [-v|--verbose]
 38   [-e|--execute]
 39   [-l|--logdest syslog|eventlog|<FILE>|console] [--noop]
 40   <file>
 41 
 42 
 43 DESCRIPTION
 44 -----------
 45 This is a standalone puppet script runner tool; use it to run puppet code
 46 without compiling a catalog.
 47 
 48 When provided with a modulepath, via command line or config file, puppet
 49 script can load functions, types, tasks and plans from modules.
 50 
 51 OPTIONS
 52 -------
 53 Note that any setting that's valid in the configuration
 54 file is also a valid long argument. For example, 'environment' is a
 55 valid setting, so you can specify '--environment mytest'
 56 as an argument.
 57 
 58 See the configuration file documentation at
 59 https://puppet.com/docs/puppet/latest/configuration.html for the
 60 full list of acceptable parameters. A commented list of all
 61 configuration options can also be generated by running puppet with
 62 '--genconfig'.
 63 
 64 * --debug:
 65   Enable full debugging.
 66 
 67 * --help:
 68   Print this help message
 69 
 70 
 71 * --logdest:
 72   Where to send log messages. Choose between 'syslog' (the POSIX syslog
 73   service), 'eventlog' (the Windows Event Log), 'console', or the path to a log
 74   file. Defaults to 'console'.
 75   Multiple destinations can be set using a comma separated list
 76   (eg: `/path/file1,console,/path/file2`)"
 77 
 78   A path ending with '.json' will receive structured output in JSON format. The
 79   log file will not have an ending ']' automatically written to it due to the
 80   appending nature of logging. It must be appended manually to make the content
 81   valid JSON.
 82 
 83   A path ending with '.jsonl' will receive structured output in JSON Lines
 84   format.
 85 
 86 * --noop:
 87   Use 'noop' mode where Puppet runs in a no-op or dry-run mode. This
 88   is useful for seeing what changes Puppet will make without actually
 89   executing the changes. Applies to tasks only.
 90 
 91 * --execute:
 92   Execute a specific piece of Puppet code
 93 
 94 * --verbose:
 95   Print extra information.
 96 
 97 EXAMPLE
 98 -------
 99     $ puppet script -l /tmp/manifest.log manifest.pp
100     $ puppet script --modulepath=/root/dev/modules -e 'notice("hello world")'
101 
102 
103 AUTHOR
104 ------
105 Henrik Lindberg
106 
107 
108 COPYRIGHT
109 ---------
110 Copyright (c) 2017 Puppet Inc., LLC Licensed under the Apache 2.0 License
111 
112     HELP
113   end
main() click to toggle source
    # File lib/puppet/application/script.rb
131 def main
132   # The tasks feature is always on
133   Puppet[:tasks] = true
134 
135   # Set the puppet code or file to use.
136   if options[:code] || command_line.args.length == 0
137     Puppet[:code] = options[:code] || STDIN.read
138   else
139     manifest = command_line.args.shift
140     raise _("Could not find file %{manifest}") % { manifest: manifest } unless Puppet::FileSystem.exist?(manifest)
141     Puppet.warning(_("Only one file can be used per run. Skipping %{files}") % { files: command_line.args.join(', ') }) if command_line.args.size > 0
142   end
143 
144   unless Puppet[:node_name_fact].empty?
145     # Collect the facts specified for that node
146     facts = Puppet::Node::Facts.indirection.find(Puppet[:node_name_value])
147     raise _("Could not find facts for %{node}") % { node: Puppet[:node_name_value] } unless facts
148 
149     Puppet[:node_name_value] = facts.values[Puppet[:node_name_fact]]
150     facts.name = Puppet[:node_name_value]
151   end
152 
153   # Find the Node
154   node = Puppet::Node.indirection.find(Puppet[:node_name_value])
155   raise _("Could not find node %{node}") % { node: Puppet[:node_name_value] } unless node
156 
157   configured_environment = node.environment || Puppet.lookup(:current_environment)
158 
159   apply_environment = manifest ?
160     configured_environment.override_with(:manifest => manifest) :
161     configured_environment
162 
163   # Modify the node descriptor to use the special apply_environment.
164   # It is based on the actual environment from the node, or the locally
165   # configured environment if the node does not specify one.
166   # If a manifest file is passed on the command line, it overrides
167   # the :manifest setting of the apply_environment.
168   node.environment = apply_environment
169 
170   # TRANSLATION, the string "For puppet script" is not user facing
171   Puppet.override({:current_environment => apply_environment}, "For puppet script") do
172     # Merge in the facts.
173     node.merge(facts.values) if facts
174 
175     # Add server facts so $server_facts[environment] exists when doing a puppet script
176     # SCRIPT TODO: May be needed when running scripts under orchestrator. Leave it for now.
177     #
178     node.add_server_facts({})
179 
180     begin
181       # Compile the catalog
182 
183       # When compiling, the compiler traps and logs certain errors
184       # Those that do not lead to an immediate exit are caught by the general
185       # rule and gets logged.
186       #
187       begin
188         # support the following features when evaluating puppet code
189         # * $facts with facts from host running the script
190         # * $settings with 'settings::*' namespace populated, and '$settings::all_local' hash
191         # * $trusted as setup when using puppet apply
192         # * an environment
193         #
194 
195         # fixup trusted information
196         node.sanitize()
197 
198         compiler = Puppet::Parser::ScriptCompiler.new(node.environment, node.name)
199         topscope = compiler.topscope
200 
201         # When scripting the trusted data are always local, but set them anyway
202         topscope.set_trusted(node.trusted_data)
203 
204         # Server facts are always about the local node's version etc.
205         topscope.set_server_facts(node.server_facts)
206 
207         # Set $facts for the node running the script
208         facts_hash = node.facts.nil? ? {} : node.facts.values
209         topscope.set_facts(facts_hash)
210 
211         # create the $settings:: variables
212         topscope.merge_settings(node.environment.name, false)
213 
214         compiler.compile()
215 
216       rescue Puppet::Error
217         # already logged and handled by the compiler, including Puppet::ParseErrorWithIssue
218         exit(1)
219       end
220 
221       exit(0)
222     rescue => detail
223       Puppet.log_exception(detail)
224       exit(1)
225     end
226   end
227 
228 ensure
229   if @profiler
230     Puppet::Util::Profiler.remove_profiler(@profiler)
231     @profiler.shutdown
232   end
233 end
run_command() click to toggle source
    # File lib/puppet/application/script.rb
121 def run_command
122   if Puppet.features.bolt?
123     Puppet.override(:bolt_executor => Bolt::Executor.new) do
124       main
125     end
126   else
127     raise _("Bolt must be installed to use the script application")
128   end
129 end
setup() click to toggle source
    # File lib/puppet/application/script.rb
235   def setup
236     exit(Puppet.settings.print_configs ? 0 : 1) if Puppet.settings.print_configs?
237 
238     handle_logdest_arg(Puppet[:logdest])
239     Puppet::Util::Log.newdestination(:console) unless options[:setdest]
240 
241     Signal.trap(:INT) do
242       $stderr.puts _("Exiting")
243       exit(1)
244     end
245 
246     # TODO: This skips applying the settings catalog for these settings, but
247     # the effect of doing this is unknown. It may be that it only works if there is a puppet
248     # installed where a settings catalog have already been applied...
249     # This saves 1/5th of the startup time
250 
251 #    Puppet.settings.use :main, :agent, :ssl
252 
253     # When running a script, the catalog is not relevant, and neither is caching of it
254     Puppet::Resource::Catalog.indirection.cache_class = nil
255 
256     # we do not want the last report to be persisted
257     Puppet::Transaction::Report.indirection.cache_class = nil
258 
259     set_log_level
260 
261     if Puppet[:profile]
262       @profiler = Puppet::Util::Profiler.add_profiler(Puppet::Util::Profiler::Aggregate.new(Puppet.method(:info), "script"))
263     end
264   end
summary() click to toggle source
   # File lib/puppet/application/script.rb
20 def summary
21   _("Run a puppet manifests as a script without compiling a catalog")
22 end