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


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

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)

113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/omnibus/metadata.rb', line 113

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:


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

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.

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

  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)

104
105
106
# File 'lib/omnibus/metadata.rb', line 104

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


143
144
145
146
147
148
149
150
151
# File 'lib/omnibus/metadata.rb', line 143

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

.platform_versionString

Platform version to be used in package metadata.

Returns:

  • (String)

    the platform version


133
134
135
# File 'lib/omnibus/metadata.rb', line 133

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)

241
242
243
# File 'lib/omnibus/metadata.rb', line 241

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

#nameString

The name of this metadata file.

Returns:

  • (String)

250
251
252
# File 'lib/omnibus/metadata.rb', line 250

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

#pathObject

See Also:

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

257
258
259
# File 'lib/omnibus/metadata.rb', line 257

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

#savetrue

Save the file to disk.

Returns:

  • (true)

266
267
268
269
270
271
272
# File 'lib/omnibus/metadata.rb', line 266

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

  true
end

#to_hashHash

Hash representation of this metadata.

Returns:

  • (Hash)

279
280
281
# File 'lib/omnibus/metadata.rb', line 279

def to_hash
  @data.dup
end

#to_jsonString

The JSON representation of this metadata.

Returns:

  • (String)

288
289
290
# File 'lib/omnibus/metadata.rb', line 288

def to_json
  JSON.pretty_generate(@data)
end