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
|
# File 'lib/chef_metal_ssh/machine_registry.rb', line 81
def match_machine_options_to_registered(ssh_cluster_path, machine_options)
ssh_cluster_machines = File.join(ssh_cluster_path, "*.json")
Dir.glob(ssh_cluster_machines).sort.each do |registered_machine_file|
matched_machine_json = false unless matched_machine_json
ip_address_match = false unless ip_address_match
mac_address_match = false unless mac_address_match
fqdn_match = false unless fqdn_match
hostname_match = false unless hostname_match
node_name_match = false unless node_name_match
will_work = false
not_gonna_work = false
available_registered_machine = true unless (available_registered_machine == false)
registered_machine_json = JSON.parse(File.read(registered_machine_file))
ip_address_match = (registered_machine_json['ip_address'] == machine_options['ip_address']) mac_address_match = (registered_machine_json['mac_address'] == machine_options['mac_address']) fqdn_match = (registered_machine_json['fqdn'] == machine_options['fqdn']) hostname_match = (registered_machine_json['hostname'] == machine_options['hostname'])
registered_machine_json.each_pair do |k,v|
if k == "available"
available_registered_machine =
registered_machine_is_available?(v) if available_registered_machine
elsif k == "node_name"
if v == machine_options[k] && (!v.nil? || !v.empty?)
node_name_match = true
end
else
if machine_options.has_key?(k)
case v
when String
if v == machine_options[k] && !v.empty?
will_work = true
else
not_gonna_work = true unless (v.empty? ||
machine_options[k].empty? ||
k == "password" )
end
when Array
Array(machine_options[k]).each do |sv|
if v.include?(sv)
will_work = true
else
not_gonna_work = true
end
end
when Hash
else
Chef::Log.debug "NOTHING?"
end
end
end
end
if (will_work == true) && (not_gonna_work == false) && (available_registered_machine == true)
matched_machine_json = true
end
error_out = false unless error_out
error_message = 'We Matched' unless error_message
if ip_address_match
error_out = true unless available_registered_machine
error_message << ' IP,'
end
if fqdn_match
error_out = true unless available_registered_machine
error_message << ' FQDN,'
end
if mac_address_match
error_out = true unless available_registered_machine
error_message << ' MAC ADDRESS,'
end
if hostname_match
error_out = true unless available_registered_machine
error_message << ' HOSTNAME,'
end
if node_name_match
error_out = true unless available_registered_machine
error_message << ' NODE NAME,'
end
if error_out && !available_registered_machine
error_message << ' but they already exist.'
error_message << ' Aborting to avoid inconsistencies.'
raise error_message
end
if matched_machine_json
stripped_machine_json = JSON.parse(machine_options.to_json).delete_if {
|k, v| v.empty? unless k == 'machine_types' }
new_registration_json = registered_machine_json.merge!(stripped_machine_json)
set_available_to_false = { "available" => "false" }
@matched_machine_json = new_registration_json.merge!(JSON.parse(set_available_to_false.to_json))
return @matched_machine_json
break
else
@matched_machine_json = false
end
end
return @matched_machine_json
end
|