class Omnibus::Metadata

Public Class Methods

arch() click to toggle source

The architecture for this machine, as reported from Ohai.

@return [String]

# File lib/omnibus/metadata.rb, line 116
def arch
  if windows? && windows_arch_i386?
    "i386"
  elsif solaris?
    if intel?
      "i386"
    elsif sparc?
      "sparc"
    end
  else
    Ohai["kernel"]["machine"]
  end
end
for_package(package) click to toggle source

Load the metadata from disk.

@param [Package] package

the package for this metadata

@return [Metadata]

# File lib/omnibus/metadata.rb, line 82
def for_package(package)
  data = File.read(path_for(package))
  hash = FFI_Yajl::Parser.parse(data, symbolize_names: true)

  # Ensure Platform version has been truncated
  if hash[:platform_version] && hash[:platform]
    hash[:platform_version] = truncate_platform_version(hash[:platform_version], hash[:platform])
  end

  # Ensure an interation exists
  hash[:iteration] ||= 1

  new(package, hash)
rescue Errno::ENOENT
  raise NoPackageMetadataFile.new(package.path)
end
generate(path, project) click to toggle source

Render the metadata for the package at the given path, generated by the given project.

@raise [NoPackageFile]

if the given +path+ does not contain a package

@param [String] path

the path to the package (or compressed object) on disk

@param [Project] project

the project which generated the given package or compressed object

@return [String]

the path to the metadata on disk
# File lib/omnibus/metadata.rb, line 40
def generate(path, project)
  unless File.exist?(path)
    raise NoPackageFile.new(path)
  end

  package = Package.new(path)

  data = {
    # Package
    basename: package.name,
    md5: package.md5,
    sha1: package.sha1,
    sha256: package.sha256,
    sha512: package.sha512,
    platform: platform_shortname,
    platform_version: platform_version,
    arch: arch,

    # Project
    name: project.name,
    friendly_name: project.friendly_name,
    homepage: project.homepage,
    version: project.build_version,
    iteration: project.build_iteration,
    license: project.license,
    version_manifest: project.built_manifest.to_hash,
    license_content: File.exist?(project.license_file_path) ? File.read(project.license_file_path) : "",
  }

  instance = new(package, data)
  instance.save
  instance.path
end
new(package, data = {}) click to toggle source

Create a new metadata object for the given package and hash data.

@param [Package] package

the package for this metadata

@param [Hash] data

the hash of attributes to set in the metadata
# File lib/omnibus/metadata.rb, line 243
def initialize(package, data = {})
  @package = package
  @data    = data.dup.freeze
end
path_for(package) click to toggle source

The metadata path that corresponds to the package.

@param [Package] package

the package for this metadata

@return [String]

# File lib/omnibus/metadata.rb, line 107
def path_for(package)
  "#{package.path}.metadata.json"
end
platform_shortname() click to toggle source

Platform name to be used when creating metadata for the artifact.

@return [String]

the platform family short name
# File lib/omnibus/metadata.rb, line 146
def platform_shortname
  if rhel?
    if "rocky" == Ohai["platform"]
      "rocky"
    else
      "el"
    end
  elsif suse?
    "sles"
  else
    Ohai["platform"]
  end
end
platform_version() click to toggle source

Platform version to be used in package metadata.

@return [String]

the platform version
# File lib/omnibus/metadata.rb, line 136
def platform_version
  truncate_platform_version(Ohai["platform_version"], platform_shortname)
end

Private Class Methods

truncate_platform_version(platform_version, platform) click to toggle source

On certain platforms we don’t care about the full MAJOR.MINOR.PATCH platform version. This method will properly truncate the version down to a more human friendly version. This version can also be thought of as a ‘marketing’ version.

@param [String] platform_version

the platform version to truncate

@param [String] platform

the platform shortname. this might be an Ohai-returned platform or
platform family but it also might be a shortname like `el`

rubocop:disable Lint/DuplicateCaseCondition

# File lib/omnibus/metadata.rb, line 175
def truncate_platform_version(platform_version, platform)
  case platform
  when "centos", "cumulus", "debian", "el", "fedora", "freebsd", "omnios", "pidora", "raspbian", "rhel", "rocky", "sles", "suse", "smartos"
    # Only want MAJOR (e.g. Debian 7, OmniOS r151006, SmartOS 20120809T221258Z)
    platform_version.split(".").first
  when "aix", "alpine", "openbsd", "slackware", "solaris2", "opensuse", "opensuseleap", "ubuntu", "amazon"
    # Only want MAJOR.MINOR (e.g. Ubuntu 12.04)
    platform_version.split(".")[0..1].join(".")
  when "mac_os_x", "darwin", "macos"
    # If running macOS >= 11, use only MAJOR version. Otherwise, use MAJOR.MINOR
    pv_bits = platform_version.split(".")
    pv_bits[0].to_i >= 11 ? pv_bits[0] : pv_bits[0..1].join(".")
  when "arch", "gentoo", "kali"
    # Arch Linux / Gentoo do not have a platform_version ohai attribute, they are rolling release (lsb_release -r)
    "rolling"
  when "windows"
    # Windows has this really awesome "feature", where their version numbers
    # internally do not match the "marketing" name.
    #
    # Definitively computing the Windows marketing name actually takes more
    # than the platform version. Take a look at the following file for the
    # details:
    #
    #   https://github.com/chef/chef/blob/master/lib/chef/win32/version.rb
    #
    # As we don't need to be exact here the simple mapping below is based on:
    #
    #  http://www.jrsoftware.org/ishelp/index.php?topic=winvernotes
    #
    # Microsoft's version listing (more general than the above) is here:
    #
    # https://msdn.microsoft.com/en-us/library/windows/desktop/ms724832(v=vs.85).aspx
    #
    case platform_version
    when "5.0.2195", "2000"   then "2000"
    when "5.1.2600", "xp"     then "xp"
    when "5.2.3790", "2003r2" then "2003r2"
    when "6.0.6001", "2008"   then "2008"
    when "6.1.7600", "7"      then "7"
    when "6.1.7601", "2008r2" then "2008r2"
    when "6.2.9200", "2012"   then "2012"
    # The following `when` will never match since Windows 8's platform
    # version is the same as Windows 2012. It's only here for completeness and
    # documentation.
    when "6.2.9200", "8"      then "8"
    when /6\.3\.\d+/, "2012r2" then "2012r2"
    # The following `when` will never match since Windows 8.1's platform
    # version is the same as Windows 2012R2. It's only here for completeness
    # and documentation.
    when /6\.3\.\d+/, "8.1" then "8.1"
    when "10", /^10\.0/ then "10"
    else
      raise UnknownPlatformVersion.new(platform, platform_version)
    end
  else
    raise UnknownPlatform.new(platform)
  end
end

Public Instance Methods

[](key) click to toggle source

Helper for accessing the information inside the metadata hash.

@return [Object]

# File lib/omnibus/metadata.rb, line 253
def [](key)
  @data[key]
end
name() click to toggle source

The name of this metadata file.

@return [String]

# File lib/omnibus/metadata.rb, line 262
def name
  @name ||= File.basename(path)
end
path() click to toggle source

@see (Metadata.path_for)

# File lib/omnibus/metadata.rb, line 269
def path
  @path ||= self.class.path_for(@package)
end
save() click to toggle source

Save the file to disk.

@return [true]

# File lib/omnibus/metadata.rb, line 278
def save
  File.open(path, "w+") do |f|
    f.write(FFI_Yajl::Encoder.encode(to_hash, pretty: true))
  end

  true
end
to_hash() click to toggle source

Hash representation of this metadata.

@return [Hash]

# File lib/omnibus/metadata.rb, line 291
def to_hash
  @data.dup
end
to_json() click to toggle source

The JSON representation of this metadata.

@return [String]

# File lib/omnibus/metadata.rb, line 300
def to_json
  FFI_Yajl::Encoder.encode(@data, pretty: true)
end