Module: PuppetX::Bodeco::Util
- Defined in:
- lib/puppet_x/bodeco/util.rb
Class Method Summary collapse
- .content(url, options = {}) ⇒ Object
- .download(url, filepath, options = {}) ⇒ Object
-
.load_file_with_any_terminus(url) ⇒ Object
rubocop:disable HandleExceptions.
-
.puppet_download(url, filepath) ⇒ Object
This allows you to use a puppet syntax for a file and return its content.
-
.with_terminus(terminus) ⇒ Object
rubocop:enable HandleExceptions.
Class Method Details
.content(url, options = {}) ⇒ Object
10 11 12 13 14 |
# File 'lib/puppet_x/bodeco/util.rb', line 10 def self.content(url, = {}) uri = URI(url) @connection = PuppetX::Bodeco.const_get(uri.scheme.upcase).new("#{uri.scheme}://#{uri.host}:#{uri.port}", ) @connection.content(uri) end |
.download(url, filepath, options = {}) ⇒ Object
4 5 6 7 8 |
# File 'lib/puppet_x/bodeco/util.rb', line 4 def self.download(url, filepath, = {}) uri = URI(url) @connection = PuppetX::Bodeco.const_get(uri.scheme.upcase).new("#{uri.scheme}://#{uri.host}:#{uri.port}", ) @connection.download(uri, filepath) end |
.load_file_with_any_terminus(url) ⇒ Object
rubocop:disable HandleExceptions
38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/puppet_x/bodeco/util.rb', line 38 def self.load_file_with_any_terminus(url) termini_to_try = [: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.
27 28 29 30 31 32 33 34 |
# File 'lib/puppet_x/bodeco/util.rb', line 27 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.open(filepath, 'wb') { |file| file.write(status.content) } end |
.with_terminus(terminus) ⇒ Object
rubocop:enable HandleExceptions
54 55 56 57 58 59 60 |
# File 'lib/puppet_x/bodeco/util.rb', line 54 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 |