Class: Y2Network::HostnameReader

Inherits:
Object
  • Object
show all
Includes:
Yast::Logger
Defined in:
src/lib/y2network/hostname_reader.rb

Overview

This class is responsible for reading the hostname

Depending on different circunstamces, the hostname can be read from different places (from a simple call to /usr/bin/hostname to the /etc/install.inf during installation).

Examples:

Read hostname

Y2Network::HostnameReader.new.hostname #=> "foo"

Instance Method Summary collapse

Instance Method Details

#hostnameString

Note:

If the hostname cannot be determined, generate a random one.

Returns the hostname


44
45
46
47
48
49
50
# File 'src/lib/y2network/hostname_reader.rb', line 44

def hostname
  if (Yast::Mode.installation || Yast::Mode.autoinst) && Yast::FileUtils.Exists("/etc/install.inf")
    fqdn = hostname_from_install_inf
  end

  fqdn || hostname_from_system || random_hostname
end

#hostname_from_install_infString?

Reads the hostname from the /etc/install.inf file


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'src/lib/y2network/hostname_reader.rb', line 55

def hostname_from_install_inf
  install_inf_hostname = Yast::SCR.Read(Yast::Path.new(".etc.install_inf.Hostname")) || ""
  log.info("Got #{install_inf_hostname} from install.inf")

  return nil if install_inf_hostname.empty?

  # if the name is actually IP, try to resolve it (bnc#556613, bnc#435649)
  if Yast::IP.Check(install_inf_hostname)
    fqdn = Yast::NetHwDetection.ResolveIP(install_inf_hostname)
    log.info("Got #{fqdn} after resolving IP from install.inf")
  else
    fqdn = install_inf_hostname
  end

  host, _domain = *Yast::Hostname.SplitFQ(fqdn)
  host.empty? ? nil : host
end

#hostname_from_systemString

Reads the hostname from the underlying system


76
77
78
79
80
81
# File 'src/lib/y2network/hostname_reader.rb', line 76

def hostname_from_system
  Yast::Execute.on_target!("/usr/bin/hostname", "--fqdn", stdout: :capture).strip
rescue Cheetah::ExecutionFailed
  name = Yast::SCR.Read(Yast::Path.new(".target.string"), "/etc/hostname").to_s.strip
  name.empty? ? nil : name
end

#random_hostnameString

Returns a random hostname

The idea is to use a name like this as fallback.


92
93
94
95
# File 'src/lib/y2network/hostname_reader.rb', line 92

def random_hostname
  suffix = HOSTNAME_CHARS.sample(4).join
  "linux-#{suffix}"
end