[MM-32234] Fix config.json getting reset (#16805)

* Add support for read-only config stores

* Allow read/write config store for plugin commands that require it
Этот коммит содержится в:
Claudio Costa
2021-01-28 20:04:17 +01:00
коммит произвёл GitHub
родитель 77da23e84b
Коммит 9c9fbe6dd9
22 изменённых файлов: 211 добавлений и 92 удалений

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

@@ -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, nil)
configStore, err := config.NewStore(getConfigDSN(command, config.GetEnvironment()), false, 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, nil)
ds, err := config.NewStore(sqlDSN, false, false, nil)
require.NoError(t, err)
fs, err := config.NewStore(fileDSN, false, nil)
fs, err := config.NewStore(fileDSN, false, false, nil)
require.NoError(t, err)
defer ds.Close()

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

@@ -57,7 +57,7 @@ func initDbCmdF(command *cobra.Command, _ []string) error {
return errors.Wrap(err, "error loading custom configuration defaults")
}
configStore, err := config.NewStore(getConfigDSN(command, config.GetEnvironment()), false, customDefaults)
configStore, err := config.NewStore(getConfigDSN(command, config.GetEnvironment()), false, false, customDefaults)
if err != nil {
return errors.Wrap(err, "failed to load configuration")
}

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

@@ -12,8 +12,8 @@ import (
"github.com/mattermost/mattermost-server/v5/utils"
)
func InitDBCommandContextCobra(command *cobra.Command) (*app.App, error) {
a, err := InitDBCommandContext(getConfigDSN(command, config.GetEnvironment()))
func initDBCommandContextCobra(command *cobra.Command, readOnlyConfigStore bool) (*app.App, error) {
a, err := initDBCommandContext(getConfigDSN(command, config.GetEnvironment()), readOnlyConfigStore)
if err != nil {
// Returning an error just prints the usage message, so actually panic
panic(err)
@@ -25,14 +25,22 @@ func InitDBCommandContextCobra(command *cobra.Command) (*app.App, error) {
return a, nil
}
func InitDBCommandContext(configDSN string) (*app.App, error) {
func InitDBCommandContextCobra(command *cobra.Command) (*app.App, error) {
return initDBCommandContextCobra(command, true)
}
func InitDBCommandContextCobraReadWrite(command *cobra.Command) (*app.App, error) {
return initDBCommandContextCobra(command, false)
}
func initDBCommandContext(configDSN string, readOnlyConfigStore bool) (*app.App, error) {
if err := utils.TranslationsPreInit(); err != nil {
return nil, err
}
model.AppErrorInit(utils.T)
s, err := app.NewServer(
app.Config(configDSN, false, nil),
app.Config(configDSN, false, readOnlyConfigStore, nil),
app.StartSearchEngine,
)
if err != nil {

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

@@ -34,7 +34,7 @@ func jobserverCmdF(command *cobra.Command, args []string) error {
noSchedule, _ := command.Flags().GetBool("noschedule")
// Initialize
a, err := InitDBCommandContext(getConfigDSN(command, config.GetEnvironment()))
a, err := initDBCommandContext(getConfigDSN(command, config.GetEnvironment()), false)
if err != nil {
return err
}

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

@@ -104,7 +104,7 @@ func init() {
}
func pluginAddCmdF(command *cobra.Command, args []string) error {
a, err := InitDBCommandContextCobra(command)
a, err := InitDBCommandContextCobraReadWrite(command)
if err != nil {
return err
}
@@ -134,7 +134,7 @@ func pluginAddCmdF(command *cobra.Command, args []string) error {
}
func pluginDeleteCmdF(command *cobra.Command, args []string) error {
a, err := InitDBCommandContextCobra(command)
a, err := InitDBCommandContextCobraReadWrite(command)
if err != nil {
return err
}
@@ -158,7 +158,7 @@ func pluginDeleteCmdF(command *cobra.Command, args []string) error {
}
func pluginEnableCmdF(command *cobra.Command, args []string) error {
a, err := InitDBCommandContextCobra(command)
a, err := InitDBCommandContextCobraReadWrite(command)
if err != nil {
return err
}
@@ -182,7 +182,7 @@ func pluginEnableCmdF(command *cobra.Command, args []string) error {
}
func pluginDisableCmdF(command *cobra.Command, args []string) error {
a, err := InitDBCommandContextCobra(command)
a, err := InitDBCommandContextCobraReadWrite(command)
if err != nil {
return err
}
@@ -265,7 +265,7 @@ func pluginPublicKeysCmdF(command *cobra.Command, args []string) error {
}
func pluginAddPublicKeyCmdF(command *cobra.Command, args []string) error {
a, err := InitDBCommandContextCobra(command)
a, err := InitDBCommandContextCobraReadWrite(command)
if err != nil {
return err
}
@@ -296,7 +296,7 @@ func pluginAddPublicKeyCmdF(command *cobra.Command, args []string) error {
}
func pluginDeletePublicKeyCmdF(command *cobra.Command, args []string) error {
a, err := InitDBCommandContextCobra(command)
a, err := InitDBCommandContextCobraReadWrite(command)
if err != nil {
return err
}

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

@@ -38,7 +38,7 @@ func TestPlugin(t *testing.T) {
fs, err := config.NewFileStore(th.ConfigPath(), false)
require.Nil(t, err)
cfsStore, err := config.NewStoreFromBacking(fs, nil)
cfsStore, err := config.NewStoreFromBacking(fs, nil, false)
require.Nil(t, err)
require.NotNil(t, cfsStore.Get().PluginSettings.PluginStates["testplugin"])
assert.True(t, cfsStore.Get().PluginSettings.PluginStates["testplugin"].Enable)
@@ -48,7 +48,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, nil)
cfsStore, err = config.NewStoreFromBacking(fs, nil, false)
require.Nil(t, err)
require.NotNil(t, cfsStore.Get().PluginSettings.PluginStates["testplugin"])
assert.False(t, cfsStore.Get().PluginSettings.PluginStates["testplugin"].Enable)

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

@@ -50,7 +50,7 @@ func serverCmdF(command *cobra.Command, args []string) error {
mlog.Warn("Error loading custom configuration defaults: " + err.Error())
}
configStore, err := config.NewStore(getConfigDSN(command, config.GetEnvironment()), !disableConfigWatch, customDefaults)
configStore, err := config.NewStore(getConfigDSN(command, config.GetEnvironment()), !disableConfigWatch, false, customDefaults)
if err != nil {
return errors.Wrap(err, "failed to load configuration")
}