Class: Mcrain::Base

Inherits:
Object
  • Object
show all
Includes:
ClientProvider, ContainerController
Defined in:
lib/mcrain/base.rb

Direct Known Subclasses

Mysql, Rabbitmq, Redis, Riak

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ClientProvider

#build_client, #client, #client_class, #client_init_args, #client_require, #client_script

Methods included from ContainerController

#add_volume_options, #build_docker_options, #container, #container_image, #find_portno, #host, included, #info, #ip, #name, #port, #ssh_uri, #url

Instance Attribute Details

#skip_reset_after_teardownObject

Returns the value of attribute skip_reset_after_teardown.



20
21
22
# File 'lib/mcrain/base.rb', line 20

def skip_reset_after_teardown
  @skip_reset_after_teardown
end

Instance Method Details

#loggerObject



16
17
18
# File 'lib/mcrain/base.rb', line 16

def logger
  Mcrain.logger
end

#resetObject



21
22
23
24
25
# File 'lib/mcrain/base.rb', line 21

def reset
  instance_variables.each do |var|
    instance_variable_set(var, nil)
  end
end

#setupObject



47
48
49
50
51
52
53
# File 'lib/mcrain/base.rb', line 47

def setup
  Timeout.timeout(30) do
    Boot2docker.setup_docker_options
    container.start!
  end
  return container
end

#startObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/mcrain/base.rb', line 27

def start
  setup
  if block_given?
    begin
      wait_port
      wait
      return yield(self)
    rescue Exception => e
      logs = container.logs(stdout: 1, stderr: 1)
      logger.error("[#{e.class.name}] #{e.message}\nthe container logs...\n#{logs}")
      raise e
    ensure
      teardown
    end
  else
    wait
    return self
  end
end

#stop_or_kill_and_removeObject



83
84
85
86
87
88
89
90
# File 'lib/mcrain/base.rb', line 83

def stop_or_kill_and_remove
  begin
    container.stop!
  rescue => e
    container.kill!
  end
  container.remove unless ENV['MCRAIN_KEEP_CONTAINERS'] =~ /true|yes|on|1/i
end

#teardownObject



78
79
80
81
# File 'lib/mcrain/base.rb', line 78

def teardown
  stop_or_kill_and_remove
  reset unless skip_reset_after_teardown
end

#waitObject

ポートはdockerがまずLISTENしておいて、その後コンテナ内のミドルウェアが起動するので、実際にそのAPIを叩いてみて例外が起きないことを確認します。



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/mcrain/base.rb', line 62

def wait
  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)


74
75
76
# File 'lib/mcrain/base.rb', line 74

def wait_for_ready
  raise NotImplementedError
end

#wait_portObject

ポートがLISTENされるまで待つ



56
57
58
# File 'lib/mcrain/base.rb', line 56

def wait_port
  Mcrain.wait_port_opened(host, port, interval: 0.5, timeout: 30)
end