/
script.rb
353 lines (293 loc) · 9.4 KB
/
script.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
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
# Copyright (c) [2020] 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 "yast"
require "shellwords"
require "transfer/file_from_url"
Yast.import "AutoinstConfig"
module Y2Autoinstallation
# Base class representing custom user script that runs during various phases of installation.
class Script
include Yast::Logger
include Yast::Transfer::FileFromUrl
include Yast::I18n
# filename of script, used as ID
# @return [String]
attr_reader :filename
# Source code of script. Either source or location should be defined.
# @return [String] empty string means not defined
attr_reader :source
# URI for location from which download script. Either source or location should be defined.
# @return [String] empty string means not defined
attr_reader :location
# Flag if script runs in debug mode.
# @return [Boolean]
attr_reader :debug
def initialize(hash)
@filename = hash["filename"] || ""
@source = hash["source"] || ""
@location = hash["location"] || ""
@debug = !!hash["debug"]
resolve_location
end
def to_hash
{
"filename" => filename,
"source" => source,
"location" => location,
"debug" => debug
}
end
def logs_dir
Yast::AutoinstConfig.logs_dir
end
# difference from filename is that it always return non empty string
# @return [String]
def script_name
return filename unless filename.empty?
if location.empty?
self.class.type
else
url = Yast::URL.Parse(location)
File.basename(url["path"])
end
end
# location where script lives
# @return [String]
def script_path
File.join(Yast::AutoinstConfig.scripts_dir, script_name)
end
# Downloads or writes down script file
def create_script_file
# ensure path are available
Yast::SCR.Execute(Yast::Path.new(".target.mkdir"), File.dirname(script_path))
Yast::SCR.Execute(Yast::Path.new(".target.mkdir"), logs_dir)
if !location.empty?
url = Yast::URL.Parse(location)
res = get_file_from_url(
scheme: url["scheme"],
host: url["host"],
urlpath: url["path"],
localfile: localfile,
urltok: url,
destdir: Yast::AutoinstConfig.destdir
)
# TODO: exception?
log.error "script #{location} could not be retrieved" unless res
elsif !source.empty?
Yast::SCR.Write(Yast::Path.new(".target.string"), script_path, source)
else
log.error "Neither location or source specified for #{inspect}"
end
end
protected
# where to download file for get_file_from_url which do not switch itself chroot
# Just work-around for not respecting changed chroot (see bsc#897212 and bsc#889931))
def localfile
script_path
end
private
def resolve_location
return if @location.empty?
log.info "Resolving location #{@location.inspect}"
@location.strip!
return unless @location.start_with?("relurl://")
path = location[9..-1] # 9 is relurl:// size
if Yast::AutoinstConfig.scheme == "relurl"
log.info "autoyast profile was relurl too"
newloc = Yast::SCR.Read(Yast::Path.new(".etc.install_inf.ayrelurl"))
tok = Yast::URL.Parse(newloc)
@location = "#{tok["scheme"]}://#{File.join(tok["host"], File.dirname(tok["path"]), path)}"
else
config = Yast::AutoinstConfig
@location = "#{config.scheme}://#{File.join(config.host, config.directory, path)}"
end
log.info "resolved location #{@location.inspect}"
end
end
class ScriptFeedback
# feedback settings
# @return [:message | :warning | :error | :popup | :no]
attr_reader :value
def initialize(hash)
@value = if hash["feedback"]
case hash["feedback_type"]
when "message" then :message
when "warning" then :warning
when "error" then :error
when "", nil then :popup
else
raise "Unknown feedback type value #{hash["feedback_type"].inspect}"
end
else
:no
end
end
def to_hash
case @value
when :message then { "feedback" => true, "feedback_type" => "message" }
when :warning then { "feedback" => true, "feedback_type" => "warning" }
when :error then { "feedback" => true, "feedback_type" => "error" }
when :popup then { "feedback" => true, "feedback_type" => "" }
when :no then { "feedback" => false, "feedback_type" => "" }
else
raise "Unknown value '#{value.inspect}"
end
end
end
# Scripts that are executed by YaST ( so all expect init scripts )
class ExecutedScript < Script
# Sets if feedback after script finish should be shown
# @return [ScriptFeedback]
attr_reader :feedback
# Interpreter to use to run script.
# @return [String]
attr_reader :interpreter
# Notification message during script run
# @return [String] empty string if no notification should be shown
attr_reader :notification
# Params passed to script
# @return [Array]
attr_reader :params
# Reruns script even if already ran
# @return [Boolean]
attr_reader :rerun
def to_hash
res = super
res["interpreter"] = interpreter
res["notification"] = notification
res["param-list"] = params
res["rerun"] = rerun
res.merge(feedback.to_hash)
end
def initialize(hash)
super
@feedback = ScriptFeedback.new(hash)
@interpreter = hash["interpreter"] || "shell"
@notification = hash["notification"] || ""
@params = hash["param-list"] || []
@rerun = !!hash["rerun"]
end
DEBUG_FLAG_MAP = {
"shell" => "-x",
"perl" => "-w",
"ruby" => "-w"
}
INTERPRETER_MAP = {
"shell" => "/bin/sh", # TODO: why not bash? at least user can now specify interpreter he wants
"perl" => "/usr/bin/perl",
"python" => "/usr/bin/python"
}
# Runs the script
def execute
return if already_run? && !rerun
# TODO: maybe own class for interpreters?
cmd = INTERPRETER_MAP[interpreter] || interpreter
debug_flag = debug ? (DEBUG_FLAG_MAP[interpreter] || "") : ""
params_s = params.join(" ") # shell escaping is up to user, see documentation
bash_path = Yast::Path.new(".target.bash")
Yast::SCR.Execute(bash_path,
"#{cmd} #{debug_flag} #{script_path.shellescape} #{params_s} " \
"&> #{log_path.shellescape}")
Yast::SCR.Execute(bash_path, "/bin/touch #{run_file.shellescape}")
end
def log_path
File.join(logs_dir, script_name + ".log")
end
private
def already_run?
Yast::SCR.Read(Yast::Path.new(".target.size"), run_file) != -1
end
def run_file
script_path + "-run"
end
end
# Script that runs before any other and can modify autoyast profile
class PreScript < ExecutedScript
def logs_dir
File.join(Yast::AutoinstConfig.tmpDir, self.class.type, "logs")
end
def self.type
"pre-scripts"
end
def script_path
File.join(Yast::AutoinstConfig.tmpDir, self.class.type, script_name)
end
end
# Script that runs after finish of second stage
class PostScript < ExecutedScript
def self.type
"post-scripts"
end
end
# Script that runs before or after changing to chroot depending on chrooted parameter
class ChrootScript < ExecutedScript
# Flag that defines when script should be run
attr_reader :chrooted
def initialize(hash)
super
@chrooted = !!hash["chrooted"]
end
def to_hash
res = super
res["chrooted"] = chrooted
res
end
def self.type
"chroot-scripts"
end
def logs_dir
if chrooted
super
else
File.join(Yast::AutoinstConfig.destdir, Yast::AutoinstConfig.logs_dir)
end
end
protected
def localfile
if chrooted
File.join(Yast::AutoinstConfig.destdir, script_path)
else
super
end
end
end
# Script that runs after partitioning before rpm install
class PostPartitioningScript < ExecutedScript
def self.type
"postpartitioning-scripts"
end
def logs_dir
File.join(Yast::AutoinstConfig.tmpDir, self.class.type, "logs")
end
def script_path
File.join(Yast::AutoinstConfig.tmpDir, self.class.type, script_name)
end
end
# Runs after finish of second stage. It is executed by systemd and yast just write it down.
class InitScript < Script
def self.type
"init-scripts"
end
def script_path
File.join(Yast::AutoinstConfig.initscripts_dir, script_name)
end
end
# List of known script
SCRIPT_TYPES = [PreScript, PostScript, InitScript, ChrootScript, PostPartitioningScript]
end