Class: Registration::Helpers

Inherits:
Object
  • Object
show all
Extended by:
Yast::I18n
Includes:
Yast::Logger
Defined in:
src/lib/registration/helpers.rb

Constant Summary collapse

FILTERED =

reg. code replacement

"[FILTERED]".freeze

Class Method Summary collapse

Class Method Details

.base_version(version) ⇒ Object

return base version "12-1.47" => "12" "12-1" => "12" "12.1-1.47" => "12.1" "12.1-1" => "12.1"


108
109
110
# File 'src/lib/registration/helpers.rb', line 108

def self.base_version(version)
  version.sub(/-.*\z/, "")
end

.collect_autoyast_config(known_reg_codes) ⇒ Object


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
# File 'src/lib/registration/helpers.rb', line 181

def self.collect_autoyast_config(known_reg_codes)
  options = Storage::InstallationOptions.instance
  configuration = {
    "do_registration" => true,
    "email"           => options.email,
    "reg_code"        => options.reg_code,
    "install_updates" => options.install_updates
  }

  reg_server = UrlHelpers.registration_url
  configuration["reg_server"] = reg_server if reg_server

  if options.imported_cert_sha256_fingerprint
    configuration["reg_server_cert_fingerprint_type"] = "SHA256"
    configuration["reg_server_cert_fingerprint"] = options.imported_cert_sha256_fingerprint
  end

  configuration["addons"] = Addon.registered.map do |addon|
    addon_hash = addon.to_h(release_type_string: true)
    addon_hash["reg_code"] = known_reg_codes[addon.identifier] || ""
    addon_hash
  end

  configuration
end

.copy_certificate_to_targetObject

copy the imported SSL certificate to the target system (if exists)


128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'src/lib/registration/helpers.rb', line 128

def self.copy_certificate_to_target
  cert_file = SslCertificate.default_certificate_path
  return unless File.exist?(cert_file) # no certificate imported?
  # copy the imported certificate
  log.info "Copying SSL certificate (#{cert_file}) to the target system..."
  cert_target_file = File.join(Yast::Installation.destdir,
    SUSE::Connect::YaST::SERVER_CERT_FILE)
  ::FileUtils.mkdir_p(File.dirname(cert_target_file))
  ::FileUtils.cp(cert_file, cert_target_file)

  # update the certificate links
  cmd = SUSE::Connect::YaST::UPDATE_CERTIFICATES
  log.info "Updating certificate links (#{cmd})..."
  Yast::SCR.Execute(Yast::Path.new(".target.bash"), cmd)
end

.current_languageString?

Get the current language (without encoding suffix)


53
54
55
56
57
58
59
60
61
62
63
64
# File 'src/lib/registration/helpers.rb', line 53

def self.current_language
  lang = Yast::WFM.GetLanguage
  log.info "Current language: #{lang}"

  if lang == "POSIX" || lang == "C"
    log.info "Ignoring #{lang.inspect} language"
    return nil
  end

  # remove the encoding (e.g. ".UTF-8")
  lang.sub(/\..*$/, "")
end

.hide_reg_codes(settings) ⇒ Object

Hide registration codes in Autoyast data The result should be just logged, the result might not be a full duplicate of the input is all cases and therefore should NOT be modified. TODO: move AutoYast settings to a separate class and override #to_s


213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'src/lib/registration/helpers.rb', line 213

def self.hide_reg_codes(settings)
  return settings unless settings.is_a?(Hash)

  # create a duplicate
  filtered = settings.dup
  filtered["reg_code"] &&= FILTERED

  return filtered unless filtered["addons"]

  # duplicate the nested values
  filtered["addons"] = filtered["addons"].map(&:dup)

  filtered["addons"].each do |addon|
    addon["reg_code"] &&= FILTERED
  end

  filtered
end

.http_languageString?

Get the language for using in HTTP requests (in "Accept-Language" header)


68
69
70
71
72
73
74
75
76
77
78
# File 'src/lib/registration/helpers.rb', line 68

def self.http_language
  lang = current_language
  return nil unless lang

  # replace lang/country separator "_" -> "-"
  # see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4
  lang.tr!("_", "-")

  log.info "Language for HTTP requests set to #{lang.inspect}"
  lang
end

.insecure_registrationObject

check if insecure registration is requested (the "reg_ssl_verify=0" boot commandline option is used)


114
115
116
117
118
119
120
121
122
123
124
125
# File 'src/lib/registration/helpers.rb', line 114

def self.insecure_registration
  # check the boot parameter only at installation/update
  if Yast::Mode.installation || Yast::Mode.update
    reg_ssl_verify = Yast::Linuxrc.InstallInf("reg_ssl_verify")
    log.info "Boot reg_ssl_verify option: #{reg_ssl_verify.inspect}"

    return reg_ssl_verify == "0"
  else
    config = SUSE::Connect::Config.new
    return config.insecure
  end
end

.network_configurableBoolean

check if the network configuration module is present


99
100
101
# File 'src/lib/registration/helpers.rb', line 99

def self.network_configurable
  Yast::WFM.ClientExists("inst_lan")
end

.render_erb_template(file, binding) ⇒ Object

TODO: move it to yast2 to share it


165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'src/lib/registration/helpers.rb', line 165

def self.render_erb_template(file, binding)
  # use erb template for rendering the richtext summary

  erb_file = if Pathname.new(file).absolute?
    file
  else
    File.expand_path(File.join("../../../data/registration", file), __FILE__)
  end

  log.info "Loading ERB template #{erb_file}"
  erb = ERB.new(File.read(erb_file))

  # render the ERB template in the context of the requested object
  erb.result(binding)
end

.report_no_base_productObject


232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'src/lib/registration/helpers.rb', line 232

def self.report_no_base_product
  # error message
  msg = _("The base product was not found,\ncheck your system.") + "\n\n"

  if Yast::Stage.initial
    # TRANSLATORS: %s = bugzilla URL
    msg += _("The installation medium or the installer itself is seriously broken.\n" \
        "Report a bug at %s.") % "https://bugzilla.suse.com"
  else
    msg += _("Make sure a product is installed and /etc/products.d/baseproduct\n" \
        "is a symlink pointing to the base product .prod file.")
  end

  Yast::Report.Error(msg)
end

.reset_registration_statusObject


156
157
158
159
160
161
162
# File 'src/lib/registration/helpers.rb', line 156

def self.reset_registration_status
  file = SUSE::Connect::YaST::GLOBAL_CREDENTIALS_FILE
  return unless File.exist?(file)

  log.info "Resetting registration status, removing #{file}"
  File.unlink(file)
end

.run_network_configurationObject

run the network configuration module


92
93
94
95
# File 'src/lib/registration/helpers.rb', line 92

def self.run_network_configuration
  log.info "Running network configuration..."
  Yast::WFM.call("inst_lan", [{ "skip_detection" => true }])
end

.service_description(service) ⇒ String

Create radio button label for a SLP service


83
84
85
86
87
88
89
# File 'src/lib/registration/helpers.rb', line 83

def self.service_description(service)
  url = UrlHelpers.service_url(service.slp_url)
  descr = service.attributes.to_h[:description]

  # display URL and the description if it is present
  (descr && !descr.empty?) ? "#{descr} (#{url})" : url
end

.write_configObject

write the current configuration to the configuration file


145
146
147
148
149
150
151
152
153
154
# File 'src/lib/registration/helpers.rb', line 145

def self.write_config
  config_params = {
    url:      UrlHelpers.registration_url,
    insecure: insecure_registration
  }

  log.info "writing registration config: #{config_params}"

  SUSE::Connect::YaST.write_config(config_params)
end