From 1447e6949d8e06e4563ccf089ecef046a87c65f7 Mon Sep 17 00:00:00 2001 From: Ta-Ching Chen Date: Mon, 19 Mar 2018 14:14:55 +0800 Subject: [PATCH] Show fission deployment version with cli (#538) Add version information to binary through go ldflags --- builder/builder.go | 7 ++++ builder/cmd/build.sh | 17 ++++++++- builder/cmd/main.go | 1 + controller/api.go | 2 +- environments/fetcher/cmd/build.sh | 17 ++++++++- environments/fetcher/cmd/main.go | 1 + environments/fetcher/fetcher.go | 5 +++ fission-bundle/build.sh | 17 ++++++++- fission-bundle/main.go | 7 +++- fission/main.go | 32 ++++++++++++++++- hack/release.sh | 58 ++++++++++++++++++++++++------- version.go | 48 +++++++++++++++++++++++++ 12 files changed, 194 insertions(+), 18 deletions(-) create mode 100644 version.go diff --git a/builder/builder.go b/builder/builder.go index 3abe84d0..18891cab 100644 --- a/builder/builder.go +++ b/builder/builder.go @@ -33,6 +33,8 @@ import ( "time" "github.com/dchest/uniuri" + + "github.com/fission/fission" ) const ( @@ -68,6 +70,11 @@ func MakeBuilder(sharedVolumePath string) *Builder { } } +func (builder *Builder) VersionHandler(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json; charset=utf-8") + fmt.Fprintf(w, fission.VersionInfo().String()) +} + func (builder *Builder) Handler(w http.ResponseWriter, r *http.Request) { if r.Method != "POST" { e := fmt.Sprintf("Method not allowed: %v", r.Method) diff --git a/builder/cmd/build.sh b/builder/cmd/build.sh index 668ffa8c..8d438476 100755 --- a/builder/cmd/build.sh +++ b/builder/cmd/build.sh @@ -1,3 +1,18 @@ #!/bin/bash -CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -gcflags=-trimpath=$GOPATH -asmflags=-trimpath=$GOPATH -o builder . +version=$1 +if [ -z $version ]; then + version=$(git rev-parse HEAD) +fi + +date=$2 +if [ -z $date ]; then + date=$(date -u +'%Y-%m-%dT%H:%M:%SZ') +fi + +gitcommit=$3 +if [ -z $gitcommit ]; then + gitcommit=$(git rev-parse HEAD) +fi + +CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -gcflags=-trimpath=$GOPATH -asmflags=-trimpath=$GOPATH -ldflags "-X github.com/fission/fission.GitCommit=$gitcommit -X github.com/fission/fission.BuildDate=$date -X github.com/fission/fission.Version=$version" -o builder . diff --git a/builder/cmd/main.go b/builder/cmd/main.go index db59e4ae..e22f6d26 100644 --- a/builder/cmd/main.go +++ b/builder/cmd/main.go @@ -38,6 +38,7 @@ func main() { builder := builder.MakeBuilder(dir) mux := http.NewServeMux() mux.HandleFunc("/", builder.Handler) + mux.HandleFunc("/version", builder.VersionHandler) mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) }) diff --git a/controller/api.go b/controller/api.go index 960f29f8..1ebabdbe 100644 --- a/controller/api.go +++ b/controller/api.go @@ -137,7 +137,7 @@ func (api *API) getLogDBConfig(dbType string) logDBConfig { func (api *API) HomeHandler(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json; charset=utf-8") - fmt.Fprintf(w, "{\"message\": \"Fission API\", \"version\": \"0.6.0\"}\n") + fmt.Fprintf(w, fission.VersionInfo().String()) } func (api *API) ApiVersionMismatchHandler(w http.ResponseWriter, r *http.Request) { diff --git a/environments/fetcher/cmd/build.sh b/environments/fetcher/cmd/build.sh index dcbf965a..f1601908 100755 --- a/environments/fetcher/cmd/build.sh +++ b/environments/fetcher/cmd/build.sh @@ -1,2 +1,17 @@ #!/bin/sh -CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -gcflags=-trimpath=$GOPATH -asmflags=-trimpath=$GOPATH -o fetcher . +version=$1 +if [ -z $version ]; then + version=$(git rev-parse HEAD) +fi + +date=$2 +if [ -z $date ]; then + date=$(date -u +'%Y-%m-%dT%H:%M:%SZ') +fi + +gitcommit=$3 +if [ -z $gitcommit ]; then + gitcommit=$(git rev-parse HEAD) +fi + +CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -gcflags=-trimpath=$GOPATH -asmflags=-trimpath=$GOPATH -ldflags "-X github.com/fission/fission.GitCommit=$gitcommit -X github.com/fission/fission.BuildDate=$date -X github.com/fission/fission.Version=$version" -o fetcher . diff --git a/environments/fetcher/cmd/main.go b/environments/fetcher/cmd/main.go index 1d930deb..66cd159f 100644 --- a/environments/fetcher/cmd/main.go +++ b/environments/fetcher/cmd/main.go @@ -71,6 +71,7 @@ func main() { mux := http.NewServeMux() mux.HandleFunc("/", fetcher.FetchHandler) mux.HandleFunc("/upload", fetcher.UploadHandler) + mux.HandleFunc("/version", fetcher.VersionHandler) mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) }) diff --git a/environments/fetcher/fetcher.go b/environments/fetcher/fetcher.go index 56d28c93..d39817a3 100644 --- a/environments/fetcher/fetcher.go +++ b/environments/fetcher/fetcher.go @@ -163,6 +163,11 @@ func writeSecretOrConfigMap(dataMap map[string][]byte, dirPath string) error { return nil } +func (fetcher *Fetcher) VersionHandler(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json; charset=utf-8") + fmt.Fprintf(w, fission.VersionInfo().String()) +} + func (fetcher *Fetcher) FetchHandler(w http.ResponseWriter, r *http.Request) { if r.Method != "POST" { http.Error(w, "only POST is supported on this endpoint", http.StatusMethodNotAllowed) diff --git a/fission-bundle/build.sh b/fission-bundle/build.sh index d57ae411..c29ca820 100755 --- a/fission-bundle/build.sh +++ b/fission-bundle/build.sh @@ -1,2 +1,17 @@ #!/bin/sh -CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -gcflags=-trimpath=$GOPATH -asmflags=-trimpath=$GOPATH +version=$1 +if [ -z $version ]; then + version=$(git rev-parse HEAD) +fi + +date=$2 +if [ -z $date ]; then + date=$(date -u +'%Y-%m-%dT%H:%M:%SZ') +fi + +gitcommit=$3 +if [ -z $gitcommit ]; then + gitcommit=$(git rev-parse HEAD) +fi + +CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -gcflags=-trimpath=$GOPATH -asmflags=-trimpath=$GOPATH -ldflags "-X github.com/fission/fission.GitCommit=$gitcommit -X github.com/fission/fission.BuildDate=$date -X github.com/fission/fission.Version=$version" diff --git a/fission-bundle/main.go b/fission-bundle/main.go index 66b77b96..c7243650 100644 --- a/fission-bundle/main.go +++ b/fission-bundle/main.go @@ -1,12 +1,14 @@ package main import ( + "fmt" "log" "os" "strconv" "github.com/docopt/docopt-go" + "github.com/fission/fission" "github.com/fission/fission/buildermgr" "github.com/fission/fission/controller" "github.com/fission/fission/executor" @@ -120,6 +122,7 @@ Usage: fission-bundle --builderMgr [--storageSvcUrl=] [--envbuilder-namespace=] fission-bundle --timer [--routerUrl=] fission-bundle --mqt [--routerUrl=] + fission-bundle --version Options: --controllerPort= Port that the controller should listen on. --routerPort= Port that the router should listen on. @@ -135,8 +138,10 @@ Options: --timer Start Timer. --mqt Start message queue trigger. --builderMgr Start builder manager. + --version Print version information ` - arguments, err := docopt.Parse(usage, nil, true, "fission-bundle", false) + version := fmt.Sprintf("Fission Bundle Version: %v", fission.VersionInfo().String()) + arguments, err := docopt.Parse(usage, nil, true, version, false) if err != nil { log.Fatalf("Error: %v", err) } diff --git a/fission/main.go b/fission/main.go index 1a82da86..94194e30 100644 --- a/fission/main.go +++ b/fission/main.go @@ -17,9 +17,14 @@ limitations under the License. package main import ( + "fmt" + "io/ioutil" + "net/http" "os" "path/filepath" + "strings" + version "github.com/fission/fission" "github.com/urfave/cli" ) @@ -41,11 +46,25 @@ func getKubeConfigPath() string { return kubeConfig } +func getFissionAPIVersion(apiUrl string) (string, error) { + resp, err := http.Get(apiUrl) + if err != nil { + return "", err + } + defer resp.Body.Close() + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return "", err + } + return strings.TrimRight(string(body), "\n"), nil +} + func main() { app := cli.NewApp() app.Name = "fission" app.Usage = "Serverless functions for Kubernetes" - app.Version = "0.6.0" + app.Version = version.Version // fetch the FISSION_URL env variable. If not set, port-forward to controller. var value string @@ -60,6 +79,17 @@ func main() { value = fissionUrl } + cli.VersionPrinter = func(c *cli.Context) { + clientVer := version.VersionInfo().String() + fmt.Printf("Client Version: %v\n", clientVer) + serverVer, err := getFissionAPIVersion(value) + if err != nil { + fmt.Printf("Error getting Fission API version: %v", err) + } else { + fmt.Printf("Server Version: %v", serverVer) + } + } + app.Flags = []cli.Flag{ cli.StringFlag{Name: "server", Value: value, Usage: "Fission server URL"}, } diff --git a/hack/release.sh b/hack/release.sh index dede954b..c6418198 100755 --- a/hack/release.sh +++ b/hack/release.sh @@ -28,15 +28,22 @@ check_clean() { # Build CLI binaries for mac/linux/windows build_all_cli() { - build_cli "linux" "linux" - build_cli "darwin" "osx" - build_cli "windows" "windows" + local version=$1 + local date=$2 + local gitcommit=$3 + + build_cli "linux" "linux" $version $date $gitcommit + build_cli "darwin" "osx" $version $date $gitcommit + build_cli "windows" "windows" $version $date $gitcommit } # Build cli binary for one OS, and put it in $BUILDDIR/cli// build_cli() { os=$1 osName=$2 + local version=$3 + local date=$4 + local gitcommit=$5 arch="amd64" # parameterize if/when we need to pushd $DIR/fission @@ -48,7 +55,7 @@ build_cli() { binary=fission-cli-${osName} fi - GOOS=$os GOARCH=$arch go build -gcflags=-trimpath=$GOPATH -asmflags=-trimpath=$GOPATH -o $binary . + GOOS=$os GOARCH=$arch go build -gcflags=-trimpath=$GOPATH -asmflags=-trimpath=$GOPATH -ldflags "-X github.com/fission/fission.GitCommit=$gitcommit -X github.com/fission/fission.BuildDate=$date -X github.com/fission/fission.Version=$version" -o $binary . outdir=$BUILDDIR/cli/$osName/ mkdir -p $outdir @@ -60,11 +67,14 @@ build_cli() { # Build fission-bundle image build_fission_bundle_image() { local version=$1 + local date=$2 + local gitcommit=$3 + local tag=fission/fission-bundle:$version pushd $DIR/fission-bundle - ./build.sh + ./build.sh $version $date $gitcommit docker build -t $tag . docker tag $tag fission/fission-bundle:latest @@ -80,11 +90,13 @@ push_fission_bundle_image() { build_fetcher_image() { local version=$1 + local date=$2 + local gitcommit=$3 local tag=fission/fetcher:$version pushd $DIR/environments/fetcher/cmd - ./build.sh + ./build.sh $version $date $gitcommit docker build -t $tag . docker tag $tag fission/fetcher:latest @@ -99,11 +111,13 @@ push_fetcher_image() { build_builder_image() { local version=$1 + local date=$2 + local gitcommit=$3 local tag=fission/builder:$version pushd $DIR/builder/cmd - ./build.sh + ./build.sh $version $date $gitcommit docker build -t $tag . docker tag $tag fission/builder:latest @@ -238,11 +252,28 @@ build_charts() { build_all() { local version=$1 + if [ -z "$version" ] then echo "Version unspecified" exit 1 fi + + local date=$2 + + if [ -z "$date" ] + then + echo "Build date unspecified" + exit 1 + fi + + local gitcommit=$3 + + if [ -z "gitcommit" ] + then + echo "Git commit unspecified" + exit 1 + fi if [ -e $BUILDDIR ] then @@ -252,11 +283,11 @@ build_all() { mkdir -p $BUILDDIR - build_fission_bundle_image $version - build_fetcher_image $version - build_builder_image $version + build_fission_bundle_image $version $date $gitcommit + build_fetcher_image $version $date $gitcommit + build_builder_image $version $date $gitcommit build_logger_image $version - build_all_cli + build_all_cli $version $date $gitcommit build_charts $version } @@ -394,10 +425,13 @@ export GITHUB_TOKEN=$(cat ~/.gh-access-token) version=$1 +date=$(date -u +'%Y-%m-%dT%H:%M:%SZ') +gitcommit=$(git rev-parse HEAD) + check_branch $version check_clean -build_all $version +build_all $version $date $gitcommit push_all $version build_and_push_all_envs $version build_and_push_all_env_builders $version diff --git a/version.go b/version.go new file mode 100644 index 00000000..e3410929 --- /dev/null +++ b/version.go @@ -0,0 +1,48 @@ +/* +Copyright 2017 The Fission Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package fission + +import ( + "encoding/json" +) + +var ( + GitCommit string // $(git rev-parse HEAD) (1b4716ab84903b2e477135a3dc5afdb07f685cb7) + BuildDate string // $(date -u +'%Y-%m-%dT%H:%M:%SZ') (2018-03-08T18:54:38Z) + Version string // fission release version (0.6.0) +) + +type ( + Info struct { + GitCommit string `json:"GitCommit,omitempty"` + BuildDate string `json:"BuildDate,omitempty"` + Version string `json:"Version,omitempty"` + } +) + +func VersionInfo() Info { + return Info{ + GitCommit: GitCommit, + BuildDate: BuildDate, + Version: Version, + } +} + +func (info Info) String() string { + v, _ := json.Marshal(info) + return string(v) +}