-load("@rules_go//go:def.bzl", "go_library")
+load("@rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "hostname",
],
importpath = "go.fuhry.dev/runtime/utils/hostname",
visibility = ["//visibility:public"],
- deps = select({
+ deps = [
+ "//utils/log",
+ ] + select({
"@rules_go//go/platform:aix": [
"//constants",
],
"@rules_go//go/platform:android": [
+ "//constants",
"//utils/fsutil",
- "//utils/log",
],
"@rules_go//go/platform:darwin": [
"//constants",
- "//utils/log",
"@net_howett_plist//:plist",
],
"@rules_go//go/platform:dragonfly": [
],
"@rules_go//go/platform:ios": [
"//constants",
- "//utils/log",
"@net_howett_plist//:plist",
],
"@rules_go//go/platform:js": [
"//constants",
],
"@rules_go//go/platform:linux": [
+ "//constants",
"//utils/fsutil",
- "//utils/log",
],
"@rules_go//go/platform:netbsd": [
"//constants",
],
"@rules_go//go/platform:openbsd": [
"//constants",
- "//utils/log",
],
"@rules_go//go/platform:osx": [
"//constants",
"//conditions:default": [],
}),
)
+
+go_test(
+ name = "hostname_test",
+ srcs = ["hostname_test.go"],
+ embed = [":hostname"],
+ deps = ["@com_github_stretchr_testify//assert"],
+)
package hostname
+import (
+ "errors"
+ "os"
+ "regexp"
+ "runtime"
+ "strings"
+ "sync"
+
+ "go.fuhry.dev/runtime/utils/log"
+)
+
type ContainerType uint
+type CgroupInfo struct {
+ HierarchyID uint
+ ControllerList []string
+ Path string
+}
+
const (
ContainerUnknown ContainerType = iota
ContainerNone
ContainerKubernetes
)
+var (
+ ValidHostname = regexp.MustCompile(`^[A-Za-z0-9-]{1,64}$`)
+ ValidDomainName = regexp.MustCompile(`^[A-Za-z0-9-]{1,64}(\.[A-Za-z0-9-]{1,64}){0,5}$`)
+
+ ErrCgroupsUnsupported = errors.New("cgroups are unsupported on "+runtime.GOOS)
+)
+
func (c ContainerType) String() string {
switch c {
case ContainerNone:
}
return "unknown"
}
+
+var (
+ isUserMachine bool
+ isUserMachineOnce sync.Once
+)
+
+func IsLikelyUserMachine() bool {
+ isUserMachineOnce.Do(func() {
+ if cgroup, err := CgroupInfoSelf(); err == nil {
+ log.Default().V(1).Debugf("cgroup for current process: %v", cgroup)
+ } else {
+ log.Default().V(1).Debugf("error getting cgroup info: %v", err)
+ }
+
+ isUserMachine = Containerization() == ContainerNone &&
+ os.Geteuid() >= 500 &&
+ (strings.HasPrefix(os.Getenv("HOME"), "/home/") ||
+ strings.HasPrefix(os.Getenv("HOME"), "/Users/") ||
+ runtime.GOOS == "win32" ||
+ runtime.GOOS == "darwin")
+
+ if isUserMachine {
+ log.Default().Info("Detected human-user environment")
+ } else {
+ log.Default().Info("Detected service/daemon environment")
+ }
+ })
+
+ return isUserMachine
+}
func Containerization() ContainerType {
return ContainerNone
}
+
+func CgroupInfoSelf() (*CgroupInfo, error) {
+ return nil, ErrCgroupsUnsupported
+}
"sync"
"syscall"
+ "go.fuhry.dev/runtime/constants"
"go.fuhry.dev/runtime/utils/fsutil"
"go.fuhry.dev/runtime/utils/log"
)
int8 | uint8
}
-var utsname syscall.Utsname
-var utsnameOnce sync.Once
-var spaceExp = regexp.MustCompile(`\s+`)
-var hashCommentExp = regexp.MustCompile(`#.*$`)
+var (
+ utsname syscall.Utsname
+ utsnameOnce sync.Once
+ spaceExp = regexp.MustCompile(`\s+`)
+ hashCommentExp = regexp.MustCompile(`#.*$`)
+)
func Hostname() string {
return strings.Split(nodeName(), ".")[0]
if d := domainNameFromResolvConf(); d != "" {
return d
}
+ } else if c == ContainerDocker {
+ return constants.DefaultHostDomain
}
err := fmt.Errorf(
return strings.Join([]string{Hostname(), DomainName()}, ".")
}
+var (
+ containerType ContainerType
+ containerTypeOnce sync.Once
+)
+
func Containerization() ContainerType {
- c := containerization()
- log.Default().Infof("Detected container type: %s", c)
+ containerTypeOnce.Do(func() {
+ containerType = containerization()
+ log.Default().Infof("Detected container type: %s", containerType)
+ })
- return c
+ return containerType
}
func containerization() ContainerType {
return string(bytes)
}
+
+func CgroupInfoSelf() (*CgroupInfo, error) {
+ return nil, ErrCgroupsUnsupported
+}
return "", errors.New("failed to lookup any hostname for 127.0.0.1 that does not resolve to just \"localhost\"")
}
+
+func CgroupInfoSelf() (*CgroupInfo, error) {
+ return nil, ErrCgroupsUnsupported
+}
return "", errors.New("failed to lookup any hostname for 127.0.0.1 that does not resolve to just \"localhost\"")
}
+
+func CgroupInfoSelf() (*CgroupInfo, error) {
+ return nil, ErrCgroupsUnsupported
+}