Module: Docker::Util
- Includes:
- Error
- Defined in:
- lib/docker/util.rb
Overview
This module holds shared logic that doesn’t really belong anywhere else in the gem.
Class Method Summary collapse
- .build_auth_header(credentials) ⇒ Object
- .create_dir_tar(directory) ⇒ Object
- .create_tar(hash = {}) ⇒ Object
- .extract_id(body) ⇒ Object
-
.file_hash_from_paths(local_paths) ⇒ Object
Convenience method to get the file hash corresponding to an array of local paths.
- .fix_json(body) ⇒ Object
- .parse_json(body) ⇒ Object
Class Method Details
.build_auth_header(credentials) ⇒ Object
65 66 67 68 69 70 71 72 |
# File 'lib/docker/util.rb', line 65 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, 'X-Registry-Config' => encoded_creds, } end |
.create_dir_tar(directory) ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/docker/util.rb', line 28 def create_dir_tar(directory) cwd = FileUtils.pwd tempfile = File.new('/tmp/out', 'wb') FileUtils.cd(directory) Archive::Tar::Minitar.pack('.', tempfile) File.new('/tmp/out', 'r') ensure FileUtils.cd(cwd) end |
.create_tar(hash = {}) ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'lib/docker/util.rb', line 18 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) ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/docker/util.rb', line 38 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) ⇒ Object
Convenience method to get the file hash corresponding to an array of local paths.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/docker/util.rb', line 49 def file_hash_from_paths(local_paths) file_hash = {} local_paths.each do |local_path| if File.exist?(local_path) basename = File.basename(local_path) file_hash[basename] = File.read(local_path) else raise ArgumentError, "#{local_path} does not exist." end end file_hash end |
.fix_json(body) ⇒ Object
14 15 16 |
# File 'lib/docker/util.rb', line 14 def fix_json(body) parse_json("[#{body.gsub(/}\s*{/, '},{')}]") end |
.parse_json(body) ⇒ Object
8 9 10 11 12 |
# 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. end |