Class: SystemDescription
Overview
The responsibility of the SystemDescription class is to represent a system description. This is our main data model.
The content of the system description is stored in a directory, which contains a manifest and sub directories for individual scopes. SystemDescription handles all the data which is in the top level of the system description directory.
The sub directories storing the data for specific scopes are handled by the ScopeFileStore class.
Constant Summary
collapse
- CURRENT_FORMAT_VERSION =
10
[
"changed_managed_files",
"changed_config_files",
"unmanaged_files"
]
Instance Attribute Summary collapse
#attributes, #scope
Class Method Summary
collapse
Instance Method Summary
collapse
#==, #[], #[]=, #as_json, #compare_with, convert_element, convert_raw_hash, #empty?, from_json, has_property, #hash, #initialize_copy, #method_missing, #respond_to?, #set_attributes
Constructor Details
#initialize(name, store, hash = {}) ⇒ SystemDescription
Returns a new instance of SystemDescription.
113
114
115
116
117
118
119
120
|
# File 'lib/system_description.rb', line 113
def initialize(name, store, hash = {})
@name = name
@store = store
@format_version = CURRENT_FORMAT_VERSION
@filter_definitions = {}
super(create_scopes(hash))
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
in the class Machinery::Object
Instance Attribute Details
#filter_definitions(command) ⇒ Object
Returns the value of attribute filter_definitions.
39
40
41
|
# File 'lib/system_description.rb', line 39
def filter_definitions
@filter_definitions
end
|
Returns the value of attribute format_version.
38
39
40
|
# File 'lib/system_description.rb', line 38
def format_version
@format_version
end
|
Returns the value of attribute name.
36
37
38
|
# File 'lib/system_description.rb', line 36
def name
@name
end
|
Returns the value of attribute store.
37
38
39
|
# File 'lib/system_description.rb', line 37
def store
@store
end
|
Class Method Details
.from_hash(name, store, hash) ⇒ Object
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/system_description.rb', line 89
def from_hash(name, store, hash)
begin
json_format_version = hash["meta"]["format_version"] if hash["meta"]
description = SystemDescription.new(name, store, hash)
rescue NameError, TypeError, RuntimeError
if json_format_version && json_format_version != SystemDescription::CURRENT_FORMAT_VERSION
raise Machinery::Errors::SystemDescriptionIncompatible.new(name, json_format_version)
else
raise Machinery::Errors::SystemDescriptionError.new(
"#{name}: This description is broken."
)
end
end
description.format_version = json_format_version
if hash["meta"] && hash["meta"]["filters"]
description.filter_definitions = hash["meta"]["filters"]
end
description
end
|
.load(name, store, options = {}) ⇒ Object
Load the system description with the given name
If there are file validation errors these are put out as warnings but the loading of the system description succeeds.
63
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/system_description.rb', line 63
def load(name, store, options = {})
manifest = Manifest.load(name, store.manifest_path(name))
manifest.validate unless options[:skip_validation]
description = from_hash(name, store, manifest.to_hash)
description.validate_file_data unless options[:skip_validation]
description.validate_format_compatibility unless options[:skip_format_compatibility]
description
end
|
.load!(name, store, options = {}) ⇒ Object
Load the system description with the given name
If there are file validation errors the call fails with an exception
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/system_description.rb', line 45
def load!(name, store, options = {})
manifest = Manifest.load(name, store.manifest_path(name))
manifest.validate!
description = from_hash(name, store, manifest.to_hash)
description.validate_file_data!
unless options[:skip_format_compatibility]
description.validate_format_compatibility
end
description
end
|
.valid_name?(name) ⇒ Boolean
75
76
77
|
# File 'lib/system_description.rb', line 75
def valid_name?(name)
!!/^[\w:-][\w\.:-]*$/.match(name)
end
|
.validate_name(name) ⇒ Object
79
80
81
82
83
84
85
86
87
|
# File 'lib/system_description.rb', line 79
def validate_name(name)
unless valid_name?(name)
raise Machinery::Errors::SystemDescriptionError.new(
"System description name '#{name}' is invalid. " \
"Only 'a-zA-Z0-9_:.-' are valid characters and a dot " \
"is not allowed at the begginning."
)
end
end
|
Instance Method Details
#assert_scopes(*scopes) ⇒ Object
#create_scopes(hash) ⇒ Object
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
# File 'lib/system_description.rb', line 122
def create_scopes(hash)
scopes = hash.map do |scope_name, json|
next if scope_name == "meta"
if store.persistent?
scope_file_store = scope_file_store(scope_name)
end
if json.is_a?(Hash) || json.is_a?(Array)
scope_object = Machinery::Scope.for(scope_name, json, scope_file_store)
if hash["meta"] && hash["meta"][scope_name]
scope_object.meta = Machinery::Object.from_json(hash["meta"][scope_name])
end
else
scope_object = json
end
[scope_name, scope_object]
end.compact
Hash[scopes]
end
|
#description_path ⇒ Object
287
288
289
|
# File 'lib/system_description.rb', line 287
def description_path
@store.description_path(name)
end
|
#has_file?(name) ⇒ Boolean
295
296
297
298
299
300
301
302
|
# File 'lib/system_description.rb', line 295
def has_file?(name)
EXTRACTABLE_SCOPES.each do |scope|
if (scope)
return true if self[scope] && self[scope].has_file?(name)
end
end
false
end
|
282
283
284
285
|
# File 'lib/system_description.rb', line 282
def host
all_hosts = attributes.keys.map { |scope| self[scope].meta.try(:[], "hostname") }
all_hosts.uniq.compact
end
|
#latest_update ⇒ Object
277
278
279
280
|
# File 'lib/system_description.rb', line 277
def latest_update
attributes.keys.map { |scope| self[scope].meta.try(:[], "modified") }
.compact.map { |t| Time.parse(t) }.sort.last
end
|
#read_config(path, key) ⇒ Object
304
305
306
307
308
309
310
311
|
# File 'lib/system_description.rb', line 304
def read_config(path, key)
EXTRACTABLE_SCOPES.each do |scope|
if (scope)
file = self[scope].find { |f| f.name == path }
return parse_variable_assignment(file.content, key) if file
end
end
end
|
#runs_service?(name) ⇒ Boolean
291
292
293
|
# File 'lib/system_description.rb', line 291
def runs_service?(name)
self["services"].any? { |service| service.name == "#{name}.service" }
end
|
198
199
200
201
202
203
204
205
|
# File 'lib/system_description.rb', line 198
def save
SystemDescription.validate_name(name)
@store.directory_for(name)
path = @store.manifest_path(name)
created = !File.exist?(path)
File.write(path, to_json)
File.chmod(0600, path) if created
end
|
251
252
253
|
# File 'lib/system_description.rb', line 251
def (scope)
self[scope] && self[scope]. && self[scope].
end
|
#scope_file_store(store_name) ⇒ Object
255
256
257
|
# File 'lib/system_description.rb', line 255
def scope_file_store(store_name)
ScopeFileStore.new(description_path, store_name)
end
|
221
222
223
|
# File 'lib/system_description.rb', line 221
def scopes
Inspector.sort_scopes(attributes.keys.map(&:to_s).sort)
end
|
#set_filter_definitions(command, filter) ⇒ Object
211
212
213
214
215
216
217
218
219
|
# File 'lib/system_description.rb', line 211
def set_filter_definitions(command, filter)
unless ["inspect"].include?(command)
raise Machinery::Errors::MachineryError.new(
"Storing the filter for command '#{command}' is not supported."
)
end
@filter_definitions[command] = filter
end
|
#short_os_version ⇒ Object
238
239
240
241
242
243
244
245
246
247
248
249
|
# File 'lib/system_description.rb', line 238
def short_os_version
assert_scopes("os")
case self.os.name
when /^SUSE Linux Enterprise Server/
"sles" + self.os.version[/\d+( SP\d+)*/].gsub(" ", "").downcase
when /^openSUSE/
self.os.version[/^\d+.\d+/]
else
"unknown"
end
end
|
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
# File 'lib/system_description.rb', line 177
def to_hash
meta = {}
meta["format_version"] = self.format_version if self.format_version
attributes.keys.each do |key|
meta[key] = self[key].meta.as_json if self[key].meta
end
@filter_definitions.each do |command, filter|
meta["filters"] ||= {}
meta["filters"][command] = filter
end
hash = as_json
hash["meta"] = meta unless meta.empty?
hash
end
|
194
195
196
|
# File 'lib/system_description.rb', line 194
def to_json
JSON.pretty_generate(to_hash)
end
|
#validate_analysis_compatibility ⇒ Object
158
159
160
161
162
163
164
165
166
|
# File 'lib/system_description.rb', line 158
def validate_analysis_compatibility
Zypper.isolated(arch: os.architecture) do |zypper|
major, minor, patch = zypper.version
if major <= 1 && minor <= 11 && patch < 4
raise Machinery::Errors::AnalysisFailed.new("Analyzing command requires zypper 1.11.4 " \
"or grater to be installed.")
end
end
end
|
#validate_build_compatibility ⇒ Object
168
169
170
171
172
173
174
175
|
# File 'lib/system_description.rb', line 168
def validate_build_compatibility
kiwi_template_path = "/usr/share/kiwi/image/#{os.kiwi_boot}"
unless Dir.exist?(kiwi_template_path)
raise Machinery::Errors::BuildFailed.new("The execution of the build script failed. " \
"Building of operating system '#{os.display_name}' can't be accomplished because the " \
"kiwi template file in `#{kiwi_template_path}` does not exist.")
end
end
|
#validate_file_data ⇒ Object
259
260
261
262
263
264
265
266
|
# File 'lib/system_description.rb', line 259
def validate_file_data
errors = FileValidator.new(to_hash, description_path).validate
unless errors.empty?
Machinery::Ui.warn("Warning: File validation errors:")
Machinery::Ui.warn("Error validating description '#{@name}'\n\n")
Machinery::Ui.warn(errors.join("\n"))
end
end
|