From 5ccc980ce9440f8b302528a2005d82169f0859a6 Mon Sep 17 00:00:00 2001 From: Justin Hiemstra Date: Mon, 11 Sep 2023 15:10:06 +0000 Subject: [PATCH] Remove hardcoded originUrl This commit removes the hardcoded origin url and instead makes the value configurable via the pelican config file. Specifying an origin url is required to serve an origin, and the command `pelican origin serve` will fail unless the value is set. It will also fail if the configured origin url specifies a port that doesn't match the port passed via the `-p` flag. --- cmd/origin_serve.go | 24 ++++++++++++++++++++++++ origin_ui/advertise.go | 7 +++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/cmd/origin_serve.go b/cmd/origin_serve.go index 16ec364b3..884a6b989 100644 --- a/cmd/origin_serve.go +++ b/cmd/origin_serve.go @@ -27,6 +27,7 @@ import ( "encoding/base64" "encoding/json" "fmt" + "net/url" "os" "path" "path/filepath" @@ -355,6 +356,29 @@ func checkDefaults() error { return err } + // Check that OriginUrl is defined in the config file. Make sure it parses. + // Fail if either condition isn't met, although note that url.Parse doesn't + // generate errors for many things that are not recognizable urls. + originUrlStr := viper.GetString("OriginUrl") + if originUrlStr == "" { + return errors.New("OriginUrl must be configured to serve an origin") + } + originUrlParsed, err := url.Parse(originUrlStr) + if err != nil { + return errors.Wrap(err, "Could not parse the provided OriginUrl") + } + + if originUrlParsed.Port() == "" { + // No port was specified, let's tack on whatever was passed in the + // command line argument + viper.Set("OriginUrl", originUrlParsed.String()+":"+viper.GetString("WebPort")) + } else if originUrlParsed.Port() != viper.GetString("WebPort") { + // The web port configured via the config file and the webport configured + // via commandline don't match. Perhaps the user is confused? + return errors.New("Mismatched webports: from command line: " + viper.GetString("WebPort") + + ", from config file: " + originUrlParsed.Port() + ". Please ensure these match") + } + return nil } diff --git a/origin_ui/advertise.go b/origin_ui/advertise.go index d9d02b7ed..91df7e897 100644 --- a/origin_ui/advertise.go +++ b/origin_ui/advertise.go @@ -57,13 +57,16 @@ func AdvertiseOrigin() error { if name == "" { return errors.New("Origin name isn't set") } + // TODO: waiting on a different branch to merge origin URL generation - originUrl := "https://localhost:8444" + // The checkdefaults func that runs before the origin is served checks for and + // parses the originUrl, so it should be safe to just grab it as a string here. + originUrl := viper.GetString("OriginUrl") // Here we instantiate the namespaceAd slice, but we still need to define the namespace namespaceUrl, err := url.Parse(viper.GetString("NamespaceUrl")) if err != nil { - return errors.New("Bad namespaceUrl") + return errors.Wrap(err, "Bad NamespaceUrl") } if namespaceUrl.String() == "" { return errors.New("No NamespaceUrl is set")