Skip to content

Commit 2ae1b01

Browse files
author
qount25
committed
Problem: users cannot set which minor version of postgresql package to use when building dev pkg
In its official deb repository, postgresql packages may have the following names: postgresql-17 postgresql-16 postgresql-15 etc. The problem here is that the latest minor version will be installed, wherease users may want a different version. Solution: packages have versions. Inside pbuilder_install_script.sh.erb we pick up which version our user wants and match it agains what's available. If it exists, we install that particular version: ... apt -y install postgresql-$PG_MAJOR_VERSION=$pkg_version ... And then build the local podman image named after the chosen postgresql version. If user selected postgresql version 17.14 on amd64, the image will be called: pgpm-debian-pg17.4-amd64 BONUS FIX: LD_CONFIG error messages are no longer present!
1 parent 9fd9648 commit 2ae1b01

File tree

4 files changed

+54
-19
lines changed

4 files changed

+54
-19
lines changed

lib/pgpm/deb/builder.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,20 @@ def build_local_image
142142
puts " Updating chroot image..."
143143
system("podman exec -w /root pgpm-deb-tmp /bin/bash -c 'fakeroot pbuilder execute --save-after-exec ./pbuilder_install_script.sh'")
144144

145-
system("podman stop pgpm-deb-tmp")
145+
# Exiting -- most likely error occurred because we cannot find the same
146+
# postgresql version in the Debian repository. The bash script
147+
# will do error reporting for us, so we just exit.
148+
if $CHILD_STATUS.to_i.positive?
149+
stop_and_remove_deb_tmp_image
150+
exit 1
151+
end
146152
system("podman commit pgpm-deb-tmp #{image_name}")
147-
system("podman container rm pgpm-deb-tmp")
153+
stop_and_remove_deb_tmp_image
154+
end
155+
156+
def stop_and_remove_deb_tmp_image
157+
system("podman stop pgpm-deb-tmp")
158+
system("podman container rm pgpm-deb-tmp")
148159
end
149160

150161
def generate_deb_src_files(pkg_type = :versioned)

lib/pgpm/deb/spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ def cmds_if_not_empty(cmds, else_echo)
6060
cmds.map! { |c| c.gsub("$", "$$") }
6161
cmds.join("\t")
6262
end
63+
6364
end
6465
end
6566
end

lib/pgpm/deb/templates/pbuilder_install_script.sh.erb

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
#!/usr/bin/env bash
22

33
# Run this script with:
4-
# fakeroot pbuilder execute --save-after-exec ./pbuilder_install_script.sh
4+
# fakeroot pbuilder execute --save-after-exec path/to/pbuilder_install_script.sh
55

6+
7+
# This helps us avoid almost all instances of a very annoying:
8+
#
9+
# ERROR: ld.so: object 'libfakeroot-sysv.so' from LD_PRELOAD cannot be preloaded...
10+
#
11+
# which isn't critical, but is polluting the output and makes debugging or
12+
# simply reading the output more difficult.
13+
export LD_PRELOAD="$(find / -name libfakeroot-sysv.so 2>/dev/null)"
14+
15+
# These packages are universally required by pbuilder, so we pre-install them
16+
# inside our chroot.
617
apt update
718
DEBIAN_FRONTEND=noninteractive apt -y install \
819
build-essential curl lsb-release ca-certificates automake autopoint \
@@ -19,20 +30,32 @@ curl -o /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc --fail https://
1930
# Create the repository configuration file:
2031
sh -c 'echo "deb [signed-by=/usr/share/postgresql-common/pgdg/apt.postgresql.org.asc] https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
2132

33+
# Add archive repos containing all postgres versions deleted from
34+
# apt.postgresql.org. For example, when version 17.4 came out, version 17.3
35+
# was moved to the archive-apt source. We want all of them, because we don't know
36+
# ahead of time which version is going to be required by the user.
37+
apt_sources_fn="/etc/apt/sources.list.d/pgdg.list"
38+
cat $apt_sources_fn | grep 's|https://apt|https://apt-archive' >> $apt_sources_fn
39+
2240
# Update the package lists:
2341
apt update
2442

25-
# Install PostgreSQL. We first try a package with the full version name, but if
26-
# (such as postgresql-17.4), but if that's not the name that exists in the apt
27-
# repository, we use postgresql-17 instead.
28-
PG_VERSION=<%= @package.postgres_version(:major) %>
29-
30-
# Checking if the output is empty, because `apt search` returns status 0
31-
# even if doesn't find anything.
32-
if [[ $(apt-cache search --names-only postgresql-<%= @package.postgres_version %> | head -c1 | wc -c) -ne 0 ]]; then
33-
PG_VERSION=<%= @package.postgres_version %>
43+
PG_VERSION=<%= @package.postgres_version %>
44+
PG_MAJOR_VERSION=<%= @package.postgres_version(:major) %>
45+
46+
# Let's check if the version we're trying to install actually exists.
47+
pkg="$(apt-cache madison postgresql-$PG_MAJOR_VERSION | grep " | $PG_VERSION" | head -n1)"
48+
if [[ "$pkg" == "" ]]; then
49+
# If it doesn't we exit with status 1 and an explanation of an error. This will
50+
# trigger the caller (spec.rb) to also exit with status one and, thus, will
51+
# not build a local image -- because user-requested version of postgresql
52+
# cannot be found in the repository.
53+
>&2 echo "ERROR:"
54+
>&2 echo " Couldn't find postgresql-$PG_MAJOR_VERSION package with version $PG_VERSION."
55+
exit 1
56+
else
57+
pkg_version="$(echo "$pkg" | cut -d "|" -f 2 | xargs)"
58+
apt -y install postgresql-$PG_MAJOR_VERSION=$pkg_version \
59+
postgresql-server-dev-$PG_MAJOR_VERSION=$pkg_version \
60+
postgresql-common
3461
fi
35-
36-
apt -y install postgresql-$PG_VERSION \
37-
postgresql-server-dev-$PG_VERSION \
38-
postgresql-common

lib/pgpm/package/dependencies.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ def build_dependencies
1111
case Pgpm::OS.in_scope.class.name
1212
when "debian", "ubuntu"
1313
deps = [
14-
"postgresql-#{postgres_version(:major)}",
15-
"postgresql-server-dev-#{postgres_version(:major)}",
14+
"postgresql-#{postgres_version(:major)} (>= #{postgres_version})",
15+
"postgresql-server-dev-#{postgres_version(:major)} (>= #{postgres_version})",
1616
"postgresql-common"
1717
]
1818
if native?
@@ -26,7 +26,7 @@ def build_dependencies
2626
def dependencies
2727
case Pgpm::OS.in_scope.class.name
2828
when "debian", "ubuntu"
29-
["postgresql-#{postgres_version(:major)}"]
29+
["postgresql-#{postgres_version(:major)} (>= #{postgres_version})"]
3030
when "rocky+epel-9", "redhat", "fedora"
3131
[]
3232
end

0 commit comments

Comments
 (0)