-
Notifications
You must be signed in to change notification settings - Fork 4
/
SlpServer.rb
472 lines (410 loc) · 12.5 KB
/
SlpServer.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
# encoding: utf-8
# File: modules/SlpServer.ycp
# Package: Configuration of slp-server
# Summary: SlpServer settings, input and output functions
# Authors: Zugec Michal <mzugec@suse.cz>
#
# $Id$
#
# Representation of the configuration of slp-server.
# Input and output routines.
require "yast"
require "yast2/system_service"
module Yast
class SlpServerClass < Module
def main
textdomain "slp-server"
Yast.import "Progress"
Yast.import "Report"
Yast.import "Summary"
Yast.import "Message"
Yast.import "Service"
Yast.import "SuSEFirewall"
Yast.import "Package"
Yast.import "Popup"
Yast.import "Confirm"
Yast.import "Mode"
Yast.import "Map"
Yast.import "NetworkService"
Yast.include self, "slp-server/helps.rb"
# Data was modified?
@modified = false
@configured = false
@serviceStatus = false
@proposal_valid = false
# Write only, used during autoinstallation.
# Don't run services and SuSEconfig, it's all done at one place.
@write_only = false
# Abort function
# return boolean return true if abort
@AbortFunction = fun_ref(method(:Modified), "boolean ()")
# Settings: Define all variables needed for configuration of slp-server
# TODO FIXME: Define all the variables necessary to hold
# TODO FIXME: the configuration here (with the appropriate
# TODO FIXME: description)
# TODO FIXME: For example:
# /**
# * List of the configured cards.
# */
# list cards = [];
#
# /**
# * Some additional parameter needed for the configuration.
# */
# boolean additional_parameter = true;
@SETTINGS = {}
@slp_config = {
"net.slp.useScopes" => "DEFAULT",
"net.slp.isDA" => "false",
"net.slp.isBroadcastOnly" => "false",
"net.slp.DAHeartBeat" => nil
}
@REGFILES = {}
@reg_files = []
end
# Service to configure
#
# @return [Yast2::SystemService]
def service
@service ||= Yast2::SystemService.find("slpd")
end
# Abort function
# @return [Boolean] return true if abort
def Abort
return @AbortFunction.call == true if @AbortFunction != nil
false
end
# Data was modified?
# @return true if modified
def Modified
Builtins.y2debug("modified=%1", @modified)
@modified
end
# read global configuration file /etc/slp.conf
def ReadGlobalConfig
@SETTINGS = Convert.convert(
SCR.Read(path(".etc.slp.all")),
:from => "any",
:to => "map <string, any>"
)
@REGFILES = Convert.convert(
SCR.Read(path(".etc.slp.reg.all")),
:from => "any",
:to => "map <string, any>"
)
Builtins.foreach(@SETTINGS) do |k1, v1|
if k1 == "value"
Builtins.foreach(
Convert.convert(
v1,
:from => "any",
:to => "list <map <string, any>>"
)
) do |v2|
if Ops.get(v2, "type") == 1
Ops.set(
@slp_config,
Ops.get_string(v2, "name", ""),
Ops.get_string(v2, "value", "")
)
end
end
end
end
Builtins.y2milestone("Values from /etc/slp.conf : %1", @slp_config)
@reg_files = Ops.get_list(@REGFILES, "value", [])
Builtins.y2milestone("values from /etc/slp.reg.d : %1", @reg_files)
true
end
# write global configuration file /etc/slp.conf
def WriteGlobalConfig
Builtins.foreach(@slp_config) do |k1, v1|
found = false
Ops.set(
@SETTINGS,
"value",
Builtins.maplist(Ops.get_list(@SETTINGS, "value", [])) do |v2|
if k1 == Ops.get_string(v2, "name", "")
Ops.set(v2, "type", v1 == nil ? 0 : 1)
Ops.set(v2, "value", v1)
found = true
end
deep_copy(v2)
end
)
if !found
Ops.set(
@SETTINGS,
"value",
Builtins.add(
Ops.get_list(@SETTINGS, "value", []),
{
"name" => k1,
"type" => v1 == nil ? 0 : 1,
"kind" => "value",
"comment" => "",
"value" => v1
}
)
)
end
end
Builtins.y2milestone("slp_config %1", @slp_config)
Builtins.y2milestone("SETTINGS %1", @SETTINGS)
Ops.set(@REGFILES, "value", @reg_files)
Builtins.y2debug("write reg_files %1", @reg_files)
Builtins.y2debug("write REGFILES %1", @REGFILES)
SCR.Write(path(".etc.slp.all"), @SETTINGS)
SCR.Write(path(".etc.slp.reg.all"), @REGFILES)
true
end
# check for package openslp-server installed
def installed_packages
Builtins.y2milestone("check for installed package")
ret = false
if !Package.InstallMsg(
"openslp-server",
_(
"<p>To configure the SLP server, the <b>%1</b> package must be installed.</p>"
) +
_("<p>Do you want to install it now?</p>")
)
Popup.Error(Message.CannotContinueWithoutPackagesInstalled)
else
ret = true
end
ret
end
# Read all slp-server settings
# @return true on success
def Read
# SlpServer read dialog caption
caption = _("Initializing SLP Server Configuration")
# TODO FIXME Set the right number of stages
steps = 4
sl = 500
Builtins.sleep(sl)
# TODO FIXME Names of real stages
# We do not set help text here, because it was set outside
Progress.New(
caption,
" ",
steps,
[
# Progress stage 1/3
_("Read the database"),
# Progress stage 2/3
_("Read the previous settings"),
# Progress stage 3/3
_("Detect the devices")
],
[
# Progress step 1/3
_("Reading the database..."),
# Progress step 2/3
_("Reading the previous settings..."),
# Progress step 3/3
_("Detecting the devices..."),
# Progress finished
_("Finished")
],
""
)
# check if user is root
return false if !Confirm.MustBeRoot
return false if !NetworkService.RunningNetworkPopup
Progress.NextStage
Builtins.sleep(sl)
Progress.set(false)
SuSEFirewall.Read
Progress.set(true)
# read database
# if(Abort()) return false;
Progress.NextStage
Report.Error(Message.CannotReadCurrentSettings) if !ReadGlobalConfig()
# read another database
return false if Abort()
Progress.NextStep
Report.Error(_("Cannot read database2.")) if false
Builtins.sleep(sl)
# detect devices
return false if Abort()
Progress.NextStage
return false if !installed_packages
Builtins.sleep(sl)
return false if Abort()
Progress.NextStage
Builtins.sleep(sl)
return false if Abort()
@modified = false
@configured = true
true
end
# Write all slp-server settings
# @return true on success
def Write
# SlpServer read dialog caption
caption = _("Saving SLP Server Configuration")
# TODO FIXME And set the right number of stages
steps = 2
sl = 500
Builtins.sleep(sl)
# TODO FIXME Names of real stages
# We do not set help text here, because it was set outside
Progress.New(
caption,
" ",
steps,
[
# Progress stage 1/2
_("Write the settings"),
# Progress stage 2/2
_("Run SuSEconfig")
],
[
# Progress step 1/2
_("Writing the settings..."),
# Progress step 2/2
_("Running SuSEconfig..."),
# Progress finished
_("Finished")
],
""
)
Progress.set(false)
SuSEFirewall.Write
Progress.set(true)
Progress.NextStage
# Error message
Report.Error(_("Cannot write settings.")) unless save_settings
Builtins.sleep(sl)
# run SuSEconfig
return false if Abort()
Progress.NextStage
Builtins.sleep(sl)
return false if Abort()
# Progress finished
Progress.NextStage
Builtins.sleep(sl)
return false if Abort()
@configured = true
true
end
# Saves service settings
#
# @return [Boolean] true if settings were correctly saved; false otherwise
def save_settings
WriteGlobalConfig() && save_status
end
# Saves service status (start mode and starts/stops the service)
#
# @note For AutoYaST and for command line actions, it does not save the service,
# due to the {#service} is not used in that cases.
#
# @return [Boolean]
def save_status
return true if Mode.auto || Mode.commandline
service.save
end
# Get all slp-server settings from the first parameter
# (For use by autoinstallation.)
# @param [Hash] settings The YCP structure to be imported.
# @return [Boolean] True on success
def Import(settings)
settings = deep_copy(settings)
Builtins.foreach(
Convert.convert(settings, :from => "map", :to => "map <string, any>")
) do |key, value|
case key
when "service"
@serviceStatus = Convert.to_boolean(value)
when "config"
Builtins.foreach(
Convert.convert(value, :from => "any", :to => "map <string, any>")
) { |k, v| Ops.set(@slp_config, k, v) }
when "files"
@reg_files = Convert.convert(
value,
:from => "any",
:to => "list <map <string, any>>"
)
end
end
true
end
# Dump the slp-server settings to a single map
# (For use by autoinstallation.)
# @return [Hash] Dumped settings (later acceptable by Import ())
def Export
result = {
"version" => "1.0",
"service" => @serviceStatus,
"config" => @slp_config,
"files" => @reg_files
}
@configured = true
deep_copy(result)
end
# Create a textual summary and a list of unconfigured cards
# @return summary of the current configuration
def Summary
summary = _("Configuration summary...")
if @configured
else
summary = Summary.NotConfigured
end
[summary, []]
end
# Create an overview table with all configured cards
# @return table items
def Overview
# TODO FIXME: your code here...
[]
end
# Return packages needed to be installed and removed during
# Autoinstallation to insure module has all needed software
# installed.
# @return [Hash] with 2 lists.
def AutoPackages
# TODO FIXME: your code here...
{ "install" => [], "remove" => [] }
end
# @deprecated
def GetStartService
@serviceStatus = Service.Enabled("slpd")
Builtins.y2milestone("Status of slpd service %1", @serviceStatus)
@serviceStatus
end
# @deprecated
def SetStartService(status)
Builtins.y2milestone("Set service status %1", status)
if status == true
Service.Enable("slpd")
else
Service.Disable("slpd")
end
@serviceStatus = status
nil
end
publish :function => :Modified, :type => "boolean ()"
publish :variable => :modified, :type => "boolean"
publish :variable => :configured, :type => "boolean"
publish :variable => :proposal_valid, :type => "boolean"
publish :variable => :write_only, :type => "boolean"
publish :variable => :AbortFunction, :type => "boolean ()"
publish :function => :Abort, :type => "boolean ()"
publish :variable => :slp_config, :type => "map <string, any>"
publish :variable => :reg_files, :type => "list <map <string, any>>"
publish :function => :Read, :type => "boolean ()"
publish :function => :Write, :type => "boolean ()"
publish :function => :Import, :type => "boolean (map)"
publish :function => :Export, :type => "map ()"
publish :function => :Summary, :type => "list ()"
publish :function => :Overview, :type => "list ()"
publish :function => :AutoPackages, :type => "map ()"
publish :function => :GetStartService, :type => "boolean ()"
publish :function => :SetStartService, :type => "void (boolean)"
end
SlpServer = SlpServerClass.new
SlpServer.main
end