-
Notifications
You must be signed in to change notification settings - Fork 20
/
AutoinstClass.rb
252 lines (220 loc) · 7.78 KB
/
AutoinstClass.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
# encoding: utf-8
# File:
# modules/AutoinstClass.ycp
#
# Module:
# AutoinstClass
#
# Summary:
# This module handles the configuration for auto-installation
#
# Authors:
# Anas Nashif <nashif@suse.de>
#
# $Id$
require "yast"
module Yast
class AutoinstClassClass < Module
include Yast::Logger
MERGE_XSLT_PATH = "/usr/share/autoinstall/xslt/merge.xslt"
def main
Yast.import "AutoinstConfig"
Yast.import "XML"
Yast.import "Summary"
Yast.include self, "autoinstall/xml.rb"
@classDir = AutoinstConfig.classDir
@ClassConf = "/etc/autoinstall"
@profile_conf = []
@Profiles = []
@Classes = []
@deletedClasses = []
@confs = []
@class_file = "classes.xml"
@classPath = File.join(@classDir, @class_file)
AutoinstClass()
end
# Finds a profile path
# @param name [String] profile name
# @param class_ [String] class name
# @return [String] profile Path
def findPath(name, class_)
result = @confs.find { |c| c["name"] == name && c["class"] == class_ }
result ||= { "class" => "", "name" => "default" }
File.join(@classDir, result["class"], result["name"])
end
# Reads classes
def Read
if SCR.Read(path(".target.size"), @classPath) != -1
# TODO: use XML module
classes_map = Convert.to_map(SCR.Read(path(".xml"), @classPath))
@Classes = (classes_map && classes_map["classes"]) || []
else
@Classes = []
end
nil
end
# we are doing some compatibility fixes here and move
# from one /etc/autoinstall/classes.xml to multiple
# classes.xml files, one for each repository
def Compat
if !class_file_exists? && compat_class_file_exists?
log.info "Compat: #{@classPath} not found but #{compat_class_file} exists"
new_classes_map = { "classes" => read_old_classes }
log.info "creating #{new_classes_map}"
XML.YCPToXMLFile(:class, new_classes_map, @classPath)
end
nil
end
# Changes the directory and reads the class definitions
#
# @param newdir [String] Path of the new directory
# @return nil
# @see class_dir=
def classDirChanged(newdir)
self.class_dir = newdir
Compat()
Read()
nil
end
# Changes the directory of classes definitions.
#
# AutoinstConfig#classDir= is called to set the new value
# in the configuration. It does not check if the directory
# exists or is accessible.
#
# @return [String] path of the new directory.
def class_dir=(newdir)
AutoinstConfig.classDir = newdir
@classDir = newdir
@classPath = File.join(@classDir, @class_file)
newdir
end
# Constructor
# @return void
def AutoinstClass
classSetup
Compat()
Read()
nil
end
MERGE_CMD = "/usr/bin/xsltproc".freeze
MERGE_DEFAULTS = "--novalid --maxdepth 10000 --param replace \"'false'\"".freeze
# Merge classes
#
def MergeClasses(configuration, base_profile, resultFileName)
dontmerge_str = ""
AutoinstConfig.dontmerge.each_with_index do |dm, i|
dontmerge_str << " --param dontmerge#{i+1} \"'#{dm}'\" "
end
merge_command =
"#{MERGE_CMD} #{MERGE_DEFAULTS} #{dontmerge_str} --param with " \
"\"'#{findPath(configuration["name"], configuration["class"])}'\" " \
"--output #{File.join(AutoinstConfig.tmpDir, resultFileName)} " \
"#{MERGE_XSLT_PATH} #{base_profile} "
out = SCR.Execute(path(".target.bash_output"), merge_command, {})
log.info "Merge command: #{merge_command}"
log.info "Merge stdout: #{out["stdout"]}, stderr: #{out["stderr"]}"
out
end
# Reads files from class directories
# @return [void]
def Files
@confs = []
@Classes.each do |class_|
class_name_ = class_["name"] || "xxx"
files_path = File.join(@classDir, class_name_)
files = Convert.convert(SCR.Read(path(".target.dir"), files_path),
:from => "any", :to => "list <string>")
next if files.nil?
log.info "Files in class #{class_name_}: #{files}"
new_confs = files.map { |f| { "class" => class_name_, "name" => f } }
log.info "Configurations: #{new_confs}"
@confs.concat(new_confs)
end
log.info "Configurations: #{@confs}"
nil
end
# Saves classes definitions
def Save
@deletedClasses.each do |c|
to_del = "/bin/rm -rf #{File.join(AutoinstConfig.classDir, c)}"
SCR.Execute(path(".target.bash"), to_del)
end
@deletedClasses = []
tmp = { "classes" => @Classes }
log.debug "saving classes: #{@classPath}"
XML.YCPToXMLFile(:class, tmp, @classPath)
end
# Imports configuration
# @param [Array<Hash>] settings Configuration
# @return true
def Import(settings)
@profile_conf = deep_copy(settings)
true
end
# Exports configuration
# @return [Array<Hash>] Copy of the configuration
def Export
deep_copy(@profile_conf)
end
# Builds the configuration summary
# @return [String] Configuration summary
def Summary
summary = ""
@profile_conf.each do |conf|
summary = Summary.AddHeader(summary, conf["class_name"] || "None")
summary = Summary.AddLine(summary, conf["configuration"] || "None")
end
summary.empty? ? Summary.NotConfigured : summary
end
publish :variable => :classDir, :type => "string"
publish :variable => :ClassConf, :type => "string"
publish :variable => :profile_conf, :type => "list <map>"
publish :variable => :Profiles, :type => "list"
publish :variable => :Classes, :type => "list <map>"
publish :variable => :deletedClasses, :type => "list <string>"
publish :variable => :confs, :type => "list <map>"
publish :function => :findPath, :type => "string (string, string)"
publish :function => :Read, :type => "void ()"
publish :function => :Compat, :type => "void ()"
publish :function => :classDirChanged, :type => "void (string)"
publish :function => :AutoinstClass, :type => "void ()"
publish :function => :MergeClasses, :type => "map (map, string, string)"
publish :function => :Files, :type => "void ()"
publish :function => :Save, :type => "boolean ()"
publish :function => :Import, :type => "boolean (list <map>)"
publish :function => :Export, :type => "list <map> ()"
publish :function => :Summary, :type => "string ()"
private
# Checks if a classes.xml exists
# @return [true,false] Returns true when present (false otherwise).
def class_file_exists?
SCR.Read(path(".target.size"), @classPath) > 0
end
# Checks if an old classes.xml exists
# @return [true,false] Returns true when present (false otherwise).
# @see compat_class_file
def compat_class_file_exists?
SCR.Read(path(".target.size"), compat_class_file) > 0
end
# Returns the path of the old classes.xml file
# By default, it is called /etc/autoinstall/classes.xml.
# @return [String] Path to the old classes.xml file.
def compat_class_file
File.join(@ClassConf, @class_file)
end
# Builds a map of classes to import from /etc/autoinstall/classes.xml
# @return [Array<Hash>] Classes defined in the file.
def read_old_classes
old_classes_map = Convert.to_map(SCR.Read(path(".xml"), compat_class_file))
old_classes = (old_classes_map && old_classes_map["classes"]) || []
old_classes.each_with_object([]) do |class_, new_classes|
class_path_ = File.join(@classDir, class_["name"] || "")
log.info "looking for #{class_path_}"
new_classes << class_ unless SCR.Read(path(".target.dir"), class_path_).nil?
end
end
end
AutoinstClass = AutoinstClassClass.new
AutoinstClass.main
end