Class: OodPackaging::Package

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

Overview

The interface to build packages using containers

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

#initialize(config = {}) ⇒ Package

Returns a new instance of Package.

Raises:

  • (ArgumentError)


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

def initialize(config = {})
  @config = config
  @config[:dist] = 'el8' if tar_only?
  @build_box = OodPackaging::BuildBox.new(config)
  raise ArgumentError, 'Package is required' if package.nil?
  raise ArgumentError, 'Version is required' if version.nil?
  raise ArgumentError, "Package #{package} is not a directory" unless Dir.exist?(package)
  raise ArgumentError, "Package #{package} is not an absolute path" unless (Pathname.new package).absolute?
end

Instance Attribute Details

#build_boxObject

Returns the value of attribute build_box.



18
19
20
# File 'lib/ood_packaging/package.rb', line 18

def build_box
  @build_box
end

Instance Method Details

#attach?Boolean

Returns:

  • (Boolean)


46
47
48
49
50
# File 'lib/ood_packaging/package.rb', line 46

def attach?
  return true if ENV['OOD_PACKAGING_ATTACH'] == 'true'

  @config[:attach].nil? ? false : @config[:attach]
end

#bootstrap!Object



227
228
229
230
# File 'lib/ood_packaging/package.rb', line 227

def bootstrap!
  sh "mkdir -p #{work_dir}", verbose: debug?
  sh "mkdir -p #{output_dir}", verbose: debug?
end

#clean!Object



222
223
224
225
# File 'lib/ood_packaging/package.rb', line 222

def clean!
  sh "rm -rf #{work_dir}", verbose: debug? if clean_work_dir
  sh "rm -rf #{output_dir}", verbose: debug? if clean_output_dir
end

#clean_output_dirObject



66
67
68
69
70
# File 'lib/ood_packaging/package.rb', line 66

def clean_output_dir
  return false if ENV['OOD_PACKAGING_CLEAN_OUTPUT_DIR'] == 'false'

  @config[:clean_output_dir].nil? ? true : @config[:clean_output_dir]
end

#clean_work_dirObject



56
57
58
59
60
# File 'lib/ood_packaging/package.rb', line 56

def clean_work_dir
  return false if ENV['OOD_PACKAGING_CLEAN_WORK_DIR'] == 'false'

  @config[:clean_work_dir].nil? ? true : @config[:clean_work_dir]
end

#cmd_suffixObject



40
41
42
43
44
# File 'lib/ood_packaging/package.rb', line 40

def cmd_suffix
  return '' if debug?

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

#container_attach!Object



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

def container_attach!
  container_exec!(exec_attach, ['-i', '-t']) if attach?
ensure
  container_kill! if container_running?
end

#container_envObject



339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/ood_packaging/package.rb', line 339

def container_env
  env = {
    'DIST'          => build_box.dist,
    'ARCH'          => build_box.arch,
    'PACKAGE'       => package_name,
    'VERSION'       => version,
    'TAR_NAME'      => "#{tar_name}.tar.gz",
    'GPG_SIGN'      => gpg_sign,
    'GPG_NAME'      => gpg_name,
    'SKIP_DOWNLOAD' => @config[:skip_download],
    'OOD_UID'       => Process.uid,
    'OOD_GID'       => Process.gid,
    'DEBUG'         => debug?
  }
  env['GPG_PUBKEY'] = '/gpg.pub' if @config[:gpg_pubkey]
  env
end

#container_exec!(exec_cmd, extra_args = []) ⇒ Object



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

def container_exec!(exec_cmd, extra_args = [])
  cmd = [container_runtime, 'exec']
  cmd.concat extra_args
  container_env.each_pair do |k, v|
    cmd.concat ['-e', "'#{k}=#{v}'"] unless v.nil?
  end
  cmd.concat [container_name]
  cmd.concat exec_cmd
  puts "Build STARTED: package=#{package} dist=#{build_box.dist} exec=#{exec_cmd[-1]}".blue
  sh cmd.join(' '), verbose: debug?
  true
rescue RuntimeError
  container_kill! if container_running? && !attach?
  raise
end

#container_initObject



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

def container_init
  '/sbin/init'
end

#container_kill!Object



330
331
332
333
334
335
336
337
# File 'lib/ood_packaging/package.rb', line 330

def container_kill!
  puts "Killing container #{container_name}".blue
  cmd = [container_runtime, 'kill', container_name]
  cmd.concat [cmd_suffix] unless debug?
  sh cmd.join(' '), verbose: debug?
rescue RuntimeError
  puts 'Error killing container'.red
end

#container_mountsObject



209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/ood_packaging/package.rb', line 209

def container_mounts
  args = []
  args.concat ['-v', "#{package}:/package:ro"]
  args.concat ['-v', "#{@config[:gpg_pubkey]}:/gpg.pub:ro"] if @config[:gpg_pubkey]
  args.concat ['-v', "#{work_dir}:/work"]
  args.concat ['-v', "#{output_dir}:/output"]
  if gpg_sign
    args.concat ['-v', "#{gpg_files.private_key}:#{gpg_private_key}:ro"]
    args.concat ['-v', "#{gpg_files.passphrase}:#{gpg_passphrase}:ro"]
  end
  args
end

#container_nameObject



30
31
32
# File 'lib/ood_packaging/package.rb', line 30

def container_name
  @container_name ||= SecureRandom.uuid
end

#container_running?Boolean

rubocop:enable Metrics/AbcSize

Returns:

  • (Boolean)


288
289
290
291
292
293
# File 'lib/ood_packaging/package.rb', line 288

def container_running?
  cmd = "#{container_runtime} inspect #{container_name} 2>/dev/null 1>/dev/null"
  puts cmd if debug?
  `#{cmd}`
  $CHILD_STATUS.success?
end

#container_start!Object



295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/ood_packaging/package.rb', line 295

def container_start!
  cmd = [container_runtime, 'run', '--detach', '--rm']
  cmd.concat ['--platform', build_box.platform]
  cmd.concat ['--name', container_name]
  cmd.concat rt_specific_flags
  cmd.concat container_mounts
  cmd.concat [build_box.image_tag]
  cmd.concat [container_init]
  cmd.concat ['1>/dev/null'] unless debug?
  puts "Starting container #{container_name} using image #{build_box.image_tag}".blue
  sh cmd.join(' '), verbose: debug?
end

#deb_tar_dest_dirObject



173
174
175
176
177
178
# File 'lib/ood_packaging/package.rb', line 173

def deb_tar_dest_dir
  dir = File.join(package, 'deb')
  return File.join(dir, 'build') if Dir.exist?(dir)

  File.join(package, 'build')
end

#deb_versionObject



180
181
182
# File 'lib/ood_packaging/package.rb', line 180

def deb_version
  tar_version.gsub('-', '.')
end

#debug?Boolean

Returns:

  • (Boolean)


34
35
36
37
38
# File 'lib/ood_packaging/package.rb', line 34

def debug?
  return true if ENV['OOD_PACKAGING_DEBUG'] == 'true'

  @config[:debug].nil? ? false : @config[:debug]
end

#default_gpg_nameObject



126
127
128
129
130
# File 'lib/ood_packaging/package.rb', line 126

def default_gpg_name
  return 'OnDemand Release Signing Key' if build_box.legacy_gpg?

  'OnDemand Release Signing Key (SHA512)'
end

#exec_attachObject



202
203
204
205
206
207
# File 'lib/ood_packaging/package.rb', line 202

def exec_attach
  cmd = []
  cmd.concat exec_launchers if docker_runtime?
  cmd.concat ['/bin/bash']
  cmd
end

#exec_launchersObject



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

def exec_launchers
  [
    File.join(ctr_scripts_dir, 'inituidgid.sh'),
    File.join(ctr_scripts_dir, 'setuser.rb'),
    ctr_user
  ]
end

#exec_rakeObject



192
193
194
195
196
197
198
199
200
# File 'lib/ood_packaging/package.rb', line 192

def exec_rake
  cmd = []
  cmd.concat exec_launchers if docker_runtime?
  cmd.concat ['scl', 'enable', scl_ruby, '--'] if podman_runtime? && build_box.scl?
  cmd.concat [File.join(ctr_scripts_dir, 'rake')]
  cmd.concat ['-q'] unless debug?
  cmd.concat ['-f', File.join(ctr_scripts_dir, 'Rakefile'), 'ood_packaging:package:build']
  cmd
end

#gpg_filesObject



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/ood_packaging/package.rb', line 107

def gpg_files
  [
    OpenStruct.new(private_key: @config[:gpg_private_key], passphrase: @config[:gpg_passphrase]),
    OpenStruct.new(private_key: ENV['OOD_PACKAGING_GPG_PRIVATE_KEY'],
                   passphrase:  ENV['OOD_PACKAGING_GPG_PASSPHRASE'])
  ].each do |gpg|
    next if gpg.private_key.nil? || gpg.passphrase.nil?
    return gpg if File.exist?(gpg.private_key) && File.exist?(gpg.passphrase)
  end
  nil
end

#gpg_nameObject



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

def gpg_name
  @config[:gpg_name].nil? ? default_gpg_name : @config[:gpg_name]
end

#gpg_signObject



119
120
121
122
123
124
# File 'lib/ood_packaging/package.rb', line 119

def gpg_sign
  return false if ENV['OOD_PACKAGING_GPG_SIGN'].to_s == 'false'
  return false if @config[:gpg_sign] == false

  !gpg_files.nil?
end

#output_dirObject



62
63
64
# File 'lib/ood_packaging/package.rb', line 62

def output_dir
  @output_dir ||= File.expand_path(@config[:output_dir])
end

#packageObject



72
73
74
# File 'lib/ood_packaging/package.rb', line 72

def package
  @config[:package]
end

#package_nameObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/ood_packaging/package.rb', line 92

def package_name
  name = File.basename(package)
  case name
  when /deb|rpm/
    name = if File.basename(File.dirname(package)) == 'packages'
             File.basename(File.dirname(File.dirname(package)))
           else
             File.basename(File.dirname(package))
           end
  when 'packaging'
    name = File.basename(File.dirname(package))
  end
  name
end

#rpm_tar_dest_dirObject



158
159
160
161
162
163
164
165
166
167
# File 'lib/ood_packaging/package.rb', line 158

def rpm_tar_dest_dir
  [
    File.join(package, 'rpm'),
    File.join(package, 'packaging/rpm'),
    File.join(package, 'packaging')
  ].each do |dir|
    return dir if Dir.exist?(dir)
  end
  File.join(package, 'packaging/rpm')
end

#rpm_versionObject



169
170
171
# File 'lib/ood_packaging/package.rb', line 169

def rpm_version
  tar_version
end

#run!Object

rubocop:disable Metrics/AbcSize



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/ood_packaging/package.rb', line 263

def run!
  # handle_signals
  if tar_only?
    tar!
    return
  end
  clean!
  bootstrap!
  tar! if tar?
  container_start!
  container_exec!(exec_rake)
  puts "Build SUCCESS: package=#{package} dist=#{build_box.dist}".green
rescue SignalException => e
  puts "Caught signal #{e}, proceeding to kill container".red
  container_kill!
  exit 1
rescue RuntimeError
  puts "Build FAILED package=#{package} dist=#{build_box.dist}".red
  raise
ensure
  container_attach! if attach?
  container_kill! if container_running? && !attach?
end

#tar!Object



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/ood_packaging/package.rb', line 232

def tar!
  cmd = ['git', 'ls-files', '.', '|', tar, '-c']
  if build_box.rpm?
    dir = rpm_tar_dest_dir
  else
    dir = deb_tar_dest_dir.tap { |p| sh "mkdir -p #{p}" }
    cmd.concat ["--transform 'flags=r;s,packaging/deb,debian,'"]
  end
  tar_file = "#{dir}/#{tar_name}.tar.gz"
  cmd.concat ["--transform 's,^,#{tar_name}/,'"]
  cmd.concat ['-T', '-', '|', "gzip > #{tar_file}"]

  sh "rm #{tar_file}" if File.exist?(tar_file)
  puts "Create tar archive #{tar_file}".blue
  Dir.chdir(tar_path) do
    sh cmd.join(' '), verbose: debug?
  end
end

#tar?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/ood_packaging/package.rb', line 84

def tar?
  @config[:tar].nil? ? build_box.deb? : @config[:tar]
end

#tar_nameObject



149
150
151
152
153
154
155
156
# File 'lib/ood_packaging/package.rb', line 149

def tar_name
  version = if build_box.rpm?
              rpm_version
            else
              deb_version
            end
  "#{package_name}-#{version}"
end

#tar_only?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/ood_packaging/package.rb', line 88

def tar_only?
  @config[:tar_only].nil? ? false : @config[:tar_only]
end

#tar_pathObject



140
141
142
143
144
145
146
147
# File 'lib/ood_packaging/package.rb', line 140

def tar_path
  if build_box.deb?
    path = File.join(package, 'deb')
    return path if Dir.exist?(path)
  end

  package
end

#tar_versionObject



80
81
82
# File 'lib/ood_packaging/package.rb', line 80

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

#versionObject



76
77
78
# File 'lib/ood_packaging/package.rb', line 76

def version
  @config[:version]
end

#work_dirObject



52
53
54
# File 'lib/ood_packaging/package.rb', line 52

def work_dir
  @work_dir ||= File.expand_path(@config[:work_dir])
end