• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

yast / yast-installation / 8705582428

16 Apr 2024 12:27PM UTC coverage: 40.913% (-0.2%) from 41.105%
8705582428

Pull #1114

github

shundhammer
Version bump and change log
Pull Request #1114: WIP: Handle autoinst AND autoupgrade (bsc#1222153)

0 of 1 new or added line in 1 file covered. (0.0%)

95 existing lines in 13 files now uncovered.

4489 of 10972 relevant lines covered (40.91%)

6.24 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.05
/src/lib/installation/upgrade_repo_manager.rb
1
# ------------------------------------------------------------------------------
2
# Copyright (c) 2020 SUSE LLC
3
#
4
# This program is free software; you can redistribute it and/or modify it under
5
# the terms of version 2 of the GNU General Public License as published by the
6
# Free Software Foundation.
7
#
8
# This program is distributed in the hope that it will be useful, but WITHOUT
9
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
11
#
12
# ------------------------------------------------------------------------------
13

14
require "yast"
1✔
15

16
require "y2packager/new_repository_setup"
1✔
17
require "y2packager/original_repository_setup"
1✔
18
require "y2packager/repository"
1✔
19
require "y2packager/service"
1✔
20

21
Yast.import "Pkg"
1✔
22

23
module Installation
1✔
24
  # This class takes care of managing the old repositories and services
25
  # during upgrade. It takes care of modifying the old repositories
26
  # and using them in the new upgraded system.
27
  class UpgradeRepoManager
1✔
28
    include Yast::Logger
1✔
29
    extend Yast::Logger
1✔
30

31
    # @return [Array<Y2Packager::Repository>] The old repositories
32
    attr_reader :repositories
1✔
33
    # @return [Array<Y2Packager::Repository>] The old services
34
    attr_reader :services
1✔
35

36
    # Constructor
37
    #
38
    # @param old_repositories [Array<Y2Packager::Repository>] the old
39
    #   repositories which should be managed
40
    # @param old_services [Array<Y2Packager::Service>] the old
41
    #   services which should be managed
42
    def initialize(old_repositories, old_services)
1✔
43
      @repositories = old_repositories
17✔
44
      @services = old_services
17✔
45
      @new_urls = {}
17✔
46
      # by default remove all repositories
47
      @status_map = repositories.map { |r| [r, :removed] }.to_h
50✔
48
    end
49

50
    def self.create_from_old_repositories
1✔
51
      # Find the current repositories for the old ones,
52
      # the repositories might have been changed during the upgrade
53
      # workflow by other clients, this ensures we use the current data.
54
      current_repos = Y2Packager::Repository.all
3✔
55
      stored_repos = Y2Packager::OriginalRepositorySetup.instance.repositories
3✔
56
      stored_repo_aliases = stored_repos.map(&:repo_alias)
3✔
57
      # exclude the newly added repositories with the same alias
58
      stored_repo_aliases -= Y2Packager::NewRepositorySetup.instance.repositories
3✔
59
      reg_urls = registration_urls
3✔
60

61
      old_repos = current_repos.select do |r|
3✔
62
        stored_repo_aliases.include?(r.repo_alias) &&
5✔
63
          !reg_urls.include?(base_url(r.raw_url.uri))
64
      end
65

66
      current_services = Y2Packager::Service.all
3✔
67
      stored_services = Y2Packager::OriginalRepositorySetup.instance.services
3✔
68
      stored_service_aliases = stored_services.map(&:alias)
3✔
69
      # exclude the newly added services with the same name
70
      stored_service_aliases -= Y2Packager::NewRepositorySetup.instance.services
3✔
71
      old_services = current_services.select { |s| stored_service_aliases.include?(s.alias) }
3✔
72

73
      # sort the repositories by name
74
      new(old_repos.sort_by(&:name), old_services)
3✔
75
    end
76

77
    # Return the configured status of a repository.
78
    # @param  repo [Y2Packager::Repository] the repository
79
    # @return [Symbol, nil] `:removed`, `:enabled` or `:disabled` symbol,
80
    #    `nil` if the repository is not known
81
    def repo_status(repo)
1✔
82
      status_map[repo]
22✔
83
    end
84

85
    # Return the repository URL, if it was changed by the user than the new
86
    # URL is returned.
87
    #
88
    # @param repo [Y2Packager::Repository] The queried repository
89
    # @return [String] The URL
90
    def repo_url(repo)
1✔
91
      new_urls[repo] || repo.url.to_s
6✔
92
    end
93

94
    # Toggle the repository status.
95
    # It cycles the repository status in this order:
96
    # Removed->Enabled->Disabled->Removed->Enabled->Disabled->...
97
    #
98
    # @param  repo [Y2Packager::Repository] the repository
99
    # @return [Symbol, nil] `:removed`, `:enabled` or `:disabled` symbol,
100
    #    `nil` if the repository is not known
101
    def toggle_repo_status(repo)
1✔
102
      case repo_status(repo)
9✔
103
      when :enabled
104
        status_map[repo] = :disabled
3✔
105
      when :disabled
106
        status_map[repo] = :removed
1✔
107
      when :removed
108
        status_map[repo] = :enabled
5✔
109
      end
110
    end
111

112
    # Change the URL of a repository.
113
    #
114
    # @param repo [Y2Packager::Repository] The repository
115
    # @param url [String] Its new URL
116
    def change_url(repo, url)
1✔
117
      new_urls[repo] = url if repo.raw_url.to_s != url
2✔
118
    end
119

120
    # Activate the changes. This will enable/disable the repositories,
121
    # set the new URLs and remove old services without saving the changes.
122
    # To make the changes permanent (saved to disk) call `YaST::Pkg.SourceSaveAll`
123
    # after calling this method.
124
    def activate_changes
1✔
125
      update_urls
7✔
126
      process_repos
7✔
127
      remove_services
7✔
128

129
      # reload the package manager to activate the changes
130
      Yast::Pkg.SourceSaveAll
7✔
131
      Yast::Pkg.SourceFinishAll
7✔
132
      Yast::Pkg.SourceRestore
7✔
133
      Yast::Pkg.SourceLoad
7✔
134
    end
135

136
  private
1✔
137

138
    # remove the old services
139
    def remove_services
1✔
140
      log.info("Old services to remove: #{services.map(&:alias).inspect}")
7✔
141
      services.each do |s|
7✔
142
        log.info("Removing old service #{s.alias}...")
7✔
143
        Yast::Pkg.ServiceDelete(s.alias)
7✔
144
      end
145
    end
146

147
    # @return [Hash<Y2Packager::Repository,Symbol>] Maps the repositories
148
    # to the new requested state.
149
    attr_reader :status_map
1✔
150

151
    # @return [Hash<Y2Packager::Repository,String>] Maps the repositories
152
    # to the new requested URLs.
153
    attr_reader :new_urls
1✔
154

155
    # change the status of the repositories to the requested states
156
    def process_repos
1✔
157
      status_map.each do |repo, status|
7✔
158
        case status
14✔
159
        when :enabled
160
          log.info("Enabling #{repo.repo_alias.inspect} ...")
1✔
161
          repo.enable!
1✔
162
        when :disabled
163
          log.info("Disabling #{repo.repo_alias.inspect} ...")
1✔
164
          repo.disable!
1✔
165
        when :removed
166
          log.info("Removing #{repo.repo_alias.inspect} ...")
12✔
167
          repo.delete!
12✔
168
        end
169
      end
170
    end
171

172
    # update the repository URLs to the requested values
173
    def update_urls
1✔
174
      new_urls.each do |repo, url|
7✔
175
        repo.url = url
1✔
176

177
        # if the repository will be enabled refresh the content
178
        Yast::Pkg.SourceForceRefreshNow(repo.repo_id) if status_map[repo] == :enabled
1✔
179
      end
180
    end
181

182
    # Collect the repository URLs for all registered products and addons,
183
    # If the system is not registered or the yast2-registration package is not
184
    # installed then it return an empty list.
185
    #
186
    # @return [Array<URI>] list of simplified URLs
187
    # @see .base_url
188
    def self.registration_urls
1✔
189
      require "registration/registration"
3✔
UNCOV
190
      require "registration/registration_ui"
×
UNCOV
191
      require "registration/url_helpers"
×
192

UNCOV
193
      return [] unless Registration::Registration.is_registered?
×
194

UNCOV
195
      registration = Registration::Registration.new(Registration::UrlHelpers.registration_url)
×
196
      registration_ui = Registration::RegistrationUI.new(registration)
×
197
      activations = registration_ui.activated_products
×
198

199
      activations.map(&:repositories).flatten.map { |repo| base_url(repo["url"]) }
×
200
    rescue LoadError
201
      # the registration package is not available in the openSUSE installer
202
      # or during RPM build
203
      log.info("Registration package not available")
3✔
204
      []
3✔
205
    end
206

207
    # Remove some URL parts to allow less strict comparison:
208
    # - remove the query parameter, the locally saved SCC repositories have
209
    #   an unique hash attached as a query parameter, the activated products
210
    #   result does not contain that
211
    # - ignore the trailing slash, it is not important for comparing repositories
212
    #
213
    # @param repo_url [String, URI] the input URL
214
    # @return [URI] simplified URL
215
    def self.base_url(repo_url)
1✔
216
      uri = repo_url.is_a?(URI) ? repo_url.dup : URI(repo_url)
5✔
217
      uri.query = nil
5✔
218
      # do NOT use the bang method here (delete_suffix!), it would modify
219
      # the original URL although the .dup is used above!
220
      uri.path = uri.path.delete_suffix("/")
5✔
221
      uri
5✔
222
    end
223

224
    private_class_method :registration_urls, :base_url
1✔
225
  end
226
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc