From 2a1ee858bd257cb6c3fc33c82a5a3d656b144e14 Mon Sep 17 00:00:00 2001 From: Conor Horan-Kates Date: Thu, 29 Aug 2013 15:41:04 -0700 Subject: [PATCH] more work on #45, need to get the pson conversion down --- lib/rouster.rb | 1 + lib/rouster/puppet.rb | 16 +++++++++------- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/rouster.rb b/lib/rouster.rb index 7d10e3c..47b053a 100644 --- a/lib/rouster.rb +++ b/lib/rouster.rb @@ -18,6 +18,7 @@ class Rouster # custom exceptions -- what else do we want them to include/do? class FileTransferError < StandardError; end # thrown by get() and put() class InternalError < StandardError; end # thrown by most (if not all) Rouster methods + class ExternalError < StandardError; end # thrown when external dependencies do not respond as expected class LocalExecutionError < StandardError; end # thrown by _run() class RemoteExecutionError < StandardError; end # thrown by run() class SSHConnectionError < StandardError; end # thrown by available_via_ssh() -- and potentially _run() diff --git a/lib/rouster/puppet.rb b/lib/rouster/puppet.rb index 8c51689..9e3240f 100644 --- a/lib/rouster/puppet.rb +++ b/lib/rouster/puppet.rb @@ -3,6 +3,7 @@ require 'json' require 'net/https' require 'socket' +require 'uri' class Rouster @@ -26,22 +27,23 @@ def facter(cache=true, custom_facts=true) res end - def get_catalog(hostname=nil, puppetmaster=nil, facts=nil) + def get_catalog(hostname=nil, puppetmaster=nil, facts=nil, puppetmaster_port=8140) # post https:///catalog/?facts_format=pson&facts= == ht to patrick@puppetlabs certname = hostname.nil? ? self.run('hostname --fqdn').chomp : hostname puppetmaster = puppetmaster.nil? ? 'puppet' : puppetmaster facts = facts.nil? ? self.facter() : facts # TODO check for presence of certain 'required' facts? + facts.to_pson # this does not work, but needs to json = nil - url = sprintf('https://%s/catalog/%s?facts_format=pson&facts=%s', puppetmaster, certname, facts) - - res = self.run(sprintf('puppet catalog find %s', certname)) + url = sprintf('https://%s:%s/catalog/%s?facts_format=pson&facts=%s', puppetmaster, puppetmaster_port, certname, facts) + uri = URI.parse(url) begin - json = JSON.parse(res) - rescue - raise InternalError.new(sprintf('unable to parse[%s] as JSON', res)) + res = Net::HTTP.get(uri) + json = res.to_json + rescue => e + raise ExternalError.new("calling[#{url}] led to exception[#{e}") end json