2017-04-12 08:27:57 -04:00
|
|
|
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
|
2017-01-25 09:32:42 -05:00
|
|
|
// See License.txt for license information.
|
|
|
|
|
|
|
|
|
|
package app
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
"net/http"
|
2017-09-19 18:31:35 -05:00
|
|
|
"sync"
|
2017-10-02 03:50:56 -05:00
|
|
|
"time"
|
2017-09-11 10:02:02 -05:00
|
|
|
|
2017-09-19 18:31:35 -05:00
|
|
|
"github.com/mattermost/mattermost-server/einterfaces"
|
2017-09-29 04:29:29 -05:00
|
|
|
ejobs "github.com/mattermost/mattermost-server/einterfaces/jobs"
|
|
|
|
|
"github.com/mattermost/mattermost-server/jobs"
|
2017-09-21 04:13:34 -05:00
|
|
|
"github.com/mattermost/mattermost-server/model"
|
2017-09-11 10:02:02 -05:00
|
|
|
"github.com/mattermost/mattermost-server/plugin/pluginenv"
|
2017-09-21 04:13:34 -05:00
|
|
|
"github.com/mattermost/mattermost-server/utils"
|
2017-01-25 09:32:42 -05:00
|
|
|
)
|
|
|
|
|
|
2017-09-06 17:12:54 -05:00
|
|
|
type App struct {
|
2017-09-19 18:31:35 -05:00
|
|
|
Srv *Server
|
|
|
|
|
|
2017-09-12 14:12:29 -05:00
|
|
|
PluginEnv *pluginenv.Environment
|
|
|
|
|
PluginConfigListenerId string
|
2017-09-19 18:31:35 -05:00
|
|
|
|
2017-09-27 11:52:34 -05:00
|
|
|
EmailBatching *EmailBatchingJob
|
|
|
|
|
|
|
|
|
|
Hubs []*Hub
|
|
|
|
|
HubsStopCheckingForDeadlock chan bool
|
|
|
|
|
|
2017-09-29 04:29:29 -05:00
|
|
|
Jobs *jobs.JobServer
|
|
|
|
|
|
2017-09-19 18:31:35 -05:00
|
|
|
AccountMigration einterfaces.AccountMigrationInterface
|
|
|
|
|
Brand einterfaces.BrandInterface
|
|
|
|
|
Cluster einterfaces.ClusterInterface
|
|
|
|
|
Compliance einterfaces.ComplianceInterface
|
|
|
|
|
Elasticsearch einterfaces.ElasticsearchInterface
|
|
|
|
|
Ldap einterfaces.LdapInterface
|
|
|
|
|
Metrics einterfaces.MetricsInterface
|
|
|
|
|
Mfa einterfaces.MfaInterface
|
|
|
|
|
Saml einterfaces.SamlInterface
|
2017-09-06 17:12:54 -05:00
|
|
|
}
|
|
|
|
|
|
2017-09-29 04:29:29 -05:00
|
|
|
var globalApp App = App{
|
|
|
|
|
Jobs: &jobs.JobServer{},
|
|
|
|
|
}
|
2017-09-06 17:12:54 -05:00
|
|
|
|
2017-10-02 03:50:56 -05:00
|
|
|
var appCount = 0
|
2017-09-19 18:31:35 -05:00
|
|
|
var initEnterprise sync.Once
|
|
|
|
|
|
2017-10-02 03:50:56 -05:00
|
|
|
var UseGlobalApp = true
|
|
|
|
|
|
|
|
|
|
// New creates a new App. You must call Shutdown when you're done with it.
|
|
|
|
|
// XXX: Doesn't necessarily create a new App yet.
|
|
|
|
|
func New() *App {
|
|
|
|
|
appCount++
|
|
|
|
|
|
|
|
|
|
if !UseGlobalApp {
|
|
|
|
|
if appCount > 1 {
|
|
|
|
|
panic("Only one App should exist at a time. Did you forget to call Shutdown()?")
|
|
|
|
|
}
|
|
|
|
|
app := &App{
|
|
|
|
|
Jobs: &jobs.JobServer{},
|
|
|
|
|
}
|
|
|
|
|
app.initEnterprise()
|
|
|
|
|
return app
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-19 18:31:35 -05:00
|
|
|
initEnterprise.Do(func() {
|
2017-09-21 04:13:34 -05:00
|
|
|
globalApp.initEnterprise()
|
2017-09-19 18:31:35 -05:00
|
|
|
})
|
2017-09-06 17:12:54 -05:00
|
|
|
return &globalApp
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-02 03:50:56 -05:00
|
|
|
func (a *App) Shutdown() {
|
|
|
|
|
appCount--
|
|
|
|
|
if appCount == 0 {
|
|
|
|
|
// XXX: This is to give all of our runaway goroutines time to complete.
|
|
|
|
|
// We should wrangle them up and remove this.
|
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
|
|
|
|
|
|
if a.Srv != nil {
|
|
|
|
|
a.StopServer()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-21 04:13:34 -05:00
|
|
|
var accountMigrationInterface func(*App) einterfaces.AccountMigrationInterface
|
|
|
|
|
|
|
|
|
|
func RegisterAccountMigrationInterface(f func(*App) einterfaces.AccountMigrationInterface) {
|
|
|
|
|
accountMigrationInterface = f
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var clusterInterface func(*App) einterfaces.ClusterInterface
|
|
|
|
|
|
|
|
|
|
func RegisterClusterInterface(f func(*App) einterfaces.ClusterInterface) {
|
|
|
|
|
clusterInterface = f
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var complianceInterface func(*App) einterfaces.ComplianceInterface
|
|
|
|
|
|
|
|
|
|
func RegisterComplianceInterface(f func(*App) einterfaces.ComplianceInterface) {
|
|
|
|
|
complianceInterface = f
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-29 04:29:29 -05:00
|
|
|
var jobsDataRetentionInterface func(*App) ejobs.DataRetentionInterface
|
|
|
|
|
|
|
|
|
|
func RegisterJobsDataRetentionInterface(f func(*App) ejobs.DataRetentionInterface) {
|
|
|
|
|
jobsDataRetentionInterface = f
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var jobsElasticsearchAggregatorInterface func(*App) ejobs.ElasticsearchAggregatorInterface
|
|
|
|
|
|
|
|
|
|
func RegisterJobsElasticsearchAggregatorInterface(f func(*App) ejobs.ElasticsearchAggregatorInterface) {
|
|
|
|
|
jobsElasticsearchAggregatorInterface = f
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var jobsElasticsearchIndexerInterface func(*App) ejobs.ElasticsearchIndexerInterface
|
|
|
|
|
|
|
|
|
|
func RegisterJobsElasticsearchIndexerInterface(f func(*App) ejobs.ElasticsearchIndexerInterface) {
|
|
|
|
|
jobsElasticsearchIndexerInterface = f
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var jobsLdapSyncInterface func(*App) ejobs.LdapSyncInterface
|
|
|
|
|
|
|
|
|
|
func RegisterJobsLdapSyncInterface(f func(*App) ejobs.LdapSyncInterface) {
|
|
|
|
|
jobsLdapSyncInterface = f
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-21 04:13:34 -05:00
|
|
|
var ldapInterface func(*App) einterfaces.LdapInterface
|
|
|
|
|
|
|
|
|
|
func RegisterLdapInterface(f func(*App) einterfaces.LdapInterface) {
|
|
|
|
|
ldapInterface = f
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var metricsInterface func(*App) einterfaces.MetricsInterface
|
|
|
|
|
|
|
|
|
|
func RegisterMetricsInterface(f func(*App) einterfaces.MetricsInterface) {
|
|
|
|
|
metricsInterface = f
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var mfaInterface func(*App) einterfaces.MfaInterface
|
|
|
|
|
|
|
|
|
|
func RegisterMfaInterface(f func(*App) einterfaces.MfaInterface) {
|
|
|
|
|
mfaInterface = f
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var samlInterface func(*App) einterfaces.SamlInterface
|
|
|
|
|
|
|
|
|
|
func RegisterSamlInterface(f func(*App) einterfaces.SamlInterface) {
|
|
|
|
|
samlInterface = f
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *App) initEnterprise() {
|
|
|
|
|
if accountMigrationInterface != nil {
|
|
|
|
|
a.AccountMigration = accountMigrationInterface(a)
|
|
|
|
|
}
|
|
|
|
|
a.Brand = einterfaces.GetBrandInterface()
|
|
|
|
|
if clusterInterface != nil {
|
|
|
|
|
a.Cluster = clusterInterface(a)
|
|
|
|
|
}
|
|
|
|
|
if complianceInterface != nil {
|
|
|
|
|
a.Compliance = complianceInterface(a)
|
|
|
|
|
}
|
|
|
|
|
a.Elasticsearch = einterfaces.GetElasticsearchInterface()
|
|
|
|
|
if ldapInterface != nil {
|
|
|
|
|
a.Ldap = ldapInterface(a)
|
|
|
|
|
utils.AddConfigListener(func(_, cfg *model.Config) {
|
|
|
|
|
if err := utils.ValidateLdapFilter(cfg, a.Ldap); err != nil {
|
|
|
|
|
panic(utils.T(err.Id))
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
if metricsInterface != nil {
|
|
|
|
|
a.Metrics = metricsInterface(a)
|
|
|
|
|
}
|
|
|
|
|
if mfaInterface != nil {
|
|
|
|
|
a.Mfa = mfaInterface(a)
|
|
|
|
|
}
|
|
|
|
|
if samlInterface != nil {
|
|
|
|
|
a.Saml = samlInterface(a)
|
|
|
|
|
utils.AddConfigListener(func(_, cfg *model.Config) {
|
|
|
|
|
a.Saml.ConfigureSP()
|
|
|
|
|
})
|
|
|
|
|
}
|
2017-09-29 04:29:29 -05:00
|
|
|
|
|
|
|
|
if jobsDataRetentionInterface != nil {
|
|
|
|
|
a.Jobs.DataRetention = jobsDataRetentionInterface(a)
|
|
|
|
|
}
|
|
|
|
|
if jobsElasticsearchAggregatorInterface != nil {
|
|
|
|
|
a.Jobs.ElasticsearchAggregator = jobsElasticsearchAggregatorInterface(a)
|
|
|
|
|
}
|
|
|
|
|
if jobsElasticsearchIndexerInterface != nil {
|
|
|
|
|
a.Jobs.ElasticsearchIndexer = jobsElasticsearchIndexerInterface(a)
|
|
|
|
|
}
|
|
|
|
|
if jobsLdapSyncInterface != nil {
|
|
|
|
|
a.Jobs.LdapSync = jobsLdapSyncInterface(a)
|
|
|
|
|
}
|
2017-09-21 04:13:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *App) Config() *model.Config {
|
|
|
|
|
return utils.Cfg
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-25 09:32:42 -05:00
|
|
|
func CloseBody(r *http.Response) {
|
|
|
|
|
if r.Body != nil {
|
|
|
|
|
ioutil.ReadAll(r.Body)
|
|
|
|
|
r.Body.Close()
|
|
|
|
|
}
|
|
|
|
|
}
|