Class: Puppet::HTTP::Service::FileServer

Inherits:
Puppet::HTTP::Service show all
Defined in:
lib/puppet/http/service/file_server.rb

Overview

The FileServer service is used to retrieve file metadata and content.

Constant Summary collapse

API =

Returns Default API for the FileServer service.

Returns:

  • (String)

    Default API for the FileServer service

'/puppet/v3'.freeze
PATH_REGEX =

Returns RegEx used to determine if a path contains a leading slash.

Returns:

  • (RegEx)

    RegEx used to determine if a path contains a leading slash

/^\//

Constants inherited from Puppet::HTTP::Service

EXCLUDED_FORMATS, SERVICE_NAMES

Instance Attribute Summary

Attributes inherited from Puppet::HTTP::Service

#url

Instance Method Summary collapse

Methods inherited from Puppet::HTTP::Service

#connect, create_service, valid_name?, #with_base_url

Constructor Details

#initialize(client, session, server, port) ⇒ FileServer

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

Parameters:

  • client (Puppet::HTTP::Client)
  • session (Puppet::HTTP::Session)
  • server (String)

    (`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.

  • port (Integer)

    (`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.


25
26
27
28
# File 'lib/puppet/http/service/file_server.rb', line 25

def initialize(client, session, server, port)
  url = build_url(API, server || Puppet[:server], port || Puppet[:serverport])
  super(client, session, url)
end

Instance Method Details

#get_file_content(path:, environment:) {|Sting| ... } ⇒ Puppet::HTTP::Response

Submit a GET request to the server to retrieve content of a file.

Parameters:

  • path (String)

    path to the file to retrieve data from

  • environment (String)

    the name of the environment we are operating in

Yields:

  • (Sting)

    Yields the body of the response returned from the server

Returns:


137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/puppet/http/service/file_server.rb', line 137

def get_file_content(path:, environment:, &block)
  validate_path(path)

  headers = add_puppet_headers('Accept' => 'application/octet-stream')
  response = @client.get(
    with_base_url("/file_content#{path}"),
    headers: headers,
    params: {
      environment: environment
    }
  ) do |res|
    if res.success?
      res.read_body(&block)
    end
  end

  process_response(response)

  response
end

#get_file_metadata(path:, environment:, links: :manage, checksum_type: Puppet[:digest_algorithm], source_permissions: :ignore) ⇒ Array<Puppet::HTTP::Response, Puppet::FileServing::Metadata>

Submit a GET request to the server to retrieve the metadata for a specified file.

Parameters:

  • path (String)

    path to the file to retrieve data from

  • environment (String)

    the name of the environment we are operating in

  • links (Symbol) (defaults to: :manage)

    Can be one of either `:follow` or `:manage`, defines how links are handled.

  • checksum_type (String) (defaults to: Puppet[:digest_algorithm])

    The digest algorithm used to verify the file. Defaults to `sha256`.

  • source_permissions (Symbol) (defaults to: :ignore)

    Can be one of `:use`, `:use_when_creating`, or `:ignore`. This parameter tells the server if it should include the file permissions in the response. If set to `:ignore`, the server will return default permissions.

Returns:


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/puppet/http/service/file_server.rb', line 49

def (path:, environment:, links: :manage, checksum_type: Puppet[:digest_algorithm], source_permissions: :ignore)
  validate_path(path)

  headers = add_puppet_headers('Accept' => get_mime_types(Puppet::FileServing::Metadata).join(', '))

  response = @client.get(
    with_base_url("/file_metadata#{path}"),
    headers: headers,
    params: {
      links: links,
      checksum_type: checksum_type,
      source_permissions: source_permissions,
      environment: environment
    }
  )

  process_response(response)

  [response, deserialize(response, Puppet::FileServing::Metadata)]
end

#get_file_metadatas(path: nil, environment:, recurse: :false, recurselimit: nil, max_files: nil, ignore: nil, links: :manage, checksum_type: Puppet[:digest_algorithm], source_permissions: :ignore) ⇒ Array<Puppet::HTTP::Response, Array<Puppet::FileServing::Metadata>>

Submit a GET request to the server to retrieve the metadata for multiple files

Parameters:

  • path (String) (defaults to: nil)

    path to the file(s) to retrieve data from

  • environment (String)

    the name of the environment we are operating in

  • recurse (Symbol) (defaults to: :false)

    Can be `:true`, `:false`, or `:remote`. Defines if we recursively return the contents of the directory. Used in conjunction with `:recurselimit`. See the reference documentation for the file type for more details.

  • recurselimit (Integer) (defaults to: nil)

    When `recurse` is set, `recurselimit` defines how far Puppet should descend into subdirectories. `0` is effectively the same as `recurse => false`, `1` will return files and directories directly inside the defined directory, `2` will return the direct content of the directory as well as the contents of the first level of subdirectories. The pattern continues for each incremental value. See the reference documentation for the file type for more details.

  • ignore (Array<String>) (defaults to: nil)

    An optional array of files to ignore, ie `['CVS', '.git', '.hg']`

  • links (Symbol) (defaults to: :manage)

    Can be one of either `:follow` or `:manage`, defines how links are handled.

  • checksum_type (String) (defaults to: Puppet[:digest_algorithm])

    The digest algorithm used to verify the file. Currently if fips is enabled, this defaults to `sha256`. Otherwise, it's `md5`.

  • source_permissions (Symbol) (defaults to: :ignore)

    Can be one of `:use`, `:use_when_creating`, or `:ignore`. This parameter tells the server if it should include the file permissions in the report. If set to `:ignore`, the server will return default permissions.

Returns:


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/puppet/http/service/file_server.rb', line 101

def get_file_metadatas(path: nil, environment:, recurse: :false, recurselimit: nil, max_files: nil, ignore: nil, links: :manage, checksum_type: Puppet[:digest_algorithm], source_permissions: :ignore)
  validate_path(path)

  headers = add_puppet_headers('Accept' => get_mime_types(Puppet::FileServing::Metadata).join(', '))

  response = @client.get(
    with_base_url("/file_metadatas#{path}"),
    headers: headers,
    params: {
      recurse: recurse,
      recurselimit: recurselimit,
      max_files: max_files,
      ignore: ignore,
      links: links,
      checksum_type: checksum_type,
      source_permissions: source_permissions,
      environment: environment,
    }
  )

  process_response(response)

  [response, deserialize_multiple(response, Puppet::FileServing::Metadata)]
end

#get_static_file_content(path:, environment:, code_id:) {|String| ... } ⇒ Puppet::HTTP::Response

Submit a GET request to retrieve file content using the `static_file_content` API uniquely identified by (`code_id`, `environment`, `path`).

Parameters:

  • path (String)

    path to the file to retrieve data from

  • environment (String)

    the name of the environment we are operating in

  • code_id (String)

    Defines the version of the resource to return

Yields:

  • (String)

    Yields the body of the response returned

Returns:


171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/puppet/http/service/file_server.rb', line 171

def get_static_file_content(path:, environment:, code_id:, &block)
  validate_path(path)

  headers = add_puppet_headers('Accept' => 'application/octet-stream')
  response = @client.get(
    with_base_url("/static_file_content#{path}"),
    headers: headers,
    params: {
      environment: environment,
      code_id: code_id,
    }
  ) do |res|
    if res.success?
      res.read_body(&block)
    end
  end

  process_response(response)

  response
end