summaryrefslogtreecommitdiff
path: root/lib/chef
diff options
context:
space:
mode:
authordanielsdeleo <dan@opscode.com>2013-10-14 09:50:18 -0700
committerdanielsdeleo <dan@opscode.com>2013-10-16 13:53:37 -0700
commit939e4fe1f8d27dba77ef8208ad792860d9766b9e (patch)
tree113aef75aecc539af9078d7468ccb80d1a7ab574 /lib/chef
parentb14c9f6e9892ab2398de4035b8d6840d3da375e3 (diff)
downloadchef-939e4fe1f8d27dba77ef8208ad792860d9766b9e.tar.gz
Move json attribs fetching to a class
* Extract duplicated code for fetching/reading JSON attributes to a shared class * Use HTTP::Simple instead of Chef::REST
Diffstat (limited to 'lib/chef')
-rw-r--r--lib/chef/application/client.rb28
-rw-r--r--lib/chef/application/solo.rb29
-rw-r--r--lib/chef/config_fetcher.rb51
-rw-r--r--lib/chef/shell.rb22
4 files changed, 60 insertions, 70 deletions
diff --git a/lib/chef/application/client.rb b/lib/chef/application/client.rb
index 0964101fad..a04b4f1cf6 100644
--- a/lib/chef/application/client.rb
+++ b/lib/chef/application/client.rb
@@ -22,7 +22,7 @@ require 'chef/client'
require 'chef/config'
require 'chef/daemon'
require 'chef/log'
-require 'chef/rest'
+require 'chef/config_fetcher'
require 'chef/handler/error_report'
@@ -248,30 +248,8 @@ class Chef::Application::Client < Chef::Application
end
if Chef::Config[:json_attribs]
- begin
- json_io = case Chef::Config[:json_attribs]
- when /^(http|https):\/\//
- @rest = Chef::REST.new(Chef::Config[:json_attribs], nil, nil)
- @rest.get_rest(Chef::Config[:json_attribs], true).open
- else
- open(Chef::Config[:json_attribs])
- end
- rescue SocketError => error
- Chef::Application.fatal!("I cannot connect to #{Chef::Config[:json_attribs]}", 2)
- rescue Errno::ENOENT => error
- Chef::Application.fatal!("I cannot find #{Chef::Config[:json_attribs]}", 2)
- rescue Errno::EACCES => error
- Chef::Application.fatal!("Permissions are incorrect on #{Chef::Config[:json_attribs]}. Please chmod a+r #{Chef::Config[:json_attribs]}", 2)
- rescue Exception => error
- Chef::Application.fatal!("Got an unexpected error reading #{Chef::Config[:json_attribs]}: #{error.message}", 2)
- end
-
- begin
- @chef_client_json = Chef::JSONCompat.from_json(json_io.read)
- json_io.close unless json_io.closed?
- rescue JSON::ParserError => error
- Chef::Application.fatal!("Could not parse the provided JSON file (#{Chef::Config[:json_attribs]})!: " + error.message, 2)
- end
+ config_fetcher = Chef::ConfigFetcher.new(Chef::Config[:json_attribs])
+ @chef_client_json = config_fetcher.fetch_json
end
end
diff --git a/lib/chef/application/solo.rb b/lib/chef/application/solo.rb
index 960c917c66..ba563ce3a8 100644
--- a/lib/chef/application/solo.rb
+++ b/lib/chef/application/solo.rb
@@ -23,7 +23,7 @@ require 'chef/config'
require 'chef/daemon'
require 'chef/log'
require 'chef/rest'
-require 'open-uri'
+require 'chef/config_fetcher'
require 'fileutils'
class Chef::Application::Solo < Chef::Application
@@ -181,36 +181,13 @@ class Chef::Application::Solo < Chef::Application
end
if Chef::Config[:json_attribs]
- begin
- json_io = case Chef::Config[:json_attribs]
- when /^(http|https):\/\//
- @rest = Chef::REST.new(Chef::Config[:json_attribs], nil, nil)
- @rest.get_rest(Chef::Config[:json_attribs], true).open
- else
- open(Chef::Config[:json_attribs])
- end
- rescue SocketError => error
- Chef::Application.fatal!("I cannot connect to #{Chef::Config[:json_attribs]}", 2)
- rescue Errno::ENOENT => error
- Chef::Application.fatal!("I cannot find #{Chef::Config[:json_attribs]}", 2)
- rescue Errno::EACCES => error
- Chef::Application.fatal!("Permissions are incorrect on #{Chef::Config[:json_attribs]}. Please chmod a+r #{Chef::Config[:json_attribs]}", 2)
- rescue Exception => error
- Chef::Application.fatal!("Got an unexpected error reading #{Chef::Config[:json_attribs]}: #{error.message}", 2)
- end
-
- begin
- @chef_client_json = Chef::JSONCompat.from_json(json_io.read)
- json_io.close unless json_io.closed?
- rescue JSON::ParserError => error
- Chef::Application.fatal!("Could not parse the provided JSON file (#{Chef::Config[:json_attribs]})!: " + error.message, 2)
- end
+ config_fetcher = Chef::ConfigFetcher.new(Chef::Config[:json_attribs])
+ @chef_solo_json = config_fetcher.fetch_json
end
if Chef::Config[:recipe_url]
cookbooks_path = Array(Chef::Config[:cookbook_path]).detect{|e| e =~ /\/cookbooks\/*$/ }
recipes_path = File.expand_path(File.join(cookbooks_path, '..'))
- target_file = File.join(recipes_path, 'recipes.tgz')
Chef::Log.debug "Creating path #{recipes_path} to extract recipes into"
FileUtils.mkdir_p recipes_path
diff --git a/lib/chef/config_fetcher.rb b/lib/chef/config_fetcher.rb
new file mode 100644
index 0000000000..369d36c459
--- /dev/null
+++ b/lib/chef/config_fetcher.rb
@@ -0,0 +1,51 @@
+require 'chef/http/simple'
+class Chef
+ class ConfigFetcher
+
+ attr_reader :config_location
+
+ def initialize(config_location)
+ @config_location = config_location
+ end
+
+ def fetch_json
+ config_data = read_config
+ begin
+ Chef::JSONCompat.from_json(config_data)
+ rescue JSON::ParserError => error
+ Chef::Application.fatal!("Could not parse the provided JSON file (#{config_location})!: " + error.message, 2)
+ end
+ end
+
+ def read_config
+ if remote_config?
+ fetch_remote_config
+ else
+ read_local_config
+ end
+ end
+
+ def fetch_remote_config
+ http = Chef::HTTP::Simple.new(config_location)
+ http.get("")
+ rescue SocketError, SystemCallError => error
+ Chef::Application.fatal!("Cannot fetch config '#{config_location}': '#{error.class}: #{error.message}", 2)
+ end
+
+ def read_local_config
+ ::File.read(config_location)
+ rescue Errno::ENOENT => error
+ Chef::Application.fatal!("I cannot find #{config_location}", 2)
+ rescue Errno::EACCES => error
+ Chef::Application.fatal!("Permissions are incorrect on #{config_location}. Please chmod a+r #{config_location}", 2)
+ end
+
+ def http
+ Chef::HTTP::Simple.new(config_location)
+ end
+
+ def remote_config?
+ !!(config_location =~ %r{^(http|https)://})
+ end
+ end
+end
diff --git a/lib/chef/shell.rb b/lib/chef/shell.rb
index 4c86f96616..0788962e62 100644
--- a/lib/chef/shell.rb
+++ b/lib/chef/shell.rb
@@ -24,6 +24,7 @@ require 'chef'
require 'chef/version'
require 'chef/client'
require 'chef/config'
+require 'chef/config_fetcher'
require 'chef/shell/shell_session'
require 'chef/shell/ext'
@@ -151,26 +152,9 @@ module Shell
end
def self.parse_json
- # HACK: copied verbatim from chef/application/client, because it's not
- # reusable as written there :(
if Chef::Config[:json_attribs]
- begin
- json_io = open(Chef::Config[:json_attribs])
- rescue SocketError => error
- fatal!("I cannot connect to #{Chef::Config[:json_attribs]}", 2)
- rescue Errno::ENOENT => error
- fatal!("I cannot find #{Chef::Config[:json_attribs]}", 2)
- rescue Errno::EACCES => error
- fatal!("Permissions are incorrect on #{Chef::Config[:json_attribs]}. Please chmod a+r #{Chef::Config[:json_attribs]}", 2)
- rescue Exception => error
- fatal!("Got an unexpected error reading #{Chef::Config[:json_attribs]}: #{error.message}", 2)
- end
-
- begin
- @json_attribs = Chef::JSONCompat.from_json(json_io.read)
- rescue JSON::ParserError => error
- fatal!("Could not parse the provided JSON file (#{Chef::Config[:json_attribs]})!: " + error.message, 2)
- end
+ config_fetcher = Chef::ConfigFetcher.new(Chef::Config[:json_attribs])
+ @json_attribs = config_fetcher.fetch_json
end
end