Class: Yast::HostClass

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

Instance Method Summary (collapse)

Instance Method Details

- (Object) EnsureHostnameResolvable



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File '../../src/modules/Host.rb', line 70

def EnsureHostnameResolvable
  local_ip = "127.0.0.2"
  if NeedDummyIP()
    Builtins.y2milestone("Dummy 127.0.0.2 IP will be added")
    # Add 127.0.0.2 entry to /etc/hosts,if product default says so
    # or user requests it otherwise some desktop apps may hang,
    # being unable to resolve hostname (bnc#304632)

    fqhostname = Hostname.MergeFQ(DNS.hostname, DNS.domain)
    Ops.set(
      @hosts,
      local_ip,
      [Ops.add(Ops.add(fqhostname, " "), DNS.hostname)]
    )
  else
    # Do not add it if product default says no
    # and remove 127.0.02 entry if it exists
    Ops.set(@hosts, local_ip, []) if Builtins.haskey(@hosts, local_ip)
  end
  @modified = true

  nil
end

- (Object) Export

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

Returns:

  • autoinstallation settings



209
210
211
212
213
214
215
216
217
218
# File '../../src/modules/Host.rb', line 209

def Export
  if Ops.greater_than(Builtins.size(@hosts), 0)
    # Filter out IPs with empty hostname (so that valid autoyast
    # profile is created)(#335120)
    @hosts = Builtins.filter(@hosts) { |_ip, names| names != [] }
    return { "hosts" => Builtins.eval(@hosts) }
  else
    return {}
  end
end

- (Boolean) GetModified

Function which returns if the settings were modified

Returns:

  • (Boolean)

    settings were modified



340
341
342
# File '../../src/modules/Host.rb', line 340

def GetModified
  @modified
end

- (Array) GetSystemHosts

Return “system” predefined hosts (should be present all the time)

Returns:

  • (Array)

    of system hosts



222
223
224
225
226
227
228
229
230
231
232
# File '../../src/modules/Host.rb', line 222

def GetSystemHosts
  [
    "127.0.0.1",
    "::1",
    "fe00::0",
    "ff00::0",
    "ff02::1",
    "ff02::2",
    "ff02::3"
  ]
end

- (Object) Import(settings)

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

Parameters:

  • settings (Hash)

    autoinstallation settings

Returns:

  • true if success



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

def Import(settings)
  settings = deep_copy(settings)
  @modified = true # trigger Write
  @initialized = true # don't let Read discard our data

  @hosts = Builtins.eval(Ops.get_map(settings, "hosts", {}))

  # convert from old format to the new one
  # use ::1 entry as a reference
  if Ops.greater_than(Builtins.size(Ops.get(@hosts, "::1", [])), 1)
    Builtins.foreach(@hosts) do |ip, hn|
      Ops.set(@hosts, ip, [Builtins.mergestring(hn, " ")])
    end
  end
  true
end

- (Object) main



33
34
35
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
# File '../../src/modules/Host.rb', line 33

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

  Yast.import "DNS"
  Yast.import "Hostname"
  Yast.import "NetworkInterfaces"
  Yast.import "String"
  Yast.import "Summary"

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

  # All hosts
  # See hosts(5)
  # keys: IPs, (But #35671 suggests that repeating IPs is valid)
  # values: names, the first one is the canonical one
  @hosts = {}

  # Data was modified?
  @modified = false

  # All hosts read at the start
  @hosts_init = {}

  # "hosts" file location
  @hosts_file = "/etc/hosts"

  # Only write configuration
  @write_only = false

  @initialized = false
end

- (Object) NeedDummyIP



66
67
68
# File '../../src/modules/Host.rb', line 66

def NeedDummyIP
  DNS.write_hostname
end

- (Object) Read

Read hosts settings

Returns:

  • true if success



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File '../../src/modules/Host.rb', line 96

def Read
  return true if @initialized == true

  # read /etc/hosts
  if Ops.greater_than(SCR.Read(path(".target.size"), @hosts_file), 0)
    hostlist = SCR.Dir(path(".etc.hosts"))
    @hosts = Builtins.listmap(hostlist) do |host|
      names = Convert.convert(
        SCR.Read(
          Builtins.topath(Builtins.sformat(".etc.hosts.\"%1\"", host))
        ),
        from: "any",
        to:   "list <string>"
      )
      next { host => names } if names != []
    end
  end

  # save hosts to check for changes later
  @hosts_init = deep_copy(@hosts)

  Builtins.y2debug("hosts=%1", @hosts)
  @initialized = true
  true
end

- (Object) ResolveHostnameToStaticIPs

if we have a static address, make sure /etc/hosts resolves it to our, bnc#664929



328
329
330
331
332
333
334
335
336
# File '../../src/modules/Host.rb', line 328

def ResolveHostnameToStaticIPs
  static_ips = StaticIPs()
  if Ops.greater_than(Builtins.size(static_ips), 0)
    fqhostname = Hostname.MergeFQ(DNS.hostname, DNS.domain)
    Update(fqhostname, fqhostname, static_ips)
  end

  nil
end

- (Object) SetModified

Function sets internal variable, which indicates, that any settings were modified, to “true”



345
346
347
348
349
# File '../../src/modules/Host.rb', line 345

def SetModified
  @modified = true

  nil
end

- (Object) StaticIPs



315
316
317
318
319
320
321
322
323
324
# File '../../src/modules/Host.rb', line 315

def StaticIPs
  NetworkInterfaces.Read
  devs = NetworkInterfaces.Locate("BOOTPROTO", "static")
  devs = Builtins.filter(devs) { |dev| dev != "lo" }
  ips = Builtins.maplist(devs) do |dev|
    NetworkInterfaces.GetValue(dev, "IPADDR")
  end
  Builtins.y2milestone("ifcfgs: %1 IPs: %2", devs, ips)
  deep_copy(ips)
end

- (Object) Summary

Create summary

Returns:

  • summary text



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File '../../src/modules/Host.rb', line 298

def Summary
  summary = ""
  return Summary.NotConfigured if @hosts == {}

  summary = Summary.OpenList(summary)
  Builtins.foreach(@hosts) do |k, v|
    Builtins.foreach(v) do |hn|
      summary = Summary.AddListItem(summary, Ops.add(Ops.add(k, " - "), hn))
    end if !Builtins.contains(
      GetSystemHosts(),
      k
    )
  end
  summary = Summary.CloseList(summary)
  summary
end

- (Object) Update(oldhn, newhn, iplist)

Update hosts according to the current hostname (only one hostname, assigned to all IPs)

Parameters:

  • hostname

    current hostname

  • domain

    current domain name

  • iplist (Array<String>)

    localhost IP addresses

Returns:

  • true if success



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File '../../src/modules/Host.rb', line 240

def Update(oldhn, newhn, iplist)
  iplist = deep_copy(iplist)
  ips = Builtins.filter(iplist) { |ip| ip != "127.0.0.1" }

  Builtins.y2milestone("Hosts: %1", @hosts)
  Builtins.y2milestone(
    "Updating /etc/hosts: %1 -> %2: %3",
    oldhn,
    newhn,
    ips
  )
  @modified = true

  nick = Ops.get(Hostname.SplitFQ(newhn), 0, "")

  # Remove old hostname from hosts
  #    list oldhnlist = [];
  if !oldhn.empty?
    Builtins.foreach(@hosts) do |ip, hs|
      wrk = Builtins.maplist(hs) { |s| Builtins.splitstring(s, " ") }
      wrk = Builtins.filter(wrk) { |lst| !Builtins.contains(lst, oldhn) }
      Ops.set(@hosts, ip, Builtins.maplist(wrk) do |lst|
        Builtins.mergestring(lst, " ")
      end)
    end
  end

  # Resurect the rest of oldhnlist without old hostname
  # FIXME: maybe

  # Add localhost if missing
  if !Builtins.haskey(@hosts, "127.0.0.1")
    Ops.set(@hosts, "127.0.0.1", ["localhost"])
  end

  # Add hostname/ip for all ips
  nickadded = false
  Builtins.maplist(ips) do |ip|
    # Only add if not present yet
    #		if(haskey(hosts, ip)) return;
    # Omit some IP addresses
    next if ip == "" || ip.nil? || ip == "127.0.0.1"
    name = newhn
    # Add nick for the first one
    if !nickadded && name != ""
      nickadded = true
      name = Ops.add(Ops.add(newhn, " "), nick)
    end
    Ops.set(@hosts, ip, Builtins.add(Ops.get(@hosts, ip, []), name))
  end

  Builtins.y2milestone("Hosts: %1", @hosts)

  true
end

- (Object) Write

Write hosts settings and apply changes

Returns:

  • true if success



124
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File '../../src/modules/Host.rb', line 124

def Write
  Builtins.y2milestone("Writing hosts configuration")

  if !@modified
    Builtins.y2milestone("No changes to Host -> nothing to write")
    return true
  end

  # Check if there is anything to do
  if @hosts_init == @hosts
    Builtins.y2milestone("Hosts not modified")
    return true
  end

  steps = [_("Update /etc/hosts")]

  caption = _("Saving Hostname Configuration")
  sl = 500 # sleep for longer time, so that progress does not disappear right afterwards

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

  ProgressNextStage(_("Updating /etc/hosts ..."))

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

  ret = false
  if @hosts == {} || @hosts.nil?
    # Workaround bug [#4476]
    ret = SCR.Write(path(".target.string"), @hosts_file, "")
  else
    # Update the hosts config
    Builtins.y2milestone("hosts=%1", @hosts)
    Builtins.maplist(@hosts) do |ho, names|
      Builtins.y2milestone(
        "%1 (%2:%3)",
        ho,
        names,
        Ops.get(@hosts_init, ho)
      )
      SCR.Write(Builtins.add(path(".etc.hosts"), ho), names)
    end
    ret = true
  end

  SCR.Write(path(".etc.hosts"), nil)
  Builtins.sleep(sl)
  Progress.NextStage
  ret == true
end