-
Notifications
You must be signed in to change notification settings - Fork 23
/
Rakefile
executable file
·1016 lines (874 loc) · 28.8 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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env rake
#
# td-agent-builder
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require_relative '../lib/package-task'
require_relative '../lib/gems_parser'
require_relative 'config.rb'
require 'rake/testtask'
require 'rake/clean'
require 'erb'
require 'shellwords'
require 'pathname'
require 'open-uri'
require 'digest'
require 'etc'
require "yaml"
DOWNLOADS_DIR = File.expand_path(ENV["TD_AGENT_DOWNLOADS_PATH"] || "downloads")
STAGING_DIR = File.expand_path(ENV["TD_AGENT_STAGING_PATH"] || "staging")
CLEAN.include(STAGING_DIR)
CLOBBER.include(DOWNLOADS_DIR)
# Debian
CLEAN.include("apt/tmp")
CLEAN.include("apt/build.sh")
CLEAN.include("apt/env.sh")
CLEAN.include("debian/tmp")
CLOBBER.include("apt/repositories")
# Red Hat
CLEAN.include("yum/tmp")
CLEAN.include("yum/build.sh")
CLEAN.include("yum/env.sh")
CLOBBER.include("yum/repositories")
# Windows
CLEAN.include("msi/env.bat")
CLEAN.include("msi/parameters.wxi")
CLEAN.include("msi/project-files.wxs")
CLEAN.include("msi/*.wixobj")
CLEAN.include("msi/*.wixpdb")
CLOBBER.include("msi/*.msi")
def windows?
GemsParser.windows?
end
def ensure_directory(dirname)
mkdir_p(dirname) unless File.exists?(dirname)
if block_given?
cd(dirname) do
yield
end
end
end
class DownloadTask
include Rake::DSL
attr_reader :file_jemalloc_source
attr_reader :file_ruby_source, :file_ruby_installer_x64
attr_reader :file_fluentd_archive
attr_reader :files_core_gems, :files_plugin_gems
def files
[
@file_jemalloc_source,
@file_ruby_source,
@file_ruby_installer_x64,
@file_fluentd_archive,
*@files_core_gems,
*@files_plugin_gems,
]
end
def define
define_jemalloc_file
define_ruby_files
define_fluentd_archive
define_gem_files("core_gems")
define_gem_files("plugin_gems")
namespace :download do
desc "Download jemalloc source"
task :jemalloc => [@file_jemalloc_source]
desc "Download Ruby source"
task :ruby => [@file_ruby_source, @file_ruby_installer_x64]
desc "Clone fluentd repository and create a tarball"
task :fluentd => @file_fluentd_archive
desc "Download core_gems"
task :core_gems => @files_core_gems
desc "Download plugin_gems"
task :plugin_gems => @files_plugin_gems
end
end
private
def download_file(url, filename, sha256sum = nil)
tmp_filename = "#{filename}.part"
ensure_directory(DOWNLOADS_DIR) do
puts "Downloading #{filename}..."
open(url) do |in_file|
open(tmp_filename, "wb") do |out_file|
out_file.write(in_file.read)
end
end
unless sha256sum.nil?
digest = Digest::SHA256.file(tmp_filename)
if digest != sha256sum
fail "
sha256sum of #{filename} did not matched!!!
expected: #{sha256sum}
actual: #{digest}
"
end
end
mv(tmp_filename, filename)
end
end
def define_jemalloc_file
version = JEMALLOC_VERSION
filename = "jemalloc-#{version}.tar.bz2"
url_base = "https://github.com/jemalloc/jemalloc/releases/download/"
@file_jemalloc_source = File.join(DOWNLOADS_DIR, filename)
file @file_jemalloc_source do
url = "#{url_base}/#{version}/#{filename}"
download_file(url, filename)
end
end
def define_ruby_files
define_ruby_source_file
define_ruby_installer_file
end
def define_ruby_source_file
version = BUNDLED_RUBY_VERSION
sha256sum = BUNDLED_RUBY_SOURCE_SHA256SUM
filename = "ruby-#{version}.tar.gz"
feature_version = version.match(/^(\d+\.\d+)/)[0]
url_base = "https://cache.ruby-lang.org/pub/ruby/"
url = "#{url_base}#{feature_version}/#{filename}"
@file_ruby_source = File.join(DOWNLOADS_DIR, filename)
file @file_ruby_source do
download_file(url, filename, sha256sum)
end
end
def define_ruby_installer_file
version = BUNDLED_RUBY_INSTALLER_X64_VERSION
sha256sum = BUNDLED_RUBY_INSTALLER_X64_SHA256SUM
filename = "rubyinstaller-#{version}-x64.7z"
url_base = "https://github.com/oneclick/rubyinstaller2/releases/download/"
url = "#{url_base}RubyInstaller-#{version}/#{filename}"
@file_ruby_installer_x64 = File.join(DOWNLOADS_DIR, filename)
file @file_ruby_installer_x64 do
download_file(url, filename, sha256sum)
end
end
def define_fluentd_archive
@file_fluentd_archive = File.join(DOWNLOADS_DIR, "fluentd-#{FLUENTD_REVISION}.tar.gz")
file @file_fluentd_archive do
ensure_directory(DOWNLOADS_DIR) do
dirname = "fluentd-#{FLUENTD_REVISION}"
rm_rf("fluentd") if File.exists?("fluentd")
rm_rf(dirname) if File.exists?(dirname)
sh("git", "clone", "https://github.com/fluent/fluentd.git")
cd("fluentd") do
sh("git", "checkout", FLUENTD_REVISION)
end
mv("fluentd", dirname)
sh("tar", "cvfz", "#{dirname}.tar.gz", dirname)
end
end
end
def define_gem_files(category)
paths = []
gems_parser = GemsParser.parse(File.read("#{category}.rb"))
digits = (gems_parser.target_files.length - 1).to_s.length
target_dir = File.join(DOWNLOADS_DIR, gems_parser.target_dir)
gems_parser.target_files.each_with_index do |target, index|
name, version = target
gem = "#{name}-#{version}.gem"
numbered_gem = sprintf("%0#{digits}d-%s", index, gem)
path = File.join(target_dir, numbered_gem)
paths << path
file path do
ensure_directory(target_dir) do
if version.include?("-")
number, platform = version.split("-", 2)
version_option = ["--version", number, "--platform", platform]
else
version_option = ["--version", "#{version}", "--platform", "ruby"]
end
gem = "#{name}-#{version}.gem"
numbered_gem = sprintf("%0#{digits}d-%s", index, gem)
sh("gem", "fetch", name, *version_option)
`gem install --explain #{gem} --no-document`
fail "Failed to download #{gem}!" unless $?.success?
mv(gem, numbered_gem)
end
end
end
instance_variable_set("@files_#{category}", paths)
end
end
class BuildTask
include Rake::DSL
def initialize(download_task)
@download_task = download_task
end
def make_wix_version_number(version)
return version unless version.include?("~")
"%s.%s" % [
version.split("~", 2)[0].delete(".").to_i.pred.to_s.chars.join("."),
Time.now.strftime("%m%d")
]
end
def define
namespace :build do
desc "Install jemalloc"
task :jemalloc => [:"download:jemalloc"] do
build_jemalloc unless windows?
end
desc "Install Ruby"
task :ruby => [:jemalloc, :"download:ruby"] do
if windows?
extract_ruby_installer
setup_windows_build_env
find_and_put_dynamiclibs
else
build_ruby_from_source
end
end
desc "Install core_gems"
task :core_gems => [:"download:core_gems", :ruby] do
install_gems(@download_task.files_core_gems)
end
desc "Install plugin_gems"
task :plugin_gems => [:"download:plugin_gems", :fluentd] do
install_gems(@download_task.files_plugin_gems)
end
desc "Install fluentd"
task :fluentd => [:"download:fluentd", :core_gems] do
cd(DOWNLOADS_DIR) do
archive_path = @download_task.file_fluentd_archive
fluentd_dir = archive_path.sub(/\.tar\.gz$/, '')
sh("tar", "xvf", archive_path) unless File.exists?(fluentd_dir)
cd("fluentd-#{FLUENTD_REVISION}") do
sh("rake", "build")
gem_install("pkg/fluentd-*.gem")
end
end
end
desc "Install all gems"
task :gems => [:plugin_gems]
desc "Collect licenses of bundled softwares"
task :licenses => [:gems] do
install_jemalloc_license
install_ruby_license
install_td_agent_license
collect_gem_licenses
end
desc "Install all components"
task :all => [:licenses] do
remove_needless_files
end
debian_pkg_scripts = ["preinst", "postinst", "prerm", "postrm"]
debian_pkg_scripts.each do |script|
CLEAN.include(File.join("..", "debian", script))
end
desc "Create debian package script files from template"
task :deb_scripts do
# Note: "debian" directory in this directory isn't used dilectly, it's
# copied to the top directory of td-agent-builder in Docker container.
# Since this task is executed in the container, package scripts should
# be generated to the "debian" directory under the top directory
# instead of this directory's one.
debian_pkg_scripts.each do |script|
src = template_path('package-scripts', 'td-agent', "deb", script)
next unless File.exist?(src)
dest = File.join("..", "debian", File.basename(script))
render_template(dest, src, template_params, { mode: 0755 })
end
end
desc "Create td-agent configuration files from template"
task :td_agent_config do
configs = [
"etc/td-agent/td-agent.conf",
"opt/td-agent/share/td-agent.conf.tmpl",
"etc/logrotate.d/td-agent",
"opt/td-agent/share/td-agent-ruby.conf"
]
configs.each do |config|
src = template_path(config)
dest = File.join(STAGING_DIR, config)
render_template(dest, src, template_params)
end
end
desc "Create systemd-tmpfiles configuration files from template"
task :systemd_tmpfiles_config do
configs = [
"usr/lib/tmpfiles.d/td-agent.conf",
]
configs.each do |config|
src = template_path(config)
dest = File.join(STAGING_DIR, config)
render_template(dest, src, template_params)
end
end
desc "Create bin script files from template"
task :bin_scripts do
scripts = [
"usr/bin/td",
"usr/sbin/td-agent",
"usr/sbin/td-agent-gem",
]
scripts.each do |script|
src = template_path("#{script}.erb")
dest = File.join(STAGING_DIR, script)
render_template(dest, src, template_params, { mode: 0755 })
end
end
desc "Install additional .bat files for Windows"
task :win_batch_files do
ensure_directory(staging_bindir)
cp("msi/assets/td-agent-prompt.bat", td_agent_staging_dir)
cp("msi/assets/td-agent-post-install.bat", staging_bindir)
cp("msi/assets/td-agent.bat", staging_bindir)
cp("msi/assets/td-agent-gem.bat", staging_bindir)
end
desc "Create systemd unit file for Red Hat like systems"
task :rpm_systemd do
dest = File.join(STAGING_DIR, 'usr', 'lib', 'systemd', 'system', PACKAGE_NAME + ".service")
params = {pkg_type: "rpm"}
render_systemd_unit_file(dest, template_params(params))
end
desc "Create sysv init file for Red Hat like systems"
task :rpm_sysvinit do
dest = File.join(STAGING_DIR, 'etc', 'init.d', PACKAGE_NAME)
params = {pkg_type: "rpm"}
render_sysv_init_file(dest, template_params(params))
end
desc "Create systemd unit file for Debian like systems"
task :deb_systemd do
dest = File.join(STAGING_DIR, 'lib', 'systemd', 'system', PACKAGE_NAME + ".service")
params = {pkg_type: "deb"}
render_systemd_unit_file(dest, template_params(params))
end
desc "Create config files for WiX Toolset"
task :wix_config do
src = File.join('msi', 'parameters.wxi.erb')
dest = File.join('msi', 'parameters.wxi')
params = {wix_package_version: make_wix_version_number(PACKAGE_VERSION)}
render_template(dest, src, template_params(params))
end
desc "Create configuration files for Red Hat like systems with systemd"
task :rpm_config => [:td_agent_config, :systemd_tmpfiles_config, :bin_scripts, :rpm_systemd]
desc "Create configuration files for Red Hat like systems without systemd"
task :rpm_old_config => [:td_agent_config, :bin_scripts, :rpm_sysvinit]
desc "Create configuration files for Debian like systems"
task :deb_config => [:td_agent_config, :systemd_tmpfiles_config, :bin_scripts, :deb_systemd, :deb_scripts]
desc "Create configuration files for Windows"
task :msi_config => [:td_agent_config, :wix_config, :win_batch_files]
end
end
private
def template_params(params = nil)
config = {
project_name: PACKAGE_NAME,
version: PACKAGE_VERSION,
install_message: nil,
pkg_type: nil,
}
unless windows?
path_params = {
install_path: install_prefix,
gem_install_path: File.join(install_prefix, gem_dir_suffix),
}
config.merge!(path_params)
end
if params
config.merge(params)
else
config
end
end
def template_path(*path_parts)
File.join('templates', *path_parts)
end
def render_template(dest, src, config, opts={})
erb_binding = binding
config.each do |key, value|
erb_binding.local_variable_set(key, value)
end
destination = dest.gsub('td-agent', config[:project_name])
directory = File.dirname(destination)
mode = opts.fetch(:mode, 0644)
puts "Generate #{destination}"
ensure_directory(directory)
File.open(destination, 'w', mode) do |f|
template = ERB.new(File.read(src), nil, '<>')
f.write(template.result(erb_binding))
end
end
def render_systemd_unit_file(dest_path, config)
template_file_path = template_path('etc', 'systemd', 'td-agent.service.erb')
render_template(dest_path, template_file_path, config)
end
def render_sysv_init_file(dest_path, config)
template_file_path = template_path('etc', 'init.d', 'td-agent.erb')
render_template(dest_path, template_file_path, config, {mode: 0755})
end
def apply_ruby_patches
return if BUNDLED_RUBY_PATCHES.nil?
BUNDLED_RUBY_PATCHES.each do |patch|
patch_name, version_condition = patch
dependency = Gem::Dependency.new('', version_condition)
if dependency.match?('', BUNDLED_RUBY_VERSION)
patch_path = File.join(__dir__, "patches", patch_name)
sh("patch", "-p1", "--input=#{patch_path}")
end
end
end
def build_jemalloc
tarball = @download_task.file_jemalloc_source
source_dir = tarball.sub(/\.tar\.bz2$/, '')
sh("tar", "xvf", tarball, "-C", DOWNLOADS_DIR)
configure_opts = [
"--prefix=#{install_prefix}",
]
cd(source_dir) do
sh("./configure", *configure_opts)
sh("make", "install", "-j#{Etc.nprocessors}", "DESTDIR=#{STAGING_DIR}")
end
end
def build_ruby_from_source
tarball = @download_task.file_ruby_source
ruby_source_dir = tarball.sub(/\.tar\.gz$/, '')
sh("tar", "xvf", tarball, "-C", DOWNLOADS_DIR)
configure_opts = [
"--prefix=#{install_prefix}",
"--enable-shared",
"--disable-install-doc",
"--with-compress-debug-sections=no", # https://bugs.ruby-lang.org/issues/12934
]
cd(ruby_source_dir) do
apply_ruby_patches
sh("./configure", *configure_opts)
sh("make", "install", "-j#{Etc.nprocessors}", "DESTDIR=#{STAGING_DIR}")
# For building gems. The built ruby & gem command cannot use without install.
sh("make", "install")
end
end
def extract_ruby_installer
ensure_directory(td_agent_staging_dir) do
path = File.expand_path(@download_task.file_ruby_installer_x64)
src_dir = File.basename(path).sub(/\.7z$/, '')
sh("7z",
"x", # Extract files with full paths
"-y", # Assume yes on all queries
path)
cp_r(Dir.glob(File.join(src_dir, "*")), ".")
rm_rf(src_dir)
end
end
def find_and_put_dynamiclibs
begin
require 'ruby_installer/runtime'
# These dlls are required to put in staging_bindir to run C++ based extension
# included gem such as winevt_c. We didn't find how to link them statically yet.
dlls = [
"libstdc++-6",
]
dlls.each do |dll|
mingw_bin_path = RubyInstaller::Runtime.msys2_installation.mingw_bin_path
windows_path = "#{mingw_bin_path}/#{dll}.dll"
if File.exist?(windows_path)
copy windows_path, staging_bindir
else
raise "Cannot find required DLL needed for dynamic linking: #{windows_path}"
end
end
rescue LoadError
raise "Cannot load RubyInstaller::Runtime class"
end
end
def setup_windows_build_env
sh("#{td_agent_staging_dir}/bin/ridk", "install", "3")
end
def install_prefix
"/opt/#{PACKAGE_NAME}"
end
def td_agent_staging_dir
# The staging directory on windows doesn't have install_prefix,
# it's added by the installer.
if windows?
STAGING_DIR
else
File.join(STAGING_DIR, install_prefix)
end
end
def staging_bindir
File.join(td_agent_staging_dir, "bin")
end
def gem_command
if windows?
File.join(staging_bindir, "gem")
else
# On GNU/Linux we don't use gem command in staging path, use the one
# installed in the proper path instead since Ruby doesn't support
# running without install (although there are some solutions like rbenv).
"#{install_prefix}/bin/gem"
end
end
def gem_dir_version
if windows?
ruby_version = BUNDLED_RUBY_INSTALLER_X64_VERSION
else
ruby_version = BUNDLED_RUBY_VERSION
end
rb_major, rb_minor, rb_teeny = ruby_version.split("-", 2).first.split(".", 3)
"#{rb_major}.#{rb_minor}.0" # gem path's teeny version is always 0
end
def gem_dir_suffix
"lib/ruby/gems/#{gem_dir_version}"
end
def licenses_staging_dir
File.join(td_agent_staging_dir, "LICENSES")
end
def gem_staging_dir
gemdir = `#{gem_command} env gemdir`.strip
fail "Failed to get default installation directory for gems!" unless $?.success?
if windows?
expected = File.join(td_agent_staging_dir, gem_dir_suffix)
staging_dir = expected
else
expected = File.join(install_prefix, gem_dir_suffix)
staging_dir = File.join(td_agent_staging_dir, gem_dir_suffix)
end
fail "Unsupposed gemdir: #{gemdir} (expected: #{expected})" unless gemdir == expected
staging_dir
end
def gem_install(gem_path)
ensure_directory(staging_bindir)
ensure_directory(gem_staging_dir)
makeflags = ENV["MAKEFLAGS"]
gem_home = ENV["GEM_HOME"]
ENV["MAKEFLAGS"] = "-j#{Etc.nprocessors}"
ENV["GEM_HOME"] = gem_staging_dir
sh(gem_command, "install",
"--no-document",
"--bindir", staging_bindir,
gem_path)
ENV["GEM_HOME"] = gem_home
ENV["MAKEFLAGS"] = makeflags
end
def install_gems(files)
files.each do |gem_path|
gem_install(gem_path)
end
end
def install_jemalloc_license
return if windows?
ensure_directory(licenses_staging_dir) do
tarball = @download_task.file_jemalloc_source
source_dir = File.basename(tarball.sub(/\.tar\.bz2$/, ''))
license_file = File.join(source_dir, "COPYING")
sh("tar", "xf", tarball, license_file)
mv(license_file, "LICENSE-jemalloc.txt")
rm_rf(source_dir)
end
end
def install_ruby_license
ensure_directory(licenses_staging_dir) do
if windows?
src = File.join(td_agent_staging_dir, "LICENSE.txt")
mv(src, "LICENSE-RubyInstaller.txt")
end
tarball = @download_task.file_ruby_source
ruby_source_dir = File.basename(tarball.sub(/\.tar\.gz$/, ''))
license_file = File.join(ruby_source_dir, "COPYING")
sh("tar", "xf", tarball, license_file)
mv(license_file, "LICENSE-Ruby.txt")
rm_rf(ruby_source_dir)
end
end
def install_td_agent_license
ensure_directory(licenses_staging_dir)
src = File.join(__dir__, "..", "LICENSE")
dest = File.join(licenses_staging_dir, "LICENSE-td-agent.txt")
cp(src, dest)
end
def collect_gem_licenses
puts "Collecting licenses of gems..."
env_restore = ENV["GEM_PATH"]
ENV["GEM_PATH"] = gem_staging_dir
gems_descriptions = `#{gem_command} list -d`
gems_descriptions.gsub!(STAGING_DIR, "") unless windows?
ENV["GEM_PATH"] = env_restore
ensure_directory(licenses_staging_dir) do
File.open("LICENSES-gems.txt", 'w', 0644) do |f|
f.write(gems_descriptions)
end
end
end
def remove_files(pattern, recursive=false)
files = Dir.glob(pattern)
return if files.empty?
if recursive
rm_rf(files)
else
rm_f(files)
end
end
def remove_needless_files
remove_files("#{td_agent_staging_dir}/share/doc", true) # Contains only jemalloc.html
cd("#{gem_staging_dir}/cache") do
remove_files("*.gem")
end
Dir.glob("#{gem_staging_dir}/gems/*").each do |gem_dir|
cd(gem_dir) do
rm_rf(["test", "spec"])
remove_files("**/gem.build_complete")
remove_files("ext/**/a.out")
remove_files("ext/**/*.{o,la,a}")
remove_files("ext/**/.libs", true)
end
end
remove_files("#{td_agent_staging_dir}/lib/lib*.a")
end
end
class LinuxPackageTask < PackageTask
def initialize(download_task)
@download_task = download_task
super(PACKAGE_NAME, PACKAGE_VERSION, detect_release_time)
@archive_tar_name = "#{@package}-#{@version}.tar"
@archive_name = "#{@archive_tar_name}.gz"
CLEAN.include(@archive_name)
end
private
def define_archive_task
repo_files = `git ls-files --full-name`.split("\n").collect do |path|
File.join("..", path)
end
debian_copyright_file = File.join("td-agent", "debian", "copyright")
file debian_copyright_file do
build_copyright_file
end
file @archive_name => [*repo_files, *@download_task.files, debian_copyright_file] do
build_archive
end
end
def build_copyright_file
# Note: maintain debian/copyright manually is inappropriate way because
# many gem is bundled with td-agent package.
# We use gem specification GEMFILE to solve this issue.
src = File.join("templates", "package-scripts", "td-agent", "deb", "copyright")
dest = File.join("debian", "copyright")
licenses = []
template = File.open(src) do |infile|
infile.read
end
@download_task.files_core_gems.concat(@download_task.files_plugin_gems).each do |gem_file|
spec = YAML.load(`gem specification #{gem_file}`)
relative_path = gem_file.sub(/#{Dir.pwd}\//, "")
unless spec.licenses.empty?
spdx_compatible_license = spec.licenses.first.sub(/Apache 2\.0/, "Apache-2.0")
.sub(/Apache License Version 2\.0/, "Apache-2.0")
license = <<-EOS
Files: #{relative_path}
Copyright: #{spec.authors.join(",")}
License: #{spdx_compatible_license}
EOS
licenses << license
else
# Note: remove this conditions when gem.licenses in gemspec was fixed in upstream
case spec.name
when "cool.io", "async-pool"
license = "MIT"
when "td", "webhdfs"
license = "Apache-2.0"
end
licenses <<= <<-EOS
Files: #{relative_path}
Copyright: #{spec.authors.join(",")}
License: #{license}
EOS
end
end
puts "Generate #{dest}"
File.open(dest, 'w+', 0644) do |f|
template.sub!(/BUNDLED_GEM_LICENSES/, licenses.join("\n"))
f.write(template)
end
end
def build_archive
cd("..") do
sh("git", "archive", "HEAD",
"--prefix", "#{@archive_base_name}/",
"--output", @full_archive_name)
sh("tar", "xvf", @full_archive_name)
@download_task.files.each do |path|
src_path = Pathname(path)
dest_path = Pathname(DOWNLOADS_DIR)
relative_path = src_path.relative_path_from(dest_path)
dest_downloads_dir = "#{@archive_base_name}/td-agent/downloads"
dest_dir = "#{dest_downloads_dir}/#{File.dirname(relative_path)}"
ensure_directory(dest_dir)
cp_r(path, dest_dir)
end
cp_r("td-agent/debian/copyright", "#{@archive_base_name}/td-agent/debian/copyright")
sh("tar", "cvfz", @full_archive_name, @archive_base_name)
rm_rf(@archive_base_name)
end
end
def apt_targets_default
[
"debian-buster",
"ubuntu-bionic",
"ubuntu-focal",
]
end
def yum_targets_default
[
"centos-6",
"centos-7",
"centos-8",
]
end
private
def detect_release_time
release_time_env = ENV["TD_AGENT_RELEASE_TIME"]
if release_time_env
Time.parse(release_time_env).utc
else
Time.now.utc
end
end
end
class WindowsPackageTask
include Rake::DSL
MSI_OUTPUT_DIR = ENV["TD_AGENT_MSI_OUTPUT_PATH"] || "."
def initialize
@package = PACKAGE_NAME
@version = PACKAGE_VERSION
@staging_dir = STAGING_DIR
end
def define
namespace :msi do
desc "Build MSI package (alias for msi:dockerbuild)"
task :build do
Rake::Task["msi:dockerbuild"].invoke
end
desc "Build MSI package without using Docker"
task :selfbuild => [:"build:msi_config", :"build:all"] do
run_build
end
desc "Build MSI package by Docker"
task :dockerbuild => ["#{PACKAGE_NAME}-#{PACKAGE_VERSION}.tar.gz"] do
run_docker("windows", arch)
end
end
end
private
def run_build
output_dir = MSI_OUTPUT_DIR.gsub(File::ALT_SEPARATOR, File::SEPARATOR)
ensure_directory(output_dir)
cd("msi") do
# Pick up package contents
sh(heat_path,
"dir", @staging_dir,
"-nologo", # Skip heat logo
"-srd", # Suppress harvesting the root directory as an element
"-sreg", # Suppress registry harvesting
"-gg", # Generate guides
"-cg", "ProjectDir", # Component Group Name
"-dr", "PROJECTLOCATION", # Root directory reference
"-var", "var.ProjectSourceDir", # Substitue File/@Source="SourceDir"
"-t", "exclude-files.xslt", # XSLT for exclude files
"-out", 'project-files.wxs')
# Build
sh(candle_path,
"-nologo", # Skip candle logo
"-dProjectSourceDir=#{@staging_dir}", # Define a parameter
"-arch", "#{arch}",
"project-files.wxs",
"source.wxs")
# Link
sh(light_path,
"-nologo", # Skip light logo
"-ext", "WixUIExtension", # Extension assembly
"-cultures:en-us", # Localization
"-loc", "localization-en-us.wxl", # Localization file
"project-files.wixobj",
"source.wixobj",
"-out", File.join(output_dir, "#{@package}-#{@version}-#{arch}.msi"))
end
end
def write_env
env_bat = "msi/env.bat"
File.open(env_bat, "w") do |file|
file.puts(<<-ENV)
SET PACKAGE=#{@package}
SET VERSION=#{@version}
SET ARCH=#{arch}
ENV
end
end
# TODO: Unify with PackageTask
def run_docker(os, architecture=nil)
top_dir = File.expand_path('../.')
id = os
id = "#{id}-#{architecture}" if architecture
docker_tag = "#{@package}-#{id}"
build_command_line = [
"docker",
"build",
"--tag", docker_tag,
]
run_command_line = [
"docker",
"run",
"--rm",
"--tty",
"--volume", "#{top_dir}:c:/td-agent-builder:rw",
]
docker_context = "msi"
build_command_line << docker_context
run_command_line.concat([docker_tag, 'c:\td-agent-builder\td-agent\msi\build.bat'])
write_env
sh(*build_command_line)
sh(*run_command_line)
end
def windows_path(*pieces)
path = File.join(*pieces)
if File::ALT_SEPARATOR
path.gsub(File::SEPARATOR, File::ALT_SEPARATOR)
else
path
end
end
def wix_dir
dir = ENV["WIX"]
fail "Can't find WiX commands path" if dir.nil? || dir.empty?
dir
end
def wix_bin_dir
windows_path(wix_dir, "bin")
end
def heat_path
windows_path(wix_bin_dir, "heat")
end
def candle_path
windows_path(wix_bin_dir, "candle")
end
def light_path
windows_path(wix_bin_dir, "light")
end
def arch
if RUBY_PLATFORM =~ /x64/
"x64"
elsif RUBY_PLATFORM =~ /i386/
"x86"
else