mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-26 20:27:03 +01:00
2cdf260f42
# 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. ---- Close #24818 Close #24222 Close #21606 Close #21498 Close #25107 Close #24981 Maybe close #24503 Replace #23301 Replace #22754 And maybe more
124 lines
4.4 KiB
Go
124 lines
4.4 KiB
Go
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package doctor
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
"code.gitea.io/gitea/modules/setting"
|
|
)
|
|
|
|
type configurationFile struct {
|
|
Name string
|
|
Path string
|
|
IsDirectory bool
|
|
Required bool
|
|
Writable bool
|
|
}
|
|
|
|
func checkConfigurationFile(logger log.Logger, autofix bool, fileOpts configurationFile) error {
|
|
logger.Info(`%-26s %q`, log.NewColoredValue(fileOpts.Name+":", log.Reset), fileOpts.Path)
|
|
fi, err := os.Stat(fileOpts.Path)
|
|
if err != nil {
|
|
if os.IsNotExist(err) && autofix && fileOpts.IsDirectory {
|
|
if err := os.MkdirAll(fileOpts.Path, 0o777); err != nil {
|
|
logger.Error(" Directory does not exist and could not be created. ERROR: %v", err)
|
|
return fmt.Errorf("Configuration directory: \"%q\" does not exist and could not be created. ERROR: %w", fileOpts.Path, err)
|
|
}
|
|
fi, err = os.Stat(fileOpts.Path)
|
|
}
|
|
}
|
|
if err != nil {
|
|
if fileOpts.Required {
|
|
logger.Error(" Is REQUIRED but is not accessible. ERROR: %v", err)
|
|
return fmt.Errorf("Configuration file \"%q\" is not accessible but is required. Error: %w", fileOpts.Path, err)
|
|
}
|
|
logger.Warn(" NOTICE: is not accessible (Error: %v)", err)
|
|
// this is a non-critical error
|
|
return nil
|
|
}
|
|
|
|
if fileOpts.IsDirectory && !fi.IsDir() {
|
|
logger.Error(" ERROR: not a directory")
|
|
return fmt.Errorf("Configuration directory \"%q\" is not a directory. Error: %w", fileOpts.Path, err)
|
|
} else if !fileOpts.IsDirectory && !fi.Mode().IsRegular() {
|
|
logger.Error(" ERROR: not a regular file")
|
|
return fmt.Errorf("Configuration file \"%q\" is not a regular file. Error: %w", fileOpts.Path, err)
|
|
} else if fileOpts.Writable {
|
|
if err := isWritableDir(fileOpts.Path); err != nil {
|
|
logger.Error(" ERROR: is required to be writable but is not writable: %v", err)
|
|
return fmt.Errorf("Configuration file \"%q\" is required to be writable but is not. Error: %w", fileOpts.Path, err)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func checkConfigurationFiles(ctx context.Context, logger log.Logger, autofix bool) error {
|
|
if fi, err := os.Stat(setting.CustomConf); err != nil || !fi.Mode().IsRegular() {
|
|
logger.Error("Failed to find configuration file at '%s'.", setting.CustomConf)
|
|
logger.Error("If you've never ran Gitea yet, this is normal and '%s' will be created for you on first run.", setting.CustomConf)
|
|
logger.Error("Otherwise check that you are running this command from the correct path and/or provide a `--config` parameter.")
|
|
logger.Critical("Cannot proceed without a configuration file")
|
|
return err
|
|
}
|
|
|
|
setting.MustInstalled()
|
|
|
|
configurationFiles := []configurationFile{
|
|
{"Configuration File Path", setting.CustomConf, false, true, false},
|
|
{"Repository Root Path", setting.RepoRootPath, true, true, true},
|
|
{"Data Root Path", setting.AppDataPath, true, true, true},
|
|
{"Custom File Root Path", setting.CustomPath, true, false, false},
|
|
{"Work directory", setting.AppWorkPath, true, true, false},
|
|
{"Log Root Path", setting.Log.RootPath, true, true, true},
|
|
}
|
|
|
|
if !setting.HasBuiltinBindata {
|
|
configurationFiles = append(configurationFiles, configurationFile{"Static File Root Path", setting.StaticRootPath, true, true, false})
|
|
}
|
|
|
|
numberOfErrors := 0
|
|
for _, configurationFile := range configurationFiles {
|
|
if err := checkConfigurationFile(logger, autofix, configurationFile); err != nil {
|
|
numberOfErrors++
|
|
}
|
|
}
|
|
|
|
if numberOfErrors > 0 {
|
|
logger.Critical("Please check your configuration files and try again.")
|
|
return fmt.Errorf("%d configuration files with errors", numberOfErrors)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func isWritableDir(path string) error {
|
|
// There's no platform-independent way of checking if a directory is writable
|
|
// https://stackoverflow.com/questions/20026320/how-to-tell-if-folder-exists-and-is-writable
|
|
|
|
tmpFile, err := os.CreateTemp(path, "doctors-order")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := os.Remove(tmpFile.Name()); err != nil {
|
|
fmt.Printf("Warning: can't remove temporary file: '%s'\n", tmpFile.Name()) //nolint:forbidigo
|
|
}
|
|
tmpFile.Close()
|
|
return nil
|
|
}
|
|
|
|
func init() {
|
|
Register(&Check{
|
|
Title: "Check paths and basic configuration",
|
|
Name: "paths",
|
|
IsDefault: true,
|
|
Run: checkConfigurationFiles,
|
|
AbortIfFailed: true,
|
|
SkipDatabaseInitialization: true,
|
|
Priority: 1,
|
|
})
|
|
}
|