module Pygmy::DockerService

Public Instance Methods

container_exists?(container_name = self.container_name) click to toggle source
# File lib/pygmy/docker_service.rb, line 35
def container_exists?(container_name = self.container_name)
  !!(self.ps(true) =~ /#{container_name}/)
end
delete(container_name = self.container_name) click to toggle source
# File lib/pygmy/docker_service.rb, line 58
def delete(container_name = self.container_name)
  if self.container_exists?
    self.stop if self.running?
    Sh.run_command("docker rm #{Shellwords.escape(container_name)}")
  end
  !self.container_exists?
end
has_docker_client?() click to toggle source
# File lib/pygmy/docker_service.rb, line 49
def has_docker_client?
  Sh.run_command('which docker').success?
end
ps(all = false) click to toggle source
# File lib/pygmy/docker_service.rb, line 39
def ps(all = false)
  cmd = "docker ps#{all ? ' -a' : ''}"
  ret = Sh.run_command(cmd)
  if ret.success?
    return ret.stdout
  else
    raise RuntimeError.new("Failure running command '#{cmd}'")
  end
end
pull() click to toggle source
# File lib/pygmy/docker_service.rb, line 21
def pull
  puts "Pulling Docker Image #{Shellwords.escape(self.image_name)}".yellow
  success = Sh.run_command("docker pull #{Shellwords.escape(self.image_name)}").success?
  unless success
    raise RuntimeError.new(
      "Failed to update #{self.container_name}.  Command #{self.pull_cmd} failed"
    )
  end
end
running?(container_name = self.container_name) click to toggle source
# File lib/pygmy/docker_service.rb, line 31
def running?(container_name = self.container_name)
  !!(self.ps =~ /#{container_name}/)
end
start() click to toggle source
# File lib/pygmy/docker_service.rb, line 5
def start
  unless self.running?
    success = if self.container_exists?
                Sh.run_command(self.start_cmd).success?
              else
                Sh.run_command(self.run_cmd).success?
              end
    unless success
      raise RuntimeError.new(
        "Failed to run #{self.container_name}.  Command #{self.run_cmd} failed"
      )
    end
  end
  self.running?
end
start_cmd() click to toggle source
# File lib/pygmy/docker_service.rb, line 66
def start_cmd
  "docker start #{Shellwords.escape(self.container_name)}"
end
stop(container_name = self.container_name) click to toggle source
# File lib/pygmy/docker_service.rb, line 53
def stop(container_name = self.container_name)
  Sh.run_command("docker stop -t 1 #{Shellwords.escape(container_name)}") if self.running?
  !self.running?
end