Skip to content

Commit

Permalink
Add tools for building hotfix RPMs
Browse files Browse the repository at this point in the history
  • Loading branch information
bdunne committed May 9, 2023
1 parent d91b6d3 commit 6c384e8
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 0 deletions.
7 changes: 7 additions & 0 deletions bin/build_hotfix.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env ruby

$LOAD_PATH << File.expand_path("../lib", __dir__)

require 'manageiq/rpm_build'

ManageIQ::RPMBuild::BuildHotfix.new.generate_rpm
3 changes: 3 additions & 0 deletions container-assets/user-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ if [[ $# -eq 0 ]]; then
elif [[ $1 == "build" ]]; then
shift
cmd="bin/build.rb $@"
elif [[ $1 == "build_hotfix" ]]; then
shift
cmd="bin/build_hotfix.rb $@"
else
echo "Run with no argument to setup build environment, or"
echo "Run with 'build <build.rb options>' to start bin/build.rb (e.g. build --git-ref jansa)"
Expand Down
2 changes: 2 additions & 0 deletions lib/manageiq/rpm_build.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class String

require 'manageiq/rpm_build/helper'
require 'manageiq/rpm_build/build_copr'
require 'manageiq/rpm_build/build_hotfix'
require 'manageiq/rpm_build/build_uploader'
require 'manageiq/rpm_build/generate_ansible_venv'
require 'manageiq/rpm_build/generate_gemset'
Expand All @@ -26,6 +27,7 @@ module RPMBuild
SCRIPT_DIR = ROOT_DIR.join("scripts")

BUILD_DIR = Pathname.new(ENV.fetch("BUILD_DIR", "~/BUILD")).expand_path
HOTFIX_DIR = BUILD_DIR.join("hotfix")
RPM_SPEC_DIR = BUILD_DIR.join("rpm_spec")
MANIFEST_DIR = BUILD_DIR.join("manifest")

Expand Down
74 changes: 74 additions & 0 deletions lib/manageiq/rpm_build/build_hotfix.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
require 'awesome_spawn'
require 'fileutils'
require 'pathname'
require 'yaml'

module ManageIQ
module RPMBuild
class BuildHotfix
include Helper

attr_reader :rpm_spec

def generate_rpm
where_am_i

Dir.chdir(HOTFIX_DIR) do
unpack_srpm
update_spec
copy_patches

arch = RUBY_PLATFORM.split("-").first
shell_cmd("rpmbuild -ba --define '_sourcedir #{HOTFIX_DIR}' --define '_srcrpmdir #{BUILD_DIR.join("rpms", arch)}' --define '_rpmdir #{BUILD_DIR.join("rpms")}' #{rpm_spec}")
end
end

private

def copy_patches
FileUtils.cp(patches, HOTFIX_DIR)
end

def patches
@patches ||= Dir.glob(ManageIQ::RPMBuild::ROOT_DIR.join("rpm_spec", "patches", "*.patch"))
end

def update_spec
where_am_i

spec_text = File.read(rpm_spec)

# We need a release number greater than the previous, but less than the next official release.
# I'd be surprised if we had more than 99 patch files in a hotfix release,
# so change out the timestamp minutes to 99 and the seconds to the number of patches.
# This allows for multiple hotfixes on the same original RPM and each to have a predictable release number greater than the last.
old_release = spec_text.match(/^Release:\s+(\d{14}).*/)[1]
new_release = old_release[0...10] + (9900 + patches.length).to_s
spec_text.sub!(/^Release:.*$/, "Release: #{new_release}%{dist}")

patch_list_lines = patches.each_with_index.collect { |patch, i| "Patch#{i + 1}: #{patch}\n" }
spec_text.sub!(/.*Source4:.+\n/) { |match| "#{match}#{patch_list_lines.join}" }

patch_apply_lines = patches.each_with_index.collect { |patch, i| "%patch -P #{i + 1} -p1\n"}
spec_text.sub!(/cd %{_builddir}\n/) { |match| "#{match}#{patch_apply_lines.join}" }

version = spec_text.match(/Version:\s+(.*)/)[1]
changelog_entry = <<~EOC
* #{Time.now.strftime("%a %b %d %Y")} Hot Fix <hotfix@example.com> - #{version}-#{new_release}
- Fixes on top of #{version}-#{old_release}
EOC

spec_text.sub!(/%changelog\n/) { |match| "#{match}#{changelog_entry}" }

File.write(rpm_spec, spec_text)
end

def unpack_srpm
srpm = Dir.glob("*.src.rpm").first
shell_cmd("rpm2cpio #{srpm} | cpio -idmv")
@rpm_spec = Dir.glob("*.spec").first
end
end
end
end
4 changes: 4 additions & 0 deletions rpm_spec/manageiq.spec.in
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ Source2: %{name}-core-%{version}.tar.gz
Source3: %{name}-gemset-%{version}.tar.gz
Source4: %{name}-manifest-%{version}.tar.gz

# Declare any patches here

%description
%{product_summary}

Expand All @@ -50,6 +52,8 @@ umask 0002
# buildsubdir is set to the last extracted archive, reset
cd %{_builddir}

# Apply any patches here

%py3_shebang_fix %{gemset_builddir} 2>&1 | grep -v "^recursedown" | grep -v "no change"

%build
Expand Down

0 comments on commit 6c384e8

Please sign in to comment.