diff --git a/CHANGELOG.md b/CHANGELOG.md index b56ba6ee..ec8abdf1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ CHANGE: The `/overview` endpoint has been adjusted to include a new `remoteAgent` `boolean` on the `environment` instances, indicating whether or not the environment has an enrolled remote agent (https://github.com/openziti/zrok/issues/977) +CHANGE: Adjusted core framework entry points to support changing zrokdir, and host interrogation functions to better support embedded zrok functionality (https://github.com/openziti/zrok/issues/976) + ## v1.0.5 FEATURE: Initial support for zrok Agent remoting; new `zrok agent enroll` and `zrok agent unenroll` commands that establish opt-in remote Agent management facilities on a per-environment basis. The central API has been augmented to allow for remote control (creating shares and private access instances) of these agents; see the [remoting guide](https://docs.zrok.io/docs/guides/agent/remoting) for details (https://github.com/openziti/zrok/issues/967) diff --git a/agent/agent.go b/agent/agent.go index d6825cf9..9b747ef7 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -2,6 +2,10 @@ package agent import ( "context" + "net" + "net/http" + "os" + "github.com/grpc-ecosystem/grpc-gateway/v2/runtime" "github.com/openziti/zrok/agent/agentGrpc" "github.com/openziti/zrok/agent/agentUi" @@ -13,9 +17,6 @@ import ( "github.com/sirupsen/logrus" "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" - "net" - "net/http" - "os" ) type Agent struct { @@ -66,7 +67,9 @@ func (a *Agent) Run() error { a.agentSocket = agentSocket go a.manager() - go a.gateway() + if a.cfg.ConsoleEnabled { + go a.gateway() + } go a.remoteAgent() a.persistRegistry = false diff --git a/agent/config.go b/agent/config.go index e44e37a8..1bb379fb 100644 --- a/agent/config.go +++ b/agent/config.go @@ -4,6 +4,7 @@ type AgentConfig struct { ConsoleAddress string ConsoleStartPort uint16 ConsoleEndPort uint16 + ConsoleEnabled bool } func DefaultConfig() *AgentConfig { @@ -11,5 +12,6 @@ func DefaultConfig() *AgentConfig { ConsoleAddress: "127.0.0.1", ConsoleStartPort: 8080, ConsoleEndPort: 8181, + ConsoleEnabled: true, } } diff --git a/cmd/zrok/agentStart.go b/cmd/zrok/agentStart.go index 965199f3..0df78286 100644 --- a/cmd/zrok/agentStart.go +++ b/cmd/zrok/agentStart.go @@ -1,13 +1,14 @@ package main import ( + "os" + "os/signal" + "syscall" + "github.com/openziti/zrok/agent" "github.com/openziti/zrok/environment" "github.com/openziti/zrok/tui" "github.com/spf13/cobra" - "os" - "os/signal" - "syscall" ) func init() { @@ -54,7 +55,7 @@ func (cmd *agentStartCommand) run(_ *cobra.Command, _ []string) { tui.Error("error creating agent", err) } - c := make(chan os.Signal) + c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt, syscall.SIGTERM) go func() { <-c diff --git a/cmd/zrok/enable.go b/cmd/zrok/enable.go index f9476f9d..0cd8f814 100644 --- a/cmd/zrok/enable.go +++ b/cmd/zrok/enable.go @@ -2,6 +2,9 @@ package main import ( "fmt" + "os" + "time" + "github.com/charmbracelet/bubbles/spinner" tea "github.com/charmbracelet/bubbletea" httptransport "github.com/go-openapi/runtime/client" @@ -9,12 +12,9 @@ import ( "github.com/openziti/zrok/environment/env_core" restEnvironment "github.com/openziti/zrok/rest_client_zrok/environment" "github.com/openziti/zrok/tui" - "github.com/shirou/gopsutil/v3/host" + "github.com/openziti/zrok/util" "github.com/sirupsen/logrus" "github.com/spf13/cobra" - "os" - user2 "os/user" - "time" ) func init() { @@ -51,26 +51,12 @@ func (cmd *enableCommand) run(_ *cobra.Command, args []string) { tui.Error(fmt.Sprintf("you already have an enabled environment, %v first before you %v", tui.Code.Render("zrok disable"), tui.Code.Render("zrok enable")), nil) } - hostName, hostDetail, err := getHost() + hostName, hostDetail, username, err := util.GetHostDetails() if err != nil { panic(err) } - var username string - userObj, err := user2.Current() - if err == nil && userObj.Username != "" { - username = userObj.Username - } else { - username = os.Getenv("USER") - if username == "" { - euid := os.Geteuid() - username = fmt.Sprintf("user-%d", euid) - logrus.Warnf("unable to determine the current user, using effective UID: %v", euid) - } - } - hostDetail = fmt.Sprintf("%v; %v", username, hostDetail) - if cmd.description == "@" { - cmd.description = fmt.Sprintf("%v@%v", username, hostName) - } + hostDetail, cmd.description = util.FormatHostDetailsWithUser(username, hostName, hostDetail, cmd.description) + zrok, err := env.Client() if err != nil { cmd.endpointError(env.ApiEndpoint()) @@ -169,16 +155,6 @@ func (cmd *enableCommand) endpointError(apiEndpoint, _ string) { fmt.Printf("(where newEndpoint is something like: %v)\n\n", tui.Code.Render("https://some.zrok.io")) } -func getHost() (string, string, error) { - info, err := host.Info() - if err != nil { - return "", "", err - } - thisHost := fmt.Sprintf("%v; %v; %v; %v; %v; %v; %v", - info.Hostname, info.OS, info.Platform, info.PlatformFamily, info.PlatformVersion, info.KernelVersion, info.KernelArch) - return info.Hostname, thisHost, nil -} - type enableTuiModel struct { spinner spinner.Model msg string diff --git a/cmd/zrok/testEndpoint.go b/cmd/zrok/testEndpoint.go index 2b49b679..291ff754 100644 --- a/cmd/zrok/testEndpoint.go +++ b/cmd/zrok/testEndpoint.go @@ -15,6 +15,7 @@ import ( "github.com/openziti/sdk-golang/ziti" "github.com/openziti/zrok/cmd/zrok/endpointUi" "github.com/openziti/zrok/tui" + "github.com/openziti/zrok/util" "github.com/pkg/errors" "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -200,7 +201,7 @@ func newEndpointData(r *http.Request) *endpointData { } func (ed *endpointData) getHostInfo() { - host, hostDetail, err := getHost() + host, hostDetail, _, err := util.GetHostDetails() if err != nil { logrus.Errorf("error getting host detail: %v", err) } diff --git a/environment/api.go b/environment/api.go index 54addb7e..9bc8d263 100644 --- a/environment/api.go +++ b/environment/api.go @@ -7,6 +7,12 @@ import ( "github.com/pkg/errors" ) +// SetRootDirName allows setting a custom name for the root directory. +// This should be called before any other environment operations. +func SetRootDirName(name string) { + env_v0_4.SetRootDirName(name) +} + func LoadRoot() (env_core.Root, error) { if assert, err := env_v0_4.Assert(); assert && err == nil { return env_v0_4.Load() diff --git a/environment/env_v0_4/dirs.go b/environment/env_v0_4/dirs.go index baf23be8..e9605247 100644 --- a/environment/env_v0_4/dirs.go +++ b/environment/env_v0_4/dirs.go @@ -6,12 +6,18 @@ import ( "path/filepath" ) +var rootDirName = ".zrok" + +func SetRootDirName(name string) { + rootDirName = name +} + func rootDir() (string, error) { home, err := os.UserHomeDir() if err != nil { return "", err } - return filepath.Join(home, ".zrok"), nil + return filepath.Join(home, rootDirName), nil } func metadataFile() (string, error) { diff --git a/util/host.go b/util/host.go new file mode 100644 index 00000000..c7253b5d --- /dev/null +++ b/util/host.go @@ -0,0 +1,51 @@ +package util + +import ( + "fmt" + "github.com/shirou/gopsutil/v3/host" + "github.com/sirupsen/logrus" + "os" + "os/user" +) + +// GetHostDetails returns the hostname, detailed host information, and username. +// The detailed host information includes OS, platform, kernel details, and username. +func GetHostDetails() (hostname string, hostDetail string, username string, err error) { + info, err := host.Info() + if err != nil { + return "", "", "", err + } + hostname = info.Hostname + + userObj, err := user.Current() + if err == nil && userObj.Username != "" { + username = userObj.Username + } else { + username = os.Getenv("USER") + if username == "" { + euid := os.Geteuid() + username = fmt.Sprintf("user-%d", euid) + logrus.Warnf("unable to determine the current user, using effective UID: %v", euid) + } + } + + hostDetail = fmt.Sprintf("%v; %v; %v; %v; %v; %v; %v", + info.Hostname, info.OS, info.Platform, info.PlatformFamily, + info.PlatformVersion, info.KernelVersion, info.KernelArch) + + return hostname, hostDetail, username, nil +} + +// FormatHostDetailsWithUser combines the username and host details into a single string. +// It also returns a formatted description string if the input description is the default "@". +func FormatHostDetailsWithUser(username, hostname, hostDetail, description string) (formattedHostDetail, formattedDescription string) { + formattedHostDetail = fmt.Sprintf("%v; %v", username, hostDetail) + + if description == "@" { + formattedDescription = fmt.Sprintf("%v@%v", username, hostname) + } else { + formattedDescription = description + } + + return formattedHostDetail, formattedDescription +} \ No newline at end of file