Module: Pkg::Paths

Includes:
Platforms
Defined in:
lib/packaging/paths.rb

Overview

Utilities surrounding the appropriate paths associated with a platform This includes both reporting the correct path and divining the platform tag associated with a variety of paths

rubocop:disable Metrics/ModuleLength

Constant Summary

Constants included from Platforms

Pkg::Platforms::DEBIAN_SOURCE_FORMATS, Pkg::Platforms::PLATFORM_INFO

Class Method Summary collapse

Methods included from Platforms

all_supported_package_formats, arches_for_codename, arches_for_platform_version, by_deb, by_package_format, by_rpm, codename_for_platform_version, codename_to_platform_version, codename_to_tags, codenames, formats, generic_platform_tag, get_attribute, get_attribute_for_platform_version, package_format_for_tag, parse_platform_tag, platform_lookup, platform_tags, platform_tags_for_package_format, signature_format_for_platform_version, signature_format_for_tag, source_architecture_for_platform_tag, source_package_formats_for_platform_tag, supported_platforms, versions_for_platform

Class Method Details

.apt_package_base_path(platform_tag, repo_name, project, nonfinal = false) ⇒ Object

This is where deb packages end up after freight repo updates



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/packaging/paths.rb', line 304

def apt_package_base_path(platform_tag, repo_name, project, nonfinal = false)
  unless Pkg::Platforms.package_format_for_tag(platform_tag) == 'deb'
    fail "Can't determine path for non-debian platform #{platform_tag}."
  end

  platform, version, _ = Pkg::Platforms.parse_platform_tag(platform_tag)
  code_name = Pkg::Platforms.codename_for_platform_version(platform, version)
  remote_repo_path = remote_repo_base(platform_tag, nonfinal: nonfinal)

  # In puppet7 and beyond, we moved the puppet major version to near the top to allow each
  # puppet major release to have its own apt repo, for example:
  # /opt/repository/apt/puppet7/pool/bionic/p/puppet-agent
  if %w(FUTURE-puppet7 FUTURE-puppet7-nightly).include? repo_name
    return File.join(remote_repo_path, repo_name, 'pool', code_name, project[0], project)
  end

  # For repos prior to puppet7, the puppet version was part of the repository
  # For example: /opt/repository/apt/pool/bionic/puppet6/p/puppet-agent
  if %w(puppet7 puppet7-nightly
        puppet6 puppet6-nightly
        puppet5 puppet5-nightly
        puppet  puppet-nightly
        puppet-tools).include? repo_name
    return File.join(remote_repo_path, 'pool', code_name, repo_name, project[0], project)
  end

  raise "Error: Cannot determine apt_package_base_path for repo: \"#{repo_name}\"."
end

.apt_repo_name(nonfinal = false) ⇒ Object

Method to determine the apt repo name. Maintains compatibility with legacy projects, where ‘Pkg::Config.apt_repo_name` is set instead of `Pkg::Config.repo_name`. Defaults to ’main’ if nothing is set.



89
90
91
92
93
94
95
96
97
# File 'lib/packaging/paths.rb', line 89

def apt_repo_name(nonfinal = false)
  if nonfinal && Pkg::Config.nonfinal_repo_name
    return Pkg::Config.nonfinal_repo_name
  elsif nonfinal
    fail "Nonfinal is set to true but Pkg::Config.nonfinal_repo_name is unset!"
  end

  return Pkg::Config.repo_name || Pkg::Config.apt_repo_name || 'main'
end

.arch_from_artifact_path(platform, version, path) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/packaging/paths.rb', line 11

def arch_from_artifact_path(platform, version, path)
  arches = Pkg::Platforms.arches_for_platform_version(platform, version)

  # First check if it's a source package
  source_formats = Pkg::Platforms.get_attribute_for_platform_version(platform, version, :source_package_formats)
  if source_formats.find { |fmt| path =~ /#{fmt}$/ }
    return Pkg::Platforms.get_attribute_for_platform_version(platform, version, :source_architecture)
  end
  arches.find { |a| path.include?(package_arch(platform, a)) } || arches[0]
rescue
  arches.find { |a| path.include?(package_arch(platform, a)) } || arches[0]
end

Given platform information, create symlink target (base_path) and link path in the form of a 2-element array



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/packaging/paths.rb', line 172

def artifacts_base_path_and_link_path(platform_tag, prefix = 'artifacts', is_nonfinal = false)
  platform_name, _ = Pkg::Platforms.parse_platform_tag(platform_tag)
  package_format = Pkg::Platforms.package_format_for_tag(platform_tag)

  path_data = {
    is_nonfinal: is_nonfinal,
    link: link_name(is_nonfinal),
    package_format: package_format,
    platform_name: platform_name,
    platform_tag: platform_tag,
    prefix: prefix
  }

  return [
    construct_base_path(path_data),
    construct_link_path(path_data)
  ]
end

.artifacts_path(platform_tag, path_prefix = 'artifacts', nonfinal = false) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/packaging/paths.rb', line 191

def artifacts_path(platform_tag, path_prefix = 'artifacts', nonfinal = false)
  base_path, _ = artifacts_base_path_and_link_path(platform_tag, path_prefix, nonfinal)
  platform, version, architecture = Pkg::Platforms.parse_platform_tag(platform_tag)
  package_format = Pkg::Platforms.package_format_for_tag(platform_tag)

  case package_format
  when 'rpm'
    File.join(base_path, platform, version, architecture)
  when 'swix'
    File.join(base_path, version, architecture)
  when 'deb'
    base_path
  when 'svr4', 'ips'
    File.join(base_path, version)
  when 'dmg'
    File.join(base_path, version, architecture)
  when 'msi'
    base_path
  else
    raise "Not sure where to find packages with a package format of '#{package_format}'"
  end
end

.construct_base_path(path_data) ⇒ Object

Construct a platform-dependent symlink target path.



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/packaging/paths.rb', line 105

def construct_base_path(path_data)
  package_format = path_data[:package_format]
  prefix = path_data[:prefix]
  is_nonfinal = path_data[:is_nonfinal]
  platform_name = path_data[:platform_name]
  platform_tag = path_data[:platform_tag]

  repo_name = Pkg::Config.repo_name

  case package_format
  when 'deb'
    debian_code_name = Pkg::Platforms.get_attribute(platform_tag, :codename)

    # In puppet7 and beyond, we moved the repo_name to the top to allow each
    # puppet major release to have its own apt repo.
    if %w(FUTURE-puppet7 FUTURE-puppet7-nightly).include? repo_name
      return File.join(prefix, apt_repo_name(is_nonfinal), debian_code_name)
    end

    # For puppet6 and prior
    return File.join(prefix, debian_code_name, apt_repo_name(is_nonfinal))
  when 'dmg'
    return File.join(prefix, 'mac', repo_name(is_nonfinal))
  when 'msi'
    return File.join(prefix, platform_name, repo_name(is_nonfinal))
  when 'rpm'
    return File.join(prefix, yum_repo_name(is_nonfinal))
  when 'swix'
    return File.join(prefix, platform_name, repo_name(is_nonfinal))
  when 'svr4', 'ips'
    return File.join(prefix, 'solaris', repo_name(is_nonfinal))
  else
    raise "Error: Unknown package format '#{package_format}'"
  end
end

Construct a platform-dependent link path



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/packaging/paths.rb', line 142

def construct_link_path(path_data)
  package_format = path_data[:package_format]
  prefix = path_data[:prefix]
  platform_name = path_data[:platform_name]
  platform_tag = path_data[:platform_tag]
  link = path_data[:link]

  return nil if link.nil?

  case package_format
  when 'rpm'
    return File.join(prefix, link)
  when 'swix'
    return File.join(prefix, platform_name, link)
  when 'deb'
    debian_code_name = Pkg::Platforms.get_attribute(platform_tag, :codename)
    return File.join(prefix, debian_code_name, link)
  when 'svr4', 'ips'
    return File.join(prefix, 'solaris', link)
  when 'dmg'
    return File.join(prefix, 'mac', link)
  when 'msi'
    return File.join(prefix, platform_name, link)
  else
    raise "Error: Unknown package format '#{package_format}'"
  end
end

.debian_component_from_path(path) ⇒ Object



350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/packaging/paths.rb', line 350

def debian_component_from_path(path)
  # substitute '.' and '/' since those aren't valid characters for debian components
  matches = path.match(/(\d+\.\d+|master|main)\/(\w+)/)
  regex_for_substitution = /[\.\/]/
  fail "Error: Could not determine Debian Component from path #{path}" if matches.nil?
  base_component = matches[1]
  component_qualifier = matches[2]
  full_component = "#{base_component}/#{component_qualifier}"
  unless regex_for_substitution.nil?
    base_component.gsub!(regex_for_substitution, '_')
    full_component.gsub!(regex_for_substitution, '_')
  end
  return base_component if component_qualifier == 'repos'
  return full_component
end


99
100
101
102
# File 'lib/packaging/paths.rb', line 99

def link_name(nonfinal = false)
  return Pkg::Config.nonfinal_repo_link_target if nonfinal
  return Pkg::Config.repo_link_target
end

.package_arch(platform, arch) ⇒ Object

for ubuntu-20.04-aarch64, debian package architecture is arm64



367
368
369
370
371
372
# File 'lib/packaging/paths.rb', line 367

def package_arch(platform, arch)
  if platform == 'ubuntu' && arch == 'aarch64'
    return 'arm64'
  end
  arch
end


333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/packaging/paths.rb', line 333

def release_package_link_path(platform_tag, nonfinal = false)
  platform, version, _ = Pkg::Platforms.parse_platform_tag(platform_tag)
  package_format = Pkg::Platforms.package_format_for_tag(platform_tag)
  case package_format
  when 'rpm'
    return File.join(remote_repo_base(platform_tag, nonfinal: nonfinal),
                     "#{repo_name(nonfinal)}-release-#{platform}-#{version}.noarch.rpm")
  when 'deb'
    codename = Pkg::Platforms.codename_for_platform_version(platform, version)
    return File.join(remote_repo_base(platform_tag, nonfinal: nonfinal),
                     "#{repo_name(nonfinal)}-release-#{codename}.deb")
  else
    warn "No release packages for package format '#{package_format}', skipping."
    return nil
  end
end

.remote_repo_base(platform_tag = nil, nonfinal: false, package_format: nil) ⇒ Object



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/packaging/paths.rb', line 274

def remote_repo_base(platform_tag = nil, nonfinal: false, package_format: nil)
  if platform_tag.nil? && package_format.nil?
    raise "Pkg::Paths.remote_repo_base must have `platform_tag` or `package_format` specified."
  end

  package_format ||= Pkg::Platforms.package_format_for_tag(platform_tag)

  repo_base = case package_format
  when 'rpm'
    nonfinal ? Pkg::Config.nonfinal_yum_repo_path : Pkg::Config.yum_repo_path
  when 'deb'
    nonfinal ? Pkg::Config.nonfinal_apt_repo_path : Pkg::Config.apt_repo_path
  when 'dmg'
    nonfinal ? Pkg::Config.nonfinal_dmg_path : Pkg::Config.dmg_path
  when 'swix'
    nonfinal ? Pkg::Config.nonfinal_swix_path : Pkg::Config.swix_path
  when 'msi'
    nonfinal ? Pkg::Config.nonfinal_msi_path : Pkg::Config.msi_path
  else
    raise "Can't determine remote repo base path for package format '#{package_format}'."
  end

  # normalize the path for things shipping to the downloads server
  if repo_base.match(/^\/opt\/downloads\/\w+$/)
    repo_base = '/opt/downloads'
  end
  repo_base
end

.repo_config_path(platform_tag) ⇒ Object



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/packaging/paths.rb', line 257

def repo_config_path(platform_tag)
  package_format = Pkg::Platforms.package_format_for_tag(platform_tag)

  case package_format
  when 'rpm'
    # rpm/pl-puppet-agent-1.2.5-el-5-i386.repo for example
    File.join('repo_configs', 'rpm', "*#{platform_tag}*.repo")
  when 'deb'
    # deb/pl-puppet-agent-1.2.5-jessie.list
    File.join('repo_configs', 'deb', "*#{Pkg::Platforms.get_attribute(platform_tag, :codename)}*.list")
  when 'msi', 'swix', 'dmg', 'svr4', 'ips'
    nil
  else
    raise "Not sure what to do with a package format of '#{package_format}'"
  end
end

.repo_name(nonfinal = false) ⇒ Object

Assign repo name If we are shipping development/beta/non-final packages, they should be shipped to the development/beta/non-final repo, if there is one defined. Otherwise, default to the final repo name. We use this for more than just shipping to the final repos, so we need this to not fail.



63
64
65
66
67
68
69
70
71
# File 'lib/packaging/paths.rb', line 63

def repo_name(nonfinal = false)
  if nonfinal && Pkg::Config.nonfinal_repo_name
    Pkg::Config.nonfinal_repo_name
  elsif nonfinal
    fail "Nonfinal is set to true but Pkg::Config.nonfinal_repo_name is unset!"
  else
    Pkg::Config.repo_name || ""
  end
end

.repo_path(platform_tag, options = { :legacy => false, :nonfinal => false }) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/packaging/paths.rb', line 214

def repo_path(platform_tag, options = { :legacy => false, :nonfinal => false })
  repo_target = repo_name(options[:nonfinal])
  # in legacy packaging methods, there was no consistent way to determine the
  # repo name. There were separate variables for apt_repo_name and
  # yum_repo_name. At times, either or both of these were unset, and they had
  # different defaults. So, for legacy automation we need to just use a splat
  # and globbing to find our packages.
  repo_target = '**' if options[:legacy]
  platform, version, arch = Pkg::Platforms.parse_platform_tag(platform_tag)
  package_format = Pkg::Platforms.package_format_for_tag(platform_tag)

  case package_format
  when 'rpm', 'swix'
    if options[:legacy]
      File.join('repos', platform, version, repo_target, arch)
    else
      File.join('repos', repo_target, platform, version, arch)
    end
  when 'deb'
    File.join('repos', 'apt', Pkg::Platforms.get_attribute(platform_tag, :codename), 'pool', repo_target)
  when 'svr4', 'ips'
    if options[:legacy]
      File.join('repos', 'solaris', version, repo_target)
    else
      File.join('repos', 'solaris', repo_target, version)
    end
  when 'dmg'
    if options[:legacy]
      File.join('repos', 'apple', version, repo_target, arch)
    else
      File.join('repos', 'mac', repo_target, version, arch)
    end
  when 'msi'
    if options[:legacy]
      File.join('repos', 'windows')
    else
      File.join('repos', platform, repo_target)
    end
  else
    raise "Not sure what to do with a package format of '#{package_format}'"
  end
end

.tag_from_artifact_path(path) ⇒ Object

Given a path to an artifact, divine the appropriate platform tag associated with the artifact and path



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/packaging/paths.rb', line 26

def tag_from_artifact_path(path)
  platform = Pkg::Platforms.supported_platforms.find { |p| path =~ /(\/|\.)#{p}[^\.]/ }
  platform = 'windowsfips' if path =~ /windowsfips/

  codename = Pkg::Platforms.codenames.find { |c| path =~ /\/#{c}/ }

  if codename
    platform, version = Pkg::Platforms.codename_to_platform_version(codename)
  end

  version = '2012' if platform =~ /^windows.*$/

  version ||= Pkg::Platforms.versions_for_platform(platform).find { |v| path =~ /#{platform}(\/|-)?#{v}/ }

  arch = arch_from_artifact_path(platform, version, path)

  return "#{platform}-#{version}-#{arch}"
rescue
  fmt = Pkg::Platforms.all_supported_package_formats.find { |ext| path =~ /#{ext}$/ }

  # We need to make sure this is actually a file, and not simply a path
  file_ext = File.extname(path)

  # Fail if we do not have a file extension or if that file extension is one
  # that is platform specific
  raise "Cannot determine tag from #{path}" if fmt || file_ext.empty?

  # Return nil otherwise, assuming that is a file type that is not tied to a
  # specific platform
  return nil
end

.yum_repo_name(nonfinal = false) ⇒ Object

Method to determine the yum repo name. Maintains compatibility with legacy projects, where ‘Pkg::Config.yum_repo_name` is set instead of `Pkg::Config.repo_name`. Defaults to ’products’ if nothing is set.



76
77
78
79
80
81
82
83
84
# File 'lib/packaging/paths.rb', line 76

def yum_repo_name(nonfinal = false)
  if nonfinal && Pkg::Config.nonfinal_repo_name
    return Pkg::Config.nonfinal_repo_name
  elsif nonfinal
    fail "Nonfinal is set to true but Pkg::Config.nonfinal_repo_name is unset!"
  end

  return Pkg::Config.repo_name || Pkg::Config.yum_repo_name || 'products'
end