Module: Yast::NtpClientMiscInclude

Defined in:
../../src/include/ntp-client/misc.rb

Instance Method Summary (collapse)

Instance Method Details

- (Fixnum) getClockType(address)

Get the type of the clock from the address

Parameters:

  • address (String)

    string the clock identification in the IP address form

Returns:

  • (Fixnum)

    the clock type



153
154
155
156
157
158
159
160
161
162
163
164
165
# File '../../src/include/ntp-client/misc.rb', line 153

def getClockType(address)
  return 0 if address == ""
  if !IP.Check4(address)
    Builtins.y2error("Invalid address: %1", address)
    return nil
  end
  cl_type = Builtins.regexpsub(
    address,
    "[0-9]+.[0-9]+.([0-9]+).[0-9]+",
    "\\1"
  )
  Builtins.tointeger(cl_type)
end

- (Array) getClockTypesCombo

Get entries for the clock type combo box

Returns:

  • (Array)

    of items for the combo box



223
224
225
226
227
228
229
230
231
# File '../../src/include/ntp-client/misc.rb', line 223

def getClockTypesCombo
  clock_names = Builtins.mapmap(@clock_types) do |k, v|
    { k => Ops.get(v, "name", "") }
  end
  ret = Builtins.maplist(clock_names) do |k, v|
    [Builtins.sformat("%1", k), v]
  end
  deep_copy(ret)
end

- (Fixnum) getClockUnitNumber(address)

Get the unit number of the clock from the address

Parameters:

  • address (String)

    string the clock identification in the IP address form

Returns:

  • (Fixnum)

    the unit number



188
189
190
191
192
193
194
195
196
197
198
199
200
# File '../../src/include/ntp-client/misc.rb', line 188

def getClockUnitNumber(address)
  return 0 if address == ""
  if !IP.Check4(address)
    Builtins.y2error("Invalid address: %1", address)
    return nil
  end
  cl_type = Builtins.regexpsub(
    address,
    "[0-9]+.[0-9]+.[0-9]+.([0-9]+)",
    "\\1"
  )
  Builtins.tointeger(cl_type)
end

- (Object) initialize_ntp_client_misc(include_target)



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File '../../src/include/ntp-client/misc.rb', line 11

def initialize_ntp_client_misc(include_target)
  Yast.import "UI"

  textdomain "ntp-client"

  Yast.import "CWMFirewallInterfaces"
  Yast.import "IP"
  Yast.import "Popup"
  Yast.import "Progress"
  Yast.import "Service"
  Yast.import "NtpClient"

  Yast.include include_target, "ntp-client/clocktypes.rb"

  # FIXME this is quite ugly ... the whole checkinf if something was changed
  # ... but it works :-)
  @sync_record_modified = false
end

- (String) opts2string(options, other)

Create options string from a map

Parameters:

  • options (Hash{String => Object})

    a map options represented as a map

  • other (String)

    string other options that were set as string

Returns:

  • (String)

    options represented as a string



125
126
127
128
129
130
131
132
133
134
135
136
# File '../../src/include/ntp-client/misc.rb', line 125

def opts2string(options, other)
  options = deep_copy(options)
  ret = other
  Builtins.foreach(options) do |k, v|
    if v == true
      ret = Builtins.sformat("%1 %2", ret, k)
    elsif v != false && v != ""
      ret = Builtins.sformat("%1 %2 %3", ret, k, v)
    end
  end
  ret
end

- (Object) PollAbort

Check for pending Abort press

Returns:

  • true if pending abort



146
147
148
# File '../../src/include/ntp-client/misc.rb', line 146

def PollAbort
  UI.PollInput == :abort
end

- (Object) proposeInterfacesToAllowAccess

Propose the interface to be allowed for access in firewall At the moment not used



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
# File '../../src/include/ntp-client/misc.rb', line 235

def proposeInterfacesToAllowAccess
  recs = NtpClient.getSyncRecords
  recs = Builtins.filter(recs) do |r|
    Ops.get_string(r, "type", "") == "peer" ||
      Ops.get_string(r, "type", "") == "broadcastclient"
  end
  addresses = Builtins.maplist(recs) { |r| Ops.get_string(r, "address", "") }
  addresses = Builtins.filter(addresses) { |a| a != "" && a != nil }
  addresses = Builtins.maplist(addresses) do |a|
    next a if IP.Check4(a)
    m = Convert.to_map(
      SCR.Execute(
        path(".target.bash_output"),
        Builtins.sformat("/usr/bin/host %1 | /bin/grep address", a)
      )
    )
    next nil if Ops.get_integer(m, "exit", 0) != 0
    out = Ops.get_string(m, "stdout", "")
    out = Builtins.regexpsub(out, "has address (.*)$", "\\1")
    out
  end
  addresses = Builtins.filter(addresses) { |a| a != "" && a != nil }
  ifaces = Builtins.maplist(addresses) do |a|
    m = Convert.to_map(
      SCR.Execute(
        path(".target.bash_output"),
        Builtins.sformat("/sbin/ip route get %1", a)
      )
    )
    next nil if Ops.get_integer(m, "exit", 0) != 0
    out = Ops.get_string(m, "stdout", "")
    out = Builtins.mergestring(Builtins.splitstring(out, "\n"), " ")
    out = Builtins.regexpsub(out, "dev[ ]+([^ ]+)[ ]+src", "\\1")
    out
  end
  ifaces = Builtins.toset(ifaces)
  ifaces = Builtins.filter(ifaces) { |i| i != nil }
  deep_copy(ifaces)
end

- (Object) ReallyAbort

If modified, ask for confirmation

Returns:

  • true if abort is confirmed



140
141
142
# File '../../src/include/ntp-client/misc.rb', line 140

def ReallyAbort
  !NtpClient.modified || Popup.ReallyAbort(true)
end

- (Boolean) reallyExit

Ask user if exit without saving

Returns:

  • (Boolean)

    true if exit



32
33
34
35
36
# File '../../src/include/ntp-client/misc.rb', line 32

def reallyExit
  # yes-no popup
  !NtpClient.modified ||
    Popup.YesNo(_("Really exit?\nAll changes will be lost."))
end

- (Object) reallyExitComplex



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File '../../src/include/ntp-client/misc.rb', line 48

def reallyExitComplex
  pol = ""
  if UI.QueryWidget(Id("policy_combo"), :Value) == :auto
    pol = "auto"
  elsif UI.QueryWidget(Id("policy_combo"), :Value) == :custom
    pol = Convert.to_string(UI.QueryWidget(Id("custom_policy"), :Value))
  end


  if NtpClient.run_service ==
      (UI.QueryWidget(Id("start"), :CurrentButton) == "boot") &&
      NtpClient.run_chroot == UI.QueryWidget(Id("run_chroot"), :Value) &&
      NtpClient.ntp_policy == pol &&
      !CWMFirewallInterfaces.OpenFirewallModified("firewall") &&
      !@sync_record_modified
    return true
  end
  @sync_record_modified = true
  reallyExit
end

- (Object) reallyExitSimple



38
39
40
41
42
43
44
45
46
# File '../../src/include/ntp-client/misc.rb', line 38

def reallyExitSimple
  if NtpClient.run_service ==
      (UI.QueryWidget(Id("start"), :CurrentButton) == "boot") &&
      Ops.get_string(NtpClient.selected_record, "address", "") ==
        UI.QueryWidget(Id("server_address"), :Value)
    return true
  end
  reallyExit
end

- (Object) restartNtpDaemon

Restart the NTP daemon service



70
71
72
73
74
# File '../../src/include/ntp-client/misc.rb', line 70

def restartNtpDaemon
  Service.RunInitScript(NtpClient.service_name, "restart")

  nil
end

- (String) setClockType(address, clock_type)

Set the clock type into an IP address

Parameters:

  • address (String)

    string the IP address to patch the clock number into

  • clock_type (Fixnum)

    integer the clock type to be set

Returns:

  • (String)

    IP address with clock type set correctly



171
172
173
174
175
176
177
178
179
180
181
182
183
# File '../../src/include/ntp-client/misc.rb', line 171

def setClockType(address, clock_type)
  address = "127.127.0.0" if address == ""
  if !IP.Check4(address)
    Builtins.y2error("Invalid address: %1", address)
    return nil
  end
  ret = Builtins.regexpsub(
    address,
    "([0-9]+.[0-9]+.)[0-9]+(.[0-9]+)",
    Builtins.sformat("\\1%1\\2", clock_type)
  )
  ret
end

- (String) setClockUnitNumber(address, unit_number)

Set the clock unit number into an IP address

Parameters:

  • address (String)

    string the IP address to patch the clock number into

  • unit_number (Fixnum)

    integer the unit number to be set

Returns:

  • (String)

    IP address with unit number set correctly



206
207
208
209
210
211
212
213
214
215
216
217
218
# File '../../src/include/ntp-client/misc.rb', line 206

def setClockUnitNumber(address, unit_number)
  address = "127.127.0.0" if address == ""
  if !IP.Check4(address)
    Builtins.y2error("Invalid address: %1", address)
    return nil
  end
  ret = Builtins.regexpsub(
    address,
    "([0-9]+.[0-9]+.[0-9]+.)[0-9]+",
    Builtins.sformat("\\1%1", unit_number)
  )
  ret
end

- (Object) silentWrite

Write the NTP settings without displaying progress



77
78
79
80
81
82
83
# File '../../src/include/ntp-client/misc.rb', line 77

def silentWrite
  progress_orig = Progress.set(false)
  NtpClient.Write
  Progress.set(progress_orig)

  nil
end

- (Hash) string2opts(options_string, with_param, without_param)

Parse string to map of options

Parameters:

  • options_string (String)

    string of options

  • with_param (Array<String>)

    a list of options that must have a parameter

  • without_param (Array<String>)

    a list of options that don't have any parameter

Returns:

  • (Hash)

    options as a map



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
# File '../../src/include/ntp-client/misc.rb', line 91

def string2opts(options_string, with_param, without_param)
  with_param = deep_copy(with_param)
  without_param = deep_copy(without_param)
  l = Builtins.splitstring(options_string, " ")
  l = Builtins.filter(l) { |e| e != "" }
  ignore_next = false
  index = -1
  unknown = []
  ret = Builtins.listmap(l) do |e|
    index = Ops.add(index, 1)
    if ignore_next
      ignore_next = false
      next { e => nil }
    end
    ignore_next = false
    if Builtins.contains(with_param, e)
      ignore_next = true
      next { e => Ops.get(l, Ops.add(index, 1), "") }
    elsif Builtins.contains(without_param, e)
      next { e => true }
    else
      unknown = Builtins.add(unknown, e)
      next { e => nil }
    end
  end
  ret = Builtins.filter(ret) { |k, v| v != nil }
  ret = { "parsed" => ret, "unknown" => Builtins.mergestring(unknown, " ") }
  deep_copy(ret)
end