/
tagger.rb
335 lines (304 loc) · 10.1 KB
/
tagger.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# === Synopsis:
# RightScale Tagger (rs_tag) - (c) 2011 RightScale Inc
#
# Tagger allows listing, adding and removing tags on the current instance and
# querying for all instances with a given set of tags
#
# === Examples:
# Retrieve all tags:
# rs_tag --list
# rs_tag -l
#
# Add tag 'a_tag' to instance:
# rs_tag --add a_tag
# rs_tag -a a_tag
#
# Remove tag 'a_tag':
# rs_tag --remove a_tag
# rs_tag -r a_tag
#
# Retrieve all instances with all of the tags in a set:
# rs_tag --query "a_tag b_tag"
# rs_tag -q "a_tag b_tag"
#
# === Usage
# rs_tag (--list, -l | --add, -a TAG | --remove, -r TAG | --query, -q TAG_LIST)
#
# Options:
# --list, -l List current server tags
# --add, -a TAG Add tag named TAG
# --remove, -r TAG Remove tag named TAG
# --query, -q TAG_LIST Query for all instances that have any of the tags in TAG_LIST
# with the TAG_LIST being quoted if it contains spaces
# --die, -e Exit with error if query/list fails
# --format, -f FMT Output format: json, yaml, text
# --verbose, -v Display debug information
# --help: Display help
# --version: Display version information
# --timeout, -t Custom timeout parameter (default 120 sec)
#
require 'rubygems'
require 'optparse'
require 'right_agent'
require 'right_agent/scripts/usage'
require 'right_agent/scripts/common_parser'
module RightScale
class Tagger
TAG_REQUEST_TIMEOUT = 2 * 60 # synchronous tag requests need a long timeout
class TagError < Exception
attr_reader :code
def initialize(msg, code=nil)
super(msg)
@code = code || 1
end
end
# Manage instance tags
#
# === Parameters
# options(Hash):: Hash of options as defined in +parse_args+
#
# === Return
# true:: Always return true
def run(options)
if options[:verbose]
log = Logger.new(STDERR)
else
log = Logger.new(StringIO.new)
end
RightScale::Log.force_logger(log)
unless options.include?(:action)
STDERR.puts "Missing argument, rs_tag --help for additional information"
fail(1)
end
cmd = { :name => options[:action] }
cmd[:tag] = options[:tag] if options[:tag]
cmd[:tags] = options[:tags] if options[:tags]
cmd[:query] = options[:query] if options[:query]
config_options = AgentConfig.agent_options('instance')
listen_port = config_options[:listen_port]
raise ArgumentError.new('Could not retrieve agent listen port') unless listen_port
command_serializer = Serializer.new
client = CommandClient.new(listen_port, config_options[:cookie])
begin
@disposition = nil
client.send_command(cmd, options[:verbose], options[:timeout] || TAG_REQUEST_TIMEOUT) do |res|
begin
case options[:action]
when :get_tags
raise TagError.new("List current server tags failed: #{res.inspect}", 45) unless res.kind_of?(Array)
if res.empty?
if options[:die]
raise TagError.new('No server tags found', 44)
else
puts format_output([], options[:format])
@disposition = 0
end
else
puts format_output(res, options[:format])
@disposition = 0
end
when :query_tags
r = OperationResult.from_results(command_serializer.load(res))
raise TagError.new("Query tags failed: #{r.inspect}", 46) unless r.kind_of?(OperationResult)
if r.success?
if r.content.empty?
if options[:die]
raise TagError.new("No servers with tags #{options[:tags].inspect}", 44)
else
puts format_output({}, options[:format])
@disposition = 0
end
else
puts format_output(r.content, options[:format])
@disposition = 0
end
else
raise TagError.new("Query tags failed: #{r.content}", 53)
end
when :add_tag
r = OperationResult.from_results(command_serializer.load(res))
raise TagError.new("Add tag failed: #{r.inspect}", 47) unless r.kind_of?(OperationResult)
if r.success?
STDERR.puts "Successfully added tag #{options[:tag]}"
@disposition = 0
else
raise TagError.new("Add tag failed: #{r.content}", 54)
end
when :remove_tag
r = OperationResult.from_results(command_serializer.load(res))
raise TagError.new("Remove tag failed: #{r.inspect}", 48) unless r.kind_of?(OperationResult)
if r.success?
STDERR.puts "Successfully removed tag #{options[:tag]}"
@disposition = 0
else
raise TagError.new("Remove tag failed: #{r.content}", 55)
end
else
STDERR.puts res
@disposition = 0
end
rescue Exception => e
@disposition = e
end
end
rescue Exception => e
@disposition = e
end
Thread.pass while @disposition.nil?
case @disposition
when 0
succeed
else
fail(@disposition)
end
rescue SystemExit => e
raise e
rescue Exception => e
fail(e)
end
# Create options hash from command line arguments
#
# === Return
# options(Hash):: Hash of options as defined by the command line
def parse_args
options = { :verbose => false }
opts = OptionParser.new do |opts|
opts.on('-l', '--list') do
options[:action] = :get_tags
end
opts.on('-a', '--add TAG') do |t|
options[:action] = :add_tag
options[:tag] = t
end
opts.on('-r', '--remove TAG') do |t|
options[:action] = :remove_tag
options[:tag] = t
end
opts.on('-q', '--query TAG_LIST') do |t|
options[:action] = :query_tags
options[:tags] = t.split
end
opts.on('-v', '--verbose') do
options[:verbose] = true
end
opts.on('-e', '--die') do
options[:die] = true
end
opts.on('-f', '--format FMT') do |fmt|
options[:format] = fmt
end
opts.on('-t', '--timeout TIMEOUT') do |tmt|
options[:timeout] = tmt
end
end
opts.on_tail('--version') do
puts version
succeed
end
opts.on_tail('--help') do
puts Usage.scan(__FILE__)
succeed
end
begin
opts.parse!(ARGV)
rescue SystemExit => e
raise e
rescue Exception => e
STDERR.puts e.message + "\nUse rs_tag --help for additional information"
fail(1)
end
options
end
protected
# Format output for display to user
#
# === Parameter
# result(Object):: JSON-compatible data structure (array, hash, etc)
# format(String):: how to print output - json, yaml, text
#
# === Return
# a String containing the specified output format
def format_output(result, format)
case format
when /^jso?n?$/, nil
JSON.pretty_generate(result)
when /^ya?ml$/
YAML.dump(result)
when /^te?xt$/, /^sh(ell)?/, 'list'
result = result.keys if result.respond_to?(:keys)
result.join(" ")
else
raise ArgumentError, "Unknown output format #{format}"
end
end
# Exit with success.
#
# === Return
# R.I.P. does not return
def succeed
exit(0)
end
# Print error on console and exit abnormally
#
# === Parameter
# reason(Exception|String|Integer):: Exception, error message or numeric failure code
#
# === Options
# :print_usage(Boolean):: Whether script usage should be printed, default to false
#
# === Return
# R.I.P. does not return
def fail(reason=nil, options={})
case reason
when TagError
STDERR.puts reason.message
code = reason.code
when Errno::EACCES
STDERR.puts reason.message
STDERR.puts "Try elevating privilege (sudo/runas) before invoking this command."
code = 2
when Exception
STDERR.puts reason.message
code = 50
when String
STDERR.puts reason
code = 50
when Integer
code = reason
else
code = 1
end
puts Usage.scan(__FILE__) if options[:print_usage]
exit(code)
end
# Version information
#
# === Return
# (String):: Version information
def version
gemspec = eval(File.read(File.join(File.dirname(__FILE__), '..', 'right_link.gemspec')))
"rs_tag #{gemspec.version} - RightLink's tagger (c) 2011 RightScale"
end
end # Tagger
end # RightScale
#
# Copyright (c) 2011 RightScale Inc
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.