Class: Mcrain::Base

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

Direct Known Subclasses

Rabbitmq, Redis, Riak

Class Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.container_imageObject

Returns the value of attribute container_image.



18
19
20
# File 'lib/mcrain/base.rb', line 18

def container_image
  @container_image
end

.portObject

Returns the value of attribute port.



18
19
20
# File 'lib/mcrain/base.rb', line 18

def port
  @port
end

.server_nameObject



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_commandObject



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_optionsObject



80
81
82
83
84
85
86
# File 'lib/mcrain/base.rb', line 80

def build_docker_command_options
  r = "-d -p #{port}:#{self.class.port} --name #{container_name}"
  if ext = docker_extra_options
    r << ext
  end
  r
end

#clear_old_containerObject



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

#clientObject

Raises:

  • (NotImplementedError)


112
113
114
# File 'lib/mcrain/base.rb', line 112

def client
  raise NotImplementedError
end

#client_requireObject

Raises:

  • (NotImplementedError)


116
117
118
# File 'lib/mcrain/base.rb', line 116

def client_require
  raise NotImplementedError
end

#client_scriptObject

Raises:

  • (NotImplementedError)


120
121
122
# File 'lib/mcrain/base.rb', line 120

def client_script
  raise NotImplementedError
end

#container_imageObject



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_nameObject



25
26
27
# File 'lib/mcrain/base.rb', line 25

def container_name
  "test-#{self.class.server_name}"
end

#docker_extra_optionsObject



88
89
90
# File 'lib/mcrain/base.rb', line 88

def docker_extra_options
  nil
end

#find_portnoObject



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

#hostObject



29
30
31
# File 'lib/mcrain/base.rb', line 29

def host
  @host ||= URI.parse(ENV["DOCKER_HOST"] || "tcp://localhost").host
end

#loggerObject



128
129
130
# File 'lib/mcrain/base.rb', line 128

def logger
  Mcrain.logger
end

#portObject



41
42
43
# File 'lib/mcrain/base.rb', line 41

def port
  @port ||= find_portno
end

#run_containerObject



71
72
73
74
# File 'lib/mcrain/base.rb', line 71

def run_container
  s = LoggerPipe.run(logger, build_docker_command, timeout: 10)

end

#startObject



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

#stopObject



124
125
126
# File 'lib/mcrain/base.rb', line 124

def stop
  LoggerPipe.run(logger, "docker kill #{container_name}", timeout: 10)
end

#urlObject



45
46
47
# File 'lib/mcrain/base.rb', line 45

def url
  @url ||= "#{self.class.server_name}://#{host}:#{port}"
end

#waitObject



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_readyObject

Raises:

  • (NotImplementedError)


108
109
110
# File 'lib/mcrain/base.rb', line 108

def wait_for_ready
  raise NotImplementedError
end