module QB::Docker::CLI

Public Class Methods

build_cmd(path_or_url, **opts) click to toggle source
# 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) click to toggle source

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

@see www.rubydoc.info/gems/cmds

@param [String] exe:

Docker executable to stick in front of `template`.

@param (see Cmds.new)

@return [Cmds]

# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/qb/docker/cli.rb, line 266
def self.inspect_image_cmd *args, **opts
  sub_cmd :inspect, *args, **opts
end
pull(name, **opts) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/qb/docker/cli.rb, line 395
def self.pull? name, **opts
  pull( name, **opts ).ok?
end
pull_cmd(*args, **opts) click to toggle source
# File lib/qb/docker/cli.rb, line 278
def self.pull_cmd *args, **opts
  sub_cmd :pull, *args, **opts
end
push(name, **opts) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

+QB::Util::Decorators::NoPropsInKwds def self.build path_or_url, **opts

end

# 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