Class: Yast::RoutingClass

Inherits:
Module
  • Object
show all
Defined in:
../../src/modules/Routing.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) Export

Dump the Routing settings to a map, for autoinstallation use.

Returns:

  • autoinstallation settings



281
282
283
284
285
286
287
288
# File '../../src/modules/Routing.rb', line 281

def Export
  exproute = {}
  if Ops.greater_than(Builtins.size(Builtins.eval(@Routes)), 0)
    Ops.set(exproute, "routes", Builtins.eval(@Routes))
  end
  Ops.set(exproute, "ip_forward", @Forward)
  deep_copy(exproute)
end

- (Object) GetDevices

Get the current devices list

Returns:

  • devices list



292
293
294
# File '../../src/modules/Routing.rb', line 292

def GetDevices
  deep_copy(@devices)
end

- (Object) GetGateway

Get the default gateway

Returns:

  • gateway



298
299
300
301
302
303
304
305
306
# File '../../src/modules/Routing.rb', line 298

def GetGateway
  defgw = ""
  Builtins.maplist(@Routes) do |r|
    if Ops.get_string(r, "destination", "") == "default"
      defgw = Ops.get_string(r, "gateway", "")
    end
  end
  defgw
end

- (Object) Import(settings)

Get all the Routing configuration from a map. When called by routing_auto (preparing autoinstallation data) the map may be empty.

Parameters:

  • settings (Hash)

    autoinstallation settings

Returns:

  • true if success



266
267
268
269
270
271
272
273
274
275
276
277
# File '../../src/modules/Routing.rb', line 266

def Import(settings)
  settings = deep_copy(settings)
  @Routes = Builtins.eval(Ops.get_list(settings, "routes", []))
  @Forward = Ops.get_boolean(settings, "ip_forward", false)

  @Orig_Routes = nil
  @Orig_Forward = nil

  @modified = true

  true
end

- (Object) main



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
# File '../../src/modules/Routing.rb', line 36

def main
  Yast.import "UI"
  textdomain "network"

  Yast.import "NetHwDetection"
  Yast.import "NetworkInterfaces"
  Yast.import "Map"
  Yast.import "SuSEFirewall"

  Yast.include self, "network/runtime.rb"
  Yast.include self, "network/routines.rb"

  # All routes
  # list <map <string, string> >:
  # keys: destination, gateway, netmask, [device, [extrapara]]
  @Routes = []

  # modified by AY (bnc#649494)
  @modified = nil
  # Enable IP forwarding
  # .etc.sysctl_conf."net.ipv4.ip_forward"
  @Forward = false

  # List of available devices
  @devices = []

  # All routes read at the start
  @Orig_Routes = nil
  @Orig_Forward = nil

  # "routes" file location
  @routes_file = "/etc/sysconfig/network/routes"
end

- (Object) Modified

Data was modified?

Returns:

  • true if modified



72
73
74
75
76
77
78
79
80
# File '../../src/modules/Routing.rb', line 72

def Modified
  ret = @Routes != @Orig_Routes || @Forward != @Orig_Forward
  # probably called without Read()  (bnc#649494)
  if @Orig_Routes == nil && @Orig_Forward == nil && @modified != true
    ret = false
  end
  Builtins.y2debug("ret=%1", ret)
  ret
end

- (Object) Read

Read routing settings If no routes, sets a default gateway from Detection

Returns:

  • true if success



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
# File '../../src/modules/Routing.rb', line 160

def Read
  # read route.conf
  if Ops.greater_than(SCR.Read(path(".target.size"), @routes_file), 0)
    @Routes = Convert.convert(
      SCR.Read(path(".routes")),
      :from => "any",
      :to   => "list <map>"
    )
  else
    @Routes = []
  end

  ReadIPForwarding()

  Builtins.y2debug("Routes=%1", @Routes)
  Builtins.y2debug("Forward=%1", @Forward)

  # save routes to check for changes later
  @Orig_Routes = Builtins.eval(@Routes)
  @Orig_Forward = Builtins.eval(@Forward)

  # read available devices
  NetworkInterfaces.Read
  @devices = NetworkInterfaces.List("")

  if @Routes == []
    ReadFromGateway(Ops.get_string(NetHwDetection.result, "GATEWAY", ""))
  end

  true
end

- (Object) ReadFromGateway(gw)

Set the routes to contain only the default route, if it is defined. Used when there is nothing better.

Parameters:

  • gw (String)

    ip of the default gateway

Returns:

  • true if success



86
87
88
89
90
91
92
93
94
95
96
97
# File '../../src/modules/Routing.rb', line 86

def ReadFromGateway(gw)
  return false if gw == "" || gw == nil
  @Routes = [
    {
      "destination" => "default",
      "gateway"     => gw,
      "netmask"     => "-",
      "device"      => "-"
    }
  ]
  true
end

- (Object) ReadIPForwarding



115
116
117
118
119
120
121
122
123
# File '../../src/modules/Routing.rb', line 115

def ReadIPForwarding
  if SuSEFirewall.IsEnabled
    @Forward = SuSEFirewall.GetSupportRoute
  else
    @Forward = SCR.Read(path(".etc.sysctl_conf.\"net.ipv4.ip_forward\"")) == "1"
  end

  nil
end

- (Object) RemoveDefaultGw

Remove route with default gateway from Routes list



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File '../../src/modules/Routing.rb', line 100

def RemoveDefaultGw
  route = []
  Builtins.y2milestone(
    "Resetting default gateway - interface has been set to DHCP mode"
  )
  Builtins.foreach(@Routes) do |row|
    if Ops.get_string(row, "destination", "") != "default"
      route = Builtins.add(route, row)
    end
  end
  @Routes = deep_copy(route)

  nil
end

- (Object) SetDevices(devs)

Set the available devices list (for expert routing dialog)

Parameters:

  • devs (Array)

    new devices list

Returns:

  • true if success



311
312
313
314
315
316
317
318
319
# File '../../src/modules/Routing.rb', line 311

def SetDevices(devs)
  devs = deep_copy(devs)
  if devs == nil
    @devices = []
    return false
  end
  @devices = deep_copy(devs)
  true
end

- (Object) Summary

Create routing text summary

Returns:

  • summary text



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
# File '../../src/modules/Routing.rb', line 323

def Summary
  return "" if Ops.less_than(Builtins.size(@Routes), 1)

  Yast.import "Summary"
  summary = ""

  gw = GetGateway()
  gwhost = NetHwDetection.ResolveIP(gw)
  gw = Ops.add(Ops.add(Ops.add(gw, " ("), gwhost), ")") if gwhost != ""

  if gw != ""
    # Summary text
    summary = Summary.AddListItem(
      summary,
      Builtins.sformat(_("Gateway: %1"), gw)
    ) 
    # summary = add(summary, Summary::Device(sformat(_("Gateway: %1"), gw), ""));
  end

  if @Forward == true
    # Summary text
    summary = Summary.AddListItem(summary, _("IP Forwarding: on"))
  else
    # summary = add(summary, Summary::Device(_("IP Forwarding: on"), ""));

    # Summary text
    summary = Summary.AddListItem(summary, _("IP Forwarding: off"))
  end
  # summary = add(summary, Summary::Device(_("IP Forwarding: off"), ""));

  if @Routes != []
    # Summary text
    # summary = add(summary, Summary::Device(sformat(_("Routes: %1"), Routes), ""));
    Builtins.y2debug("not adding Routes to summary")
  end

  return "" if Ops.less_than(Builtins.size(summary), 1)
  Ops.add(Ops.add("<ul>", summary), "</ul>")
end

- (Object) Write

Write routing settings and apply changes

Returns:

  • true if success



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
# File '../../src/modules/Routing.rb', line 194

def Write
  Builtins.y2milestone("Writing configuration")
  if !Modified()
    Builtins.y2milestone("No changes to routing -> nothing to write")
    return true
  end

  steps = [
    # Progress stage 1
    _("Write IP forwarding settings"),
    # Progress stage 2
    _("Write routing settings")
  ]

  caption = _("Saving Routing Configuration")
  sl = 0 #100; //for testing

  Progress.New(caption, " ", Builtins.size(steps), steps, [], "")

  #Progress stage 1/2
  ProgressNextStage(_("Writing IP forwarding settings..."))

  WriteIPForwarding()
  Builtins.sleep(sl)

  # at first stop the running routes
  # FIXME SCR::Execute(.target.bash, "/etc/init.d/route stop");
  # sysconfig does not support restarting routes only,
  # so we let our caller do it together with other things

  #Progress stage 2/2
  ProgressNextStage(_("Writing routing settings..."))

  # create if not exists, otherwise backup
  if Ops.less_than(SCR.Read(path(".target.size"), @routes_file), 0)
    SCR.Write(path(".target.string"), @routes_file, "")
  else
    SCR.Execute(
      path(".target.bash"),
      Ops.add(
        Ops.add(
          Ops.add(Ops.add("/bin/cp ", @routes_file), " "),
          @routes_file
        ),
        ".YaST2save"
      )
    )
  end

  ret = false
  if @Routes == []
    # workaround bug [#4476]
    ret = SCR.Write(path(".target.string"), @routes_file, "")
  else
    # update the routes config
    ret = SCR.Write(path(".routes"), @Routes)
  end
  Builtins.sleep(sl)
  Progress.NextStage

  # and finally set up the new routes
  # FIXME SCR::Execute(.target.bash, "/etc/init.d/route start");

  ret == true
end

- (Object) WriteIPForwarding



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
# File '../../src/modules/Routing.rb', line 125

def WriteIPForwarding
  if SuSEFirewall.IsEnabled
    SuSEFirewall.SetSupportRoute(@Forward)
  else
    SCR.Write(
      path(".etc.sysctl_conf.\"net.ipv4.ip_forward\""),
      @Forward ? "1" : "0"
    )
    SCR.Write(
      path(".etc.sysctl_conf.\"net.ipv6.conf.all.forwarding\""),
      @Forward ? "1" : "0"
    )
    SCR.Write(path(".etc.sysctl_conf"), nil)
  end
  SCR.Execute(
    path(".target.bash"),
    Builtins.sformat(
      "echo %1 > /proc/sys/net/ipv4/ip_forward",
      @Forward ? 1 : 0
    )
  )
  SCR.Execute(
    path(".target.bash"),
    Builtins.sformat(
      "echo %1 > /proc/sys/net/ipv6/conf/all/forwarding",
      @Forward ? 1 : 0
    )
  )

  nil
end