cap-rightscale 0.2.5 → 0.2.6

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.5
1
+ 0.2.6
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{cap-rightscale}
8
- s.version = "0.2.5"
8
+ s.version = "0.2.6"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Satoshi Ohki"]
@@ -32,6 +32,7 @@ Gem::Specification.new do |s|
32
32
  "lib/cap-rightscale.rb",
33
33
  "lib/cap-rightscale/configuration.rb",
34
34
  "lib/cap-rightscale/configuration/rightscale.rb",
35
+ "lib/cap-rightscale/configuration/rightscale/resource.rb",
35
36
  "lib/cap-rightscale/recipes.rb",
36
37
  "lib/cap-rightscale/recipes/rightscale.rb",
37
38
  "lib/cap-rightscale/recipes/rightscale/cache.rb",
@@ -1,17 +1,23 @@
1
1
  require 'cap-rightscale/utils/rs_utils.rb'
2
+ require 'cap-rightscale/configuration/rightscale/resource'
2
3
  require 'ping'
4
+ require 'thread'
3
5
 
4
6
  module Capistrano
5
7
  class Configuration
6
8
  module RightScale
7
9
  attr_writer :validate_echo, :use_nickname, :use_public_ip, :use_rs_cache
8
10
 
11
+ def get_rs_instance
12
+ @rs_instance ||= Capistrano::RightScale::Resource.instance
13
+ end
14
+
9
15
  def get_rs_confpath
10
- @rs_confpath ||= File.join(ENV['HOME'], ".rsconf", "rsapiconfig.yml")
16
+ get_rs_instance.confpath
11
17
  end
12
18
 
13
19
  def set_rs_confpath(path)
14
- @rs_confpath = path
20
+ get_rs_instance.confpath = path
15
21
  end
16
22
 
17
23
  def rs_enable(*args)
@@ -62,24 +68,33 @@ start = Time.now
62
68
  role(role, params) { host_list } # set cache to role()
63
69
  else
64
70
  # Request RightScale API
65
- array = rs_array(_array_id)
71
+ array = get_rs_instance.__send__(:array, _array_id)
66
72
  logger.info("querying rightscale for server_array #{array.nickname}...")
67
- dept = rs_deployment(array.deployment_href.match(/[0-9]+$/).to_s, :server_settings => 'true')
73
+ dept = get_rs_instance.__send__(:deployment, array.deployment_href.match(/[0-9]+$/).to_s, :server_settings => 'true')
68
74
  deployment_name = dept.nickname
69
75
  logger.info("Deployment #{deployment_name}:")
70
76
 
71
- host_list = rs_array_instances(array.id).select {|i| i[:state] == "operational"}.map do |instance|
77
+ host_list = get_rs_instance.__send__(:array_instances, array.id).select {|i| i[:state] == "operational"}.map do |instance|
72
78
  hostname = instance[:nickname].sub(/ #[0-9]+$/, "-%03d" % instance[:nickname].match(/[0-9]+$/).to_s.to_i)
73
79
  hostname << ".#{_domain}" if _domain
74
80
  ip = use_public_ip ? instance[:ip_address] : instance[:private_ip_address]
75
- if validate_echo
76
- next unless Ping.pingecho(ip)
77
- end
78
81
 
79
82
  logger.info("Found server: #{hostname}(#{ip})")
80
83
  use_nickname ? hostname : ip
81
84
  end
82
- host_list.delete(nil)
85
+
86
+ if validate_echo
87
+ threads = []
88
+ host_list.each do |host|
89
+ puts host
90
+ threads << Thread.new {Ping.pingecho(host)}
91
+ end
92
+ threads.each.with_index do |t,i|
93
+ host_list[i] = nil if t.value == false
94
+ end
95
+ threads.clear
96
+ host_list.delete(nil)
97
+ end
83
98
 
84
99
  if host_list && host_list.size > 0
85
100
  role(role, params) { host_list }
@@ -121,23 +136,32 @@ start = Time.now
121
136
  role(role, params) { host_list } # set cache to role()
122
137
  else
123
138
  # Request RightScale API
124
- dept = rs_deployment(_dept_id, :server_settings => 'true')
139
+ dept = get_rs_instance.__send__(:deployment, _dept_id, :server_settings => 'true')
125
140
  logger.info("querying rightscale for servers #{_name_prefix} in deployment #{dept.nickname}...")
126
- srvs = dept.servers.select {|s| s[:state] == "operational"}
141
+ # srvs = dept.servers.select {|s| s[:state] == "operational"}
142
+ srvs = dept.servers
127
143
  srvs = srvs.select {|s| /#{_name_prefix}/ =~ s[:nickname]} if _name_prefix
128
144
 
129
145
  host_list = srvs.map do |server|
130
146
  hostname = server[:nickname]
131
147
  hostname << ".#{_domain}" if _domain
132
148
  ip = use_public_ip ? server[:settings][:ip_address] : server[:settings][:private_ip_address]
133
- if validate_echo
134
- next unless Ping.pingecho(ip)
135
- end
136
149
 
137
150
  logger.info("Found server: #{hostname}(#{ip})")
138
151
  use_nickname ? hostname : ip
139
152
  end
140
- host_list.delete(nil)
153
+
154
+ if validate_echo
155
+ threads = []
156
+ host_list.each do |host|
157
+ threads << Thread.new {Ping.pingecho(host)}
158
+ end
159
+ threads.each.with_index do |t,i|
160
+ host_list[i] = nil unless t.value
161
+ end
162
+ threads.clear
163
+ host_list.delete(nil)
164
+ end
141
165
 
142
166
  if host_list && host_list.size > 0
143
167
  role(role, params) { host_list }
@@ -180,12 +204,12 @@ start = Time.now
180
204
  role(role, params) { host_list } # set cache to role()
181
205
  else
182
206
  # Request RightScale API
183
- dept = rs_deployment(_dept_id, :server_settings => 'true')
207
+ dept = get_rs_instance.__send__(:deployment, _dept_id, :server_settings => 'true')
184
208
  logger.info("querying rightscale for servers matching tags #{_tags} in deployment #{dept.nickname}...")
185
209
  srvs = dept.servers.select {|s| s[:state] == "operational"}
186
210
 
187
211
  ts_params = {:resource_type => "ec2_instance", :tags => [_tags]}
188
- ts = Tag.search(ts_params).
212
+ ts = get_rs_instance.__send__(:tag, ts_params).
189
213
  select {|s| s.state == "operational"}.
190
214
  select {|s| s.deployment_href.match(/[0-9]+$/).to_s == _dept_id.to_s}
191
215
 
@@ -225,100 +249,6 @@ puts "Time: #{Time.now - start}"
225
249
  return true
226
250
  end
227
251
 
228
- def rs_array(id, params={})
229
- array = self.instance_variable_get("@array_#{id}")
230
-
231
- unless array
232
- connect
233
- begin
234
- self.instance_variable_set("@array_#{id}", ServerArray.show(id, params))
235
- rescue => e
236
- STDERR.puts("#{e.class}: #{e.pretty_inspect}")
237
- warn("Backtrace:\n#{e.backtrace.pretty_inspect}")
238
- exit(1)
239
- end
240
-
241
- unless ServerArray.status_code == 200
242
- STDERR.puts("Errors: STATUS is NOT 200 OK")
243
- warn(ServerArray.headers)
244
- exit(1)
245
- end
246
-
247
- array = self.instance_variable_get("@array_#{id}")
248
- end
249
- array
250
- end
251
-
252
- def rs_array_instances(id)
253
- array_instances = self.instance_variable_get("@array_instances_#{id}")
254
-
255
- unless array_instances
256
- connect
257
- begin
258
- self.instance_variable_set("@array_instances_#{id}", ServerArray.instances(id))
259
- rescue => e
260
- STDERR.puts("#{e.class}: #{e.pretty_inspect}")
261
- warn("Backtrace:\n#{e.backtrace.pretty_inspect}")
262
- exit(1)
263
- end
264
-
265
- unless ServerArray.status_code == 200
266
- STDERR.puts("Errors: STATUS is NOT 200 OK")
267
- warn(ServerArray.headers)
268
- exit(1)
269
- end
270
-
271
- array_instances = self.instance_variable_get("@array_instances_#{id}")
272
- end
273
- array_instances
274
- end
275
-
276
- def rs_deployment(id, params={})
277
- dept = self.instance_variable_get("@deployment_#{id}")
278
-
279
- unless dept
280
- connect
281
- begin
282
- self.instance_variable_set("@deployment_#{id}", Deployment.show(id, params))
283
- rescue => e
284
- STDERR.puts("#{e.class}: #{e.pretty_inspect}")
285
- warn("Backtrace:\n#{e.backtrace.pretty_inspect}")
286
- exit(1)
287
- end
288
-
289
- unless Deployment.status_code == 200
290
- STDERR.puts("Errors: STATUS is NOT 200 OK")
291
- warn(Deployment.headers)
292
- exit(1)
293
- end
294
-
295
- dept = self.instance_variable_get("@deployment_#{id}")
296
- end
297
- dept
298
- end
299
-
300
- def connect
301
- begin
302
- @auth ||= open(get_rs_confpath) {|f| YAML.load(f)}
303
- @conn ||= RightResource::Connection.new do |c|
304
- c.login(:username => @auth["username"], :password => @auth["password"], :account => @auth["account"])
305
- end
306
- rescue => e
307
- auth_data = open(File.join(File.expand_path(File.dirname(__FILE__)), '/../../../rsapiconfig.yml.sample')) {|f| f.read}
308
- STDERR.puts <<-"USAGE"
309
- Cannot load RightScale Auth data!!:
310
- Put authfile:<rsapiconfig.yml> in <HOME>/.rsconf/
311
- OR
312
- Set param: set_rs_confpath <authfile_path>
313
-
314
- Authfile contents:
315
- #{auth_data}
316
- USAGE
317
- exit(1)
318
- end
319
- RightResource::Base.connection = @conn
320
- end
321
-
322
252
  def get_server_cache(role)
323
253
  @lifetime ||= 86400
324
254
  @server_cache ||= {}
@@ -0,0 +1,83 @@
1
+ require 'singleton'
2
+
3
+ module Capistrano
4
+ module RightScale
5
+ class Resource
6
+ include Singleton
7
+
8
+ attr_accessor :confpath
9
+ attr_reader :array, :array_instances, :deployment
10
+ def initialize
11
+ @confpath = File.join(ENV['HOME'], ".rsconf", "rsapiconfig.yml")
12
+ end
13
+
14
+ def connect
15
+ begin
16
+ @auth ||= open(confpath) {|f| YAML.load(f)}
17
+ @conn ||= RightResource::Connection.new do |c|
18
+ c.login(:username => @auth["username"], :password => @auth["password"], :account => @auth["account"])
19
+ end
20
+ rescue => e
21
+ auth_data = open(File.join(File.expand_path(File.dirname(__FILE__)), '/../../../../rsapiconfig.yml.sample')) {|f| f.read}
22
+ STDERR.puts <<-"USAGE"
23
+ Cannot load RightScale Auth data!!:
24
+ Put authfile:<rsapiconfig.yml> in <HOME>/.rsconf/
25
+ OR
26
+ Set param: set_rs_confpath <authfile_path>
27
+
28
+ Authfile contents:
29
+ #{auth_data}
30
+ USAGE
31
+ exit(1)
32
+ end
33
+ RightResource::Base.connection = @conn
34
+ end
35
+
36
+ def _instance_variable_set(method, id, api)
37
+ connect
38
+ begin
39
+ self.instance_variable_set("@#{method}_#{id}", api.call)
40
+ rescue => e
41
+ STDERR.puts("#{e.class}: #{e.pretty_inspect}")
42
+ warn("Backtrace:\n#{e.backtrace.pretty_inspect}")
43
+ exit(1)
44
+ end
45
+
46
+ unless RightResource::Base.status_code == 200
47
+ STDERR.puts("Errors: STATUS is NOT 200 OK")
48
+ warn(RightResource::Base.headers)
49
+ exit(1)
50
+ end
51
+
52
+ data = self.instance_variable_get("@#{method}_#{id}")
53
+ data
54
+ end
55
+
56
+ def array(id)
57
+ _instance_variable_set(:array, id, lambda {ServerArray.show(id)}) unless self.instance_variable_get("@array_#{id}")
58
+ self.instance_variable_get("@array_#{id}")
59
+ end
60
+
61
+ def array_instances(id)
62
+ _instance_variable_set(:array_instances, id, lambda {ServerArray.instances(id)}) unless self.instance_variable_get("@array_instances_#{id}")
63
+ self.instance_variable_get("@array_instances_#{id}")
64
+ end
65
+
66
+ def deployment(id, params)
67
+ _instance_variable_set(:deployment, id, lambda {Deployment.show(id, params)}) unless self.instance_variable_get("@deployment_#{id}")
68
+ self.instance_variable_get("@deployment_#{id}")
69
+ end
70
+
71
+ def tag(params)
72
+ tags = Tag.search(params) # not stored
73
+
74
+ unless Tag.status_code == 200
75
+ STDERR.puts("Errors: STATUS is NOT 200 OK")
76
+ warn(Tag.headers)
77
+ exit(1)
78
+ end
79
+ tags
80
+ end
81
+ end
82
+ end
83
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cap-rightscale
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 5
10
- version: 0.2.5
9
+ - 6
10
+ version: 0.2.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Satoshi Ohki
@@ -150,6 +150,7 @@ files:
150
150
  - lib/cap-rightscale.rb
151
151
  - lib/cap-rightscale/configuration.rb
152
152
  - lib/cap-rightscale/configuration/rightscale.rb
153
+ - lib/cap-rightscale/configuration/rightscale/resource.rb
153
154
  - lib/cap-rightscale/recipes.rb
154
155
  - lib/cap-rightscale/recipes/rightscale.rb
155
156
  - lib/cap-rightscale/recipes/rightscale/cache.rb