Skip to content

Commit

Permalink
Problem: can't build linux package under macOS
Browse files Browse the repository at this point in the history
The problem is generally solved by running inside the specialized `pgpm`
container which has necessary dependencies installed.

However, this requires us to run it there.

Solution: podmanize everything!

We already use podman to bring components we may not have – like rust
toolchain. Why not use it to bring `mock` in?

This is not perfect yet, and relies on volume mapping (which is what we do
already anyway) – for which we had to do special accommodations to ensure
we know these paths and they are absolute.

We currently use the `pgpm` image as the one that should contain `mock`. But
may be it should be a different, smaller image?
  • Loading branch information
yrashk committed Feb 8, 2025
1 parent 109d3e5 commit 93663f5
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
4 changes: 2 additions & 2 deletions exe/pgpm
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module Pgpm

include SharedOptions

option :os, type: :string, default: Pgpm::OS.auto_detect.name, desc: "OS name"
option :os, type: :string, default: Pgpm::OS.auto_detect&.name, desc: "OS name"
option :arch, type: :string, default: Pgpm::Arch.host.name, desc: "Target architecture"
option :pgdist, type: :string, default: "pgdg", desc: "Target Postgres distribution"
option :pgver, type: :string, default: Pgpm::Postgres::Distribution.versions.last.to_s, desc: "Target Postgres version"
Expand Down Expand Up @@ -71,7 +71,7 @@ module Pgpm
pkg
end

os = Pgpm::OS.auto_detect
os = os ? Pgpm::OS.find(os) : Pgpm::OS.auto_detect
arch = if arch
Pgpm::Arch.new(arch)
else
Expand Down
2 changes: 1 addition & 1 deletion lib/pgpm/arch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def self.in_scope
end

def initialize(name)
@name = name
@name = name == "arm64" ? "aarch64" : name
end

attr_reader :name
Expand Down
4 changes: 4 additions & 0 deletions lib/pgpm/os.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ def self.auto_detect
Pgpm::OS::Linux.auto_detect
end

def self.find(name)
Base.all_subclasses.find { |klass| klass.name == name }&.new
end

def self.in_scope
LSpace[:pgpm_target_operating_system]
end
Expand Down
19 changes: 13 additions & 6 deletions lib/pgpm/rpm/mock/operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ def self.buildsrpm(spec, sources, config: nil, result_dir: nil, cb: nil)
]
args.push("--sources", sources) if sources
args.push("-r", config.to_s) unless config.nil?
new(*args, cb: lambda {
paths = [buffer_result_dir]
paths.push(File.dirname(spec))
paths.push(File.dirname(sources)) if sources
new(*args, paths:, cb: lambda {
rpms = Dir.glob("*.rpm", base: buffer_result_dir).map do |f|
FileUtils.cp(Pathname(buffer_result_dir).join(f), result_dir) unless result_dir.nil?
f
File.join(File.absolute_path(result_dir), f)
end
FileUtils.rm_rf(buffer_result_dir)
cb.call unless cb.nil?
Expand All @@ -29,22 +32,25 @@ def self.rebuild(srpm, config: nil, result_dir: nil, cb: nil)
"--rebuild", "--chain", "--recurse", srpm, "--localrepo", buffer_result_dir
]
args.push("-r", config.to_s) unless config.nil?
new(*args, cb: lambda {
paths = [buffer_result_dir]
paths.push(File.dirname(srpm))
new(*args, paths:, cb: lambda {
# Here we glob for **/*.rpm as ``--localrepo` behaves differently from
# `--resultdir`
rpms = Dir.glob("**/*.rpm", base: buffer_result_dir).map do |f|
FileUtils.cp(Pathname(buffer_result_dir).join(f), result_dir) unless result_dir.nil?
f
File.join(File.absolute_path(result_dir), f)
end
FileUtils.rm_rf(buffer_result_dir)
cb.call unless cb.nil?
rpms
})
end

def initialize(*args, opts: nil, cb: nil)
def initialize(*args, opts: nil, paths: [], cb: nil)
@args = args
@cb = cb
@paths = paths
@opts = opts || { "print_main_output" => "True", "pgdg_version" => Postgres::Distribution.in_scope.major_version }
end

Expand All @@ -53,7 +59,8 @@ def initialize(*args, opts: nil, cb: nil)
def call
options = @opts.flat_map { |(k, v)| ["--config-opts", "#{k}=#{v}"] }.compact.join(" ")
command = "mock #{options} #{@args.join(" ")}"
raise "Failed to execute `#{command}`" unless system command
map_paths = @paths.map { |p| "-v #{p}:#{p}" }.join(" ")
raise "Failed to execute `#{command}`" unless Podman.run("run -v #{Dir.pwd}:#{Dir.pwd} #{map_paths} --privileged -ti ghcr.io/postgres-pm/pgpm #{command}")

@cb&.call
end
Expand Down

0 comments on commit 93663f5

Please sign in to comment.