class Chef::Resource::Nexus

Chef resource for managing artifacts on Nexus by Sonatype

Protected Instance Methods

can_generate_pom() click to toggle source
# File lib/chef/nexus.rb, line 356
def can_generate_pom
  @can_generate_pom ||= [:groupId, :artifactId, :version, :packaging].all? { |x| cords.key?(x) }
end
cords() click to toggle source
# File lib/chef/nexus.rb, line 187
      def cords
        @cords ||= begin
          hsh = {}
          if remote_url
            scn = remote_url.scan(%r{^.*?/repositories/.*?/(.*?)/((?:\d+\.)*\d+)/(.*?)-((?:\d+\.)*\d+)(?:-(.*?))?\.(.*)$})
            if scn.length == 1 && scn[0][1] == scn[0][3]
              group_id, _, artifact_id = scn[0][0].rpartition('/')
              if !group_id.empty? && artifact_id == scn[0][2]
                hsh[:groupId] = group_id.tr('/', '.')
                hsh[:artifactId] = artifact_id
                hsh[:version] = scn[0][1]
                hsh[:packaging] = scn[0][5]
                hsh[:classifier] = scn[0][4]
              end
            end
            hsh.delete_if { |_, v| v.nil? }
          end
          if coordinates
            splt = coordinates.split(':')
            unless splt.length >= 3 && splt.length <= 5
              raise ':coordinates must follow the one of the following formats
groupId:artifactId:version
groupId:artifactId:packaging:version
groupId:artifactId:packaging:classifier:version'
            end
            hsh[:groupId] = splt.first
            hsh[:artifactId] = splt[1]
            hsh[:version] = splt.last
            hsh[:packaging] = splt[2] if splt.length >= 4
            hsh[:classifier] = splt[3] if splt.length == 5
          end
          %w(groupId artifactId version packaging classifier).each do |x|
            # rubocop:disable Lint/Eval
            p = eval(x)
            # rubocop:enable Lint/Eval
            hsh[x.to_sym] = p if p
          end

          [:groupId, :artifactId, :version].each do |x|
            raise 'Your must specify :coordinates OR at least all of [:groupId, :artifactId, :version]' unless hsh[x].present?
          end unless remote_url

          if local_file && !hsh[:packaging].present?
            extn = ::File.extname(local_file)
            if !remote_url && extn.empty?
              raise 'Files require an extension, or specify it with :packaging'
            else
              hsh[:packaging] = extn[1..-1]
            end
          end
          hsh
        end
      end
curl_base_url() click to toggle source
# File lib/chef/nexus.rb, line 252
def curl_base_url
  @curl_base_url ||= begin
    url = [n_url, 'repositories', n_repo]
    url.push(cords[:groupId].split('.')).flatten!
    url.push(cords[:artifactId])
    url.push(cords[:version])
    url.push("#{cords[:artifactId]}-#{cords[:version]}")
    url.join('/')
  end
end
curl_url() click to toggle source
# File lib/chef/nexus.rb, line 241
def curl_url
  @curl_url ||= begin
    remote_url || begin
      url = curl_base_url.dup
      url += "-#{cords[:classifier]}" if cords[:classifier]
      url += ".#{cords[:packaging]}"
      url
    end
  end
end
delete(remote) click to toggle source
# File lib/chef/nexus.rb, line 165
def delete(remote)
  execute_or_fail("curl -v #{use_auth ? "-u #{n_auth} " : nil}-X DELETE #{remote}")
  raise "Server responded with successful deletion, but '#{remote}' still exists." if url_exists(remote)
end
download(remote, local) click to toggle source
# File lib/chef/nexus.rb, line 160
def download(remote, local)
  execute_or_fail("curl -v #{use_auth ? "-u #{n_auth} " : nil}#{remote} > #{local}")
  raise "File appears to have been downloaded, but '#{local}' does not exist." unless ::File.exist?(local)
end
download_and_read(url) click to toggle source
# File lib/chef/nexus.rb, line 365
def download_and_read(url)
  return nil unless url_exists(url)
  tmp = '/tmp/' + Digest::SHA1.hexdigest(rand(100000000000000).to_s)
  cmd = "curl -v #{use_auth ? "-u #{n_auth} " : nil}#{url} > #{tmp}"
  execute_or_fail(cmd)
  content = ::File.read(tmp)
  ::File.delete(tmp)
  content
end
execute_or_fail(cmd, check_http = true) click to toggle source
# File lib/chef/nexus.rb, line 170
def execute_or_fail(cmd, check_http = true)
  output = `#{cmd}`
  fail_me = false
  output.scan(%r{^ +<title>(\d{3}) - .*?</title>$}).each { |code| fail_me = true unless code[0] == '1' || code[0] == '2' } if check_http
  if $?.exitstatus != 0 || fail_me
    raise "Command failed: #{cmd}\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n#{output.strip}\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"
  end
  output
end
file_equal(current_resource) click to toggle source

If neither SHA1 or MD5 checksums are on Nexus, we will assume not equal.

# File lib/chef/nexus.rb, line 332
def file_equal(current_resource)
  @file_equal ||= begin
    if file_exists
      return true if current_resource.file_sha1 && local_file_sha1 == current_resource.file_sha1
      return true if current_resource.file_md5 && local_file_md5 == current_resource.file_md5
    end
    false
  end
end
file_exists() click to toggle source
# File lib/chef/nexus.rb, line 327
def file_exists
  @file_exists ||= url_exists(curl_url)
end
local_file_md5() click to toggle source
# File lib/chef/nexus.rb, line 305
def local_file_md5
  @local_file_md5 ||= begin
    ::File.open(local_file, 'rb') do |f|
      digest = Digest::MD5.new
      buffer = ''
      digest.update(buffer) while f.read(4096, buffer)
      digest.hexdigest
    end if ::File.exist?(local_file)
  end
end
local_file_sha1() click to toggle source
# File lib/chef/nexus.rb, line 316
def local_file_sha1
  @local_file_sha1 ||= begin
    ::File.open(local_file, 'rb') do |f|
      digest = Digest::SHA1.new
      buffer = ''
      digest.update(buffer) while f.read(4096, buffer)
      digest.hexdigest
    end if ::File.exist?(local_file)
  end
end
n_auth() click to toggle source
# File lib/chef/nexus.rb, line 271
def n_auth
  @n_auth ||= begin
    nauth = nexus_auth || ENV['NEXUS_AUTH'] || n_config['auth']
    raise "Please provide Nexus auth as either an attribute :nexus_auth or in ~/.nexus/config profile '#{n_profile}'" if use_auth && !nauth
    nauth
  end
end
n_config() click to toggle source
# File lib/chef/nexus.rb, line 287
def n_config
  @n_config ||= begin
    if ENV['NEXUS_CONFIG'] && ::File.exist?(ENV['NEXUS_CONFIG'])
      JSON.parse(::File.read(ENV['NEXUS_CONFIG']))[n_profile] || {}
    elsif ::File.exist?("#{ENV['HOME']}/.nexus/config")
      JSON.parse(::File.read("#{ENV['HOME']}/.nexus/config"))[n_profile] || {}
    elsif ::File.exist?('/etc/.nexus/config')
      JSON.parse(::File.read('/etc/.nexus/config'))[n_profile] || {}
    else
      {}
    end
  end
end
n_profile() click to toggle source
# File lib/chef/nexus.rb, line 301
def n_profile
  @n_profile ||= nexus_profile || ENV['NEXUS_PROFILE'] || 'default'
end
n_repo() click to toggle source
# File lib/chef/nexus.rb, line 279
def n_repo
  @n_repo ||= begin
    nrepo = nexus_repo || ENV['NEXUS_REPO'] || n_config['repo']
    raise "Please provide Nexus repository as either an attribute :repository or in ~/.nexus/config profile '#{n_profile}'" unless nrepo
    nrepo
  end
end
n_url() click to toggle source
# File lib/chef/nexus.rb, line 263
def n_url
  @n_url ||= begin
    nurl = nexus_url || ENV['NEXUS_URL'] || n_config['url']
    raise "Please provide Nexus url as either an attribute :nexus_url or in ~/.nexus/config profile '#{n_profile}'" unless nurl
    nurl.chomp('/')
  end
end
pom_equal(current_resource) click to toggle source
# File lib/chef/nexus.rb, line 346
def pom_equal(current_resource)
  @pom_equal ||= begin
    if pom_exists
      return true if current_resource.pom_sha1 && pom_sha1 == current_resource.pom_sha1
      return true if current_resource.pom_md5 && pom_md5 == current_resource.pom_md5
    end
    false
  end
end
pom_exists() click to toggle source
# File lib/chef/nexus.rb, line 342
def pom_exists
  @pom_exists ||= url_exists(curl_base_url + '.pom')
end
upload(local, remote) click to toggle source
# File lib/chef/nexus.rb, line 155
def upload(local, remote)
  execute_or_fail("curl -v #{use_auth ? "-u #{n_auth} " : nil}-T #{local} #{remote}")
  raise "Server responded with successful creation, but '#{remote}' does not exist." unless url_exists(remote)
end
url_exists(url) click to toggle source
# File lib/chef/nexus.rb, line 360
def url_exists(url)
  `curl --output /dev/null --silent --head --fail #{use_auth ? "-u #{n_auth} " : nil}#{url}`
  $?.exitstatus == 0
end
write_file(remote, content) click to toggle source
# File lib/chef/nexus.rb, line 180
def write_file(remote, content)
  tmp = '/tmp/' + Digest::SHA1.hexdigest(rand(100000000000000).to_s)
  ::File.open(tmp, 'w') { |file| file.write(content) }
  upload(tmp, remote)
  ::File.delete(tmp)
end