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, #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)


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

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

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

#bootstrap!Object



217
218
219
220
# File 'lib/ood_packaging/package.rb', line 217

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

#clean!Object



212
213
214
215
# File 'lib/ood_packaging/package.rb', line 212

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



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

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



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

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

#container_envObject



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/ood_packaging/package.rb', line 298

def container_env
  env = {
    'DIST'          => build_box.dist,
    '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



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

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
end

#container_initObject



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

def container_init
  '/sbin/init'
end

#container_kill!Object



291
292
293
294
295
296
# File 'lib/ood_packaging/package.rb', line 291

def container_kill!
  puts "Killing container #{container_name}".blue
  cmd = [container_runtime, 'kill', container_name]
  cmd.concat ['1>/dev/null', '2>/dev/null'] unless debug
  sh cmd.join(' '), verbose: debug
end

#container_mountsObject



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

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

Returns:

  • (Boolean)


262
263
264
265
# File 'lib/ood_packaging/package.rb', line 262

def container_running?
  `#{container_runtime} inspect #{container_name} 2>/dev/null 1>/dev/null`
  $CHILD_STATUS.success?
end

#container_start!Object



267
268
269
270
271
272
273
274
275
276
277
# File 'lib/ood_packaging/package.rb', line 267

def container_start!
  cmd = [container_runtime, 'run', '--detach', '--rm']
  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



163
164
165
166
167
168
# File 'lib/ood_packaging/package.rb', line 163

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



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

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

#debugObject



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

#exec_attachObject



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

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

#exec_launchersObject



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

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

#exec_rakeObject



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

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



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/ood_packaging/package.rb', line 101

def gpg_files
  [
    OpenStruct.new(private_key: File.join(proj_root, 'ondemand.sec'), passphrase: File.join(proj_root, '.gpgpass')),
    OpenStruct.new(private_key: File.join(package, 'ondemand.sec'), passphrase: File.join(package, '.gpgpass')),
    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



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

def gpg_name
  @config[:gpg_name].nil? ? 'OnDemand Release Signing Key' : @config[:gpg_name]
end

#gpg_signObject



115
116
117
118
119
120
# File 'lib/ood_packaging/package.rb', line 115

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



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

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

#packageObject



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

def package
  @config[:package]
end

#package_nameObject



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

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



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

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



159
160
161
# File 'lib/ood_packaging/package.rb', line 159

def rpm_version
  tar_version
end

#run!Object



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/ood_packaging/package.rb', line 241

def run!
  if tar_only?
    tar!
    return
  end
  clean!
  bootstrap!
  tar! if tar?
  container_start!
  container_exec!(exec_rake)
rescue RuntimeError
  # ret = 1
  puts "Build FAILED package=#{package} dist=#{build_box.dist}".red
  raise
else
  puts "Build SUCCESS: package=#{package} dist=#{build_box.dist}".green
ensure
  container_exec!(exec_attach, ['-i', '-t']) if attach?
  container_kill! if container_running?
end

#tar!Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/ood_packaging/package.rb', line 222

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)


78
79
80
# File 'lib/ood_packaging/package.rb', line 78

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

#tar_nameObject



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

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

#tar_only?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/ood_packaging/package.rb', line 82

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

#tar_pathObject



130
131
132
133
134
135
136
137
# File 'lib/ood_packaging/package.rb', line 130

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

  package
end

#tar_versionObject



74
75
76
# File 'lib/ood_packaging/package.rb', line 74

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

#versionObject



70
71
72
# File 'lib/ood_packaging/package.rb', line 70

def version
  @config[:version]
end

#work_dirObject



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

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