From 6224849fd13f1660c52e3872bcc880cb0429dc68 Mon Sep 17 00:00:00 2001 From: Shubhendu Date: Wed, 4 Sep 2024 22:32:39 +0530 Subject: [PATCH] Dont start console service if MINIO_BROWSER=off (#20374) By default, even if MINIO_BROWSER=off set code tries to get free port available for the console. Signed-off-by: Shubhendu Ram Tripathi --- cmd/common-main.go | 44 ++++++++++++++++++++++++++------------------ cmd/server-main.go | 3 +++ cmd/signals.go | 6 ++++-- 3 files changed, 33 insertions(+), 20 deletions(-) diff --git a/cmd/common-main.go b/cmd/common-main.go index c3d80e0f8..c9f709fa3 100644 --- a/cmd/common-main.go +++ b/cmd/common-main.go @@ -461,25 +461,27 @@ func handleCommonArgs(ctxt serverCtxt) { certsDir := ctxt.CertsDir certsSet := ctxt.certsDirSet - if consoleAddr == "" { - p, err := xnet.GetFreePort() - if err != nil { - logger.FatalIf(err, "Unable to get free port for Console UI on the host") + if globalBrowserEnabled { + if consoleAddr == "" { + p, err := xnet.GetFreePort() + if err != nil { + logger.FatalIf(err, "Unable to get free port for Console UI on the host") + } + // hold the port + l, err := net.Listen("TCP", fmt.Sprintf(":%s", p.String())) + if err == nil { + defer l.Close() + } + consoleAddr = net.JoinHostPort("", p.String()) } - // hold the port - l, err := net.Listen("TCP", fmt.Sprintf(":%s", p.String())) - if err == nil { - defer l.Close() + + if _, _, err := net.SplitHostPort(consoleAddr); err != nil { + logger.FatalIf(err, "Unable to start listening on console port") } - consoleAddr = net.JoinHostPort("", p.String()) - } - if _, _, err := net.SplitHostPort(consoleAddr); err != nil { - logger.FatalIf(err, "Unable to start listening on console port") - } - - if consoleAddr == addr { - logger.FatalIf(errors.New("--console-address cannot be same as --address"), "Unable to start the server") + if consoleAddr == addr { + logger.FatalIf(errors.New("--console-address cannot be same as --address"), "Unable to start the server") + } } globalMinioHost, globalMinioPort = mustSplitHostPort(addr) @@ -492,7 +494,9 @@ func handleCommonArgs(ctxt serverCtxt) { globalDynamicAPIPort = true } - globalMinioConsoleHost, globalMinioConsolePort = mustSplitHostPort(consoleAddr) + if globalBrowserEnabled { + globalMinioConsoleHost, globalMinioConsolePort = mustSplitHostPort(consoleAddr) + } if globalMinioPort == globalMinioConsolePort { logger.FatalIf(errors.New("--console-address port cannot be same as --address port"), "Unable to start the server") @@ -696,12 +700,16 @@ func loadEnvVarsFromFiles() { } } -func serverHandleEnvVars() { +func serverHandleEarlyEnvVars() { var err error globalBrowserEnabled, err = config.ParseBool(env.Get(config.EnvBrowser, config.EnableOn)) if err != nil { logger.Fatal(config.ErrInvalidBrowserValue(err), "Invalid MINIO_BROWSER value in environment variable") } +} + +func serverHandleEnvVars() { + var err error if globalBrowserEnabled { if redirectURL := env.Get(config.EnvBrowserRedirectURL, ""); redirectURL != "" { u, err := xnet.ParseHTTPURL(redirectURL) diff --git a/cmd/server-main.go b/cmd/server-main.go index 19036e5cf..98b2e4a23 100644 --- a/cmd/server-main.go +++ b/cmd/server-main.go @@ -773,6 +773,9 @@ func serverMain(ctx *cli.Context) { // Always load ENV variables from files first. loadEnvVarsFromFiles() + // Handle early server environment vars + serverHandleEarlyEnvVars() + // Handle all server command args and build the disks layout bootstrapTrace("serverHandleCmdArgs", func() { err := buildServerCtxt(ctx, &globalServerCtxt) diff --git a/cmd/signals.go b/cmd/signals.go index 09f3fe691..1b2f3ffa2 100644 --- a/cmd/signals.go +++ b/cmd/signals.go @@ -81,8 +81,10 @@ func handleSignals() { shutdownLogIf(context.Background(), objAPI.Shutdown(context.Background())) } - if srv := newConsoleServerFn(); srv != nil { - shutdownLogIf(context.Background(), srv.Shutdown()) + if globalBrowserEnabled { + if srv := newConsoleServerFn(); srv != nil { + shutdownLogIf(context.Background(), srv.Shutdown()) + } } if globalEventNotifier != nil {