Skip to content

Commit

Permalink
feat: dashboard use ip instead of localhost
Browse files Browse the repository at this point in the history
  • Loading branch information
leaf-potato committed Aug 17, 2024
1 parent ff41601 commit 1480127
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pkg/cluster/baremetal/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type Cluster struct {
type ClusterComponents struct {
MetaSrv components.ClusterComponent
Datanode components.ClusterComponent
Frontend components.ClusterComponent
Frontend components.Frontend
Etcd components.ClusterComponent
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/cluster/baremetal/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func (c *Cluster) Wait(ctx context.Context, close bool) error {
csd := c.mm.GetClusterScopeDirs()
if !close {
c.logger.V(0).Infof("The cluster(pid=%d, version=%s) is running in bare-metal mode now...", os.Getpid(), v)
c.logger.V(0).Infof("To view dashboard by accessing: %s", logger.Bold("http://localhost:4000/dashboard/"))
c.cc.Frontend.PrintDashboardLog()
} else {
c.logger.Warnf("The cluster(pid=%d, version=%s) run in bare-metal has been shutting down...", os.Getpid(), v)
c.logger.Warnf("To view the failure by browsing logs in: %s", logger.Bold(csd.LogsDir))
Expand Down
46 changes: 39 additions & 7 deletions pkg/components/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package components
import (
"context"
"fmt"
"net"
"net/http"
"path"
"sync"
Expand All @@ -30,7 +31,7 @@ import (
fileutils "github.com/GreptimeTeam/gtctl/pkg/utils/file"
)

type frontend struct {
type Frontend struct {
config *config.Frontend
metaSrvAddr string

Expand All @@ -42,8 +43,8 @@ type frontend struct {
}

func NewFrontend(config *config.Frontend, metaSrvAddr string, workingDirs WorkingDirs,
wg *sync.WaitGroup, logger logger.Logger) ClusterComponent {
return &frontend{
wg *sync.WaitGroup, logger logger.Logger) Frontend {
return Frontend{
config: config,
metaSrvAddr: metaSrvAddr,
workingDirs: workingDirs,
Expand All @@ -52,11 +53,11 @@ func NewFrontend(config *config.Frontend, metaSrvAddr string, workingDirs Workin
}
}

func (f *frontend) Name() string {
func (f *Frontend) Name() string {
return string(greptimedbclusterv1alpha1.FrontendComponentKind)
}

func (f *frontend) Start(ctx context.Context, stop context.CancelFunc, binary string) error {
func (f *Frontend) Start(ctx context.Context, stop context.CancelFunc, binary string) error {
for i := 0; i < f.config.Replicas; i++ {
dirName := fmt.Sprintf("%s.%d", f.Name(), i)

Expand Down Expand Up @@ -87,7 +88,7 @@ func (f *frontend) Start(ctx context.Context, stop context.CancelFunc, binary st
return nil
}

func (f *frontend) BuildArgs(params ...interface{}) []string {
func (f *Frontend) BuildArgs(params ...interface{}) []string {
logLevel := f.config.LogLevel
if logLevel == "" {
logLevel = DefaultLogLevel
Expand Down Expand Up @@ -115,7 +116,7 @@ func (f *frontend) BuildArgs(params ...interface{}) []string {
return args
}

func (f *frontend) IsRunning(_ context.Context) bool {
func (f *Frontend) IsRunning(_ context.Context) bool {
for i := 0; i < f.config.Replicas; i++ {
addr := FormatAddrArg(f.config.HTTPAddr, i)
healthy := fmt.Sprintf("http://%s/health", addr)
Expand All @@ -138,3 +139,34 @@ func (f *frontend) IsRunning(_ context.Context) bool {
}
return true
}

func (f *Frontend) PrintDashboardLog() {
format := "http://%s:%s/dashboard/"

// Split addr to host:ip
http_addr := f.config.HTTPAddr
host, port, err := net.SplitHostPort(http_addr)
if len(http_addr) > 0 && err != nil {
f.logger.Warnf("%s use incorrect http_addr: %s err: %s", f.Name(), http_addr, err)
return
}

var log string
if len(http_addr) == 0 || host == "0.0.0.0" {
ip, err := HostnameIP()

if err != nil {
f.logger.Warnf("%s hostname local resolution failed: %s", f.Name(), err)
return
}
if len(port) == 0 {
port = "4000"
}

log = fmt.Sprintf(format, ip, port)
} else {
log = fmt.Sprintf(format, host, port)
}

f.logger.V(0).Infof("If enable dashboard, view it by accessing: %s", logger.Bold(log))
}
18 changes: 18 additions & 0 deletions pkg/components/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package components
import (
"fmt"
"net"
"os"
"strconv"
)

Expand Down Expand Up @@ -48,3 +49,20 @@ func GenerateAddrArg(config string, addr string, nodeId int, args []string) []st

return append(args, fmt.Sprintf("%s=%s", config, socketAddr))
}

func HostnameIP() (ip string, err error) {
hostname, err := os.Hostname()
if err != nil {
return "", err
}

ips, err := net.LookupHost(hostname)
if err != nil {
return "", err
}

if len(ips) == 0 {
return "", nil
}
return ips[0], nil
}

0 comments on commit 1480127

Please sign in to comment.