Skip to content

Commit 9fd9648

Browse files
author
qount25
committed
Use base deb podman image and patch it locally with correct pg version installed inside chroot
1 parent e7669ea commit 9fd9648

File tree

7 files changed

+92
-40
lines changed

7 files changed

+92
-40
lines changed

lib/pgpm/deb/Dockerfile

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,3 @@ RUN echo 'MIRRORSITE=http://deb.debian.org/debian' > /etc/pbuilderrc
2626
RUN echo 'AUTO_DEBSIGN=${AUTO_DEBSIGN:-no}' > /root/.pbuilderrc
2727
RUN echo 'HOOKDIR=/var/cache/pbuilder/hooks' >> /root/.pbuilderrc
2828
RUN --security=insecure pbuilder create
29-
30-
COPY pbuilder_install_script.sh /root/pbuilder_install_script.sh
31-
RUN --security=insecure pbuilder execute --save-after-exec /root/pbuilder_install_script.sh

lib/pgpm/deb/builder.rb

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# frozen_string_literal: true
22

33
require "English"
4-
require "debug"
54

65
module 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"

lib/pgpm/deb/pbuilder_install_script.sh

Lines changed: 0 additions & 21 deletions
This file was deleted.

lib/pgpm/deb/spec.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@
77
module Pgpm
88
module Deb
99
class Spec
10-
attr_reader :package, :release, :postgres_distribution
10+
attr_reader :package, :release
1111

1212
def initialize(package)
13-
@postgres_distribution = Pgpm::Postgres::Distribution.in_scope
1413
@package = package
15-
@package.postgres_version = @postgres_distribution.version
1614
@release = 1
1715
end
1816

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env bash
2+
3+
# Run this script with:
4+
# fakeroot pbuilder execute --save-after-exec ./pbuilder_install_script.sh
5+
6+
apt update
7+
DEBIAN_FRONTEND=noninteractive apt -y install \
8+
build-essential curl lsb-release ca-certificates automake autopoint \
9+
autotools-dev bsdextrautils debhelper dh-autoreconf dh-strip-nondeterminism \
10+
dwz file gettext gettext-base groff-base intltool-debian libarchive-zip-perl \
11+
libdebhelper-perl libelf1t64 libfile-stripnondeterminism-perl libmagic-mgc \
12+
libmagic1t64 libpipeline1 libtool libuchardet0 man-db po-debconf
13+
14+
### PostgreSQL installation
15+
#
16+
install -d /usr/share/postgresql-common/pgdg
17+
curl -o /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc --fail https://www.postgresql.org/media/keys/ACCC4CF8.asc
18+
19+
# Create the repository configuration file:
20+
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'
21+
22+
# Update the package lists:
23+
apt update
24+
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 %>
34+
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: 11 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}",
15-
"postgresql-server-dev-#{postgres_version}",
14+
"postgresql-#{postgres_version(:major)}",
15+
"postgresql-server-dev-#{postgres_version(:major)}",
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_major_version}"]
29+
["postgresql-#{postgres_version(:major)}"]
3030
when "rocky+epel-9", "redhat", "fedora"
3131
[]
3232
end
@@ -44,6 +44,14 @@ def topologically_ordered_with_dependencies
4444
TopologicalPackageSorter.new([self, *all_requirements]).sorted_packages
4545
end
4646

47+
def postgres_version(version_type=:major_minor)
48+
v = Pgpm::Postgres::Distribution.in_scope.version
49+
if version_type == :major
50+
v = v.split(".").first
51+
end
52+
v
53+
end
54+
4755
class TopologicalPackageSorter
4856
include TSort
4957

lib/pgpm/rpm/spec.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@
66
module Pgpm
77
module RPM
88
class Spec
9-
attr_reader :package, :release, :postgres_distribution
9+
attr_reader :package, :release
1010

1111
def initialize(package)
1212
@postgres_distribution = Pgpm::Postgres::Distribution.in_scope
1313
@package = package
14-
@package.postgres_version = @postgres_distribution.version
1514
@release = 1
1615
end
1716

0 commit comments

Comments
 (0)