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
|
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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 "English"
require "json"
require "open-uri"
require "time"
class PackageTask
include Rake::DSL
def initialize(package, version, release_time, options={})
@package = package
@version = version
@release_time = release_time
@archive_base_name = "#{@package}-#{@version}"
@archive_name = "#{@archive_base_name}.tar.gz"
@full_archive_name = File.expand_path(@archive_name)
@rpm_package = @package
case @version
when /-((dev|rc)\d+)\z/
base_version = $PREMATCH
sub_version = $1
type = $2
if type == "rc" and options[:rc_build_type] == :release
@deb_upstream_version = base_version
@deb_archive_base_name_version = base_version
@rpm_version = base_version
@rpm_release = "1"
else
@deb_upstream_version = "#{base_version}~#{sub_version}"
@deb_archive_base_name_version = @version
@rpm_version = base_version
@rpm_release = "0.#{sub_version}"
end
else
@deb_upstream_version = @version
@deb_archive_base_name_version = @version
@rpm_version = @version
@rpm_release = "1"
end
@deb_release = ENV["DEB_RELEASE"] || "1"
end
def define
define_dist_task
define_apt_task
define_yum_task
define_version_task
define_docker_tasks
end
private
def env_value(name)
value = ENV[name]
raise "Specify #{name} environment variable" if value.nil?
value
end
def debug_build?
ENV["DEBUG"] != "no"
end
def download(url, output_path)
if File.directory?(output_path)
base_name = url.split("/").last
output_path = File.join(output_path, base_name)
end
absolute_output_path = File.expand_path(output_path)
unless File.exist?(absolute_output_path)
mkdir_p(File.dirname(absolute_output_path))
rake_output_message "Downloading... #{url}"
open_url(url) do |downloaded_file|
File.open(absolute_output_path, "wb") do |output_file|
IO.copy_stream(downloaded_file, output_file)
end
end
end
absolute_output_path
end
def open_url(url, &block)
URI(url).open(&block)
end
def substitute_content(content)
content.gsub(/@(.+?)@/) do |matched|
yield($1, matched)
end
end
def docker_image(os, architecture)
image = "#{@package}-#{os}"
image << "-#{architecture}" if architecture
image
end
def docker_run(os, architecture, console: false)
id = os
id = "#{id}-#{architecture}" if architecture
image = docker_image(os, architecture)
build_command_line = [
"docker",
"build",
"--build-arg", "BUILDKIT_INLINE_CACHE=1",
"--cache-from", image,
"--tag", image,
]
run_command_line = [
"docker",
"run",
"--rm",
"--log-driver", "none",
"--volume", "#{Dir.pwd}:/host:rw",
]
if $stdin.tty?
run_command_line << "--interactive"
run_command_line << "--tty"
else
run_command_line.concat(["--attach", "STDOUT"])
run_command_line.concat(["--attach", "STDERR"])
end
build_dir = ENV["BUILD_DIR"]
if build_dir
build_dir = "#{File.expand_path(build_dir)}/#{id}"
mkdir_p(build_dir)
run_command_line.concat(["--volume", "#{build_dir}:/build:rw"])
end
if debug_build?
build_command_line.concat(["--build-arg", "DEBUG=yes"])
run_command_line.concat(["--env", "DEBUG=yes"])
end
pass_through_env_names = [
"DEB_BUILD_OPTIONS",
"RPM_BUILD_NCPUS",
]
pass_through_env_names.each do |name|
value = ENV[name]
next unless value
run_command_line.concat(["--env", "#{name}=#{value}"])
end
if File.exist?(File.join(id, "Dockerfile"))
docker_context = id
else
lines = File.readlines(File.join(id, "from"), encoding: "UTF-8")
from = lines.find do |line|
/^[a-z-]/i =~ line
end
from_components = from.chomp.split
from = from_components.pop
build_arguments = from_components
case build_arguments
when ["--platform=linux/arm64"]
docker_info = JSON.parse(`docker info --format '{{json .}}'`)
case docker_info["Architecture"]
when "aarch64"
# Do nothing
else
# docker build ... -> docker buildx build ...
build_command_line[1, 0] = "buildx"
build_command_line.concat(build_arguments)
end
end
build_command_line.concat(["--build-arg", "FROM=#{from}"])
docker_context = os
end
build_command_line.concat(docker_build_options(os, architecture))
run_command_line.concat(docker_run_options(os, architecture))
build_command_line << docker_context
run_command_line << image
run_command_line << "/host/build.sh" unless console
sh(*build_command_line)
sh(*run_command_line)
end
def docker_build_options(os, architecture)
[]
end
def docker_run_options(os, architecture)
[]
end
def docker_pull(os, architecture)
image = docker_image(os, architecture)
command_line = [
"docker",
"pull",
image,
]
command_line.concat(docker_pull_options(os, architecture))
sh(*command_line)
end
def docker_pull_options(os, architecture)
[]
end
def docker_push(os, architecture)
image = docker_image(os, architecture)
command_line = [
"docker",
"push",
image,
]
command_line.concat(docker_push_options(os, architecture))
sh(*command_line)
end
def docker_push_options(os, architecture)
[]
end
def define_dist_task
define_archive_task
desc "Create release package"
task :dist => [@archive_name]
end
def split_target(target)
components = target.split("-")
if components[0, 2] == ["amazon", "linux"]
components[0, 2] = components[0, 2].join("-")
elsif components.values_at(0, 2) == ["centos", "stream"]
components[1, 2] = components[1, 2].join("-")
end
if components.size >= 3
components[2..-1] = components[2..-1].join("-")
end
components
end
def enable_apt?
true
end
def apt_targets
return [] unless enable_apt?
targets = (ENV["APT_TARGETS"] || "").split(",")
targets = apt_targets_default if targets.empty?
targets.find_all do |target|
Dir.exist?(File.join(apt_dir, target))
end
end
def apt_targets_default
# Disable arm64 targets by default for now
# because they require some setups on host.
[
"debian-bookworm",
# "debian-bookworm-arm64",
"debian-trixie",
# "debian-trixie-arm64",
"debian-forky",
# "debian-forky-arm64",
"ubuntu-jammy",
# "ubuntu-jammy-arm64",
"ubuntu-noble",
# "ubuntu-noble-arm64",
]
end
def deb_archive_base_name
"#{@package}-#{@deb_archive_base_name_version}"
end
def deb_archive_name
"#{@package}-#{@deb_upstream_version}.tar.gz"
end
def apt_dir
"apt"
end
def apt_prepare_debian_dir(tmp_dir, target)
source_debian_dir = nil
specific_debian_dir = "debian.#{target}"
distribution, code_name, _architecture = split_target(target)
platform = [distribution, code_name].join("-")
platform_debian_dir = "debian.#{platform}"
if File.exist?(specific_debian_dir)
source_debian_dir = specific_debian_dir
elsif File.exist?(platform_debian_dir)
source_debian_dir = platform_debian_dir
else
source_debian_dir = "debian"
end
prepared_debian_dir = "#{tmp_dir}/debian.#{target}"
cp_r(source_debian_dir, prepared_debian_dir)
control_in_path = "#{prepared_debian_dir}/control.in"
if File.exist?(control_in_path)
control_in = File.read(control_in_path, encoding: "UTF-8")
rm_f(control_in_path)
File.open("#{prepared_debian_dir}/control", "w") do |control|
prepared_control = apt_prepare_debian_control(control_in, target)
control.print(prepared_control)
end
end
end
def apt_prepare_debian_control(control_in, target)
message = "#{__method__} must be defined to use debian/control.in"
raise NotImplementedError, message
end
def apt_build(console: false)
tmp_dir = "#{apt_dir}/tmp"
rm_rf(tmp_dir)
mkdir_p(tmp_dir)
cp(deb_archive_name,
File.join(tmp_dir, deb_archive_name))
apt_targets.each do |target|
apt_prepare_debian_dir(tmp_dir, target)
end
env_sh = "#{apt_dir}/env.sh"
File.open(env_sh, "w") do |file|
file.puts(<<-ENV)
PACKAGE=#{@package}
VERSION=#{@deb_upstream_version}
ENV
end
apt_targets.each do |target|
cd(apt_dir) do
distribution, version, architecture = split_target(target)
os = "#{distribution}-#{version}"
docker_run(os, architecture, console: console)
end
end
end
def define_apt_task
namespace :apt do
source_build_sh = "#{__dir__}/apt/build.sh"
build_sh = "#{apt_dir}/build.sh"
repositories_dir = "#{apt_dir}/repositories"
file build_sh => source_build_sh do
cp(source_build_sh, build_sh)
end
directory repositories_dir
desc "Build deb packages"
if enable_apt?
build_dependencies = [
deb_archive_name,
build_sh,
repositories_dir,
]
else
build_dependencies = []
end
task :build => build_dependencies do
apt_build if enable_apt?
end
namespace :build do
desc "Open console"
task :console => build_dependencies do
apt_build(console: true) if enable_apt?
end
end
end
desc "Release APT repositories"
apt_tasks = [
"apt:build",
]
task :apt => apt_tasks
end
def enable_yum?
true
end
def yum_targets
return [] unless enable_yum?
targets = (ENV["YUM_TARGETS"] || "").split(",")
targets = yum_targets_default if targets.empty?
targets.find_all do |target|
Dir.exist?(File.join(yum_dir, target))
end
end
def yum_targets_default
# Disable aarch64 targets by default for now
# because they require some setups on host.
[
"almalinux-10",
# "almalinux-10-arch64",
"almalinux-9",
# "almalinux-9-arch64",
"almalinux-8",
# "almalinux-8-arch64",
"amazon-linux-2023",
# "amazon-linux-2023-arch64",
"centos-9-stream",
# "centos-9-stream-aarch64",
]
end
def rpm_archive_base_name
"#{@package}-#{@rpm_version}"
end
def rpm_archive_name
"#{rpm_archive_base_name}.tar.gz"
end
def yum_dir
"yum"
end
def yum_build_sh
"#{yum_dir}/build.sh"
end
def yum_expand_variable(key)
case key
when "PACKAGE"
@rpm_package
when "VERSION"
@rpm_version
when "RELEASE"
@rpm_release
else
nil
end
end
def yum_spec_in_path
"#{yum_dir}/#{@rpm_package}.spec.in"
end
def yum_build(console: false)
tmp_dir = "#{yum_dir}/tmp"
rm_rf(tmp_dir)
mkdir_p(tmp_dir)
cp(rpm_archive_name,
File.join(tmp_dir, rpm_archive_name))
env_sh = "#{yum_dir}/env.sh"
File.open(env_sh, "w") do |file|
file.puts(<<-ENV)
SOURCE_ARCHIVE=#{rpm_archive_name}
PACKAGE=#{@rpm_package}
VERSION=#{@rpm_version}
RELEASE=#{@rpm_release}
ENV
end
spec = "#{tmp_dir}/#{@rpm_package}.spec"
spec_in_data = File.read(yum_spec_in_path, encoding: "UTF-8")
spec_data = substitute_content(spec_in_data) do |key, matched|
yum_expand_variable(key) || matched
end
File.open(spec, "w") do |spec_file|
spec_file.print(spec_data)
end
yum_targets.each do |target|
cd(yum_dir) do
distribution, version, architecture = split_target(target)
os = "#{distribution}-#{version}"
docker_run(os, architecture, console: console)
end
end
end
def define_yum_task
namespace :yum do
source_build_sh = "#{__dir__}/yum/build.sh"
file yum_build_sh => source_build_sh do
cp(source_build_sh, yum_build_sh)
end
repositories_dir = "#{yum_dir}/repositories"
directory repositories_dir
desc "Build RPM packages"
if enable_yum?
build_dependencies = [
repositories_dir,
rpm_archive_name,
yum_build_sh,
yum_spec_in_path,
]
else
build_dependencies = []
end
task :build => build_dependencies do
yum_build if enable_yum?
end
namespace :build do
desc "Open console"
task :console => build_dependencies do
yum_build(console: true) if enable_yum?
end
end
end
desc "Release Yum repositories"
yum_tasks = [
"yum:build",
]
task :yum => yum_tasks
end
def define_version_task
namespace :version do
desc "Update versions"
task :update do
update_debian_changelog
update_spec
end
end
end
def package_changelog_message
"New upstream release."
end
def packager_name
ENV["DEBFULLNAME"] || ENV["NAME"] || guess_packager_name_from_git
end
def guess_packager_name_from_git
name = `git config --get user.name`.chomp
return name unless name.empty?
`git log -n 1 --format=%aN`.chomp
end
def packager_email
ENV["DEBEMAIL"] || ENV["EMAIL"] || guess_packager_email_from_git
end
def guess_packager_email_from_git
email = `git config --get user.email`.chomp
return email unless email.empty?
`git log -n 1 --format=%aE`.chomp
end
def update_content(path)
if File.exist?(path)
content = File.read(path, encoding: "UTF-8")
else
content = ""
end
content = yield(content)
File.open(path, "w") do |file|
file.puts(content)
end
end
def update_debian_changelog
return unless enable_apt?
Dir.glob("debian*") do |debian_dir|
update_content("#{debian_dir}/changelog") do |content|
<<-CHANGELOG.rstrip
#{@package} (#{@deb_upstream_version}-#{@deb_release}) unstable; urgency=low
* New upstream release.
-- #{packager_name} <#{packager_email}> #{@release_time.rfc2822}
#{content}
CHANGELOG
end
end
end
def update_spec
return unless enable_yum?
release_time = @release_time.strftime("%a %b %d %Y")
update_content(yum_spec_in_path) do |content|
content = content.sub(/^(%changelog\n)/, <<-CHANGELOG)
%changelog
* #{release_time} #{packager_name} <#{packager_email}> - #{@rpm_version}-#{@rpm_release}
- #{package_changelog_message}
CHANGELOG
content = content.sub(/^(Release:\s+)\d+/, "\\11")
content.rstrip
end
end
def define_docker_tasks
namespace :docker do
pull_tasks = []
push_tasks = []
(apt_targets + yum_targets).each do |target|
distribution, version, architecture = split_target(target)
os = "#{distribution}-#{version}"
namespace :pull do
desc "Pull built image for #{target}"
task target do
docker_pull(os, architecture)
end
pull_tasks << "docker:pull:#{target}"
end
namespace :push do
desc "Push built image for #{target}"
task target do
docker_push(os, architecture)
end
push_tasks << "docker:push:#{target}"
end
end
desc "Pull built images"
task :pull => pull_tasks
desc "Push built images"
task :push => push_tasks
end
end
end
|