This repository has been archived by the owner on Jul 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Rakefile
541 lines (447 loc) · 15.1 KB
/
Rakefile
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
require "docker"
require "mogilefs"
require "pathname"
require "rake/packagetask"
require "rake/tasklib"
require "toml"
# noop.
class CargoBuildTask < Rake::TaskLib
attr_accessor :name, :project, :binary, :lib, :release, :sources
def initialize(name, project, binary: nil, lib: false, release: false, sources: project.sources)
self.name = name
self.project = project
self.binary = binary
self.lib = lib
self.release = release
self.sources = sources
define
end
def define
CargoBuildTask::Task.define_task(project, binary, lib, release, sources, name, [ :verbose ] => sources) do |t, args|
args.with_defaults(:verbose => false)
project.cd_dir
build(args.verbose)
project.cd_root
end
end
class Task < Rake::Task
attr_accessor :project, :binary, :lib, :release, :sources
def self.define_task(project, binary, lib, release, sources, *args, &block)
Rake.application.define_task(self, *args, &block).tap do |task|
task.project = project
task.binary = binary
task.lib = lib
task.release = release
task.sources = sources
task.add_description(task.description)
end
end
def description
"Build #{building_what} in #{project.name} -- #{profile}"
end
def building_what
if binary
binary
elsif lib
"lib#{project.name}.rlib"
else
"all"
end
end
def timestamp
sources.map { |f| File.mtime(f) rescue Time.at(0) }.max
end
def profile
release ? "release" : "debug"
end
def output_timestamp
target_profile_path = project.target_path(full: true).join(profile)
if binary
File.mtime(target_profile_path.join(binary)) rescue Time.at(0)
elsif lib
File.mtime(target_profile_path.join("lib#{project.name}.rlib")) rescue Time.at(0)
elsif !target_profile_path.exist?
Time.at(0)
else
min_time = target_profile_path.children
.select { |c| c.exist? && c.file? }
.map { |c| c.mtime }
.min
min_time || Time.at(0)
end
end
def needed?
timestamp > output_timestamp
end
end
def build(verbose)
cmdline = [ "cargo", "build" ]
if binary
cmdline += [ "--bin", binary ]
end
# if package
# cmdline += [ "-p", package ]
# end
if lib
cmdline << "--lib"
end
if release
cmdline << "--release"
end
if verbose
cmdline << "--verbose"
end
sh(*cmdline)
end
end
class RustProjectTask < Rake::TaskLib
@@projects = {}
def self.[](*args)
@@projects.send(:[], *args)
end
def self.[]=(*args)
@@projects.send(:[]=, *args)
end
attr_accessor :path
def initialize(path = ".")
self.path = Pathname.new(path)
@should_define = RustProjectTask[path].nil?
RustProjectTask[path] = self
# Look for the subprojects before defining tasks.
subprojects
if @should_define
define_build_tasks
define_test_tasks
define_clean_tasks
end
end
def root_path
@root_path ||= Pathname.new(__FILE__).dirname.expand_path
end
def full_path
@full_path ||= root_path.join(path).expand_path
end
def relative_path
@relative_path = full_path.relative_path_from(root_path)
end
def in_subpath?
if @in_subpath.nil?
@in_subpath = root_path != full_path
end
@in_subpath
end
def cargo_toml_path(full: true)
if full
@full_cargo_toml_path ||= full_path.join("Cargo.toml")
else
@relative_cargo_toml_path ||= relative_path.join("Cargo.toml")
end
end
def src_path(full: false)
if full
@full_src_path ||= full_path.join("src")
else
@relative_src_path ||= relative_path.join("src")
end
end
def target_path(full: false)
if full
@full_target_path ||= full_path.join("target")
else
@relative_target_path ||= relative_path.join("target")
end
end
def sources
@sources ||= FileList[
relative_path.join("Cargo.*").to_s,
src_path.join("**", "*.rs").to_s,
].exclude { |f| !File.exist?(f) }
end
def recursive_sources
if @recursive_sources.nil?
@recursive_sources = sources.dup
subprojects.each do |p|
@recursive_sources += p.recursive_sources
end
end
@recursive_sources
end
def binary_sources
@binary_sources ||= FileList[
src_path.join("main.rs").to_s,
src_path.join("bin", "*.rs").to_s,
].exclude { |f| !File.exist?(f) }
end
def lib_sources
@lib_sources ||= FileList[
src_path.join("lib.rs").to_s
].exclude { |f| !File.exist?(f) }
end
def cargo_data
@cargo_data ||= TOML.load_file(cargo_toml_path)
end
def name
@name ||= cargo_data["package"]["name"]
end
def subprojects
@subprojects ||= cargo_data["dependencies"].map do |name, opts|
if opts.is_a?(Hash) && opts["path"]
RustProjectTask.new(relative_path.join(opts["path"]).to_s)
end
end.compact
end
def cd_dir
cd full_path.to_s if in_subpath?
end
def cd_root
cd root_path.to_s if in_subpath?
end
def do_in_dir
cd_dir
yield
cd_root
end
def project_namespace
if in_subpath?
string_as_task(relative_path.to_s)
else
string_as_task(name)
end
end
def string_as_task(str)
str.gsub(/[^A-Za-z0-9]/, "_").squeeze("_")
end
def in_project_namespace
namespace project_namespace do
yield
end
end
def build_task_name(artifact, release: false)
[ project_namespace,
"build",
string_as_task(artifact),
release ? "release" : "debug"
].join(":")
end
def define_build_tasks
in_project_namespace do
namespace :build do
CargoBuildTask.new(:debug, self, sources: recursive_sources)
CargoBuildTask.new(:release, self, sources: recursive_sources, release: true)
binary_sources.each do |bin_source|
bin_srcdir = File.basename(File.dirname(bin_source))
bin_name = File.basename(bin_source, ".rs")
if bin_srcdir == "src" && bin_name == "main"
namespace string_as_task(name) do
CargoBuildTask.new(:debug, self, binary: name, sources: recursive_sources)
CargoBuildTask.new(:release, self, binary: name, sources: recursive_sources, release: true)
end
elsif bin_srcdir == "bin"
namespace string_as_task(bin_name) do
CargoBuildTask.new(:debug, self, binary: bin_name, sources: recursive_sources)
CargoBuildTask.new(:release, self, binary: bin_name, sources: recursive_sources, release: true)
end
end
end
lib_sources.each do |lib_source|
namespace :lib do
CargoBuildTask.new(:debug, self, lib: true, sources: recursive_sources)
CargoBuildTask.new(:release, self, lib: true, sources: recursive_sources, release: true)
end
end
end
end
debug_path = target_path(full: false).join("debug")
release_path = target_path(full: false).join("release")
binary_sources.each do |bin_source|
bin_srcdir = File.basename(File.dirname(bin_source))
bin_name = File.basename(bin_source, ".rs")
if bin_srcdir == "src" && bin_name == "main"
file debug_path.join(name), [ :verbose ] => [ build_task_name(name) ]
file release_path.join(name), [ :verbose ] => [ build_task_name(name, release: true) ]
elsif bin_srcdir == "bin"
file debug_path.join(bin_name), [ :verbose ] => [ build_task_name(bin_name) ]
file release_path.join(bin_name), [ :verbose ] => [ build_task_name(bin_name, release: true) ]
end
end
lib_sources.each do |lib_source|
lib_name = "lib#{name}.rlib"
file debug_path.join(lib_name), [ :verbose ] => [ build_task_name("lib") ]
file release_path.join(lib_name), [ :verbose ] => [ build_task_name("lib", release: true) ]
end
end
def define_test_tasks
in_project_namespace do
desc "run tests for #{name}"
task :test, [ :verbose ] do |t, args|
args.with_defaults(verbose: false)
cmdline = [ "cargo", "test" ]
if args[:verbose]
cmdline << "--verbose"
end
do_in_dir { sh(*cmdline) }
end
end
end
def define_clean_tasks
in_project_namespace do
desc "Clean #{name}"
task :clean, [ :with_deps, :verbose ] do |t, args|
args.with_defaults(verbose: false, with_deps: false)
cmdline = [ "cargo", "clean" ]
if args.verbose
cmdline << "--verbose"
end
if args.with_deps
do_in_dir { sh(*cmdline) }
else
do_in_dir do
([ name ] + subprojects.map(&:name)).each do |package|
cmdline2 = cmdline + [ "-p", package ]
sh(*cmdline2)
end
end
end
end
end
end
end
def cargo_data(path = ".")
@cargo_data ||= {}
@cargo_data[path] ||= RustProjectTask[path].cargo_data
end
def git_revision
File.read("git-revision").strip
end
def release_filename
metadata = cargo_data
package = metadata["package"]
if RUBY_PLATFORM == "x86_64-linux"
triple = "x86_64-unknown-linux-gnu"
elsif RUBY_PLATFORM == "x86_64-linux-gnu"
triple = "x86_64-unknown-linux-gnu"
elsif RUBY_PLATFORM =~ /universal.x86_64-darwin1[45]/
triple = "x86_64-apple-darwin"
elsif RUBY_PLATFORM =~ /x64-mingw32/
triple = "x86_64-pc-windows-gnu"
else
raise "Unknown triple for platform #{RUBY_PLATFORM.inspect}"
end
"filament-#{package["version"]}-#{triple}.tar.gz"
end
RustProjectTask.new
namespace :docker do
desc "Set up the shell environment for testing with the real mogilefs cluster."
task :env do
ENV["COMPOSE_PROJECT_NAME"] = "filament"
ENV["FILAMENT_TEST_DOMAIN"] = "test_domain"
end
desc "Start the docker containers."
task :start => [ :env ] do
sh "docker-compose", "-f", "test/containers/docker-compose.yml", "up", "-d"
end
desc "Stop the docker containers."
task :stop => [ :env ] do
sh "docker-compose", "-f", "test/containers/docker-compose.yml", "stop"
end
desc "Delete the docker containers."
task :clean => [ :stop ] do
sh "docker-compose", "-f", "test/containers/docker-compose.yml", "rm", "-v", "--force"
end
desc "Make sure the docker mogilefs infrastructure is properly set up with hosts, devices, domains, and classes."
task :init => [ :start ] do
docker_host = ENV["DOCKER_HOST"] || "tcp://127.0.0.1:2375"
docker_ip = docker_host.match(/^tcp:\/\/(.*):\d+$/).captures.first
# Figure out the tracker IP / port.
tracker = Docker::Container.all("all" => 1).find { |c| c.info["Names"].include?("/filament_mogilefsd_1") }
tracker_port = tracker.info["Ports"].find { |p| p["PrivatePort"] == 7001 }["PublicPort"]
tracker_addr = "#{docker_ip}:#{tracker_port}"
puts "tracker_addr = #{tracker_addr.inspect}"
ENV["FILAMENT_TEST_TRACKERS"] = tracker_addr
# Figure out the storage server IP / port.
storage = Docker::Container.all("all" => 1).find { |c| c.info["Names"].include?("/filament_storage_1_1") }
storage_port = storage.info["Ports"].find { |p| p["PrivatePort"] == 7500 }["PublicPort"]
storage_addr = "#{docker_ip}:#{storage_port}"
puts "storage_addr = #{storage_addr.inspect}"
# Create our client objects.
mogadm = MogileFS::Admin.new(hosts: [ tracker_addr ])
# mogcl = MogileFS::MogileFS.new(hosts: [ tracker_addr ], domain: "test_domain")
# Wait until the tracker is ready to handle requests.
loop do
begin
mogadm.get_domains
break
rescue
puts "#{$!.message} (#{$!.class}), retrying..."
sleep 2
end
end
# Create / update the host for the storage server.
storage_host = mogadm.get_hosts.find { |h| h["hostname"] == "storage_1" }
if storage_host.nil?
mogadm.create_host("storage_1", ip: docker_ip, port: storage_port, status: "alive")
else
mogadm.update_host("storage_1", ip: docker_ip, port: storage_port, status: "alive")
end
# Create / update the device on the storage server.
storage_device = mogadm.get_devices.find { |d| d["devid"] == 1 }
if storage_device.nil?
mogadm.create_device("storage_1", 1, status: "alive")
else
mogadm.change_device_state("storage_1", 1, "alive")
end
# Create the test domain.
test_domain = mogadm.get_domains["test_domain"]
if test_domain.nil?
mogadm.create_domain("test_domain")
test_domain = mogadm.get_domains["test_domain"]
end
# Make sure the default class has a mindevcount of 1.
if test_domain["default"]["mindevcount"] != 1
mogadm.update_class("test_domain", "default", mindevcount: 1)
end
# Create the test class.
if test_domain["test_class"].nil?
mogadm.create_class("test_domain", "test_class", mindevcount: 1)
test_domain = mogadm.get_domains["test_domain"]
end
# Make sure the test class has a mindevcount of 1.
if test_domain["test_class"]["mindevcount"] != 1
mogadm.update_class("test_domain", "test_class", mindevcount: 1)
end
end
end
desc "The dist directory"
directory "dist"
desc "The release tarball"
file File.join("dist", release_filename) => [ "target/release/filament", "target/release/filament-cli", "dist" ] do
sh "tar", "-czf", File.join("dist", release_filename), "-C", "target/release", "filament", "filament-cli"
end
desc "Compile the source, and put the release tarball in dist"
task :package => [ File.join("dist", release_filename) ]
desc "Clean the main crate and the sub-crates"
task :clean, [ :with_deps, :verbose ] => [ "filament:clean", "client:clean", "common:clean", "server:clean" ]
namespace :build do
desc "Build debug builds of all the things that don't depend on filament-ext. (you probably don't need to do this)"
task :ci, [ :verbose ] => [ "client:build:debug", "common:build:debug", "server:build:debug" ]
desc "Build debug builds of all the things in all the subdirs (you probably don't need to do this)"
task :debug, [ :verbose ] => [ "filament:build:debug", "client:build:debug", "common:build:debug", "server:build:debug" ]
desc "Build release builds of all the things in all the subdirs (you probably really don't need to do this)"
task :release, [ :verbose ] => [ "filament:build:release", "client:build:release", "common:build:release", "server:build:release" ]
task :default, [ :verbose ] => "build:debug"
end
task :build, [ :verbose ] => [ "build:debug" ]
namespace :test do
desc "Run the tests for the sub-crates, skipping things that require private repos and / or a real MogileFS cluster."
task :ci, [ :verbose ] => [ "client:test", "common:test", "server:test" ]
desc "Run the tests for all the sub-crates, skipping things that require a real MogileFS cluster."
task :unit, [ :verbose ] => [ "filament:test", "client:test", "common:test", "server:test" ]
desc "Run the tests for all the sub-crates, using a real MogileFS running in Docker."
task :integration, [ :verbose ] => [ "docker:env", "docker:init", "test:unit" ]
end
task :test, [ :verbose ] => [ "test:unit" ]
desc "Build the docs"
task :doc do
sh "cargo", "doc"
end
task :default, [ :verbose ] => [ :build, :test ]