-
Notifications
You must be signed in to change notification settings - Fork 19
/
scc_auto.rb
324 lines (261 loc) · 9.06 KB
/
scc_auto.rb
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# encoding: utf-8
# ***************************************************************************
#
# Copyright (c) 2014 SUSE LLC
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, contact Novell, Inc.
#
# To contact Novell about this file by physical or electronic mail,
# you may find current contact information at www.novell.com
#
# ***************************************************************************
#
# Summary: Configure Product Registration for Autoinstallation
#
#
require "yast/suse_connect"
require "erb"
require "registration/storage"
require "registration/sw_mgmt"
require "registration/registration"
require "registration/registration_ui"
require "registration/helpers"
require "registration/connect_helpers"
require "registration/ssl_certificate"
require "registration/url_helpers"
require "registration/ui/autoyast_config_workflow"
module Yast
class SccAutoClient < Client
include Yast::Logger
include ERB::Util
extend Yast::I18n
# popup message
CONTACTING_MESSAGE = N_("Contacting the Registration Server")
def main
Yast.import "UI"
Yast.import "Pkg"
textdomain "registration"
Yast.import "Wizard"
Yast.import "Label"
Yast.import "Report"
Yast.import "Popup"
Yast.import "Installation"
log.info "scc_auto started"
@config = ::Registration::Storage::Config.instance
func = WFM.Args[0]
param = WFM.Args[1] || {}
log.info "func: #{func}, param: #{::Registration::Helpers.hide_reg_codes(param)}"
case func
when "Summary"
# Create a summary
ret = summary
when "Reset"
# Reset configuration
@config.reset
ret = {}
when "Change"
# Change configuration
ret = start_workflow
when "Import"
# import configuration
ret = import(param)
when "Export"
# Return the current config
ret = export
when "Packages"
# Return needed packages
ret = auto_packages
when "Write"
# Write given settings
ret = write
when "GetModified"
ret = @config.modified
when "SetModified"
@config.modified = true
ret = true
else
log.error "Unknown function: #{func}"
raise "Unknown function parameter: #{func}"
end
log.info "ret: #{::Registration::Helpers.hide_reg_codes(ret)}"
log.info "scc_auto finished"
ret
end
private
# Get all settings from the first parameter
# (For use by autoinstallation.)
# param [Hash] settings The structure to be imported.
def import(settings)
log.debug "Importing config: #{settings}"
@config.import(settings)
end
# Export the settings to a single Hash
# (For use by autoinstallation.)
# @return [Hash] AutoYast configuration
def export
ret = @config.export
log.debug "Exported config: #{ret}"
ret
end
# Create a textual summary
# @return [String] summary of the current configuration
def summary
::Registration::Helpers.render_erb_template("autoyast_summary.erb", binding)
end
# register the system, base product and optional addons
# return true on success
def write
# try updating the registratin in AutoUpgrade mode
if Mode.update
updated = update_registration
log.info "Registration updated: #{updated}"
# if update failed continue with the normal registration
return true if updated
end
# registration disabled, nothing to do
return true unless @config.do_registration
# initialize libzypp if applying settings in installed system or
# in AutoYast configuration mode ("Apply to System")
::Registration::SwMgmt.init if Mode.normal || Mode.config
# set the registration URL
url = @config.reg_server if @config.reg_server && !@config.reg_server.empty?
# use SLP discovery
if !url && @config.slp_discovery
url = find_slp_server
return false unless url
end
# nil = use the default URL
switch_registration(url)
ret = ::Registration::ConnectHelpers.catch_registration_errors do
if @config.reg_server_cert && !@config.reg_server_cert.empty?
import_certificate(@config.reg_server_cert)
end
register_base_product && register_addons
end
return false unless ret
# save the registered repositories
Pkg.SourceSaveAll
if Mode.normal || Mode.config
# popup message: registration finished properly
Popup.Message(_("Registration was successfull."))
elsif Stage.initial
# copy the SSL certificate to the target system
::Registration::Helpers.copy_certificate_to_target
end
return true
end
def auto_packages
ret = { "install" => [], "remove" => [] }
log.info "Registration needs these packages: #{ret}"
ret
end
# find registration server via SLP
# @retun [String,nil] URL of the server, nil on error
def find_slp_server
# do SLP query
slp_services = ::Registration::UrlHelpers.slp_discovery_feedback
slp_urls = slp_services.map(&:slp_url)
# remove possible duplicates
slp_urls.uniq!
log.info "Found #{slp_urls.size} SLP servers"
case slp_urls.size
when 0
Report.Error(_("SLP discovery failed, no server found"))
return nil
when 1
return slp_urls.first
else
# more than one server found: let the user select, we cannot automatically
# decide which one to use, asking user in AutoYast mode is not nice
# but better than aborting the installation...
return ::Registration::UrlHelpers.slp_service_url
end
end
def import_certificate(url)
log.info "Importing certificate from #{url}..."
cert = Popup.Feedback(_("Downloading SSL Certificate"), url) do
::Registration::SslCertificate.download(url)
end
Popup.Feedback(_("Importing SSL Certificate"), cert.subject_name) do
cert.import_to_system
end
end
# UI workflow definition
def start_workflow
::Registration::UI::AutoyastConfigWorkflow.run(@config)
end
def switch_registration(url = nil)
@registration = ::Registration::Registration.new(url)
#reset registration ui as it depends on registration
@registration_ui = nil
@registration
end
def registration
if !@registration
url = ::Registration::UrlHelpers.registration_url
log.info "Updating registration using URL: #{url}"
@registration = switch_registration(url)
end
@registration
end
def registration_ui
@registration_ui ||= ::Registration::RegistrationUI.new(registration)
end
def update_registration
# the old system was not registered
return false unless prepare_update
return false unless update_base_product
return false unless update_addons
# register additional addons (e.g. originally not present in SLE11)
register_addons
end
# TODO FIXME: share these methods with inst_scc.rb
def register_base_product
registration_ui.register_system_and_base_product(@config.email, @config.reg_code,
disable_updates: !@config.install_updates)
end
def register_addons
# register addons
@config.addons.each do |addon|
product_service = Popup.Feedback(
_(CONTACTING_MESSAGE),
# %s is name of given product
_("Registering %s ...") % addon["name"]) do
registration.register_product(addon)
end
::Registration::Storage::Cache.instance.addon_services << product_service
registration_ui.disable_update_repos(product_service) if !@config.install_updates
end
# install the new products
::Registration::SwMgmt.select_addon_products
end
def prepare_update
::Registration::SwMgmt.copy_old_credentials(Installation.destdir)
# update the registration using the old credentials
File.exists?(::Registration::Registration::SCC_CREDENTIALS)
end
# @return [Boolean] true on success
def update_base_product
registration_ui.update_base_product(enable_updates: @config.install_updates)
end
# @return [Boolean] true on success
# TODO FIXME share with inst_scc.rb
def update_addons
addons = registration_ui.get_available_addons
failed_addons = registration_ui.update_addons(addons, enable_updates: @config.install_updates)
failed_addons.empty?
end
end unless defined?(SccAutoClient)
end
Yast::SccAutoClient.new.main