Module: Nutkins::Docker

Defined in:
lib/nutkins/docker.rb

Defined Under Namespace

Modules: Builder

Class Method Summary collapse

Class Method Details

.container_id_for_name(name) ⇒ Object



30
31
32
# File 'lib/nutkins/docker.rb', line 30

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

.container_id_for_tag(tag, running: false) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/nutkins/docker.rb', line 13

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

.get_short_commit(commit) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/nutkins/docker.rb', line 22

def self.get_short_commit commit
  if /^sha256:/.match(commit)
    commit[7...19]
  else
    commit
  end
end

.image_id_for_tag(tag) ⇒ Object



4
5
6
# File 'lib/nutkins/docker.rb', line 4

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

.kill_and_remove_container(id) ⇒ Object



8
9
10
11
# File 'lib/nutkins/docker.rb', line 8

def self.kill_and_remove_container id
  raise "issue killing existing container" unless Nutkins::Docker.run 'kill', id
  raise "issue removing existing container" unless Nutkins::Docker.run 'rm', id
end

.run(*args, stdout: false, stderr: true) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/nutkins/docker.rb', line 39

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

.run_get_stdout(*args) ⇒ Object



34
35
36
37
# File 'lib/nutkins/docker.rb', line 34

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