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
-
.attach_for(block, msg_stack, tty = false) ⇒ Object
Attaches to a HTTP stream.
- .attach_for_multiplex(block, msg_stack) ⇒ Object
- .attach_for_tty(block, msg_stack) ⇒ Object
- .build_auth_header(credentials) ⇒ Object
- .build_config_header(credentials) ⇒ Object
- .close_write(socket) ⇒ Object
- .create_dir_tar(directory) ⇒ Object
- .create_tar(hash = {}) ⇒ Object
- .debug(msg) ⇒ 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
- .hijack_for(stdin, block, msg_stack, tty) ⇒ Object
- .parse_json(body) ⇒ Object
- .parse_repo_tag(str) ⇒ Object
Class Method Details
.attach_for(block, msg_stack, tty = false) ⇒ Object
Attaches to a HTTP stream
13 14 15 16 17 18 19 20 |
# File 'lib/docker/util.rb', line 13 def attach_for(block, msg_stack, tty = false) # If TTY is enabled expect raw data and append to stdout if tty attach_for_tty(block, msg_stack) else attach_for_multiplex(block, msg_stack) end end |
.attach_for_multiplex(block, msg_stack) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/docker/util.rb', line 30 def attach_for_multiplex(block, msg_stack) = Docker::Messages.new lambda do |c,r,t| = .(c) msg_stack.append() unless block.nil? ..each do |msg| block.call(:stdout, msg) end ..each do |msg| block.call(:stderr, msg) end end end end |
.attach_for_tty(block, msg_stack) ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/docker/util.rb', line 22 def attach_for_tty(block, msg_stack) return lambda do |c,r,t| msg_stack. << c msg_stack. << c block.call c if block end end |
.build_auth_header(credentials) ⇒ Object
165 166 167 168 169 170 171 |
# File 'lib/docker/util.rb', line 165 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) ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/docker/util.rb', line 173 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 |
.close_write(socket) ⇒ Object
87 88 89 90 91 92 93 94 95 |
# File 'lib/docker/util.rb', line 87 def close_write(socket) if socket.respond_to?(:close_write) socket.close_write elsif socket.respond_to?(:io) socket.io.close_write else raise IOError, 'Cannot close socket' end end |
.create_dir_tar(directory) ⇒ Object
125 126 127 128 129 130 131 132 133 134 |
# File 'lib/docker/util.rb', line 125 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 = {}) ⇒ Object
115 116 117 118 119 120 121 122 123 |
# File 'lib/docker/util.rb', line 115 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 |
.debug(msg) ⇒ Object
47 48 49 |
# File 'lib/docker/util.rb', line 47 def debug(msg) Docker.logger.debug(msg) if Docker.logger end |
.extract_id(body) ⇒ Object
136 137 138 139 140 141 142 143 |
# File 'lib/docker/util.rb', line 136 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.
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/docker/util.rb', line 147 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) ⇒ Object
111 112 113 |
# File 'lib/docker/util.rb', line 111 def fix_json(body) parse_json("[#{body.gsub(/}\s*{/, '},{')}]") end |
.hijack_for(stdin, block, msg_stack, tty) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/docker/util.rb', line 51 def hijack_for(stdin, block, msg_stack, tty) attach_block = attach_for(block, msg_stack, tty) lambda do |socket| debug "hijack: hijacking the HTTP socket" threads = [] debug "hijack: starting stdin copy thread" threads << Thread.start do debug "hijack: copying stdin => socket" IO.copy_stream stdin, socket debug "hijack: closing write end of hijacked socket" close_write(socket) end debug "hijack: starting hijacked socket read thread" threads << Thread.start do debug "hijack: reading from hijacked socket" begin while chunk = socket.readpartial(512) debug "hijack: got #{chunk.bytesize} bytes from hijacked socket" attach_block.call chunk, nil, nil end rescue EOFError end debug "hijack: killing stdin copy thread" threads.first.kill end threads.each(&:join) end end |
.parse_json(body) ⇒ Object
97 98 99 100 101 |
# File 'lib/docker/util.rb', line 97 def parse_json(body) JSON.parse(body) unless body.nil? || body.empty? || (body == 'null') rescue JSON::ParserError => ex raise UnexpectedResponseError, ex. end |
.parse_repo_tag(str) ⇒ Object
103 104 105 106 107 108 109 |
# File 'lib/docker/util.rb', line 103 def parse_repo_tag(str) if match = str.match(/\A(.*):([^:]*)\z/) match.captures else [str, ''] end end |