diff options
author | Joram Wilander <jwawilander@gmail.com> | 2017-02-28 16:39:25 -0500 |
---|---|---|
committer | Harrison Healey <harrisonmhealey@gmail.com> | 2017-02-28 16:39:25 -0500 |
commit | 5e9adddb6f19a8d18d568871559495ea51b401ae (patch) | |
tree | adb7f82df01611d56f6f31b6e870afd0540a1b71 /app | |
parent | 5a3bc43668353e41949d2d48c8956eaa9d061860 (diff) | |
download | chat-5e9adddb6f19a8d18d568871559495ea51b401ae.tar.gz chat-5e9adddb6f19a8d18d568871559495ea51b401ae.tar.bz2 chat-5e9adddb6f19a8d18d568871559495ea51b401ae.zip |
Reload license from DB for all cluster app servers (#5525)
* Reload license from DB for all cluster app servers
* Increase test timeout
Diffstat (limited to 'app')
-rw-r--r-- | app/admin.go | 1 | ||||
-rw-r--r-- | app/license.go | 99 |
2 files changed, 100 insertions, 0 deletions
diff --git a/app/admin.go b/app/admin.go index 00d60a802..c551da50c 100644 --- a/app/admin.go +++ b/app/admin.go @@ -90,6 +90,7 @@ func InvalidateAllCachesSkipSend() { store.ClearUserCaches() store.ClearPostCaches() store.ClearWebhookCaches() + LoadLicense() } func GetConfig() *model.Config { diff --git a/app/license.go b/app/license.go new file mode 100644 index 000000000..87b2d1b05 --- /dev/null +++ b/app/license.go @@ -0,0 +1,99 @@ +// Copyright (c) 2017 Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package app + +import ( + "strings" + + l4g "github.com/alecthomas/log4go" + "github.com/mattermost/platform/model" + "github.com/mattermost/platform/utils" +) + +func LoadLicense() { + utils.RemoveLicense() + + licenseId := "" + if result := <-Srv.Store.System().Get(); result.Err == nil { + props := result.Data.(model.StringMap) + licenseId = props[model.SYSTEM_ACTIVE_LICENSE_ID] + } + + if len(licenseId) != 26 { + l4g.Info(utils.T("mattermost.load_license.find.warn")) + return + } + + if result := <-Srv.Store.License().Get(licenseId); result.Err == nil { + record := result.Data.(*model.LicenseRecord) + utils.LoadLicense([]byte(record.Bytes)) + } else { + l4g.Info(utils.T("mattermost.load_license.find.warn")) + } +} + +func SaveLicense(licenseBytes []byte) (*model.License, *model.AppError) { + var license *model.License + + if success, licenseStr := utils.ValidateLicense(licenseBytes); success { + license = model.LicenseFromJson(strings.NewReader(licenseStr)) + + if result := <-Srv.Store.User().AnalyticsUniqueUserCount(""); result.Err != nil { + return nil, model.NewLocAppError("addLicense", "api.license.add_license.invalid_count.app_error", nil, result.Err.Error()) + } else { + uniqueUserCount := result.Data.(int64) + + if uniqueUserCount > int64(*license.Features.Users) { + return nil, model.NewLocAppError("addLicense", "api.license.add_license.unique_users.app_error", map[string]interface{}{"Users": *license.Features.Users, "Count": uniqueUserCount}, "") + } + } + + if ok := utils.SetLicense(license); !ok { + return nil, model.NewLocAppError("addLicense", model.EXPIRED_LICENSE_ERROR, nil, "") + } + + record := &model.LicenseRecord{} + record.Id = license.Id + record.Bytes = string(licenseBytes) + rchan := Srv.Store.License().Save(record) + + sysVar := &model.System{} + sysVar.Name = model.SYSTEM_ACTIVE_LICENSE_ID + sysVar.Value = license.Id + schan := Srv.Store.System().SaveOrUpdate(sysVar) + + if result := <-rchan; result.Err != nil { + RemoveLicense() + return nil, model.NewLocAppError("addLicense", "api.license.add_license.save.app_error", nil, "err="+result.Err.Error()) + } + + if result := <-schan; result.Err != nil { + RemoveLicense() + return nil, model.NewLocAppError("addLicense", "api.license.add_license.save_active.app_error", nil, "") + } + } else { + return nil, model.NewLocAppError("addLicense", model.INVALID_LICENSE_ERROR, nil, "") + } + + InvalidateAllCaches() + + return license, nil +} + +func RemoveLicense() *model.AppError { + utils.RemoveLicense() + + sysVar := &model.System{} + sysVar.Name = model.SYSTEM_ACTIVE_LICENSE_ID + sysVar.Value = "" + + if result := <-Srv.Store.System().SaveOrUpdate(sysVar); result.Err != nil { + utils.RemoveLicense() + return result.Err + } + + InvalidateAllCaches() + + return nil +} |