Class: Yast::HostnameClass
- Inherits:
-
Module
- Object
- Module
- Yast::HostnameClass
- Defined in:
- library/types/src/modules/Hostname.rb
Instance Method Summary collapse
-
#Check(host) ⇒ Object
Check syntax of hostname entry (that is a domain name component, unqualified, without dots).
-
#CheckDomain(domain) ⇒ Object
Check syntax of domain entry.
-
#CheckFQ(host) ⇒ Object
Check syntax of fully qualified hostname.
-
#CurrentDomain ⇒ Object
Retrieve currently set domain name.
-
#CurrentFQ ⇒ Object
Retrieve currently set fully qualified hostname (uses hostname --fqdn).
-
#CurrentHostname ⇒ Object
Retrieve currently set (short) hostname.
- #main ⇒ Object
-
#MergeFQ(hostname, domain) ⇒ Object
Merge short hostname and domain to full-qualified host name.
-
#SplitFQ(fqhostname) ⇒ Array
Split FQ hostname to hostname and domain name.
-
#ValidDomain ⇒ Object
describe a valid domain name.
-
#ValidFQ ⇒ Object
describe a valid FQ host name.
-
#ValidHost ⇒ Object
describe a valid host name.
Instance Method Details
#Check(host) ⇒ Object
Check syntax of hostname entry (that is a domain name component, unqualified, without dots)
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
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
110 111 112 |
# File 'library/types/src/modules/Hostname.rb', line 110 def CheckFQ(host) CheckDomain(host) end |
#CurrentDomain ⇒ Object
Retrieve currently set domain name
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 |
#CurrentFQ ⇒ Object
Retrieve currently set fully qualified hostname (uses hostname --fqdn)
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 |
#CurrentHostname ⇒ Object
Retrieve currently set (short) 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 |
#main ⇒ Object
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
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.
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 |
#ValidDomain ⇒ Object
describe a valid domain name
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 |
#ValidFQ ⇒ Object
describe a valid FQ host name
72 73 74 |
# File 'library/types/src/modules/Hostname.rb', line 72 def ValidFQ ValidDomain() end |
#ValidHost ⇒ Object
describe a valid host name
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 |