feat(cli): allow specifying name of provisioner daemon (#11077)

- Adds a --name argument to provisionerd start
- Plumbs through name to integrated and external provisioners
- Defaults to hostname if not specified for external, hostname-N for integrated
- Adds cliutil.Hostname
This commit is contained in:
Cian Johnston
2023-12-07 16:59:13 +00:00
committed by GitHub
parent 8aea6040c8
commit 1e349f0d50
16 changed files with 170 additions and 22 deletions
+40
View File
@@ -0,0 +1,40 @@
package cliutil
import (
"os"
"strings"
"sync"
)
var (
hostname string
hostnameOnce sync.Once
)
// Hostname returns the hostname of the machine, lowercased,
// with any trailing domain suffix stripped.
// It is cached after the first call.
// If the hostname cannot be determined, for any reason,
// localhost will be returned instead.
func Hostname() string {
hostnameOnce.Do(func() { hostname = getHostname() })
return hostname
}
func getHostname() string {
h, err := os.Hostname()
if err != nil {
// Something must be very wrong if this fails.
// We'll just return localhost and hope for the best.
return "localhost"
}
// On some platforms, the hostname can be an FQDN. We only want the hostname.
if idx := strings.Index(h, "."); idx != -1 {
h = h[:idx]
}
// For the sake of consistency, we also want to lowercase the hostname.
// Per RFC 4343, DNS lookups must be case-insensitive.
return strings.ToLower(h)
}