Skip to content

Commit 1480127

Browse files
committed
feat: dashboard use ip instead of localhost
1 parent ff41601 commit 1480127

File tree

4 files changed

+59
-9
lines changed

4 files changed

+59
-9
lines changed

pkg/cluster/baremetal/cluster.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ type Cluster struct {
5050
type ClusterComponents struct {
5151
MetaSrv components.ClusterComponent
5252
Datanode components.ClusterComponent
53-
Frontend components.ClusterComponent
53+
Frontend components.Frontend
5454
Etcd components.ClusterComponent
5555
}
5656

pkg/cluster/baremetal/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ func (c *Cluster) Wait(ctx context.Context, close bool) error {
236236
csd := c.mm.GetClusterScopeDirs()
237237
if !close {
238238
c.logger.V(0).Infof("The cluster(pid=%d, version=%s) is running in bare-metal mode now...", os.Getpid(), v)
239-
c.logger.V(0).Infof("To view dashboard by accessing: %s", logger.Bold("http://localhost:4000/dashboard/"))
239+
c.cc.Frontend.PrintDashboardLog()
240240
} else {
241241
c.logger.Warnf("The cluster(pid=%d, version=%s) run in bare-metal has been shutting down...", os.Getpid(), v)
242242
c.logger.Warnf("To view the failure by browsing logs in: %s", logger.Bold(csd.LogsDir))

pkg/components/frontend.go

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package components
1919
import (
2020
"context"
2121
"fmt"
22+
"net"
2223
"net/http"
2324
"path"
2425
"sync"
@@ -30,7 +31,7 @@ import (
3031
fileutils "github.com/GreptimeTeam/gtctl/pkg/utils/file"
3132
)
3233

33-
type frontend struct {
34+
type Frontend struct {
3435
config *config.Frontend
3536
metaSrvAddr string
3637

@@ -42,8 +43,8 @@ type frontend struct {
4243
}
4344

4445
func NewFrontend(config *config.Frontend, metaSrvAddr string, workingDirs WorkingDirs,
45-
wg *sync.WaitGroup, logger logger.Logger) ClusterComponent {
46-
return &frontend{
46+
wg *sync.WaitGroup, logger logger.Logger) Frontend {
47+
return Frontend{
4748
config: config,
4849
metaSrvAddr: metaSrvAddr,
4950
workingDirs: workingDirs,
@@ -52,11 +53,11 @@ func NewFrontend(config *config.Frontend, metaSrvAddr string, workingDirs Workin
5253
}
5354
}
5455

55-
func (f *frontend) Name() string {
56+
func (f *Frontend) Name() string {
5657
return string(greptimedbclusterv1alpha1.FrontendComponentKind)
5758
}
5859

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

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

90-
func (f *frontend) BuildArgs(params ...interface{}) []string {
91+
func (f *Frontend) BuildArgs(params ...interface{}) []string {
9192
logLevel := f.config.LogLevel
9293
if logLevel == "" {
9394
logLevel = DefaultLogLevel
@@ -115,7 +116,7 @@ func (f *frontend) BuildArgs(params ...interface{}) []string {
115116
return args
116117
}
117118

118-
func (f *frontend) IsRunning(_ context.Context) bool {
119+
func (f *Frontend) IsRunning(_ context.Context) bool {
119120
for i := 0; i < f.config.Replicas; i++ {
120121
addr := FormatAddrArg(f.config.HTTPAddr, i)
121122
healthy := fmt.Sprintf("http://%s/health", addr)
@@ -138,3 +139,34 @@ func (f *frontend) IsRunning(_ context.Context) bool {
138139
}
139140
return true
140141
}
142+
143+
func (f *Frontend) PrintDashboardLog() {
144+
format := "http://%s:%s/dashboard/"
145+
146+
// Split addr to host:ip
147+
http_addr := f.config.HTTPAddr
148+
host, port, err := net.SplitHostPort(http_addr)
149+
if len(http_addr) > 0 && err != nil {
150+
f.logger.Warnf("%s use incorrect http_addr: %s err: %s", f.Name(), http_addr, err)
151+
return
152+
}
153+
154+
var log string
155+
if len(http_addr) == 0 || host == "0.0.0.0" {
156+
ip, err := HostnameIP()
157+
158+
if err != nil {
159+
f.logger.Warnf("%s hostname local resolution failed: %s", f.Name(), err)
160+
return
161+
}
162+
if len(port) == 0 {
163+
port = "4000"
164+
}
165+
166+
log = fmt.Sprintf(format, ip, port)
167+
} else {
168+
log = fmt.Sprintf(format, host, port)
169+
}
170+
171+
f.logger.V(0).Infof("If enable dashboard, view it by accessing: %s", logger.Bold(log))
172+
}

pkg/components/utils.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package components
1919
import (
2020
"fmt"
2121
"net"
22+
"os"
2223
"strconv"
2324
)
2425

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

4950
return append(args, fmt.Sprintf("%s=%s", config, socketAddr))
5051
}
52+
53+
func HostnameIP() (ip string, err error) {
54+
hostname, err := os.Hostname()
55+
if err != nil {
56+
return "", err
57+
}
58+
59+
ips, err := net.LookupHost(hostname)
60+
if err != nil {
61+
return "", err
62+
}
63+
64+
if len(ips) == 0 {
65+
return "", nil
66+
}
67+
return ips[0], nil
68+
}

0 commit comments

Comments
 (0)