/
addon.rb
271 lines (230 loc) · 8.1 KB
/
addon.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
# encoding: utf-8
# ------------------------------------------------------------------------------
# Copyright (c) 2014 SUSE LLC
#
# 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.
# ------------------------------------------------------------------------------
#
require "forwardable"
require "registration/sw_mgmt"
module Registration
# this is a wrapper class around SUSE::Connect::Product object
class Addon
class << self
# read the remote add-on from the registration server
# @param registration [Registration::Registration] use this object for
# reading the remote add-ons
def find_all(registration)
return @cached_addons if @cached_addons
pure_addons = registration.get_addon_list
# get IDs of the already activated addons
activated_addon_ids = registration.activated_products.map(&:id)
@cached_addons = pure_addons.reduce([]) do |res, addon|
yast_addons = create_addon_with_deps(addon)
# mark as registered if found in the status call
yast_addons.each do |yast_addon|
yast_addon.registered if activated_addon_ids.include?(yast_addon.id)
end
res.concat(yast_addons)
end
end
def reset!
@cached_addons = nil
@registered = nil
end
# list of registered add-ons
# @return [Array<Addon>] registered add-ons
def registered
@registered ||= []
end
# list of selected add-ons
# @return [Array<Addon>] selected add-ons
def selected
@selected ||= []
end
# Returns only those selected addons with accepted EULA
#
# @return [Array<Addon>]
def accepted
selected.reject(&:eula_refused?)
end
# Returns only those selected addons with accepted EULA but pending to be registered
#
# @return [Array<Addon>]
def to_register
accepted - registered
end
# return add-ons which are registered but not installed in the system
# and are available to install
# @return [Array<Addon>] the list of add-ons
def registered_not_installed
registered.select do |addon|
installed = SwMgmt.installed_products.find do |product|
product["name"] == addon.identifier &&
product["version_version"] == addon.version &&
product["arch"] == addon.arch
end
available = Yast::Pkg.ResolvableProperties(addon.identifier, :product, "").find do |p|
p["status"] == :available
end
!installed && available
end
end
private
# create an Addon from a SUSE::Connect::Product
# @param root [SUSE::Connect::Product] the root add-on object
# @return [Addon] created Addon object
def create_addon_with_deps(root)
root_addon = Addon.new(root)
result = [root_addon]
(root.extensions || []).each do |ext|
child = create_addon_with_deps(ext)
result.concat(child)
child.first.depends_on = root_addon
root_addon.children << child.first
end
result
end
end
extend Forwardable
attr_reader :children, :eula_accepted
attr_accessor :depends_on, :regcode
alias_method :eula_accepted?, :eula_accepted
# delegate methods to underlaying suse connect object
def_delegators :@pure_addon,
:arch,
:description,
:eula_url,
:free,
:friendly_name,
:id,
:identifier,
:name,
:product_type,
:release_type,
:release_stage,
:version,
:repositories
# the constructor
# @param pure_addon [SUSE::Connect::Product] a pure add-on from the registration server
def initialize(pure_addon)
@pure_addon = pure_addon
@eula_accepted = false
@children = []
end
# is the add-on selected
# @return [Boolean] true if the add-on is selectec
def selected?
Addon.selected.include?(self)
end
# select the add-on
def selected
Addon.selected << self unless selected?
end
# unselect the add-on
def unselected
Addon.selected.delete(self) if selected?
end
# toggle the selection state of the add-on
def toggle_selected
if selected?
unselected
else
selected
end
end
# has been the add-on registered?
# @return [Boolean] true if the add-on has been registered
def registered?
Addon.registered.include?(self)
end
# mark the add-on as registered
def registered
Addon.registered << self unless registered?
end
# just internally mark the addon as NOT registered, not a real unregistration
def unregistered
Addon.registered.delete(self) if registered?
end
def beta_release?
release_stage == "beta"
end
# get a product printable name (long name if present, fallbacks to the short name)
# @return [String] label usable in UI
def label
(friendly_name && !friendly_name.empty?) ? friendly_name : name
end
# can be the addon selected in UI or should it be disabled?
# return [Boolean] true if it should be enabled
def selectable?
# Do not support unregister
return false if registered?
# Do not select not available addons
return false if !available?
# Do not allow to select child without selected or already registered parent
return false if depends_on && !(depends_on.selected? || depends_on.registered?)
# Do not allow to unselect parent if any children is selected
return false if children.any?(&:selected?)
true
end
# Convert to a Hash, exports only the basic Addon properties
# @param [Boolean] release_type_string if true the "release_type" atribute
# will be always a String (nil will be converted to "nil")
# @return [Hash] Hash with basic Addon properties
def to_h(release_type_string: false)
{
"name" => identifier,
"arch" => arch,
"version" => version,
"release_type" => (release_type.nil? && release_type_string) ? "nil" : release_type
}
end
# is the addon available? SMT may have mirrored only some extensions,
# the not mirrored extensions are marked as not available
# @return [Boolean] true if the addon is available to register
def available?
# explicitly check for false, undefined (nil) means it is available,
# it's only reported by SMT
@pure_addon.available != false
end
# Checks whether this addon updates an old addon
# @param [Hash] old_addon addon Hash received from pkg-bindings
# @return [Boolean] true if it updates the old addon, false otherwise
def updates_addon?(old_addon)
old_addon["name"] == identifier || old_addon["name"] == @pure_addon.former_identifier
end
def matches_remote_product?(remote_product)
[:arch, :identifier, :version, :release_type].all? do |attr|
send(attr) == remote_product.send(attr)
end
end
# Set the EULA as accepted
def accept_eula
@eula_accepted = true
end
# Whether the eula has been refused
#
# @return [Boolean] true if EULA acceptance was required but refused; false otherwise
def eula_refused?
eula_acceptance_needed? && !eula_accepted
end
# Whether the EULA acceptance is required
#
# @return [Boolean] true if a not empty EULA url is present; false otherwise
def eula_acceptance_needed?
!eula_url.to_s.strip.empty?
end
end
end