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
- .add_file_to_tar(tar, name, mode, size, mtime) {|os| ... } ⇒ Object
-
.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_relative_dir_tar(directory, output) ⇒ Object
- .create_tar(hash = {}) ⇒ Object
- .create_temp_file ⇒ 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.
- .filesystem_permissions(path) ⇒ Object
- .fix_json(body) ⇒ Object
- .hijack_for(stdin, block, msg_stack, tty) ⇒ Object
- .parse_json(body) ⇒ Object
- .parse_repo_tag(str) ⇒ Object
Class Method Details
.add_file_to_tar(tar, name, mode, size, mtime) {|os| ... } ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/docker/util.rb', line 158 def add_file_to_tar(tar, name, mode, size, mtime) tar.check_closed io = tar.instance_variable_get(:@io) name, prefix = tar.split_name(name) header = Gem::Package::TarHeader.new(:name => name, :mode => mode, :size => size, :prefix => prefix, :mtime => mtime).to_s io.write header os = Gem::Package::TarWriter::BoundedStream.new io, size yield os if block_given? min_padding = size - os.written io.write("\0" * min_padding) remainder = (512 - (size % 512)) % 512 io.write("\0" * remainder) tar end |
.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
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/docker/util.rb', line 33 def attach_for_multiplex(block, msg_stack) = Docker::Messages.new lambda do |c,r,t| = .(c) unless block.nil? ..each do |msg| block.call(:stdout, msg) end ..each do |msg| block.call(:stderr, msg) end end msg_stack.append() end end |
.attach_for_tty(block, msg_stack) ⇒ Object
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/docker/util.rb', line 22 def attach_for_tty(block, msg_stack) = Docker::Messages.new lambda do |c,r,t| . << c . << c msg_stack.append() block.call c if block end end |
.build_auth_header(credentials) ⇒ Object
230 231 232 233 234 235 236 |
# File 'lib/docker/util.rb', line 230 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
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/docker/util.rb', line 238 def build_config_header(credentials) if credentials.is_a?(String) credentials = JSON.parse(credentials, symbolize_names: true) end header = { 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
91 92 93 94 95 96 97 98 99 |
# File 'lib/docker/util.rb', line 91 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
133 134 135 136 137 138 139 140 |
# File 'lib/docker/util.rb', line 133 def create_dir_tar(directory) tempfile = create_temp_file directory += '/' unless directory.end_with?('/') create_relative_dir_tar(directory, tempfile) File.new(tempfile.path, 'r') end |
.create_relative_dir_tar(directory, output) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/docker/util.rb', line 142 def create_relative_dir_tar(directory, output) Gem::Package::TarWriter.new(output) do |tar| Find.find(directory) do |prefixed_file_name| stat = File.stat(prefixed_file_name) next unless stat.file? unprefixed_file_name = prefixed_file_name[directory.length..-1] add_file_to_tar( tar, unprefixed_file_name, stat.mode, stat.size, stat.mtime ) do |tar_file| IO.copy_stream(File.open(prefixed_file_name, 'rb'), tar_file) end end end end |
.create_tar(hash = {}) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/docker/util.rb', line 119 def create_tar(hash = {}) output = StringIO.new Gem::Package::TarWriter.new(output) do |tar| hash.each do |file_name, file_details| = file_details.is_a?(Hash) ? file_details[:permissions] : 0640 tar.add_file(file_name, ) do |tar_file| content = file_details.is_a?(Hash) ? file_details[:content] : file_details tar_file.write(content) end end end output.tap(&:rewind).string end |
.create_temp_file ⇒ Object
183 184 185 186 |
# File 'lib/docker/util.rb', line 183 def create_temp_file tempfile_name = Dir::Tmpname.create('out') {} File.open(tempfile_name, 'wb+') end |
.debug(msg) ⇒ Object
51 52 53 |
# File 'lib/docker/util.rb', line 51 def debug(msg) Docker.logger.debug(msg) if Docker.logger end |
.extract_id(body) ⇒ Object
188 189 190 191 192 193 194 195 196 197 |
# File 'lib/docker/util.rb', line 188 def extract_id(body) body.lines.reverse_each do |line| if (id = line.match(/Successfully built ([a-f0-9]+)/)) && !id[1].empty? return id[1] elsif (id = line.match(/sha256:([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.
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/docker/util.rb', line 201 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] = { content: tar.read, permissions: (local_path) } tar.close FileUtils.rm(tar.path) else file_hash[basename] = { content: File.read(local_path, mode: 'rb'), permissions: (local_path) } end end end |
.filesystem_permissions(path) ⇒ Object
225 226 227 228 |
# File 'lib/docker/util.rb', line 225 def (path) mode = sprintf("%o", File.stat(path).mode) mode[(mode.length - 3)...mode.length].to_i(8) end |
.fix_json(body) ⇒ Object
115 116 117 |
# File 'lib/docker/util.rb', line 115 def fix_json(body) parse_json("[#{body.gsub(/}\s*{/, '},{')}]") end |
.hijack_for(stdin, block, msg_stack, tty) ⇒ Object
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 86 87 88 89 |
# File 'lib/docker/util.rb', line 55 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
101 102 103 104 105 |
# File 'lib/docker/util.rb', line 101 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
107 108 109 110 111 112 113 |
# File 'lib/docker/util.rb', line 107 def parse_repo_tag(str) if match = str.match(/\A(.*):([^:]*)\z/) match.captures else [str, ''] end end |