Skip to content

Commit

Permalink
Add version info to cb-spider binary, start.sh and start-dyna.sh acce…
Browse files Browse the repository at this point in the history
…ssible with -v (or --version)
  • Loading branch information
powerkimhub committed Sep 22, 2024
1 parent 425fe3b commit 057ec2d
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 54 deletions.
30 changes: 22 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
66 changes: 45 additions & 21 deletions api-runtime/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
20 changes: 14 additions & 6 deletions bin/nohup-start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 13 additions & 6 deletions bin/start-dyna.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 14 additions & 7 deletions bin/start.sh
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down

0 comments on commit 057ec2d

Please sign in to comment.