Merge pull request #979 from openziti/framework_usage

Framework Usage (#976)
This commit is contained in:
Michael Quigley 2025-06-09 18:21:09 +00:00 committed by GitHub
commit 70e3822d7a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 91 additions and 41 deletions

View File

@ -1,5 +1,9 @@
# CHANGELOG
## v1.0.6
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)

View File

@ -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

View File

@ -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,
}
}

View File

@ -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

View File

@ -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 == "<user>@<hostname>" {
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

View File

@ -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)
}

View File

@ -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()

View File

@ -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) {

51
util/host.go Normal file
View File

@ -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 "<user>@<hostname>".
func FormatHostDetailsWithUser(username, hostname, hostDetail, description string) (formattedHostDetail, formattedDescription string) {
formattedHostDetail = fmt.Sprintf("%v; %v", username, hostDetail)
if description == "<user>@<hostname>" {
formattedDescription = fmt.Sprintf("%v@%v", username, hostname)
} else {
formattedDescription = description
}
return formattedHostDetail, formattedDescription
}