forked from chef/omnibus
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmetadata.rb
More file actions
298 lines (273 loc) · 8.68 KB
/
metadata.rb
File metadata and controls
298 lines (273 loc) · 8.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#
# Copyright 2014 Chef Software, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require "ffi_yajl"
module Omnibus
class Metadata
extend Sugarable
include Sugarable
class << self
#
# 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
#
def generate(path, project)
unless File.exist?(path)
raise NoPackageFile.new(path)
end
package = Package.new(path)
data = {
# Package
basename: package.name,
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
#
# Load the metadata from disk.
#
# @param [Package] package
# the package for this metadata
#
# @return [Metadata]
#
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
#
# The metadata path that corresponds to the package.
#
# @param [Package] package
# the package for this metadata
#
# @return [String]
#
def path_for(package)
"#{package.path}.metadata.json"
end
#
# The architecture for this machine, as reported from Ohai.
#
# @return [String]
#
def arch
if windows? && windows_arch_i386?
"i386"
elsif solaris?
if intel?
"i386"
elsif sparc?
"sparc"
end
else
Ohai["kernel"]["machine"]
end
end
#
# Platform version to be used in package metadata.
#
# @return [String]
# the platform version
#
def platform_version
truncate_platform_version(Ohai["platform_version"], platform_shortname)
end
#
# Platform name to be used when creating metadata for the artifact.
#
# @return [String]
# the platform family short name
#
def platform_shortname
if rhel?
"el"
elsif suse?
"sles"
else
Ohai["platform"]
end
end
private
#
# 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`
#
def truncate_platform_version(platform_version, platform)
case platform
when "centos", "debian", "el", "fedora", "freebsd", "omnios", "pidora", "raspbian", "rhel", "sles", "suse", "smartos", "nexus", "ios_xr"
# Only want MAJOR (e.g. Debian 7, OmniOS r151006, SmartOS 20120809T221258Z)
platform_version.split(".").first
when "aix", "gentoo", "openbsd", "slackware", "solaris2", "opensuse", "ubuntu"
# 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"
# Arch Linux does not have a platform_version ohai attribute, it is 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/opscode/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.0.6002", '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\.0/ then "10"
else
raise UnknownPlatformVersion.new(platform, platform_version)
end
else
raise UnknownPlatform.new(platform)
end
end
end
#
# 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
#
def initialize(package, data = {})
@package = package
@data = data.dup.freeze
end
#
# Helper for accessing the information inside the metadata hash.
#
# @return [Object]
#
def [](key)
@data[key]
end
#
# The name of this metadata file.
#
# @return [String]
#
def name
@name ||= File.basename(path)
end
#
# @see (Metadata.path_for)
#
def path
@path ||= self.class.path_for(@package)
end
#
# Save the file to disk.
#
# @return [true]
#
def save
File.open(path, "w+") do |f|
f.write(FFI_Yajl::Encoder.encode(to_hash, pretty: true))
end
true
end
#
# Hash representation of this metadata.
#
# @return [Hash]
#
def to_hash
@data.dup
end
#
# The JSON representation of this metadata.
#
# @return [String]
#
def to_json
FFI_Yajl::Encoder.encode(@data, pretty: true)
end
end
end