Class: Mcrain::Riak
Class Attribute Summary collapse
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods inherited from Base
#build_docker_command, #build_docker_command_options, #container_image, #container_name, #docker_extra_options, #find_portno, #logger, #port, #start, #url
Constructor Details
#initialize ⇒ Riak
Returns a new instance of Riak.
115
116
117
118
119
120
121
122
|
# File 'lib/mcrain/riak.rb', line 115
def initialize
w = @work_dir = Mcrain::Riak.docker_riak_path
raise "#{self.class.name}.docker_riak_path is blank. You have to set it to use the class" if w.blank?
raise "#{w}/Makefile not found" unless File.readable?(File.join(w, "Makefile"))
@prepare_cmd = Boot2docker.preparing_command
@automatic_clustering = false
@cluster_size = 1
end
|
Class Attribute Details
.docker_riak_path ⇒ Object
10
11
12
|
# File 'lib/mcrain/riak.rb', line 10
def docker_riak_path
@docker_riak_path
end
|
Instance Attribute Details
#admin_uris ⇒ Object
Returns the value of attribute admin_uris.
112
113
114
|
# File 'lib/mcrain/riak.rb', line 112
def admin_uris
@admin_uris
end
|
#automatic_clustering ⇒ Object
Returns the value of attribute automatic_clustering.
113
114
115
|
# File 'lib/mcrain/riak.rb', line 113
def automatic_clustering
@automatic_clustering
end
|
#cids ⇒ Object
Returns the value of attribute cids.
112
113
114
|
# File 'lib/mcrain/riak.rb', line 112
def cids
@cids
end
|
#cluster_size ⇒ Object
Returns the value of attribute cluster_size.
113
114
115
|
# File 'lib/mcrain/riak.rb', line 113
def cluster_size
@cluster_size
end
|
#host ⇒ Object
Returns the value of attribute host.
112
113
114
|
# File 'lib/mcrain/riak.rb', line 112
def host
@host
end
|
#pb_ports ⇒ Object
Returns the value of attribute pb_ports.
112
113
114
|
# File 'lib/mcrain/riak.rb', line 112
def pb_ports
@pb_ports
end
|
#uris ⇒ Object
Returns the value of attribute uris.
112
113
114
|
# File 'lib/mcrain/riak.rb', line 112
def uris
@uris
end
|
Instance Method Details
#build_client_options ⇒ Object
32
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/mcrain/riak.rb', line 32
def build_client_options
options = {
nodes: uris.map{|uri| {host: uri.host, pb_port: uri.port} }
}
if uri = uris.first
if !uri.user.blank? or !uri.password.blank?
options[:authentication] = {user: uri.user, password: uri.password}
end
end
options
end
|
#build_command ⇒ Object
124
125
126
|
# File 'lib/mcrain/riak.rb', line 124
def build_command
"DOCKER_RIAK_AUTOMATIC_CLUSTERING=#{automatic_clustering ? 1 : 0} DOCKER_RIAK_CLUSTER_SIZE=#{cluster_size} make start-cluster"
end
|
#build_uris ⇒ Object
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/mcrain/riak.rb', line 53
def build_uris
Boot2docker.setup_docker_options
uri = URI.parse(ENV["DOCKER_HOST"])
@host = (uri.scheme == "unix") ? "localhost" : uri.host
list = Docker::Container.all
riak_containers = list.select{|r| r.info['Image'] == "hectcastro/riak:latest"}
@cids = riak_containers.map(&:id)
@pb_ports = riak_containers.map do |r|
map = r.info['Ports'].each_with_object({}){|rr,d| d[ rr['PrivatePort'] ] = rr['PublicPort']}
map[8087]
end
@port = @pb_ports.first
@admin_uris = @cids.map do |cid|
r = Docker::Container.get(cid)
host = r.info["NetworkSettings"]["IPAddress"]
"ssh://root@#{host}:22"
end
@uris = @pb_ports.map do |port|
URI::Generic.build(scheme: "riak", host: @host, port: port)
end
end
|
#clear_old_container ⇒ Object
109
110
|
# File 'lib/mcrain/riak.rb', line 109
def clear_old_container
end
|
#client ⇒ Object
23
24
25
26
27
28
29
30
|
# File 'lib/mcrain/riak.rb', line 23
def client
unless @client
require client_require
build_uris
@client = ::Riak::Client.new(build_client_options)
end
@client
end
|
#client_require ⇒ Object
44
45
46
|
# File 'lib/mcrain/riak.rb', line 44
def client_require
'riak'
end
|
#client_script ⇒ Object
48
49
50
51
|
# File 'lib/mcrain/riak.rb', line 48
def client_script
client
"Riak::Client.new(#{build_client_options.inspect})"
end
|
#run_container ⇒ Object
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
# File 'lib/mcrain/riak.rb', line 128
def run_container
logger.debug("cd #{@work_dir.inspect}")
Dir.chdir(@work_dir) do
LoggerPipe.run(logger, "#{@prepare_cmd} #{build_command}")
sleep(1)
20.times do
begin
LoggerPipe.run(logger, "#{@prepare_cmd} make test-cluster")
sleep(45) return
rescue
sleep(0.5)
retry
end
end
raise "failed to run a riak server"
end
end
|
#stop ⇒ Object
148
149
150
151
152
|
# File 'lib/mcrain/riak.rb', line 148
def stop
Dir.chdir(@work_dir) do
LoggerPipe.run(logger, "#{@prepare_cmd} make stop-cluster")
end
end
|
#wait ⇒ Object
18
19
20
21
|
# File 'lib/mcrain/riak.rb', line 18
def wait
build_uris
super
end
|
#wait_for_ready ⇒ Object
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
# File 'lib/mcrain/riak.rb', line 82
def wait_for_ready
c = client
logger.debug("sending a ping")
r = c.ping
raise "Ping failure with #{c.inspect}" unless r
20.times do |i|
begin
logger.debug("get and store ##{i}")
o1 = c.bucket("test").get_or_new("foo")
o1.data = {"bar" => 100}
o1.store
o2 = c.bucket("test").get_or_new("foo")
raise "Something wrong!" unless o2.data == o1.data
break
rescue => e
if e.message =~ /Expected success from Riak but received 0/
sleep(0.5)
logger.debug("retrying [#{e.class}] #{e.message}")
retry
else
logger.warn(e)
raise
end
end
end
end
|