cap-rightscale 0.3.13 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.4.0
|
data/cap-rightscale.gemspec
CHANGED
@@ -74,13 +74,14 @@ module Capistrano
|
|
74
74
|
rescue => e
|
75
75
|
STDERR.puts("#{e.class}: #{e.pretty_inspect}")
|
76
76
|
warn("Backtrace:\n#{e.backtrace.pretty_inspect}")
|
77
|
-
|
77
|
+
raise
|
78
78
|
end
|
79
79
|
|
80
80
|
unless Tag.status_code == 200
|
81
|
-
|
81
|
+
message = "Errors: STATUS is NOT 200 OK"
|
82
|
+
STDERR.puts(message)
|
82
83
|
warn(Tag.headers)
|
83
|
-
|
84
|
+
raise message
|
84
85
|
end
|
85
86
|
tags
|
86
87
|
end
|
@@ -9,14 +9,6 @@ module Capistrano
|
|
9
9
|
attr_writer :validate_echo, :use_nickname, :use_public_ip, :use_rs_cache
|
10
10
|
attr_accessor :rs_cache_lifetime
|
11
11
|
|
12
|
-
def get_rs_instance
|
13
|
-
@rs_instance ||= Capistrano::RightScale::Resource.instance
|
14
|
-
end
|
15
|
-
|
16
|
-
def get_cache_instance
|
17
|
-
@cache_instance ||= Capistrano::RightScale::Cache.instance
|
18
|
-
end
|
19
|
-
|
20
12
|
def get_rs_confpath
|
21
13
|
get_rs_instance.confpath
|
22
14
|
end
|
@@ -61,24 +53,28 @@ module Capistrano
|
|
61
53
|
start = Time.now
|
62
54
|
logger.info("SETTING ROLE: #{role}")
|
63
55
|
|
64
|
-
# Set rightscale's parameters
|
65
|
-
_array_id = params[:array_id]
|
66
|
-
params.delete(:array_id) # remove rightscale's parameters
|
67
|
-
|
68
56
|
host_list = use_rs_cache ? get_cache_instance.load_server_cache(role, @caller) : [] # Get cache
|
69
57
|
|
70
58
|
if host_list && host_list.size > 0
|
59
|
+
[:array_id, :except_tags].each {|key| params.delete(key)} # remove rightscale's parameters
|
71
60
|
logger.info("restore cache of servers:\n#{host_list.pretty_inspect}")
|
72
61
|
role(role, params) { host_list } # set cache to role()
|
73
62
|
else
|
74
63
|
# Request RightScale API
|
75
|
-
array = get_rs_instance.array(
|
64
|
+
array = get_rs_instance.array(params[:array_id])
|
76
65
|
logger.info("querying rightscale for server_array #{array.nickname}...")
|
77
66
|
dept = get_rs_instance.deployment(array.deployment_href.match(/[0-9]+$/).to_s, :server_settings => 'true')
|
78
67
|
deployment_name = dept.nickname
|
79
68
|
logger.info("Deployment #{deployment_name}:")
|
69
|
+
srvs = get_rs_instance.array_instances(array.id).select {|i| i[:state] == "operational"}
|
70
|
+
|
71
|
+
if params.include?(:except_tags)
|
72
|
+
except_tags_params = {:resource_type => "ec2_instance", :tags => [params[:except_tags]]}
|
73
|
+
srvs = servers_with_tags_set(params[:deployment], srvs, except_tags_params, :minus)
|
74
|
+
return [] if srvs.size == 0 # Not found servers matching tag
|
75
|
+
end
|
80
76
|
|
81
|
-
host_list =
|
77
|
+
host_list = srvs.map do |instance|
|
82
78
|
hostname = instance[:nickname].sub(/ #[0-9]+$/, "-%03d" % instance[:nickname].match(/[0-9]+$/).to_s.to_i)
|
83
79
|
hostname << ".#{domainname}" if domainname && hostname.match(/#{domainname}/).nil?
|
84
80
|
ip = use_public_ip ? instance[:ip_address] : instance[:private_ip_address]
|
@@ -89,6 +85,7 @@ start = Time.now
|
|
89
85
|
host_list = RSUtils.valid_echo(host_list, logger) if validate_echo
|
90
86
|
|
91
87
|
if host_list && host_list.size > 0
|
88
|
+
[:array_id, :except_tags].each {|key| params.delete(key)} # remove rightscale's parameters
|
92
89
|
role(role, params) { host_list }
|
93
90
|
get_cache_instance.dump_server_cache(role, host_list, @caller) if use_rs_cache # Dump cache
|
94
91
|
end
|
@@ -114,27 +111,28 @@ puts "Time: #{Time.now - start}"
|
|
114
111
|
start = Time.now
|
115
112
|
logger.info("SETTING ROLE: #{role}")
|
116
113
|
|
117
|
-
# Set rightscale's parameters
|
118
|
-
_dept_id = params[:deployment]
|
119
|
-
_name_prefix = params[:name_prefix]
|
120
|
-
|
121
|
-
params.delete(:deployment)
|
122
|
-
params.delete(:name_prefix) if params.has_key?(:name_prefix)
|
123
|
-
|
124
114
|
host_list = use_rs_cache ? get_cache_instance.load_server_cache(role, @caller) : [] # Get cache
|
125
115
|
|
126
|
-
if host_list
|
116
|
+
if host_list.size > 0
|
117
|
+
[:deployment, :name_prefix, :except_tags].each {|key| params.delete(key)} # remove rightscale's parameters
|
127
118
|
logger.info("restore cache of servers:\n#{host_list.pretty_inspect}")
|
128
119
|
role(role, params) { host_list } # set cache to role()
|
129
120
|
else
|
121
|
+
|
130
122
|
# Request RightScale API
|
131
|
-
dept = get_rs_instance.deployment(
|
132
|
-
logger.info("querying rightscale for servers #{
|
123
|
+
dept = get_rs_instance.deployment(params[:deployment], :server_settings => 'true')
|
124
|
+
logger.info("querying rightscale for servers #{params[:name_prefix]} in deployment #{dept.nickname}...")
|
133
125
|
srvs = dept.servers.select {|s| s[:state] == "operational"}
|
134
|
-
srvs = srvs.select {|s| /#{
|
126
|
+
srvs = srvs.select {|s| /#{params[:name_prefix]}/ =~ s[:nickname]} if params[:name_prefix]
|
127
|
+
|
128
|
+
if params.include?(:except_tags)
|
129
|
+
except_tags_params = {:resource_type => "ec2_instance", :tags => [params[:except_tags]]}
|
130
|
+
srvs = servers_with_tags_set(params[:deployment], srvs, except_tags_params, :minus)
|
131
|
+
return [] if srvs.size == 0 # Not found servers matching tag
|
132
|
+
end
|
135
133
|
|
136
134
|
host_list = srvs.map do |server|
|
137
|
-
hostname = server[:nickname]
|
135
|
+
hostname = server[:nickname].sub(/ #[0-9]+$/, "-%03d" % server[:nickname].match(/[0-9]+$/).to_s.to_i)
|
138
136
|
hostname << ".#{domainname}" if domainname && hostname.match(/#{domainname}/).nil?
|
139
137
|
ip = use_public_ip ? server[:settings][:ip_address] : server[:settings][:private_ip_address]
|
140
138
|
|
@@ -144,6 +142,7 @@ start = Time.now
|
|
144
142
|
host_list = RSUtils.valid_echo(host_list, logger) if validate_echo
|
145
143
|
|
146
144
|
if host_list && host_list.size > 0
|
145
|
+
[:array_id, :except_tags].each {|key| params.delete(key)} # remove rightscale's parameters
|
147
146
|
role(role, params) { host_list }
|
148
147
|
get_cache_instance.dump_server_cache(role, host_list, @caller) if use_rs_cache # Dump cache
|
149
148
|
end
|
@@ -170,47 +169,41 @@ puts "Time: #{Time.now - start}"
|
|
170
169
|
start = Time.now
|
171
170
|
logger.info("SETTING ROLE: #{role}")
|
172
171
|
|
173
|
-
# Set rightscale's parameters
|
174
|
-
_dept_id = params[:deployment]
|
175
|
-
_tags = params[:tags]
|
176
|
-
|
177
|
-
params.delete(:deployment)
|
178
|
-
params.delete(:tags)
|
179
|
-
|
180
172
|
host_list = use_rs_cache ? get_cache_instance.load_server_cache(role, @caller) : [] # Get cache
|
181
173
|
|
182
174
|
if host_list && host_list.size > 0
|
175
|
+
[:deployment, :tags, :except_tags].each {|key| params.delete(key)} # remove rightscale's parameters
|
183
176
|
logger.info("restore cache of servers:\n#{host_list.pretty_inspect}")
|
184
177
|
role(role, params) { host_list } # set cache to role()
|
185
178
|
else
|
186
179
|
# Request RightScale API
|
187
|
-
dept = get_rs_instance.
|
188
|
-
logger.info("querying rightscale for servers matching tags #{
|
180
|
+
dept = get_rs_instance.deployment(params[:deployment], :server_settings => 'true')
|
181
|
+
logger.info("querying rightscale for servers matching tags #{params[:tags]} in deployment #{dept.nickname}...")
|
189
182
|
srvs = dept.servers.select {|s| s[:state] == "operational"}
|
190
183
|
|
191
|
-
ts_params = {:resource_type => "ec2_instance", :tags => [
|
192
|
-
|
193
|
-
|
194
|
-
select {|s| s.deployment_href.match(/[0-9]+$/).to_s == _dept_id.to_s}
|
184
|
+
ts_params = {:resource_type => "ec2_instance", :tags => [params[:tags]]}
|
185
|
+
srvs = servers_with_tags_set(params[:deployment], srvs, ts_params, :intersect)
|
186
|
+
return [] if srvs.size == 0 # Not found servers matching tag
|
195
187
|
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
end
|
210
|
-
host_list = RSUtils.valid_echo(host_list, logger) if validate_echo
|
188
|
+
if params.include?(:except_tags)
|
189
|
+
except_tags_params = {:resource_type => "ec2_instance", :tags => [params[:except_tags]]}
|
190
|
+
srvs = servers_with_tags_set(params[:deployment], srvs, except_tags_params, :minus)
|
191
|
+
return [] if srvs.size == 0 # Not found servers matching tag
|
192
|
+
end
|
193
|
+
|
194
|
+
host_list = srvs.map do |server|
|
195
|
+
hostname = server[:nickname]
|
196
|
+
hostname << ".#{domainname}" if domainname && hostname.match(/#{domainname}/).nil?
|
197
|
+
ip = use_public_ip ? server[:settings][:ip_address] : server[:settings][:private_ip_address]
|
198
|
+
|
199
|
+
logger.info("Found server: #{hostname}(#{ip})")
|
200
|
+
use_nickname ? hostname : ip
|
211
201
|
end
|
212
202
|
|
203
|
+
host_list = RSUtils.valid_echo(host_list, logger) if validate_echo
|
204
|
+
|
213
205
|
if host_list && host_list.size > 0
|
206
|
+
[:deployment, :tags, :except_tags].each {|key| params.delete(key)} # remove rightscale's parameters
|
214
207
|
role(role, params) { host_list }
|
215
208
|
get_cache_instance.dump_server_cache(role, host_list, @caller) if use_rs_cache # Dump cache
|
216
209
|
end
|
@@ -226,6 +219,53 @@ puts "Time: #{Time.now - start}"
|
|
226
219
|
return true
|
227
220
|
end
|
228
221
|
|
222
|
+
def get_rs_instance
|
223
|
+
@rs_instance ||= Capistrano::RightScale::Resource.instance
|
224
|
+
end
|
225
|
+
|
226
|
+
def get_cache_instance
|
227
|
+
@cache_instance ||= Capistrano::RightScale::Cache.instance
|
228
|
+
end
|
229
|
+
|
230
|
+
# set(union, intersect, minus) servers in deployment and servers matching tags in deployment
|
231
|
+
def servers_with_tags_set(deployment_id, servers, tags_params, operator)
|
232
|
+
servers_ids = servers.map {|s| s[:href].match(/[0-9]+$/).to_s}
|
233
|
+
|
234
|
+
ts = servers_with_tags_in_deployment(deployment_id, tags_params)
|
235
|
+
return [] if ts.size == 0
|
236
|
+
|
237
|
+
ts_ids = ts.map {|s| s.href.sub("/current", "").match(/[0-9]+$/).to_s}
|
238
|
+
case operator
|
239
|
+
when :intersect then
|
240
|
+
oper_ids = servers_ids & ts_ids
|
241
|
+
when :minus then
|
242
|
+
oper_ids = servers_ids - ts_ids
|
243
|
+
end
|
244
|
+
return [] if oper_ids.size == 0
|
245
|
+
|
246
|
+
servers.select {|s| oper_ids.include?(s[:href].match(/[0-9]+$/).to_s)} || []
|
247
|
+
end
|
248
|
+
|
249
|
+
def intersect_servers_with_tags(deployment, servers, tags_params, tags_api)
|
250
|
+
ts = tags_api.call
|
251
|
+
return [] if ts.size == 0
|
252
|
+
|
253
|
+
ts_ids = ts.map {|s| s.href.sub("/current", "").match(/[0-9]+$/).to_s}
|
254
|
+
intersect_ids = servers_ids & ts_ids
|
255
|
+
intersect_ids.size > 0 ? intersect_ids : []
|
256
|
+
end
|
257
|
+
|
258
|
+
def servers_with_tags_in_deployment(deployment_id, params)
|
259
|
+
begin
|
260
|
+
servers = get_rs_instance.tag(params).
|
261
|
+
select {|s| s.state == "operational"}.
|
262
|
+
select {|s| s.deployment_href.match(/[0-9]+$/).to_s == deployment_id.to_s}
|
263
|
+
rescue => e
|
264
|
+
{}
|
265
|
+
end
|
266
|
+
servers
|
267
|
+
end
|
268
|
+
|
229
269
|
def validate_echo
|
230
270
|
@validate_echo ||= false
|
231
271
|
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:
|
4
|
+
hash: 15
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 4
|
9
|
+
- 0
|
10
|
+
version: 0.4.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Satoshi Ohki
|