Class: PmgmtLib::DockerHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/dockerhelper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(common) ⇒ DockerHelper

Returns a new instance of DockerHelper.



5
6
7
# File 'lib/dockerhelper.rb', line 5

def initialize(common)
  @c = common
end

Instance Attribute Details

#cObject (readonly)

Returns the value of attribute c.



3
4
5
# File 'lib/dockerhelper.rb', line 3

def c
  @c
end

Instance Method Details

#ensure_image(name) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/dockerhelper.rb', line 38

def ensure_image(name)
  requires_docker
  if not image_exists?(name)
    c.error "Missing docker image \"#{name}\". Pulling..."
    c.run_inline(%W{docker pull #{name}})
    c.status "Image \"#{name}\" pulled."
  end
end

#image_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
# File 'lib/dockerhelper.rb', line 32

def image_exists?(name)
  requires_docker
  fmt = "{{.Repository}}:{{.Tag}}"
  c.capture_stdout(%W{docker images --format #{fmt}}).include?(name)
end

#in_docker?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/dockerhelper.rb', line 9

def in_docker?()
  File.exist?("/.dockerenv")
end

#requires_dockerObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/dockerhelper.rb', line 13

def requires_docker()
  status = c.run %W{which docker}
  unless status.success?
    c.error "docker not installed."
    STDERR.puts "Installation instructions:"
    STDERR.puts "\n  https://www.docker.com/community-edition\n\n"
    exit 1
  end
  status = c.run %W{docker info}
  unless status.success?
    c.error "`docker info` command failed."
    STDERR.puts "This is usually a permissions problem. Try allowing your user to run docker\n"
    STDERR.puts "without sudo:"
    STDERR.puts "\n$ sudo usermod -aG docker #{ENV["USER"]}\n\n"
    c.error "Note: You will need to log-in to a new shell before this change will take effect.\n"
    exit 1
  end
end