PLT-2561 Add commandline option to upload license file (#2757)

* Add commandline option to upload license file

* Remove unnecessary comment
Этот коммит содержится в:
Joram Wilander
2016-04-21 09:43:10 -04:00
коммит произвёл Harrison Healey
родитель 94d5a79342
Коммит cf1f3ba322
2 изменённых файлов: 95 добавлений и 30 удалений

Просмотреть файл

@@ -14,6 +14,11 @@ import (
"strings"
)
const (
EXPIRED_LICENSE_ERROR = "api.license.add_license.expired.app_error"
INVALID_LICENSE_ERROR = "api.license.add_license.invalid.app_error"
)
func InitLicense(r *mux.Router) {
l4g.Debug(utils.T("api.license.init.debug"))
@@ -78,33 +83,45 @@ func addLicense(c *Context, w http.ResponseWriter, r *http.Request) {
buf := bytes.NewBuffer(nil)
io.Copy(buf, file)
data := buf.Bytes()
if license, err := SaveLicense(buf.Bytes()); err != nil {
if err.Id == EXPIRED_LICENSE_ERROR {
c.LogAudit("failed - expired or non-started license")
} else if err.Id == INVALID_LICENSE_ERROR {
c.LogAudit("failed - invalid license")
} else {
c.LogAudit("failed - unable to save license")
}
c.Err = err
return
} else {
c.LogAudit("success")
w.Write([]byte(license.ToJson()))
}
}
func SaveLicense(licenseBytes []byte) (*model.License, *model.AppError) {
var license *model.License
if success, licenseStr := utils.ValidateLicense(data); success {
if success, licenseStr := utils.ValidateLicense(licenseBytes); success {
license = model.LicenseFromJson(strings.NewReader(licenseStr))
if result := <-Srv.Store.User().AnalyticsUniqueUserCount(""); result.Err != nil {
c.Err = model.NewLocAppError("addLicense", "api.license.add_license.invalid_count.app_error", nil, result.Err.Error())
return
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) {
c.Err = model.NewLocAppError("addLicense", "api.license.add_license.unique_users.app_error", map[string]interface{}{"Users": *license.Features.Users, "Count": uniqueUserCount}, "")
return
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 {
c.LogAudit("failed - expired or non-started license")
c.Err = model.NewLocAppError("addLicense", "api.license.add_license.expired.app_error", nil, "")
return
return nil, model.NewLocAppError("addLicense", EXPIRED_LICENSE_ERROR, nil, "")
}
record := &model.LicenseRecord{}
record.Id = license.Id
record.Bytes = string(data)
record.Bytes = string(licenseBytes)
rchan := Srv.Store.License().Save(record)
sysVar := &model.System{}
@@ -113,37 +130,26 @@ func addLicense(c *Context, w http.ResponseWriter, r *http.Request) {
schan := Srv.Store.System().SaveOrUpdate(sysVar)
if result := <-rchan; result.Err != nil {
c.Err = model.NewLocAppError("addLicense", "api.license.add_license.save.app_error", nil, "err="+result.Err.Error())
utils.RemoveLicense()
return
RemoveLicense()
return nil, model.NewLocAppError("addLicense", "api.license.add_license.save.app_error", nil, "err="+result.Err.Error())
}
if result := <-schan; result.Err != nil {
c.Err = model.NewLocAppError("addLicense", "api.license.add_license.save_active.app_error", nil, "")
utils.RemoveLicense()
return
RemoveLicense()
return nil, model.NewLocAppError("addLicense", "api.license.add_license.save_active.app_error", nil, "")
}
} else {
c.LogAudit("failed - invalid license")
c.Err = model.NewLocAppError("addLicense", "api.license.add_license.invalid.app_error", nil, "")
return
return nil, model.NewLocAppError("addLicense", INVALID_LICENSE_ERROR, nil, "")
}
c.LogAudit("success")
w.Write([]byte(license.ToJson()))
return license, nil
}
func removeLicense(c *Context, w http.ResponseWriter, r *http.Request) {
c.LogAudit("")
utils.RemoveLicense()
sysVar := &model.System{}
sysVar.Name = model.SYSTEM_ACTIVE_LICENSE_ID
sysVar.Value = ""
if result := <-Srv.Store.System().Update(sysVar); result.Err != nil {
c.Err = model.NewLocAppError("removeLicense", "api.license.remove_license.update.app_error", nil, "")
if err := RemoveLicense(); err != nil {
c.Err = err
return
}
@@ -152,6 +158,21 @@ func removeLicense(c *Context, w http.ResponseWriter, r *http.Request) {
w.Write([]byte(model.MapToJson(rdata)))
}
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
}
return nil
}
func getClientLicenceConfig(c *Context, w http.ResponseWriter, r *http.Request) {
etag := utils.GetClientLicenseEtag()
if HandleEtag(etag, w, r) {

Просмотреть файл

@@ -43,7 +43,9 @@ var flagCmdVersion bool
var flagCmdResetPassword bool
var flagCmdPermanentDeleteUser bool
var flagCmdPermanentDeleteTeam bool
var flagCmdUploadLicense bool
var flagConfigFile string
var flagLicenseFile string
var flagEmail string
var flagPassword string
var flagTeamName string
@@ -221,6 +223,7 @@ func parseCmds() {
}
flag.StringVar(&flagConfigFile, "config", "config.json", "")
flag.StringVar(&flagLicenseFile, "license", "", "")
flag.StringVar(&flagEmail, "email", "", "")
flag.StringVar(&flagPassword, "password", "", "")
flag.StringVar(&flagTeamName, "team_name", "", "")
@@ -233,6 +236,7 @@ func parseCmds() {
flag.BoolVar(&flagCmdResetPassword, "reset_password", false, "")
flag.BoolVar(&flagCmdPermanentDeleteUser, "permanent_delete_user", false, "")
flag.BoolVar(&flagCmdPermanentDeleteTeam, "permanent_delete_team", false, "")
flag.BoolVar(&flagCmdUploadLicense, "upload_license", false, "")
flag.Parse()
@@ -242,7 +246,8 @@ func parseCmds() {
flagCmdResetPassword ||
flagCmdVersion ||
flagCmdPermanentDeleteUser ||
flagCmdPermanentDeleteTeam)
flagCmdPermanentDeleteTeam ||
flagCmdUploadLicense)
}
func runCmds() {
@@ -253,6 +258,7 @@ func runCmds() {
cmdResetPassword()
cmdPermDeleteUser()
cmdPermDeleteTeam()
cmdUploadLicense()
}
func cmdCreateTeam() {
@@ -541,6 +547,37 @@ func cmdPermDeleteTeam() {
}
}
func cmdUploadLicense() {
if flagCmdUploadLicense {
if model.BuildEnterpriseReady != "true" {
fmt.Fprintln(os.Stderr, "build must be enterprise ready")
os.Exit(1)
}
if len(flagLicenseFile) == 0 {
fmt.Fprintln(os.Stderr, "flag needs an argument: -team_name")
flag.Usage()
os.Exit(1)
}
var fileBytes []byte
var err error
if fileBytes, err = ioutil.ReadFile(flagLicenseFile); err != nil {
l4g.Error("%v", err)
flushLogAndExit(1)
}
if _, err := api.SaveLicense(fileBytes); err != nil {
l4g.Error("%v", err)
flushLogAndExit(1)
} else {
flushLogAndExit(0)
}
os.Exit(0)
}
}
func flushLogAndExit(code int) {
l4g.Close()
time.Sleep(time.Second)
@@ -567,6 +604,8 @@ USAGE:
FLAGS:
-config="config.json" Path to the config file
-license="ex.mattermost-license" Path to your license file
-email="user@example.com" Email address used in other commands
-password="mypassword" Password used in other commands
@@ -619,6 +658,11 @@ COMMANDS:
Example:
platform -permanent_delete_team -team_name="name"
-upload_license Uploads a license to the server. Requires the -license flag.
Example:
platform -upload_license -license="/path/to/license/example.mattermost-license"
-version Display the current of the Mattermost platform
-help Displays this help page`