[MM-30436] Add support for configuration custom defaults (#16265)

* [MM-30436] Add support for configuration custom defaults

* Addressing review comments

* Addressing PR comments

* Soft fail if the configuration defaults cannot be read

* Directly print error in log message

* Fix linter

Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
Miguel de la Cruz
2020-11-16 15:25:48 +01:00
коммит произвёл GitHub
родитель c82c37a36e
Коммит 6c857a4525
18 изменённых файлов: 334 добавлений и 131 удалений

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

@@ -145,7 +145,7 @@ func getConfigStore(command *cobra.Command) (*config.Store, error) {
return nil, errors.Wrap(err, "failed to initialize i18n")
}
configStore, err := config.NewStore(getConfigDSN(command, config.GetEnvironment()), false)
configStore, err := config.NewStore(getConfigDSN(command, config.GetEnvironment()), false, nil)
if err != nil {
return nil, errors.Wrap(err, "failed to initialize config store")
}

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

@@ -543,9 +543,9 @@ func TestConfigMigrate(t *testing.T) {
sqlDSN := getDsn(*sqlSettings.DriverName, *sqlSettings.DataSource)
fileDSN := "config.json"
ds, err := config.NewStore(sqlDSN, false)
ds, err := config.NewStore(sqlDSN, false, nil)
require.NoError(t, err)
fs, err := config.NewStore(fileDSN, false)
fs, err := config.NewStore(fileDSN, false, nil)
require.NoError(t, err)
defer ds.Close()

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

@@ -32,7 +32,7 @@ func InitDBCommandContext(configDSN string) (*app.App, error) {
model.AppErrorInit(utils.T)
s, err := app.NewServer(
app.Config(configDSN, false),
app.Config(configDSN, false, nil),
app.StartSearchEngine,
)
if err != nil {

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

@@ -37,7 +37,7 @@ func TestPlugin(t *testing.T) {
fs, err := config.NewFileStore(th.ConfigPath(), false)
require.Nil(t, err)
cfsStore, err := config.NewStoreFromBacking(fs)
cfsStore, err := config.NewStoreFromBacking(fs, nil)
require.Nil(t, err)
require.NotNil(t, cfsStore.Get().PluginSettings.PluginStates["testplugin"])
assert.True(t, cfsStore.Get().PluginSettings.PluginStates["testplugin"].Enable)
@@ -47,7 +47,7 @@ func TestPlugin(t *testing.T) {
assert.Contains(t, output, "Disabled plugin: testplugin")
fs, err = config.NewFileStore(th.ConfigPath(), false)
require.Nil(t, err)
cfsStore, err = config.NewStoreFromBacking(fs)
cfsStore, err = config.NewStoreFromBacking(fs, nil)
require.Nil(t, err)
require.NotNil(t, cfsStore.Get().PluginSettings.PluginStates["testplugin"])
assert.False(t, cfsStore.Get().PluginSettings.PluginStates["testplugin"].Enable)

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

@@ -4,6 +4,7 @@
package commands
import (
"encoding/json"
"net"
"os"
"os/signal"
@@ -15,6 +16,7 @@ import (
"github.com/mattermost/mattermost-server/v5/config"
"github.com/mattermost/mattermost-server/v5/manualtesting"
"github.com/mattermost/mattermost-server/v5/mlog"
"github.com/mattermost/mattermost-server/v5/model"
"github.com/mattermost/mattermost-server/v5/utils"
"github.com/mattermost/mattermost-server/v5/web"
"github.com/mattermost/mattermost-server/v5/wsapi"
@@ -22,6 +24,8 @@ import (
"github.com/spf13/cobra"
)
const CUSTOM_DEFAULTS_ENV_VAR = "MM_CUSTOM_DEFAULTS_PATH"
var serverCmd = &cobra.Command{
Use: "server",
Short: "Run the Mattermost server",
@@ -34,6 +38,27 @@ func init() {
RootCmd.RunE = serverCmdF
}
func loadCustomDefaults() (*model.Config, error) {
customDefaultsPath := os.Getenv(CUSTOM_DEFAULTS_ENV_VAR)
if customDefaultsPath == "" {
return nil, nil
}
file, err := os.Open(customDefaultsPath)
if err != nil {
return nil, errors.Wrapf(err, "unable to open custom defaults file at %q", customDefaultsPath)
}
defer file.Close()
var customDefaults *model.Config
err = json.NewDecoder(file).Decode(&customDefaults)
if err != nil {
return nil, errors.Wrap(err, "unable to decode custom defaults configuration")
}
return customDefaults, nil
}
func serverCmdF(command *cobra.Command, args []string) error {
disableConfigWatch, _ := command.Flags().GetBool("disableconfigwatch")
usedPlatform, _ := command.Flags().GetBool("platform")
@@ -41,9 +66,15 @@ func serverCmdF(command *cobra.Command, args []string) error {
interruptChan := make(chan os.Signal, 1)
if err := utils.TranslationsPreInit(); err != nil {
return errors.Wrapf(err, "unable to load Mattermost translation files")
return errors.Wrap(err, "unable to load Mattermost translation files")
}
configStore, err := config.NewStore(getConfigDSN(command, config.GetEnvironment()), !disableConfigWatch)
customDefaults, err := loadCustomDefaults()
if err != nil {
mlog.Error("Error loading custom configuration defaults: " + err.Error())
}
configStore, err := config.NewStore(getConfigDSN(command, config.GetEnvironment()), !disableConfigWatch, customDefaults)
if err != nil {
return errors.Wrap(err, "failed to load configuration")
}