Class: OodPackaging::Build

Inherits:
Object
  • Object
show all
Includes:
FileUtils, Utils
Defined in:
lib/ood_packaging/build.rb

Overview

Class to handle builds of packages from within buildbox container

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#container_runtime, #ctr_gems_dir, #ctr_gpg_dir, #ctr_home, #ctr_rpmmacros, #ctr_scripts_dir, #ctr_user, #docker_runtime?, #gpg_passphrase, #gpg_private_key, #nodejs_version, #ondemand_repo_version, #podman_runtime?, #proj_root, #rt_specific_flags, #ruby_version, #scl_ruby, #sed, #tar, #template_file

Constructor Details

#initializeBuild

Returns a new instance of Build.



17
18
19
# File 'lib/ood_packaging/build.rb', line 17

def initialize
  @build_box = OodPackaging::BuildBox.new(dist: ENV['DIST'])
end

Instance Attribute Details

#build_boxObject

Returns the value of attribute build_box.



15
16
17
# File 'lib/ood_packaging/build.rb', line 15

def build_box
  @build_box
end

Instance Method Details

#bootstrap_copy_source!Object



184
185
186
187
188
189
190
191
192
# File 'lib/ood_packaging/build.rb', line 184

def bootstrap_copy_source!
  puts "\tCopy sources".blue
  if build_box.rpm?
    sh "find #{spec_dir} -maxdepth 1 -type f -exec cp {} #{work_dir}/SOURCES/ \\;"
    sh "find #{spec_dir} -maxdepth 1 -mindepth 1 -type d -exec cp -r {} #{work_dir}/SOURCES/ \\;"
  elsif build_box.deb?
    sh "cp -a #{deb_build_dir}/* #{work_dir}/"
  end
end

#bootstrap_deb!Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/ood_packaging/build.rb', line 210

def bootstrap_deb!
  puts '== Bootstrap DEB =='.blue
  unless Dir.exist?(work_dir)
    puts "\tCreating #{work_dir}".blue
    sh "mkdir -p #{work_dir}"
  end
  bootstrap_copy_source!
  puts "\tExtract source".blue
  Dir.chdir(work_dir) do
    sh "tar -xf #{deb_name}.tar.gz"
  end
  puts "\tBootstrap debian build files".blue
  Dir.chdir(deb_work_dir) do
    sh "dh_make -s -y --createorig -f ../#{deb_name}.tar.gz#{cmd_suffix} || true"
    sh "dch -b -v #{deb_version} --controlmaint 'Release #{deb_version}'#{cmd_suffix}"
  end
end

#bootstrap_get_source!Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/ood_packaging/build.rb', line 194

def bootstrap_get_source!
  if ENV['SKIP_DOWNLOAD'] == 'true'
    puts "\tSKIP_DOWNLOAD detected, skipping download sources".blue
    return
  end
  output = `spectool #{rpm_defines.join(' ')} -l -R -S #{spec_file} 2>&1 | grep 'Source0:'`.strip
  exit_code = $CHILD_STATUS.exitstatus
  if exit_code.zero?
    source = File.join(work_dir, 'SOURCES', File.basename(output))
    tar = File.join(work_dir, 'SOURCES', ENV['TAR_NAME'])
    sh "mv #{tar} #{source}" if !File.exist?(source) && File.exist?(tar)
  end
  puts "\tDownloading sources defined in #{spec_file}".blue
  sh "spectool #{rpm_defines.join(' ')} -g -R -S #{spec_file}#{cmd_suffix}"
end

#bootstrap_gpg!Object



177
178
179
180
181
182
# File 'lib/ood_packaging/build.rb', line 177

def bootstrap_gpg!
  puts "\tBootstrap GPG".blue
  sh "sed -i 's|@GPG_NAME@|#{ENV['GPG_NAME']}|g' #{ctr_rpmmacros}"
  sh "gpg --batch --passphrase-file #{gpg_passphrase} --import #{gpg_private_key}#{cmd_suffix}"
  sh "sudo rpm --import #{ENV['GPG_PUBKEY']}#{cmd_suffix}" if ENV['GPG_PUBKEY']
end

#bootstrap_rpm!Object



163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/ood_packaging/build.rb', line 163

def bootstrap_rpm!
  puts '== Bootstrap RPM =='.blue
  bootstrap_gpg! if gpg_sign?
  if podman_runtime?
    puts "\tBootstrap /root".blue
    sh "cp -r #{ctr_rpmmacros} #{ctr_gpg_dir} /root/"
    sh "sed -i 's|/home/ood|/root|g' /root/.rpmmacros"
  end
  puts "\tBootstrap work dir".blue
  sh "mkdir -p #{work_dir}/{RPMS,SRPMS,SOURCES,SPECS,rpmbuild/BUILD}"
  bootstrap_copy_source!
  bootstrap_get_source!
end

#cmd_suffixObject



68
69
70
71
72
# File 'lib/ood_packaging/build.rb', line 68

def cmd_suffix
  return '' if debug?

  ' 2>/dev/null 1>/dev/null'
end

#configObject



21
22
23
24
25
26
27
# File 'lib/ood_packaging/build.rb', line 21

def config
  @config ||= begin
    c = packaging_config
    c.merge!(c[build_box.dist]) if c.key?(build_box.dist)
    c.transform_keys(&:to_sym)
  end
end

#copy_output!Object



276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/ood_packaging/build.rb', line 276

def copy_output!
  puts '== Copy output =='.blue
  unless Dir.exist?(output_dir)
    puts "\tCreating #{output_dir}".blue
    sh "mkdir -p #{output_dir}"
  end
  if build_box.rpm?
    puts "\tcopy #{work_dir}/**/*.rpm -> #{output_dir}/".blue
    sh "find #{work_dir} -type f -name '*.rpm' -exec cp {} #{output_dir}/ \\;"
  elsif build_box.deb?
    puts "\tcopy #{work_dir}/*.deb #{output_dir}/".blue
    sh "cp #{work_dir}/*.deb #{output_dir}/"
  end
end

#deb_build_dirObject



86
87
88
89
90
91
92
# File 'lib/ood_packaging/build.rb', line 86

def deb_build_dir
  @deb_build_dir ||= if Dir.exist?('/package/deb/build')
                       '/package/deb/build'
                     else
                       '/package/build'
                     end
end

#deb_nameObject



132
133
134
# File 'lib/ood_packaging/build.rb', line 132

def deb_name
  "#{package}-#{deb_version}"
end

#deb_versionObject



56
57
58
# File 'lib/ood_packaging/build.rb', line 56

def deb_version
  version.gsub(/^v/, '').gsub('-', '.')
end

#deb_work_dirObject



104
105
106
# File 'lib/ood_packaging/build.rb', line 104

def deb_work_dir
  File.join(work_dir, deb_name)
end

#debian_dirObject



94
95
96
97
98
99
100
101
102
# File 'lib/ood_packaging/build.rb', line 94

def debian_dir
  @debian_dir ||= if Dir.exist?('/package/deb/debian')
                    '/package/deb/debian'
                  elsif Dir.exist?('/package/packaging/deb')
                    '/package/packaging/deb'
                  else
                    '/package/debian'
                  end
end

#debug?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/ood_packaging/build.rb', line 33

def debug?
  ENV['DEBUG'] == 'true'
end

#debuild!Object



267
268
269
270
271
272
273
274
# File 'lib/ood_packaging/build.rb', line 267

def debuild!
  puts "== DEB build package=#{deb_work_dir} ==".blue
  prepend_path = ''
  prepend_path = "--prepend-path=#{config[:prepend_path]}" if config[:prepend_path]
  Dir.chdir(deb_work_dir) do
    sh "debuild --no-lintian --preserve-env #{prepend_path}#{cmd_suffix}"
  end
end

#env_dump!Object



157
158
159
160
161
# File 'lib/ood_packaging/build.rb', line 157

def env_dump!
  ENV.sort.to_h.each_pair do |k, v|
    puts "#{k}=#{v}"
  end
end

#fix_env!Object



153
154
155
# File 'lib/ood_packaging/build.rb', line 153

def fix_env!
  ENV.delete('GEM_PATH')
end

#gpg_sign!Object



291
292
293
294
295
296
297
298
299
300
301
# File 'lib/ood_packaging/build.rb', line 291

def gpg_sign!
  puts '== GPG sign RPMs =='.blue
  rpms.each do |rpm|
    puts "\tGPG signing #{rpm}".blue
    cmd = []
    # Work around differences in RHEL
    cmd.concat ['cat /dev/null | setsid'] unless build_box.dnf?
    cmd.concat ['rpmsign', '--addsign', rpm]
    sh "#{cmd.join(' ')}#{cmd_suffix}"
  end
end

#gpg_sign?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/ood_packaging/build.rb', line 37

def gpg_sign?
  ENV['GPG_SIGN'] == 'true'
end

#install_deb_dependencies!Object



248
249
250
251
252
253
254
255
256
257
# File 'lib/ood_packaging/build.rb', line 248

def install_deb_dependencies!
  sh "sudo apt update -y#{cmd_suffix}"
  cmd = [
    'mk-build-deps --install --remove --root-cmd sudo',
    "--tool='DEBIAN_FRONTEND=noninteractive apt-get -o Debug::pkgProblemResolver=yes --no-install-recommends --yes'"
  ]
  Dir.chdir(deb_work_dir) do
    sh "#{cmd.join(' ')}#{cmd_suffix}"
  end
end

#install_dependencies!Object



228
229
230
231
232
233
234
235
# File 'lib/ood_packaging/build.rb', line 228

def install_dependencies!
  puts '== Install Dependencies =='.blue
  if build_box.rpm?
    install_rpm_dependencies!
  elsif build_box.deb?
    install_deb_dependencies!
  end
end

#install_rpm_dependencies!Object



237
238
239
240
241
242
243
244
245
246
# File 'lib/ood_packaging/build.rb', line 237

def install_rpm_dependencies!
  cmd = ['sudo']
  cmd.concat [build_box.package_manager, 'builddep'] if build_box.dnf?
  cmd.concat ['yum-builddep'] if build_box.package_manager == 'yum'
  cmd.concat ['-y']
  cmd.concat rpm_defines
  cmd.concat ['--spec'] if build_box.dnf?
  cmd.concat [spec_file]
  sh "#{cmd.join(' ')}#{cmd_suffix}"
end

#output_dirObject



112
113
114
# File 'lib/ood_packaging/build.rb', line 112

def output_dir
  File.join('/output', build_box.dist)
end

#packageObject



29
30
31
# File 'lib/ood_packaging/build.rb', line 29

def package
  ENV['PACKAGE']
end

#packaging_configObject



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/ood_packaging/build.rb', line 120

def packaging_config
  @packaging_config ||= begin
    path = File.join(spec_dir, 'packaging.yaml')
    path = File.join(debian_dir, 'packaging.yaml') if build_box.deb?
    if File.exist?(path)
      YAML.load_file(path)
    else
      {}
    end
  end
end

#rpm_definesObject



60
61
62
63
64
65
66
# File 'lib/ood_packaging/build.rb', line 60

def rpm_defines
  defines = ["--define 'git_tag #{version}'"]
  defines.concat ["--define 'package_version #{rpm_version}'"]
  defines.concat ["--define 'package_release #{rpm_release}'"]
  defines.concat ["--define 'scl #{config[:scl]}'"] if config[:scl]
  defines
end

#rpm_releaseObject



49
50
51
52
53
54
# File 'lib/ood_packaging/build.rb', line 49

def rpm_release
  v = version.split('-', 2)
  return '1' if v.size < 2

  v[1].gsub('-', '.')
end

#rpm_versionObject



45
46
47
# File 'lib/ood_packaging/build.rb', line 45

def rpm_version
  version.gsub(/^v/, '').split('-', 2)[0]
end

#rpmbuild!Object



259
260
261
262
263
264
265
# File 'lib/ood_packaging/build.rb', line 259

def rpmbuild!
  puts "== RPM build spec=#{spec_file} ==".blue
  cmd = ['rpmbuild', '-ba']
  cmd.concat rpm_defines
  cmd.concat [spec_file]
  sh "#{cmd.join(' ')}#{cmd_suffix}"
end

#rpmsObject



136
137
138
# File 'lib/ood_packaging/build.rb', line 136

def rpms
  @rpms ||= Dir["#{output_dir}/*.rpm"].grep_v(/.src.rpm$/)
end

#run!Object



140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/ood_packaging/build.rb', line 140

def run!
  fix_env!
  env_dump! if debug?
  bootstrap_rpm! if build_box.rpm?
  bootstrap_deb! if build_box.deb?
  install_dependencies!
  rpmbuild! if build_box.rpm?
  debuild! if build_box.deb?
  copy_output!
  gpg_sign! if build_box.rpm? && gpg_sign?
  sanity!
end

#sanity!Object



303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/ood_packaging/build.rb', line 303

def sanity!
  puts '== Sanity tests =='.blue
  failure = false
  if build_box.rpm? && gpg_sign?
    rpms.each do |rpm|
      puts "\tTest GPG signing #{rpm}".blue
      output = `rpm -K #{rpm} 2>&1`
      exit_code = $CHILD_STATUS.exitstatus
      puts output if debug?
      if exit_code != 0
        puts "\tGPG check failure: exit code #{exit_code}".red
        failure = true
      end
      if output !~ /(pgp|OK)/
        puts "\tRPM not GPG signed".red
        failure = true
      end
    end
  end
  exit 1 if failure
end

#spec_dirObject



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ood_packaging/build.rb', line 74

def spec_dir
  @spec_dir ||= if Dir.exist?('/package/rpm')
                  '/package/rpm'
                elsif Dir.exist?('/package/packaging/rpm')
                  '/package/packaging/rpm'
                elsif Dir.exist?('/package/packaging')
                  '/package/packaging'
                else
                  '/package'
                end
end

#spec_fileObject



108
109
110
# File 'lib/ood_packaging/build.rb', line 108

def spec_file
  @spec_file ||= Dir["#{spec_dir}/*.spec"][0]
end

#versionObject



41
42
43
# File 'lib/ood_packaging/build.rb', line 41

def version
  ENV['VERSION']
end

#work_dirObject



116
117
118
# File 'lib/ood_packaging/build.rb', line 116

def work_dir
  File.join('/work', build_box.dist)
end