Adding git hash to version of the binaries

This commit is contained in:
DanB
2017-01-02 14:41:31 +01:00
parent 54500e38dd
commit d200708083
8 changed files with 78 additions and 8 deletions

View File

@@ -2,13 +2,14 @@
echo "Building CGRateS ..."
go install github.com/cgrates/cgrates/cmd/cgr-engine
GIT_LAST_LOG=$(git log -1)
go install -ldflags "-X 'github.com/cgrates/cgrates/utils.GitLastLog=$GIT_LAST_LOG'" github.com/cgrates/cgrates/cmd/cgr-engine
cr=$?
go install github.com/cgrates/cgrates/cmd/cgr-loader
go install -ldflags "-X 'github.com/cgrates/cgrates/utils.GitLastLog=$GIT_LAST_LOG'" github.com/cgrates/cgrates/cmd/cgr-loader
cl=$?
go install github.com/cgrates/cgrates/cmd/cgr-console
go install -ldflags "-X 'github.com/cgrates/cgrates/utils.GitLastLog=$GIT_LAST_LOG'" github.com/cgrates/cgrates/cmd/cgr-console
cc=$?
go install github.com/cgrates/cgrates/cmd/cgr-tester
go install -ldflags "-X 'github.com/cgrates/cgrates/utils.GitLastLog=$GIT_LAST_LOG'" github.com/cgrates/cgrates/cmd/cgr-tester
ct=$?
exit $cr || $cl || $cc || $ct

View File

@@ -105,7 +105,7 @@ func executeCommand(command string) {
func main() {
flag.Parse()
if *version {
fmt.Println("CGRateS " + utils.VERSION)
fmt.Println(utils.GetCGRVersion())
return
}
var err error

View File

@@ -558,7 +558,7 @@ func initLogger(cfg *config.CGRConfig) error {
func main() {
flag.Parse()
if *version {
fmt.Println("CGRateS " + utils.VERSION)
fmt.Println(utils.GetCGRVersion())
return
}
if *pidFile != "" {

View File

@@ -85,7 +85,7 @@ var (
func main() {
flag.Parse()
if *version {
fmt.Println("CGRateS " + utils.VERSION)
fmt.Println(utils.GetCGRVersion())
return
}
var errRatingDb, errAccDb, errStorDb, err error

View File

@@ -60,6 +60,7 @@ var (
destination = flag.String("destination", "1002", "The destination to use in queries.")
json = flag.Bool("json", false, "Use JSON RPC")
loadHistorySize = flag.Int("load_history_size", cgrConfig.LoadHistorySize, "Limit the number of records in the load history")
version = flag.Bool("version", false, "Prints the application version.")
nilDuration = time.Duration(0)
)
@@ -152,7 +153,10 @@ func durRemoteRater(cd *engine.CallDescriptor) (time.Duration, error) {
func main() {
flag.Parse()
if *version {
fmt.Println(utils.GetCGRVersion())
return
}
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {

View File

@@ -21,11 +21,13 @@ var (
CdreCdrFormats = []string{CSV, DRYRUN, CDRE_FIXED_WIDTH}
PrimaryCdrFields = []string{CGRID, CDRSOURCE, CDRHOST, ACCID, TOR, REQTYPE, DIRECTION, TENANT, CATEGORY, ACCOUNT, SUBJECT, DESTINATION, SETUP_TIME, PDD, ANSWER_TIME, USAGE,
SUPPLIER, DISCONNECT_CAUSE, COST, RATED, PartialField, MEDI_RUNID}
GitLastLog string // If set, it will be processed as part of versioning
)
const (
CGRateS = "CGRateS"
VERSION = "0.9.1~rc8"
GitLastLogFileName = ".git_lastlog.txt"
DIAMETER_FIRMWARE_REVISION = 918
REDIS_MAX_CONNS = 10
POSTGRES = "postgres"

View File

@@ -630,3 +630,54 @@ func CapitalizedMessage(errMessage string) (capStr string) {
capStr = strings.Replace(capStr, " ", "_", -1)
return
}
func GetCGRVersion() (vers string) {
vers = fmt.Sprintf("%s %s", CGRateS, VERSION)
if GitLastLog == "" {
return
}
rdr := bytes.NewBufferString(GitLastLog)
var commitHash string
var commitDate time.Time
for i := 0; i < 5; i++ { // read a maximum of 5 lines
ln, err := rdr.ReadString('\n')
if err != nil {
Logger.Err(fmt.Sprintf("Building version - error: <%s> reading line from file", err.Error()))
return
}
if ln == "" {
return
}
if strings.HasPrefix(ln, "commit ") {
commitSplt := strings.Split(ln, " ")
if len(commitSplt) != 2 {
Logger.Err("Building version - cannot extract commit hash")
fmt.Println(fmt.Sprintf("Building version - cannot extract commit hash"))
return
}
commitHash = commitSplt[1]
continue
}
if strings.HasPrefix(ln, "Date:") {
dateSplt := strings.Split(ln, ": ")
if len(dateSplt) != 2 {
Logger.Err("Building version - cannot split commit date")
fmt.Println(fmt.Sprintf("Building version - cannot split commit date"))
return
}
commitDate, err = time.Parse("Mon Jan 2 15:04:05 2006 -0700", strings.TrimSpace(dateSplt[1]))
if err != nil {
Logger.Err(fmt.Sprintf("Building version - error: <%s> compiling commit date", err.Error()))
fmt.Println(fmt.Sprintf("Building version - error: <%s> compiling commit date", err.Error()))
return
}
break
}
}
if commitHash == "" || commitDate.IsZero() {
Logger.Err("Cannot find commitHash or commitDate information")
return
}
//CGRateS 0.9.1~rc8 git+73014da (2016-12-30T19:48:09+01:00)
return fmt.Sprintf("%s %s git+%s (%s)", CGRateS, VERSION, commitHash[:7], commitDate.Format(time.RFC3339))
}

View File

@@ -747,3 +747,15 @@ func TestCapitalizedMessage(t *testing.T) {
t.Errorf("Received: <%s>", capMsg)
}
}
func TestGetCGRVersion(t *testing.T) {
GitLastLog = `commit 73014daa0c1d7edcb532d5fe600b8a20d588cdf8
Author: DanB <danb@cgrates.org>
Date: Fri Dec 30 19:48:09 2016 +0100
Fixes for db driver to avoid returning new values in case of errors`
eVers := "CGRateS 0.9.1~rc8 git+73014da (2016-12-30T19:48:09+01:00)"
if vers := GetCGRVersion(); vers != eVers {
t.Errorf("Expecting: <%s>, received: <%s>", eVers, vers)
}
}