Skip to content

Commit

Permalink
Merge pull request #699 from mattermost/PLT-92
Browse files Browse the repository at this point in the history
PLT-92 Adding server side versioning to the binary
  • Loading branch information
jwilander committed Sep 18, 2015
2 parents 7caef4a + c63da24 commit 5a436fd
Show file tree
Hide file tree
Showing 21 changed files with 500 additions and 28 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ dist
npm-debug.log

bundle*.js

model/version.go
model/version.go.bak

# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
Expand Down
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
GOPATH ?= $(GOPATH:)
GOFLAGS ?= $(GOFLAGS:)
BUILD_NUMBER ?= $(BUILD_NUMBER:)
BUILD_DATE = $(shell date -u)
BUILD_HASH = $(shell git rev-parse HEAD)

GO=$(GOPATH)/bin/godep go
ESLINT=node_modules/eslint/bin/eslint.js
Expand Down Expand Up @@ -54,6 +56,10 @@ travis:
exit 1; \
fi

@sed -i'.bak' 's|_BUILD_NUMBER_|$(BUILD_NUMBER)|g' ./model/version.go
@sed -i'.bak' 's|_BUILD_DATE_|$(BUILD_DATE)|g' ./model/version.go
@sed -i'.bak' 's|_BUILD_HASH_|$(BUILD_HASH)|g' ./model/version.go

@$(GO) build $(GOFLAGS) ./...
@$(GO) install $(GOFLAGS) ./...

Expand Down Expand Up @@ -227,6 +233,10 @@ cleandb:
fi
dist: install

@sed -i'.bak' 's|_BUILD_NUMBER_|$(BUILD_NUMBER)|g' ./model/version.go
@sed -i'.bak' 's|_BUILD_DATE_|$(BUILD_DATE)|g' ./model/version.go
@sed -i'.bak' 's|_BUILD_HASH_|$(BUILD_HASH)|g' ./model/version.go

@$(GO) build $(GOFLAGS) -i ./...
@$(GO) install $(GOFLAGS) ./...

Expand Down
2 changes: 1 addition & 1 deletion api/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
c.setSiteURL(protocol + "://" + r.Host)

w.Header().Set(model.HEADER_REQUEST_ID, c.RequestId)
w.Header().Set(model.HEADER_VERSION_ID, utils.Cfg.ServiceSettings.Version+fmt.Sprintf(".%v", utils.CfgLastModified))
w.Header().Set(model.HEADER_VERSION_ID, fmt.Sprintf("%v.%v", model.CurrentVersion, utils.CfgLastModified))

// Instruct the browser not to display us in an iframe for anti-clickjacking
if !h.isApi {
Expand Down
1 change: 0 additions & 1 deletion config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"AllowTesting" : false,
"UseSSL": false,
"Port": "8065",
"Version": "developer",
"Shards": {
},
"InviteSalt": "gxHVDcKUyP2y1eiyW8S8na1UYQAfq6J6",
Expand Down
19 changes: 18 additions & 1 deletion mattermost.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
var flagCmdCreateTeam bool
var flagCmdCreateUser bool
var flagCmdAssignRole bool
var flagCmdVersion bool
var flagCmdResetPassword bool
var flagConfigFile string
var flagEmail string
Expand All @@ -42,6 +43,7 @@ func main() {
}

pwd, _ := os.Getwd()
l4g.Info("Current version is %v (%v/%v/%v)", model.CurrentVersion, model.BuildNumber, model.BuildDate, model.BuildHash)
l4g.Info("Current working directory is %v", pwd)
l4g.Info("Loaded config file from %v", utils.FindConfigFile(flagConfigFile))

Expand Down Expand Up @@ -83,14 +85,16 @@ func parseCmds() {
flag.BoolVar(&flagCmdCreateTeam, "create_team", false, "")
flag.BoolVar(&flagCmdCreateUser, "create_user", false, "")
flag.BoolVar(&flagCmdAssignRole, "assign_role", false, "")
flag.BoolVar(&flagCmdVersion, "version", false, "")
flag.BoolVar(&flagCmdResetPassword, "reset_password", false, "")

flag.Parse()

flagRunCmds = flagCmdCreateTeam || flagCmdCreateUser || flagCmdAssignRole || flagCmdResetPassword
flagRunCmds = flagCmdCreateTeam || flagCmdCreateUser || flagCmdAssignRole || flagCmdResetPassword || flagCmdVersion
}

func runCmds() {
cmdVersion()
cmdCreateTeam()
cmdCreateUser()
cmdAssignRole()
Expand Down Expand Up @@ -184,6 +188,17 @@ func cmdCreateUser() {
}
}

func cmdVersion() {
if flagCmdVersion {
fmt.Fprintln(os.Stderr, "Version: "+model.CurrentVersion)
fmt.Fprintln(os.Stderr, "Build Number: "+model.BuildNumber)
fmt.Fprintln(os.Stderr, "Build Date: "+model.BuildDate)
fmt.Fprintln(os.Stderr, "Build Hash: "+model.BuildHash)

os.Exit(0)
}
}

func cmdAssignRole() {
if flagCmdAssignRole {
if len(flagTeamName) == 0 {
Expand Down Expand Up @@ -298,6 +313,8 @@ Usage:
platform [options]
-version Display the current version
-config="config.json" Path to the config file
-email="user@example.com" Email address used in other commands
Expand Down
34 changes: 34 additions & 0 deletions model/system.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved.
// See License.txt for license information.

package model

import (
"encoding/json"
"io"
)

type System struct {
Name string `json:"name"`
Value string `json:"value"`
}

func (o *System) ToJson() string {
b, err := json.Marshal(o)
if err != nil {
return ""
} else {
return string(b)
}
}

func SystemFromJson(data io.Reader) *System {
decoder := json.NewDecoder(data)
var o System
err := decoder.Decode(&o)
if err == nil {
return &o
} else {
return nil
}
}
19 changes: 19 additions & 0 deletions model/system_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved.
// See License.txt for license information.

package model

import (
"strings"
"testing"
)

func TestSystemJson(t *testing.T) {
system := System{Name: "test", Value: NewId()}
json := system.ToJson()
result := SystemFromJson(strings.NewReader(json))

if result.Name != "test" {
t.Fatal("Ids do not match")
}
}
7 changes: 1 addition & 6 deletions model/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ import (
"time"
)

const (
// Also change web/react/stores/browser_store.jsx BROWSER_STORE_VERSION
ETAG_ROOT_VERSION = "12"
)

type StringMap map[string]string
type StringArray []string
type EncryptStringMap map[string]string
Expand Down Expand Up @@ -235,7 +230,7 @@ func IsValidAlphaNum(s string, allowUnderscores bool) bool {

func Etag(parts ...interface{}) string {

etag := ETAG_ROOT_VERSION
etag := CurrentVersion

for _, part := range parts {
etag += fmt.Sprintf(".%v", part)
Expand Down
90 changes: 90 additions & 0 deletions model/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved.
// See License.txt for license information.

package model

import (
"strconv"
"strings"
)

// This is a list of all the current viersions including any patches.
// It should be maitained in chronological order with most current
// release at the front of the list.
var versions = []string{
"0.8.0",
"0.7.1",
"0.7.0",
"0.6.0",
"0.5.0",
}

var CurrentVersion string = versions[0]
var BuildNumber = "_BUILD_NUMBER_"
var BuildDate = "_BUILD_DATE_"
var BuildHash = "_BUILD_HASH_"

func SplitVersion(version string) (int64, int64, int64) {
parts := strings.Split(version, ".")

major := int64(0)
minor := int64(0)
patch := int64(0)

if len(parts) > 0 {
major, _ = strconv.ParseInt(parts[0], 10, 64)
}

if len(parts) > 1 {
minor, _ = strconv.ParseInt(parts[1], 10, 64)
}

if len(parts) > 2 {
patch, _ = strconv.ParseInt(parts[2], 10, 64)
}

return major, minor, patch
}

func GetPreviousVersion(currentVersion string) (int64, int64) {
currentIndex := -1
currentMajor, currentMinor, _ := SplitVersion(currentVersion)

for index, version := range versions {
major, minor, _ := SplitVersion(version)

if currentMajor == major && currentMinor == minor {
currentIndex = index
}

if currentIndex >= 0 {
if currentMajor != major || currentMinor != minor {
return major, minor
}
}
}

return 0, 0
}

func IsCurrentVersion(versionToCheck string) bool {
currentMajor, currentMinor, _ := SplitVersion(CurrentVersion)
toCheckMajor, toCheckMinor, _ := SplitVersion(versionToCheck)

if toCheckMajor == currentMajor && toCheckMinor == currentMinor {
return true
} else {
return false
}
}

func IsPreviousVersion(versionToCheck string) bool {
toCheckMajor, toCheckMinor, _ := SplitVersion(versionToCheck)
prevMajor, prevMinor := GetPreviousVersion(CurrentVersion)

if toCheckMajor == prevMajor && toCheckMinor == prevMinor {
return true
} else {
return false
}
}
74 changes: 74 additions & 0 deletions model/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved.
// See License.txt for license information.

package model

import (
"fmt"
"testing"
)

func TestSplitVersion(t *testing.T) {
major1, minor1, patch1 := SplitVersion("junk")
if major1 != 0 || minor1 != 0 || patch1 != 0 {
t.Fatal()
}

major2, minor2, patch2 := SplitVersion("1.2.3")
if major2 != 1 || minor2 != 2 || patch2 != 3 {
t.Fatal()
}

major3, minor3, patch3 := SplitVersion("1.2")
if major3 != 1 || minor3 != 2 || patch3 != 0 {
t.Fatal()
}

major4, minor4, patch4 := SplitVersion("1")
if major4 != 1 || minor4 != 0 || patch4 != 0 {
t.Fatal()
}

major5, minor5, patch5 := SplitVersion("1.2.3.junkgoeswhere")
if major5 != 1 || minor5 != 2 || patch5 != 3 {
t.Fatal()
}
}

func TestGetPreviousVersion(t *testing.T) {
if major, minor := GetPreviousVersion("0.8.0"); major != 0 || minor != 7 {
t.Fatal(major, minor)
}

if major, minor := GetPreviousVersion("0.7.0"); major != 0 || minor != 6 {
t.Fatal(major, minor)
}

if major, minor := GetPreviousVersion("0.7.1"); major != 0 || minor != 6 {
t.Fatal(major, minor)
}

if major, minor := GetPreviousVersion("0.7111.1"); major != 0 || minor != 0 {
t.Fatal(major, minor)
}
}

func TestIsCurrentVersion(t *testing.T) {
major, minor, patch := SplitVersion(CurrentVersion)

if !IsCurrentVersion(CurrentVersion) {
t.Fatal()
}

if !IsCurrentVersion(fmt.Sprintf("%v.%v.%v", major, minor, patch+100)) {
t.Fatal()
}

if IsCurrentVersion(fmt.Sprintf("%v.%v.%v", major, minor+1, patch)) {
t.Fatal()
}

if IsCurrentVersion(fmt.Sprintf("%v.%v.%v", major+1, minor, patch)) {
t.Fatal()
}
}
1 change: 0 additions & 1 deletion store/sql_channel_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ func NewSqlChannelStore(sqlStore *SqlStore) ChannelStore {
}

func (s SqlChannelStore) UpgradeSchemaIfNeeded() {
s.CreateColumnIfNotExists("Channels", "CreatorId", "varchar(26)", "character varying(26)", "")
}

func (s SqlChannelStore) CreateIndexesIfNotExists() {
Expand Down
4 changes: 2 additions & 2 deletions store/sql_post_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,9 @@ func (s SqlPostStore) GetEtag(channelId string) StoreChannel {
var et etagPosts
err := s.GetReplica().SelectOne(&et, "SELECT Id, UpdateAt FROM Posts WHERE ChannelId = :ChannelId ORDER BY UpdateAt DESC LIMIT 1", map[string]interface{}{"ChannelId": channelId})
if err != nil {
result.Data = fmt.Sprintf("%v.0.%v", model.ETAG_ROOT_VERSION, model.GetMillis())
result.Data = fmt.Sprintf("%v.0.%v", model.CurrentVersion, model.GetMillis())
} else {
result.Data = fmt.Sprintf("%v.%v.%v", model.ETAG_ROOT_VERSION, et.Id, et.UpdateAt)
result.Data = fmt.Sprintf("%v.%v.%v", model.CurrentVersion, et.Id, et.UpdateAt)
}

storeChannel <- result
Expand Down
Loading

0 comments on commit 5a436fd

Please sign in to comment.