11# frozen_string_literal: true
22
33require "English"
4- require "debug"
54
65module Pgpm
76 module Deb
@@ -13,7 +12,7 @@ def initialize(spec)
1312 end
1413
1514 def build
16- pull_image
15+ prepare_image
1716 start_container
1817 patch_pbuilder
1918
@@ -32,9 +31,16 @@ def build
3231
3332 private
3433
35- # Depends on postgres version and arch
34+ # Only depends on the arch -- this is the image being pulled from
35+ # a remote repo, which is then used to build a local image with correct
36+ # postgres version installed inside its pbuilder's chroot.
37+ def base_image_name
38+ "quay.io/qount25/pgpm-debian-#{ @spec . arch } "
39+ end
40+
41+ # Locally built image with correct chroot installed.
3642 def image_name
37- "quay.io/qount25/ pgpm-debian-pg#{ @spec . package . postgres_version } -#{ @spec . arch } "
43+ "pgpm-debian-pg#{ @spec . package . postgres_version } -#{ @spec . arch } "
3844 end
3945
4046 def prepare_versioned_source
@@ -102,18 +108,45 @@ def prepare_default_source
102108 end
103109 end
104110
105- def pull_image
111+ def prepare_image
106112 puts "Checking if podman image exists..."
107113 # Check if image exists
108114 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 } " )
115+ if $CHILD_STATUS. to_i . positive?
116+ puts " Image for the specific pg version doesn't exist. Will build."
117+ system ( "podman image exists #{ base_image_name } " )
118+ if $CHILD_STATUS. to_i . positive?
119+ puts " Base image doesn't exist. Pulling it..."
120+ system ( "podman pull #{ base_image_name } " )
121+ end
122+ build_local_image
112123 else
113124 puts " Yes, image #{ image_name } already exists! OK"
114125 end
115126 end
116127
128+ def build_local_image
129+ puts " Building local #{ image_name } ..."
130+ system ( "podman create -it --privileged --tmpfs /tmp --name pgpm-deb-tmp #{ base_image_name } " )
131+ system ( "podman start pgpm-deb-tmp" )
132+
133+ # Generate pbuilder_install script.sh, copy it inside the image
134+ pbuild_install_script_path = "#{ @pgpm_dir } /pbuilder_install_script.sh"
135+ puts " Generating #{ pbuild_install_script_path } ..."
136+ File . write "#{ pbuild_install_script_path } " , @spec . generate ( "pbuilder_install_script.sh" )
137+ system ( "podman container cp #{ pbuild_install_script_path } pgpm-deb-tmp:/root/" )
138+
139+ # This command installs relevant postgresql packages into the chroot
140+ # base image inside the container (along with some other necessary
141+ # packages) and saves chroot base image with these changes.
142+ puts " Updating chroot image..."
143+ system ( "podman exec -w /root pgpm-deb-tmp /bin/bash -c 'fakeroot pbuilder execute --save-after-exec ./pbuilder_install_script.sh'" )
144+
145+ system ( "podman stop pgpm-deb-tmp" )
146+ system ( "podman commit pgpm-deb-tmp #{ image_name } " )
147+ system ( "podman container rm pgpm-deb-tmp" )
148+ end
149+
117150 def generate_deb_src_files ( pkg_type = :versioned )
118151 puts "Generating debian files..."
119152 Dir . mkdir "#{ @pgpm_dir } /source-#{ pkg_type } /debian"
0 commit comments