Class: Nexus::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/nexus_client.rb,
lib/nexus_client/version.rb

Constant Summary collapse

VERSION =
"0.4.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nexus_host = nil, cache_dir = '/tmp/cache', enable_cache = true, enable_analytics = false, logger = nil, path_prefix = '/nexus') ⇒ Client

Returns a new instance of Client.



16
17
18
19
20
21
22
23
24
25
26
27
28
# 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

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



13
14
15
# File 'lib/nexus_client.rb', line 13

def cache
  @cache
end

#hostObject (readonly)

Returns the value of attribute host.



13
14
15
# File 'lib/nexus_client.rb', line 13

def host
  @host
end

#logObject

Returns the value of attribute log.



14
15
16
# File 'lib/nexus_client.rb', line 14

def log
  @log
end

#path_prefixObject

Returns the value of attribute path_prefix.



14
15
16
# File 'lib/nexus_client.rb', line 14

def path_prefix
  @path_prefix
end

#use_cacheObject

Returns the value of attribute use_cache.



14
15
16
# File 'lib/nexus_client.rb', line 14

def use_cache
  @use_cache
end

Class Method Details

.download(destination, gav_str, cache_dir = '/tmp/cache', enable_cache = false, enable_analytics = false, host = nil, path_prefix = '/nexus') ⇒ Object



56
57
58
59
# 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

.versionObject



5
6
7
# File 'lib/nexus_client/version.rb', line 5

def self.version
  VERSION
end

Instance Method Details

#create_target(destination) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# 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_hostObject



41
42
43
# File 'lib/nexus_client.rb', line 41

def default_host
  read_host
end

#download_gav(destination, gav_str) ⇒ Object



61
62
63
64
65
# 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) ⇒ Object

retrieves the attributes of the gav



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# 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_urlObject



52
53
54
# File 'lib/nexus_client.rb', line 52

def host_url
  "#{host}#{path_prefix}"
end

#read_host(filename = "#{Etc.getpwuid.dir}/.nexus_host") ⇒ Object

read host will read ~/.nexus_host file and



31
32
33
34
35
36
37
38
39
# 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) ⇒ Object

returns the sha1 of the file



99
100
101
102
103
104
105
106
107
108
109
110
111
# 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) ⇒ Boolean

sha_match? returns bool by comparing the sha1 of the nexus gav artifact and the local file

Returns:

  • (Boolean)


114
115
116
117
118
119
120
121
122
123
# 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