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
|
#
# Many leap_cli commands accept a list of filters to select a subset of nodes for the command to
# be applied to. This class is a helper for manager to run these filters.
#
# Classes other than Manager should not use this class.
#
# Filter rules:
#
# * A filter consists of a list of tokens
# * A token may be a service name, tag name, environment name, or node name.
# * Each token may be optionally prefixed with a plus sign.
# * Multiple tokens with a plus are treated as an OR condition,
# but treated as an AND condition with the plus sign.
#
# For example
#
# * openvpn +development => all nodes with service 'openvpn' AND environment 'development'
# * openvpn seattle => all nodes with service 'openvpn' OR tag 'seattle'.
#
# There can only be one environment specified. Typically, there are also tags
# for each environment name. These name are treated as environments, not tags.
#
module LeapCli
module Config
class Filter
#
# filter -- array of strings, each one a filter
# options -- hash, possible keys include
# :nopin -- disregard environment pinning
# :local -- if false, disallow local nodes
# :warning -- if false, don't print a warning when no nodes are found.
#
# A nil value in the filters array indicates
# the default environment. This is in order to support
# calls like `manager.filter(environments)`
#
def initialize(filters, options, manager)
@filters = filters.nil? ? [] : filters.dup
@environments = []
@options = options
@manager = manager
# split filters by pulling out items that happen
# to be environment names.
if LeapCli.leapfile.environment.nil? || @options[:nopin]
@environments = []
else
@environments = [LeapCli.leapfile.environment]
end
@filters.select! do |filter|
if filter.nil?
@environments << nil unless @environments.include?(nil)
false
else
filter_text = filter.sub(/^\+/,'')
if is_environment?(filter_text)
if filter_text == LeapCli.leapfile.environment
# silently ignore already pinned environments
elsif (filter =~ /^\+/ || @filters.first == filter) && !@environments.empty?
LeapCli::Util.bail! do
LeapCli.log "Environments are exclusive: no node is in two environments." do
LeapCli.log "Tried to filter on '#{@environments.join('\' AND \'')}' AND '#{filter_text}'"
end
end
else
@environments << filter_text
end
false
else
true
end
end
end
# don't let the first filter have a + prefix
if @filters[0] =~ /^\+/
@filters[0] = @filters[0][1..-1]
end
end
# actually run the filter, returns a filtered list of nodes
def nodes()
if @filters.empty?
return nodes_for_empty_filter
else
return nodes_for_filter
end
end
private
def nodes_for_empty_filter
node_list = @manager.nodes
if @environments.any?
node_list = node_list[ @environments.collect{|e|[:environment, env_to_filter(e)]} ]
end
if @options[:local] === false
node_list = node_list[:environment => '!local']
end
if @options[:disabled] === false
node_list = node_list[:environment => '!disabled']
end
node_list
end
def nodes_for_filter
node_list = Config::ObjectList.new
@filters.each do |filter|
if filter =~ /^\+/
keep_list = nodes_for_name(filter[1..-1])
node_list.delete_if do |name, node|
if keep_list[name]
false
else
true
end
end
else
node_list.merge!(nodes_for_name(filter))
end
end
node_list
end
private
#
# returns a set of nodes corresponding to a single name,
# where name could be a node name, service name, or tag name.
#
# For services and tags, we only include nodes for the
# environments that are active
#
def nodes_for_name(name)
if node = @manager.nodes[name]
return Config::ObjectList.new(node)
elsif @environments.empty?
if @manager.services[name]
return @manager.env('_all_').services[name].node_list
elsif @manager.tags[name]
return @manager.env('_all_').tags[name].node_list
elsif @options[:warning] != false
LeapCli.log :warning, "filter '#{name}' does not match any node names, tags, services, or environments."
return Config::ObjectList.new
else
return Config::ObjectList.new
end
else
node_list = Config::ObjectList.new
if @manager.services[name]
@environments.each do |env|
node_list.merge!(@manager.env(env).services[name].node_list)
end
elsif @manager.tags[name]
@environments.each do |env|
node_list.merge!(@manager.env(env).tags[name].node_list)
end
elsif @options[:warning] != false
LeapCli.log :warning, "filter '#{name}' does not match any node names, tags, services, or environments."
end
return node_list
end
end
#
# when pinning, we use the name 'default' to specify nodes
# without an environment set, but when filtering, we need to filter
# on :environment => nil.
#
def env_to_filter(environment)
environment == 'default' ? nil : environment
end
def is_environment?(text)
text == 'default' || @manager.environment_names.include?(text)
end
end
end
end
|