Class: Omnibus::Metadata

Inherits:
Object
  • Object
show all
Defined in:
lib/omnibus/metadata.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

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


213
214
215
216
# File 'lib/omnibus/metadata.rb', line 213

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

Class Method Details

.archString

The architecture for this machine, as reported from Ohai.


110
111
112
# File 'lib/omnibus/metadata.rb', line 110

def arch
  Ohai['kernel']['machine']
end

.for_package(package) ⇒ Metadata

Load the metadata from disk.


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/omnibus/metadata.rb', line 76

def for_package(package)
  data = File.read(path_for(package))
  hash = JSON.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.

Raises:

  • (NoPackageFile)

    if the given path does not contain a package


37
38
39
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
# File 'lib/omnibus/metadata.rb', line 37

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,
  }

  instance = new(package, data)
  instance.save
  instance.path
end

.path_for(package) ⇒ String

The metadata path that corresponds to the package.


101
102
103
# File 'lib/omnibus/metadata.rb', line 101

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

.platform_shortnameString

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


130
131
132
133
134
135
136
137
138
# File 'lib/omnibus/metadata.rb', line 130

def platform_shortname
  if Ohai['platform_family'] == 'rhel'
    'el'
  elsif Ohai['platform'] == 'suse'
    'sles'
  else
    Ohai['platform']
  end
end

.platform_versionString

Platform version to be used in package metadata.


120
121
122
# File 'lib/omnibus/metadata.rb', line 120

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.


223
224
225
# File 'lib/omnibus/metadata.rb', line 223

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

#nameString

The name of this metadata file.


232
233
234
# File 'lib/omnibus/metadata.rb', line 232

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

#pathObject

See Also:

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

239
240
241
# File 'lib/omnibus/metadata.rb', line 239

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

#savetrue

Save the file to disk.


248
249
250
251
252
253
254
# File 'lib/omnibus/metadata.rb', line 248

def save
  File.open(path, 'w+')  do |f|
    f.write(to_json)
  end

  true
end

#to_jsonString

The JSON representation of this metadata.


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

def to_json
  JSON.pretty_generate(@data)
end