Module: Dory::DockerService

Included in:
Dnsmasq, Proxy
Defined in:
lib/dory/docker_service.rb

Instance Method Summary collapse

Instance Method Details

#container_exists?(container_name = self.container_name) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/dory/docker_service.rb', line 62

def container_exists?(container_name = self.container_name)
  !!(self.ps(all: true) =~ /#{container_name}/)
end

#delete(container_name = self.container_name) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/dory/docker_service.rb', line 85

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

#docker_installed?Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/dory/docker_service.rb', line 6

def docker_installed?
  Sh.run_command('which docker').success?
end

#handle_error(command_output) ⇒ Object



15
16
17
18
# File 'lib/dory/docker_service.rb', line 15

def handle_error(command_output)
  # Override to provide error handling
  return false
end

#has_docker_client?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/dory/docker_service.rb', line 76

def has_docker_client?
  Sh.run_command('which docker').success?
end

#ps(all: false) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/dory/docker_service.rb', line 66

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

#run_preconditionsObject



10
11
12
13
# File 'lib/dory/docker_service.rb', line 10

def run_preconditions
  # Override if preconditions are needed
  return true
end

#running?(container_name = self.container_name) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
60
# File 'lib/dory/docker_service.rb', line 57

def running?(container_name = self.container_name)
  return false unless docker_installed?
  !!(self.ps =~ /#{container_name}/)
end

#start(handle_error: true) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/dory/docker_service.rb', line 20

def start(handle_error: true)
  if self.running?
    if Dory::Config.debug?
      puts "[DEBUG] Container '#{self.container_name}' is already running. Doing nothing"
    end
  else
    if docker_installed?
      self.run_preconditions
      if self.container_exists?
        puts "[DEBUG] Container '#{self.container_name}' exists.  Deleting" if Dory::Config.debug?
        self.delete
      end
      begin
        if Dory::Config.debug?
          puts "[DEBUG] '#{self.container_name}' does not exist.  Creating/starting " \
               "'#{self.container_name}' with '#{self.run_command}'"
        end
        status = Sh.run_command(self.run_command)
        unless status.success?
          if !handle_error || !self.handle_error(status)
            puts "Failed to start docker container '#{self.container_name}' " \
                 ".  Command '#{self.run_command}' failed".red
          end
        end
      rescue DinghyError => e
        puts e.message.red
      end
    else
      err_msg = "Docker does not appear to be installed /o\\\n" \
        "Docker is required for DNS and Nginx proxy.  These can be " \
        "disabled in the config file if you don't need them."
      puts err_msg.red
    end
  end
  self.running?
end

#start_cmdObject



93
94
95
# File 'lib/dory/docker_service.rb', line 93

def start_cmd
  "docker start #{Shellwords.escape(self.container_name)}"
end

#stop(container_name = self.container_name) ⇒ Object



80
81
82
83
# File 'lib/dory/docker_service.rb', line 80

def stop(container_name = self.container_name)
  Sh.run_command("docker kill #{Shellwords.escape(container_name)}") if self.running?
  !self.running?
end