Method: Puppet::Application::Script#main

Defined in:
lib/puppet/application/script.rb

#mainObject



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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/puppet/application/script.rb', line 130

def main
  # The tasks feature is always on
  Puppet[:tasks] = true

  # Set the puppet code or file to use.
  if options[:code] || command_line.args.length == 0
    Puppet[:code] = options[:code] || STDIN.read
  else
    manifest = command_line.args.shift
    raise _("Could not find file %{manifest}") % { manifest: manifest } unless Puppet::FileSystem.exist?(manifest)
    Puppet.warning(_("Only one file can be used per run. Skipping %{files}") % { files: command_line.args.join(', ') }) if command_line.args.size > 0
  end

  unless Puppet[:node_name_fact].empty?
    # Collect the facts specified for that node
    facts = Puppet::Node::Facts.indirection.find(Puppet[:node_name_value])
    raise _("Could not find facts for %{node}") % { node: Puppet[:node_name_value] } unless facts

    Puppet[:node_name_value] = facts.values[Puppet[:node_name_fact]]
    facts.name = Puppet[:node_name_value]
  end

  # Find the Node
  node = Puppet::Node.indirection.find(Puppet[:node_name_value])
  raise _("Could not find node %{node}") % { node: Puppet[:node_name_value] } unless node

  configured_environment = node.environment || Puppet.lookup(:current_environment)

  apply_environment = manifest ?
    configured_environment.override_with(:manifest => manifest) :
    configured_environment

  # Modify the node descriptor to use the special apply_environment.
  # It is based on the actual environment from the node, or the locally
  # configured environment if the node does not specify one.
  # If a manifest file is passed on the command line, it overrides
  # the :manifest setting of the apply_environment.
  node.environment = apply_environment

  # TRANSLATION, the string "For puppet script" is not user facing
  Puppet.override({:current_environment => apply_environment}, "For puppet script") do
    # Merge in the facts.
    node.merge(facts.values) if facts

    # Add server facts so $server_facts[environment] exists when doing a puppet script
    # SCRIPT TODO: May be needed when running scripts under orchestrator. Leave it for now.
    #
    node.add_server_facts({})

    begin
      # Compile the catalog

      # When compiling, the compiler traps and logs certain errors
      # Those that do not lead to an immediate exit are caught by the general
      # rule and gets logged.
      #
      begin
        # support the following features when evaluating puppet code
        # * $facts with facts from host running the script
        # * $settings with 'settings::*' namespace populated, and '$settings::all_local' hash
        # * $trusted as setup when using puppet apply
        # * an environment
        #

        # fixup trusted information
        node.sanitize()

        compiler = Puppet::Parser::ScriptCompiler.new(node.environment, node.name)
        topscope = compiler.topscope

        # When scripting the trusted data are always local, but set them anyway
        topscope.set_trusted(node.trusted_data)

        # Server facts are always about the local node's version etc.
        topscope.set_server_facts(node.server_facts)

        # Set $facts for the node running the script
        facts_hash = node.facts.nil? ? {} : node.facts.values
        topscope.set_facts(facts_hash)

        # create the $settings:: variables
        topscope.merge_settings(node.environment.name, false)

        compiler.compile()

      rescue Puppet::Error
        # already logged and handled by the compiler, including Puppet::ParseErrorWithIssue
        exit(1)
      end

      exit(0)
    rescue => detail
      Puppet.log_exception(detail)
      exit(1)
    end
  end

ensure
  if @profiler
    Puppet::Util::Profiler.remove_profiler(@profiler)
    @profiler.shutdown
  end
end