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

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

@@ -43,7 +43,9 @@ var flagCmdVersion bool
var flagCmdResetPassword bool var flagCmdResetPassword bool
var flagCmdPermanentDeleteUser bool var flagCmdPermanentDeleteUser bool
var flagCmdPermanentDeleteTeam bool var flagCmdPermanentDeleteTeam bool
var flagCmdUploadLicense bool
var flagConfigFile string var flagConfigFile string
var flagLicenseFile string
var flagEmail string var flagEmail string
var flagPassword string var flagPassword string
var flagTeamName string var flagTeamName string
@@ -221,6 +223,7 @@ func parseCmds() {
} }
flag.StringVar(&flagConfigFile, "config", "config.json", "") flag.StringVar(&flagConfigFile, "config", "config.json", "")
flag.StringVar(&flagLicenseFile, "license", "", "")
flag.StringVar(&flagEmail, "email", "", "") flag.StringVar(&flagEmail, "email", "", "")
flag.StringVar(&flagPassword, "password", "", "") flag.StringVar(&flagPassword, "password", "", "")
flag.StringVar(&flagTeamName, "team_name", "", "") flag.StringVar(&flagTeamName, "team_name", "", "")
@@ -233,6 +236,7 @@ func parseCmds() {
flag.BoolVar(&flagCmdResetPassword, "reset_password", false, "") flag.BoolVar(&flagCmdResetPassword, "reset_password", false, "")
flag.BoolVar(&flagCmdPermanentDeleteUser, "permanent_delete_user", false, "") flag.BoolVar(&flagCmdPermanentDeleteUser, "permanent_delete_user", false, "")
flag.BoolVar(&flagCmdPermanentDeleteTeam, "permanent_delete_team", false, "") flag.BoolVar(&flagCmdPermanentDeleteTeam, "permanent_delete_team", false, "")
flag.BoolVar(&flagCmdUploadLicense, "upload_license", false, "")
flag.Parse() flag.Parse()
@@ -242,7 +246,8 @@ func parseCmds() {
flagCmdResetPassword || flagCmdResetPassword ||
flagCmdVersion || flagCmdVersion ||
flagCmdPermanentDeleteUser || flagCmdPermanentDeleteUser ||
flagCmdPermanentDeleteTeam) flagCmdPermanentDeleteTeam ||
flagCmdUploadLicense)
} }
func runCmds() { func runCmds() {
@@ -253,6 +258,7 @@ func runCmds() {
cmdResetPassword() cmdResetPassword()
cmdPermDeleteUser() cmdPermDeleteUser()
cmdPermDeleteTeam() cmdPermDeleteTeam()
cmdUploadLicense()
} }
func cmdCreateTeam() { 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) { func flushLogAndExit(code int) {
l4g.Close() l4g.Close()
time.Sleep(time.Second) time.Sleep(time.Second)
@@ -567,6 +604,8 @@ USAGE:
FLAGS: FLAGS:
-config="config.json" Path to the config file -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 -email="user@example.com" Email address used in other commands
-password="mypassword" Password used in other commands -password="mypassword" Password used in other commands
@@ -619,6 +658,11 @@ COMMANDS:
Example: Example:
platform -permanent_delete_team -team_name="name" 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 -version Display the current of the Mattermost platform
-help Displays this help page` -help Displays this help page`