diff --git a/lib/puppet/module_tool/cache.rb b/lib/puppet/module_tool/cache.rb index 5b82b7632..b25478096 100644 --- a/lib/puppet/module_tool/cache.rb +++ b/lib/puppet/module_tool/cache.rb @@ -1,55 +1,56 @@ +require 'uri' + module Puppet::Module::Tool # = Cache # # Provides methods for reading files from local cache, filesystem or network. class Cache - include Puppet::Module::Tool::Utils::URI # Instantiate new cahe for the +repositry+ instance. def initialize(repository, options = {}) @repository = repository @options = options end # Return filename retrieved from +uri+ instance. Will download this file and # cache it if needed. # # TODO: Add checksum support. # TODO: Add error checking. def retrieve(url) (path + File.basename(url.to_s)).tap do |cached_file| - uri = normalize(url) + uri = url.is_a?(::URI) ? url : ::URI.parse(url) unless cached_file.file? if uri.scheme == 'file' FileUtils.cp(URI.unescape(uri.path), cached_file) else # TODO: Handle HTTPS; probably should use repository.contact data = read_retrieve(uri) cached_file.open('wb') { |f| f.write data } end end end end # Return contents of file at the given URI's +uri+. def read_retrieve(uri) return uri.read end # Return Pathname for repository's cache directory, create it if needed. def path return @path ||= (self.class.base_path + @repository.cache_key).tap{ |o| o.mkpath } end # Return the base Pathname for all the caches. def self.base_path Pathname(Puppet.settings[:module_working_dir]) + 'cache' end # Clean out all the caches. def self.clean base_path.rmtree if base_path.exist? end end end diff --git a/lib/puppet/module_tool/repository.rb b/lib/puppet/module_tool/repository.rb index dcbd7f4ef..5bdd58775 100644 --- a/lib/puppet/module_tool/repository.rb +++ b/lib/puppet/module_tool/repository.rb @@ -1,79 +1,79 @@ require 'net/http' require 'digest/sha1' +require 'uri' module Puppet::Module::Tool # = Repository # # This class is a file for accessing remote repositories with modules. class Repository - include Utils::URI include Utils::Interrogation attr_reader :uri, :cache # Instantiate a new repository instance rooted at the optional string # +url+, else an instance of the default Puppet modules repository. def initialize(url=Puppet[:module_repository]) - @uri = normalize(url) + @uri = url.is_a?(::URI) ? url : ::URI.parse(url) @cache = Cache.new(self) end # Return a Net::HTTPResponse read for this +request+. # # Options: # * :authenticate => Request authentication on the terminal. Defaults to false. def make_http_request(request, options = {}) if options[:authenticate] authenticate(request) end if ! @uri.user.nil? && ! @uri.password.nil? request.basic_auth(@uri.user, @uri.password) end return read_response(request) end # Return a Net::HTTPResponse read from this HTTPRequest +request+. def read_response(request) begin Net::HTTP::Proxy( Puppet::Module::Tool::http_proxy_host, Puppet::Module::Tool::http_proxy_port ).start(@uri.host, @uri.port) do |http| http.request(request) end rescue Errno::ECONNREFUSED, SocketError raise SystemExit, "Could not reach remote repository" end end # Set the HTTP Basic Authentication parameters for the Net::HTTPRequest # +request+ by asking the user for input on the console. def authenticate(request) Puppet.notice "Authenticating for #{@uri}" email = prompt('Email Address') password = prompt('Password', true) request.basic_auth(email, password) end # Return the local file name containing the data downloaded from the # repository at +release+ (e.g. "myuser-mymodule"). def retrieve(release) return cache.retrieve(@uri + release) end # Return the URI string for this repository. def to_s return @uri.to_s end # Return the cache key for this repository, this a hashed string based on # the URI. def cache_key return @cache_key ||= [ @uri.to_s.gsub(/[^[:alnum:]]+/, '_').sub(/_$/, ''), Digest::SHA1.hexdigest(@uri.to_s) ].join('-') end end end diff --git a/lib/puppet/module_tool/utils.rb b/lib/puppet/module_tool/utils.rb index bbec6ff1b..85f57c973 100644 --- a/lib/puppet/module_tool/utils.rb +++ b/lib/puppet/module_tool/utils.rb @@ -1,6 +1,5 @@ module Puppet::Module::Tool module Utils - require 'puppet/module_tool/utils/uri' require 'puppet/module_tool/utils/interrogation' end end diff --git a/lib/puppet/module_tool/utils/uri.rb b/lib/puppet/module_tool/utils/uri.rb deleted file mode 100644 index f1389f120..000000000 --- a/lib/puppet/module_tool/utils/uri.rb +++ /dev/null @@ -1,15 +0,0 @@ -require 'uri' - -module Puppet::Module::Tool - module Utils - module URI - - # Return a URI instance for the +uri+, a a string or URI object. - def normalize(url) - return url.is_a?(::URI) ? - url : - ::URI.parse(url) - end - end - end -end