Sha256: 5e2e37d7351c10c9ac3b1c1faf490f7a5c9e2e2e60990a05754933af82c092d9

Contents?: true

Size: 1004 Bytes

Versions: 2

Compression:

Stored size: 1004 Bytes

Contents

require 'open3'

module Nutkins::Docker
  def self.image_id_for_tag tag
    self.run_get_stdout 'inspect', '--format="{{.Id}}"', tag
  end

  def self.container_id_for_tag tag
    regex = /^[0-9a-f]+ +#{tag} +/
    `docker ps -a`.each_line do |line|
      return line.split(' ')[0] if line =~ regex
    end
    nil
  end

  def self.container_id_for_name name
    self.run_get_stdout 'inspect', '--format="{{.Id}}"', name
  end

  def self.run_get_stdout *args
    stdout_str, stderr_str, status = Open3.capture3 'docker', *args
    status.success? && stdout_str.chomp
  end

  def self.run *args, stdout: false, stderr: true
    stdout_backup = ! stdout && $stdout.clone
    stderr_backup = ! stderr && $stderr.clone
    $stdout.reopen File.new('/dev/null', 'w') unless stdout
    $stderr.reopen File.new('/dev/null', 'w') unless stderr
    begin
      system 'docker', *args
    ensure
      $stdout.reopen stdout_backup unless stdout
      $stderr.reopen stderr_backup unless stderr
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
nutkins-0.3.0 lib/nutkins/docker.rb
nutkins-0.2.0 lib/nutkins/docker.rb