From 057ec2d6fb697076956ef87c2c4997d4f6327db2 Mon Sep 17 00:00:00 2001 From: powerkimhub Date: Sun, 22 Sep 2024 13:29:55 +0900 Subject: [PATCH] Add version info to cb-spider binary, start.sh and start-dyna.sh accessible with -v (or --version) --- Makefile | 30 +++++++++++++----- api-runtime/apiserver.go | 66 +++++++++++++++++++++++++++------------- bin/nohup-start.sh | 20 ++++++++---- bin/start-dyna.sh | 19 ++++++++---- bin/start.sh | 21 ++++++++----- go.mod | 3 +- go.sum | 6 ++-- 7 files changed, 111 insertions(+), 54 deletions(-) diff --git a/Makefile b/Makefile index 368463d39..487ff8cb4 100644 --- a/Makefile +++ b/Makefile @@ -1,16 +1,30 @@ +VERSION := $(shell git describe --tags --abbrev=8 | sed 's/-g.*//') +COMMIT_SHA := $(shell git rev-parse --short HEAD) +BUILD_TIME := $(shell date) + default: swag - @echo -e '\t[CB-Spider] building ./bin/cb-spider....' - @go mod download - @go mod tidy - @go build -o bin/cb-spider ./api-runtime + @echo -e '\t[CB-Spider] building ./bin/cb-spider...' + @go mod download + @go mod tidy + @go build -ldflags="-X 'main.Version=$(VERSION)' \ + -X 'main.CommitSHA=$(COMMIT_SHA)' \ + -X 'main.BuildTime=$(BUILD_TIME)'" \ + -o bin/cb-spider ./api-runtime + dyna plugin plug dynamic: swag - @echo -e '\t[CB-Spider] build ./bin/cb-spider with plugin mode...' - @go mod download - @go build -tags dyna -o bin/cb-spider-dyna ./api-runtime - @./build_all_driver_lib.sh; + @echo -e '\t[CB-Spider] building ./bin/cb-spider-dyna with plugin mode...' + @go mod download + @go mod tidy + @go build -tags dyna -ldflags="-X 'main.Version=$(VERSION)' \ + -X 'main.CommitSHA=$(COMMIT_SHA)' \ + -X 'main.BuildTime=$(BUILD_TIME)'" \ + -o bin/cb-spider-dyna ./api-runtime + @./build_all_driver_lib.sh; + cc: swag @echo -e '\t[CB-Spider] build ./bin/cb-spider-arm for arm...' GOOS=linux GOARCH=arm go build -o cb-spider-arm ./api-runtime + clean clear: @echo -e '\t[CB-Spider] cleaning...' @rm -rf bin/cb-spider bin/cb-spider-dyna bin/cb-spider-arm diff --git a/api-runtime/apiserver.go b/api-runtime/apiserver.go index 8462fed78..69acc38d7 100644 --- a/api-runtime/apiserver.go +++ b/api-runtime/apiserver.go @@ -3,74 +3,98 @@ // The CB-Spider Mission is to connect all the clouds with a single interface. // // * Cloud-Barista: https://github.com/cloud-barista -// -// by CB-Spider Team, 2020.09. package main import ( "fmt" + "io/ioutil" + "net/http" + "os" "runtime" "sync" - "time" cr "github.com/cloud-barista/cb-spider/api-runtime/common-runtime" restruntime "github.com/cloud-barista/cb-spider/api-runtime/rest-runtime" - - "github.com/go-resty/resty/v2" "github.com/spf13/cobra" ) +var ( + Version string // Populated by ldflags + CommitSHA string // Populated by ldflags + BuildTime string // Populated by ldflags +) + func main() { - // use multi-Core + // Use multi-core CPUs runtime.GOMAXPROCS(runtime.NumCPU()) - rootCmd := NewRootCmd() - if err := rootCmd.Execute(); err != nil { + if err := NewRootCmd().Execute(); err != nil { fmt.Printf("cb-spider terminated with error: %v\n", err) + os.Exit(1) } } func NewRootCmd() *cobra.Command { - rootCmd := &cobra.Command{ + Use: "cb-spider", + Short: "CB-Spider API Server for managing multi-cloud infrastructure", Run: func(cmd *cobra.Command, args []string) { + if versionFlag, _ := cmd.Flags().GetBool("version"); versionFlag { + printVersion() + return + } + // Start the server wg := new(sync.WaitGroup) - wg.Add(1) - go func() { restruntime.RunServer() wg.Done() }() - time.Sleep(time.Millisecond * 5) - wg.Wait() - }, } + // Add global flags for version info + rootCmd.Flags().BoolP("version", "v", false, "Print version information") + + // Add subcommands rootCmd.AddCommand(NewInfoCmd()) return rootCmd } func NewInfoCmd() *cobra.Command { - infoCmd := &cobra.Command{ - Use: "info", + Use: "info", + Short: "Fetch information from the CB-Spider server", Run: func(cmd *cobra.Command, args []string) { - client := resty.New() - resp, err := client.R().Get("http://" + cr.ServiceIPorName + cr.ServicePort + "/spider/endpointinfo") + url := "http://" + cr.ServiceIPorName + cr.ServicePort + "/spider/endpointinfo" + resp, err := http.Get(url) if err != nil { - fmt.Printf("%v\n", err) - } else { - fmt.Printf("%v\n", resp) + fmt.Printf("Error: %v\n", err) + return } + defer resp.Body.Close() + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + fmt.Printf("Error reading response body: %v\n", err) + return + } + + fmt.Printf("%s\n", body) }, } return infoCmd } + +// Print the version information +func printVersion() { + fmt.Printf("Version: %s\n", Version) + fmt.Printf("Commit SHA: %s\n", CommitSHA) + fmt.Printf("Build Time: %s\n", BuildTime) +} diff --git a/bin/nohup-start.sh b/bin/nohup-start.sh index 7f4037dc0..5c7b47071 100755 --- a/bin/nohup-start.sh +++ b/bin/nohup-start.sh @@ -19,11 +19,19 @@ source ${BIN_DIR}/../setup.env # OFF is a static package type. export PLUGIN_SW=OFF -${BIN_DIR}/stop.sh &> /dev/null +# If arguments are provided, pass them to cb-spider +if [ "$#" -gt 0 ]; then + $BIN_DIR/cb-spider "$@" +else + # If no arguments are provided, start the CB-Spider server + # Stop any running instance of cb-spider + ${BIN_DIR}/stop.sh &> /dev/null -echo -e '\n' -echo -e '\t[CB-Spider] Driver Plugin Mode: Static Builtin Mode' -echo -e '\n' -nohup $BIN_DIR/cb-spider > spider-nohup.out 2>&1 & -echo $! > $BIN_DIR/spider.pid + echo -e '\n' + echo -e '\t[CB-Spider] Driver Plugin Mode: Static Builtin Mode' + echo -e '\n' + + nohup $BIN_DIR/cb-spider > spider-nohup.out 2>&1 & + echo $! > $BIN_DIR/spider.pid +fi \ No newline at end of file diff --git a/bin/start-dyna.sh b/bin/start-dyna.sh index d485e669a..8882c0008 100755 --- a/bin/start-dyna.sh +++ b/bin/start-dyna.sh @@ -19,11 +19,18 @@ source ${BIN_DIR}/../setup.env # OFF is a static package type. export PLUGIN_SW=ON -./stop.sh &> /dev/null +# If arguments are provided, pass them to cb-spider +if [ "$#" -gt 0 ]; then + $BIN_DIR/cb-spider "$@" +else + # If no arguments are provided, start the CB-Spider server + # Stop any running instance of cb-spider + ${BIN_DIR}/stop.sh &> /dev/null -echo -e '\n' -echo -e '\t[CB-Spider] Driver Plugin Mode: Dynamic Plugin Mode' -echo -e '\n' + echo -e '\n' + echo -e '\t[CB-Spider] Driver Plugin Mode: Dynamic Plugin Mode' + echo -e '\n' -$BIN_DIR/cb-spider-dyna & -echo $! > $BIN_DIR/spider.pid + $BIN_DIR/cb-spider-dyna & + echo $! > $BIN_DIR/spider.pid +fi diff --git a/bin/start.sh b/bin/start.sh index 8b79d4886..88076ace4 100755 --- a/bin/start.sh +++ b/bin/start.sh @@ -1,6 +1,6 @@ #!/bin/bash -# start CB-Spider Server. +# Start CB-Spider Server or pass arguments to cb-spider. # # The CB-Spider is a sub-Framework of the Cloud-Barista Multi-Cloud Project. # The CB-Spider Mission is to connect all the clouds with a single interface. @@ -19,11 +19,18 @@ source ${BIN_DIR}/../setup.env # OFF is a static package type. export PLUGIN_SW=OFF -${BIN_DIR}/stop.sh &> /dev/null +# If arguments are provided, pass them to cb-spider +if [ "$#" -gt 0 ]; then + $BIN_DIR/cb-spider "$@" +else + # If no arguments are provided, start the CB-Spider server + # Stop any running instance of cb-spider + ${BIN_DIR}/stop.sh &> /dev/null -echo -e '\n' -echo -e '\t[CB-Spider] Driver Plugin Mode: Static Builtin Mode' -echo -e '\n' + echo -e '\n' + echo -e '\t[CB-Spider] Driver Plugin Mode: Static Builtin Mode' + echo -e '\n' -$BIN_DIR/cb-spider & -echo $! > $BIN_DIR/spider.pid + $BIN_DIR/cb-spider & + echo $! > "$BIN_DIR/spider.pid" +fi diff --git a/go.mod b/go.mod index 1b5f365f8..b33079b40 100644 --- a/go.mod +++ b/go.mod @@ -22,7 +22,6 @@ require ( github.com/docker/docker v26.1.5+incompatible github.com/docker/go-connections v0.4.0 github.com/fsnotify/fsnotify v1.7.0 - github.com/go-resty/resty/v2 v2.6.0 github.com/gogo/protobuf v1.3.2 // indirect github.com/golang/protobuf v1.5.4 // indirect github.com/gophercloud/gophercloud v1.3.0 @@ -178,7 +177,7 @@ require ( golang.org/x/sys v0.24.0 // indirect golang.org/x/term v0.22.0 // indirect golang.org/x/text v0.17.0 // indirect - golang.org/x/time v0.5.0 // indirect + golang.org/x/time v0.6.0 // indirect golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect google.golang.org/appengine v1.6.8 // indirect google.golang.org/protobuf v1.34.2 // indirect diff --git a/go.sum b/go.sum index 2c10095b6..dc2944d7c 100644 --- a/go.sum +++ b/go.sum @@ -263,8 +263,6 @@ github.com/go-playground/locales v0.14.0/go.mod h1:sawfccIbzZTqEDETgFXqTho0QybSa github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA= github.com/go-playground/universal-translator v0.18.0 h1:82dyy6p4OuJq4/CByFNOn/jYrnRPArHwAcmLoJZxyho= github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl+lu/H90nyDXpg0fqeB/AQUGNTVA= -github.com/go-resty/resty/v2 v2.6.0 h1:joIR5PNLM2EFqqESUjCMGXrWmXNHEU9CEiK813oKYS4= -github.com/go-resty/resty/v2 v2.6.0/go.mod h1:PwvJS6hvaPkjtjNg9ph+VrSD92bi5Zq73w/BIH7cC3Q= github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE= @@ -989,8 +987,8 @@ golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxb golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= -golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk= -golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= +golang.org/x/time v0.6.0 h1:eTDhh4ZXt5Qf0augr54TN6suAUudPcawVZeIAPU7D4U= +golang.org/x/time v0.6.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= golang.org/x/tools v0.0.0-20180525024113-a5b4c53f6e8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=