Compare commits
No commits in common. "3dd6affd6fde2f0af23b35e7014ba59b6233320a" and "beb811c51d2d59fa0ec66189ec44dae066004299" have entirely different histories.
3dd6affd6f
...
beb811c51d
|
@ -1,3 +1,6 @@
|
||||||
Get() the full hostname for the OS.
|
protobuf definition files for zookeeper
|
||||||
|
|
||||||
* only cares about the hostname in DNS
|
* experimental. at this point, it's just stubbed in
|
||||||
|
* zookeeper is similar to the apache zookeeper project
|
||||||
|
* you can use it to maintain the systems in your cluster
|
||||||
|
* zookeeper works with virtigo to maintain your private cloud
|
||||||
|
|
47
common.go
47
common.go
|
@ -1,47 +0,0 @@
|
||||||
package hostname
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"net"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
var ErrorEmptyDomainName error = errors.New("domain name is empty")
|
|
||||||
var ErrorDomainNameMisconfigured error = errors.New("your OS domain name is not configured correctly")
|
|
||||||
|
|
||||||
// returns 'hostname', 'domainname'
|
|
||||||
func Split(s string) (string, string) {
|
|
||||||
parts := strings.Fields(s)
|
|
||||||
if len(parts) == 0 {
|
|
||||||
return "", ""
|
|
||||||
}
|
|
||||||
if len(parts) == 1 {
|
|
||||||
return parts[0], ""
|
|
||||||
}
|
|
||||||
return parts[0], strings.Join(parts[1:], ".")
|
|
||||||
}
|
|
||||||
|
|
||||||
// checks to make sure there is a domainname
|
|
||||||
func ValidDomainname(s string) error {
|
|
||||||
_, domainname := Split(s)
|
|
||||||
if domainname == "" {
|
|
||||||
return ErrorEmptyDomainName
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func ReverseLookupFQDN(hostname string) (string, error) {
|
|
||||||
addrs, err := net.LookupIP(hostname)
|
|
||||||
if err != nil || len(addrs) == 0 {
|
|
||||||
return hostname, fmt.Errorf("failed to lookup IP for hostname %s: %w", hostname, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Try reverse lookup on the first IP address
|
|
||||||
names, err := net.LookupAddr(addrs[0].String())
|
|
||||||
if err != nil || len(names) == 0 {
|
|
||||||
return hostname, fmt.Errorf("reverse DNS lookup failed for IP %s: %w", addrs[0].String(), err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return names[0], nil
|
|
||||||
}
|
|
6
get.go
6
get.go
|
@ -1,8 +1,8 @@
|
||||||
package hostname
|
package hostname
|
||||||
|
|
||||||
// returns the hostname
|
// functions to import and export the protobuf
|
||||||
// hostname is always set to the best effort
|
// data to and from config files
|
||||||
// error is set if hostname isn't real
|
|
||||||
func Get() (string, error) {
|
func Get() (string, error) {
|
||||||
hostname, err := osGetHostname()
|
hostname, err := osGetHostname()
|
||||||
return hostname, err
|
return hostname, err
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
package hostname
|
package hostname
|
||||||
|
|
||||||
|
// functions to import and export the protobuf
|
||||||
|
// data to and from config files
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"os"
|
||||||
"fmt"
|
|
||||||
"os/exec"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// scutil --get HostName
|
// scutil --get HostName
|
||||||
|
@ -12,17 +13,6 @@ import (
|
||||||
// scutil --set HostName my-mac.example.com
|
// scutil --set HostName my-mac.example.com
|
||||||
|
|
||||||
func osGetHostname() (string, error) {
|
func osGetHostname() (string, error) {
|
||||||
return scutil([]string{"-get", "HostName"})
|
hostname, err := os.Hostname()
|
||||||
}
|
return hostname, err
|
||||||
|
|
||||||
// getDomainName executes the 'domainname' command and returns its output.
|
|
||||||
func scutil(args []string) (string, error) {
|
|
||||||
cmd := exec.Command("scutil", args)
|
|
||||||
var out bytes.Buffer
|
|
||||||
cmd.Stdout = &out
|
|
||||||
if err := cmd.Run(); err != nil {
|
|
||||||
return "", fmt.Errorf("failed to execute 'scutil': %w", err)
|
|
||||||
}
|
|
||||||
domain := out.String()
|
|
||||||
return domain, nil
|
|
||||||
}
|
}
|
||||||
|
|
33
get_linux.go
33
get_linux.go
|
@ -4,39 +4,10 @@ package hostname
|
||||||
// data to and from config files
|
// data to and from config files
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"fmt"
|
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func osGetHostname() (string, error) {
|
func osGetHostname() (string, error) {
|
||||||
host, err := os.Hostname()
|
hostname, err := os.Hostname()
|
||||||
if err != nil {
|
return hostname, err
|
||||||
return host, fmt.Errorf("failed to get hostname: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
domain, err := getDomainName()
|
|
||||||
if err != nil || domain == "" {
|
|
||||||
return host, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return fmt.Sprintf("%s.%s", host, domain), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// getDomainName executes the 'domainname' command and returns its output.
|
|
||||||
func getDomainName() (string, error) {
|
|
||||||
cmd := exec.Command("domainname")
|
|
||||||
var out bytes.Buffer
|
|
||||||
cmd.Stdout = &out
|
|
||||||
if err := cmd.Run(); err != nil {
|
|
||||||
return "", fmt.Errorf("failed to execute 'domainname': %w", err)
|
|
||||||
}
|
|
||||||
domain := strings.TrimSpace(out.String())
|
|
||||||
if domain == "(none)" {
|
|
||||||
return "", ErrorDomainNameMisconfigured
|
|
||||||
}
|
|
||||||
|
|
||||||
return domain, nil
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue