Class: Yast::HostClass

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

Instance Method Summary (collapse)

Instance Method Details

- (Object) add_name(address, name)

Add another name to the list for address (which may be empty so far)



86
87
88
89
90
91
# File '../../src/modules/Host.rb', line 86

def add_name(address, name)
  @hosts[address] ||= []
  @hosts[address] << name

  @modified = true
end

- (Object) clear

Remove all entries from the host table.



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

def clear
  @hosts = {}
  @modified = true
end

- (Object) EnsureHostnameResolvable



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

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



236
237
238
239
240
241
242
243
244
245
# File '../../src/modules/Host.rb', line 236

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



367
368
369
# File '../../src/modules/Host.rb', line 367

def GetModified
  @modified
end

- (Array) GetSystemHosts

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

Returns:

  • (Array)

    of system hosts



249
250
251
252
253
254
255
256
257
258
259
# File '../../src/modules/Host.rb', line 249

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



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

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
# 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"

  @initialized = false
end

- (hash) name_map

Returns address->list of names

Returns:

  • (hash)

    address->list of names



70
71
72
# File '../../src/modules/Host.rb', line 70

def name_map
  @hosts
end

- (array) names(address)

Returns names for that address

Returns:

  • (array)

    names for that address



75
76
77
# File '../../src/modules/Host.rb', line 75

def names(address)
  @hosts[address] || []
end

- (Object) NeedDummyIP



93
94
95
# File '../../src/modules/Host.rb', line 93

def NeedDummyIP
  DNS.write_hostname
end

- (Object) Read

Read hosts settings

Returns:

  • true if success



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File '../../src/modules/Host.rb', line 123

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



355
356
357
358
359
360
361
362
363
# File '../../src/modules/Host.rb', line 355

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) set_names(address, names)

Give address a new list of names.



80
81
82
83
# File '../../src/modules/Host.rb', line 80

def set_names(address, names)
  @hosts[address] = names
  @modified = true
end

- (Object) SetModified

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



373
374
375
376
377
# File '../../src/modules/Host.rb', line 373

def SetModified
  @modified = true

  nil
end

- (Object) StaticIPs



342
343
344
345
346
347
348
349
350
351
# File '../../src/modules/Host.rb', line 342

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



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File '../../src/modules/Host.rb', line 325

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



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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File '../../src/modules/Host.rb', line 267

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



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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File '../../src/modules/Host.rb', line 151

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