Class: Omnibus::Metadata

Inherits:
Object
  • Object
show all
Extended by:
Sugarable
Includes:
Sugarable
Defined in:
lib/omnibus/metadata.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Sugarable

extended, included, node

Constructor Details

#initialize(package, data = {}) ⇒ Metadata

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

Parameters:

  • package (Package)

    the package for this metadata

  • data (Hash) (defaults to: {})

    the hash of attributes to set in the metadata



243
244
245
246
# File 'lib/omnibus/metadata.rb', line 243

def initialize(package, data = {})
  @package = package
  @data    = data.dup.freeze
end

Class Method Details

.archString

The architecture for this machine, as reported from Ohai.

Returns:

  • (String)


116
117
118
119
120
121
122
123
124
125
126
127
128
# 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) ⇒ Metadata

Load the metadata from disk.

Parameters:

  • package (Package)

    the package for this metadata

Returns:



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# 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) ⇒ String

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

Parameters:

  • path (String)

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

  • project (Project)

    the project which generated the given package or compressed object

Returns:

  • (String)

    the path to the metadata on disk

Raises:

  • (NoPackageFile)

    if the given path does not contain a package



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 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

.path_for(package) ⇒ String

The metadata path that corresponds to the package.

Parameters:

  • package (Package)

    the package for this metadata

Returns:

  • (String)


107
108
109
# File 'lib/omnibus/metadata.rb', line 107

def path_for(package)
  "#{package.path}.metadata.json"
end

.platform_shortnameString

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

Returns:

  • (String)

    the platform family short name



146
147
148
149
150
151
152
153
154
155
156
157
158
# 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_versionString

Platform version to be used in package metadata.

Returns:

  • (String)

    the platform version



136
137
138
# File 'lib/omnibus/metadata.rb', line 136

def platform_version
  truncate_platform_version(Ohai["platform_version"], platform_shortname)
end

Instance Method Details

#[](key) ⇒ Object

Helper for accessing the information inside the metadata hash.

Returns:

  • (Object)


253
254
255
# File 'lib/omnibus/metadata.rb', line 253

def [](key)
  @data[key]
end

#nameString

The name of this metadata file.

Returns:

  • (String)


262
263
264
# File 'lib/omnibus/metadata.rb', line 262

def name
  @name ||= File.basename(path)
end

#pathObject

See Also:

  • Omnibus::Metadata.(Metadata(Metadata.path_for)


269
270
271
# File 'lib/omnibus/metadata.rb', line 269

def path
  @path ||= self.class.path_for(@package)
end

#savetrue

Save the file to disk.

Returns:

  • (true)


278
279
280
281
282
283
284
# 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_hashHash

Hash representation of this metadata.

Returns:

  • (Hash)


291
292
293
# File 'lib/omnibus/metadata.rb', line 291

def to_hash
  @data.dup
end

#to_jsonString

The JSON representation of this metadata.

Returns:

  • (String)


300
301
302
# File 'lib/omnibus/metadata.rb', line 300

def to_json
  FFI_Yajl::Encoder.encode(@data, pretty: true)
end