Class: Yast::NetworkServiceClass

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

Instance Method Summary (collapse)

Instance Method Details

- (Boolean) ConfirmNetworkManager

Opens up a continue/cancel confirmation popup in the case when NetworkManager is enabled. User is informed that continuing the configuration may produce undefined results. If NetworkManager is not used, silently returns true.

Returns:

  • (Boolean)

    continue



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File '../../src/modules/NetworkService.rb', line 230

def ConfirmNetworkManager
  if !@already_asked_for_NetworkManager && IsManaged()
    # TRANSLATORS: pop-up question when reading the service configuration
    cont = Popup.ContinueCancel(
      _(
        "Your network interfaces are currently controlled by NetworkManager\n" +
          "but the service to configure might not work well with it.\n" +
          "\n" +
          "Really continue?"
      )
    )
    Builtins.y2milestone(
      "Network is controlled by NetworkManager, user decided %1...",
      cont ? "to continue" : "not to continue"
    )
    @already_asked_for_NetworkManager = true

    return cont
  else
    return true
  end
end

- (Object) EnableDisable

Enables and disables the appropriate services.



135
136
137
138
139
140
141
142
# File '../../src/modules/NetworkService.rb', line 135

def EnableDisable
  # Workaround for bug #61055:
  Builtins.y2milestone("Enabling service %1", "network")
  cmd = "cd /; /sbin/insserv -d /etc/init.d/network"
  SCR.Execute(path(".target.bash"), cmd)

  nil
end

- (Object) EnableDisableNow

Helper to apply a change of the network service



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File '../../src/modules/NetworkService.rb', line 157

def EnableDisableNow
  if Modified()
    # Stop should be called before, but when the service
    # were not correctly started until now, stop may have
    # no effect.
    # So let's kill all processes in the network service
    # cgroup to make sure e.g. dhcp clients are stopped.
    RunSystemCtl(@cur_service_id_name, "kill")

    if @new_service_id_name == "network"
      RunSystemCtl(@cur_service_id_name, "disable")
    else
      RunSystemCtl(@new_service_id_name, "--force enable")
    end
    @cur_service_id_name = Service.GetServiceId("network")
    @new_service_id_name = @cur_service_id_name
  end

  nil
end

- (Object) IsActive

Reports if network service is active or not. It does not report if network is connected.

Returns:

  • true when network service is active



181
182
183
# File '../../src/modules/NetworkService.rb', line 181

def IsActive
  RunSystemCtl("network", "is-active") == 0
end

- (Object) IsManaged

Whether use NetworkManager or ifup

Returns:

  • true when the network is managed, false when the /etc/init.d/network script is in use.



107
108
109
110
# File '../../src/modules/NetworkService.rb', line 107

def IsManaged
  Read()
  @new_service_id_name != "network"
end

- (Object) isNetworkRunning

test for IPv4



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File '../../src/modules/NetworkService.rb', line 255

def isNetworkRunning
  net = Convert.to_integer(
    SCR.Execute(
      path(".target.bash"),
      "ip addr|grep -v '127.0.0\\|inet6'|grep -c inet"
    )
  )
  if net == 0
    Builtins.y2milestone("Network is running ...")
    return true
  else
    Builtins.y2milestone("Network is not running ...")
    return false
  end
end

- (Object) isNetworkv6Running

test for IPv6



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File '../../src/modules/NetworkService.rb', line 271

def isNetworkv6Running
  net = Convert.to_integer(
    SCR.Execute(
      path(".target.bash"),
      "ip addr|grep -v 'inet6 ::1\\|inet6 fe80'|grep -c inet6"
    )
  )
  if net == 0
    Builtins.y2milestone("Network is running ...")
    return true
  else
    Builtins.y2milestone("Network is not running ...")
    return false
  end
end

- (Object) main



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

def main

  Yast.import "Service"
  Yast.import "NetworkConfig"
  Yast.import "Popup"
  Yast.import "Mode"

  textdomain "base"

  # if false, read needs to do work
  @initialized = false

  # current network service id name
  @cur_service_id_name = ""

  # the new network service id name
  @new_service_id_name = ""

  # Path to the systemctl command
  @systemctl = "/bin/systemctl"

  # Variable remembers that the question has been asked during this run already.
  # It avoids useless questions over and over again.
  @already_asked_for_NetworkManager = false
end

- (Object) Modified

Whether a network service change were requested

Returns:

  • true when service change were requested



91
92
93
94
95
96
97
98
99
100
101
102
# File '../../src/modules/NetworkService.rb', line 91

def Modified
  ret = false
  Read()
  ret = true if @new_service_id_name != @cur_service_id_name
  Builtins.y2debug(
    "NetworkService::Modified(%1, %2) => %3",
    @cur_service_id_name,
    @new_service_id_name,
    ret
  )
  ret
end

- (Object) Read

Initialize module data



121
122
123
124
125
126
127
128
129
130
131
132
# File '../../src/modules/NetworkService.rb', line 121

def Read
  if !@initialized
    @cur_service_id_name = Service.GetServiceId("network")
    @new_service_id_name = @cur_service_id_name

    nm = @new_service_id_name != "network"
    Builtins.y2milestone("NetworkManager: %1", nm)
  end
  @initialized = true

  nil
end

- (Object) ReloadOrRestart

Reload or restars the network service.



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File '../../src/modules/NetworkService.rb', line 186

def ReloadOrRestart
  if IsActive()
    if Modified()
      # reload is not sufficient
      RunSystemCtl("network", "stop")
      EnableDisableNow()
      RunSystemCtl("network", "start")
    else
      # reload may be unsupported
      RunSystemCtl("network", "reload-or-try-restart")
    end
  else
    # always stop, it does not hurt if the net was stopped.
    RunSystemCtl("network", "stop")
    EnableDisableNow()
    RunSystemCtl("network", "start")
  end

  nil
end

- (Object) Restart

Restarts the network service



208
209
210
211
212
213
214
# File '../../src/modules/NetworkService.rb', line 208

def Restart
  RunSystemCtl("network", "stop")
  EnableDisableNow()
  RunSystemCtl("network", "start")

  nil
end

- (Object) RunningNetworkPopup

If there is network running, return true. Otherwise show error popup depending on Mode and return false

Returns:

  • true if network running



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File '../../src/modules/NetworkService.rb', line 290

def RunningNetworkPopup
  Builtins.y2internal("RunningNetworkPopup %1", isNetworkRunning)
  if isNetworkRunning
    return true
  else
    error_text = Builtins.sformat(
      "%1\n%2 %3",
      _("No running network detected."),
      Mode.installation ?
        _("Restart installation and configure network in Linuxrc") :
        _(
          "Configure network with YaST or Network Manager plug-in\nand start this module again"
        ),
      _("or continue without network.")
    )
    Popup.ContinueCancel(error_text)
    Builtins.y2error("Network not runing!")
    return false
  end
end

- (Object) RunScript(script, action)

Run /etc/init.d script with specified action

Parameters:

  • script

    name of the init script

  • action

    the action to use

Returns:

  • true, when the script exits with 0



148
149
150
151
152
153
154
# File '../../src/modules/NetworkService.rb', line 148

def RunScript(script, action)
  return true if script == ""
  Builtins.y2milestone("rc%1 %2", script, action)
  # Workaround for bug #61055:
  cmd = Builtins.sformat("cd /; /etc/init.d/%1 %2", script, action)
  SCR.Execute(path(".target.bash"), cmd) == 0
end

- (Object) RunSystemCtl(service, action)

Helper to run systemctl actions

Returns:

  • exit code



78
79
80
81
82
83
84
85
86
87
# File '../../src/modules/NetworkService.rb', line 78

def RunSystemCtl(service, action)
  cmd = Builtins.sformat("%1 %2 %3.service", @systemctl, action, service)
  ret = Convert.convert(
    SCR.Execute(path(".target.bash_output"), cmd, { "TERM" => "raw" }),
    :from => "any",
    :to   => "map <string, any>"
  )
  Builtins.y2debug("RunSystemCtl: Command '%1' returned '%2'", cmd, ret)
  Ops.get_integer(ret, "exit", -1)
end

- (Object) SetManaged(m)

Parameters:

  • m (Boolean)

    whether networkmanager will be used



113
114
115
116
117
118
# File '../../src/modules/NetworkService.rb', line 113

def SetManaged(m)
  Read()
  @new_service_id_name = m ? "NetworkManager" : "network"

  nil
end

- (Object) StartStop

This is an old, confusing name for ReloadOrRestart() now



217
218
219
220
221
# File '../../src/modules/NetworkService.rb', line 217

def StartStop
  ReloadOrRestart()

  nil
end