github.com/coreos/mantle@v0.13.0/system/hostname.go (about) 1 // Copyright 2015 CoreOS, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package system 16 17 import ( 18 "net" 19 "os" 20 "strings" 21 ) 22 23 // FullHostname is a best effort attempt to resolve the canonical FQDN of 24 // the host. On failure it will fall back to a reasonable looking default 25 // such as 'localhost.' or 'hostname.invalid.' 26 func FullHostname() string { 27 hostname, err := os.Hostname() 28 if err != nil || hostname == "localhost" || hostname == "(none)" { 29 return "localhost." 30 } 31 fullname, err := net.LookupCNAME(hostname) 32 if err != nil { 33 fullname = hostname 34 if !strings.Contains(fullname, ".") { 35 fullname += ".invalid." 36 } 37 } 38 return fullname 39 }