Class: Mcrain::Base
- Inherits:
-
Object
- Object
- Mcrain::Base
- Defined in:
- lib/mcrain/base.rb
Class Attribute Summary collapse
-
.container_image ⇒ Object
Returns the value of attribute container_image.
-
.port ⇒ Object
Returns the value of attribute port.
- .server_name ⇒ Object
Instance Method Summary collapse
- #build_docker_command ⇒ Object
- #build_docker_command_options ⇒ Object
- #clear_old_container ⇒ Object
- #client ⇒ Object
- #client_require ⇒ Object
- #client_script ⇒ Object
- #container_image ⇒ Object
- #container_name ⇒ Object
- #docker_extra_options ⇒ Object
- #find_portno ⇒ Object
- #host ⇒ Object
- #logger ⇒ Object
- #port ⇒ Object
- #run_container ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
- #url ⇒ Object
- #wait ⇒ Object
- #wait_for_ready ⇒ Object
Class Attribute Details
.container_image ⇒ Object
Returns the value of attribute container_image.
18 19 20 |
# File 'lib/mcrain/base.rb', line 18 def container_image @container_image end |
.port ⇒ Object
Returns the value of attribute port.
18 19 20 |
# File 'lib/mcrain/base.rb', line 18 def port @port end |
.server_name ⇒ Object
14 15 16 |
# File 'lib/mcrain/base.rb', line 14 def server_name @server_name ||= self.name.split(/::/).last.underscore.to_sym end |
Instance Method Details
#build_docker_command ⇒ Object
76 77 78 |
# File 'lib/mcrain/base.rb', line 76 def build_docker_command "docker run #{build_docker_command_options} #{container_image}" end |
#build_docker_command_options ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/mcrain/base.rb', line 80 def r = "-d -p #{port}:#{self.class.port} --name #{container_name}" if ext = r << ext end r end |
#clear_old_container ⇒ Object
65 66 67 68 69 |
# File 'lib/mcrain/base.rb', line 65 def clear_old_container LoggerPipe.run(logger, "docker rm #{container_name}", timeout: 10) rescue => e logger.warn("[#{e.class}] #{e.message}") end |
#client ⇒ Object
112 113 114 |
# File 'lib/mcrain/base.rb', line 112 def client raise NotImplementedError end |
#client_require ⇒ Object
116 117 118 |
# File 'lib/mcrain/base.rb', line 116 def client_require raise NotImplementedError end |
#client_script ⇒ Object
120 121 122 |
# File 'lib/mcrain/base.rb', line 120 def client_script raise NotImplementedError end |
#container_image ⇒ Object
21 22 23 |
# File 'lib/mcrain/base.rb', line 21 def container_image self.class.container_image or raise "No container_image for #{self.class.name}" end |
#container_name ⇒ Object
25 26 27 |
# File 'lib/mcrain/base.rb', line 25 def container_name "test-#{self.class.server_name}" end |
#docker_extra_options ⇒ Object
88 89 90 |
# File 'lib/mcrain/base.rb', line 88 def nil end |
#find_portno ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/mcrain/base.rb', line 33 def find_portno # 未使用のポートをシステムに割り当てさせてすぐ閉じてそれを利用する tmpserv = TCPServer.new(0) portno = tmpserv.local_address.ip_port tmpserv.close portno end |
#host ⇒ Object
29 30 31 |
# File 'lib/mcrain/base.rb', line 29 def host @host ||= URI.parse(ENV["DOCKER_HOST"] || "tcp://localhost").host end |
#logger ⇒ Object
128 129 130 |
# File 'lib/mcrain/base.rb', line 128 def logger Mcrain.logger end |
#port ⇒ Object
41 42 43 |
# File 'lib/mcrain/base.rb', line 41 def port @port ||= find_portno end |
#run_container ⇒ Object
71 72 73 74 |
# File 'lib/mcrain/base.rb', line 71 def run_container s = LoggerPipe.run(logger, build_docker_command, timeout: 10) end |
#start ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/mcrain/base.rb', line 49 def start clear_old_container run_container if block_given? begin wait return yield(self) ensure stop end else wait return self end end |
#stop ⇒ Object
124 125 126 |
# File 'lib/mcrain/base.rb', line 124 def stop LoggerPipe.run(logger, "docker kill #{container_name}", timeout: 10) end |
#url ⇒ Object
45 46 47 |
# File 'lib/mcrain/base.rb', line 45 def url @url ||= "#{self.class.server_name}://#{host}:#{port}" end |
#wait ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/mcrain/base.rb', line 92 def wait # ポートがLISTENされるまで待つ Mcrain.wait_port_opened(host, port, interval: 0.5, timeout: 30) # ポートはdockerがまずLISTENしておいて、その後コンテナ内のredisが起動するので、 # 実際にAPIを叩いてみて例外が起きないことを確認します。 Timeout.timeout(30) do begin wait_for_ready rescue => e # $stderr.puts "[#{e.class}] #{e.message}" sleep(1) retry end end end |
#wait_for_ready ⇒ Object
108 109 110 |
# File 'lib/mcrain/base.rb', line 108 def wait_for_ready raise NotImplementedError end |