Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print pelican version at server startup to stderr #1027

Merged
merged 3 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ builds:
tags:
- forceposix
ldflags:
- -s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}} -X main.builtBy=goreleaser -X github.com/pelicanplatform/pelican/config.version={{.Version}}
- -s -w -X github.com/pelicanplatform/pelican/config.commit={{.Commit}} -X github.com/pelicanplatform/pelican/config.date={{.Date}} -X github.com/pelicanplatform/pelican/config.builtBy=goreleaser -X github.com/pelicanplatform/pelican/config.version={{.Version}}
ignore:
- goos: windows
goarch: arm64
Expand Down
6 changes: 1 addition & 5 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package main

import (
"fmt"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -56,10 +55,7 @@ func handleCLI(args []string) error {
// version info regardless of the commands and whether they are defined
// * Remove the -v shorthand since in "origin serve" flagset it's already used for "volume" flag
if args[len(args)-1] == "--version" {
fmt.Println("Version:", config.GetVersion())
fmt.Println("Build Date:", date)
fmt.Println("Build Commit:", commit)
fmt.Println("Built By:", builtBy)
config.PrintPelicanVersion()
return nil
}
err := Execute()
Expand Down
78 changes: 54 additions & 24 deletions cmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,39 @@ import (

"github.com/pelicanplatform/pelican/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestHandleCLIVersionFlag(t *testing.T) {
// Save the current version to reset this variable
var currentVersion = config.GetVersion()
config.SetVersion("0.0.1")
date = "2023-10-06T15:26:50Z"
commit = "f0f94a3edf6641c2472345819a0d5453fc9e68d1"
builtBy = "goreleaser"

currentVersion := config.GetVersion()
currentDate := config.GetBuiltDate()
currentCommit := config.GetBuiltCommit()
currentBuiltBy := config.GetBuiltBy()
// Reset os.Args to ensure Windows doesn't do weird things to the test
oldArgs := os.Args

config.SetVersion("0.0.1")
config.SetBuiltDate("2023-10-06T15:26:50Z")
config.SetBuiltCommit("f0f94a3edf6641c2472345819a0d5453fc9e68d1")
config.SetBuiltBy("goreleaser")

t.Cleanup(func() {
// Restore the args back when test finished
os.Args = oldArgs

// Set the version back to what it was
config.SetVersion(currentVersion)
config.SetBuiltDate(currentDate)
config.SetBuiltCommit(currentCommit)
config.SetBuiltBy(currentBuiltBy)
})

os.Args = []string{os.Args[0]}

mockVersionOutput := fmt.Sprintf(
"Version: %s\nBuild Date: %s\nBuild Commit: %s\nBuilt By: %s",
config.GetVersion(), date, commit, builtBy,
config.GetVersion(), config.GetBuiltDate(), config.GetBuiltCommit(), config.GetBuiltBy(),
)

testCases := []struct {
Expand Down Expand Up @@ -92,20 +108,40 @@ func TestHandleCLIVersionFlag(t *testing.T) {
}

batchTest := func(t *testing.T, arguments []string, expected string) {
// Redirect output to a pip
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
got := ""

if expected == mockVersionOutput {
// Redirect output to a pip
oldStderr := os.Stderr
r, w, _ := os.Pipe()
os.Stderr = w

err := handleCLI(arguments)
err := handleCLI(arguments)
require.NoError(t, err)

// Close the write of pip and redirect output back to stdout
w.Close()
out, _ := io.ReadAll(r)
os.Stdout = oldStdout
// Close the write of pip and redirect output back to Stderr
w.Close()
out, _ := io.ReadAll(r)
os.Stderr = oldStderr

got = strings.TrimSpace(string(out))
} else {
// Redirect output to a pip
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w

err := handleCLI(arguments)
require.NoError(t, err)

// Close the write of pip and redirect output back to Stderr
w.Close()
out, _ := io.ReadAll(r)
os.Stdout = oldStdout

got = strings.TrimSpace(string(out))
}

got := strings.TrimSpace(string(out))
assert.NoError(t, err, "Should not have error running the function")
if expected != mockVersionOutput {
// If the expected string is not the version output, use Contains to check
// This is mainly for checking against command help output
Expand All @@ -120,12 +156,6 @@ func TestHandleCLIVersionFlag(t *testing.T) {
batchTest(t, tc.args, tc.expected)
})
}

// Restore the args back when test finished
os.Args = oldArgs

// Set the version back to what it was
config.SetVersion(currentVersion)
}

func TestHandleCLIExecutableAlias(t *testing.T) {
Expand Down
5 changes: 1 addition & 4 deletions cmd/object_copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,7 @@ func copyMain(cmd *cobra.Command, args []string) {
}

if val, err := cmd.Flags().GetBool("version"); err == nil && val {
fmt.Println("Version:", config.GetVersion())
fmt.Println("Build Date:", date)
fmt.Println("Build Commit:", commit)
fmt.Println("Built By:", builtBy)
config.PrintPelicanVersion()
os.Exit(0)
}

Expand Down
9 changes: 1 addition & 8 deletions cmd/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ import (
)

var (
commit = "none"
date = "unknown"
builtBy = "unknown"

// Holds the various plugin commands
rootPluginCmd = &cobra.Command{
Use: "plugin",
Expand Down Expand Up @@ -133,10 +129,7 @@ func stashPluginMain(args []string) {
fmt.Println("SupportedMethods = \"stash, osdf\"")
os.Exit(0)
} else if args[0] == "-version" || args[0] == "-v" {
fmt.Println("Version:", config.GetVersion())
fmt.Println("Build Date:", date)
fmt.Println("Build Commit:", commit)
fmt.Println("Built By:", builtBy)
config.PrintPelicanVersion()
os.Exit(0)
} else if args[0] == "-upload" {
log.Debugln("Upload detected")
Expand Down
43 changes: 40 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@ const (
TokenSharedRead
)

// This block of variables will be overwritten at build time
var (
commit = "none"
date = "unknown"
builtBy = "unknown"
// Pelican version
version = "dev"
)

var (
// Some of the unit tests probe behavior specific to OSDF vs Pelican. Hence,
// we need a way to override the preferred prefix.
Expand Down Expand Up @@ -139,9 +148,6 @@ var (

RestartFlag = make(chan any) // A channel flag to restart the server instance that launcher listens to (including cache)

// Pelican version, this is overwritten at build time
version string = "dev"

MetadataTimeoutErr *MetadataErr = &MetadataErr{msg: "Timeout when querying metadata"}
)

Expand Down Expand Up @@ -239,6 +245,30 @@ func SetVersion(newVersion string) {
version = newVersion
}

func GetBuiltCommit() string {
return commit
}

func SetBuiltCommit(newCommit string) {
commit = newCommit
}

func GetBuiltDate() string {
return date
}

func SetBuiltDate(builtDate string) {
date = builtDate
}

func GetBuiltBy() string {
return builtBy
}

func SetBuiltBy(newBuiltBy string) {
builtBy = newBuiltBy
}

// Get a string slice of currently enabled servers, sorted by alphabetical order.
// By default, it calls String method of each enabled server.
// To get strings in lowerCase, set lowerCase = true.
Expand Down Expand Up @@ -803,6 +833,13 @@ func setXrootdRunLocations(currentServers ServerType, dir string) error {
return nil
}

func PrintPelicanVersion() {
fmt.Fprintln(os.Stderr, "Version:", GetVersion())
fmt.Fprintln(os.Stderr, "Build Date:", GetBuiltDate())
fmt.Fprintln(os.Stderr, "Build Commit:", GetBuiltCommit())
fmt.Fprintln(os.Stderr, "Built By:", GetBuiltBy())
}

// Print Pelican configuration to stderr
func PrintConfig() error {
rawConfig, err := param.UnmarshalConfig()
Expand Down
2 changes: 2 additions & 0 deletions launchers/launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ func LaunchModules(ctx context.Context, modules config.ServerType) (context.Canc

ctx, shutdownCancel := context.WithCancel(ctx)

config.PrintPelicanVersion()

// Print Pelican config at server start if it's in debug or info level
if log.GetLevel() >= log.InfoLevel {
if err := config.PrintConfig(); err != nil {
Expand Down
Loading