Skip to content

Commit cb58e0a

Browse files
author
qount25
committed
Create second .deb package (default) which depends on the versioned package in Pgpm::Deb::Builder
1 parent 71bcfdd commit cb58e0a

File tree

8 files changed

+134
-90
lines changed

8 files changed

+134
-90
lines changed

lib/pgpm/deb/builder.rb

Lines changed: 82 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env bash
2+
3+
ext_dir="$PGPM_BUILDDEB/$(pg_config --sharedir)/extension"
4+
control_fn="$ext_dir/$PGPM_EXTENSION_NAME.control"
5+
6+
echo "Creating extension dir: $ext_dir"
7+
mkdir -p "$ext_dir"
8+
9+
echo "Creating control file: $control_fn"
10+
cp "$PGPM_BUILDROOT/$PGPM_EXTENSION_NAME.control" "$ext_dir/"
11+
echo >> "$control_fn"
12+
echo "default_version = '$PGPM_EXTENSION_VERSION'" >> "$control_fn"

lib/pgpm/deb/scripts/pg_config.sh

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

lib/pgpm/deb/spec.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,26 @@ def sources
2020
@package.sources
2121
end
2222

23-
def generate(template_name)
23+
def generate(template_name, pkg_type=:versioned)
2424
fn = "#{__dir__}/templates/#{template_name}.erb"
2525
raise "No such template: #{fn}" unless File.exist?(fn)
2626
erb = ERB.new(File.read(fn))
27+
28+
# Uses pkg_type parameter (which is in scope) to generate
29+
# debian/* files for versionless and main packages.
2730
erb.result(binding)
2831
end
2932

3033
def source_version
3134
@package.version.to_s
3235
end
3336

34-
def deb_pkg_name
35-
"#{@package.name}+#{@package.version.to_s}-pg#{@package.postgres_major_version}"
37+
def deb_pkg_name(type=:versioned)
38+
if type == :versioned
39+
"#{@package.name}+#{@package.version.to_s}-pg#{@package.postgres_major_version}"
40+
else
41+
"#{@package.name}-pg#{@package.postgres_major_version}"
42+
end
3643
end
3744

3845
def arch

lib/pgpm/deb/templates/changelog.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
<%= deb_pkg_name %> (0-1) UNRELEASED; urgency=medium
1+
<%= deb_pkg_name(pkg_type) %> (0-1) UNRELEASED; urgency=medium
22

3-
* Version <%= source_version %> package release.
3+
* Version <%= pkg_type == :versioned ? 1 : source_version %> package release.
44

55
-- PGPM Debian maintainer <debian.maintainer@pgpm.org> <%= Time.now.strftime('%a, %d %b %Y %H:%M:%S %z')%>

lib/pgpm/deb/templates/control.erb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Source: <%= deb_pkg_name %>
1+
Source: <%= deb_pkg_name(pkg_type) %>
22
Description: <%= self.package.description %>
33
Section: libs
44
Priority: optional
@@ -7,8 +7,8 @@ Rules-Requires-Root: no
77
Build-Depends: debhelper-compat (= 13), <%= self.package.build_dependencies.join(", ") %>
88
Standards-Version: 4.6.2
99

10-
Package: <%= deb_pkg_name %>
11-
Depends: <%= self.package.dependencies.join(", ") %>
10+
Package: <%= deb_pkg_name(pkg_type) %>
11+
Depends: <%= pkg_type == :versioned ? self.package.dependencies.join(", ") : deb_pkg_name(:versioned) %>
1212
Section: libdevel
1313
Architecture: <%= arch %>
1414
Description: <%= self.package.description %>

lib/pgpm/deb/templates/files.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<%= deb_pkg_name %>_source.buildinfo libs optional
1+
<%= deb_pkg_name(pkg_type) %>_source.buildinfo libs optional

lib/pgpm/deb/templates/rules.erb

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,40 @@
11
#!/usr/bin/make -f
22

3-
# See debhelper(7) (uncomment to enable).
4-
# Output every command that modifies files on the build system.
5-
#export DH_VERBOSE = 1
6-
7-
8-
# See FEATURE AREAS in dpkg-buildflags(1).
9-
#export DEB_BUILD_MAINT_OPTIONS = hardening=+all
10-
11-
# See ENVIRONMENT in dpkg-buildflags(1).
12-
# Package maintainers to append CFLAGS.
13-
#export DEB_CFLAGS_MAINT_APPEND = -Wall -pedantic
14-
# Package maintainers to append LDFLAGS.
15-
#export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed
16-
173
DEB_BUILDDIR ?= $(CURDIR)
4+
EXTENSION_NAME ?= "<%= self.package.extension_name %>"
185

6+
<% if pkg_type == :versioned %>
197
%:
208
dh $@
219

2210
override_dh_install:
2311
dh_install
2412
chmod +x "$(DEB_BUILDDIR)/prepare_artifacts.sh"
25-
chmod +x "$(DEB_BUILDDIR)/pg_config.sh"
2613
find $(DEB_BUILDDIR) -type f | sort - | sed 's|^$(DEB_BUILDDIR)||' > .pgpm_before | sort
27-
export PG_CONFIG="$(DEB_BUILDDIR)/pg_config.sh"; \
14+
export PG_CONFIG="which pg_config"; \
2815
export PGPM_BUILDROOT="$(DEB_BUILDDIR)"; \
29-
export PGPM_EXTENSION_NAME="<%= self.package.extension_name %>"; \
16+
export PGPM_EXTENSION_NAME=$(EXTENSION_NAME); \
3017
export PGPM_EXTENSION_VERSION="<%= self.package.version %>"; \
31-
export PGPM_BUILDDEB="$(DEB_BUILDDIR)/debian/<%= deb_pkg_name %>"; \
18+
export PGPM_BUILDDEB="$(DEB_BUILDDIR)/debian/<%= deb_pkg_name(pkg_type) %>"; \
3219
./prepare_artifacts.sh
3320

3421
<%= self.package.build_info[:rules] %>
22+
23+
<% else %>
24+
%:
25+
dh $@
26+
27+
build:
28+
# Custom build commands
29+
echo " --> BUILD"
30+
31+
install:
32+
echo " --> INSTALL"
33+
chmod +x "$(DEB_BUILDDIR)/install_default_control.sh"
34+
export PG_CONFIG="which pg_config"; \
35+
export PGPM_BUILDROOT="$(DEB_BUILDDIR)"; \
36+
export PGPM_EXTENSION_NAME=$(EXTENSION_NAME); \
37+
export PGPM_EXTENSION_VERSION="<%= self.package.version %>"; \
38+
export PGPM_BUILDDEB="$(DEB_BUILDDIR)/debian/<%= deb_pkg_name(pkg_type) %>"; \
39+
./install_default_control.sh
40+
<% end %>

0 commit comments

Comments
 (0)