Class: Y2Network::Wicked::HostnameReader

Inherits:
Object
  • Object
show all
Includes:
Yast::Logger, Yast::Wicked
Defined in:
src/lib/y2network/wicked/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"

Constant Summary

Constants included from Yast::Wicked

Yast::Wicked::BASH_OUTPUT_PATH, Yast::Wicked::BASH_PATH, Yast::Wicked::IBFT_CMD

Instance Method Summary collapse

Methods included from Yast::Wicked

#ibft_interfaces, #parse_hostname, #parse_ntp_servers, #query_wicked, #reload_config

Instance Method Details

#configY2Network::Hostname

Return configuration from sysconfig files

Returns:


46
47
48
49
50
51
52
53
# File 'src/lib/y2network/wicked/hostname_reader.rb', line 46

def config
  Y2Network::Hostname.new(
    installer:     hostname_from_install_inf,
    static:        static_hostname,
    transient:     hostname_from_system,
    dhcp_hostname: dhcp_hostname
  )
end

#dhcp_hostnameString, ...

Returns whether the hostname should be taken from DHCP

Returns:

  • (String, :any, :none)

    Interface to set the hostname based on DHCP settings; :any for any interface; :none for ignoring the hostname assigned via DHCP


59
60
61
62
63
64
65
66
67
68
69
# File 'src/lib/y2network/wicked/hostname_reader.rb', line 59

def dhcp_hostname
  value = Yast::SCR.Read(Yast::Path.new(".sysconfig.network.dhcp.DHCLIENT_SET_HOSTNAME"))
  return :any if value == "yes"

  files = CFA::InterfaceFile.all
  file = files.find do |f|
    f.load
    f.dhclient_set_hostname == "yes"
  end
  file ? file.interface : :none
end

#hostname_from_dhcpString?

Queries for hostname obtained as part of dhcp configuration

Returns:

  • (String, nil)

    Hostname


123
124
125
126
127
128
129
130
131
132
133
# File 'src/lib/y2network/wicked/hostname_reader.rb', line 123

def hostname_from_dhcp
  # We currently cannot use connections for getting only dhcp aware configurations here
  # bcs this can be called during Y2Network::Config initialization and this is
  # acceptable replacement for this case.
  ifaces = CFA::InterfaceFile.all.map(&:interface)
  dhcp_hostname = ifaces.map { |i| parse_hostname(i) }.compact.first

  log.info("Hostname obtained from DHCP: #{dhcp_hostname}")

  dhcp_hostname
end

#hostname_from_install_infString?

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

Returns:

  • (String, nil)

    Hostname


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'src/lib/y2network/wicked/hostname_reader.rb', line 74

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)
  return nil if host.nil? || host.empty?

  host
end

#hostname_from_resolverString?

Reads the (transient) hostname known to the resolver

Returns:

  • (String, nil)

    Hostname or nil if transient hostname is not known


97
98
99
100
101
# File 'src/lib/y2network/wicked/hostname_reader.rb', line 97

def hostname_from_resolver
  Yast::Execute.on_target!("/usr/bin/hostname", "--fqdn", stdout: :capture).strip
rescue Cheetah::ExecutionFailed
  nil
end

#hostname_from_systemString?

Reads the transient hostname or system (local) hostname

Returns:

  • (String, nil)

    Hostname


106
107
108
109
110
# File 'src/lib/y2network/wicked/hostname_reader.rb', line 106

def hostname_from_system
  Yast::Execute.on_target!("/usr/bin/hostname", stdout: :capture).strip
rescue Cheetah::ExecutionFailed
  static_hostname
end

#static_hostnameString?

Reads the static hostname from /etc/hostname

Returns:

  • (String, nil)

115
116
117
118
# File 'src/lib/y2network/wicked/hostname_reader.rb', line 115

def static_hostname
  name = Yast::SCR.Read(Yast::Path.new(".target.string"), "/etc/hostname").to_s.strip
  name.empty? ? nil : name
end