|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "English" |
| 4 | +require "debug" |
| 5 | + |
| 6 | +module Pgpm |
| 7 | + module Deb |
| 8 | + class Builder |
| 9 | + def initialize(spec) |
| 10 | + @spec = spec |
| 11 | + @container_name = "pgpm-debian_build-#{Time.now.to_i}_#{rand(10_000)}" |
| 12 | + @pgpm_dir = Dir.mktmpdir |
| 13 | + end |
| 14 | + |
| 15 | + def build |
| 16 | + pull_image |
| 17 | + start_container |
| 18 | + patch_pbuilder |
| 19 | + |
| 20 | + prepare_versioned_source |
| 21 | + generate_deb_src_files(:versioned) |
| 22 | + run_build(:versioned) |
| 23 | + copy_build_from_container(:versioned) |
| 24 | + |
| 25 | + prepare_default_source |
| 26 | + generate_deb_src_files(:default) |
| 27 | + run_build(:default) |
| 28 | + copy_build_from_container(:default) |
| 29 | + |
| 30 | + cleanup |
| 31 | + end |
| 32 | + |
| 33 | + private |
| 34 | + |
| 35 | + # Depends on postgres version and arch |
| 36 | + def image_name |
| 37 | + "quay.io/qount25/pgpm-debian-pg#{@spec.package.postgres_major_version}-#{@spec.arch}" |
| 38 | + end |
| 39 | + |
| 40 | + def prepare_versioned_source |
| 41 | + puts "Preparing build..." |
| 42 | + puts " Creating container dir structure..." |
| 43 | + Dir.mkdir "#{@pgpm_dir}/source-versioned" |
| 44 | + Dir.mkdir "#{@pgpm_dir}/out" |
| 45 | + |
| 46 | + puts " Downloading and unpacking sources to #{@pgpm_dir}" |
| 47 | + |
| 48 | + fn = nil |
| 49 | + @spec.sources.map do |src| |
| 50 | + srcfile = File.join(@pgpm_dir.to_s, src.name) |
| 51 | + File.write(srcfile, src.read) |
| 52 | + fn = src.name |
| 53 | + end |
| 54 | + |
| 55 | + system("tar -xf #{@pgpm_dir}/#{fn} -C #{@pgpm_dir}/source-versioned/") |
| 56 | + FileUtils.remove("#{@pgpm_dir}/#{fn}") |
| 57 | + |
| 58 | + untar_dir_entries = Dir.entries("#{@pgpm_dir}/source-versioned/").reject do |entry| |
| 59 | + [".", ".."].include?(entry) |
| 60 | + end |
| 61 | + |
| 62 | + if untar_dir_entries.size == 1 |
| 63 | + entry = untar_dir_entries[0] |
| 64 | + if File.directory?("#{@pgpm_dir}/source-versioned/#{entry}") |
| 65 | + FileUtils.mv "#{@pgpm_dir}/source-versioned/#{entry}", "#{@pgpm_dir}/" |
| 66 | + FileUtils.remove_dir "#{@pgpm_dir}/source-versioned/" |
| 67 | + FileUtils.mv "#{@pgpm_dir}/#{entry}", "#{@pgpm_dir}/source-versioned" |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + ["prepare_artifacts.sh"].each do |f| |
| 72 | + script_fn = File.expand_path("#{__dir__}/scripts/#{f}") |
| 73 | + FileUtils.cp script_fn, "#{@pgpm_dir}/source-versioned/" |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + def prepare_default_source |
| 78 | + Dir.mkdir "#{@pgpm_dir}/source-default" |
| 79 | + |
| 80 | + # 1. All pbuilder builds are in /var/cache/pbuilder/build. At this point |
| 81 | + # there's only one build, but we don't know what the directory is named |
| 82 | + # (the name is usually some numbers). So we just pick the first (and only) |
| 83 | + # entry at this location and this is our build dir. |
| 84 | + pbuilds_dir = "/var/cache/pbuilder/build" |
| 85 | + cmd = "ls -U #{pbuilds_dir} | head -1" |
| 86 | + build_dir = `podman exec #{@container_name} /bin/bash -c '#{cmd}'`.strip |
| 87 | + puts "BUILD DIR IS: #{pbuilds_dir}/#{build_dir}" |
| 88 | + |
| 89 | + # 2. Determine the name of the .control file inside the versioned build |
| 90 | + deb_dir = "#{pbuilds_dir}/#{build_dir}/build/#{@spec.deb_pkg_name(:versioned)}-0/debian/#{@spec.deb_pkg_name(:versioned)}" |
| 91 | + control_fn = "#{deb_dir}/usr/share/postgresql/#{@spec.package.postgres_major_version}/extension/#{@spec.package.extension_name}--#{@spec.package.version}.control" |
| 92 | + |
| 93 | + # 3. Copy .control file to the source-default dir |
| 94 | + puts "Copying #{control_fn} into /root/pgpm/source-default/" |
| 95 | + target_control_fn = "/root/pgpm/source-default/#{@spec.package.extension_name}.control" |
| 96 | + cmd = "cp #{control_fn} #{target_control_fn}" |
| 97 | + system("podman exec #{@container_name} /bin/bash -c '#{cmd}'") |
| 98 | + |
| 99 | + ["install_default_control.sh"].each do |fn| |
| 100 | + script_fn = File.expand_path("#{__dir__}/scripts/#{fn}") |
| 101 | + FileUtils.cp script_fn, "#{@pgpm_dir}/source-default/" |
| 102 | + end |
| 103 | + end |
| 104 | + |
| 105 | + def pull_image |
| 106 | + puts "Checking if podman image exists..." |
| 107 | + # Check if image exists |
| 108 | + system("podman image exists #{image_name}") |
| 109 | + if $CHILD_STATUS.to_i.positive? # image doesn't exist -- pull image from a remote repository |
| 110 | + puts " No. Pulling image #{image_name}..." |
| 111 | + system("podman pull #{image_name}") |
| 112 | + else |
| 113 | + puts " Yes, image #{image_name} already exists! OK" |
| 114 | + end |
| 115 | + end |
| 116 | + |
| 117 | + def generate_deb_src_files(pkg_type = :versioned) |
| 118 | + puts "Generating debian files..." |
| 119 | + Dir.mkdir "#{@pgpm_dir}/source-#{pkg_type}/debian" |
| 120 | + %i[changelog control copyright files rules].each do |f| |
| 121 | + puts " -> #{@pgpm_dir}/source-#{pkg_type}/debian/#{f}" |
| 122 | + File.write "#{@pgpm_dir}/source-#{pkg_type}/debian/#{f}", @spec.generate(f, pkg_type) |
| 123 | + end |
| 124 | + File.chmod 0o740, "#{@pgpm_dir}/source-#{pkg_type}/debian/rules" # rules file must be executable |
| 125 | + end |
| 126 | + |
| 127 | + def start_container |
| 128 | + # podman create options |
| 129 | + create_opts = " -v #{@pgpm_dir}:/root/pgpm" |
| 130 | + create_opts += ":z" if selinux_enabled? |
| 131 | + create_opts += " --privileged --tmpfs /tmp" |
| 132 | + create_opts += " --name #{@container_name} #{image_name}" |
| 133 | + |
| 134 | + puts " Creating and starting container #{@container_name} & running pbuilder" |
| 135 | + system("podman create -it #{create_opts}") |
| 136 | + exit(1) if $CHILD_STATUS.to_i.positive? |
| 137 | + system("podman start #{@container_name}") |
| 138 | + exit(1) if $CHILD_STATUS.to_i.positive? |
| 139 | + end |
| 140 | + |
| 141 | + # Prevents clean-up after pbuilder finishes. There's no option |
| 142 | + # in pbuilder to do it, so we have to patch it manually. The issue is |
| 143 | + # with pbuilder not being able to delete some directories (presumably, |
| 144 | + # due to directory names starting with ".") and returning error. |
| 145 | + # |
| 146 | + # This little patch avoids the error by returning from the python cleanup |
| 147 | + # function early -- because the package itself is built successfully and |
| 148 | + # we don't actually care that pbuilder is unable to clean something up. |
| 149 | + # The container is going to be removed anyway, so it's even less work as |
| 150 | + # a result. |
| 151 | + def patch_pbuilder |
| 152 | + cmd = "sed -E -i \"s/(^function clean_subdirectories.*$)/\\1\\n return/g\" /usr/lib/pbuilder/pbuilder-modules" |
| 153 | + system("podman exec #{@container_name} /bin/bash -c '#{cmd}'") |
| 154 | + end |
| 155 | + |
| 156 | + def run_build(pkg_type = :versioned) |
| 157 | + dsc_fn = "#{@spec.deb_pkg_name(pkg_type)}_0-1.dsc" |
| 158 | + deb_fn = "#{@spec.deb_pkg_name(pkg_type)}_0-1_#{@spec.arch}.deb" |
| 159 | + |
| 160 | + cmds = [] |
| 161 | + cmds << "dpkg-buildpackage --build=source -d" # -d flag helps with dependencies error |
| 162 | + cmds << "fakeroot pbuilder build ../#{dsc_fn}" |
| 163 | + cmds << "mv /var/cache/pbuilder/result/#{deb_fn} /root/pgpm/out/" |
| 164 | + |
| 165 | + puts " Building package with pbuilder..." |
| 166 | + cmds.each do |cmd| |
| 167 | + system("podman exec -w /root/pgpm/source-#{pkg_type} #{@container_name} /bin/bash -c '#{cmd}'") |
| 168 | + exit(1) if $CHILD_STATUS.to_i.positive? |
| 169 | + end |
| 170 | + end |
| 171 | + |
| 172 | + def copy_build_from_container(pkg_type = :versioned) |
| 173 | + puts "Copying .deb file from podman container into current directory..." |
| 174 | + deb_fn = "#{@spec.deb_pkg_name(pkg_type)}_0-1_#{@spec.arch}.deb" |
| 175 | + deb_copy_fn = "#{@spec.deb_pkg_name(pkg_type)}_#{@spec.arch}.deb" |
| 176 | + FileUtils.cp("#{@pgpm_dir}/out/#{deb_fn}", "#{Dir.pwd}/#{deb_copy_fn}") |
| 177 | + end |
| 178 | + |
| 179 | + def cleanup |
| 180 | + puts "Cleaning up..." |
| 181 | + |
| 182 | + puts " Stopping destroying podman container: #{@container_name}" |
| 183 | + system("podman container stop #{@container_name}") |
| 184 | + system("podman container rm #{@container_name}") |
| 185 | + |
| 186 | + # Remove temporary files |
| 187 | + # |
| 188 | + # Make sure @pgpm_dir starts with "/tmp/" or we may accidentally |
| 189 | + # delete something everything! You can never be sure! |
| 190 | + if @pgpm_dir.start_with?("/tmp/") |
| 191 | + puts " Removing temporary files in #{@pgpm_dir}" |
| 192 | + FileUtils.rm_rf(@pgpm_dir) |
| 193 | + else |
| 194 | + puts "WARNING: will not remove temporary files, strange path: \"#{@pgpm_dir}\"" |
| 195 | + end |
| 196 | + end |
| 197 | + |
| 198 | + # Needed because SELinux requires :z suffix for mounted directories to |
| 199 | + # be accessible -- otherwise we get "Permission denied" when cd into a |
| 200 | + # mounted dir inside the container. |
| 201 | + def selinux_enabled? |
| 202 | + # This returns true or false by itself |
| 203 | + system("sestatus | grep 'SELinux status' | grep -o 'enabled'") |
| 204 | + end |
| 205 | + end |
| 206 | + end |
| 207 | +end |
0 commit comments