Included Modules

Docker::Util

This module holds shared logic that doesn't really belong anywhere else in the gem.

Public Instance Methods

build_auth_header(credentials) click to toggle source
# File lib/docker/util.rb, line 76
def build_auth_header(credentials)
  credentials = credentials.to_json if credentials.is_a?(Hash)
  encoded_creds = Base64.encode64(credentials).gsub(/\n/, '')
  {
    'X-Registry-Auth' => encoded_creds
  }
end
build_config_header(credentials) click to toggle source
# File lib/docker/util.rb, line 84
def build_config_header(credentials)
  if credentials.is_a?(String)
    credentials = JSON.parse(credentials, symbolize_names: true)
  end
  header = {
    "configs" => {
      credentials[:serveraddress].to_s => {
        "username" => credentials[:username].to_s,
        "password" => credentials[:password].to_s,
        "email" => credentials[:email].to_s
      }
    }
  }.to_json

  encoded_header = Base64.encode64(header).gsub(/\n/, '')

  {
    'X-Registry-Config' => encoded_header
  }
end
create_dir_tar(directory) click to toggle source
# File lib/docker/util.rb, line 36
def create_dir_tar(directory)
  cwd = FileUtils.pwd
  tempfile_name = Dir::Tmpname.create('out') {}
  tempfile = File.open(tempfile_name, 'wb+')
  FileUtils.cd(directory)
  Archive::Tar::Minitar.pack('.', tempfile)
  File.new(tempfile.path, 'r')
ensure
  FileUtils.cd(cwd)
end
create_tar(hash = {}) click to toggle source
# File lib/docker/util.rb, line 26
def create_tar(hash = {})
  output = StringIO.new
  Gem::Package::TarWriter.new(output) do |tar|
    hash.each do |file_name, input|
      tar.add_file(file_name, 0640) { |tar_file| tar_file.write(input) }
    end
  end
  output.tap(&:rewind).string
end
extract_id(body) click to toggle source
# File lib/docker/util.rb, line 47
def extract_id(body)
  body.lines.to_a.reverse.each do |line|
    if (id = line.match(/Successfully built ([a-f0-9]+)/)) && !id[1].empty?
      return id[1]
    end
  end
  raise UnexpectedResponseError, "Couldn't find id: #{body}"
end
file_hash_from_paths(local_paths) click to toggle source

Convenience method to get the file hash corresponding to an array of local paths.

# File lib/docker/util.rb, line 58
def file_hash_from_paths(local_paths)
  local_paths.each_with_object({}) do |local_path, file_hash|
    unless File.exist?(local_path)
      raise ArgumentError, "#{local_path} does not exist."
    end

    basename = File.basename(local_path)
    if File.directory?(local_path)
      tar = create_dir_tar(local_path)
      file_hash[basename] = tar.read
      tar.close
      FileUtils.rm(tar.path)
    else
      file_hash[basename] = File.read(local_path)
    end
  end
end
fix_json(body) click to toggle source
# File lib/docker/util.rb, line 22
def fix_json(body)
  parse_json("[#{body.gsub(/}\s*{/, '},{')}]")
end
parse_json(body) click to toggle source
# File lib/docker/util.rb, line 8
def parse_json(body)
  JSON.parse(body) unless body.nil? || body.empty? || (body == 'null')
rescue JSON::ParserError => ex
  raise UnexpectedResponseError, ex.message
end
parse_repo_tag(str) click to toggle source
# File lib/docker/util.rb, line 14
def parse_repo_tag(str)
  if match = str.match(/\A(.*):([^:]*)\z/)
    match.captures
  else
    [str, '']
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.