Module: PuppetX::Bodeco::Util

Defined in:
lib/puppet_x/bodeco/util.rb

Class Method Summary collapse

Class Method Details

.content(url, options = {}) ⇒ Object



12
13
14
15
16
# File 'lib/puppet_x/bodeco/util.rb', line 12

def self.content(url, options = {})
  uri = URI(url)
  @connection = PuppetX::Bodeco.const_get(uri.scheme.upcase).new("#{uri.scheme}://#{uri.host}:#{uri.port}", options)
  @connection.content(uri)
end

.download(url, filepath, options = {}) ⇒ Object



6
7
8
9
10
# File 'lib/puppet_x/bodeco/util.rb', line 6

def self.download(url, filepath, options = {})
  uri = URI(url)
  @connection = PuppetX::Bodeco.const_get(uri.scheme.upcase).new("#{uri.scheme}://#{uri.host}:#{uri.port}", options)
  @connection.download(uri, filepath)
end

.load_file_with_any_terminus(url) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/puppet_x/bodeco/util.rb', line 40

def self.load_file_with_any_terminus(url)
  termini_to_try = %i[file_server rest]
  termini_to_try.each do |terminus|
    with_terminus(terminus) do
      begin
        content = Puppet::FileServing::Content.indirection.find(url)
      rescue SocketError, Timeout::Error, Errno::ECONNREFUSED, Errno::EHOSTDOWN, Errno::EHOSTUNREACH, Errno::ETIMEDOUT, Puppet::HTTP::RouteError
        # rescue any network error
      end
      return content if content
    end
  end
  nil
end

.puppet_download(url, filepath) ⇒ Object

This allows you to use a puppet syntax for a file and return its content.

Examples:

puppet_download 'puppet:///modules/my_module_name/my_file.dat

Parameters:

  • url (String)

    this is the puppet url of the file to be fetched

  • filepath (String)

    this is path of the file to create

Raises:

  • (ArgumentError)

    when the file doesn’t exist



29
30
31
32
33
34
35
36
37
# File 'lib/puppet_x/bodeco/util.rb', line 29

def self.puppet_download(url, filepath)
  # Somehow there is no consistent way to determine what terminus to use. So we switch to a
  # trial and error method. First we start withe the default. And if it doesn't work, we try the
  # other ones
  status = load_file_with_any_terminus(url)
  raise ArgumentError, "Previous error(s) resulted in Puppet being unable to retrieve information from environment #{Puppet['environment']} source(s) #{url}'\nMost probable cause is file not found." unless status

  File.binwrite(filepath, status.content)
end

.with_terminus(terminus) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/puppet_x/bodeco/util.rb', line 55

def self.with_terminus(terminus)
  old_terminus = Puppet[:default_file_terminus]
  Puppet[:default_file_terminus] = terminus
  value = yield
  Puppet[:default_file_terminus] = old_terminus
  value
end