Class: Yast::HostnameClass

Inherits:
Module
  • Object
show all
Defined in:
library/types/src/modules/Hostname.rb

Instance Method Summary collapse

Instance Method Details

#Check(host) ⇒ Object

Check syntax of hostname entry (that is a domain name component, unqualified, without dots)

Parameters:

Returns:

  • true if correct

See Also:

  • rfc2396 and obsoleted rfc1034


81
82
83
84
85
# File 'library/types/src/modules/Hostname.rb', line 81

def Check(host)
  return false if host.nil? || host == "" || Ops.greater_than(Builtins.size(host), 63)

  Builtins.regexpmatch(host, "^[[:alnum:]]([[:alnum:]-]*[[:alnum:]])?$")
end

#CheckDomain(domain) ⇒ Object

Check syntax of domain entry

Parameters:

  • domain (String)

    domain name

Returns:

  • true if correct



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'library/types/src/modules/Hostname.rb', line 90

def CheckDomain(domain)
  return false if domain.nil? || domain == ""

  # if "domain" contains "." character as last character remove it before validation (but it's valid)
  if Ops.greater_than(Builtins.size(domain), 1) && (Builtins.substring(domain, Ops.subtract(Builtins.size(domain), 1), 1) == ".")
    domain = Builtins.substring(
      domain,
      0,
      Ops.subtract(Builtins.size(domain), 1)
    )
  end
  l = Builtins.splitstring(domain, ".")
  return false if Builtins.contains(Builtins.maplist(l) { |h| Check(h) }, false)

  !Builtins.regexpmatch(domain, "\\.[[:digit:]][^.]*$")
end

#CheckFQ(host) ⇒ Object

Check syntax of fully qualified hostname

Parameters:

Returns:

  • true if correct



110
111
112
# File 'library/types/src/modules/Hostname.rb', line 110

def CheckFQ(host)
  CheckDomain(host)
end

#CurrentDomainObject

Retrieve currently set domain name

Returns:

  • domain



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'library/types/src/modules/Hostname.rb', line 195

def CurrentDomain
  domain = ""
  fqhostname = CurrentFQ()

  # the same as above, if FQDN is IP address
  # let's claim domainname as empty (#415109)
  if !IP.Check(fqhostname)
    data = SplitFQ(fqhostname)

    domain = Ops.get(data, 1, "") if data != [] && Ops.greater_than(Builtins.size(data), 1)
  end

  Builtins.y2debug("Current domainname: %1", domain)
  domain
end

#CurrentFQObject

Retrieve currently set fully qualified hostname (uses hostname --fqdn)

Returns:

  • FQ hostname



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'library/types/src/modules/Hostname.rb', line 155

def CurrentFQ
  hostname_data = SCR.Execute(path(".target.bash_output"), "/usr/bin/hostname --fqdn")

  if hostname_data["exit"] == 0
    fqhostname = hostname_data["stdout"]
  else
    Builtins.y2warning("Using fallback hostname")

    fqhostname = SCR.Read(path(".target.string"), "/etc/hostname") || ""
    fqhostname = "linux.#{@DefaultDomain}" if fqhostname.empty?
  end

  fqhostname = String.FirstChunk(fqhostname, "\n")

  Builtins.y2milestone("Current FQDN: %1", fqhostname)
  fqhostname
end

#CurrentHostnameObject

Retrieve currently set (short) hostname

Returns:

  • hostname



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'library/types/src/modules/Hostname.rb', line 175

def CurrentHostname
  hostname = ""
  fqhostname = CurrentFQ()

  # current FQDN is IP address - it happens, esp. in inst-sys :)
  # so let's not cut it into pieces (#415109)
  if IP.Check(fqhostname)
    hostname = fqhostname
  else
    data = SplitFQ(fqhostname)

    hostname = Ops.get(data, 0, "") if data != []

    Builtins.y2debug("Current hostname: %1", hostname)
  end
  hostname
end

#mainObject



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

def main
  textdomain "base"

  Yast.import "IP"
  Yast.import "String"
  Yast.import "FileUtils"
  Yast.import "Stage"

  # i18n characters in domain names are still not allowed
  #
  # @note This is an unstable API function and may change in the future
  @ValidChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-"
  @ValidCharsDomain = Ops.add(@ValidChars, ".")
  @ValidCharsFQ = @ValidCharsDomain
  @DefaultDomain = ""
end

#MergeFQ(hostname, domain) ⇒ Object

Merge short hostname and domain to full-qualified host name

Parameters:

  • hostname (String)

    short host name

  • domain (String)

    domain name

Returns:

  • FQ hostname



146
147
148
149
150
# File 'library/types/src/modules/Hostname.rb', line 146

def MergeFQ(hostname, domain)
  return hostname if domain == "" || domain.nil?

  Ops.add(Ops.add(hostname, "."), domain)
end

#SplitFQ(fqhostname) ⇒ Array

Split FQ hostname to hostname and domain name

If domain is not defined, returns empty string.

Examples:

Hostname::SplitFQ("ftp.suse.cz") -> ["ftp", "suse.cz"]

Hostname::SplitFQ("ftp") -> ["ftp", ""]

Parameters:

  • fqhostname (String)

    FQ hostname

Returns:

  • (Array)

    of hostname and domain name or empty in case of error



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'library/types/src/modules/Hostname.rb', line 122

def SplitFQ(fqhostname)
  if fqhostname == "" || fqhostname.nil?
    Builtins.y2error("Bad FQ hostname: %1", fqhostname)
    return []
  end

  hn = ""
  dn = ""

  dot = Builtins.findfirstof(fqhostname, ".")
  if dot.nil?
    hn = fqhostname
  else
    hn = Builtins.substring(fqhostname, 0, dot)
    dn = Builtins.substring(fqhostname, Ops.add(dot, 1))
  end

  [hn, dn]
end

#ValidDomainObject

describe a valid domain name

Returns:

  • description



52
53
54
55
56
57
58
59
# File 'library/types/src/modules/Hostname.rb', line 52

def ValidDomain
  # Translators: dot: ".", hyphen: "-"
  _(
    "A valid domain name consists of components separated by dots.\n" \
    "Each component contains letters, digits, and hyphens. A hyphen may not\n" \
    "start or end a component and the last component may not begin with a digit."
  )
end

#ValidFQObject

describe a valid FQ host name

Returns:

  • describe a valid FQ host name



72
73
74
# File 'library/types/src/modules/Hostname.rb', line 72

def ValidFQ
  ValidDomain()
end

#ValidHostObject

describe a valid host name

Returns:

  • description



63
64
65
66
67
68
# File 'library/types/src/modules/Hostname.rb', line 63

def ValidHost
  # Translators: hyphen: "-"
  _(
    "A valid host name consists of letters, digits, and hyphens.\nA host name may not begin or end with a hyphen.\n"
  )
end