Class: Yast::NetworkConfigClass

Inherits:
Module
  • Object
show all
Defined in:
library/network/src/modules/NetworkConfig.rb

Instance Method Summary collapse

Instance Method Details

#ExportObject

Export data

Returns:

  • dumped settings (later acceptable by Import())


189
190
191
# File 'library/network/src/modules/NetworkConfig.rb', line 189

def Export
  Builtins.eval("config" => @Config, "dhcp" => @DHCP)
end

#Import(settings) ⇒ Object

Import data

Parameters:

  • settings (Hash)

    settings to be imported

Returns:

  • true on success


176
177
178
179
180
181
182
183
184
185
# File 'library/network/src/modules/NetworkConfig.rb', line 176

def Import(settings)
  settings = deep_copy(settings)
  @Config = Builtins.eval(Ops.get_map(settings, "config", {}))
  @DHCP = Builtins.eval(Ops.get_map(settings, "dhcp", {}))

  @Orig_Config = nil
  @Orig_DHCP = nil

  true
end

#mainObject


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'library/network/src/modules/NetworkConfig.rb', line 36

def main
  Yast.import "String"

  # Basic network settings (/etc/sysconfig/network/config)
  @Config = {}

  # DHCP settings (/etc/sysconfig/network/dhcp)
  @DHCP = {}

  # Basic network settings
  @Orig_Config = nil

  # DHCP settings
  @Orig_DHCP = nil

  #--------------
  # PRIVATE DATA

  # True if data are already read
  @initialized = false
end

#ModifiedObject

Data was modified?

Returns:

  • true if modified


137
138
139
140
141
# File 'library/network/src/modules/NetworkConfig.rb', line 137

def Modified
  ret = @DHCP != @Orig_DHCP || @Config != @Orig_Config
  Builtins.y2debug("ret=%1", ret)
  ret
end

#ReadObject

Read all network settings from the SCR

Returns:

  • true on success


145
146
147
148
149
150
151
152
153
154
155
156
# File 'library/network/src/modules/NetworkConfig.rb', line 145

def Read
  return true if @initialized == true

  @Config = ReadConfig(path(".sysconfig.network.config"))
  @DHCP = ReadConfig(path(".sysconfig.network.dhcp"))

  @Orig_Config = Builtins.eval(@Config)
  @Orig_DHCP = Builtins.eval(@DHCP)

  @initialized = true
  true
end

#ReadConfig(config) ⇒ Object

Read sysconfig file. Uses metadata to distinguish between booleans, integers, and strings

Parameters:

  • config (Yast::Path)

    sysconfig file SCR path

Returns:

  • sysconfig file contents


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'library/network/src/modules/NetworkConfig.rb', line 62

def ReadConfig(config)
  Builtins.y2debug("config=%1", config)
  return {} if config.nil?

  ret = {}

  vars = SCR.Dir(config)
  vars = [] if vars.nil?
  Builtins.maplist(vars) do |var|
    varpath = Builtins.add(config, var)
    comment = Convert.to_string(SCR.Read(Builtins.add(varpath, "comment")))
    if Builtins.regexpmatch(
      comment,
      Ops.add(Ops.add("^.*## Type:[ \t]*([", String.CLower), "]*).*$")
    )
      comment = Builtins.regexpsub(
        comment,
        Ops.add(Ops.add("^.*## Type:[ \t]*([", String.CLower), "]*).*$"),
        "\\1"
      )
    end
    val = Convert.to_string(SCR.Read(varpath))
    Builtins.y2debug("%1[%2]=%3", var, comment, val)
    if !val.nil?
      if comment == "yesno" || val == "yes" || val == "no"
        Ops.set(ret, var, val == "yes")
      elsif comment == "integer"
        Ops.set(ret, var, Builtins.tointeger(val)) if val != ""
      else
        Ops.set(ret, var, val)
      end
    end
  end
  ret = {} if ret.nil?
  Builtins.y2debug("ret=%1", ret)
  deep_copy(ret)
end

#WriteObject

Update the SCR according to network settings

Returns:

  • true on success


160
161
162
163
164
165
166
167
168
169
170
171
# File 'library/network/src/modules/NetworkConfig.rb', line 160

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

  WriteConfig(path(".sysconfig.network.dhcp"), @DHCP)
  WriteConfig(path(".sysconfig.network.config"), @Config)

  true
end

#WriteConfig(config, data) ⇒ Object

Write sysconfig file

Parameters:

  • config (Yast::Path)

    sysconfig file SCR path

  • data (Hash)

    sysconfig file contents

Returns:

  • true if success


104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'library/network/src/modules/NetworkConfig.rb', line 104

def WriteConfig(config, data)
  data = deep_copy(data)
  Builtins.y2debug("config=%1", config)
  Builtins.y2debug("data=%1", data)

  return false if config.nil? || data.nil?

  changed = false
  Builtins.maplist(
    Convert.convert(data, from: "map", to: "map <string, any>")
  ) do |var, val|
    oldval = Convert.to_string(SCR.Read(Builtins.add(config, var)))
    newval = if Ops.is_boolean?(val)
      Convert.to_boolean(val) ? "yes" : "no"
    else
      Builtins.sformat("%1", val)
    end
    if oldval.nil? || oldval != newval
      SCR.Write(Builtins.add(config, var), newval)
      changed = true
    end
  end
  SCR.Write(config, nil) if changed == true

  Builtins.y2debug("changed=%1", changed)
  true
end