Sha256: 64b4df742863d48e60e173ac39eb51eda4c8864b68303c4fcf482e68e3fd395a

Contents?: true

Size: 1009 Bytes

Versions: 6

Compression:

Stored size: 1009 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 : nil
  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

6 entries across 6 versions & 1 rubygems

Version Path
nutkins-0.6.1 lib/nutkins/docker.rb
nutkins-0.6.0 lib/nutkins/docker.rb
nutkins-0.5.1 lib/nutkins/docker.rb
nutkins-0.5.0 lib/nutkins/docker.rb
nutkins-0.4.1 lib/nutkins/docker.rb
nutkins-0.4.0 lib/nutkins/docker.rb