Repository navigation
Expand file tree
/
Copy pathloader.rb
More file actions
402 lines (369 loc) · 13.2 KB
/
Copy pathloader.rb
File metadata and controls
402 lines (369 loc) · 13.2 KB
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
def merge_yaml(first, second)
if first.nil?
return second
end
# user can overwrite defaults
if second.nil?
return nil
end
if second.is_a?(Array) && second[0].nil?
if !first.is_a?(Array) && second.length == 2
return second[1]
end
return second[1..-1]
end
if first.is_a?(Array)
if second.is_a?(Array)
result = first + second
else
result = first.clone
result << second
end
result = result.uniq
return result
end
if first.is_a?(Hash)
if second.is_a?(Hash)
result = first.clone
second.each do |key, value|
if result.has_key?(key)
if value.nil?
result.delete(key)
else
new_value = merge_yaml(result[key], value)
result[key] = new_value
end
else
result[key] = value
end
end
return result;
end
# already a value, assume flattened hash
if first.has_value?(second)
return first
end
if second.is_a?(String)
first[second] = nil
return first
end
STDERR.puts "Can't insert #{second.class} '#{second}' into hash #{first}"
return first
end
return second
end
def merge_settings(yaml, user_yaml)
# a hash
$settings = yaml["settings"]
$settings = {} if $settings.nil?
user_settings = user_yaml["settings"] unless user_yaml.nil?
$settings = merge_yaml($settings, user_settings) unless user_settings.nil?
home_share = $settings["home_share"] || $HOME
home_share = home_share.gsub("~", "$HOME")
home_share = expand_env(home_share)
ENV["HOME_SHARE"] = home_share
$settings["defaults"] = {} if $settings["defaults"].nil?
yaml["settings"] = $settings
end
def merge_urls(yaml, user_yaml)
# a hash of arrays
$urls = yaml["urls"]
user_urls = user_yaml["urls"] unless user_yaml.nil?
$urls = merge_yaml($urls, user_urls) unless user_urls.nil?
yaml["urls"] = $urls
end
def merge_machines(yaml, user_yaml)
machines = yaml["machines"]
user_machines = user_yaml["machines"] unless user_yaml.nil?
if user_machines.nil?
user_machines = []
elsif user_machines.length > 0 && user_machines.first.nil?
machines = []
end
user_machines.each do |user_machine|
next if user_machine.nil?
idx = machines.find_index {|m| m["name"] == user_machine["name"] }
unless idx.nil?
new_machine = merge_yaml(machines[idx], user_machine)
machines[idx] = new_machine
else
machines << user_machine
end
end
machines.each do |machine|
machine_name = machine["name"]
defaults = $settings["defaults"].dup
$settings.each do |machine_exp, machine_settings|
next if machine_settings.nil?
if machine_exp.start_with?("/") && machine_exp.end_with?("/") && machine_name.match?(machine_exp[1..-2])
defaults = merge_yaml(defaults, machine_settings)
end
end
(defaults || {}).each do |setting, value|
# make deep copies
default_value = value.dup
if machine[setting].nil?
machine[setting] = default_value
else
machine[setting] = merge_yaml(default_value, machine[setting])
end
end
end
yaml["machines"] = machines
end
def load_includes(yaml, basedir)
includes = yaml["includes"] || yaml["Include"] unless yaml.nil?
unless includes.nil?
includes.each do |include|
include_file = File.join(basedir, include)
begin
include_yaml = YAML.load_file(include_file)
include_yaml = load_includes(include_yaml, File.dirname(include_file))
include_yaml.each do |section, data|
yaml[section] = merge_yaml(yaml[section], data) unless section == "includes"
end
rescue => error
STDERR.puts "Error loading file #{include_file}: #{error}"
end
end
end
return yaml
end
def combine_roles(machines)
# post processing of roles: if a role is defined multiple times, combine them
machines.each do |machine|
roles = machine["roles"]
unless roles.nil?
role_set = []
role_indices = {}
index = 0
roles.each do |role|
if role.is_a?(Hash)
rolename = role["role"]
else
rolename = role
end
if rolename == "script"
role_set << role
index += 1
elsif role_indices.has_key?(rolename)
oldindex = role_indices[rolename]
# special case: merge all install roles; for that to work, one of the packages
# values must be an array, otherwise we end up with multiple script provisioners
# called "install" and vagrant will only run one of them
if rolename == "install"
old_packages = role_set[oldindex]["packages"]
role_set[oldindex]["packages"] = [ old_packages ] unless old_packages.is_a?(Array)
end
role_set[oldindex] = merge_yaml(role_set[oldindex], role)
else
role_indices[rolename] = index
role_set << role
index += 1
end
end
role_set.each do |role|
machine["roles"] = role_set
end
end
end
end
def extend_machines(machines)
machines.each do |machine|
index = machines.rindex {|m| m["name"] == machine["name"] && m != machine}
unless index.nil?
duplicate = machines[index]
duplicate.each do |key, value|
machine[key] = merge_yaml(machine[key], value)
end
machines.delete_at(index)
end
end
machines.each do |machine|
basename = machine["extends"]
unless basename.nil?
basemachine = machines.select{|m| m["name"] == basename}.first
if basemachine.nil?
STDERR.puts "#{machine["name"]} extends #{basename}, which does not exist!"
else
basemachine.each do |key, value|
machine_value = machine[key]
if !machine.has_key?(key)
machine[key] = value
elsif key != "name"
machine[key] = merge_yaml(value, machine_value)
end
end
machine.delete("extends")
end
end
end
end
def detect_os(machines)
machines.each do |machine|
name = machine["name"]
box = machine["box"]
if machine["os"].nil?
if ((name =~ /windows/i) || (box =~ /windows/i)) != nil
machine["os"] = "windows"
elsif ((name =~ /mac/i) || (box =~ /mac/i)) != nil
machine["os"] = "macos"
else
machine["os"] = "linux"
end
end
end
end
def find_config(root, config_name)
while !Dir.exist?(File.join(root, config_name)) do
old_root = root
root = File.dirname(root)
if File.identical?(root, old_root)
root = nil
break
end
end
root = nil unless root.nil? || Dir.exists?(root)
return root
end
module Minicoin
class Context
attr_accessor :machines
attr_accessor :variables
def initialize(machines)
@variables = {}
@all_machines = machines.dup
@machine_rx = Regexp.new('^\/machines/(\d+)\/$')
@current_machine = nil
end
def if_eval(expr)
@variables[:machine] = @all_machines[@current_machine] if @current_machine
if_context = binding
variables.each do |name, value|
if value.is_a?(Hash)
variable = Object.new
value.each do |k, v|
next if k == "name" # breaks ruby in vagrant 2.3?
variable.class.class_eval { attr_reader k.to_sym}
variable.instance_variable_set("@#{k}", v)
end
if_context.local_variable_set(name.to_sym, variable)
else
if_context.local_variable_set(name.to_sym, value)
end
end
if_context.eval(expr)
end
def preprocess(data, path = "/")
machine_match = @machine_rx.match(path)
if machine_match
@current_machine = machine_match[1].to_i
elsif !path.start_with?('/machine')
@current_machine = nil
end
return true unless data.is_a?(Hash)
data.each do |key, value|
if key == "if"
Dir.chdir($MINICOIN_PROJECT_DIR) do
begin
exp = if_eval(value)
return [exp, "#{value} => #{exp}"] if !exp
# if-conditions that evaluate to true can be removed
data.delete(key)
rescue => error
reason = "Error in expression #{value}:\n\t#{error}"
STDERR.puts reason
return [false, reason]
end
end
elsif value.is_a?(Hash)
exp, reason = preprocess(value, path + "#{key}/")
data.delete(key) if !exp
elsif value.is_a?(Array)
pre = value.dup
value.clear
pre.each_with_index do |entry, index|
exp, reason = preprocess(entry, path + "#{key}/#{index}/")
if !exp
# we don't want to remove machines that don't meet all requirements
# so that they are listed with the failing condition
if "#{path}#{key}" == "/machines" && entry.is_a?(Hash)
entry[:disabled] = reason
value << entry
end
else
value << entry
end
end
end
end
true
end
end
end
def load_minicoin()
begin # see tests/autotest.rb
load_testmachines()
rescue NoMethodError => error # Not running autotest, continue
rescue => error
raise
end
global_file = File.join($PWD, 'minicoin.yml')
yaml = YAML.load_file(global_file)
yaml = load_includes(yaml, $PWD)
user_file = File.join($MINICOIN_USER_DIR, 'minicoin.yml')
user_yaml = nil
if File.file?(user_file)
user_yaml = YAML.load_file(user_file)
end
user_yaml = load_includes(user_yaml, $MINICOIN_USER_DIR) || {}
Dir["#{$MINICOIN_USER_DIR}/machines/**/*.yml"].each do |machineFile|
machineFileData = YAML.load_file(machineFile)
user_yaml["machines"] = [] if user_yaml["machines"].nil?
user_yaml["machines"] << machineFileData
end
local_yaml = nil
if $MINICOIN_PROJECT_DIR
project_dir = find_config($MINICOIN_PROJECT_DIR, ".minicoin")
if project_dir && project_dir != $PWD && project_dir != File.basename($MINICOIN_USER_DIR)
local_file = File.join(project_dir, '.minicoin/minicoin.yml')
if File.file?(local_file)
local_yaml = YAML.load_file(local_file)
end
end
end
local_yaml = load_includes(local_yaml, project_dir)
merge_settings(yaml, user_yaml)
merge_settings(yaml, local_yaml)
merge_urls(yaml, user_yaml)
merge_urls(yaml, local_yaml)
Vagrant::Plugin::V2::Plugin.manager.registered.each do |plugin|
merge_machines(yaml, plugin.load_minicoin) if plugin.respond_to?(:load_minicoin)
end
merge_machines(yaml, user_yaml)
merge_machines(yaml, local_yaml)
machines = yaml["machines"]
# inheritance after override resolution
extend_machines(machines)
# role merging once each box is fully defined
combine_roles(machines)
# attributes from the environment override everything else
if ENV["MINICOIN"]
environment_yaml = ENV["MINICOIN"]
begin
envyaml = YAML.load(environment_yaml)
machines.each do |machine|
envyaml.each do |env_key, env_value|
machine[env_key] = env_value
end
end
rescue
STDERR.puts "Settings from MINICOIN environment are not valid yaml"
end
end
detect_os(machines)
context = Minicoin::Context.new(machines)
context.preprocess(yaml)
$TEST_OUTPUT=yaml
return machines
end