class Puppet::HTTP::Service::Compiler

The Compiler service is used to submit and retrieve data from the puppetserver.

@api public

Constants

API

@return [String] Default API for the Compiler service

Public Class Methods

new(client, session, server, port) click to toggle source

Use `Puppet::HTTP::Session.route_to(:puppet)` to create or get an instance of this class.

@param [Puppet::HTTP::Client] client @param [Puppet::HTTP::Session] session @param [String] server (`Puppet`) If an explicit server is given,

create a service using that server. If server is nil, the default value
is used to create the service.

@param [Integer] port (`Puppet`) If an explicit port is given, create

a service using that port. If port is nil, the default value is used to
create the service.
Calls superclass method Puppet::HTTP::Service::new
   # File lib/puppet/http/service/compiler.rb
21 def initialize(client, session, server, port)
22   url = build_url(API, server || Puppet[:server], port || Puppet[:serverport])
23   super(client, session, url)
24 end

Public Instance Methods

get_facts(name, environment:) click to toggle source

@api private

Submit a GET request to retrieve the facts for the named node

@param [String] name Name of the node to retrieve facts for @param [String] environment Name of the environment we are operating in

@return [Array<Puppet::HTTP::Response, Puppet::Node::Facts>] An array

containing the request response and the deserialized facts for the
specified node

@api public

    # File lib/puppet/http/service/compiler.rb
210 def get_facts(name, environment:)
211   headers = add_puppet_headers('Accept' => get_mime_types(Puppet::Node::Facts).join(', '))
212 
213   response = @client.get(
214     with_base_url("/facts/#{name}"),
215     headers: headers,
216     params: { environment: environment }
217   )
218 
219   process_response(response)
220 
221   [response, deserialize(response, Puppet::Node::Facts)]
222 end
get_filebucket_file(path, environment:, bucket_path: nil, diff_with: nil, list_all: nil, fromdate: nil, todate: nil) click to toggle source

Submit a GET request to retrieve a file stored with filebucket.

@param [String] path The request path, formatted by `Puppet::FileBucket::Dipper` @param [String] environment Name of the environment we are operating in.

This should not impact filebucket at all, but is included to be consistent
with legacy code.

@param [String] bucket_path @param [String] diff_with a checksum to diff against if we are comparing

files that are both stored in the bucket

@param [String] list_all @param [String] fromdate @param [String] todate

@return [Array<Puppet::HTTP::Response, Puppet::FileBucket::File>] An array

containing the request response and the deserialized file returned from
the server.

@api public

    # File lib/puppet/http/service/compiler.rb
271 def get_filebucket_file(path, environment:, bucket_path: nil, diff_with: nil, list_all: nil, fromdate: nil, todate: nil)
272   headers = add_puppet_headers('Accept' => 'application/octet-stream')
273 
274   response = @client.get(
275     with_base_url("/file_bucket_file/#{path}"),
276     headers: headers,
277     params: {
278       environment: environment,
279       bucket_path: bucket_path,
280       diff_with: diff_with,
281       list_all: list_all,
282       fromdate: fromdate,
283       todate: todate
284     }
285   )
286 
287   process_response(response)
288 
289   [response, deserialize(response, Puppet::FileBucket::File)]
290 end
get_node(name, environment:, configured_environment: nil, transaction_uuid: nil) click to toggle source

Submit a GET request to retrieve a node from the server.

@param [String] name The name of the node being requested @param [String] environment The name of the environment we are operating in @param [String] configured_environment Optional, the name of the configured

environment. If unset, `environment` is used.

@param [String] transaction_uuid An agent generated transaction uuid, used

for connecting catalogs and reports.

@return [Array<Puppet::HTTP::Response, Puppet::Node>] An array containing

the request response and the deserialized requested node

@api public

   # File lib/puppet/http/service/compiler.rb
39 def get_node(name, environment:, configured_environment: nil, transaction_uuid: nil)
40   headers = add_puppet_headers('Accept' => get_mime_types(Puppet::Node).join(', '))
41 
42   response = @client.get(
43     with_base_url("/node/#{name}"),
44     headers: headers,
45     params: {
46       environment: environment,
47       configured_environment: configured_environment || environment,
48       transaction_uuid: transaction_uuid,
49     }
50   )
51 
52   process_response(response)
53 
54   [response, deserialize(response, Puppet::Node)]
55 end
head_filebucket_file(path, environment:, bucket_path: nil) click to toggle source

Submit a HEAD request to check the status of a file stored with filebucket.

@param [String] path The request path, formatted by `Puppet::FileBucket::Dipper` @param [String] environment Name of the environment we are operating in.

This should not impact filebucket at all, but is included to be consistent
with legacy code.

@param [String] bucket_path

@return [Puppet::HTTP::Response] The request response

@api public

    # File lib/puppet/http/service/compiler.rb
334 def head_filebucket_file(path, environment:, bucket_path: nil)
335   headers = add_puppet_headers('Accept' => 'application/octet-stream')
336 
337   response = @client.head(
338     with_base_url("/file_bucket_file/#{path}"),
339     headers: headers,
340     params: {
341       environment: environment,
342       bucket_path: bucket_path
343     }
344   )
345 
346   process_response(response)
347 
348   response
349 end
post_catalog(name, facts:, environment:, configured_environment: nil, check_environment: false, transaction_uuid: nil, job_uuid: nil, static_catalog: true, checksum_type: Puppet[:supported_checksum_types]) click to toggle source

Submit a POST request to submit a catalog to the server.

@param [String] name The name of the catalog to be submitted @param [Puppet::Node::Facts] facts Facts for this catalog @param [String] environment The name of the environment we are operating in @param [String] configured_environment Optional, the name of the configured

environment. If unset, `environment` is used.

@param [Boolean] check_environment If true, request that the server check if

our `environment` matches the server-specified environment. If they do not
match, then the server may return an empty catalog in the server-specified
environment.

@param [String] transaction_uuid An agent generated transaction uuid, used

for connecting catalogs and reports.

@param [String] job_uuid A unique job identifier defined when the orchestrator

starts a puppet run via pxp-agent. This is used to correlate catalogs and
reports with the orchestrator job.

@param [Boolean] static_catalog Indicates if the file metadata(s) are inlined

in the catalog. This informs the agent if it needs to make a second request
to retrieve metadata in addition to the initial catalog request.

@param [Array<String>] checksum_type An array of accepted checksum types.

@return [Array<Puppet::HTTP::Response, Puppet::Resource::Catalog>] An array

containing the request response and the deserialized catalog returned by
the server

@api public

    # File lib/puppet/http/service/compiler.rb
 83 def post_catalog(name, facts:, environment:, configured_environment: nil, check_environment: false, transaction_uuid: nil, job_uuid: nil, static_catalog: true, checksum_type: Puppet[:supported_checksum_types])
 84   if Puppet[:preferred_serialization_format] == "pson"
 85     formatter = Puppet::Network::FormatHandler.format_for(:pson)
 86     # must use 'pson' instead of 'text/pson'
 87     facts_format = 'pson'
 88   else
 89     formatter = Puppet::Network::FormatHandler.format_for(:json)
 90     facts_format = formatter.mime
 91   end
 92 
 93   facts_as_string = serialize(formatter, facts)
 94 
 95   # query parameters are sent in the POST request body
 96   body = {
 97     facts_format: facts_format,
 98     facts: Puppet::Util.uri_query_encode(facts_as_string),
 99     environment: environment,
100     configured_environment: configured_environment || environment,
101     check_environment: !!check_environment,
102     transaction_uuid: transaction_uuid,
103     job_uuid: job_uuid,
104     static_catalog: static_catalog,
105     checksum_type: checksum_type.join('.')
106   }.map do |key, value|
107     "#{key}=#{Puppet::Util.uri_query_encode(value.to_s)}"
108   end.join("&")
109 
110   headers = add_puppet_headers(
111     'Accept' => get_mime_types(Puppet::Resource::Catalog).join(', '),
112     'Content-Type' => 'application/x-www-form-urlencoded'
113   )
114 
115   response = @client.post(
116     with_base_url("/catalog/#{name}"),
117     body,
118     headers: headers,
119     # for legacy reasons we always send environment as a query parameter too
120     params: { environment: environment },
121   )
122 
123   process_response(response)
124 
125   [response, deserialize(response, Puppet::Resource::Catalog)]
126 end
post_catalog4(certname, persistence:, environment:, facts: nil, trusted_facts: nil, transaction_uuid: nil, job_id: nil, options: nil) click to toggle source

@api private

Submit a POST request to request a catalog to the server using v4 endpoint

@param [String] certname The name of the node for which to compile the catalog. @param [Hash] persistent A hash containing two required keys, facts and catalog,

which when set to true will cause the facts and reports to be stored in
PuppetDB, or discarded if set to false.

@param [String] environment The name of the environment for which to compile the catalog. @param [Hash] facts A hash with a required values key, containing a hash of all the

facts for the node. If not provided, Puppet will attempt to fetch facts for the node
from PuppetDB.

@param [Hash] trusted_facts A hash with a required values key containing a hash of

the trusted facts for a node

@param [String] transaction_uuid The id for tracking the catalog compilation and

report submission.

@param [String] job_id The id of the orchestrator job that triggered this run. @param [Hash] options A hash of options beyond direct input to catalogs. Options:

- prefer_requested_environment Whether to always override a node's classified 
  environment with the one supplied in the request. If this is true and no environment
  is supplied, fall back to the classified environment, or finally, 'production'.
- capture_logs Whether to return the errors and warnings that occurred during
  compilation alongside the catalog in the response body.
- log_level The logging level to use during the compile when capture_logs is true.
  Options are 'err', 'warning', 'info', and 'debug'.

@return [Array<Puppet::HTTP::Response, Puppet::Resource::Catalog, Array<String>>] An array

containing the request response, the deserialized catalog returned by
the server and array containing logs (log array will be empty if capture_logs is false)
    # File lib/puppet/http/service/compiler.rb
159 def post_catalog4(certname, persistence:, environment:, facts: nil, trusted_facts: nil, transaction_uuid: nil, job_id: nil, options: nil)
160   unless persistence.is_a?(Hash) && (missing = [:facts, :catalog] - persistence.keys.map(&:to_sym)).empty?
161     raise ArgumentError.new("The 'persistence' hash is missing the keys: #{missing.join(', ')}")
162   end
163   raise ArgumentError.new("Facts must be a Hash not a #{facts.class}") unless facts.nil? || facts.is_a?(Hash)
164   body = {
165     certname: certname,
166     persistence: persistence,
167     environment: environment,
168     transaction_uuid: transaction_uuid,
169     job_id: job_id,
170     options: options
171   }
172   body[:facts] = { values: facts } unless facts.nil?
173   body[:trusted_facts] = { values: trusted_facts } unless trusted_facts.nil?
174   headers = add_puppet_headers(
175     'Accept' => get_mime_types(Puppet::Resource::Catalog).join(', '),
176     'Content-Type' => 'application/json'
177   )
178 
179   url = URI::HTTPS.build(host: @url.host, port: @url.port, path: Puppet::Util.uri_encode("/puppet/v4/catalog"))
180   response = @client.post(
181     url,
182     body.to_json,
183     headers: headers
184   )
185   process_response(response)
186   begin
187     response_body = JSON.parse(response.body)
188     catalog = Puppet::Resource::Catalog.from_data_hash(response_body['catalog'])
189   rescue => err
190     raise Puppet::HTTP::SerializationError.new("Failed to deserialize catalog from puppetserver response: #{err.message}", err)
191   end
192   
193   logs = response_body['logs'] || []
194   [response, catalog, logs]
195 end
put_facts(name, environment:, facts:) click to toggle source

Submits a PUT request to submit facts for the node to the server.

@param [String] name Name of the node we are submitting facts for @param [String] environment Name of the environment we are operating in @param [Puppet::Node::Facts] facts Facts for the named node

@return [Puppet::HTTP::Response] The request response

@api public

    # File lib/puppet/http/service/compiler.rb
233 def put_facts(name, environment:, facts:)
234   formatter = Puppet::Network::FormatHandler.format_for(Puppet[:preferred_serialization_format])
235 
236   headers = add_puppet_headers(
237     'Accept' => get_mime_types(Puppet::Node::Facts).join(', '),
238     'Content-Type' => formatter.mime
239   )
240 
241   response = @client.put(
242     with_base_url("/facts/#{name}"),
243     serialize(formatter, facts),
244     headers: headers,
245     params: { environment: environment },
246   )
247 
248   process_response(response)
249 
250   response
251 end
put_filebucket_file(path, body:, environment:) click to toggle source

Submit a PUT request to store a file with filebucket.

@param [String] path The request path, formatted by `Puppet::FileBucket::Dipper` @param [String] body The contents of the file to be backed @param [String] environment Name of the environment we are operating in.

This should not impact filebucket at all, but is included to be consistent
with legacy code.

@return [Puppet::HTTP::Response] The response request

@api public

    # File lib/puppet/http/service/compiler.rb
303 def put_filebucket_file(path, body:, environment:)
304   headers = add_puppet_headers({
305     'Accept' => 'application/octet-stream',
306     'Content-Type' => 'application/octet-stream'
307     })
308 
309   response = @client.put(
310     with_base_url("/file_bucket_file/#{path}"),
311     body,
312     headers: headers,
313     params: {
314       environment: environment
315     }
316   )
317 
318   process_response(response)
319 
320   response
321 end