@@ -9,15 +9,25 @@ class Builder
99 def initialize ( spec )
1010 @spec = spec
1111 @container_name = "pgpm-debian_build-#{ Time . now . to_i } _#{ rand ( 10000 ) } "
12+ @pgpm_dir = Dir . mktmpdir
1213 end
1314
1415 def build
15- prepare
16- generate_deb_src_files
1716 pull_image
18- run_build
19- copy_build_from_container
20- #cleanup
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
2131 end
2232
2333 private
@@ -27,11 +37,10 @@ def image_name
2737 "quay.io/qount25/pgpm-debian-pg#{ @spec . package . postgres_major_version } -#{ @spec . arch } "
2838 end
2939
30- def prepare
40+ def prepare_versioned_source
3141 puts "Preparing build..."
3242 puts " Creating container dir structure..."
33- @pgpm_dir = Dir . mktmpdir
34- Dir . mkdir "#{ @pgpm_dir } /source"
43+ Dir . mkdir "#{ @pgpm_dir } /source-versioned"
3544 Dir . mkdir "#{ @pgpm_dir } /out"
3645
3746 puts " Downloading and unpacking sources to #{ @pgpm_dir } "
@@ -43,29 +52,57 @@ def prepare
4352 fn = src . name
4453 end
4554
46- system ( "tar -xf #{ @pgpm_dir } /#{ fn } -C #{ @pgpm_dir } /source/" )
55+ system ( "tar -xf #{ @pgpm_dir } /#{ fn } -C #{ @pgpm_dir } /source-versioned /" )
4756 FileUtils . remove ( "#{ @pgpm_dir } /#{ fn } " )
4857
49- untar_dir_entries = Dir . entries ( "#{ @pgpm_dir } /source/" ) . select do |entry |
58+ untar_dir_entries = Dir . entries ( "#{ @pgpm_dir } /source-versioned /" ) . select do |entry |
5059 !( [ "." , ".." ] . include? ( entry ) )
5160 end
5261
5362 if untar_dir_entries . size == 1
5463 entry = untar_dir_entries [ 0 ]
55- if File . directory? ( "#{ @pgpm_dir } /source/#{ entry } " )
56- FileUtils . mv "#{ @pgpm_dir } /source/#{ entry } " , "#{ @pgpm_dir } /"
57- FileUtils . remove_dir "#{ @pgpm_dir } /source/"
58- FileUtils . mv "#{ @pgpm_dir } /#{ entry } " , "#{ @pgpm_dir } /source"
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 "
5968 end
6069 end
6170
62- [ "prepare_artifacts.sh" , "pg_config.sh" ] . each do |fn |
71+ [ "prepare_artifacts.sh" ] . each do |fn |
6372 script_fn = File . expand_path ( "#{ __dir__ } /scripts/#{ fn } " )
64- FileUtils . cp script_fn , "#{ @pgpm_dir } /source/"
73+ FileUtils . cp script_fn , "#{ @pgpm_dir } /source-versioned /"
6574 end
6675
6776 end
6877
78+ def prepare_default_source
79+ Dir . mkdir "#{ @pgpm_dir } /source-default"
80+
81+ # 1. All pbuilder builds are in /var/cache/pbuilder/build. At this point
82+ # there's only one build, but we don't know what the directory is named
83+ # (the name is usually some numbers). So we just pick the first (and only)
84+ # entry at this location and this is our build dir.
85+ pbuilds_dir = "/var/cache/pbuilder/build"
86+ cmd = "ls -U #{ pbuilds_dir } | head -1"
87+ build_dir = `podman exec #{ @container_name } /bin/bash -c '#{ cmd } '` . strip
88+ puts "BUILD DIR IS: #{ pbuilds_dir } /#{ build_dir } "
89+
90+ # 2. Determine the name of the .control file inside the versioned build
91+ deb_dir = "#{ pbuilds_dir } /#{ build_dir } /build/#{ @spec . deb_pkg_name ( :versioned ) } -0/debian/#{ @spec . deb_pkg_name ( :versioned ) } "
92+ control_fn = "#{ deb_dir } /usr/share/postgresql/#{ @spec . package . postgres_major_version } /extension/#{ @spec . package . extension_name } --#{ @spec . package . version } .control"
93+
94+ # 3. Copy .control file to the source-default dir
95+ puts "Copying #{ control_fn } into /root/pgpm/source-default/"
96+ target_control_fn = "/root/pgpm/source-default/#{ @spec . package . extension_name } .control"
97+ cmd = "cp #{ control_fn } #{ target_control_fn } "
98+ system ( "podman exec #{ @container_name } /bin/bash -c '#{ cmd } '" )
99+
100+ [ "install_default_control.sh" ] . each do |fn |
101+ script_fn = File . expand_path ( "#{ __dir__ } /scripts/#{ fn } " )
102+ FileUtils . cp script_fn , "#{ @pgpm_dir } /source-default/"
103+ end
104+ end
105+
69106 def pull_image
70107 puts "Checking if podman image exists..."
71108 # Check if image exists
@@ -78,62 +115,65 @@ def pull_image
78115 end
79116 end
80117
81- def generate_deb_src_files
118+ def generate_deb_src_files ( pkg_type = :versioned )
82119 puts "Generating debian files..."
83- Dir . mkdir "#{ @pgpm_dir } /source/debian"
120+ Dir . mkdir "#{ @pgpm_dir } /source- #{ pkg_type } /debian"
84121 [ :changelog , :control , :copyright , :files , :rules ] . each do |f |
85- puts " -> #{ @pgpm_dir } /source/debian/#{ f } "
86- File . write "#{ @pgpm_dir } /source/debian/#{ f } " , @spec . generate ( f )
122+ puts " -> #{ @pgpm_dir } /source- #{ pkg_type } /debian/#{ f } "
123+ File . write "#{ @pgpm_dir } /source- #{ pkg_type } /debian/#{ f } " , @spec . generate ( f , pkg_type )
87124 end
88- File . chmod 0740 , "#{ @pgpm_dir } /source/debian/rules" # rules file must be executable
125+ File . chmod 0740 , "#{ @pgpm_dir } /source- #{ pkg_type } /debian/rules" # rules file must be executable
89126 end
90127
91- def run_build
128+ def start_container
92129 # podman create options
93130 create_opts = " -v #{ @pgpm_dir } :/root/pgpm"
94131 create_opts += ":z" if selinux_enabled?
95132 create_opts += " --privileged --tmpfs /tmp"
96133 create_opts += " --name #{ @container_name } #{ image_name } "
97134
98- dsc_fn = "#{ @spec . deb_pkg_name } _0-1.dsc"
99- deb_fn = "#{ @spec . deb_pkg_name } _0-1_#{ @spec . arch } .deb"
100-
101135 puts " Creating and starting container #{ @container_name } & running pbuilder"
102136 system ( "podman create -it #{ create_opts } " )
103137 exit ( 1 ) if $?. to_i > 0
104138 system ( "podman start #{ @container_name } " )
105139 exit ( 1 ) if $?. to_i > 0
140+ end
106141
107- cmds = [ ]
142+ # Prevents clean-up after pbuilder finishes. There's no option
143+ # in pbuilder to do it, so we have to patch it manually. The issue is
144+ # with pbuilder not being able to delete some directories (presumably,
145+ # due to directory names starting with ".") and returning error.
146+ #
147+ # This little patch avoids the error by returning from the python cleanup
148+ # function early -- because the package itself is built successfully and
149+ # we don't actually care that pbuilder is unable to clean something up.
150+ # The container is going to be removed anyway, so it's even less work as
151+ # a result.
152+ def patch_pbuilder
153+ cmd = "sed -E -i \" s/(^function clean_subdirectories.*$)/\\ 1\\ n return/g\" /usr/lib/pbuilder/pbuilder-modules"
154+ system ( "podman exec #{ @container_name } /bin/bash -c '#{ cmd } '" )
155+ end
108156
109- # This line prevents clean-up after pbuilder finishes. There's no option
110- # in pbuilder to do it, so we have to patch it manually. The issue is
111- # with pbuilder not being able to delete some directories (presumably,
112- # due to directory names starting with ".") and returning error.
113- #
114- # This little patch avoids the error by returning from the python cleanup
115- # function early -- because the package itself is built successfully and
116- # we don't actually care that pbuilder is unable to clean something up.
117- # The container is going to be removed anyway, so it's even less work as
118- # a result.
119- cmds << "sed -E -i \" s/(^function clean_subdirectories.*$)/\\ 1\\ n return/g\" /usr/lib/pbuilder/pbuilder-modules"
157+ def run_build ( pkg_type = :versioned )
158+ dsc_fn = "#{ @spec . deb_pkg_name ( pkg_type ) } _0-1.dsc"
159+ deb_fn = "#{ @spec . deb_pkg_name ( pkg_type ) } _0-1_#{ @spec . arch } .deb"
120160
161+ cmds = [ ]
121162 cmds << "dpkg-buildpackage --build=source -d" # -d flag helps with dependencies error
122163 cmds << "fakeroot pbuilder build ../#{ dsc_fn } "
123164 cmds << "mv /var/cache/pbuilder/result/#{ deb_fn } /root/pgpm/out/"
124165
125166 puts " Building package with pbuilder..."
126167 cmds . each do |cmd |
127- system ( "podman exec -w /root/pgpm/source #{ @container_name } /bin/bash -c '#{ cmd } '" )
168+ system ( "podman exec -w /root/pgpm/source- #{ pkg_type } #{ @container_name } /bin/bash -c '#{ cmd } '" )
128169 exit ( 1 ) if $?. to_i > 0
129170 end
130-
131171 end
132172
133- def copy_build_from_container
173+ def copy_build_from_container ( pkg_type = :versioned )
134174 puts "Copying .deb file from podman container into current directory..."
135- deb_fn = "#{ @spec . deb_pkg_name } _0-1_#{ @spec . arch } .deb"
136- deb_copy_fn = "#{ @spec . deb_pkg_name } _#{ @spec . arch } .deb"
175+ deb_fn = "#{ @spec . deb_pkg_name ( pkg_type ) } _0-1_#{ @spec . arch } .deb"
176+ deb_copy_fn = "#{ @spec . deb_pkg_name ( pkg_type ) } _#{ @spec . arch } .deb"
137177 FileUtils . cp ( "#{ @pgpm_dir } /out/#{ deb_fn } " , "#{ Dir . pwd } /#{ deb_copy_fn } " )
138178 end
139179
0 commit comments