@@ -9,15 +9,25 @@ class Builder
9
9
def initialize ( spec )
10
10
@spec = spec
11
11
@container_name = "pgpm-debian_build-#{ Time . now . to_i } _#{ rand ( 10000 ) } "
12
+ @pgpm_dir = Dir . mktmpdir
12
13
end
13
14
14
15
def build
15
- prepare
16
- generate_deb_src_files
17
16
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
21
31
end
22
32
23
33
private
@@ -27,11 +37,10 @@ def image_name
27
37
"quay.io/qount25/pgpm-debian-pg#{ @spec . package . postgres_major_version } -#{ @spec . arch } "
28
38
end
29
39
30
- def prepare
40
+ def prepare_versioned_source
31
41
puts "Preparing build..."
32
42
puts " Creating container dir structure..."
33
- @pgpm_dir = Dir . mktmpdir
34
- Dir . mkdir "#{ @pgpm_dir } /source"
43
+ Dir . mkdir "#{ @pgpm_dir } /source-versioned"
35
44
Dir . mkdir "#{ @pgpm_dir } /out"
36
45
37
46
puts " Downloading and unpacking sources to #{ @pgpm_dir } "
@@ -43,29 +52,57 @@ def prepare
43
52
fn = src . name
44
53
end
45
54
46
- system ( "tar -xf #{ @pgpm_dir } /#{ fn } -C #{ @pgpm_dir } /source/" )
55
+ system ( "tar -xf #{ @pgpm_dir } /#{ fn } -C #{ @pgpm_dir } /source-versioned /" )
47
56
FileUtils . remove ( "#{ @pgpm_dir } /#{ fn } " )
48
57
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 |
50
59
!( [ "." , ".." ] . include? ( entry ) )
51
60
end
52
61
53
62
if untar_dir_entries . size == 1
54
63
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 "
59
68
end
60
69
end
61
70
62
- [ "prepare_artifacts.sh" , "pg_config.sh" ] . each do |fn |
71
+ [ "prepare_artifacts.sh" ] . each do |fn |
63
72
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 /"
65
74
end
66
75
67
76
end
68
77
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
+
69
106
def pull_image
70
107
puts "Checking if podman image exists..."
71
108
# Check if image exists
@@ -78,62 +115,65 @@ def pull_image
78
115
end
79
116
end
80
117
81
- def generate_deb_src_files
118
+ def generate_deb_src_files ( pkg_type = :versioned )
82
119
puts "Generating debian files..."
83
- Dir . mkdir "#{ @pgpm_dir } /source/debian"
120
+ Dir . mkdir "#{ @pgpm_dir } /source- #{ pkg_type } /debian"
84
121
[ :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 )
87
124
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
89
126
end
90
127
91
- def run_build
128
+ def start_container
92
129
# podman create options
93
130
create_opts = " -v #{ @pgpm_dir } :/root/pgpm"
94
131
create_opts += ":z" if selinux_enabled?
95
132
create_opts += " --privileged --tmpfs /tmp"
96
133
create_opts += " --name #{ @container_name } #{ image_name } "
97
134
98
- dsc_fn = "#{ @spec . deb_pkg_name } _0-1.dsc"
99
- deb_fn = "#{ @spec . deb_pkg_name } _0-1_#{ @spec . arch } .deb"
100
-
101
135
puts " Creating and starting container #{ @container_name } & running pbuilder"
102
136
system ( "podman create -it #{ create_opts } " )
103
137
exit ( 1 ) if $?. to_i > 0
104
138
system ( "podman start #{ @container_name } " )
105
139
exit ( 1 ) if $?. to_i > 0
140
+ end
106
141
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
108
156
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"
120
160
161
+ cmds = [ ]
121
162
cmds << "dpkg-buildpackage --build=source -d" # -d flag helps with dependencies error
122
163
cmds << "fakeroot pbuilder build ../#{ dsc_fn } "
123
164
cmds << "mv /var/cache/pbuilder/result/#{ deb_fn } /root/pgpm/out/"
124
165
125
166
puts " Building package with pbuilder..."
126
167
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 } '" )
128
169
exit ( 1 ) if $?. to_i > 0
129
170
end
130
-
131
171
end
132
172
133
- def copy_build_from_container
173
+ def copy_build_from_container ( pkg_type = :versioned )
134
174
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"
137
177
FileUtils . cp ( "#{ @pgpm_dir } /out/#{ deb_fn } " , "#{ Dir . pwd } /#{ deb_copy_fn } " )
138
178
end
139
179
0 commit comments