Class: Mcrain::Base

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

Direct Known Subclasses

Hbase, Mysql, Rabbitmq, Redis, Riak

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ClientProvider

#build_client, #client, #client_class, #client_init_args, #client_instantiation_script, #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

Constructor Details

#initialize(attrs = {}) ⇒ Base

Returns a new instance of Base.



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

def initialize(attrs = {})
  attrs.each do |key, value|
    send("#{key}=", value)
  end
end

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

Class Method Details

.work_dirObject



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

def work_dir
  File.join(Mcrain.home_dir, server_name.to_s)
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



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

def setup
  logger.info("#{self.class.name}#setup STARTED")
  return false if Mcrain.before_setup && !Mcrain.before_setup.call(self)
  Timeout.timeout(30) do
    DockerMachine.setup_docker_options
    container.start!
  end
  logger.info("#{self.class.name}#setup COMPLETED")
  return container
end

#start(&block) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/mcrain/base.rb', line 33

def start(&block)
  r = setup
  return nil unless r
  if block_given?
    start_callback(&block)
  else
    wait
  end
  return self
end

#start_callbackObject



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/mcrain/base.rb', line 55

def start_callback
  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
end

#stop_or_kill_and_removeObject



99
100
101
102
103
104
105
106
# File 'lib/mcrain/base.rb', line 99

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

#teardownObject



94
95
96
97
# File 'lib/mcrain/base.rb', line 94

def teardown
  stop_or_kill_and_remove
  reset unless skip_reset_after_teardown
end

#waitObject

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



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/mcrain/base.rb', line 76

def wait
  logger.info("#{self.class.name}#wait STARTED")
  Timeout.timeout(60) do
    begin
      wait_for_ready
    rescue => e
      $stderr.puts "[#{e.class}] #{e.message}"
      sleep(1)
      retry
    end
  end
  logger.info("#{self.class.name}#wait COMPLETED")
end

#wait_for_readyObject

Raises:

  • (NotImplementedError)


90
91
92
# File 'lib/mcrain/base.rb', line 90

def wait_for_ready
  raise NotImplementedError
end

#wait_portObject

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



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

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