Class: Installation::InstallationInfo

Inherits:
Object
  • Object
show all
Includes:
Singleton, Yast::Logger
Defined in:
library/general/src/lib/installation/installation_info.rb

Overview

Class which collects all installation/update information in order to write it into the /var/log/YaST2/installation_info directory when the process has been finished correctly or the process has crashed.

The implementation uses a callback mechanism to allow easily extending the logged data, it avoids circular dependencies between packages and easily handles optional YaST modules (e.g. the registration module is not present in the openSUSE Leap installer).

The callbacks also ensure that we really log the current values at the time of writing the dump file.

::Installation::InstallationInfo.instance.add_callback("my_module") do { "foo" => foo.value, "bar" => bar.value } end

if failed ::Installation::InstallationInfo.instance.write( "Setting foo option failed", additional_info: "File foo does not exist" ) end

Examples:

Registering a custom callback

Dumping the data when an error occurs

Constant Summary collapse

LOGDIR =
"/var/log/YaST2/installation_info/".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInstallationInfo

Returns a new instance of InstallationInfo.



63
64
65
66
67
68
69
70
71
72
# File 'library/general/src/lib/installation/installation_info.rb', line 63

def initialize
  # index of the saved file to have unique file names
  self.index = 0

  # Function calls which has been set by other modules,
  # these functions will be called while generating the output.
  # The return value (usually a Hash) of each call will be logged into the output file.
  # Uses "id" => block mapping
  @callbacks = {}
end

Instance Attribute Details

#callbacksObject (readonly, protected)

Returns the value of attribute callbacks.



126
127
128
# File 'library/general/src/lib/installation/installation_info.rb', line 126

def callbacks
  @callbacks
end

#indexObject (protected)

Returns the value of attribute index.



127
128
129
# File 'library/general/src/lib/installation/installation_info.rb', line 127

def index
  @index
end

Instance Method Details

#add_callback(name, &block) ⇒ Object

Register a block which will be called while generating the data file.

Parameters:

  • name (String)

    id of the function call, using the same id will overwrite the previous setting, use the module/package name to avoid conflicts



79
80
81
82
83
84
# File 'library/general/src/lib/installation/installation_info.rb', line 79

def add_callback(name, &block)
  return unless block_given?

  log.info("Adding callback #{name.inspect}")
  callbacks[name] = block
end

#callback?(name) ⇒ Boolean

is the callback already registered?

Parameters:

  • name (String)

    name of the callback

Returns:

  • (Boolean)

    true if registered, false otherwise



89
90
91
# File 'library/general/src/lib/installation/installation_info.rb', line 89

def callback?(name)
  callbacks.key?(name)
end

#write(description, additional_info: nil, path: nil) ⇒ String

Collects the data and writes the dump into an YAML file.

Parameters:

  • description (String)

    description of data, e.g. what happened

  • additional_info (Object) (defaults to: nil)

    optional additional information

  • path (String, nil) (defaults to: nil)

    path to the saved dump file, uses the default path if nil

Returns:

  • (String)

    path to the written file



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'library/general/src/lib/installation/installation_info.rb', line 100

def write(description, additional_info: nil, path: nil)
  file = path || File.join(LOGDIR, "dump_#{Process.pid}_#{format("%03d", index)}.yml")
  log.info("Writing installation information to #{file}")

  # the collected data
  data = {
    "description" => description
  }

  data["additional_info"] = additional_info if additional_info

  @callbacks.each do |name, callback|
    data[name] = callback.call
  end

  ::FileUtils.mkdir_p(File.dirname(file))
  File.write(file, data.to_yaml)

  # increase the file counter for the next file
  self.index += 1

  file
end