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


83
84
85
86
87
88
# File 'library/types/src/modules/Hostname.rb', line 83

def Check(host)
  if host.nil? || host == "" || Ops.greater_than(Builtins.size(host), 63)
    return false
  end
  Builtins.regexpmatch(host, "^[[:alnum:]]([[:alnum:]-]*[[:alnum:]])?$")
end

#CheckDomain(domain) ⇒ Object

Check syntax of domain entry

Parameters:

  • domain (String)

    domain name

Returns:

  • true if correct



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

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)
    if Builtins.substring(domain, Ops.subtract(Builtins.size(domain), 1), 1) == "."
      domain = Builtins.substring(
        domain,
        0,
        Ops.subtract(Builtins.size(domain), 1)
      )
    end
  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



113
114
115
# File 'library/types/src/modules/Hostname.rb', line 113

def CheckFQ(host)
  CheckDomain(host)
end

#CurrentDomainObject

Retrieve currently set domain name

Returns:

  • domain



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

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)

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

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

#CurrentFQObject

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

Returns:

  • FQ hostname



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

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

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

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

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

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

#CurrentHostnameObject

Retrieve currently set (short) hostname

Returns:

  • hostname



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

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



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

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



149
150
151
152
# File 'library/types/src/modules/Hostname.rb', line 149

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



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

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 = Builtins.substring(fqhostname, 0, dot)
    dn = Builtins.substring(fqhostname, Ops.add(dot, 1))
  else
    hn = fqhostname
  end

  [hn, dn]
end

#ValidDomainObject

describe a valid domain name

Returns:

  • description



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

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



74
75
76
# File 'library/types/src/modules/Hostname.rb', line 74

def ValidFQ
  ValidDomain()
end

#ValidHostObject

describe a valid host name

Returns:

  • description



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

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