-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathmanager.rb
More file actions
579 lines (492 loc) · 20.4 KB
/
manager.rb
File metadata and controls
579 lines (492 loc) · 20.4 KB
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
# frozen_string_literal: true
# Copyright (c) [2022-2026] 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 SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.
require "y2storage/storage_manager"
require "agama/dbus/base_object"
require "agama/dbus/with_issues"
require "agama/dbus/with_progress"
require "agama/storage/config_conversions"
require "agama/storage/encryption_settings"
require "agama/storage/volume_templates_builder"
require "agama/storage/devicegraph_conversions"
require "agama/storage/volume_conversions"
require "dbus"
require "json"
require "yast"
module Agama
module DBus
module Storage
# D-Bus object to manage storage installation
class Manager < BaseObject
extend Yast::I18n
include Yast::I18n
include WithIssues
include WithProgress
PATH = "/org/opensuse/Agama/Storage1"
private_constant :PATH
# @param manager [Agama::Storage::Manager]
# @param task_runner [Agama::TaskRunner]
# @param logger [Logger, nil]
def initialize(manager, task_runner, logger: nil)
textdomain "agama"
super(PATH, logger: logger)
@manager = manager
@task_runner = task_runner
@serialized_system = serialize_system
@serialized_config = serialize_config
@serialized_config_model = serialize_config_model
@serialized_proposal = serialize_proposal
@serialized_issues = serialize_issues
@serialized_bootloader_config = serialize_bootloader_config
@serialized_encryption_methods = serialize_encryption_methods
register_progress_callbacks
end
dbus_interface "org.opensuse.Agama.Storage1" do
dbus_reader_attr_accessor :serialized_system, "s", dbus_name: "System"
dbus_reader_attr_accessor :serialized_config, "s", dbus_name: "Config"
dbus_reader_attr_accessor :serialized_config_model, "s", dbus_name: "ConfigModel"
dbus_reader_attr_accessor :serialized_proposal, "s", dbus_name: "Proposal"
dbus_reader_attr_accessor :serialized_issues, "s", dbus_name: "Issues"
dbus_reader_attr_accessor :serialized_encryption_methods, "s",
dbus_name: "EncryptionMethods"
dbus_method(:Activate) { activate }
dbus_method(:Probe) { probe }
dbus_method(:Install) { install }
dbus_method(:Finish) { finish }
dbus_method(:Umount) { umount }
dbus_method(:SetLocale, "in locale:s") { |locale| manager.configure_locale(locale) }
dbus_method(
:SetConfig, "in serialized_product_config:s, in serialized_config:s"
) { |p, c| configure(p, c) }
dbus_method(
:GetConfigFromModel, "in serialized_model:s, out result:s"
) { |m| convert_config_model(m) }
dbus_method(
:SolveConfigModel, "in serialized_model:s, out result:s"
) { |m| solve_config_model(m) }
dbus_signal(:SystemChanged, "serialized_system:s")
dbus_signal(:ProposalChanged, "serialized_proposal:s")
dbus_signal(:ProgressChanged, "serialized_progress:s")
dbus_signal(:ProgressFinished)
end
# Implementation for the API method #Activate.
#
# @raise [Agama::TaskRunner::BusyError] If an async task is running, see {TaskRunner}.
def activate
task_runner.run("Activate storage") do
logger.info("Activating storage")
start_progress(3, ACTIVATING_STEP)
manager.reset_activation if manager.activated?
manager.activate
next_progress_step(PROBING_STEP)
perform_probe
next_progress_step(CONFIGURING_STEP)
configure_with_current
finish_progress
end
end
# Implementation for the API method #Probe.
#
# @raise [Agama::TaskRunner::BusyError] If an async task is running, see {TaskRunner}.
def probe
task_runner.run("Probe storage") do
logger.info("Probing storage")
start_progress(3, ACTIVATING_STEP)
manager.activate unless manager.activated?
next_progress_step(PROBING_STEP)
perform_probe
next_progress_step(CONFIGURING_STEP)
configure_with_current
finish_progress
end
end
# Configures storage.
#
# The JSON schema supports two different variants:
# { "storage": ... } or { "legacyAutoyastStorage": ... }.
#
# @raise If the config is not valid.
# @raise [Agama::TaskRunner::BusyError] If an async task is running and the system needs to
# be probed, see {TaskRunner}.
#
# @param serialized_product_config [String] Serialized product config.
# @param serialized_config [String] Serialized storage config.
def configure(serialized_product_config, serialized_config)
product_config_json = JSON.parse(serialized_product_config)
config_json = JSON.parse(serialized_config, symbolize_names: true)
# Do not configure if there is nothing to change.
return if manager.configured?(product_config_json, config_json)
# It is safe to run the task if the system was already probed.
return configure_task(product_config_json, config_json) if manager.probed?
# Prevent to probe the system if there is an async task running (e.g., formatting DASD).
task_runner.run("Configure storage") { configure_task(product_config_json, config_json) }
end
# Converts the given serialized config model to a config.
#
# @param serialized_model [String] Serialized config model.
# @return [String] Serialized config according to JSON schema.
def convert_config_model(serialized_model)
model_json = JSON.parse(serialized_model, symbolize_names: true)
config = Agama::Storage::ConfigConversions::FromModel.new(
model_json,
product_config: product_config,
storage_system: proposal.storage_system
).convert
config_json = { storage: Agama::Storage::ConfigConversions::ToJSON.new(config).convert }
JSON.pretty_generate(config_json)
end
# Solves the given serialized config model.
#
# @param serialized_model [String] Serialized storage config model.
# @return [String] Serialized solved model.
def solve_config_model(serialized_model)
model_json = JSON.parse(serialized_model, symbolize_names: true)
solved_model_json = proposal.solve_model(model_json)
JSON.pretty_generate(solved_model_json)
end
# Generates the serialized JSON of the available encryption methods.
#
# @return [String] Serialized list of encryption method IDs.
def serialize_encryption_methods
methods = Agama::Storage::EncryptionSettings
.available_methods
.map { |m| Agama::Storage::EncryptionSettings.method_id(m) }
JSON.pretty_generate(methods)
end
# Implementation for the API method #Install.
def install
start_progress(3, _("Preparing bootloader proposal"))
manager.bootloader.configure
next_progress_step(_("Preparing the storage devices"))
manager.install
next_progress_step(_("Writing bootloader sysconfig"))
manager.bootloader.install
finish_progress
end
# Implementation for the API method #Finish.
def finish
start_progress(1, _("Finishing installation"))
manager.finish
finish_progress
end
# Implementation for the API method #Umount.
def umount
start_progress(1, _("Unmounting devices"))
manager.umount
finish_progress
end
dbus_interface "org.opensuse.Agama.Storage1.Bootloader" do
dbus_reader_attr_accessor :serialized_bootloader_config, "s", dbus_name: "Config"
dbus_method(:SetConfig, "in serialized_config:s, out result:u") do |serialized_config|
configure_bootloader(serialized_config)
end
end
# Applies the given serialized config according to the JSON schema.
#
# @raise If the config is not valid.
#
# @param serialized_config [String] Serialized bootloader config.
# @return [Integer] 0 success; 1 error
def configure_bootloader(serialized_config)
logger.info("Setting bootloader config: #{serialized_config}")
manager.bootloader.config.load_json(serialized_config)
# after loading config try to apply it, so proper packages can be requested
# TODO: generate also new issue from configuration
calculate_bootloader
0
end
private
ACTIVATING_STEP = N_("Activating storage devices")
private_constant :ACTIVATING_STEP
PROBING_STEP = N_("Probing storage devices")
private_constant :PROBING_STEP
CONFIGURING_STEP = N_("Applying storage configuration")
private_constant :CONFIGURING_STEP
# @return [Agama::Storage::Manager]
attr_reader :manager
# @return [Agama::TaskRunner]
attr_reader :task_runner
def register_progress_callbacks
on_progress_change { self.ProgressChanged(serialize_progress) }
on_progress_finish { self.ProgressFinished }
end
# Performs the configuration task.
#
# @param product_config_json [Hash, nil]
# @param config_json [Hash, nil]
def configure_task(product_config_json, config_json)
logger.info("Configuring storage")
product_config = Agama::Config.new(product_config_json)
manager.update_product_config(product_config) if manager.product_config != product_config
start_progress(3, ACTIVATING_STEP)
manager.activate unless manager.activated?
next_progress_step(PROBING_STEP)
manager.probe unless manager.probed?
update_serialized_system
next_progress_step(CONFIGURING_STEP)
calculate_proposal(config_json)
finish_progress
end
# Probes storage and updates the associated info.
#
# @see #update_system_info
def perform_probe
manager.probe
update_serialized_system
end
# Configures storage using the current config.
#
# @note Skips if no proposal has been calculated yet.
def configure_with_current
return unless manager.product_config
calculate_proposal(manager.config_json)
# The storage proposal with the current settings is not explicitly requested. It is
# automatically calculated as side effect of calling to probe or activate. All the
# dependant steps has to be automatically done too, for example, reconfiguring bootloader.
calculate_bootloader
end
# Calculates a proposal with the given config and updates the associated info.
#
# @see #configure and #configure_with_current
#
# @param config_json [Hash, nil]
def calculate_proposal(config_json = nil)
manager.configure(config_json)
manager.add_packages if manager.proposal.success?
# The "return if unchanged" guard has been removed from the methods below to always
# emit the corresponding signal.
#
# Since signals do not carry payloads yet, the UI cannot update the query cache
# directly and must refetch after receiving the signal. Without emitting the signal,
# the related queries are never invalidated and never refetched, leaving the progress
# overlay blocked indefinitely.
#
# The overlay intentionally waits until fresh data arrives before unblocking, since
# data can take time to appear after progress completes. Dismissing it
# earlier would cause flickering and leave users able to interact with stale data.
#
# It can be reverted (and UI progress adapted accordingly) when signals carry payloads
# that allow the UI to update the cache directly, removing the need to wait for a
# refetch as part of progress completion.
update_serialized_config
update_serialized_config_model
update_serialized_proposal
update_serialized_issues
end
# Performs the bootloader configuration applying the current config.
def calculate_bootloader
logger.info("Configuring bootloader")
manager.bootloader.configure
update_serialized_bootloader_config
end
# Updates the system info if needed.
def update_serialized_system
serialized_system = serialize_system
# return if self.serialized_system == serialized_system
# This assignment emits a D-Bus PropertiesChanged.
self.serialized_system = serialized_system
self.SystemChanged(serialized_system)
end
# Updates the config info if needed.
def update_serialized_config
serialized_config = serialize_config
# return if self.serialized_config == serialized_config
# This assignment emits a D-Bus PropertiesChanged.
self.serialized_config = serialized_config
end
# Updates the config model info if needed.
def update_serialized_config_model
serialized_config_model = serialize_config_model
# return if self.serialized_config_model == serialized_config_model
# This assignment emits a D-Bus PropertiesChanged.
self.serialized_config_model = serialized_config_model
end
# Updates the proposal info if needed.
def update_serialized_proposal
serialized_proposal = serialize_proposal
# return if self.serialized_proposal == serialized_proposal
# This assignment emits a D-Bus PropertiesChanged.
self.serialized_proposal = serialized_proposal
self.ProposalChanged(serialized_proposal)
end
# Updates the issues info if needed.
def update_serialized_issues
serialized_issues = serialize_issues
# return if self.serialized_issues == serialized_issues
# This assignment emits a D-Bus PropertiesChanged.
self.serialized_issues = serialized_issues
end
# Updates the bootloader config if needed.
def update_serialized_bootloader_config
serialized_bootloader_config = serialize_bootloader_config
return if self.serialized_bootloader_config == serialized_bootloader_config
# This assignment emits a D-Bus PropertiesChanged.
self.serialized_bootloader_config = serialized_bootloader_config
end
# Generates the serialized JSON of the system.
#
# @return [String]
def serialize_system
return serialize_nil unless manager.probed?
json = {
devices: devices_json(:probed),
availableDrives: available_drives,
availableMdRaids: available_md_raids,
candidateDrives: candidate_drives,
candidateMdRaids: candidate_md_raids,
issues: system_issues_json,
productMountPoints: product_mount_points,
volumeTemplates: volume_templates
}
JSON.pretty_generate(json)
end
# Generates the serialized JSON of the storage config used for calculating the current
# proposal.
#
# @return [String]
def serialize_config
json = proposal.storage_json
JSON.pretty_generate(json)
end
# Generates the serialized JSON of the storage config model.
#
# @return [String]
def serialize_config_model
json = proposal.model_json
JSON.pretty_generate(json)
end
# Generates the serialized JSON of the proposal.
#
# @return [String]
def serialize_proposal
return serialize_nil unless manager.proposal.success?
json = {
devices: devices_json(:staging),
actions: actions_json
}
JSON.pretty_generate(json)
end
# Generates the serialized JSON of the list of issues.
#
# @return [String]
def serialize_issues
super(manager.issues)
end
# Generates the serialized JSON of the bootloader config.
#
# @return [String]
def serialize_bootloader_config
manager.bootloader.config.to_json
end
# Representation of the null JSON.
#
# @return [String]
def serialize_nil
nil.to_json
end
# Hash representation of the given devicegraph from StorageManager.
#
# @param meth [Symbol] method used to get the devicegraph from StorageManager
# @return [Hash]
def devices_json(meth)
devicegraph = Y2Storage::StorageManager.instance.send(meth)
Agama::Storage::DevicegraphConversions::ToJSON.new(devicegraph).convert
end
# List of hash representation of the actions.
#
# @return [Array<Hash>]
# * :device [Integer]
# * :text [String]
# * :subvol [Boolean]
# * :delete [Boolean]
# * :resize [Boolean]
def actions_json
manager.actions.map do |action|
{
device: action.device_sid,
text: action.text,
subvol: action.on_btrfs_subvolume?,
delete: action.delete?,
resize: action.resize?
}
end
end
# List of hash representation of the problems found during system probing.
#
# @see #serialize_system
#
# @return [Array<Hash>]
def system_issues_json
manager.system_issues.map { |i| issue_json(i) }
end
# @see Storage::System#available_drives
# @return [Array<Integer>]
def available_drives
proposal.storage_system.available_drives.map(&:sid)
end
# @see Storage::System#available_drives
# @return [Array<Integer>]
def candidate_drives
proposal.storage_system.candidate_drives.map(&:sid)
end
# @see Storage::System#available_drives
# @return [Array<Integer>]
def available_md_raids
proposal.storage_system.available_md_raids.map(&:sid)
end
# @see Storage::System#available_drives
# @return [Array<Integer>]
def candidate_md_raids
proposal.storage_system.candidate_md_raids.map(&:sid)
end
# Meaningful mount points for the current product.
#
# @return [Array<String>]
def product_mount_points
volume_templates_builder
.all
.map(&:mount_path)
.reject(&:empty?)
end
# Default volumes to be used as templates
#
# @return [Array<Hash>]
def volume_templates
volumes = volume_templates_builder.all
volumes << volume_templates_builder.for("") unless volumes.map(&:mount_path).include?("")
volumes.map do |vol|
Agama::Storage::VolumeConversions::ToJSON.new(vol).convert
end
end
# @return [Agama::Storage::Proposal]
def proposal
manager.proposal
end
# @return [Agama::Config]
def product_config
manager.product_config
end
# @return [Agama::VolumeTemplatesBuilder]
def volume_templates_builder
Agama::Storage::VolumeTemplatesBuilder.new_from_config(product_config)
end
end
end
end
end