Class: SystemDescription

Inherits:
Machinery::Object show all
Defined in:
lib/system_description.rb

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 =
3
EXTRACTABLE_SCOPES =
[
  "changed_managed_files",
  "config_files",
  "unmanaged_files"
]

Instance Attribute Summary collapse

Attributes inherited from Machinery::Object

#attributes

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Machinery::Object

#==, #[], #[]=, #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.



133
134
135
136
137
138
139
140
# File 'lib/system_description.rb', line 133

def initialize(name, store, hash = {})
  @name = name
  @store = store
  @format_version = CURRENT_FORMAT_VERSION
  @filter_definitions = {}

  super(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

#format_versionObject

Returns the value of attribute format_version.



38
39
40
# File 'lib/system_description.rb', line 38

def format_version
  @format_version
end

#nameObject

Returns the value of attribute name.



36
37
38
# File 'lib/system_description.rb', line 36

def name
  @name
end

#storeObject

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



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 91

def from_hash(name, store, hash)
  begin
    json_format_version = hash["meta"]["format_version"] if hash["meta"]
    description = SystemDescription.new(name, store, create_scopes(hash))
  rescue NameError, TypeError
    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
    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 if !options[:skip_validation]

  description = from_hash(name, store, manifest.to_hash)
  description.validate_file_data if !options[:skip_validation]

  description.validate_format_compatibility if !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!

  if !options[:skip_format_compatibility]
    description.validate_format_compatibility
  end

  description
end

.validate_name(name) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/system_description.rb', line 75

def validate_name(name)
  if ! /^[\w\.:-]*$/.match(name)
    raise Machinery::Errors::SystemDescriptionError.new(
      "System description name \"#{name}\" is invalid. " +
      "Only \"a-zA-Z0-9_:.-\" are valid characters."
    )
  end

  if name.start_with?(".")
    raise Machinery::Errors::SystemDescriptionError.new(
      "System description name \"#{name}\" is invalid. " +
      "A dot is not allowed as first character."
    )
  end
end

Instance Method Details

#assert_scopes(*scopes) ⇒ Object



215
216
217
218
219
220
221
222
223
# File 'lib/system_description.rb', line 215

def assert_scopes(*scopes)
  missing = scopes.select { |scope| !self[scope] || self[scope].empty? }

  unless missing.empty?
    raise Machinery::Errors::SystemDescriptionError.new(
      "The system description misses the following section(s): #{missing.join(", ")}."
    )
  end
end

#compatible?Boolean

Returns:

  • (Boolean)


142
143
144
145
# File 'lib/system_description.rb', line 142

def compatible?
  !format_version.nil? &&
    format_version == SystemDescription::CURRENT_FORMAT_VERSION
end

#description_pathObject



264
265
266
# File 'lib/system_description.rb', line 264

def description_path
  @store.description_path(name)
end

#saveObject



188
189
190
191
192
193
194
195
# File 'lib/system_description.rb', line 188

def save
  SystemDescription.validate_name(name)
  @store.directory_for(name)
  path = @store.manifest_path(name)
  created = !File.exists?(path)
  File.write(path, to_json)
  File.chmod(0600, path) if created
end

#scope_extracted?(scope) ⇒ Boolean

Returns:

  • (Boolean)


238
239
240
# File 'lib/system_description.rb', line 238

def scope_extracted?(scope)
  self[scope] && self[scope].is_extractable? && self[scope].extracted
end

#scope_file_store(store_name) ⇒ Object



242
243
244
# File 'lib/system_description.rb', line 242

def scope_file_store(store_name)
  ScopeFileStore.new(description_path, store_name)
end

#scopesObject



211
212
213
# File 'lib/system_description.rb', line 211

def scopes
  attributes.keys.map(&:to_s).sort
end

#set_filter_definitions(command, filter) ⇒ Object



201
202
203
204
205
206
207
208
209
# File 'lib/system_description.rb', line 201

def set_filter_definitions(command, filter)
  if !["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_versionObject



225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/system_description.rb', line 225

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

#to_hashObject



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/system_description.rb', line 167

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

#to_jsonObject



184
185
186
# File 'lib/system_description.rb', line 184

def to_json
  JSON.pretty_generate(to_hash)
end

#validate_analysis_compatibilityObject



153
154
155
156
157
158
# File 'lib/system_description.rb', line 153

def validate_analysis_compatibility
  if !os.can_be_analyzed?
    raise Machinery::Errors::AnalysisFailed.new("Analysis of operating " +
      "system '#{os.display_name}' is not supported.")
  end
end

#validate_export_compatibilityObject



160
161
162
163
164
165
# File 'lib/system_description.rb', line 160

def validate_export_compatibility
  if !os.can_be_exported?
    raise Machinery::Errors::ExportFailed.new("Export of operating " +
      "system '#{os.display_name}' is not supported.")
  end
end

#validate_file_dataObject



246
247
248
249
250
251
252
253
# File 'lib/system_description.rb', line 246

def validate_file_data
  errors = FileValidator.new(to_hash, description_path).validate
  if !errors.empty?
    Machinery::Ui.warn("Warning: File validation errors:")
    Machinery::Ui.warn("Error validating description '#{@name}'\n\n")
    Machinery::Ui.warn(errors)
  end
end

#validate_file_data!Object



255
256
257
258
259
260
261
262
# File 'lib/system_description.rb', line 255

def validate_file_data!
  errors = FileValidator.new(to_hash, description_path).validate
  if !errors.empty?
    e = Machinery::Errors::SystemDescriptionValidationFailed.new(errors)
    e.header = "Error validating description '#{@name}'"
    raise e
  end
end

#validate_format_compatibilityObject



147
148
149
150
151
# File 'lib/system_description.rb', line 147

def validate_format_compatibility
  if !compatible?
    raise Machinery::Errors::SystemDescriptionIncompatible.new(name, format_version)
  end
end