Module: Yast::Wicked

Constant Summary collapse

BASH_PATH =
Path.new(".target.bash")
BASH_OUTPUT_PATH =
Path.new(".target.bash_output")
IBFT_CMD =
"/etc/wicked/extensions/ibft".freeze
WICKED_PATH =
"/usr/sbin/wicked".freeze

Instance Method Summary collapse

Instance Method Details

#firmware_configured_by?(interface) ⇒ Symbol?

Returns the firmware extension used for configuring the given interface or nil when it is not configured by firmware

Returns:

  • (Symbol, nil)

    Firmware extension used for configuring the interface or nil



119
120
121
# File 'src/lib/network/wicked.rb', line 119

def firmware_configured_by?(interface)
  firmware_interfaces_by_extension.find { |_, v| v.include?(interface) }&.first
end

#firmware_interfacesArray <String>

Returns an array of interface names which are configured via firmware

Returns:

  • (Array <String>)

    array of interface names



93
94
95
96
# File 'src/lib/network/wicked.rb', line 93

def firmware_interfaces
  interfaces = firmware_interfaces_by_extension.values.flatten
  (ibft_interfaces + interfaces).uniq
end

#firmware_interfaces_by_extensionHash

Returns a hash with each firmware extension as the key and the specific extension configured interfaces as an array value

Examples:

Yast::Lan.firmware_interfaces_by_extension => { ibft: ["ibft0"], nbft: ["nbft0"] }

Returns:

  • (Hash)

    configured by firmware interfaces indexed by the firmware extension



105
106
107
108
109
110
111
112
113
# File 'src/lib/network/wicked.rb', line 105

def firmware_interfaces_by_extension
  return @firmware_interfaces_by_extension if @firmware_interfaces_by_extension

  output = Yast::Execute.stdout.locally!(WICKED_PATH, "firmware", "interfaces")
  @firmware_interfaces_by_extension = output.split("\n").each_with_object({}) do |line, result|
    firmware, *interfaces = line.split(/\s+/)
    result[firmware.to_sym] = result.fetch(firmware.to_sym, []) + interfaces if firmware
  end
end

#ibft_interfacesArray <String>

Returns an array of interface names which are configured using iBFT

Returns:

  • (Array <String>)

    array of interface names



86
87
88
# File 'src/lib/network/wicked.rb', line 86

def ibft_interfaces
  @ibft_interfaces ||= Yast::Execute.stdout.locally!(IBFT_CMD, "-l").split(/\s+/).uniq
end

#parse_hostname(iface) ⇒ String

Parses wicked runtime configuration and returns hostname if set

Parameters:

  • iface (String)

    network device

Returns:

  • (String)

    hostname



54
55
56
57
58
# File 'src/lib/network/wicked.rb', line 54

def parse_hostname(iface)
  result = query_wicked(iface, "//hostname")
  # If there is more than one just pick the first one
  result.first
end

#parse_ntp_servers(iface) ⇒ Array<String>

Parses wicked runtime configuration and returns list of ntp servers

Parameters:

  • iface (String)

    network device

Returns:

  • (Array<String>)

    list of NTP servers



46
47
48
# File 'src/lib/network/wicked.rb', line 46

def parse_ntp_servers(iface)
  query_wicked(iface, "//ntp/server")
end

#query_wicked(iface, query) ⇒ String

Parses wicked runtime dhcp lease file for the given query

It parses both ipv4 and ipv6 lease files at once.

Parameters:

  • iface (String)

    queried interface

  • query (String)

    xpath query. See man wicked for info what is supported there.

Returns:

  • (String)

    result of the query

Raises:

  • (ArgumentError)


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'src/lib/network/wicked.rb', line 67

def query_wicked(iface, query)
  Yast.import "NetworkService"
  raise ArgumentError, "A network device has to be specified" if iface.nil? || iface.empty?
  raise "Parsing not supported for network service in use" if !NetworkService.is_wicked

  lease_files = ["ipv4", "ipv6"].map { |ip| "/var/lib/wicked/lease-#{iface}-dhcp-#{ip}.xml" }
  lease_files.find_all { |f| File.file?(f) }.reduce([]) do |stack, file|
    result = SCR.Execute(
      BASH_OUTPUT_PATH,
      "/usr/sbin/wicked xpath --file #{file.shellescape} \"%{#{query}}\""
    )

    stack + result.fetch("stdout", "").split("\n")
  end
end

#reload_config(devs) ⇒ Boolean

Reloads configuration for each device named in devs

Parameters:

  • devs (Array)

    list of device names

Returns:

  • (Boolean)

    true if configuration was reloaded; false otherwise

Raises:

  • (ArgumentError)


35
36
37
38
39
40
# File 'src/lib/network/wicked.rb', line 35

def reload_config(devs)
  raise ArgumentError if devs.nil?
  return true if devs.empty?

  SCR.Execute(BASH_PATH, "/usr/sbin/wicked ifreload #{devs.map(&:shellescape).join(" ")}").zero?
end