forked from byxorna/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcf
executable file
·94 lines (79 loc) · 3.07 KB
/
cf
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
#!/usr/bin/env tumblr_ruby
# stands for collins-find
# look up hosts quickly from collins from the CLI
require 'collins_client'
require 'yaml'
require 'optparse'
options = {
:collins_config_file => "/var/db/collins.yaml",
:query_size => 9999,
:separator => "\t",
:tags => [],
:timeout => 10 # default in collins_client library
}
default_tags = [:tag, :hostname, :nodeclass, :status, :pool, :primary_role, :secondary_role]
selector_keys = [:nodeclass, :pool, :hostname, :primary_role,
:secondary_role, :status, :hardware_product]
OptionParser.new do |opts|
opts.banner = "Usage: #{$0} [options] [hostpattern]"
opts.on('-c','--config FILE',String,
"Specify collins config (Default: #{options[:collins_config_file]})"
) {|v| options[:collins_config_file] = v}
opts.on('-n','--nodeclass NODECLASS',String,
"Specify a nodeclass to search for"
) {|v| options[:nodeclass] = v}
opts.on('-p','--pool POOL',String,
"Specify a pool to search for") {|v| options[:pool] = v}
opts.on('-s','--size SIZE',Integer,
"Number of assets to return") {|v| options[:query_size] = v}
opts.on('-r','--role ROLE',String,"Role of machine"
) {|v| options[:primary_role] = v}
opts.on('-R','--secondary-role ROLE',String,"Secondary Role of machine"
) {|v| options[:secondary_role] = v}
opts.on('-H','--hardware-product PRODUCT',String,"Formal product model"
) {|v| options[:hardware_product] = v}
opts.on('-S','--status STATUS',String,"Asset Status"
) {|v| options[:status] = v}
opts.on('-T','--tag-separator TAGS',String,"Separator between tags in output"
) {|v| options[:separator] = v}
opts.on('-t','--tags TAGS',String,"Tags to output (Default: #{default_tags.map(&:to_s).inspect})"
) {|v| options[:tags] << v}
opts.on('-x','--expire SECONDS',Integer,"Query timeout in seconds"
) {|v| options[:timeout] = v}
opts.on('-h','--help',"Help") {puts opts ; exit 0}
end.parse!
# hostname is the final option, no flags
options[:hostname] = ARGV.shift
# if nothing passed to us, lets not search for EVERYTHING
abort "You need to query for _something_, see --help" if
selector_keys.all? {|key| options[key].nil?}
options[:tags] = default_tags if options[:tags].empty?
c = YAML.load_file(options[:collins_config_file]).reduce({}){|memo,(k,v)|memo[k.to_sym] = v ; memo}
c = c[:collins] if c.key? :collins
c[:host] = "https://collins.#{c[:environment]}.tumblr.net"
c[:timeout] = options[:timeout]
# useful for debugging timout, but not otherwise
# c[:trace] = true
options[:collins_config] = c
collins = Collins::Client.new options[:collins_config]
query_opts = {
:operation => 'AND',
:size => options[:query_size]
}
selector_keys.each {|key|
query_opts[key] = options[key] if options[key]
}
assets = collins.find(query_opts)
if assets.length > 0
assets.each {|a| puts options[:tags].map {|t|
if t == "state"
a.send(t).label
else
a.send(t)
end
}.join(options[:separator])
}
else
$stderr.puts "No assets found"
exit 1
end