From ec5a98cc8177f6b686a7b9880a87be06beac8172 Mon Sep 17 00:00:00 2001 From: Ran Joe Date: Sat, 17 Aug 2024 10:30:43 +0000 Subject: [PATCH 1/2] feat: dashboard use ip instead of localhost --- pkg/cluster/baremetal/cluster.go | 2 +- pkg/cluster/baremetal/create.go | 2 +- pkg/components/frontend.go | 46 +++++++++++++++++++++++++++----- pkg/components/utils.go | 18 +++++++++++++ 4 files changed, 59 insertions(+), 9 deletions(-) diff --git a/pkg/cluster/baremetal/cluster.go b/pkg/cluster/baremetal/cluster.go index b1098be2..607b6490 100644 --- a/pkg/cluster/baremetal/cluster.go +++ b/pkg/cluster/baremetal/cluster.go @@ -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 } diff --git a/pkg/cluster/baremetal/create.go b/pkg/cluster/baremetal/create.go index f1f39c3f..41a5b9bc 100644 --- a/pkg/cluster/baremetal/create.go +++ b/pkg/cluster/baremetal/create.go @@ -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)) diff --git a/pkg/components/frontend.go b/pkg/components/frontend.go index 39a18b4b..bed08d14 100644 --- a/pkg/components/frontend.go +++ b/pkg/components/frontend.go @@ -19,6 +19,7 @@ package components import ( "context" "fmt" + "net" "net/http" "path" "sync" @@ -30,7 +31,7 @@ import ( fileutils "github.com/GreptimeTeam/gtctl/pkg/utils/file" ) -type frontend struct { +type Frontend struct { config *config.Frontend metaSrvAddr string @@ -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, @@ -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) @@ -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 @@ -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) @@ -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)) +} diff --git a/pkg/components/utils.go b/pkg/components/utils.go index 7e632ec1..c9f729a4 100644 --- a/pkg/components/utils.go +++ b/pkg/components/utils.go @@ -19,6 +19,7 @@ package components import ( "fmt" "net" + "os" "strconv" ) @@ -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 +} From f2ad33dac2ef5664424aa2742fd79a4e8c4240d0 Mon Sep 17 00:00:00 2001 From: Ran Joe Date: Mon, 19 Aug 2024 03:53:39 +0000 Subject: [PATCH 2/2] fix: word grammar error --- pkg/components/frontend.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/components/frontend.go b/pkg/components/frontend.go index bed08d14..67f93c17 100644 --- a/pkg/components/frontend.go +++ b/pkg/components/frontend.go @@ -168,5 +168,5 @@ func (f *Frontend) PrintDashboardLog() { log = fmt.Sprintf(format, host, port) } - f.logger.V(0).Infof("If enable dashboard, view it by accessing: %s", logger.Bold(log)) + f.logger.V(0).Infof("If enabled dashboard, view it by accessing: %s", logger.Bold(log)) }