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, #ondemand_rpm_repo_baseurl, #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'], arch: ENV['ARCH'])
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



210
211
212
213
214
215
216
217
218
# File 'lib/ood_packaging/build.rb', line 210

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



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/ood_packaging/build.rb', line 236

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
  return unless config.fetch(:update_changelog, true)

  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_chlog_version} --controlmaint 'Release #{deb_chlog_version}'#{cmd_suffix}"
  end
end

#bootstrap_get_source!Object



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/ood_packaging/build.rb', line 220

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



192
193
194
195
196
197
# File 'lib/ood_packaging/build.rb', line 192

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



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/ood_packaging/build.rb', line 176

def bootstrap_rpm!
  puts '== Bootstrap RPM =='.blue
  bootstrap_gpg! if gpg_sign?
  if podman_runtime?
    puts "\tBootstrap /root".blue
    sh "cp -r #{ctr_rpmmacros} /root/"
    sh "cp -r #{ctr_gpg_dir} /root/" if gpg_sign? && build_box.dnf?
    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_rpm_packages! if config[:bootstrap_packages]
  bootstrap_copy_source!
  bootstrap_get_source!
end

#bootstrap_rpm_packages!Object



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

def bootstrap_rpm_packages!
  return if config[:bootstrap_packages].nil?

  cmd = ['sudo', 'dnf'] if build_box.dnf?
  cmd = ['sudo', 'yum'] unless build_box.dnf?
  cmd.concat ['install', '-y']
  cmd.concat config[:bootstrap_packages]
  puts "\tBootstrapping additional packages".blue
  sh cmd.join(' ')
end

#cmd_suffixObject



80
81
82
83
84
# File 'lib/ood_packaging/build.rb', line 80

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



309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/ood_packaging/build.rb', line 309

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



98
99
100
101
102
103
104
# File 'lib/ood_packaging/build.rb', line 98

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

#deb_chlog_versionObject



65
66
67
68
69
# File 'lib/ood_packaging/build.rb', line 65

def deb_chlog_version
  return "#{deb_version}-#{build_box.codename}" if config[:codename_version]

  deb_version
end

#deb_nameObject



144
145
146
# File 'lib/ood_packaging/build.rb', line 144

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

#deb_versionObject



61
62
63
# File 'lib/ood_packaging/build.rb', line 61

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

#deb_work_dirObject



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

def deb_work_dir
  File.join(work_dir, deb_name)
end

#debian_dirObject



106
107
108
109
110
111
112
113
114
# File 'lib/ood_packaging/build.rb', line 106

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



300
301
302
303
304
305
306
307
# File 'lib/ood_packaging/build.rb', line 300

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



170
171
172
173
174
# File 'lib/ood_packaging/build.rb', line 170

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

#fix_env!Object



166
167
168
# File 'lib/ood_packaging/build.rb', line 166

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

#gpg_sign!Object



331
332
333
334
335
336
337
338
339
340
341
# File 'lib/ood_packaging/build.rb', line 331

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



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

def install_deb_dependencies!
  sh "sudo apt update -y#{cmd_suffix}"
  tool = [
    'DEBIAN_FRONTEND=noninteractive apt-cudf-get --solver aspcud',
    '-o APT::Get::Assume-Yes=1 -o APT::Get::Allow-Downgrades=1',
    '-o Debug::pkgProblemResolver=0 -o APT::Install-Recommends=0'
  ]
  cmd = [
    'mk-build-deps --install --remove --root-cmd sudo',
    "--tool='#{tool.join(' ')}'"
  ]
  Dir.chdir(deb_work_dir) do
    sh "#{cmd.join(' ')}#{cmd_suffix}"
  end
end

#install_dependencies!Object



256
257
258
259
260
261
262
263
# File 'lib/ood_packaging/build.rb', line 256

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



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

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



124
125
126
# File 'lib/ood_packaging/build.rb', line 124

def output_dir
  File.join('/output', "#{build_box.dist}-#{build_box.arch}")
end

#packageObject



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

def package
  ENV['PACKAGE']
end

#packaging_configObject



132
133
134
135
136
137
138
139
140
141
142
# File 'lib/ood_packaging/build.rb', line 132

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



71
72
73
74
75
76
77
78
# File 'lib/ood_packaging/build.rb', line 71

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

#rpm_releaseObject



52
53
54
55
56
57
58
59
# File 'lib/ood_packaging/build.rb', line 52

def rpm_release
  return nil if version.nil?

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

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

#rpm_versionObject



48
49
50
# File 'lib/ood_packaging/build.rb', line 48

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

#rpmbuild!Object



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

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



148
149
150
# File 'lib/ood_packaging/build.rb', line 148

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

#run!Object



152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/ood_packaging/build.rb', line 152

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!
  show_output!
  gpg_sign! if build_box.rpm? && gpg_sign?
  sanity!
end

#sanity!Object



343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/ood_packaging/build.rb', line 343

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

#show_output!Object



324
325
326
327
328
329
# File 'lib/ood_packaging/build.rb', line 324

def show_output!
  puts '== Copied output =='.blue
  Dir["#{output_dir}/*"].each do |f|
    puts "\tSaved output #{f}".blue
  end
end

#spec_dirObject



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

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



120
121
122
# File 'lib/ood_packaging/build.rb', line 120

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

#versionObject



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

def version
  ver = ENV['VERSION']
  return nil if ver == ''

  ver
end

#work_dirObject



128
129
130
# File 'lib/ood_packaging/build.rb', line 128

def work_dir
  build_box.work_dir
end