Class: Yast::HostClass

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

Instance Method Summary collapse

Instance Method Details

#add_name(address, name) ⇒ Object

Add another name to the list for address (which may be empty so far) FIXME: used only in one place, which looks wrong


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

def add_name(address, name)
  canonical, *aliases = name.split(" ")
  @hosts.add_entry(address, canonical, aliases)
end

#clearObject

Remove all entries from the host table.


52
53
54
55
56
57
# File 'src/modules/Host.rb', line 52

def clear
  @hosts.hosts.keys.each do |ip|
    @hosts.delete_by_ip(ip)
  end
  @configuration_imported = false
end

#ExportObject

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


166
167
168
169
170
171
172
173
174
175
# File 'src/modules/Host.rb', line 166

def Export
  exported_hosts = @hosts.hosts
  return {} if exported_hosts.empty?

  # Filter out IPs with empty hostname (so that valid autoyast
  # profile is created)(#335120)
  exported_hosts.keep_if { |_, names| !names.empty? }

  { "hosts" => exported_hosts }
end

#GetModifiedBoolean

Function which returns if the settings were modified


287
288
289
290
# File 'src/modules/Host.rb', line 287

def GetModified
  return true if @configuration_imported # hosts section has been imported.
  @initial_hosts && (@hosts.hosts != @initial_hosts.hosts)
end

#GetSystemHostsArray

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


179
180
181
182
183
184
185
186
187
188
189
# File 'src/modules/Host.rb', line 179

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

#Import(settings) ⇒ Object

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


141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'src/modules/Host.rb', line 141

def Import(settings)
  @initialized = true # don't let Read discard our data
  @configuration_imported = true if settings.key?("hosts")

  load_hosts(load_only: true)

  imported_hosts = settings.fetch("hosts", {})

  # convert from old format to the new one
  # use ::1 entry as a reference
  if (imported_hosts["::1"] || []).size > 1
    imported_hosts.each_pair do |k, v|
      imported_hosts[k] = [v.join(" ")]
    end
  end

  imported_hosts.each_pair do |ip, names|
    set_names(ip, names)
  end

  true
end

#mainObject


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'src/modules/Host.rb', line 33

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

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

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

  @initialized = false

  @hosts = CFA::Hosts.new

  @configuration_imported = false
end

#name_maphash


60
61
62
# File 'src/modules/Host.rb', line 60

def name_map
  @hosts.hosts
end

#names(address) ⇒ array


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

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

#ReadObject

Reads /etc/hosts settings

It reads /etc/hosts only when the module was not initialized already.


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

def Read
  return true if @initialized
  return false if !load_hosts

  Builtins.y2debug("hosts=#{@hosts.inspect}")

  @initialized = true
end

#remove_ip(address) ⇒ Object

remove all instances of ip in hosts table


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

def remove_ip(address)
  @hosts.delete_by_ip(address)
end

#ResolveHostnameToStaticIPsObject

Configure system to resolve static ips without hostname to system wide hostname

It is expected to be used during installation only. If user configures static ips during installation and do not assign them particular hostname, then such ips are configuret to resolve to the system wide hostname (see Hostname module, /etc/HOSTNAME)

Originally implemented as a fix for bnc#664929, later extended for bnc#1039532


270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'src/modules/Host.rb', line 270

def ResolveHostnameToStaticIPs
  Yast.import "DNS"

  # reject those static ips which have particular hostnames already configured
  static_ips = StaticIPs().reject { |sip| @hosts.include_ip?(sip) }
  return if static_ips.empty?

  fqhostname = Hostname.MergeFQ(DNS.hostname, DNS.domain)

  # assign system wide hostname to a static ip without particular hostname
  static_ips.each { |sip| Update(fqhostname, fqhostname, sip) }

  nil
end

#StaticIPsArray<string>

Creates a list os static ips present in the system


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

def StaticIPs
  NetworkInterfaces.Read
  devs = NetworkInterfaces.Locate("BOOTPROTO", "static")

  devs.reject! { |dev| dev == "lo" }
  static_ips = devs.map { |dev| NetworkInterfaces.GetValue(dev, "IPADDR") }
  static_ips.reject! { |ip| ip.nil? || ip.empty? }

  log.info("StaticIPs: found in ifcfgs: #{devs} IPs list: #{static_ips}")

  static_ips
end

#SummaryObject

Create summary


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

def Summary
  return Summary.NotConfigured if @hosts.hosts.empty?

  summary = Summary.OpenList("")
  @hosts.hosts.each do |k, v|
    next if GetSystemHosts().include?(k)
    # currently all names are placed as a one string in first array item
    summary = Summary.AddListItem(summary, "#{k} - #{v.first}")
  end

  Summary.CloseList(summary)
end

#Update(oldhn, newhn, ip) ⇒ Boolean

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

Raises:

  • (ArgumentError)

197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'src/modules/Host.rb', line 197

def Update(oldhn, newhn, ip)
  raise ArgumentError, "IP cannot be nil" if ip.nil?
  raise ArgumentError, "Nonempty IP expected" if ip.empty?

  log.info("Updating /etc/hosts: #{oldhn} -> #{newhn}: #{ip}")

  # Remove old hostname from hosts
  @hosts.delete_hostname(oldhn) if !oldhn.empty?

  # Add localhost if missing
  if @hosts.host("127.0.0.1").empty?
    @hosts.add_entry("127.0.0.1", "localhost")
  end

  # Omit some IP addresses
  return true if ["127.0.0.1", "", nil].include?(ip)
  # Omit invalid newhn
  return true if [nil, ""].include?(newhn)

  nick = Hostname.SplitFQ(newhn)[0] || ""
  nick = nick.empty? || nick == newhn ? [] : [nick]
  hosts = @hosts.host(ip)
  if hosts.empty?
    @hosts.add_entry(ip, newhn, nick)
  else
    canonical, *aliases = hosts.last.split(" ")
    aliases << newhn
    aliases.concat(nick)
    @hosts.set_entry(ip, canonical, aliases)
  end

  true
end

#Write(gui: false) ⇒ Object

Write hosts settings and apply changes


97
98
99
100
101
102
103
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
131
132
# File 'src/modules/Host.rb', line 97

def Write(gui: false)
  log.info("Writing hosts configuration")

  # Check if there is anything to do
  if !GetModified()
    log.info("No changes to Host -> nothing to write")
    return true
  end

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

    caption = _("Saving Hostname Configuration")

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

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

  # backup if exists
  if SCR.Read(path(".target.size"), CFA::Hosts::PATH) >= 0
    Yast::Execute.on_target("cp", CFA::Hosts::PATH, "#{CFA::Hosts::PATH}.YaST2save")
  end

  @hosts.save

  # Reset that the configuration has been taken from AY file because the settings
  # are now on the target system.
  @configuration_imported = false
  # Syncing after the settings have been written.
  @initial_hosts = @hosts.clone

  Progress.NextStage if gui

  true
end