Merge pull request #408 from sean-/solaris_zone_host_id

Add HostID support to the `solaris` build target.
This commit is contained in:
shirou 2017-08-02 23:09:19 +09:00 committed by GitHub
commit d97b1c368a
1 changed files with 45 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package host
import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"os"
@ -76,6 +77,50 @@ func Info() (*InfoStat, error) {
}
}
switch result.Platform {
case "SmartOS":
// If everything works, use the current zone ID as the HostID if present.
zonename, err := exec.LookPath("/usr/bin/zonename")
if err == nil {
out, err := invoke.Command(zonename)
if err == nil {
sc := bufio.NewScanner(bytes.NewReader(out))
for sc.Scan() {
line := sc.Text()
// If we're in the global zone, rely on the hostname.
if line == "global" {
hostname, err := os.Hostname()
if err == nil {
result.HostID = hostname
}
} else {
result.HostID = strings.TrimSpace(line)
break
}
}
}
}
}
// If HostID is still empty, use hostid(1), which can lie to callers but at
// this point there are no hardware facilities available. This behavior
// matches that of other supported OSes.
if result.HostID == "" {
hostID, err := exec.LookPath("/usr/bin/hostid")
if err == nil {
out, err := invoke.Command(hostID)
if err == nil {
sc := bufio.NewScanner(bytes.NewReader(out))
for sc.Scan() {
line := sc.Text()
result.HostID = strings.TrimSpace(line)
break
}
}
}
}
// Find the boot time and calculate uptime relative to it
bootTime, err := BootTime()
if err != nil {