Module: QB::Docker::CLI

Extended by:
MethodDecorators
Includes:
NRSER::Log::Mixin
Defined in:
lib/qb/docker/cli.rb

Defined Under Namespace

Classes: Error, ManifestNotFoundError

Utility Class Methods collapse

Sugar Class Methods collapse

Class Method Details

.build_cmd(path_or_url, **opts) ⇒ Object



321
322
323
# File 'lib/qb/docker/cli.rb', line 321

def self.build_cmd path_or_url, **opts
  sub_cmd :build, path_or_url, **opts
end

.cmd(template, exe: 'docker', array_mode: :repeat, dash_opt_names: true, hash_join_string: '=', long_opt_separator: ' ', **options) ⇒ Cmds

Create a Cmds with formatting options defaulted for how (at least most) docker subcommands seem to work.

Parameters:

  • exe: (String) (defaults to: 'docker')

    Docker executable to stick in front of template.

Returns:

  • (Cmds)

See Also:



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/qb/docker/cli.rb', line 87

def self.cmd \
      template,
      exe: 'docker',
      array_mode: :repeat,
      dash_opt_names: true,
      hash_join_string: '=',
      long_opt_separator: ' ',
      **options
  Cmds.new \
    "#{ exe.shellescape } #{ template }",
    array_mode: array_mode,
    dash_opt_names: dash_opt_names,
    hash_join_string: hash_join_string,
    long_opt_separator: long_opt_separator,
    **options
end

.image_named?(name, **opts) ⇒ Boolean

Returns:

  • (Boolean)


382
383
384
# File 'lib/qb/docker/cli.rb', line 382

def self.image_named? name, **opts
  !images( name, load: false, only_named: false, **opts ).empty?
end

.image_names(*args, **opts) ⇒ Object



370
371
372
373
# File 'lib/qb/docker/cli.rb', line 370

def self.image_names *args, **opts
  images( *args, load: true, only_named: true, **opts ).
    map &:name.to_retriever
end

.images(*args, load: true, only_named: true, **opts) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/qb/docker/cli.rb', line 211

def self.images *args, load: true, only_named: true, **opts
  hashes = images_cmd( *args, **opts ).
    out!.
    split( "\n\n" ).
    map { |chunk|
      chunk.lines.map { |line|
          key, _, value = line.chomp.partition ': '
          
          if key == 'created_at'
            value = Time.parse value
          end
          
          [key, value]
        }.
        to_h.
        with_indifferent_access
    }
  
  if only_named
    hashes.reject! { |hash|
      hash.values_at( :repository, :tag ).any? { |v| v == '<none>' }
    }
  end
  
  if load
    hashes.each { |hash|
      values = hash.values_at :repository, :tag
      
      unless values.any? { |v| v == '<none>' }
        hash[:name] = QB::Docker::Image::Name.from_s values.join( ':' )
      end
    }
  end
  
  hashes
end

.images_cmd(*args, format: :raw, **opts) ⇒ Object



152
153
154
# File 'lib/qb/docker/cli.rb', line 152

def self.images_cmd *args, format: :raw, **opts
  sub_cmd :images, *args, format: format, **opts
end

.inspect_image(*names_or_ids, **opts) ⇒ Object



272
273
274
# File 'lib/qb/docker/cli.rb', line 272

def self.inspect_image *names_or_ids, **opts
  inspect_cmd( *names_or_ids, **opts ).out!.thru { |s| JSON.load s }
end

.inspect_image_cmd(*args, **opts) ⇒ Object



266
267
268
# File 'lib/qb/docker/cli.rb', line 266

def self.inspect_image_cmd *args, **opts
  sub_cmd :inspect, *args, **opts
end

.pull(name, **opts) ⇒ Object



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/qb/docker/cli.rb', line 288

def self.pull name, **opts
  logger.info "Pulling #{ name }...",
    name: name,
    opts: opts
  
  result = pull_cmd( name, **opts ).capture
  
  if result.ok?
    logger.info "Successfully pulled #{ name }."
  else
    logger.info "Failed to pull #{ name }",
      stderr: result.err
  end
  
  result
end

.pull!(name, **opts) ⇒ Object



414
415
416
417
418
419
420
421
422
423
424
425
426
# File 'lib/qb/docker/cli.rb', line 414

def self.pull! name, **opts
  result = pull name, **opts
  
  if result.err =~ /manifest.*not\ found/
    raise QB::Docker::CLI::ManifestNotFoundError.new \
      "Failed to pull - manifest for #{ name } not found",
      name: name,
      cmd: result.cmd,
      stderr: result.err
  else
    raise QB::Docker::CLI::Error.from_result( result )
  end
end

.pull?(name, **opts) ⇒ Boolean

Returns:

  • (Boolean)


395
396
397
# File 'lib/qb/docker/cli.rb', line 395

def self.pull? name, **opts
  pull( name, **opts ).ok?
end

.pull_cmd(*args, **opts) ⇒ Object



278
279
280
# File 'lib/qb/docker/cli.rb', line 278

def self.pull_cmd *args, **opts
  sub_cmd :pull, *args, **opts
end

.push(name, **opts) ⇒ Object



313
314
315
316
317
# File 'lib/qb/docker/cli.rb', line 313

def self.push name, **opts
  logger.info "Pushing `#{ name }`...", name: name, opts: opts
  
  result = push_cmd( name, **opts ).capture
end

.push_cmd(name, **opts) ⇒ Object



307
308
309
# File 'lib/qb/docker/cli.rb', line 307

def self.push_cmd name, **opts
  sub_cmd :push, name, **opts
end

.rmi(*args, method: :stream!, **opts) ⇒ Object



258
259
260
# File 'lib/qb/docker/cli.rb', line 258

def self.rmi *args, method: :stream!, **opts
  rmi_cmd( *args, **opts ).public_send method
end

.rmi_cmd(*args, **opts) ⇒ Object



250
251
252
# File 'lib/qb/docker/cli.rb', line 250

def self.rmi_cmd *args, **opts
  sub_cmd :rmi, *args, **opts
end

.sub_cmd(name, *args, **opts) ⇒ Object



122
123
124
125
126
127
128
129
130
# File 'lib/qb/docker/cli.rb', line 122

def self.sub_cmd name, *args, **opts
  cmd \
    "<%= sub_cmd %> <%= opts %> <%= *args %>",
    args: args,
    kwds: {
      sub_cmd: name,
      opts: opts,
    }
end

.tag_cmd(current_name, new_name_or_tag) ⇒ Object



332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/qb/docker/cli.rb', line 332

def self.tag_cmd current_name, new_name_or_tag
  # Load whatever we have
  current_name = QB::Docker::Image::Name.from current_name
  
  new_name_or_tag = [
    QB::Docker::Image::Name,
    QB::Docker::Image::Tag,
  ].try_find { |klass| klass.from new_name_or_tag }
  
  new_name = if new_name_or_tag.is_a?( QB::Docker::Image::Name )
    if new_name_or_tag.tag
      new_name_or_tag
    else
      new_name_or_tag.merge tag: current_name.tag
    end
  else
    current_name.merge tag: new_name_or_tag
  end
  
  sub_cmd :tag, current_name, new_name_or_tag
end