class NexusAPI::DockerManager

Public Class Methods

new(docker:, options:) click to toggle source
# File lib/nexus_api/docker_manager.rb, line 6
def initialize(docker:, options:)
  @docker = docker
  @username = options['username']
  @password = options['password']
  @pull_host = options['pull_host']
  @push_host = options['push_host']
end

Public Instance Methods

delete(image_name:, tag:) click to toggle source
# File lib/nexus_api/docker_manager.rb, line 48
def delete(image_name:, tag:)
  return false unless docker_valid?
  begin
    image = find_image(image_name(@pull_host, image_name, tag))
    return false if image.nil?
    return false unless image.remove(:force => true)
  rescue StandardError => error
    puts "ERROR: #{error.inspect}"
    return false
  end
  true
end
download(image_name:, tag:) click to toggle source
# File lib/nexus_api/docker_manager.rb, line 14
def download(image_name:, tag:)
  return false unless docker_valid?
  image_name = image_name(@pull_host, image_name, tag)
  begin
    @docker.pull_image(@username, @password, image_name)
  rescue Docker::Error::NotFoundError
    puts "ERROR: Failed to pull Docker image #{image_name}.\nDoes it exist in Nexus?"
    return false
  end
  true
end
exists?(image_name:, tag:) click to toggle source
# File lib/nexus_api/docker_manager.rb, line 41
def exists?(image_name:, tag:)
  return false unless docker_valid?
  images = find_images(image_name(@pull_host, image_name, tag))
  return false if images.empty?
  true
end
upload(image_name:, tag:) click to toggle source
# File lib/nexus_api/docker_manager.rb, line 26
def upload(image_name:, tag:)
  return false unless docker_valid?
  begin
    return false unless login
    image = find_image(image_name(@pull_host, image_name, tag))
    return false if image.nil?
    tag(image, @push_host, image_name, tag)
    image.push(nil, repo_tag: image_name(@push_host, image_name, tag))
  rescue StandardError => error
    puts "ERROR: #{error.inspect}"
    return false
  end
  true
end

Private Instance Methods

docker_valid?() click to toggle source
# File lib/nexus_api/docker_manager.rb, line 63
def docker_valid?
  return true if @docker.validate_version!
  puts 'ERROR: Your installed version of the Docker API is not supported by the docker-api gem!'
  false
end
find_image(name) click to toggle source
# File lib/nexus_api/docker_manager.rb, line 97
def find_image(name)
  images = find_images(name)
  if valid?(images)
    images.first
  else
    nil
  end
end
find_images(name) click to toggle source
# File lib/nexus_api/docker_manager.rb, line 79
def find_images(name)
  @docker.list_images.select do |image|
    image.info['RepoTags'].include?(name)
  end
end
image_name(host, name, tag) click to toggle source
# File lib/nexus_api/docker_manager.rb, line 69
def image_name(host, name, tag)
  "#{host}/#{name}:#{tag}"
end
login() click to toggle source
# File lib/nexus_api/docker_manager.rb, line 73
def login
  return true if @docker.authenticate!(@username, @password, @push_host)
  puts "ERROR: Failed to authenticate to #{@push_host} as #{@username}"
  false
end
tag(image, host, name, tag) click to toggle source
# File lib/nexus_api/docker_manager.rb, line 106
def tag(image, host, name, tag)
  unless image.info['RepoTags'].include?(image_name(host, name, tag))
    image.tag('repo'=>"#{host}/#{name}", 'tag'=>tag)
  end
end
valid?(images) click to toggle source
# File lib/nexus_api/docker_manager.rb, line 85
def valid?(images)
  if images.empty?
    puts 'ERROR: No matching docker images found'
    return false
  end
  if images.count > 1
    puts "ERROR: Found multiple images that match: #{images.map {|image| image.info['RepoTags']} }"
    return false
  end
  true
end