class Nexus::Client
Constants
- VERSION
Attributes
cache[R]
host[R]
log[RW]
path_prefix[RW]
use_cache[RW]
Public Class Methods
download(destination, gav_str, cache_dir='/tmp/cache', enable_cache=false,enable_analytics=false,host=nil,path_prefix='/nexus')
click to toggle source
# File lib/nexus_client.rb, line 56 def self.download(destination, gav_str, cache_dir='/tmp/cache', enable_cache=false,enable_analytics=false,host=nil,path_prefix='/nexus') client = Nexus::Client.new(host, cache_dir, enable_cache,enable_analytics, nil, path_prefix) client.download_gav(destination, gav_str) end
new(nexus_host=nil, cache_dir='/tmp/cache', enable_cache=true, enable_analytics=false,logger=nil, path_prefix='/nexus')
click to toggle source
# File lib/nexus_client.rb, line 16 def initialize(nexus_host=nil, cache_dir='/tmp/cache', enable_cache=true, enable_analytics=false,logger=nil, path_prefix='/nexus') @log = logger @host = nexus_host || default_host @host = @host.gsub(/\/nexus$/, '') # just in case user enters /nexus @use_cache = enable_cache @path_prefix = path_prefix if @use_cache @cache_base = cache_dir @cache = Nexus::Cache.new(@cache_base, enable_analytics, log) end #Typhoeus::Config.verbose = true end
version()
click to toggle source
# File lib/nexus_client/version.rb, line 5 def self.version VERSION end
Public Instance Methods
create_target(destination)
click to toggle source
# File lib/nexus_client.rb, line 67 def create_target(destination) destination = File.expand_path(destination) if ! File.directory?(destination) begin FileUtils.mkdir_p(destination) if not File.exists?(destination) rescue SystemCallError => e raise e, 'Cannot create directory' end end end
default_host()
click to toggle source
# File lib/nexus_client.rb, line 41 def default_host read_host end
download_gav(destination, gav_str)
click to toggle source
# File lib/nexus_client.rb, line 61 def download_gav(destination, gav_str) log.info("Downloading #{gav_str} from #{host_url} to #{destination}" ) gav = Nexus::Gav.new(gav_str) download(destination, gav) end
gav_data(gav)
click to toggle source
retrieves the attributes of the gav
# File lib/nexus_client.rb, line 80 def gav_data(gav) res = {} request = Typhoeus::Request.new( "#{host_url}/service/local/artifact/maven/resolve", :params => gav.to_hash,:connecttimeout => 5, :headers => { 'Accept' => 'application/json' } ) request.on_failure do |response| raise("Failed to get gav data for #{gav.to_s}") end request.on_complete do |response| res = JSON.parse(response.response_body) end request.run res['data'] end
host_url()
click to toggle source
# File lib/nexus_client.rb, line 52 def host_url "#{host}#{path_prefix}" end
read_host(filename="
click to toggle source
read host will read ~/.nexus_host file and
# File lib/nexus_client.rb, line 31 def read_host(filename="#{Etc.getpwuid.dir}/.nexus_host") fn = File.expand_path(filename) abort("Please create the file #{filename} and add your nexus host") if not File.exists?(filename) begin File.open(fn, 'r') { |f| f.read }.strip rescue Exception => e raise(e) end end
sha(file, use_sha_file=false)
click to toggle source
returns the sha1 of the file
# File lib/nexus_client.rb, line 99 def sha(file, use_sha_file=false) if use_sha_file and File.exists?("#{file}.sha1") # reading the file is faster than doing a hash, so we keep the hash in the file # then we read back and compare. There is no reason to perform sha1 everytime begin File.open("#{file}.sha1", 'r') { |f| f.read().strip} rescue Digest::SHA1.file(File.expand_path(file)).hexdigest end else Digest::SHA1.file(File.expand_path(file)).hexdigest end end
sha_match?(file, gav, use_sha_file=false)
click to toggle source
sha_match? returns bool by comparing the sha1 of the nexus gav artifact and the local file
# File lib/nexus_client.rb, line 114 def sha_match?(file, gav, use_sha_file=false) if File.exists?(file) if gav.sha1.nil? gav.sha1 = gav_data(gav)['sha1'] end sha(file,use_sha_file) == gav.sha1 else false end end
Private Instance Methods
download(destination, gav)
click to toggle source
downloads the gav to the destination, returns the file if download was successful if cache is on then it will use the cache and if file is new will also cache the new file TODO need a timeout when host is unreachable
# File lib/nexus_client.rb, line 136 def download(destination, gav) raise 'Download destination must not be empty' if destination.empty? create_target(destination) # ensure directory path is created destination = File.expand_path(destination) if File.directory?(destination) dstfile = File.expand_path("#{destination}/#{gav.filename}") else dstfile = File.expand_path(destination) end # if the file already exists at the destination path than we don't need to download it again if sha_match?(dstfile, gav) # create a file that stores the sha1 for faster file comparisions later # This will only get created when the sha1 matches write_sha1("#{dstfile}", gav.sha1) if use_cache and not cache.exists?(gav) cache.add_file(gav, dstfile) end return true end # remove the previous sha1 file if it already exists FileUtils.rm("#{dstfile}.sha1") if File.exists?("#{dstfile}.sha1") if gav.sha1.nil? gav.sha1 = gav_data(gav)['sha1'] end # use the cache if the file is in the cache if use_cache and cache.exists?(gav) cache_file_path = cache.file_path(gav) FileUtils.copy(cache_file_path, dstfile) cache.record_hit(gav) else request = Typhoeus::Request.new( "#{host_url}/service/local/artifact/maven/redirect", :params => gav.to_hash, :connecttimeout => 5, :followlocation => true ) request.on_failure do |response| raise("Failed to download #{gav.to_s}") end # when complete, lets write the data to the file # first lets compare the sha matches # if the gav we thought we downloaded has the same checksum, were are good request.on_complete do |response| File.open(dstfile, 'wb') { |f| f.write(response.body) } unless ! response.success? if not sha_match?(dstfile, gav, false) raise("Error sha1 mismatch gav #{gav.sha1} != #{sha(dstfile)}") end gav.attributes[:size] = File.size(dstfile) gav.attributes[:total_time] = response.options[:total_time] # lets cache the file if cache is on if use_cache cache.add_file(gav, dstfile) end dstfile end request.run dstfile end dstfile end
write_sha1(file,sha1)
click to toggle source
writes the sha1 a file if and only if the contents of the file do not match
# File lib/nexus_client.rb, line 128 def write_sha1(file,sha1) shafile = "#{file}.sha1" File.open(shafile, 'w') { |f| f.write(sha1) } end