1
0
Fork 0
forked from forgejo/forgejo

Refactor path & config system (#25330) (#25416)

Backport #25330

# The problem

There were many "path tricks":

* By default, Gitea uses its program directory as its work path
* Gitea tries to use the "work path" to guess its "custom path" and
"custom conf (app.ini)"
* Users might want to use other directories as work path
* The non-default work path should be passed to Gitea by GITEA_WORK_DIR
or "--work-path"
* But some Gitea processes are started without these values
    * The "serv" process started by OpenSSH server
    * The CLI sub-commands started by site admin
* The paths are guessed by SetCustomPathAndConf again and again
* The default values of "work path / custom path / custom conf" can be
changed when compiling

# The solution

* Use `InitWorkPathAndCommonConfig` to handle these path tricks, and use
test code to cover its behaviors.
* When Gitea's web server runs, write the WORK_PATH to "app.ini", this
value must be the most correct one, because if this value is not right,
users would find that the web UI doesn't work and then they should be
able to fix it.
* Then all other sub-commands can use the WORK_PATH in app.ini to
initialize their paths.
* By the way, when Gitea starts for git protocol, it shouldn't output
any log, otherwise the git protocol gets broken and client blocks
forever.

The "work path" priority is: WORK_PATH in app.ini > cmd arg --work-path
> env var GITEA_WORK_DIR > builtin default

The "app.ini" searching order is: cmd arg --config > cmd arg "work path
/ custom path" > env var "work path / custom path" > builtin default


## ⚠️ BREAKING

If your instance's "work path / custom path / custom conf" doesn't meet
the requirements (eg: work path must be absolute), Gitea will report a
fatal error and exit. You need to set these values according to the
error log.
This commit is contained in:
wxiaoguang 2023-06-23 00:27:18 +08:00 committed by GitHub
parent 734fd93f59
commit 061b68e995
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
31 changed files with 651 additions and 460 deletions

View file

@ -55,15 +55,15 @@ type ConfigProvider interface {
DisableSaving()
PrepareSaving() (ConfigProvider, error)
IsLoadedFromEmpty() bool
}
type iniConfigProvider struct {
opts *Options
file string
ini *ini.File
disableSaving bool
newFile bool // whether the file has not existed previously
disableSaving bool // disable the "Save" method because the config options could be polluted
loadedFromEmpty bool // whether the file has not existed previously
}
type iniConfigSection struct {
@ -182,53 +182,43 @@ func NewConfigProviderFromData(configContent string) (ConfigProvider, error) {
}
cfg.NameMapper = ini.SnackCase
return &iniConfigProvider{
ini: cfg,
newFile: true,
ini: cfg,
loadedFromEmpty: true,
}, nil
}
type Options struct {
CustomConf string // the ini file path
AllowEmpty bool // whether not finding configuration files is allowed
ExtraConfig string
DisableLoadCommonSettings bool // only used by "Init()", not used by "NewConfigProvider()"
}
// NewConfigProviderFromFile load configuration from file.
// NOTE: do not print any log except error.
func NewConfigProviderFromFile(opts *Options) (ConfigProvider, error) {
func NewConfigProviderFromFile(file string, extraConfigs ...string) (ConfigProvider, error) {
cfg := ini.Empty(ini.LoadOptions{KeyValueDelimiterOnWrite: " = "})
newFile := true
loadedFromEmpty := true
if opts.CustomConf != "" {
isFile, err := util.IsFile(opts.CustomConf)
if file != "" {
isFile, err := util.IsFile(file)
if err != nil {
return nil, fmt.Errorf("unable to check if %s is a file. Error: %v", opts.CustomConf, err)
return nil, fmt.Errorf("unable to check if %q is a file. Error: %v", file, err)
}
if isFile {
if err := cfg.Append(opts.CustomConf); err != nil {
return nil, fmt.Errorf("failed to load custom conf '%s': %v", opts.CustomConf, err)
if err = cfg.Append(file); err != nil {
return nil, fmt.Errorf("failed to load config file %q: %v", file, err)
}
newFile = false
loadedFromEmpty = false
}
}
if newFile && !opts.AllowEmpty {
return nil, fmt.Errorf("unable to find configuration file: %q, please ensure you are running in the correct environment or set the correct configuration file with -c", CustomConf)
}
if opts.ExtraConfig != "" {
if err := cfg.Append([]byte(opts.ExtraConfig)); err != nil {
return nil, fmt.Errorf("unable to append more config: %v", err)
if len(extraConfigs) > 0 {
for _, s := range extraConfigs {
if err := cfg.Append([]byte(s)); err != nil {
return nil, fmt.Errorf("unable to append more config: %v", err)
}
}
}
cfg.NameMapper = ini.SnackCase
return &iniConfigProvider{
opts: opts,
ini: cfg,
newFile: newFile,
file: file,
ini: cfg,
loadedFromEmpty: loadedFromEmpty,
}, nil
}
@ -266,20 +256,17 @@ func (p *iniConfigProvider) Save() error {
if p.disableSaving {
return errDisableSaving
}
filename := p.opts.CustomConf
filename := p.file
if filename == "" {
if !p.opts.AllowEmpty {
return fmt.Errorf("custom config path must not be empty")
}
return nil
return fmt.Errorf("config file path must not be empty")
}
if p.newFile {
if p.loadedFromEmpty {
if err := os.MkdirAll(filepath.Dir(filename), os.ModePerm); err != nil {
return fmt.Errorf("failed to create '%s': %v", filename, err)
return fmt.Errorf("failed to create %q: %v", filename, err)
}
}
if err := p.ini.SaveTo(filename); err != nil {
return fmt.Errorf("failed to save '%s': %v", filename, err)
return fmt.Errorf("failed to save %q: %v", filename, err)
}
// Change permissions to be more restrictive
@ -313,11 +300,14 @@ func (p *iniConfigProvider) DisableSaving() {
// it makes the "Save" outputs a lot of garbage options
// After the INI package gets refactored, no "MustXxx" pollution, this workaround can be dropped.
func (p *iniConfigProvider) PrepareSaving() (ConfigProvider, error) {
cfgFile := p.opts.CustomConf
if cfgFile == "" {
if p.file == "" {
return nil, errors.New("no config file to save")
}
return NewConfigProviderFromFile(p.opts)
return NewConfigProviderFromFile(p.file)
}
func (p *iniConfigProvider) IsLoadedFromEmpty() bool {
return p.loadedFromEmpty
}
func mustMapSetting(rootCfg ConfigProvider, sectionName string, setting any) {
@ -356,8 +346,8 @@ func NewConfigProviderForLocale(source any, others ...any) (ConfigProvider, erro
}
iniFile.BlockMode = false
return &iniConfigProvider{
ini: iniFile,
newFile: true,
ini: iniFile,
loadedFromEmpty: true,
}, nil
}