-
Notifications
You must be signed in to change notification settings - Fork 103
/
setup-2.1.1.rb
219 lines (197 loc) · 8.07 KB
/
setup-2.1.1.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
81
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#
# Cookbook Name:: couchbase
#
# Copyright RightScale, Inc. All rights reserved. All access and use subject to the
# RightScale Terms of Service available at http://www.rightscale.com/terms.php and,
# if applicable, other agreements such as a RightScale Master Subscription Agreement.
rightscale_marker :begin
couchbase_edition = node[:db_couchbase][:edition]
couchbase_version = "2.1.1"
couchbase_package = value_for_platform(
["centos", "redhat", "suse", "fedora" ] => {
"default" => "couchbase-server-#{couchbase_edition}_x86_64_#{couchbase_version}.rpm"
},
["ubuntu", "debian"] => {
"default" => "couchbase-server-#{couchbase_edition}_x86_64_#{couchbase_version}.deb"
}
)
log "downloading #{couchbase_package}"
if not File.exists?("/tmp/#{couchbase_package}")
remote_file "/tmp/#{couchbase_package}" do
source "http://packages.couchbase.com/releases/#{couchbase_version}/#{couchbase_package}"
mode "0644"
end
end
log "installing #{couchbase_package}"
package "couchbase-server" do
source "/tmp/#{couchbase_package}"
if platform?("redhat", "centos", "suse", "fedora")
provider Chef::Provider::Package::Rpm
action :install
elsif platform?("ubuntu", "debian")
provider Chef::Provider::Package::Dpkg
action :install
else
log "unsupported source package #{couchbase_package}"
abort "unsupported source package #{couchbase_package}"
end
end
log "configuring #{couchbase_package}"
begin
unless (node[:block_device].nil? or
node[:block_device][:devices].nil? or
node[:block_device][:devices][:device1].nil? or
node[:block_device][:devices][:device1][:mount_point].nil?)
mount_point = node[:block_device][:devices][:device1][:mount_point]
execute "configure data path to use mount point: #{mount_point}" do
command("sleep 30 && chown couchbase:couchbase #{mount_point} &&" +
" /opt/couchbase/bin/couchbase-cli node-init" +
" --node-init-data-path=#{mount_point}" +
" -c 127.0.0.1:8091" +
" -u=#{node[:db_couchbase][:cluster][:username]}" +
" -password=#{node[:db_couchbase][:cluster][:password]}")
action :run
end
end
rescue Exception => e
log e
end
log("/opt/couchbase/bin/couchbase-cli cluster-init" +
" -c 127.0.0.1:8091" +
" --cluster-init-username=#{node[:db_couchbase][:cluster][:username]}")
execute "initializing cluster with username: #{node[:db_couchbase][:cluster][:username]}" do
command("sleep 30" +
" && /opt/couchbase/bin/couchbase-cli cluster-init" +
" -c 127.0.0.1:8091" +
" --cluster-init-username=#{node[:db_couchbase][:cluster][:username]}" +
" --cluster-init-password=#{node[:db_couchbase][:cluster][:password]}")
action :run
end
# cluster the nodes based on cluster tag
cluster_tag = node[:db_couchbase][:cluster][:tag]
log("db_couchbase/cluster/tag: #{cluster_tag}")
known_hosts = ""
if cluster_tag and !cluster_tag.empty?
now = DateTime.now.strftime("%Y%m%d-%H%M%S.%L")
log("clustering - now is #{now}")
unless `which rs_tag`.empty?
ip = node[:cloud][:private_ips][0]
if ip
log("clustering - private ip is #{ip}")
add_cmd = "rs_tag -a couchbase_cluster_tag:#{cluster_tag}=#{now}:#{ip}:couchbase"
log("clustering - rs_tag add cmd: #{add_cmd}")
add_res = `#{add_cmd}`
log("clustering - rs_tag add res: #{add_res}")
qry_cmd = "rs_tag -q couchbase_cluster_tag:#{cluster_tag}"
log("clustering - rs_tag qry cmd: #{qry_cmd}")
qry_res = `#{qry_cmd}`
log("clustering - rs_tag qry res: #{qry_res}")
username = node[:db_couchbase][:cluster][:username]
password = node[:db_couchbase][:cluster][:password]
cmd = "/opt/couchbase/bin/couchbase-cli server-list" +
" -c 127.0.0.1" +
" -u #{username}" +
" -p #{password} 2>\&1"
log("clustering - server-list cmd: #{cmd}")
known_hosts = `#{cmd}`.strip
log("clustering - server-list res: #{known_hosts}")
failed_cmd = "/etc/init.d/couchbase-server stop"
unless known_hosts.match(/^ERROR:/)
if known_hosts.split("\n").length <= 1
cmd = "rs_tag -q couchbase_cluster_tag:#{cluster_tag}" +
" | grep couchbase_cluster_tag:#{cluster_tag}=" +
" | grep -v :#{ip}:couchbase" +
" | cut -d '=' -f 2 | cut -d '\"' -f 1 | sort | cut -d ':' -f 2"
log("clustering - rs_tag private_ip qry: #{cmd}")
private_ips = `#{cmd}`.strip.split("\n")
log("clustering - rs_tag private_ip res: #{private_ips}")
if private_ips.length >= 1
add = "sleep 30 && /opt/couchbase/bin/couchbase-cli server-add" +
" -c #{private_ips[0]}" +
" -u #{username}" +
" -p #{password}" +
" --server-add=#{ip}" +
" --server-add-username=#{username}" +
" --server-add-password=#{password} 2>\&1"
cmd = "for i in {1..5}; do x=\`#{add}\`; if [[ -z \"$x\" ]]; then break; else sleep 30 && echo 'retrying...'$i; fi; done"
begin
log("clustering - server-add cmd: #{cmd}")
execute "clustering - server-add cmd: #{cmd}" do
command(cmd)
action :run
end
log("clustering - server added")
rescue Exception => e
log e
execute "stopping couchbase server cmd: #{failed_cmd}" do
command(failed_cmd)
action :run
end
end
else
log("clustering - no other servers to join")
end
else
log("clustering - already joined")
end
else
log("clustering - error: could not retrieve server-list")
end
else
log("clustering - error: no cloud private ip")
end
else
log("clustering - error: could not find rs_tag")
end
else
log("clustering - skipped, no cluster_tag")
end
# rebalance if certain conditions been met
rebalance_count = node[:db_couchbase][:cluster][:rebalance_count]
num_nodes = known_hosts.split("\n").length
if num_nodes >= rebalance_count.to_i
log("rebalancing: (rebalance_count = #{rebalance_count}) > (num_nodes = #{num_nodes}")
log("/opt/couchbase/bin/couchbase-cli rebalance" +
" -u #{node[:db_couchbase][:cluster][:username]}" +
" -p #{node[:db_couchbase][:cluster][:username]}" +
" -c localhost:8091")
begin
execute "rebalance cluster" do
ommmand("/opt/couchbase/bin/couchbase-cli rebalance" +
" -u #{node[:db_couchbase][:cluster][:username]}" +
" -p #{node[:db_couchbase][:cluster][:username]}" +
" -c localhost:8091")
action :run
end
rescue Exception => e
log e
end
else
log("exiting due to insufficient amount of known nodes")
exit(0)
end
# create bucket
log("sleep 30 && /opt/couchbase/bin/couchbase-cli bucket-create" +
" -c 127.0.0.1:8091" +
" -u #{node[:db_couchbase][:cluster][:username]}" +
" --bucket=#{node[:db_couchbase][:bucket][:name]}" +
" --bucket-type=couchbase" +
" --bucket-ramsize=#{node[:db_couchbase][:bucket][:ram]}" +
" --bucket-replica=#{node[:db_couchbase][:bucket][:replica]}")
begin
execute "creating bucket: #{node[:db_couchbase][:bucket][:name]}" do
command("sleep 30 && /opt/couchbase/bin/couchbase-cli bucket-create" +
" -c 127.0.0.1:8091" +
" -u #{node[:db_couchbase][:cluster][:username]}" +
" -p #{node[:db_couchbase][:cluster][:password]}" +
" --bucket=#{node[:db_couchbase][:bucket][:name]}" +
" --bucket-type=couchbase" +
" --bucket-password=\"#{node[:db_couchbase][:bucket][:password]}\"" +
" --bucket-ramsize=#{node[:db_couchbase][:bucket][:ram]}" +
" --bucket-replica=#{node[:db_couchbase][:bucket][:replica]}")
action :run
end
rescue Exception => e
log e
end
rightscale_marker :end