Class: Yast::NetworkServiceClass

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

Constant Summary

BACKENDS =

network backend identification to service name mapping

{
  # <internal-id>        <service name>
  netconfig:       "network",
  network_manager: "NetworkManager",
  wicked:          "wicked"
}
BACKEND_PKG_NAMES =

network backend identification to its rpm package name mapping

{
  # <internal-id>        <service name>
  netconfig:       "sysconfig-network",
  network_manager: "NetworkManager",
  wicked:          "wicked"
}
SYSTEMCTL =
"/bin/systemctl"
WICKED =
"/usr/sbin/wicked"
DEFAULT_BACKEND =
:wicked

Instance Method Summary (collapse)

Instance Method Details

- (Boolean) backend_available?(backend) Also known as: is_backend_available

Checks if given network backend is available in the system

Returns:

  • (Boolean)


124
125
126
# File '../../src/modules/NetworkService.rb', line 124

def backend_available?(backend)
  PackageSystem.Installed(BACKEND_PKG_NAMES[backend])
end

- (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



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File '../../src/modules/NetworkService.rb', line 276

def ConfirmNetworkManager
  if !@already_asked_for_NetworkManager && network_manager?
    # 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) disable

disables network service completely



180
181
182
183
184
185
186
# File '../../src/modules/NetworkService.rb', line 180

def disable
  @cached_name = nil
  stop_service(@current_name)
  disable_service(@current_name)

  Read()
end

- (Boolean) disabled? Also known as: is_disabled

Returns:

  • (Boolean)


152
153
154
# File '../../src/modules/NetworkService.rb', line 152

def disabled?
  cached_service?(nil)
end

- (Object) EnableDisableNow

Helper to apply a change of the network service



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File '../../src/modules/NetworkService.rb', line 209

def EnableDisableNow
  return if !Modified()

  stop_service(@current_name)
  disable_service(@current_name)

  case @cached_name
  when :network_manager, :wicked
    RunSystemCtl(BACKENDS[@cached_name], "--force enable")
  when :netconfig
    RunSystemCtl(BACKENDS[@current_name], "disable")

    # 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)
  end

  @initialized = false
  Read()

  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



236
237
238
# File '../../src/modules/NetworkService.rb', line 236

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

- (Object) isNetworkRunning



299
300
301
# File '../../src/modules/NetworkService.rb', line 299

def isNetworkRunning
  isNetworkv4Running || isNetworkv6Running
end

- (Object) isNetworkv4Running

test for IPv4



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File '../../src/modules/NetworkService.rb', line 304

def isNetworkv4Running
  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("IPv4 network is running ...")
    return true
  else
    Builtins.y2milestone("IPv4 network is not running ...")
    return false
  end
end

- (Object) isNetworkv6Running

test for IPv6



320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File '../../src/modules/NetworkService.rb', line 320

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("IPv6 network is running ...")
    return true
  else
    Builtins.y2milestone("IPv6 network is not running ...")
    return false
  end
end

- (Object) main



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File '../../src/modules/NetworkService.rb', line 75

def main
  Yast.import "SystemdService"
  Yast.import "NetworkConfig"
  Yast.import "Popup"
  Yast.import "Mode"
  Yast.import "Stage"
  Yast.import "PackageSystem"

  textdomain "base"

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

  # 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



118
119
120
121
# File '../../src/modules/NetworkService.rb', line 118

def Modified
  Read()
  @cached_name != @current_name
end

- (Boolean) netconfig? Also known as: is_netconfig

Returns:

  • (Boolean)


140
141
142
# File '../../src/modules/NetworkService.rb', line 140

def netconfig?
  cached_service?(:netconfig)
end

- (Boolean) network_manager? Also known as: is_network_manager

Checks if configuration is managed by NetworkManager

Returns:

  • (Boolean)

    true when the network is managed by an external tool, like NetworkManager, false otherwise



134
135
136
# File '../../src/modules/NetworkService.rb', line 134

def network_manager?
  cached_service?(:network_manager)
end

- (Object) Read

Initialize module data



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

def Read
  return if @initialized

  if Stage.initial
    @current_name = DEFAULT_BACKEND
    log.info "Running in installer/AutoYaST, use default: #{@current_name}"
  else
    service = SystemdService.find("network")
    @current_name = BACKENDS.invert[service.name] if service
  end

  @cached_name = @current_name

  log.info "Current backend: #{@current_name}"
  @initialized = true

  nil
end

- (Object) ReloadOrRestart

Reload or restars the network service.



241
242
243
244
245
246
247
248
249
# File '../../src/modules/NetworkService.rb', line 241

def ReloadOrRestart
  if Stage.initial
    # inst-sys is not running systemd nor sysV init, so systemctl call
    # is not available and service has to be restarted directly
    wicked_restart
  else
    systemctl_reload_restart
  end
end

- (Object) Restart

Restarts the network service



252
253
254
255
256
257
258
259
260
# File '../../src/modules/NetworkService.rb', line 252

def Restart
  if Stage.initial
    wicked_restart
  else
    systemctl_restart
  end

  nil
end

- (Object) run_wicked(*params)



106
107
108
109
110
111
112
113
114
# File '../../src/modules/NetworkService.rb', line 106

def run_wicked(*params)
  cmd = "#{WICKED} #{params.join(" ")}"
  ret = SCR.Execute(
    path(".target.bash"),
    cmd
  )

  Builtins.y2milestone("run_wicked: #{cmd} -> #{ret}")
end

- (Object) RunningNetworkPopup

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

Returns:

  • true if network running



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

def RunningNetworkPopup
  network_running = isNetworkRunning

  log.info "RunningNetworkPopup #{network_running}"

  if network_running
    return true
  else
    error_text = if Stage.initial
                   _(
                     "No running network detected.\n" \
                     "Restart installation and configure network in Linuxrc\n" \
                     "or continue without network."
                   )
                 else
                   _(
                     "No running network detected.\n" \
                     "Configure network with YaST or Network Manager plug-in\n" \
                     "and start this module again\n" \
                     "or continue without network."
                   )
                 end

    ret = Popup.ContinueCancel(error_text)

    log.error "Network not runing!"
    return ret
  end
end

- (Object) RunSystemCtl(service, action)

Helper to run systemctl actions

Returns:

  • exit code



95
96
97
98
99
100
101
102
103
104
# File '../../src/modules/NetworkService.rb', line 95

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) StartStop

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



263
264
265
266
267
# File '../../src/modules/NetworkService.rb', line 263

def StartStop
  ReloadOrRestart()

  nil
end

- (Object) use_netconfig



165
166
167
168
169
170
# File '../../src/modules/NetworkService.rb', line 165

def use_netconfig
  Read()
  @cached_name = :netconfig

  nil
end

- (Object) use_network_manager



158
159
160
161
162
163
# File '../../src/modules/NetworkService.rb', line 158

def use_network_manager
  Read()
  @cached_name = :network_manager

  nil
end

- (Object) use_wicked



172
173
174
175
176
177
# File '../../src/modules/NetworkService.rb', line 172

def use_wicked
  Read()
  @cached_name = :wicked

  nil
end

- (Boolean) wicked? Also known as: is_wicked

Returns:

  • (Boolean)


146
147
148
# File '../../src/modules/NetworkService.rb', line 146

def wicked?
  cached_service?(:wicked)
end