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