Skip to content

Commit

Permalink
Refactor the versioning code
Browse files Browse the repository at this point in the history
This commit includes the following changes:
- Move the versioning related constants to a common package pkg/version
- Use the BUILDMETA and VERSION files to specify the version for all components
- Modify the build scripts to use the version info from above files, instead of computing
- Provide a functionality to auto-generate version, for go get cases
- Refactor to code of apiserver connection status to pkg/client/mapiserver
- Change mayactl version to use the above client package for showing mapiserver status
- Refactor detection of orchprovider to its own file (detect.go)
- Change mayactl version to use the above detect.go to display provider
- Add the COBRA cli logic for mayactl version - using above refactored changes
- Change the m-apiserver version to use the refactored version package

Tested the following on local setup:
```
kiran@kmaya:~/go/src/github.com/openebs/maya$ ./bin/maya/maya version
Maya v0.5.0-unreleased (5254285)
Go Version: go1.9.1
OS/Arch: linux / amd64
m-apiserver url:  http://127.0.1.1:5656
m-apiserver status:  not reachable
Provider:  Unknown
kiran@kmaya:~/go/src/github.com/openebs/maya$ ./bin/apiserver/maya-apiserver version
m-apiserver v0.5.0-unreleased (5254285)
kiran@kmaya:~/go/src/github.com/openebs/maya$
```

Also tested on minikube:
```
root@maya-apiserver-757497993-bnrcc:/# /tmp/maya version
Maya v0.5.0-unreleased (5254285)
Go Version: go1.9.1
OS/Arch: linux / amd64
m-apiserver url:  http://172.17.0.5:5656
m-apiserver status:  running
Provider:  KUBERNETES
root@maya-apiserver-757497993-bnrcc:/#
```
  • Loading branch information
kmova committed Oct 26, 2017
1 parent ec6cc6d commit f701d50
Show file tree
Hide file tree
Showing 17 changed files with 237 additions and 208 deletions.
1 change: 1 addition & 0 deletions BUILDMETA
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
unreleased
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v0.5.0
19 changes: 7 additions & 12 deletions buildscripts/apiserver/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ cd "$DIR"

# Get the git commit
GIT_COMMIT="$(git rev-parse HEAD)"
GIT_DIRTY="$(test -n "`git status --porcelain`" && echo "+CHANGES" || true)"

# Get the version details
VERSION="`cat $GOPATH/src/github.com/openebs/maya/VERSION`"
VERSION_META="`cat $GOPATH/src/github.com/openebs/maya/BUILDMETA`"

# Determine the arch/os combos we're building for
XC_ARCH=${XC_ARCH:-"386 amd64"}
Expand All @@ -27,15 +30,6 @@ echo "==> Removing old bin/apiserver contents..."
rm -rf bin/apiserver/*
mkdir -p bin/apiserver/

# Fetch the tags before using git rev-list --tags
git fetch --tags >/dev/null 2>&1
GIT_TAG="$(git describe --tags $(git rev-list --tags --max-count=1))"

if [ -z "${GIT_TAG}" ];
then
GIT_TAG="0.0.1"
fi

if [ -z "${CTLNAME}" ];
then
CTLNAME="apiserver"
Expand All @@ -60,9 +54,10 @@ do
output_name+='.exe'
fi
env GOOS=$GOOS GOARCH=$GOARCH go build -ldflags \
"-X main.GitCommit='${GIT_COMMIT}${GIT_DIRTY}' \
"-X github.com/openebs/maya/pkg/version.GitCommit=${GIT_COMMIT} \
-X main.CtlName='${CTLNAME}' \
-X main.Version='${GIT_TAG}'"\
-X github.com/openebs/maya/pkg/version.Version=${VERSION} \
-X github.com/openebs/maya/pkg/version.VersionMeta=${VERSION_META}"\
-o $output_name\
./cmd/maya-apiserver

Expand Down
17 changes: 6 additions & 11 deletions buildscripts/mayactl/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ cd "$DIR"

# Get the git commit
GIT_COMMIT="$(git rev-parse HEAD)"
GIT_DIRTY="$(test -n "`git status --porcelain`" && echo "+CHANGES" || true)"

# Fetch the tags before using git rev-list --tags
git fetch --tags >/dev/null 2>&1
GIT_TAG="$(git describe --tags $(git rev-list --tags --max-count=1))"
# Get the version details
VERSION="`cat $GOPATH/src/github.com/openebs/maya/VERSION`"
VERSION_META="`cat $GOPATH/src/github.com/openebs/maya/BUILDMETA`"

# Determine the arch/os combos we're building for
XC_ARCH=${XC_ARCH:-"386 amd64"}
Expand All @@ -31,11 +30,6 @@ echo "==> Removing old directory..."
rm -rf bin/maya/*
mkdir -p bin/maya/

if [ -z "${GIT_TAG}" ];
then
GIT_TAG="0.0.1"
fi

if [ -z "${MAYACTL}" ];
then
MAYACTL="mayactl"
Expand All @@ -60,9 +54,10 @@ do
output_name+='.exe'
fi
env GOOS=$GOOS GOARCH=$GOARCH go build -ldflags \
"-X main.GitCommit='${GIT_COMMIT}${GIT_DIRTY}' \
"-X github.com/openebs/maya/pkg/version.GitCommit=${GIT_COMMIT} \
-X main.CtlName='${CTLNAME}' \
-X main.Version='${GIT_TAG}'"\
-X github.com/openebs/maya/pkg/version.Version=${VERSION} \
-X github.com/openebs/maya/pkg/version.VersionMeta=${VERSION_META}"\
-o $output_name

done
Expand Down
4 changes: 2 additions & 2 deletions cmd/maya-apiserver/app/command/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ func (c *VersionCommand) Help() string {
func (c *VersionCommand) Run(_ []string) int {
var versionString bytes.Buffer

fmt.Fprintf(&versionString, "m-apiserver v%s", c.Version)
fmt.Fprintf(&versionString, "m-apiserver %s", c.Version)
if c.VersionPrerelease != "" {
fmt.Fprintf(&versionString, "-%s", c.VersionPrerelease)
fmt.Fprintf(&versionString, "%s", c.VersionPrerelease)

if c.Revision != "" {
fmt.Fprintf(&versionString, " (%s)", c.Revision)
Expand Down
26 changes: 7 additions & 19 deletions cmd/maya-apiserver/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/mitchellh/cli"
"github.com/openebs/maya/cmd/maya-apiserver/app/command"
"github.com/openebs/maya/pkg/version"
)

// Commands returns the mapping of CLI commands for Maya server. The meta
Expand All @@ -26,31 +27,18 @@ func Commands(metaPtr *command.Meta) map[string]cli.CommandFactory {
return map[string]cli.CommandFactory{
"up": func() (cli.Command, error) {
return &command.UpCommand{
Revision: GitCommit,
Version: Version,
VersionPrerelease: VersionPrerelease,
Revision: version.GetGitCommit(),
Version: version.GetVersion(),
VersionPrerelease: version.GetBuildMeta(),
Ui: meta.Ui,
ShutdownCh: make(chan struct{}),
}, nil
},
"version": func() (cli.Command, error) {
ver := Version
rel := VersionPrerelease
if GitDescribe != "" {
ver = GitDescribe
// Trim off a leading 'v', we append it anyways.
if ver[0] == 'v' {
ver = ver[1:]
}
}
if GitDescribe == "" && rel == "" && VersionPrerelease != "" {
rel = "dev"
}

return &command.VersionCommand{
Revision: GitCommit,
Version: ver,
VersionPrerelease: rel,
Revision: version.GetGitCommit(),
Version: version.GetVersion(),
VersionPrerelease: version.GetBuildMeta(),
Ui: meta.Ui,
}, nil
},
Expand Down
16 changes: 0 additions & 16 deletions cmd/maya-apiserver/version.go

This file was deleted.

74 changes: 17 additions & 57 deletions cmd/mayactl/app/command/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,80 +18,40 @@ package command

import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"

"github.com/openebs/maya/orchprovider"
"github.com/openebs/maya/pkg/client/mapiserver"
"github.com/openebs/maya/pkg/version"
"github.com/spf13/cobra"
)

var (
// Version is the current version of Ark, set by the go linker's -X flag at build time.
Version string

// GitSHA is the actual commit that is being built, set by the go linker's -X flag at build time.
GitSHA string

// GitTreeState indicates if the git tree is clean or dirty, set by the go linker's -X flag at build
// time.
GitTreeState string
)

// NewCommand creates the version command
func NewCmdVersion() *cobra.Command {
cmd := &cobra.Command{
Use: "version",
Short: "Print Maya version information",
Long: `Print Maya version information for the current context
Short: "Prints version and other details relevant to maya",
Long: `Prints version and other details relevant to maya
Example:
Usage:
maya version
`,

Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("Version: %s\n", getVersion())
fmt.Printf("Git commit: %s", getGitCommit())
//fmt.Printf("Git tree state: %s\n", GitTreeState)
fmt.Printf("Go-Version: %s\n", runtime.Version())
fmt.Printf("GOARCH: %s\n", runtime.GOARCH)
fmt.Printf("GOOS: %s\n", runtime.GOOS)
fmt.Printf("Version: %s\n",
version.GetVersion()+version.GetBuildMeta())
fmt.Printf("Git commit: %s\n", version.GetGitCommit())

},
}
fmt.Printf("GO Version: %s\n", runtime.Version())
fmt.Printf("GO ARCH: %s\n", runtime.GOARCH)
fmt.Printf("GO OS: %s\n", runtime.GOOS)

return cmd
}
fmt.Println("m-apiserver url: ", mapiserver.GetURL())
fmt.Println("m-apiserver status: ", mapiserver.GetConnectionStatus())

// FormattedGitSHA renders the Git SHA with an indicator of the tree state.
func FormattedGitSHA() string {
if GitTreeState != "clean" {
return fmt.Sprintf("%s-%s", GitSHA, GitTreeState)
}
return GitSHA
}

var (
versionFile = "/src/github.com/openebs/maya/VERSION"
)

func getVersion() string {
path := filepath.Join(os.Getenv("GOPATH") + versionFile)
vBytes, err := ioutil.ReadFile(path)
if err != nil {
// ignore error
return ""
fmt.Println("Provider: ", orchprovider.DetectOrchProviderFromEnv())
},
}
return string(vBytes)
}

func getGitCommit() string {
cmd := exec.Command("git", "rev-parse", "--verify", "HEAD")
output, err := cmd.Output()
if err != nil {
// ignore error
return ""
}
return string(output)
return cmd
}
9 changes: 8 additions & 1 deletion cmd/mayactl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@

package main

import "github.com/openebs/maya/cmd/mayactl/app/command"
import (
"github.com/openebs/maya/cmd/mayactl/app/command"
"github.com/openebs/maya/pkg/client/mapiserver"
)

func init() {
mapiserver.Initialize()
}

func main() {
err := command.NewMayaCommand().Execute()
Expand Down
39 changes: 7 additions & 32 deletions command/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package command
import (
"bytes"
"fmt"
"os"
"runtime"
"strings"

"github.com/mitchellh/cli"
"github.com/openebs/maya/orchprovider"
"github.com/openebs/maya/pkg/client/mapiserver"
)

// VersionCommand is a Command implementation prints the version.
Expand All @@ -32,10 +33,9 @@ This command provides versioning and other details relevant to maya.
// Run holds the flag values for CLI subcommands
func (c *VersionCommand) Run(_ []string) int {
var versionString bytes.Buffer
var s *ServerMembersCommand
fmt.Fprintf(&versionString, "Maya v%s", c.Version)
fmt.Fprintf(&versionString, "Maya %s", c.Version)
if c.VersionPrerelease != "" {
fmt.Fprintf(&versionString, "-%s", c.VersionPrerelease)
fmt.Fprintf(&versionString, "%s", c.VersionPrerelease)

if c.Revision != "" {
fmt.Fprintf(&versionString, " (%s)", c.Revision)
Expand All @@ -46,36 +46,11 @@ func (c *VersionCommand) Run(_ []string) int {

fmt.Println("Go Version:", runtime.Version())
fmt.Println("OS/Arch:", runtime.GOOS, "/", runtime.GOARCH)
addr := os.Getenv("MAPI_ADDR")

/*if addr == "" {
addr = getEnvOrDefault(addr)
fmt.Println("m-apiserver url: ", mapiserver.GetURL())
fmt.Println("m-apiserver status: ", mapiserver.GetConnectionStatus())

os.Setenv("MAPI_ADDR", addr)
addr = os.Getenv("MAPI_ADDR")
}*/
_, err := s.mserverStatus()
if err != nil {
fmt.Println("M-apiserver: Unable to contact M-apiserver :", addr)
}
if err == nil {
fmt.Printf("M-apiserver: %v\n", addr)
}
_, ok := os.LookupEnv("KUBERNETES_SERVICE_HOST")
if !ok {
for _, e := range os.Environ() {
ok := strings.Contains(e, "NOMAD_ADDR")
if !ok {
fmt.Println("Provider : Unknown")
return 0
}
fmt.Println("Provider : NOMAD")
return 0
}

}
fmt.Println("Provider: KUBERNETES")
fmt.Println("Provider: ", orchprovider.DetectOrchProviderFromEnv())

return 0
}
Expand Down
20 changes: 4 additions & 16 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/mitchellh/cli"
"github.com/openebs/maya/command"
"github.com/openebs/maya/pkg/version"
)

// Commands returns the mapping of CLI commands for Maya. The meta
Expand Down Expand Up @@ -110,23 +111,10 @@ func Commands(metaPtr *command.Meta) map[string]cli.CommandFactory {
},
*/
"version": func() (cli.Command, error) {
ver := Version
rel := VersionPrerelease
if GitDescribe != "" {
ver = GitDescribe
// Trim off a leading 'v', we append it anyways.
if ver[0] == 'v' {
ver = ver[1:]
}
}
if GitDescribe == "" && rel == "" && VersionPrerelease != "" {
rel = "dev"
}

return &command.VersionCommand{
Revision: GitCommit,
Version: ver,
VersionPrerelease: rel,
Revision: version.GetGitCommit(),
Version: version.GetVersion(),
VersionPrerelease: version.GetBuildMeta(),
Ui: meta.Ui,
}, nil
},
Expand Down
Loading

0 comments on commit f701d50

Please sign in to comment.