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

yast / yast-autoinstallation / 7347071288

28 Dec 2023 11:13AM UTC coverage: 69.009% (+0.4%) from 68.607%
7347071288

push

github

web-flow
Merge pull request #874 from yast/rubocop_update

Rubocop update

94 of 254 new or added lines in 51 files covered. (37.01%)

22 existing lines in 13 files now uncovered.

6373 of 9235 relevant lines covered (69.01%)

10.29 hits per line

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

62.12
/src/modules/ProfileLocation.rb
1
# Copyright (c) [2013-2021] SUSE LLC
2
#
3
# All Rights Reserved.
4
#
5
# This program is free software; you can redistribute it and/or modify it
6
# under the terms of version 2 of the GNU General Public License as published
7
# by the Free Software Foundation.
8
#
9
# This program is distributed in the hope that it will be useful, but WITHOUT
10
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12
# more details.
13
#
14
# You should have received a copy of the GNU General Public License along
15
# with this program; if not, contact SUSE LLC.
16
#
17
# To contact SUSE LLC about this file by physical or electronic mail, you may
18
# find current contact information at www.suse.com.
19

20
# File:  modules/ProfileLocation.ycp
21
# Package:  Auto-installation
22
# Summary:  Process Auto-Installation Location
23
# Author:  Anas Nashif <nashif@suse.de>
24
#
25
# $Id$
26
require "yast"
1✔
27

28
require "ui/password_dialog"
1✔
29
require "autoinstall/xml_checks"
1✔
30
require "autoinstall/y2erb"
1✔
31
require "y2storage"
1✔
32
require "fileutils"
1✔
33
require "yast2/popup"
1✔
34
require "digest/sha1"
1✔
35

36
module Yast
1✔
37
  class ProfileLocationClass < Module
1✔
38
    include Yast::Logger
1✔
39

40
    def main
1✔
41
      Yast.import "UI"
1✔
42
      textdomain "autoinst"
1✔
43

44
      Yast.import "AutoinstConfig"
1✔
45
      Yast.import "AutoInstallRules"
1✔
46
      Yast.import "Mode"
1✔
47
      Yast.import "Installation"
1✔
48
      Yast.import "Report"
1✔
49
      Yast.import "Label"
1✔
50
      Yast.import "URL"
1✔
51
      Yast.import "InstURL"
1✔
52

53
      Yast.include self, "autoinstall/io.rb"
1✔
54
      ProfileLocation()
1✔
55
    end
56

57
    def profile_checker
1✔
58
      Y2Autoinstallation::XmlChecks.instance
1✔
59
    end
60

61
    # Constructor
62
    # @return [void]
63
    def ProfileLocation
1✔
64
      nil
65
    end
66

67
    # Initiate retrieving of control files and Rules.
68
    # @return [Boolean]
69
    def Process
1✔
70
      Builtins.y2milestone(
5✔
71
        "Path to remote control file: %1",
72
        AutoinstConfig.filepath
73
      )
74

75
      # Due to self-update this process could be called twice.
76
      # So we have to initialize the stack again. (bnc#1051483)
77
      AutoInstallRules.reset
5✔
78

79
      localfile = AutoinstConfig.xml_tmpfile
5✔
80

81
      is_directory = false
5✔
82

83
      if AutoinstConfig.scheme == "relurl"
5✔
84
        url_str = InstURL.installInf2Url("")
5✔
85
        log.info("installation path from install.inf: #{url_str}")
5✔
86

87
        if url_str.empty?
5✔
NEW
88
          log.warn("Cannot evaluate ZyppRepoURL from /etc/install.inf")
×
89
        else
90
          url = URL.Parse(url_str)
5✔
91
          AutoinstConfig.scheme = url["scheme"]
5✔
92
          AutoinstConfig.host = url["host"]
5✔
93
          AutoinstConfig.filepath = File.join(url["path"], AutoinstConfig.filepath)
5✔
94

95
          AutoinstConfig.scheme = "file" if ["cd", "cdrom"].include? AutoinstConfig.scheme
5✔
96

97
          ayrelurl = "#{AutoinstConfig.scheme}://#{AutoinstConfig.host}/#{AutoinstConfig.filepath}"
5✔
98
          log.info("relurl for profile changed to: #{ayrelurl}")
5✔
99
          SCR.Write(path(".etc.install_inf.ayrelurl"), ayrelurl)
5✔
100
          SCR.Write(path(".etc.install_inf"), nil)
5✔
101
        end
102
      end
103

104
      filename = basename(AutoinstConfig.filepath)
5✔
105

106
      if filename == ""
5✔
NEW
107
        is_directory = true
×
108
      else
109
        Builtins.y2milestone("File=%1", filename)
5✔
110
        Builtins.y2milestone(
5✔
111
          "Get %1://%2/%3 to %4",
112
          AutoinstConfig.scheme,
113
          AutoinstConfig.host,
114
          AutoinstConfig.filepath,
115
          localfile
116
        )
117
        ret = Get(
5✔
118
          AutoinstConfig.scheme,
119
          AutoinstConfig.host,
120
          AutoinstConfig.filepath,
121
          localfile
122
        )
123
        if !ret
5✔
124
          # autoyast hit an error while fetching it's config file
125
          error = _("An error occurred while fetching the profile:\n")
1✔
126
          Report.Error(Ops.add(error, @GET_error))
1✔
127
          return false
1✔
128
        end
129
        tmp = SCR.Read(path(".target.string"), localfile)
4✔
130

131
        unless tmp.valid_encoding?
4✔
132
          # TRANSLATORS: %s is the filename
133
          Report.Error(
1✔
134
            format(_("AutoYaST file %s\nhas no valid encoding or is corrupted."), filename)
135
          )
136
          return false
1✔
137
        end
138

139
        # log the profile checksum so we can verify that a particular file was really used
140
        log.info("Profile SHA1 checksum: #{Digest::SHA1.hexdigest(tmp)}")
3✔
141

142
        if GPG.encrypted_symmetric?(localfile)
3✔
143
          label = _("Encrypted AutoYaST profile.")
×
144
          begin
145
            if AutoinstConfig.ProfilePassword.empty?
×
146
              pwd = ::UI::PasswordDialog.new(label).run
×
147
              return false unless pwd
×
148
            else
149
              pwd = AutoinstConfig.ProfilePassword
×
150
            end
151

152
            content = GPG.decrypt_symmetric(localfile, pwd)
×
153
            AutoinstConfig.ProfilePassword = pwd
×
154
          rescue GPGFailed => e
155
            res = Yast2::Popup.show(_("Decryption of profile failed."),
×
156
              details: e.message, headline: :error, buttons: :continue_cancel)
157
            if res == :continue
×
158
              retry
×
159
            else
160
              return false
×
161
            end
162
          end
163
          SCR.Write(path(".target.string"), localfile, content)
×
164
        end
165

166
        # render erb template
167
        return false if AutoinstConfig.filepath.end_with?(".erb") && !render_erb(localfile)
3✔
168
      end
169

170
      AutoinstConfig.directory = dirname(AutoinstConfig.filepath)
1✔
171

172
      Builtins.y2milestone("Dir=%1", AutoinstConfig.directory)
1✔
173
      Builtins.y2milestone("Fetching Rules File")
1✔
174

175
      # Get rules file
176

177
      mkdir = Convert.to_boolean(
1✔
178
        SCR.Execute(path(".target.mkdir"), AutoinstConfig.local_rules_location)
179
      )
180
      if !mkdir
1✔
181
        Builtins.y2error(
×
182
          "Error creating directory: %1",
183
          AutoinstConfig.local_rules_location
184
        )
185
      end
186

187
      return false if !is_directory && !profile_checker.valid_profile?
1✔
188

189
      # no rules and classes support for erb templates
190
      return true if AutoinstConfig.filepath.end_with?(".erb")
1✔
191

192
      ret = if is_directory
×
193
        Get(
×
194
          AutoinstConfig.scheme,
195
          AutoinstConfig.host,
196
          Ops.add(
197
            Ops.add(AutoinstConfig.directory, "/"),
198
            AutoinstConfig.remote_rules_location
199
          ),
200
          AutoinstConfig.local_rules_file
201
        )
202
      else
203
        false
×
204
      end
205

206
      # Download has produced an error which is stored in the target
207
      # file --> delete it.
208
      if !ret && ::File.exist?(AutoinstConfig.local_rules_file)
×
209
        log.info("cleanup #{AutoinstConfig.local_rules_file}")
×
210
        ::FileUtils.rm(AutoinstConfig.local_rules_file)
×
211
      end
212

213
      if ret
×
214
        AutoInstallRules.userrules = true
×
215

216
        rules_sha1 = Digest::SHA1.hexdigest(File.read(AutoinstConfig.local_rules_file))
×
217
        # log the rules checksum so we can verify that a particular file was really used
218
        log.info("Rules SHA1 checksum: #{rules_sha1}")
×
219
      else
220
        AutoInstallRules.userrules = false
×
221
        SCR.Execute(path(".target.remove"), AutoinstConfig.local_rules_file)
×
222
      end
223

224
      process_rules = true
×
225
      try_default_rules = false
×
226
      if AutoInstallRules.userrules
×
227
        Builtins.y2milestone("Reading Rules File")
×
228
        # display an error when the rules file is not valid and return false
229
        return false unless profile_checker.valid_rules?
×
230

231
        AutoInstallRules.Read
×
232
        # returns false if no rules have matched
233
        ret = AutoInstallRules.GetRules
×
234
        try_default_rules = true if !ret
×
235
      else
236
        # required for classes too. Without it, they only work with rules together
237
        try_default_rules = true
×
238
      end
239

240
      # Hex IP, MAC Address.
241
      if try_default_rules
×
242
        Builtins.y2milestone("Creating default Rules")
×
243
        if is_directory
×
244
          # Create rules for hex ip and mac address
245
          AutoInstallRules.CreateDefault
×
246
        else
247
          # Create rules for file
248
          AutoInstallRules.CreateFile(filename)
×
249
          # And add there already downloaded and decrypted profile (bsc#1176336)
250
          ::FileUtils.cp(localfile, File.join(AutoinstConfig.local_rules_location, filename))
×
251
        end
252
        ret = AutoInstallRules.GetRules
×
253
        return false if !ret
×
254
      end
255

256
      if process_rules
×
257
        rulesret = AutoInstallRules.Process(AutoinstConfig.xml_tmpfile)
×
258
        # validate the profile
259
        return false if rulesret && !profile_checker.valid_profile?
×
260

261
        Builtins.y2milestone("rulesret=%1", rulesret)
×
262
        return rulesret
×
263
      end
264

265
      true
×
266
    end
267

268
    publish function: :ProfileLocation, type: "void ()"
1✔
269
    publish function: :Process, type: "boolean ()"
1✔
270

271
  private
1✔
272

273
    # Renders the ERB profile and saves the result
274
    #
275
    # An error popup is shown if there is an error while rendering the profile.
276
    #
277
    # @return [Boolean] true if everything was ok.
278
    def render_erb(file)
1✔
279
      res = nil
3✔
280

281
      begin
282
        res = Y2Autoinstallation::Y2ERB.render(file)
3✔
283
      rescue StandardError => e
284
        message = _("There was an error while rendering the ERB profile.")
2✔
285
        details = e.message + "\n\n" + e.backtrace.join("\n")
2✔
286

287
        Yast2::Popup.show(message, headline: :error, details: details)
2✔
288
        return false
2✔
289
      end
290

291
      SCR.Write(path(".target.string"), file, res)
1✔
292
      true
1✔
293
    end
294
  end
295

296
  ProfileLocation = ProfileLocationClass.new
1✔
297
  ProfileLocation.main
1✔
298
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

© 2026 Coveralls, Inc