Class: Omnibus::Metadata
- Inherits:
-
Object
- Object
- Omnibus::Metadata
- Extended by:
- Sugarable
- Includes:
- Sugarable
- Defined in:
- lib/omnibus/metadata.rb
Class Method Summary collapse
-
.arch ⇒ String
The architecture for this machine, as reported from Ohai.
-
.for_package(package) ⇒ Metadata
Load the metadata from disk.
-
.generate(path, project) ⇒ String
Render the metadata for the package at the given path, generated by the given project.
-
.path_for(package) ⇒ String
The metadata path that corresponds to the package.
-
.platform_shortname ⇒ String
Platform name to be used when creating metadata for the artifact.
-
.platform_version ⇒ String
Platform version to be used in package metadata.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Helper for accessing the information inside the metadata hash.
-
#initialize(package, data = {}) ⇒ Metadata
constructor
Create a new metadata object for the given package and hash data.
-
#name ⇒ String
The name of this metadata file.
- #path ⇒ Object
-
#save ⇒ true
Save the file to disk.
-
#to_hash ⇒ Hash
Hash representation of this metadata.
-
#to_json ⇒ String
The JSON representation of this metadata.
Methods included from Sugarable
Constructor Details
#initialize(package, data = {}) ⇒ Metadata
Create a new metadata object for the given package and hash data.
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
.arch ⇒ String
The architecture for this machine, as reported from Ohai.
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.
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.
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.
107 108 109 |
# File 'lib/omnibus/metadata.rb', line 107 def path_for(package) "#{package.path}.metadata.json" end |
.platform_shortname ⇒ String
Platform name to be used when creating metadata for the artifact.
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_version ⇒ String
Platform version to be used in package metadata.
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.
253 254 255 |
# File 'lib/omnibus/metadata.rb', line 253 def [](key) @data[key] end |
#name ⇒ String
The name of this metadata file.
262 263 264 |
# File 'lib/omnibus/metadata.rb', line 262 def name @name ||= File.basename(path) end |
#path ⇒ Object
269 270 271 |
# File 'lib/omnibus/metadata.rb', line 269 def path @path ||= self.class.path_for(@package) end |
#save ⇒ true
Save the file to disk.
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_hash ⇒ Hash
Hash representation of this metadata.
291 292 293 |
# File 'lib/omnibus/metadata.rb', line 291 def to_hash @data.dup end |
#to_json ⇒ String
The JSON representation of this metadata.
300 301 302 |
# File 'lib/omnibus/metadata.rb', line 300 def to_json FFI_Yajl::Encoder.encode(@data, pretty: true) end |