Module: Dory::DockerService
Class Method Summary collapse
Instance Method Summary collapse
- #container_exists?(container_name = self.container_name) ⇒ Boolean
- #delete(container_name = self.container_name) ⇒ Object
- #delete_container_if_exists ⇒ Object
- #docker_installed? ⇒ Boolean
- #execute_run_command(handle_error:) ⇒ Object
- #handle_error(_command_output) ⇒ Object
- #ps(all: false) ⇒ Object
- #run_postconditions ⇒ Object
- #run_preconditions ⇒ Object
- #running?(container_name = self.container_name) ⇒ Boolean
- #start(handle_error: true) ⇒ Object
- #start_cmd ⇒ Object
- #stop(container_name = self.container_name) ⇒ Object
Class Method Details
.docker_installed? ⇒ Boolean
6 7 8 |
# File 'lib/dory/docker_service.rb', line 6 def self.docker_installed? Sh.run_command('which docker').success? end |
Instance Method Details
#container_exists?(container_name = self.container_name) ⇒ Boolean
55 56 57 |
# File 'lib/dory/docker_service.rb', line 55 def container_exists?(container_name = self.container_name) !!(self.ps(all: true) =~ /#{container_name}/) end |
#delete(container_name = self.container_name) ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/dory/docker_service.rb', line 74 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 |
#delete_container_if_exists ⇒ Object
86 87 88 89 90 91 |
# File 'lib/dory/docker_service.rb', line 86 def delete_container_if_exists if self.container_exists? puts "[DEBUG] Container '#{self.container_name}' exists. Deleting" if Dory::Config.debug? self.delete end end |
#docker_installed? ⇒ Boolean
10 11 12 |
# File 'lib/dory/docker_service.rb', line 10 def docker_installed? Dory::DockerService.docker_installed? end |
#execute_run_command(handle_error:) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/dory/docker_service.rb', line 93 def execute_run_command(handle_error:) 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 Dory::Dinghy::DinghyError => e puts e..red end status end |
#handle_error(_command_output) ⇒ Object
24 25 26 27 |
# File 'lib/dory/docker_service.rb', line 24 def handle_error(_command_output) # Override to provide error handling return false end |
#ps(all: false) ⇒ Object
59 60 61 62 63 64 65 66 67 |
# File 'lib/dory/docker_service.rb', line 59 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_postconditions ⇒ Object
19 20 21 22 |
# File 'lib/dory/docker_service.rb', line 19 def run_postconditions # Override if postconditions are needed return true end |
#run_preconditions ⇒ Object
14 15 16 17 |
# File 'lib/dory/docker_service.rb', line 14 def run_preconditions # Override if preconditions are needed return true end |
#running?(container_name = self.container_name) ⇒ Boolean
50 51 52 53 |
# File 'lib/dory/docker_service.rb', line 50 def running?(container_name = self.container_name) return false unless docker_installed? !!(self.ps =~ /#{container_name}/) end |
#start(handle_error: true) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/dory/docker_service.rb', line 29 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.delete_container_if_exists self.run_preconditions self.execute_run_command(handle_error: handle_error) self.run_postconditions 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_cmd ⇒ Object
82 83 84 |
# File 'lib/dory/docker_service.rb', line 82 def start_cmd "docker start #{Shellwords.escape(self.container_name)}" end |
#stop(container_name = self.container_name) ⇒ Object
69 70 71 72 |
# File 'lib/dory/docker_service.rb', line 69 def stop(container_name = self.container_name) Sh.run_command("docker kill #{Shellwords.escape(container_name)}") if self.running? !self.running? end |