Class: Yast::HostnameClass

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

Instance Method Summary (collapse)

Instance Method Details

- (Object) Check(host)

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


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

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

- (Object) CheckDomain(domain)

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 '../../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)
    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

- (Object) CheckFQ(host)

Check syntax of fully qualified hostname

Parameters:

Returns:

  • true if correct



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

def CheckFQ(host)
  CheckDomain(host)
end

- (Object) CurrentDomain

Retrieve currently set domain name

Returns:

  • domain



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File '../../src/modules/Hostname.rb', line 203

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

- (Object) CurrentFQ

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

Returns:

  • FQ hostname



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File '../../src/modules/Hostname.rb', line 153

def CurrentFQ
  fqhostname = ""

  hostname_data = Convert.to_map(
    SCR.Execute(path(".target.bash_output"), "hostname --fqdn")
  )
  if hostname_data.nil? || Ops.get_integer(hostname_data, "exit", -1) != 0
    fqhostname = if SCR.Read(path(".target.stat"), "/etc/HOSTNAME").empty?
                   SCR.Read(path(".target.string"), "/etc/HOSTNAME")
                 else
                   ""
                 end

    if fqhostname == "" || fqhostname.nil?
      # last resort (#429792)
      fqhostname = "linux.site"
    end
    Builtins.y2warning("Using fallback hostname %1", fqhostname)
  else
    fqhostname = Ops.get_string(hostname_data, "stdout", "")
  end

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

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

- (Object) CurrentHostname

Retrieve currently set (short) hostname

Returns:

  • hostname



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File '../../src/modules/Hostname.rb', line 183

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

- (Object) main



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

def main
  textdomain "base"

  Yast.import "IP"
  Yast.import "String"

  # 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
end

- (Object) MergeFQ(hostname, domain)

Merge short hostname and domain to full-qualified host name

Parameters:

  • hostname (String)

    short host name

  • domain (String)

    domain name

Returns:

  • FQ hostname



145
146
147
148
# File '../../src/modules/Hostname.rb', line 145

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

- (Array) SplitFQ(fqhostname)

Split FQ hostname to hostname and domain name

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



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

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))
    return [hn, dn]
  else
    hn = fqhostname
    return [hn]
  end

  [hn, dn]
end

- (Object) ValidDomain

describe a valid domain name

Returns:

  • description



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

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

- (Object) ValidFQ

describe a valid FQ host name

Returns:

  • describe a valid FQ host name



71
72
73
# File '../../src/modules/Hostname.rb', line 71

def ValidFQ
  ValidDomain()
end

- (Object) ValidHost

describe a valid host name

Returns:

  • description



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

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