Refactor path & config system (#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. ---- Close #24818 Close #24222 Close #21606 Close #21498 Close #25107 Close #24981 Maybe close #24503 Replace #23301 Replace #22754 And maybe more
This commit is contained in:
parent
1454f9dafc
commit
2cdf260f42
32 changed files with 652 additions and 461 deletions
|
@ -28,7 +28,6 @@ import (
|
|||
"code.gitea.io/gitea/modules/system"
|
||||
"code.gitea.io/gitea/modules/templates"
|
||||
"code.gitea.io/gitea/modules/translation"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
"code.gitea.io/gitea/modules/web"
|
||||
actions_router "code.gitea.io/gitea/routers/api/actions"
|
||||
packages_router "code.gitea.io/gitea/routers/api/packages"
|
||||
|
@ -101,21 +100,16 @@ func syncAppConfForGit(ctx context.Context) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// GlobalInitInstalled is for global installed configuration.
|
||||
func GlobalInitInstalled(ctx context.Context) {
|
||||
if !setting.InstallLock {
|
||||
log.Fatal("Gitea is not installed")
|
||||
}
|
||||
func InitWebInstallPage(ctx context.Context) {
|
||||
translation.InitLocales(ctx)
|
||||
setting.LoadSettingsForInstall()
|
||||
mustInit(svg.Init)
|
||||
}
|
||||
|
||||
// InitWebInstalled is for global installed configuration.
|
||||
func InitWebInstalled(ctx context.Context) {
|
||||
mustInitCtx(ctx, git.InitFull)
|
||||
log.Info("Gitea Version: %s%s", setting.AppVer, setting.AppBuiltWith)
|
||||
log.Info("Git Version: %s (home: %s)", git.VersionInfo(), git.HomeDir())
|
||||
log.Info("AppPath: %s", setting.AppPath)
|
||||
log.Info("AppWorkPath: %s", setting.AppWorkPath)
|
||||
log.Info("Custom path: %s", setting.CustomPath)
|
||||
log.Info("Log path: %s", setting.Log.RootPath)
|
||||
log.Info("Configuration file: %s", setting.CustomConf)
|
||||
log.Info("Run Mode: %s", util.ToTitleCase(setting.RunMode))
|
||||
log.Info("Git version: %s (home: %s)", git.VersionInfo(), git.HomeDir())
|
||||
|
||||
// Setup i18n
|
||||
translation.InitLocales(ctx)
|
||||
|
|
|
@ -32,6 +32,7 @@ import (
|
|||
"code.gitea.io/gitea/modules/util"
|
||||
"code.gitea.io/gitea/modules/web"
|
||||
"code.gitea.io/gitea/modules/web/middleware"
|
||||
"code.gitea.io/gitea/routers/common"
|
||||
"code.gitea.io/gitea/services/forms"
|
||||
|
||||
"gitea.com/go-chi/session"
|
||||
|
@ -370,11 +371,16 @@ func SubmitInstall(ctx *context.Context) {
|
|||
}
|
||||
|
||||
// Save settings.
|
||||
cfg, err := setting.NewConfigProviderFromFile(&setting.Options{CustomConf: setting.CustomConf, AllowEmpty: true})
|
||||
cfg, err := setting.NewConfigProviderFromFile(setting.CustomConf)
|
||||
if err != nil {
|
||||
log.Error("Failed to load custom conf '%s': %v", setting.CustomConf, err)
|
||||
}
|
||||
|
||||
cfg.Section("").Key("APP_NAME").SetValue(form.AppName)
|
||||
cfg.Section("").Key("RUN_USER").SetValue(form.RunUser)
|
||||
cfg.Section("").Key("WORK_PATH").SetValue(setting.AppWorkPath)
|
||||
cfg.Section("").Key("RUN_MODE").SetValue("prod")
|
||||
|
||||
cfg.Section("database").Key("DB_TYPE").SetValue(setting.Database.Type.String())
|
||||
cfg.Section("database").Key("HOST").SetValue(setting.Database.Host)
|
||||
cfg.Section("database").Key("NAME").SetValue(setting.Database.Name)
|
||||
|
@ -386,9 +392,7 @@ func SubmitInstall(ctx *context.Context) {
|
|||
cfg.Section("database").Key("PATH").SetValue(setting.Database.Path)
|
||||
cfg.Section("database").Key("LOG_SQL").SetValue("false") // LOG_SQL is rarely helpful
|
||||
|
||||
cfg.Section("").Key("APP_NAME").SetValue(form.AppName)
|
||||
cfg.Section("repository").Key("ROOT").SetValue(form.RepoRootPath)
|
||||
cfg.Section("").Key("RUN_USER").SetValue(form.RunUser)
|
||||
cfg.Section("server").Key("SSH_DOMAIN").SetValue(form.Domain)
|
||||
cfg.Section("server").Key("DOMAIN").SetValue(form.Domain)
|
||||
cfg.Section("server").Key("HTTP_PORT").SetValue(form.HTTPPort)
|
||||
|
@ -450,8 +454,6 @@ func SubmitInstall(ctx *context.Context) {
|
|||
cfg.Section("service").Key("NO_REPLY_ADDRESS").SetValue(fmt.Sprint(form.NoReplyAddress))
|
||||
cfg.Section("cron.update_checker").Key("ENABLED").SetValue(fmt.Sprint(form.EnableUpdateChecker))
|
||||
|
||||
cfg.Section("").Key("RUN_MODE").SetValue("prod")
|
||||
|
||||
cfg.Section("session").Key("PROVIDER").SetValue("file")
|
||||
|
||||
cfg.Section("log").Key("MODE").MustString("console")
|
||||
|
@ -514,7 +516,13 @@ func SubmitInstall(ctx *context.Context) {
|
|||
// ---- All checks are passed
|
||||
|
||||
// Reload settings (and re-initialize database connection)
|
||||
reloadSettings(ctx)
|
||||
setting.InitCfgProvider(setting.CustomConf)
|
||||
setting.LoadCommonSettings()
|
||||
setting.MustInstalled()
|
||||
setting.LoadDBSetting()
|
||||
if err := common.InitDBEngine(ctx); err != nil {
|
||||
log.Fatal("ORM engine initialization failed: %v", err)
|
||||
}
|
||||
|
||||
// Create admin account
|
||||
if len(form.AdminName) > 0 {
|
||||
|
|
|
@ -1,51 +0,0 @@
|
|||
// Copyright 2021 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package install
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/modules/svg"
|
||||
"code.gitea.io/gitea/modules/translation"
|
||||
"code.gitea.io/gitea/routers/common"
|
||||
)
|
||||
|
||||
// PreloadSettings preloads the configuration to check if we need to run install
|
||||
func PreloadSettings(ctx context.Context) bool {
|
||||
setting.Init(&setting.Options{
|
||||
AllowEmpty: true,
|
||||
})
|
||||
if !setting.InstallLock {
|
||||
log.Info("AppPath: %s", setting.AppPath)
|
||||
log.Info("AppWorkPath: %s", setting.AppWorkPath)
|
||||
log.Info("Custom path: %s", setting.CustomPath)
|
||||
log.Info("Log path: %s", setting.Log.RootPath)
|
||||
log.Info("Configuration file: %s", setting.CustomConf)
|
||||
log.Info("Prepare to run install page")
|
||||
translation.InitLocales(ctx)
|
||||
if setting.EnableSQLite3 {
|
||||
log.Info("SQLite3 is supported")
|
||||
}
|
||||
|
||||
setting.LoadSettingsForInstall()
|
||||
_ = svg.Init()
|
||||
}
|
||||
|
||||
return !setting.InstallLock
|
||||
}
|
||||
|
||||
// reloadSettings reloads the existing settings and starts up the database
|
||||
func reloadSettings(ctx context.Context) {
|
||||
setting.Init(&setting.Options{})
|
||||
setting.LoadDBSetting()
|
||||
if setting.InstallLock {
|
||||
if err := common.InitDBEngine(ctx); err == nil {
|
||||
log.Info("ORM engine initialization successful!")
|
||||
} else {
|
||||
log.Fatal("ORM engine initialization failed: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -8,7 +8,6 @@ import (
|
|||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
|
@ -167,20 +166,6 @@ func Config(ctx *context.Context) {
|
|||
ctx.Data["SessionConfig"] = sessionCfg
|
||||
|
||||
ctx.Data["Git"] = setting.Git
|
||||
|
||||
type envVar struct {
|
||||
Name, Value string
|
||||
}
|
||||
|
||||
envVars := map[string]*envVar{}
|
||||
if len(os.Getenv("GITEA_WORK_DIR")) > 0 {
|
||||
envVars["GITEA_WORK_DIR"] = &envVar{"GITEA_WORK_DIR", os.Getenv("GITEA_WORK_DIR")}
|
||||
}
|
||||
if len(os.Getenv("GITEA_CUSTOM")) > 0 {
|
||||
envVars["GITEA_CUSTOM"] = &envVar{"GITEA_CUSTOM", os.Getenv("GITEA_CUSTOM")}
|
||||
}
|
||||
|
||||
ctx.Data["EnvVars"] = envVars
|
||||
ctx.Data["AccessLogTemplate"] = setting.Log.AccessLogTemplate
|
||||
ctx.Data["LogSQL"] = setting.Database.LogSQL
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue