From c5ff560dcfc2699316483ee8c6d031d83d8a47f2 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Tue, 23 Nov 2021 20:47:21 +0800 Subject: [PATCH 01/35] Improve install code to avoid low-level mistakes. If a user tries to do a re-install in a Gitea database, they gets a warning and double check. When Gitea runs, it never create empty app.ini automatically. --- models/db/engine.go | 59 ++++++++------------ models/unittest/testdb.go | 3 +- modules/setting/setting.go | 44 ++++++--------- modules/setting/setting_i18n.go | 50 +++++++++++++++++ options/locale/locale_en-US.ini | 7 +++ routers/install/install.go | 99 +++++++++++++++++++++++++-------- services/forms/user_form.go | 5 ++ templates/base/head.tmpl | 2 + templates/install.tmpl | 22 ++++++++ web_src/less/_install.less | 29 +++++++--- 10 files changed, 227 insertions(+), 93 deletions(-) create mode 100644 modules/setting/setting_i18n.go diff --git a/models/db/engine.go b/models/db/engine.go index 0f744d027e2b7..b74495290a32f 100755 --- a/models/db/engine.go +++ b/models/db/engine.go @@ -8,7 +8,6 @@ package db import ( "context" "database/sql" - "errors" "fmt" "io" "reflect" @@ -126,40 +125,46 @@ func SyncAllTables() error { return x.StoreEngine("InnoDB").Sync2(tables...) } -// InitEngine sets the xorm.Engine -func InitEngine(ctx context.Context) (err error) { - x, err = NewEngine() +// InitEngine initializes the xorm.Engine and set it as db.DefaultContext +func InitEngine(ctx context.Context) error { + eng, err := NewEngine() if err != nil { return fmt.Errorf("Failed to connect to database: %v", err) } - x.SetMapper(names.GonicMapper{}) + eng.SetMapper(names.GonicMapper{}) // WARNING: for serv command, MUST remove the output to os.stdout, // so use log file to instead print to stdout. - x.SetLogger(NewXORMLogger(setting.Database.LogSQL)) - x.ShowSQL(setting.Database.LogSQL) - x.SetMaxOpenConns(setting.Database.MaxOpenConns) - x.SetMaxIdleConns(setting.Database.MaxIdleConns) - x.SetConnMaxLifetime(setting.Database.ConnMaxLifetime) + eng.SetLogger(NewXORMLogger(setting.Database.LogSQL)) + eng.ShowSQL(setting.Database.LogSQL) + eng.SetMaxOpenConns(setting.Database.MaxOpenConns) + eng.SetMaxIdleConns(setting.Database.MaxIdleConns) + eng.SetConnMaxLifetime(setting.Database.ConnMaxLifetime) + eng.SetDefaultContext(ctx) + + SetDefaultEngine(ctx, eng) + return nil +} +// SetDefaultEngine sets the default engine for db +func SetDefaultEngine(ctx context.Context, eng *xorm.Engine) { + x = eng DefaultContext = &Context{ Context: ctx, e: x, } - x.SetDefaultContext(ctx) - return nil } -// SetEngine is used by unit test code -func SetEngine(eng *xorm.Engine) { - x = eng - DefaultContext = &Context{ - Context: context.Background(), - e: x, +// UnsetDefaultEngine closes and unsets the default engine +func UnsetDefaultEngine() { + if x != nil { + _ = x.Close() + x = nil } + DefaultContext = nil } -// InitEngineWithMigration initializes a new xorm.Engine +// InitEngineWithMigration initializes a new xorm.Engine and set it as db.DefaultContext // This function must never call .Sync2() if the provided migration function fails. // When called from the "doctor" command, the migration function is a version check // that prevents the doctor from fixing anything in the database if the migration level @@ -226,14 +231,6 @@ func NamesToBean(names ...string) ([]interface{}, error) { return beans, nil } -// Ping tests if database is alive -func Ping() error { - if x != nil { - return x.Ping() - } - return errors.New("database not configured") -} - // DumpDatabase dumps all data from database according the special database SQL syntax to file system. func DumpDatabase(filePath, dbType string) error { var tbs []*schemas.Table @@ -291,11 +288,3 @@ func GetMaxID(beanOrTableName interface{}) (maxID int64, err error) { _, err = x.Select("MAX(id)").Table(beanOrTableName).Get(&maxID) return } - -// FindByMaxID filled results as the condition from database -func FindByMaxID(maxID int64, limit int, results interface{}) error { - return x.Where("id <= ?", maxID). - OrderBy("id DESC"). - Limit(limit). - Find(results) -} diff --git a/models/unittest/testdb.go b/models/unittest/testdb.go index 8771bc1d2139a..94c93755e0bd6 100644 --- a/models/unittest/testdb.go +++ b/models/unittest/testdb.go @@ -5,6 +5,7 @@ package unittest import ( + "context" "fmt" "net/url" "os" @@ -124,7 +125,7 @@ func CreateTestEngine(opts FixturesOptions) error { return err } x.SetMapper(names.GonicMapper{}) - db.SetEngine(x) + db.SetDefaultEngine(context.Background(), x) if err = db.SyncAllTables(); err != nil { return err diff --git a/modules/setting/setting.go b/modules/setting/setting.go index 16ebde1791b7c..f4f0fcfe461cb 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -857,6 +857,10 @@ func NewContext() { SuccessfulTokensCacheSize = sec.Key("SUCCESSFUL_TOKENS_CACHE_SIZE").MustInt(20) InternalToken = loadInternalToken(sec) + if InstallLock && InternalToken == "" { + // if Gitea has been installed but the InternalToken hasn't been generated (upgrade from an old release), we should generate + GenerateSaveInternalToken() + } cfgdata := sec.Key("PASSWORD_COMPLEXITY").Strings(",") if len(cfgdata) == 0 { @@ -968,19 +972,11 @@ func NewContext() { Langs = Cfg.Section("i18n").Key("LANGS").Strings(",") if len(Langs) == 0 { - Langs = []string{ - "en-US", "zh-CN", "zh-HK", "zh-TW", "de-DE", "fr-FR", "nl-NL", "lv-LV", - "ru-RU", "uk-UA", "ja-JP", "es-ES", "pt-BR", "pt-PT", "pl-PL", "bg-BG", - "it-IT", "fi-FI", "tr-TR", "cs-CZ", "sr-SP", "sv-SE", "ko-KR", "el-GR", - "fa-IR", "hu-HU", "id-ID", "ml-IN"} + Langs = defaultI18nLangs() } Names = Cfg.Section("i18n").Key("NAMES").Strings(",") if len(Names) == 0 { - Names = []string{"English", "简体中文", "繁體中文(香港)", "繁體中文(台灣)", "Deutsch", - "français", "Nederlands", "latviešu", "русский", "Українська", "日本語", - "español", "português do Brasil", "Português de Portugal", "polski", "български", - "italiano", "suomi", "Türkçe", "čeština", "српски", "svenska", "한국어", "ελληνικά", - "فارسی", "magyar nyelv", "bahasa Indonesia", "മലയാളം"} + Names = defaultI18nNames() } ShowFooterBranding = Cfg.Section("other").Key("SHOW_FOOTER_BRANDING").MustBool(false) @@ -1047,8 +1043,8 @@ func parseAuthorizedPrincipalsAllow(values []string) ([]string, bool) { func loadInternalToken(sec *ini.Section) string { uri := sec.Key("INTERNAL_TOKEN_URI").String() - if len(uri) == 0 { - return loadOrGenerateInternalToken(sec) + if uri == "" { + return sec.Key("INTERNAL_TOKEN").String() } tempURI, err := url.Parse(uri) if err != nil { @@ -1085,21 +1081,17 @@ func loadInternalToken(sec *ini.Section) string { return "" } -func loadOrGenerateInternalToken(sec *ini.Section) string { - var err error - token := sec.Key("INTERNAL_TOKEN").String() - if len(token) == 0 { - token, err = generate.NewInternalToken() - if err != nil { - log.Fatal("Error generate internal token: %v", err) - } - - // Save secret - CreateOrAppendToCustomConf(func(cfg *ini.File) { - cfg.Section("security").Key("INTERNAL_TOKEN").SetValue(token) - }) +// GenerateSaveInternalToken generates and saves the internal token to app.ini +func GenerateSaveInternalToken() { + token, err := generate.NewInternalToken() + if err != nil { + log.Fatal("Error generate internal token: %v", err) } - return token + + InternalToken = token + CreateOrAppendToCustomConf(func(cfg *ini.File) { + cfg.Section("security").Key("INTERNAL_TOKEN").SetValue(token) + }) } // MakeAbsoluteAssetURL returns the absolute asset url prefix without a trailing slash diff --git a/modules/setting/setting_i18n.go b/modules/setting/setting_i18n.go new file mode 100644 index 0000000000000..127a10ad8a89f --- /dev/null +++ b/modules/setting/setting_i18n.go @@ -0,0 +1,50 @@ +// Copyright 2021 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package setting + +var defaultI18nLangMap = map[string]string{ + "en-US": "English", + "zh-CN": "简体中文", + "zh-HK": "繁體中文(香港)", + "zh-TW": "繁體中文(台灣)", + "de-DE": "Deutsch", + "fr-FR": "français", + "nl-NL": "Nederlands", + "lv-LV": "latviešu", + "ru-RU": "русский", + "uk-UA": "Українська", + "ja-JP": "日本語", + "es-ES": "español", + "pt-BR": "português do Brasil", + "pt-PT": "Português de Portugal", + "pl-PL": "polski", + "bg-BG": "български", + "it-IT": "italiano", + "fi-FI": "suomi", + "tr-TR": "Türkçe", + "cs-CZ": "čeština", + "sr-SP": "српски", + "sv-SE": "svenska", + "ko-KR": "한국어", + "el-GR": "ελληνικά", + "fa-IR": "فارسی", + "hu-HU": "magyar nyelv", + "id-ID": "bahasa Indonesia", + "ml-IN": "മലയാളം", +} + +func defaultI18nLangs() (res []string) { + for k := range defaultI18nLangMap { + res = append(res, k) + } + return +} + +func defaultI18nNames() (res []string) { + for _, v := range defaultI18nLangMap { + res = append(res, v) + } + return +} diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index ca6cd66625ed0..87041f632739f 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -138,6 +138,11 @@ ssl_mode = SSL charset = Charset path = Path sqlite_helper = File path for the SQLite3 database.
Enter an absolute path if you run Gitea as a service. +reinstall_error = You are trying to re-install in a Gitea database +reinstall_confirm_message = In most cases, you should use your existing "app.ini" to run Gitea instance, re-installing in a Gitea database would cause potential problems. If you know what you are doing, please mark these tips as checked to continue. +reinstall_confirm_check_1 = All secret keys encrypted by app.ini will be lost, users can not login via 2FA/OTP. +reinstall_confirm_check_2 = You may need to re-sync your repositories and settings. +reinstall_confirm_check_3 = I have lost my app.ini and I have to re-install, I know these risks. err_empty_db_path = The SQLite3 database path cannot be empty. no_admin_and_disable_registration = You cannot disable user self-registration without creating an administrator account. err_empty_admin_password = The administrator password cannot be empty. @@ -205,6 +210,8 @@ sqlite3_not_available = This Gitea version does not support SQLite3. Please down invalid_db_setting = The database settings are invalid: %v invalid_repo_path = The repository root path is invalid: %v run_user_not_match = The 'run as' username is not the current username: %s -> %s +internal_token_failed = Failed to generate internal token: %v +secret_key_failed = Failed to generate secret key: %v save_config_failed = Failed to save configuration: %v invalid_admin_setting = Administrator account setting is invalid: %v install_success = Welcome! Thank you for choosing Gitea. Have fun and take care! diff --git a/routers/install/install.go b/routers/install/install.go index 837467056dc13..1abfc335bf39a 100644 --- a/routers/install/install.go +++ b/routers/install/install.go @@ -161,10 +161,65 @@ func Install(ctx *context.Context) { ctx.HTML(http.StatusOK, tplInstall) } +func checkDatabase(ctx *context.Context, form *forms.InstallForm) bool { + var err error + + if (setting.Database.Type == "sqlite3") && + len(setting.Database.Path) == 0 { + ctx.Data["Err_DbPath"] = true + ctx.RenderWithErr(ctx.Tr("install.err_empty_db_path"), tplInstall, form) + return false + } + + // Check if the user is trying to re-install in an installed database + db.UnsetDefaultEngine() + defer db.UnsetDefaultEngine() + + if err = db.InitEngine(ctx); err != nil { + if strings.Contains(err.Error(), `Unknown database type: sqlite3`) { + ctx.Data["Err_DbType"] = true + ctx.RenderWithErr(ctx.Tr("install.sqlite3_not_available", "https://docs.gitea.io/en-us/install-from-binary/"), tplInstall, form) + } else { + ctx.Data["Err_DbSetting"] = true + ctx.RenderWithErr(ctx.Tr("install.invalid_db_setting", err), tplInstall, form) + } + return false + } + + e := db.GetEngine(db.DefaultContext) + _, err = e.Exec("SELECT 1") + if err != nil { + ctx.Data["Err_DbSetting"] = true + ctx.RenderWithErr(ctx.Tr("install.invalid_db_setting", err), tplInstall, form) + return false + } + + var installedDbVersion int64 + has, _ := e.Table("version").Cols("`version`").Get(&installedDbVersion) + if has && installedDbVersion > 0 { + log.Error("The database is likely to have been installed by a Gitea before, database migration version=%d", installedDbVersion) + confirmed := form.ReinstallConfirmFirst && form.ReinstallConfirmSecond && form.ReinstallConfirmThird + if !confirmed { + ctx.Data["Err_DbInstalledBefore"] = true + ctx.RenderWithErr(ctx.Tr("install.reinstall_error"), tplInstall, form) + return false + } + } + + return true +} + // SubmitInstall response for submit install items func SubmitInstall(ctx *context.Context) { - form := *web.GetForm(ctx).(*forms.InstallForm) var err error + + form := *web.GetForm(ctx).(*forms.InstallForm) + + // fix form values + if form.AppURL != "" && form.AppURL[len(form.AppURL)-1] != '/' { + form.AppURL += "/" + } + ctx.Data["CurDbOption"] = form.DbType if ctx.HasError() { @@ -186,9 +241,9 @@ func SubmitInstall(ctx *context.Context) { return } - // Pass basic check, now test configuration. - // Test database setting. + // ---- Basic checks are passed, now test configuration. + // Test database setting. setting.Database.Type = setting.GetDBTypeByName(form.DbType) setting.Database.Host = form.DbHost setting.Database.User = form.DbUser @@ -201,22 +256,7 @@ func SubmitInstall(ctx *context.Context) { setting.Database.LogSQL = !setting.IsProd setting.PasswordHashAlgo = form.PasswordAlgorithm - if (setting.Database.Type == "sqlite3") && - len(setting.Database.Path) == 0 { - ctx.Data["Err_DbPath"] = true - ctx.RenderWithErr(ctx.Tr("install.err_empty_db_path"), tplInstall, &form) - return - } - - // Set test engine. - if err = db.InitEngineWithMigration(ctx, migrations.Migrate); err != nil { - if strings.Contains(err.Error(), `Unknown database type: sqlite3`) { - ctx.Data["Err_DbType"] = true - ctx.RenderWithErr(ctx.Tr("install.sqlite3_not_available", "https://docs.gitea.io/en-us/install-from-binary/"), tplInstall, &form) - } else { - ctx.Data["Err_DbSetting"] = true - ctx.RenderWithErr(ctx.Tr("install.invalid_db_setting", err), tplInstall, &form) - } + if !checkDatabase(ctx, &form) { return } @@ -299,9 +339,14 @@ func SubmitInstall(ctx *context.Context) { } } - if form.AppURL[len(form.AppURL)-1] != '/' { - form.AppURL += "/" + // Init the engine with migration + if err = db.InitEngineWithMigration(ctx, migrations.Migrate); err != nil { + db.UnsetDefaultEngine() + ctx.Data["Err_DbSetting"] = true + ctx.RenderWithErr(ctx.Tr("install.invalid_db_setting", err), tplInstall, &form) + return } + db.UnsetDefaultEngine() // Save settings. cfg := ini.Empty() @@ -390,6 +435,14 @@ func SubmitInstall(ctx *context.Context) { cfg.Section("log").Key("ROUTER").SetValue("console") cfg.Section("security").Key("INSTALL_LOCK").SetValue("true") + + var internalToken string + if internalToken, err = generate.NewInternalToken(); err != nil { + ctx.RenderWithErr(ctx.Tr("install.internal_token_failed", err), tplInstall, &form) + return + } + cfg.Section("security").Key("INTERNAL_TOKEN").SetValue(internalToken) + var secretKey string if secretKey, err = generate.NewSecretKey(); err != nil { ctx.RenderWithErr(ctx.Tr("install.secret_key_failed", err), tplInstall, &form) @@ -411,7 +464,9 @@ func SubmitInstall(ctx *context.Context) { return } - // Re-read settings + // ---- All checks are passed + + // Reload settings (and re-initialize database connection) ReloadSettings(ctx) // Create admin account diff --git a/services/forms/user_form.go b/services/forms/user_form.go index 9f86bf61661f6..f114682b9f784 100644 --- a/services/forms/user_form.go +++ b/services/forms/user_form.go @@ -67,6 +67,11 @@ type InstallForm struct { AdminPasswd string `binding:"OmitEmpty;MaxSize(255)" locale:"install.admin_password"` AdminConfirmPasswd string AdminEmail string `binding:"OmitEmpty;MinSize(3);MaxSize(254);Include(@)" locale:"install.admin_email"` + + // ReinstallConfirmFirst we can not use 1/2/3 or A/B/C here, there is a framework bug, can not parse "reinstall_confirm_1" or "reinstall_confirm_a" + ReinstallConfirmFirst bool + ReinstallConfirmSecond bool + ReinstallConfirmThird bool } // Validate validates the fields diff --git a/templates/base/head.tmpl b/templates/base/head.tmpl index d529e6bfda76b..dd8e68761baa7 100644 --- a/templates/base/head.tmpl +++ b/templates/base/head.tmpl @@ -51,6 +51,8 @@ copy_error: '{{.i18n.Tr "copy_error"}}', } }; + {{/* in case some pages don't render the pageData, we make sure it is an object to prevent null access */}} + window.config.pageData = window.config.pageData || {}; diff --git a/templates/install.tmpl b/templates/install.tmpl index a004e19399c32..7a4b6387aa020 100644 --- a/templates/install.tmpl +++ b/templates/install.tmpl @@ -91,6 +91,28 @@ +
+

{{.i18n.Tr "install.reinstall_confirm_message"}}

+
+
+ + +
+
+
+
+ + +
+
+
+
+ + +
+
+
+

{{.i18n.Tr "install.general_title"}}

diff --git a/web_src/less/_install.less b/web_src/less/_install.less index 3f59a99aacaf3..fca59a89aaa56 100644 --- a/web_src/less/_install.less +++ b/web_src/less/_install.less @@ -4,7 +4,7 @@ form { @input-padding: 320px !important; - label { + .inline.field label { text-align: right; width: @input-padding; } @@ -20,19 +20,30 @@ margin-left: @input-padding+15px; } - &.optional .title { - margin-left: 38%; + &.optional { + .title { + margin-left: 38%; + } + .checkbox { + margin-left: 40% !important; + label { + width: auto !important; + } + } } } } .ui { - .checkbox { - margin-left: 40% !important; - - label { - width: auto !important; - } + .detail-message { + width: 70%; + margin: 20px auto; + color: red; + } + .reinstall-confirm { + width: 70%; + text-align: left; + margin: 10px auto; } } } From 2bd7628da42dadac4a1d808e8bcc149f50dc303b Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Tue, 23 Nov 2021 22:13:05 +0800 Subject: [PATCH 02/35] Check app.ini in cmd --- cmd/cmd.go | 17 ++++++++++++----- cmd/convert.go | 1 - cmd/doctor.go | 2 ++ cmd/dump_repo.go | 1 - cmd/migrate.go | 1 - cmd/migrate_storage.go | 1 - modules/private/internal.go | 7 ++++--- 7 files changed, 18 insertions(+), 12 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index b89dd5d8b8acd..9dccd4ea7d0a2 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -16,6 +16,7 @@ import ( "syscall" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/util" @@ -56,16 +57,22 @@ func confirm() (bool, error) { } } -func initDB(ctx context.Context) error { - return initDBDisableConsole(ctx, false) +func ensureInstallLock() { + if !setting.InstallLock { + log.Fatal("invalid app.ini (no installation flag), please use the correctly installed config file") + } } -func initDBDisableConsole(ctx context.Context, disableConsole bool) error { +func initDB(ctx context.Context) error { setting.NewContext() setting.InitDBConfig() - setting.NewXORMLogService(disableConsole) + setting.NewXORMLogService(false) + + if setting.Database.Type == "" { + return errors.New("invalid database settings in app.ini, please use the correctly installed config file") + } if err := db.InitEngine(ctx); err != nil { - return fmt.Errorf("models.SetEngine: %v", err) + return fmt.Errorf("cmd.initDB: %v", err) } return nil } diff --git a/cmd/convert.go b/cmd/convert.go index 0b2e240b32f43..6d4d99a255040 100644 --- a/cmd/convert.go +++ b/cmd/convert.go @@ -35,7 +35,6 @@ func runConvert(ctx *cli.Context) error { log.Info("Custom path: %s", setting.CustomPath) log.Info("Log path: %s", setting.LogRootPath) log.Info("Configuration file: %s", setting.CustomConf) - setting.InitDBConfig() if !setting.Database.UseMySQL { fmt.Println("This command can only be used with a MySQL database") diff --git a/cmd/doctor.go b/cmd/doctor.go index 27f91e41bb2b3..05f04814dea31 100644 --- a/cmd/doctor.go +++ b/cmd/doctor.go @@ -82,6 +82,8 @@ You should back-up your database before doing this and ensure that your database } func runRecreateTable(ctx *cli.Context) error { + ensureInstallLock() + // Redirect the default golog to here golog.SetFlags(0) golog.SetPrefix("") diff --git a/cmd/dump_repo.go b/cmd/dump_repo.go index 31f4574c2d53c..1a9344dbe18f0 100644 --- a/cmd/dump_repo.go +++ b/cmd/dump_repo.go @@ -88,7 +88,6 @@ func runDumpRepository(ctx *cli.Context) error { log.Info("Custom path: %s", setting.CustomPath) log.Info("Log path: %s", setting.LogRootPath) log.Info("Configuration file: %s", setting.CustomConf) - setting.InitDBConfig() var ( serviceType structs.GitServiceType diff --git a/cmd/migrate.go b/cmd/migrate.go index 054772d9ec4cc..49a13adeb5672 100644 --- a/cmd/migrate.go +++ b/cmd/migrate.go @@ -36,7 +36,6 @@ func runMigrate(ctx *cli.Context) error { log.Info("Custom path: %s", setting.CustomPath) log.Info("Log path: %s", setting.LogRootPath) log.Info("Configuration file: %s", setting.CustomConf) - setting.InitDBConfig() if err := db.InitEngineWithMigration(context.Background(), migrations.Migrate); err != nil { log.Fatal("Failed to initialize ORM engine: %v", err) diff --git a/cmd/migrate_storage.go b/cmd/migrate_storage.go index 9f1d9057f9ccd..8d0a1a2001dc6 100644 --- a/cmd/migrate_storage.go +++ b/cmd/migrate_storage.go @@ -120,7 +120,6 @@ func runMigrateStorage(ctx *cli.Context) error { log.Info("Custom path: %s", setting.CustomPath) log.Info("Log path: %s", setting.LogRootPath) log.Info("Configuration file: %s", setting.CustomConf) - setting.InitDBConfig() if err := db.InitEngineWithMigration(context.Background(), migrations.Migrate); err != nil { log.Fatal("Failed to initialize ORM engine: %v", err) diff --git a/modules/private/internal.go b/modules/private/internal.go index f5b5db0ca1b50..da706f26f4b0e 100644 --- a/modules/private/internal.go +++ b/modules/private/internal.go @@ -8,6 +8,7 @@ import ( "context" "crypto/tls" "fmt" + "log" "net" "net/http" @@ -17,6 +18,9 @@ import ( ) func newRequest(ctx context.Context, url, method string) *httplib.Request { + if setting.InternalToken == "" { + log.Fatal("no internal token, can not send internal request to server. please use correct app.ini") + } return httplib.NewRequest(url, method). SetContext(ctx). Header("Authorization", @@ -44,9 +48,6 @@ func newInternalRequest(ctx context.Context, url, method string) *httplib.Reques }) if setting.Protocol == setting.UnixSocket { req.SetTransport(&http.Transport{ - Dial: func(_, _ string) (net.Conn, error) { - return net.Dial("unix", setting.HTTPAddr) - }, DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) { var d net.Dialer return d.DialContext(ctx, "unix", setting.HTTPAddr) From fc2404a2ddcfc58cd78136afaf7e36d21f8b6013 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Tue, 23 Nov 2021 22:45:24 +0800 Subject: [PATCH 03/35] Fix setting i18n order --- modules/setting/setting_i18n.go | 67 +++++++++++++++++---------------- 1 file changed, 34 insertions(+), 33 deletions(-) diff --git a/modules/setting/setting_i18n.go b/modules/setting/setting_i18n.go index 127a10ad8a89f..113e1b558596d 100644 --- a/modules/setting/setting_i18n.go +++ b/modules/setting/setting_i18n.go @@ -4,47 +4,48 @@ package setting -var defaultI18nLangMap = map[string]string{ - "en-US": "English", - "zh-CN": "简体中文", - "zh-HK": "繁體中文(香港)", - "zh-TW": "繁體中文(台灣)", - "de-DE": "Deutsch", - "fr-FR": "français", - "nl-NL": "Nederlands", - "lv-LV": "latviešu", - "ru-RU": "русский", - "uk-UA": "Українська", - "ja-JP": "日本語", - "es-ES": "español", - "pt-BR": "português do Brasil", - "pt-PT": "Português de Portugal", - "pl-PL": "polski", - "bg-BG": "български", - "it-IT": "italiano", - "fi-FI": "suomi", - "tr-TR": "Türkçe", - "cs-CZ": "čeština", - "sr-SP": "српски", - "sv-SE": "svenska", - "ko-KR": "한국어", - "el-GR": "ελληνικά", - "fa-IR": "فارسی", - "hu-HU": "magyar nyelv", - "id-ID": "bahasa Indonesia", - "ml-IN": "മലയാളം", +// defaultI18nLangNames must be a slice, we need the order +var defaultI18nLangNames = []string{ + "en-US", "English", + "zh-CN", "简体中文", + "zh-HK", "繁體中文(香港)", + "zh-TW", "繁體中文(台灣)", + "de-DE", "Deutsch", + "fr-FR", "français", + "nl-NL", "Nederlands", + "lv-LV", "latviešu", + "ru-RU", "русский", + "uk-UA", "Українська", + "ja-JP", "日本語", + "es-ES", "español", + "pt-BR", "português do Brasil", + "pt-PT", "Português de Portugal", + "pl-PL", "polski", + "bg-BG", "български", + "it-IT", "italiano", + "fi-FI", "suomi", + "tr-TR", "Türkçe", + "cs-CZ", "čeština", + "sr-SP", "српски", + "sv-SE", "svenska", + "ko-KR", "한국어", + "el-GR", "ελληνικά", + "fa-IR", "فارسی", + "hu-HU", "magyar nyelv", + "id-ID", "bahasa Indonesia", + "ml-IN", "മലയാളം", } func defaultI18nLangs() (res []string) { - for k := range defaultI18nLangMap { - res = append(res, k) + for i := 0; i < len(defaultI18nLangNames); i += 2 { + res = append(res, defaultI18nLangNames[i]) } return } func defaultI18nNames() (res []string) { - for _, v := range defaultI18nLangMap { - res = append(res, v) + for i := 0; i < len(defaultI18nLangNames); i += 2 { + res = append(res, defaultI18nLangNames[i+1]) } return } From 384a32af6d3ed265553afc2859a878424cb3bba9 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Wed, 24 Nov 2021 01:17:31 +0800 Subject: [PATCH 04/35] fix --- options/locale/locale_en-US.ini | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 87041f632739f..6c1346fa71195 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -140,8 +140,8 @@ path = Path sqlite_helper = File path for the SQLite3 database.
Enter an absolute path if you run Gitea as a service. reinstall_error = You are trying to re-install in a Gitea database reinstall_confirm_message = In most cases, you should use your existing "app.ini" to run Gitea instance, re-installing in a Gitea database would cause potential problems. If you know what you are doing, please mark these tips as checked to continue. -reinstall_confirm_check_1 = All secret keys encrypted by app.ini will be lost, users can not login via 2FA/OTP. -reinstall_confirm_check_2 = You may need to re-sync your repositories and settings. +reinstall_confirm_check_1 = The data in database encrypted by the secret keys in app.ini will be lost, users may not be able to log in with 2FA/OTP. +reinstall_confirm_check_2 = The repositories and settings may need to be re-synchronize. reinstall_confirm_check_3 = I have lost my app.ini and I have to re-install, I know these risks. err_empty_db_path = The SQLite3 database path cannot be empty. no_admin_and_disable_registration = You cannot disable user self-registration without creating an administrator account. From 86fec99bfbc4ef1909e9d6198a0a5fd5f2476941 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Wed, 24 Nov 2021 01:25:14 +0800 Subject: [PATCH 05/35] fix --- options/locale/locale_en-US.ini | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 6c1346fa71195..087d4167fcb2b 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -138,9 +138,9 @@ ssl_mode = SSL charset = Charset path = Path sqlite_helper = File path for the SQLite3 database.
Enter an absolute path if you run Gitea as a service. -reinstall_error = You are trying to re-install in a Gitea database -reinstall_confirm_message = In most cases, you should use your existing "app.ini" to run Gitea instance, re-installing in a Gitea database would cause potential problems. If you know what you are doing, please mark these tips as checked to continue. -reinstall_confirm_check_1 = The data in database encrypted by the secret keys in app.ini will be lost, users may not be able to log in with 2FA/OTP. +reinstall_error = You are trying to re-install in an existing Gitea database +reinstall_confirm_message = In most cases, you should use your existing "app.ini" to run Gitea instance. Re-installing in an existing Gitea database would cause potential problems. If you know what you are doing, please mark these tips as checked to continue. +reinstall_confirm_check_1 = The data encrypted by the secret keys in app.ini will be lost, users may not be able to log in with 2FA/OTP. reinstall_confirm_check_2 = The repositories and settings may need to be re-synchronize. reinstall_confirm_check_3 = I have lost my app.ini and I have to re-install, I know these risks. err_empty_db_path = The SQLite3 database path cannot be empty. From 2721c0c56cbe6ed5a0daa5048656a819fbe838b2 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Wed, 24 Nov 2021 01:28:04 +0800 Subject: [PATCH 06/35] fix --- options/locale/locale_en-US.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 087d4167fcb2b..f0c496e9ed748 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -139,7 +139,7 @@ charset = Charset path = Path sqlite_helper = File path for the SQLite3 database.
Enter an absolute path if you run Gitea as a service. reinstall_error = You are trying to re-install in an existing Gitea database -reinstall_confirm_message = In most cases, you should use your existing "app.ini" to run Gitea instance. Re-installing in an existing Gitea database would cause potential problems. If you know what you are doing, please mark these tips as checked to continue. +reinstall_confirm_message = In most cases, you should use your existing "app.ini" to run Gitea. Re-installing in an existing Gitea database would cause potential problems. If you know what you are doing, mark these tips as checked to continue. reinstall_confirm_check_1 = The data encrypted by the secret keys in app.ini will be lost, users may not be able to log in with 2FA/OTP. reinstall_confirm_check_2 = The repositories and settings may need to be re-synchronize. reinstall_confirm_check_3 = I have lost my app.ini and I have to re-install, I know these risks. From 0bf4489c037ffbebf2a01ee3199d784e9079793b Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Wed, 24 Nov 2021 01:44:19 +0800 Subject: [PATCH 07/35] fix typo --- options/locale/locale_en-US.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index f0c496e9ed748..728820e2caec9 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -141,7 +141,7 @@ sqlite_helper = File path for the SQLite3 database.
Enter an absolute path if reinstall_error = You are trying to re-install in an existing Gitea database reinstall_confirm_message = In most cases, you should use your existing "app.ini" to run Gitea. Re-installing in an existing Gitea database would cause potential problems. If you know what you are doing, mark these tips as checked to continue. reinstall_confirm_check_1 = The data encrypted by the secret keys in app.ini will be lost, users may not be able to log in with 2FA/OTP. -reinstall_confirm_check_2 = The repositories and settings may need to be re-synchronize. +reinstall_confirm_check_2 = The repositories and settings may need to be re-synchronized. reinstall_confirm_check_3 = I have lost my app.ini and I have to re-install, I know these risks. err_empty_db_path = The SQLite3 database path cannot be empty. no_admin_and_disable_registration = You cannot disable user self-registration without creating an administrator account. From 61e17ca18c540c6f4346d565e155dca1f7518863 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Wed, 24 Nov 2021 09:29:35 +0800 Subject: [PATCH 08/35] Update models/db/engine.go Co-authored-by: zeripath --- models/db/engine.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/db/engine.go b/models/db/engine.go index b74495290a32f..096f484184127 100755 --- a/models/db/engine.go +++ b/models/db/engine.go @@ -164,7 +164,7 @@ func UnsetDefaultEngine() { DefaultContext = nil } -// InitEngineWithMigration initializes a new xorm.Engine and set it as db.DefaultContext +// InitEngineWithMigration initializes a new xorm.Engine and sets it as the db.DefaultContext // This function must never call .Sync2() if the provided migration function fails. // When called from the "doctor" command, the migration function is a version check // that prevents the doctor from fixing anything in the database if the migration level From 6c9564a1c2e697baf28d1f2979a7a4448b91edbb Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Wed, 24 Nov 2021 11:53:46 +0800 Subject: [PATCH 09/35] refactor database package, check SECRET_KEY to prevent overwriting --- models/db/engine.go | 29 +++++++++++--------- models/db/install/dbinstall.go | 40 ++++++++++++++++++++++++++++ models/migrations/migrations_test.go | 21 +++++---------- options/locale/locale_en-US.ini | 4 +-- routers/install/install.go | 33 ++++++++++++----------- templates/install.tmpl | 10 ++++--- 6 files changed, 88 insertions(+), 49 deletions(-) create mode 100644 models/db/install/dbinstall.go diff --git a/models/db/engine.go b/models/db/engine.go index 096f484184127..b07fb9ff4c33e 100755 --- a/models/db/engine.go +++ b/models/db/engine.go @@ -91,8 +91,8 @@ func init() { } } -// NewEngine returns a new xorm engine from the configuration -func NewEngine() (*xorm.Engine, error) { +// newXORMEngine returns a new XORM engine from the configuration +func newXORMEngine() (*xorm.Engine, error) { connStr, err := setting.DBConnStr() if err != nil { return nil, err @@ -127,22 +127,22 @@ func SyncAllTables() error { // InitEngine initializes the xorm.Engine and set it as db.DefaultContext func InitEngine(ctx context.Context) error { - eng, err := NewEngine() + xormEngine, err := newXORMEngine() if err != nil { - return fmt.Errorf("Failed to connect to database: %v", err) + return fmt.Errorf("failed to connect to database: %v", err) } - eng.SetMapper(names.GonicMapper{}) + xormEngine.SetMapper(names.GonicMapper{}) // WARNING: for serv command, MUST remove the output to os.stdout, // so use log file to instead print to stdout. - eng.SetLogger(NewXORMLogger(setting.Database.LogSQL)) - eng.ShowSQL(setting.Database.LogSQL) - eng.SetMaxOpenConns(setting.Database.MaxOpenConns) - eng.SetMaxIdleConns(setting.Database.MaxIdleConns) - eng.SetConnMaxLifetime(setting.Database.ConnMaxLifetime) - eng.SetDefaultContext(ctx) - - SetDefaultEngine(ctx, eng) + xormEngine.SetLogger(NewXORMLogger(setting.Database.LogSQL)) + xormEngine.ShowSQL(setting.Database.LogSQL) + xormEngine.SetMaxOpenConns(setting.Database.MaxOpenConns) + xormEngine.SetMaxIdleConns(setting.Database.MaxIdleConns) + xormEngine.SetConnMaxLifetime(setting.Database.ConnMaxLifetime) + xormEngine.SetDefaultContext(ctx) + + SetDefaultEngine(ctx, xormEngine) return nil } @@ -156,6 +156,9 @@ func SetDefaultEngine(ctx context.Context, eng *xorm.Engine) { } // UnsetDefaultEngine closes and unsets the default engine +// We hope the SetDefaultEngine and UnsetDefaultEngine can be paired, but it's impossible now, +// there are many calls to InitEngine -> SetDefaultEngine directly to overwrite the `x` and DefaultContext without close +// Global database engine related functions are all racy and there is no graceful close right now. func UnsetDefaultEngine() { if x != nil { _ = x.Close() diff --git a/models/db/install/dbinstall.go b/models/db/install/dbinstall.go new file mode 100644 index 0000000000000..9f0c71be2eb7f --- /dev/null +++ b/models/db/install/dbinstall.go @@ -0,0 +1,40 @@ +// Copyright 2021 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package install + +import ( + "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/modules/setting" +) + +// CheckDatabaseConnection checks the database connection +func CheckDatabaseConnection() error { + e := db.GetEngine(db.DefaultContext) + _, err := e.Exec("SELECT 1") + return err +} + +// GetMigrationVersion gets the database migration version +func GetMigrationVersion() int64 { + var installedDbVersion int64 + e := db.GetEngine(db.DefaultContext) + // the error can be safely ignored, then we still get version=0 + _, _ = e.Table("version").Cols("`version`").Get(&installedDbVersion) + return installedDbVersion +} + +// HasPostInstallationUsers checks whether there are users after installation +func HasPostInstallationUsers() bool { + e := db.GetEngine(db.DefaultContext) + // the error can be ignored safely, if there is no user table, we still get count=0 + // if there are 2 or more users in database, we consider there are users created after installation + threshold := 2 + if !setting.IsProd { + // to debug easily, with non-prod RUN_MODE, we only check the count to 1 + threshold = 1 + } + res, _ := e.Table("user").Cols("id").Where("1=1").Limit(threshold).Query() + return len(res) >= threshold +} diff --git a/models/migrations/migrations_test.go b/models/migrations/migrations_test.go index f46070cf8e588..5ebabd5f0a4ff 100644 --- a/models/migrations/migrations_test.go +++ b/models/migrations/migrations_test.go @@ -5,6 +5,7 @@ package migrations import ( + "context" "database/sql" "fmt" "os" @@ -85,21 +86,11 @@ func removeAllWithRetry(dir string) error { return err } -// newEngine sets the xorm.Engine -func newEngine() (*xorm.Engine, error) { - x, err := db.NewEngine() - if err != nil { - return x, fmt.Errorf("Failed to connect to database: %v", err) +func newXORMEngine() (*xorm.Engine, error) { + if err := db.InitEngine(context.Background()); err != nil { + return nil, err } - - x.SetMapper(names.GonicMapper{}) - // WARNING: for serv command, MUST remove the output to os.stdout, - // so use log file to instead print to stdout. - x.SetLogger(db.NewXORMLogger(setting.Database.LogSQL)) - x.ShowSQL(setting.Database.LogSQL) - x.SetMaxOpenConns(setting.Database.MaxOpenConns) - x.SetMaxIdleConns(setting.Database.MaxIdleConns) - x.SetConnMaxLifetime(setting.Database.ConnMaxLifetime) + x := db.DefaultContext.(*db.Context).Engine().(*xorm.Engine) return x, nil } @@ -213,7 +204,7 @@ func prepareTestEnv(t *testing.T, skip int, syncModels ...interface{}) (*xorm.En return nil, deferFn } - x, err := newEngine() + x, err := newXORMEngine() assert.NoError(t, err) if x != nil { oldDefer := deferFn diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 728820e2caec9..13367b313431c 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -140,9 +140,9 @@ path = Path sqlite_helper = File path for the SQLite3 database.
Enter an absolute path if you run Gitea as a service. reinstall_error = You are trying to re-install in an existing Gitea database reinstall_confirm_message = In most cases, you should use your existing "app.ini" to run Gitea. Re-installing in an existing Gitea database would cause potential problems. If you know what you are doing, mark these tips as checked to continue. -reinstall_confirm_check_1 = The data encrypted by the secret keys in app.ini will be lost, users may not be able to log in with 2FA/OTP. +reinstall_confirm_check_1 = The data encrypted by the SECRET_KEY in app.ini may be lost, users may not be able to log in with 2FA/OTP. reinstall_confirm_check_2 = The repositories and settings may need to be re-synchronized. -reinstall_confirm_check_3 = I have lost my app.ini and I have to re-install, I know these risks. +reinstall_confirm_check_3 = I have lost/broke my "app.ini" and I have to re-install, I know these risks. err_empty_db_path = The SQLite3 database path cannot be empty. no_admin_and_disable_registration = You cannot disable user self-registration without creating an administrator account. err_empty_admin_password = The administrator password cannot be empty. diff --git a/routers/install/install.go b/routers/install/install.go index 1abfc335bf39a..fb5cc98aea1e1 100644 --- a/routers/install/install.go +++ b/routers/install/install.go @@ -16,6 +16,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + db_install "code.gitea.io/gitea/models/db/install" "code.gitea.io/gitea/models/migrations" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" @@ -186,18 +187,17 @@ func checkDatabase(ctx *context.Context, form *forms.InstallForm) bool { return false } - e := db.GetEngine(db.DefaultContext) - _, err = e.Exec("SELECT 1") + err = db_install.CheckDatabaseConnection() if err != nil { ctx.Data["Err_DbSetting"] = true ctx.RenderWithErr(ctx.Tr("install.invalid_db_setting", err), tplInstall, form) return false } - var installedDbVersion int64 - has, _ := e.Table("version").Cols("`version`").Get(&installedDbVersion) - if has && installedDbVersion > 0 { - log.Error("The database is likely to have been installed by a Gitea before, database migration version=%d", installedDbVersion) + hasPostInstallationUser := db_install.HasPostInstallationUsers() + dbMigrationVersion := db_install.GetMigrationVersion() + if hasPostInstallationUser && dbMigrationVersion > 0 { + log.Error("The database is likely to have been used by a Gitea before, database migration version=%d", dbMigrationVersion) confirmed := form.ReinstallConfirmFirst && form.ReinstallConfirmSecond && form.ReinstallConfirmThird if !confirmed { ctx.Data["Err_DbInstalledBefore"] = true @@ -205,7 +205,6 @@ func checkDatabase(ctx *context.Context, form *forms.InstallForm) bool { return false } } - return true } @@ -389,12 +388,12 @@ func SubmitInstall(ctx *context.Context) { if form.LFSRootPath != "" { cfg.Section("server").Key("LFS_START_SERVER").SetValue("true") cfg.Section("server").Key("LFS_CONTENT_PATH").SetValue(form.LFSRootPath) - var secretKey string - if secretKey, err = generate.NewJwtSecretBase64(); err != nil { + var lfsJwtSecret string + if lfsJwtSecret, err = generate.NewJwtSecretBase64(); err != nil { ctx.RenderWithErr(ctx.Tr("install.lfs_jwt_secret_failed", err), tplInstall, &form) return } - cfg.Section("server").Key("LFS_JWT_SECRET").SetValue(secretKey) + cfg.Section("server").Key("LFS_JWT_SECRET").SetValue(lfsJwtSecret) } else { cfg.Section("server").Key("LFS_START_SERVER").SetValue("false") } @@ -443,12 +442,16 @@ func SubmitInstall(ctx *context.Context) { } cfg.Section("security").Key("INTERNAL_TOKEN").SetValue(internalToken) - var secretKey string - if secretKey, err = generate.NewSecretKey(); err != nil { - ctx.RenderWithErr(ctx.Tr("install.secret_key_failed", err), tplInstall, &form) - return + // if there is already a SECRET_KEY, we should not overwrite it, otherwise the encrypted data will not be able to be decrypted + if setting.SecretKey == "" { + var secretKey string + if secretKey, err = generate.NewSecretKey(); err != nil { + ctx.RenderWithErr(ctx.Tr("install.secret_key_failed", err), tplInstall, &form) + return + } + cfg.Section("security").Key("SECRET_KEY").SetValue(secretKey) } - cfg.Section("security").Key("SECRET_KEY").SetValue(secretKey) + if len(form.PasswordAlgorithm) > 0 { cfg.Section("security").Key("PASSWORD_HASH_ALGO").SetValue(form.PasswordAlgorithm) } diff --git a/templates/install.tmpl b/templates/install.tmpl index 7a4b6387aa020..b04d9b74c4e4f 100644 --- a/templates/install.tmpl +++ b/templates/install.tmpl @@ -91,27 +91,29 @@
-
+ {{if .Err_DbInstalledBefore}} +

{{.i18n.Tr "install.reinstall_confirm_message"}}

- +
- +
- +
+ {{end}}

{{.i18n.Tr "install.general_title"}}

From 7bc17924fba91f637805e019de6e7235e8f3461a Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Wed, 24 Nov 2021 13:33:45 +0800 Subject: [PATCH 10/35] optimize installation check --- cmd/cmd.go | 4 +-- models/db/install/dbinstall.go | 45 +++++++++++++++++++++------- models/migrations/migrations_test.go | 2 +- models/unittest/fixtures.go | 7 +++-- options/locale/locale_en-US.ini | 1 + routers/install/install.go | 14 +++++++-- 6 files changed, 55 insertions(+), 18 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index 9dccd4ea7d0a2..b380ff5be5797 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -59,7 +59,7 @@ func confirm() (bool, error) { func ensureInstallLock() { if !setting.InstallLock { - log.Fatal("invalid app.ini (no installation flag), please use the correctly installed config file") + log.Fatal("invalid installation settings in app.ini (%s), please use the correctly installed config file", setting.CustomConf) } } @@ -69,7 +69,7 @@ func initDB(ctx context.Context) error { setting.NewXORMLogService(false) if setting.Database.Type == "" { - return errors.New("invalid database settings in app.ini, please use the correctly installed config file") + return fmt.Errorf("invalid database settings in app.ini (%s), please use the correctly installed config file", setting.CustomConf) } if err := db.InitEngine(ctx); err != nil { return fmt.Errorf("cmd.initDB: %v", err) diff --git a/models/db/install/dbinstall.go b/models/db/install/dbinstall.go index 9f0c71be2eb7f..83197fb43f662 100644 --- a/models/db/install/dbinstall.go +++ b/models/db/install/dbinstall.go @@ -7,8 +7,14 @@ package install import ( "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/setting" + + "xorm.io/xorm" ) +func getXORMEngine() *xorm.Engine { + return db.DefaultContext.(*db.Context).Engine().(*xorm.Engine) +} + // CheckDatabaseConnection checks the database connection func CheckDatabaseConnection() error { e := db.GetEngine(db.DefaultContext) @@ -17,24 +23,43 @@ func CheckDatabaseConnection() error { } // GetMigrationVersion gets the database migration version -func GetMigrationVersion() int64 { +func GetMigrationVersion() (int64, error) { var installedDbVersion int64 - e := db.GetEngine(db.DefaultContext) - // the error can be safely ignored, then we still get version=0 - _, _ = e.Table("version").Cols("`version`").Get(&installedDbVersion) - return installedDbVersion + x := getXORMEngine() + exist, err := x.IsTableExist("version") + if err != nil { + return 0, err + } + if !exist { + return 0, nil + } + _, err = x.Table("version").Cols("`version`").Get(&installedDbVersion) + if err != nil { + return 0, err + } + return installedDbVersion, nil } // HasPostInstallationUsers checks whether there are users after installation -func HasPostInstallationUsers() bool { - e := db.GetEngine(db.DefaultContext) - // the error can be ignored safely, if there is no user table, we still get count=0 +func HasPostInstallationUsers() (bool, error) { + x := getXORMEngine() + exist, err := x.IsTableExist("user") + if err != nil { + return false, err + } + if !exist { + return false, nil + } + // if there are 2 or more users in database, we consider there are users created after installation threshold := 2 if !setting.IsProd { // to debug easily, with non-prod RUN_MODE, we only check the count to 1 threshold = 1 } - res, _ := e.Table("user").Cols("id").Where("1=1").Limit(threshold).Query() - return len(res) >= threshold + res, err := x.Table("user").Cols("id").Where("1=1").Limit(threshold).Query() + if err != nil { + return false, err + } + return len(res) >= threshold, nil } diff --git a/models/migrations/migrations_test.go b/models/migrations/migrations_test.go index 5ebabd5f0a4ff..df0e187188312 100644 --- a/models/migrations/migrations_test.go +++ b/models/migrations/migrations_test.go @@ -90,7 +90,7 @@ func newXORMEngine() (*xorm.Engine, error) { if err := db.InitEngine(context.Background()); err != nil { return nil, err } - x := db.DefaultContext.(*db.Context).Engine().(*xorm.Engine) + x := unittest.GetXORMEngine() return x, nil } diff --git a/models/unittest/fixtures.go b/models/unittest/fixtures.go index 6277d1c72591d..e39acd766f931 100644 --- a/models/unittest/fixtures.go +++ b/models/unittest/fixtures.go @@ -18,7 +18,8 @@ import ( var fixtures *testfixtures.Loader -func getXORMEngine(engine ...*xorm.Engine) (x *xorm.Engine) { +// GetXORMEngine gets the XORM engine +func GetXORMEngine(engine ...*xorm.Engine) (x *xorm.Engine) { if len(engine) == 1 { return engine[0] } @@ -27,7 +28,7 @@ func getXORMEngine(engine ...*xorm.Engine) (x *xorm.Engine) { // InitFixtures initialize test fixtures for a test database func InitFixtures(opts FixturesOptions, engine ...*xorm.Engine) (err error) { - e := getXORMEngine(engine...) + e := GetXORMEngine(engine...) var testfiles func(*testfixtures.Loader) error if opts.Dir != "" { testfiles = testfixtures.Directory(opts.Dir) @@ -69,7 +70,7 @@ func InitFixtures(opts FixturesOptions, engine ...*xorm.Engine) (err error) { // LoadFixtures load fixtures for a test database func LoadFixtures(engine ...*xorm.Engine) error { - e := getXORMEngine(engine...) + e := GetXORMEngine(engine...) var err error // Database transaction conflicts could occur and result in ROLLBACK // As a simple workaround, we just retry 20 times. diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 13367b313431c..f329d0243e2e3 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -208,6 +208,7 @@ install_btn_confirm = Install Gitea test_git_failed = Could not test 'git' command: %v sqlite3_not_available = This Gitea version does not support SQLite3. Please download the official binary version from %s (not the 'gobuild' version). invalid_db_setting = The database settings are invalid: %v +invalid_db_table = The database table '%s' is invalid: %v invalid_repo_path = The repository root path is invalid: %v run_user_not_match = The 'run as' username is not the current username: %s -> %s internal_token_failed = Failed to generate internal token: %v diff --git a/routers/install/install.go b/routers/install/install.go index fb5cc98aea1e1..ae7af7403c20b 100644 --- a/routers/install/install.go +++ b/routers/install/install.go @@ -194,8 +194,18 @@ func checkDatabase(ctx *context.Context, form *forms.InstallForm) bool { return false } - hasPostInstallationUser := db_install.HasPostInstallationUsers() - dbMigrationVersion := db_install.GetMigrationVersion() + hasPostInstallationUser, err := db_install.HasPostInstallationUsers() + if err != nil { + ctx.Data["Err_DbSetting"] = true + ctx.RenderWithErr(ctx.Tr("install.invalid_db_table", "user", err), tplInstall, form) + return false + } + dbMigrationVersion, err := db_install.GetMigrationVersion() + if err != nil { + ctx.Data["Err_DbSetting"] = true + ctx.RenderWithErr(ctx.Tr("install.invalid_db_table", "version", err), tplInstall, form) + return false + } if hasPostInstallationUser && dbMigrationVersion > 0 { log.Error("The database is likely to have been used by a Gitea before, database migration version=%d", dbMigrationVersion) confirmed := form.ReinstallConfirmFirst && form.ReinstallConfirmSecond && form.ReinstallConfirmThird From 056989d3e52571c9e1f466eeac587f5e6e86ac30 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Wed, 24 Nov 2021 14:03:56 +0800 Subject: [PATCH 11/35] fix rename --- models/db/install/{dbinstall.go => db.go} | 2 +- modules/setting/{setting_i18n.go => i18n.go} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename models/db/install/{dbinstall.go => db.go} (95%) rename modules/setting/{setting_i18n.go => i18n.go} (100%) diff --git a/models/db/install/dbinstall.go b/models/db/install/db.go similarity index 95% rename from models/db/install/dbinstall.go rename to models/db/install/db.go index 83197fb43f662..777c33c4bf507 100644 --- a/models/db/install/dbinstall.go +++ b/models/db/install/db.go @@ -57,7 +57,7 @@ func HasPostInstallationUsers() (bool, error) { // to debug easily, with non-prod RUN_MODE, we only check the count to 1 threshold = 1 } - res, err := x.Table("user").Cols("id").Where("1=1").Limit(threshold).Query() + res, err := x.Table("user").Cols("id").Limit(threshold).Query() if err != nil { return false, err } diff --git a/modules/setting/setting_i18n.go b/modules/setting/i18n.go similarity index 100% rename from modules/setting/setting_i18n.go rename to modules/setting/i18n.go From bd8ff1e4e44c74b29eba83ed4125ca383743a8f6 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Wed, 24 Nov 2021 14:31:44 +0800 Subject: [PATCH 12/35] rename generateSaveInternalToken --- modules/setting/setting.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/setting/setting.go b/modules/setting/setting.go index f4f0fcfe461cb..e99769c3017a3 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -859,7 +859,7 @@ func NewContext() { InternalToken = loadInternalToken(sec) if InstallLock && InternalToken == "" { // if Gitea has been installed but the InternalToken hasn't been generated (upgrade from an old release), we should generate - GenerateSaveInternalToken() + generateSaveInternalToken() } cfgdata := sec.Key("PASSWORD_COMPLEXITY").Strings(",") @@ -1081,8 +1081,8 @@ func loadInternalToken(sec *ini.Section) string { return "" } -// GenerateSaveInternalToken generates and saves the internal token to app.ini -func GenerateSaveInternalToken() { +// generateSaveInternalToken generates and saves the internal token to app.ini +func generateSaveInternalToken() { token, err := generate.NewInternalToken() if err != nil { log.Fatal("Error generate internal token: %v", err) From a0a438dfa82068bbe631db858ae75fd01ec9e1d3 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Thu, 25 Nov 2021 10:27:35 +0800 Subject: [PATCH 13/35] Add logs --- routers/install/install.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/routers/install/install.go b/routers/install/install.go index a79b397db57cb..cfd2f11b10854 100644 --- a/routers/install/install.go +++ b/routers/install/install.go @@ -206,6 +206,7 @@ func checkDatabase(ctx *context.Context, form *forms.InstallForm) bool { ctx.RenderWithErr(ctx.Tr("install.invalid_db_table", "version", err), tplInstall, form) return false } + if hasPostInstallationUser && dbMigrationVersion > 0 { log.Error("The database is likely to have been used by a Gitea before, database migration version=%d", dbMigrationVersion) confirmed := form.ReinstallConfirmFirst && form.ReinstallConfirmSecond && form.ReinstallConfirmThird @@ -214,7 +215,13 @@ func checkDatabase(ctx *context.Context, form *forms.InstallForm) bool { ctx.RenderWithErr(ctx.Tr("install.reinstall_error"), tplInstall, form) return false } + log.Info("User confirmed to continue to install Gitea in an existing database") + } + + if hasPostInstallationUser || dbMigrationVersion > 0 { + log.Info("Gitea will be installed in a database with: hasPostInstallationUser=%v, dbMigrationVersion=%v, confirmed=%v", hasPostInstallationUser, dbMigrationVersion) } + return true } From ce8846e84bd8e0bf90a52538c8ed8acd5d06c2dd Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Thu, 25 Nov 2021 10:32:21 +0800 Subject: [PATCH 14/35] Remove unnecessary db quote --- models/db/install/db.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/db/install/db.go b/models/db/install/db.go index 777c33c4bf507..363a8c86794b6 100644 --- a/models/db/install/db.go +++ b/models/db/install/db.go @@ -33,7 +33,7 @@ func GetMigrationVersion() (int64, error) { if !exist { return 0, nil } - _, err = x.Table("version").Cols("`version`").Get(&installedDbVersion) + _, err = x.Table("version").Cols("version").Get(&installedDbVersion) if err != nil { return 0, err } From 89c37b6fc377b0837e1797da3824637d1fa5bcc6 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Thu, 25 Nov 2021 10:35:39 +0800 Subject: [PATCH 15/35] fix log --- routers/install/install.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routers/install/install.go b/routers/install/install.go index cfd2f11b10854..fb019c61988cd 100644 --- a/routers/install/install.go +++ b/routers/install/install.go @@ -219,7 +219,7 @@ func checkDatabase(ctx *context.Context, form *forms.InstallForm) bool { } if hasPostInstallationUser || dbMigrationVersion > 0 { - log.Info("Gitea will be installed in a database with: hasPostInstallationUser=%v, dbMigrationVersion=%v, confirmed=%v", hasPostInstallationUser, dbMigrationVersion) + log.Info("Gitea will be installed in a database with: hasPostInstallationUser=%v, dbMigrationVersion=%v", hasPostInstallationUser, dbMigrationVersion) } return true From 61fb4d933efc88e1e4bc68e3439dc33839a9bcbc Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Fri, 26 Nov 2021 18:38:01 +0000 Subject: [PATCH 16/35] Adjust install page messages and appearances Signed-off-by: Andrew Thornton --- options/locale/locale_en-US.ini | 10 +++++----- templates/install.tmpl | 2 +- web_src/less/_install.less | 4 ++++ 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index f329d0243e2e3..6c050824e168b 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -138,11 +138,11 @@ ssl_mode = SSL charset = Charset path = Path sqlite_helper = File path for the SQLite3 database.
Enter an absolute path if you run Gitea as a service. -reinstall_error = You are trying to re-install in an existing Gitea database -reinstall_confirm_message = In most cases, you should use your existing "app.ini" to run Gitea. Re-installing in an existing Gitea database would cause potential problems. If you know what you are doing, mark these tips as checked to continue. -reinstall_confirm_check_1 = The data encrypted by the SECRET_KEY in app.ini may be lost, users may not be able to log in with 2FA/OTP. -reinstall_confirm_check_2 = The repositories and settings may need to be re-synchronized. -reinstall_confirm_check_3 = I have lost/broke my "app.ini" and I have to re-install, I know these risks. +reinstall_error = You are trying to install into an existing Gitea database +reinstall_confirm_message = Re-installing with an existing Gitea database can cause multiple problems. In most cases, you should use your existing "app.ini" to run Gitea. If you know what you are doing, confirm the following: +reinstall_confirm_check_1 = The data encrypted by the SECRET_KEY in app.ini may be lost: users may not be able to log in with 2FA/OTP & mirrors may not function correctly. By checking this box you confirm that the current app.ini file contains the correct the SECRET_KEY. +reinstall_confirm_check_2 = The repositories and settings may need to be re-synchronized. By checking this box you confirm that you will resynchronize the hooks for the repositories and authorized_keys file manually. You confirm that you will ensure that repository and mirror settings are correct. +reinstall_confirm_check_3 = You confirm that you are absolutely sure that this Gitea is running with the correct app.ini location and that you are sure that you have have to re-install. You confirm that you acknowledge the above risks. err_empty_db_path = The SQLite3 database path cannot be empty. no_admin_and_disable_registration = You cannot disable user self-registration without creating an administrator account. err_empty_admin_password = The administrator password cannot be empty. diff --git a/templates/install.tmpl b/templates/install.tmpl index b04d9b74c4e4f..bf8e1ef85d52a 100644 --- a/templates/install.tmpl +++ b/templates/install.tmpl @@ -93,7 +93,7 @@ {{if .Err_DbInstalledBefore}}
-

{{.i18n.Tr "install.reinstall_confirm_message"}}

+

{{.i18n.Tr "install.reinstall_confirm_message"}}

diff --git a/web_src/less/_install.less b/web_src/less/_install.less index fca59a89aaa56..e7589aa6a32f4 100644 --- a/web_src/less/_install.less +++ b/web_src/less/_install.less @@ -40,6 +40,10 @@ margin: 20px auto; color: red; } + .reinstall-message { + text-align: left; + font-weight: bold; + } .reinstall-confirm { width: 70%; text-align: left; From c1235fa449c52a127d49c6198c906aef09c0edbf Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Fri, 26 Nov 2021 18:40:29 +0000 Subject: [PATCH 17/35] Add testing for empty app.ini on setting.NewContext and only allow web.go to function with an empty app.ini Signed-off-by: Andrew Thornton --- cmd/cmd.go | 14 +++++--------- cmd/doctor.go | 4 +--- cmd/dump.go | 3 ++- cmd/mailer.go | 2 +- cmd/restore_repo.go | 2 +- cmd/serv.go | 2 +- cmd/web.go | 4 ++++ contrib/pr/checkout.go | 2 +- integrations/integration_test.go | 2 +- integrations/migration-test/migration_test.go | 2 +- models/migrations/migrations_test.go | 2 +- models/ssh_key_test.go | 2 +- modules/doctor/doctor.go | 2 +- modules/doctor/paths.go | 2 +- modules/setting/setting.go | 6 ++++-- routers/init.go | 2 +- routers/install/install.go | 5 +++-- routers/install/setting.go | 4 ++-- 18 files changed, 32 insertions(+), 30 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index b380ff5be5797..d3774484ad0fd 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -57,22 +57,18 @@ func confirm() (bool, error) { } } -func ensureInstallLock() { - if !setting.InstallLock { - log.Fatal("invalid installation settings in app.ini (%s), please use the correctly installed config file", setting.CustomConf) - } -} - func initDB(ctx context.Context) error { - setting.NewContext() + setting.NewContext(false) setting.InitDBConfig() setting.NewXORMLogService(false) if setting.Database.Type == "" { - return fmt.Errorf("invalid database settings in app.ini (%s), please use the correctly installed config file", setting.CustomConf) + log.Fatal(`Database settings are missing from the configuration file: %q. +Ensure you are running in the correct environment or set the correct configuration file with -c. +If this is the intended configuration file complete the [database] section.`, setting.CustomConf) } if err := db.InitEngine(ctx); err != nil { - return fmt.Errorf("cmd.initDB: %v", err) + return fmt.Errorf("unable to initialise the database using the configuration in %q. Error: %v", setting.CustomConf, err) } return nil } diff --git a/cmd/doctor.go b/cmd/doctor.go index 05f04814dea31..c8203c4840fe8 100644 --- a/cmd/doctor.go +++ b/cmd/doctor.go @@ -82,14 +82,12 @@ You should back-up your database before doing this and ensure that your database } func runRecreateTable(ctx *cli.Context) error { - ensureInstallLock() - // Redirect the default golog to here golog.SetFlags(0) golog.SetPrefix("") golog.SetOutput(log.NewLoggerAsWriter("INFO", log.GetLogger(log.DEFAULT))) - setting.NewContext() + setting.NewContext(false) setting.InitDBConfig() setting.EnableXORMLog = ctx.Bool("debug") diff --git a/cmd/dump.go b/cmd/dump.go index efb9397208594..887b61ca471ae 100644 --- a/cmd/dump.go +++ b/cmd/dump.go @@ -159,7 +159,8 @@ func runDump(ctx *cli.Context) error { fatal("Deleting default logger failed. Can not write to stdout: %v", err) } } - setting.NewContext() + setting.NewContext(false) + // make sure we are logging to the console no matter what the configuration tells us do to if _, err := setting.Cfg.Section("log").NewKey("MODE", "console"); err != nil { fatal("Setting logging mode to console failed: %v", err) diff --git a/cmd/mailer.go b/cmd/mailer.go index a3d6baaa27a5a..dfb6cd2d62998 100644 --- a/cmd/mailer.go +++ b/cmd/mailer.go @@ -18,7 +18,7 @@ func runSendMail(c *cli.Context) error { ctx, cancel := installSignals() defer cancel() - setting.NewContext() + setting.NewContext(false) if err := argsSet(c, "title"); err != nil { return err diff --git a/cmd/restore_repo.go b/cmd/restore_repo.go index 1208796c9bde4..593a24e643edc 100644 --- a/cmd/restore_repo.go +++ b/cmd/restore_repo.go @@ -50,7 +50,7 @@ func runRestoreRepository(c *cli.Context) error { ctx, cancel := installSignals() defer cancel() - setting.NewContext() + setting.NewContext(false) statusCode, errStr := private.RestoreRepo( ctx, diff --git a/cmd/serv.go b/cmd/serv.go index 13c27f1ac05dd..af099dd31d715 100644 --- a/cmd/serv.go +++ b/cmd/serv.go @@ -57,7 +57,7 @@ func setup(logPath string, debug bool) { } else { _ = log.NewLogger(1000, "console", "console", `{"level":"fatal","stacktracelevel":"NONE","stderr":true}`) } - setting.NewContext() + setting.NewContext(false) if debug { setting.RunMode = "dev" } diff --git a/cmd/web.go b/cmd/web.go index 4b6dfa71a2305..0b3eaf67bc1e8 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -124,6 +124,10 @@ func runWeb(ctx *cli.Context) error { } c := install.Routes() err := listen(c, false) + if err != nil { + log.Critical("Unable to open listener for installer. Is Gitea already running?") + graceful.GetManager().DoGracefulShutdown() + } select { case <-graceful.GetManager().IsShutdown(): <-graceful.GetManager().Done() diff --git a/contrib/pr/checkout.go b/contrib/pr/checkout.go index 1e2a9714e38a6..5cd74f600ddf0 100644 --- a/contrib/pr/checkout.go +++ b/contrib/pr/checkout.go @@ -49,7 +49,7 @@ func runPR() { log.Fatal(err) } setting.SetCustomPathAndConf("", "", "") - setting.NewContext() + setting.NewContext(true) setting.RepoRootPath, err = os.MkdirTemp(os.TempDir(), "repos") if err != nil { diff --git a/integrations/integration_test.go b/integrations/integration_test.go index 710e2b3b30a59..9c2279e9c4226 100644 --- a/integrations/integration_test.go +++ b/integrations/integration_test.go @@ -164,7 +164,7 @@ func initIntegrationTest() { } setting.SetCustomPathAndConf("", "", "") - setting.NewContext() + setting.NewContext(true) util.RemoveAll(models.LocalCopyPath()) git.CheckLFSVersion() setting.InitDBConfig() diff --git a/integrations/migration-test/migration_test.go b/integrations/migration-test/migration_test.go index 1e74fcc94dcb0..9dced5de8b623 100644 --- a/integrations/migration-test/migration_test.go +++ b/integrations/migration-test/migration_test.go @@ -56,7 +56,7 @@ func initMigrationTest(t *testing.T) func() { setting.CustomConf = giteaConf } - setting.NewContext() + setting.NewContext(true) assert.True(t, len(setting.RepoRootPath) != 0) assert.NoError(t, util.RemoveAll(setting.RepoRootPath)) diff --git a/models/migrations/migrations_test.go b/models/migrations/migrations_test.go index df0e187188312..7c3354049b017 100644 --- a/models/migrations/migrations_test.go +++ b/models/migrations/migrations_test.go @@ -58,7 +58,7 @@ func TestMain(m *testing.M) { } setting.SetCustomPathAndConf("", "", "") - setting.NewContext() + setting.NewContext(true) git.CheckLFSVersion() setting.InitDBConfig() setting.NewLogServices(true) diff --git a/models/ssh_key_test.go b/models/ssh_key_test.go index cc78f2604bf3c..98512891dda13 100644 --- a/models/ssh_key_test.go +++ b/models/ssh_key_test.go @@ -16,7 +16,7 @@ import ( func init() { setting.SetCustomPathAndConf("", "", "") - setting.NewContext() + setting.NewContext(true) } func Test_SSHParsePublicKey(t *testing.T) { diff --git a/modules/doctor/doctor.go b/modules/doctor/doctor.go index 6451788f9df4b..cbda3082d91be 100644 --- a/modules/doctor/doctor.go +++ b/modules/doctor/doctor.go @@ -44,7 +44,7 @@ func (w *wrappedLevelLogger) Log(skip int, level log.Level, format string, v ... } func initDBDisableConsole(ctx context.Context, disableConsole bool) error { - setting.NewContext() + setting.NewContext(false) setting.InitDBConfig() setting.NewXORMLogService(disableConsole) diff --git a/modules/doctor/paths.go b/modules/doctor/paths.go index 88172d3150967..c8f9e90789ab3 100644 --- a/modules/doctor/paths.go +++ b/modules/doctor/paths.go @@ -67,7 +67,7 @@ func checkConfigurationFiles(logger log.Logger, autofix bool) error { return err } - setting.NewContext() + setting.NewContext(false) configurationFiles := []configurationFile{ {"Configuration File Path", setting.CustomConf, false, true, false}, diff --git a/modules/setting/setting.go b/modules/setting/setting.go index e99769c3017a3..a7272ce8cd383 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -548,7 +548,7 @@ func SetCustomPathAndConf(providedCustom, providedConf, providedWorkPath string) // NewContext initializes configuration context. // NOTE: do not print any log except error. -func NewContext() { +func NewContext(allowEmpty bool) { Cfg = ini.Empty() if WritePIDFile && len(PIDFile) > 0 { @@ -563,8 +563,10 @@ func NewContext() { if err := Cfg.Append(CustomConf); err != nil { log.Fatal("Failed to load custom conf '%s': %v", CustomConf, err) } + } else if !allowEmpty { + log.Fatal("Unable to find configuration file: %q.\nEnsure you are running in the correct environment or set the correct configuration file with -c.", CustomConf) } else { - log.Warn("Custom config '%s' not found, ignore this if you're running first time", CustomConf) + log.Warn("Unable to find configuration file: %q.\nA new configuration file will be created.", CustomConf) } Cfg.NameMapper = ini.SnackCase diff --git a/routers/init.go b/routers/init.go index 58c7384092215..fd851f8be3407 100644 --- a/routers/init.go +++ b/routers/init.go @@ -97,7 +97,7 @@ func syncAppPathForGit(ctx context.Context) error { // GlobalInit is for global configuration reload-able. func GlobalInit(ctx context.Context) { - setting.NewContext() + setting.NewContext(false) if !setting.InstallLock { log.Fatal("Gitea is not installed") } diff --git a/routers/install/install.go b/routers/install/install.go index fb019c61988cd..acfdbe03aa1c1 100644 --- a/routers/install/install.go +++ b/routers/install/install.go @@ -208,14 +208,15 @@ func checkDatabase(ctx *context.Context, form *forms.InstallForm) bool { } if hasPostInstallationUser && dbMigrationVersion > 0 { - log.Error("The database is likely to have been used by a Gitea before, database migration version=%d", dbMigrationVersion) + log.Error("The database is likely to have been used by Gitea before, database migration version=%d", dbMigrationVersion) confirmed := form.ReinstallConfirmFirst && form.ReinstallConfirmSecond && form.ReinstallConfirmThird if !confirmed { ctx.Data["Err_DbInstalledBefore"] = true ctx.RenderWithErr(ctx.Tr("install.reinstall_error"), tplInstall, form) return false } - log.Info("User confirmed to continue to install Gitea in an existing database") + + log.Info("User confirmed reinstallation of Gitea into a pre-existing database") } if hasPostInstallationUser || dbMigrationVersion > 0 { diff --git a/routers/install/setting.go b/routers/install/setting.go index e2af66cfb4553..f845b796c51b5 100644 --- a/routers/install/setting.go +++ b/routers/install/setting.go @@ -16,7 +16,7 @@ import ( // PreloadSettings preloads the configuration to check if we need to run install func PreloadSettings(ctx context.Context) bool { - setting.NewContext() + setting.NewContext(true) if !setting.InstallLock { log.Info("AppPath: %s", setting.AppPath) log.Info("AppWorkPath: %s", setting.AppWorkPath) @@ -38,7 +38,7 @@ func PreloadSettings(ctx context.Context) bool { // ReloadSettings rereads the settings and starts up the database func ReloadSettings(ctx context.Context) { - setting.NewContext() + setting.NewContext(true) setting.InitDBConfig() if setting.InstallLock { if err := common.InitDBEngine(ctx); err == nil { From f15d610da3ddae52d203d034ff55fabc232c6af0 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sat, 27 Nov 2021 10:18:12 +0800 Subject: [PATCH 18/35] Improve setting.NewContext --- cmd/cmd.go | 2 +- cmd/doctor.go | 2 +- cmd/dump.go | 2 +- cmd/mailer.go | 2 +- cmd/restore_repo.go | 2 +- cmd/serv.go | 2 +- .../environment-to-ini/environment-to-ini.go | 1 + contrib/pr/checkout.go | 2 +- integrations/integration_test.go | 2 +- integrations/migration-test/migration_test.go | 2 +- models/migrations/migrations_test.go | 2 +- models/ssh_key_test.go | 2 +- modules/doctor/doctor.go | 2 +- modules/doctor/paths.go | 2 +- modules/setting/setting.go | 29 ++++++++++++++----- routers/init.go | 2 +- routers/install/install.go | 2 ++ routers/install/setting.go | 4 +-- services/auth/signin.go | 6 ++-- templates/install.tmpl | 2 +- web_src/less/_install.less | 4 +-- 21 files changed, 45 insertions(+), 31 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index d3774484ad0fd..4d934cd01d833 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -58,7 +58,7 @@ func confirm() (bool, error) { } func initDB(ctx context.Context) error { - setting.NewContext(false) + setting.NewContextFromExistingConf() setting.InitDBConfig() setting.NewXORMLogService(false) diff --git a/cmd/doctor.go b/cmd/doctor.go index c8203c4840fe8..d7ebaa2eca30f 100644 --- a/cmd/doctor.go +++ b/cmd/doctor.go @@ -87,7 +87,7 @@ func runRecreateTable(ctx *cli.Context) error { golog.SetPrefix("") golog.SetOutput(log.NewLoggerAsWriter("INFO", log.GetLogger(log.DEFAULT))) - setting.NewContext(false) + setting.NewContextFromExistingConf() setting.InitDBConfig() setting.EnableXORMLog = ctx.Bool("debug") diff --git a/cmd/dump.go b/cmd/dump.go index 887b61ca471ae..a06bf74126ccc 100644 --- a/cmd/dump.go +++ b/cmd/dump.go @@ -159,7 +159,7 @@ func runDump(ctx *cli.Context) error { fatal("Deleting default logger failed. Can not write to stdout: %v", err) } } - setting.NewContext(false) + setting.NewContextFromExistingConf() // make sure we are logging to the console no matter what the configuration tells us do to if _, err := setting.Cfg.Section("log").NewKey("MODE", "console"); err != nil { diff --git a/cmd/mailer.go b/cmd/mailer.go index dfb6cd2d62998..dcb464de9a5e6 100644 --- a/cmd/mailer.go +++ b/cmd/mailer.go @@ -18,7 +18,7 @@ func runSendMail(c *cli.Context) error { ctx, cancel := installSignals() defer cancel() - setting.NewContext(false) + setting.NewContextFromExistingConf() if err := argsSet(c, "title"); err != nil { return err diff --git a/cmd/restore_repo.go b/cmd/restore_repo.go index 593a24e643edc..549e606a63b8b 100644 --- a/cmd/restore_repo.go +++ b/cmd/restore_repo.go @@ -50,7 +50,7 @@ func runRestoreRepository(c *cli.Context) error { ctx, cancel := installSignals() defer cancel() - setting.NewContext(false) + setting.NewContextFromExistingConf() statusCode, errStr := private.RestoreRepo( ctx, diff --git a/cmd/serv.go b/cmd/serv.go index af099dd31d715..815d97e0befdb 100644 --- a/cmd/serv.go +++ b/cmd/serv.go @@ -57,7 +57,7 @@ func setup(logPath string, debug bool) { } else { _ = log.NewLogger(1000, "console", "console", `{"level":"fatal","stacktracelevel":"NONE","stderr":true}`) } - setting.NewContext(false) + setting.NewContextFromExistingConf() if debug { setting.RunMode = "dev" } diff --git a/contrib/environment-to-ini/environment-to-ini.go b/contrib/environment-to-ini/environment-to-ini.go index 03c876aa627c5..5f63a494cc5d7 100644 --- a/contrib/environment-to-ini/environment-to-ini.go +++ b/contrib/environment-to-ini/environment-to-ini.go @@ -156,6 +156,7 @@ func runEnvironmentToIni(c *cli.Context) error { destination = setting.CustomConf } if destination != setting.CustomConf || changed { + log.Info("Save settings to custom config file %s", destination) err = cfg.SaveTo(destination) if err != nil { return err diff --git a/contrib/pr/checkout.go b/contrib/pr/checkout.go index 5cd74f600ddf0..feb23dc0d59b4 100644 --- a/contrib/pr/checkout.go +++ b/contrib/pr/checkout.go @@ -49,7 +49,7 @@ func runPR() { log.Fatal(err) } setting.SetCustomPathAndConf("", "", "") - setting.NewContext(true) + setting.NewContextAllowEmptyConf() setting.RepoRootPath, err = os.MkdirTemp(os.TempDir(), "repos") if err != nil { diff --git a/integrations/integration_test.go b/integrations/integration_test.go index 9c2279e9c4226..879f523aa0b95 100644 --- a/integrations/integration_test.go +++ b/integrations/integration_test.go @@ -164,7 +164,7 @@ func initIntegrationTest() { } setting.SetCustomPathAndConf("", "", "") - setting.NewContext(true) + setting.NewContextAllowEmptyConf() util.RemoveAll(models.LocalCopyPath()) git.CheckLFSVersion() setting.InitDBConfig() diff --git a/integrations/migration-test/migration_test.go b/integrations/migration-test/migration_test.go index 9dced5de8b623..ae17c1fab3f87 100644 --- a/integrations/migration-test/migration_test.go +++ b/integrations/migration-test/migration_test.go @@ -56,7 +56,7 @@ func initMigrationTest(t *testing.T) func() { setting.CustomConf = giteaConf } - setting.NewContext(true) + setting.NewContextAllowEmptyConf() assert.True(t, len(setting.RepoRootPath) != 0) assert.NoError(t, util.RemoveAll(setting.RepoRootPath)) diff --git a/models/migrations/migrations_test.go b/models/migrations/migrations_test.go index 7c3354049b017..0661d64cdd3c5 100644 --- a/models/migrations/migrations_test.go +++ b/models/migrations/migrations_test.go @@ -58,7 +58,7 @@ func TestMain(m *testing.M) { } setting.SetCustomPathAndConf("", "", "") - setting.NewContext(true) + setting.NewContextAllowEmptyConf() git.CheckLFSVersion() setting.InitDBConfig() setting.NewLogServices(true) diff --git a/models/ssh_key_test.go b/models/ssh_key_test.go index 98512891dda13..2ac775334adbf 100644 --- a/models/ssh_key_test.go +++ b/models/ssh_key_test.go @@ -16,7 +16,7 @@ import ( func init() { setting.SetCustomPathAndConf("", "", "") - setting.NewContext(true) + setting.NewContextAllowEmptyConf() } func Test_SSHParsePublicKey(t *testing.T) { diff --git a/modules/doctor/doctor.go b/modules/doctor/doctor.go index cbda3082d91be..1e4e412763042 100644 --- a/modules/doctor/doctor.go +++ b/modules/doctor/doctor.go @@ -44,7 +44,7 @@ func (w *wrappedLevelLogger) Log(skip int, level log.Level, format string, v ... } func initDBDisableConsole(ctx context.Context, disableConsole bool) error { - setting.NewContext(false) + setting.NewContextFromExistingConf() setting.InitDBConfig() setting.NewXORMLogService(disableConsole) diff --git a/modules/doctor/paths.go b/modules/doctor/paths.go index c8f9e90789ab3..d7fb49ddb6768 100644 --- a/modules/doctor/paths.go +++ b/modules/doctor/paths.go @@ -67,7 +67,7 @@ func checkConfigurationFiles(logger log.Logger, autofix bool) error { return err } - setting.NewContext(false) + setting.NewContextFromExistingConf() configurationFiles := []configurationFile{ {"Configuration File Path", setting.CustomConf, false, true, false}, diff --git a/modules/setting/setting.go b/modules/setting/setting.go index a7272ce8cd383..de312fd2090a8 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -546,9 +546,19 @@ func SetCustomPathAndConf(providedCustom, providedConf, providedWorkPath string) } } -// NewContext initializes configuration context. +// NewContextFromExistingConf initializes configuration context from an exisint config file (app.ini) +func NewContextFromExistingConf() { + newContextFromConf(false) +} + +// NewContextAllowEmptyConf initializes configuration context, it's also fine that if the config file (app.ini) doesn't exist +func NewContextAllowEmptyConf() { + newContextFromConf(true) +} + +// newContextFromConf initializes configuration context. // NOTE: do not print any log except error. -func NewContext(allowEmpty bool) { +func newContextFromConf(allowEmpty bool) { Cfg = ini.Empty() if WritePIDFile && len(PIDFile) > 0 { @@ -565,9 +575,8 @@ func NewContext(allowEmpty bool) { } } else if !allowEmpty { log.Fatal("Unable to find configuration file: %q.\nEnsure you are running in the correct environment or set the correct configuration file with -c.", CustomConf) - } else { - log.Warn("Unable to find configuration file: %q.\nA new configuration file will be created.", CustomConf) - } + } // else: no config file, a config file might be created at CustomConf (might not) + Cfg.NameMapper = ini.SnackCase homeDir, err := com.HomeDir() @@ -700,9 +709,11 @@ func NewContext(allowEmpty bool) { // The correct behavior should be: creating parent directories is end users' duty. We only create sub-directories in existing parent directories. // For quickstart, the parent directories should be created automatically for first startup (eg: a flag or a check of INSTALL_LOCK). // Now we can take the first step to do correctly (using Mkdir) in other packages, and prepare the AppDataPath here, then make a refactor in future. - err = os.MkdirAll(AppDataPath, os.ModePerm) - if err != nil { - log.Fatal("Failed to create the directory for app data path '%s'", AppDataPath) + if allowEmpty { + err = os.MkdirAll(AppDataPath, os.ModePerm) + if err != nil { + log.Fatal("Failed to create the directory for app data path '%s'", AppDataPath) + } } } EnableGzip = sec.Key("ENABLE_GZIP").MustBool() @@ -1173,6 +1184,8 @@ func CreateOrAppendToCustomConf(callback func(cfg *ini.File)) { callback(cfg) + log.Info("Save settings to custom config file %s", CustomConf) + if err := os.MkdirAll(filepath.Dir(CustomConf), os.ModePerm); err != nil { log.Fatal("failed to create '%s': %v", CustomConf, err) return diff --git a/routers/init.go b/routers/init.go index fd851f8be3407..58d3201f56efa 100644 --- a/routers/init.go +++ b/routers/init.go @@ -97,7 +97,7 @@ func syncAppPathForGit(ctx context.Context) error { // GlobalInit is for global configuration reload-able. func GlobalInit(ctx context.Context) { - setting.NewContext(false) + setting.NewContextFromExistingConf() if !setting.InstallLock { log.Fatal("Gitea is not installed") } diff --git a/routers/install/install.go b/routers/install/install.go index acfdbe03aa1c1..d8fb86615998f 100644 --- a/routers/install/install.go +++ b/routers/install/install.go @@ -474,6 +474,8 @@ func SubmitInstall(ctx *context.Context) { cfg.Section("security").Key("PASSWORD_HASH_ALGO").SetValue(form.PasswordAlgorithm) } + log.Info("Save settings to custom config file %s", setting.CustomConf) + err = os.MkdirAll(filepath.Dir(setting.CustomConf), os.ModePerm) if err != nil { ctx.RenderWithErr(ctx.Tr("install.save_config_failed", err), tplInstall, &form) diff --git a/routers/install/setting.go b/routers/install/setting.go index f845b796c51b5..3d1c9fba833c1 100644 --- a/routers/install/setting.go +++ b/routers/install/setting.go @@ -16,7 +16,7 @@ import ( // PreloadSettings preloads the configuration to check if we need to run install func PreloadSettings(ctx context.Context) bool { - setting.NewContext(true) + setting.NewContextAllowEmptyConf() if !setting.InstallLock { log.Info("AppPath: %s", setting.AppPath) log.Info("AppWorkPath: %s", setting.AppWorkPath) @@ -38,7 +38,7 @@ func PreloadSettings(ctx context.Context) bool { // ReloadSettings rereads the settings and starts up the database func ReloadSettings(ctx context.Context) { - setting.NewContext(true) + setting.NewContextAllowEmptyConf() setting.InitDBConfig() if setting.InstallLock { if err := common.InitDBEngine(ctx); err == nil { diff --git a/services/auth/signin.go b/services/auth/signin.go index 5f75000d9836a..80be419962a5c 100644 --- a/services/auth/signin.go +++ b/services/auth/signin.go @@ -11,12 +11,12 @@ import ( "code.gitea.io/gitea/models/login" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/services/auth/source/oauth2" + "code.gitea.io/gitea/services/auth/source/smtp" _ "code.gitea.io/gitea/services/auth/source/db" // register the sources (and below) _ "code.gitea.io/gitea/services/auth/source/ldap" // register the ldap source - "code.gitea.io/gitea/services/auth/source/oauth2" - _ "code.gitea.io/gitea/services/auth/source/pam" // register the pam source - "code.gitea.io/gitea/services/auth/source/smtp" + _ "code.gitea.io/gitea/services/auth/source/pam" // register the pam source _ "code.gitea.io/gitea/services/auth/source/sspi" // register the sspi source ) diff --git a/templates/install.tmpl b/templates/install.tmpl index bf8e1ef85d52a..1fa0929afab98 100644 --- a/templates/install.tmpl +++ b/templates/install.tmpl @@ -93,7 +93,7 @@ {{if .Err_DbInstalledBefore}}
-

{{.i18n.Tr "install.reinstall_confirm_message"}}

+

{{.i18n.Tr "install.reinstall_confirm_message"}}

diff --git a/web_src/less/_install.less b/web_src/less/_install.less index e7589aa6a32f4..c6d93c514c04d 100644 --- a/web_src/less/_install.less +++ b/web_src/less/_install.less @@ -35,12 +35,10 @@ } .ui { - .detail-message { + .reinstall-message { width: 70%; margin: 20px auto; color: red; - } - .reinstall-message { text-align: left; font-weight: bold; } From fbaf6357d48b33a89c6399e006105e512fc572c3 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sat, 27 Nov 2021 12:47:12 +0800 Subject: [PATCH 19/35] Only create APP_DATA_PATH if necessary --- integrations/integration_test.go | 2 +- integrations/migration-test/migration_test.go | 2 +- models/migrations/migrations_test.go | 2 +- models/ssh_key_test.go | 2 +- modules/setting/directory.go | 36 +++++++++++++++++++ modules/setting/setting.go | 21 ++++++----- options/locale/locale_en-US.ini | 1 + routers/install/install.go | 8 ++++- routers/install/setting.go | 10 +++--- 9 files changed, 63 insertions(+), 21 deletions(-) create mode 100644 modules/setting/directory.go diff --git a/integrations/integration_test.go b/integrations/integration_test.go index 879f523aa0b95..519e527933955 100644 --- a/integrations/integration_test.go +++ b/integrations/integration_test.go @@ -164,7 +164,7 @@ func initIntegrationTest() { } setting.SetCustomPathAndConf("", "", "") - setting.NewContextAllowEmptyConf() + setting.NewContextForTest() util.RemoveAll(models.LocalCopyPath()) git.CheckLFSVersion() setting.InitDBConfig() diff --git a/integrations/migration-test/migration_test.go b/integrations/migration-test/migration_test.go index ae17c1fab3f87..9e13022f9ccd2 100644 --- a/integrations/migration-test/migration_test.go +++ b/integrations/migration-test/migration_test.go @@ -56,7 +56,7 @@ func initMigrationTest(t *testing.T) func() { setting.CustomConf = giteaConf } - setting.NewContextAllowEmptyConf() + setting.NewContextForTest() assert.True(t, len(setting.RepoRootPath) != 0) assert.NoError(t, util.RemoveAll(setting.RepoRootPath)) diff --git a/models/migrations/migrations_test.go b/models/migrations/migrations_test.go index 0661d64cdd3c5..583ff1e09354b 100644 --- a/models/migrations/migrations_test.go +++ b/models/migrations/migrations_test.go @@ -58,7 +58,7 @@ func TestMain(m *testing.M) { } setting.SetCustomPathAndConf("", "", "") - setting.NewContextAllowEmptyConf() + setting.NewContextForTest() git.CheckLFSVersion() setting.InitDBConfig() setting.NewLogServices(true) diff --git a/models/ssh_key_test.go b/models/ssh_key_test.go index 2ac775334adbf..b3740e2204b4f 100644 --- a/models/ssh_key_test.go +++ b/models/ssh_key_test.go @@ -16,7 +16,7 @@ import ( func init() { setting.SetCustomPathAndConf("", "", "") - setting.NewContextAllowEmptyConf() + setting.NewContextForTest() } func Test_SSHParsePublicKey(t *testing.T) { diff --git a/modules/setting/directory.go b/modules/setting/directory.go new file mode 100644 index 0000000000000..6d33c0e8161aa --- /dev/null +++ b/modules/setting/directory.go @@ -0,0 +1,36 @@ +package setting + +import ( + "fmt" + "os" +) + +// PrepareAppDataPath creates app data directory if necessary +func PrepareAppDataPath() error { + // FIXME: There are too many calls to MkdirAll in old code. It is incorrect. + // For example, if someDir=/mnt/vol1/gitea-home/data, if the mount point /mnt/vol1 is not mounted when Gitea runs, + // then gitea will make new empty directories in /mnt/vol1, all are stored in the root filesystem. + // The correct behavior should be: creating parent directories is end users' duty. We only create sub-directories in existing parent directories. + // For quickstart, the parent directories should be created automatically for first startup (eg: a flag or a check of INSTALL_LOCK). + // Now we can take the first step to do correctly (using Mkdir) in other packages, and prepare the AppDataPath here, then make a refactor in future. + + st, err := os.Stat(AppDataPath) + + if os.IsNotExist(err) { + err = os.MkdirAll(AppDataPath, os.ModePerm) + if err != nil { + return fmt.Errorf("failed to create the directory for app data path '%s', err: %v", AppDataPath, err) + } + return nil + } + + if err != nil { + return fmt.Errorf("can not use '%s' as app data path, err: %v", AppDataPath, err) + } + + if !st.IsDir() /* also works for symlink */ { + return fmt.Errorf("can not use non-directory '%s' as app data path", AppDataPath) + } + + return nil +} diff --git a/modules/setting/setting.go b/modules/setting/setting.go index de312fd2090a8..0394d4f6e325a 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -556,6 +556,14 @@ func NewContextAllowEmptyConf() { newContextFromConf(true) } +// NewContextForTest initializes configuration context for tests +func NewContextForTest() { + newContextFromConf(true) + if err := PrepareAppDataPath(); err != nil { + log.Fatal("Can not prepare APP_DATA_PATH: %v", err) + } +} + // newContextFromConf initializes configuration context. // NOTE: do not print any log except error. func newContextFromConf(allowEmpty bool) { @@ -703,17 +711,8 @@ func newContextFromConf(allowEmpty bool) { StaticCacheTime = sec.Key("STATIC_CACHE_TIME").MustDuration(6 * time.Hour) AppDataPath = sec.Key("APP_DATA_PATH").MustString(path.Join(AppWorkPath, "data")) if _, err = os.Stat(AppDataPath); err != nil { - // FIXME: There are too many calls to MkdirAll in old code. It is incorrect. - // For example, if someDir=/mnt/vol1/gitea-home/data, if the mount point /mnt/vol1 is not mounted when Gitea runs, - // then gitea will make new empty directories in /mnt/vol1, all are stored in the root filesystem. - // The correct behavior should be: creating parent directories is end users' duty. We only create sub-directories in existing parent directories. - // For quickstart, the parent directories should be created automatically for first startup (eg: a flag or a check of INSTALL_LOCK). - // Now we can take the first step to do correctly (using Mkdir) in other packages, and prepare the AppDataPath here, then make a refactor in future. - if allowEmpty { - err = os.MkdirAll(AppDataPath, os.ModePerm) - if err != nil { - log.Fatal("Failed to create the directory for app data path '%s'", AppDataPath) - } + if !allowEmpty { + log.Fatal("Can not find APP_DATA_PATH '%s'", AppDataPath) } } EnableGzip = sec.Key("ENABLE_GZIP").MustBool() diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 6c050824e168b..46d4f5d7501ce 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -210,6 +210,7 @@ sqlite3_not_available = This Gitea version does not support SQLite3. Please down invalid_db_setting = The database settings are invalid: %v invalid_db_table = The database table '%s' is invalid: %v invalid_repo_path = The repository root path is invalid: %v +invalid_app_data_path = The app data path is invalid: %v run_user_not_match = The 'run as' username is not the current username: %s -> %s internal_token_failed = Failed to generate internal token: %v secret_key_failed = Failed to generate secret key: %v diff --git a/routers/install/install.go b/routers/install/install.go index d8fb86615998f..b2f04b14dd279 100644 --- a/routers/install/install.go +++ b/routers/install/install.go @@ -277,6 +277,12 @@ func SubmitInstall(ctx *context.Context) { return } + // Prepare AppDataPath, it is very important for Gitea + if err = setting.PrepareAppDataPath(); err != nil { + ctx.RenderWithErr(ctx.Tr("install.invalid_app_data_path", err), tplInstall, &form) + return + } + // Test repository root path. form.RepoRootPath = strings.ReplaceAll(form.RepoRootPath, "\\", "/") if err = os.MkdirAll(form.RepoRootPath, os.ModePerm); err != nil { @@ -490,7 +496,7 @@ func SubmitInstall(ctx *context.Context) { // ---- All checks are passed // Reload settings (and re-initialize database connection) - ReloadSettings(ctx) + reloadSettings(ctx) // Create admin account if len(form.AdminName) > 0 { diff --git a/routers/install/setting.go b/routers/install/setting.go index 3d1c9fba833c1..f718854124123 100644 --- a/routers/install/setting.go +++ b/routers/install/setting.go @@ -23,10 +23,10 @@ func PreloadSettings(ctx context.Context) bool { log.Info("Custom path: %s", setting.CustomPath) log.Info("Log path: %s", setting.LogRootPath) log.Info("Configuration file: %s", setting.CustomConf) - log.Info("Preparing to run install page") + log.Info("Prepare to run install page") translation.InitLocales() if setting.EnableSQLite3 { - log.Info("SQLite3 Supported") + log.Info("SQLite3 is supported") } setting.InitDBConfig() setting.NewServicesForInstall() @@ -36,9 +36,9 @@ func PreloadSettings(ctx context.Context) bool { return !setting.InstallLock } -// ReloadSettings rereads the settings and starts up the database -func ReloadSettings(ctx context.Context) { - setting.NewContextAllowEmptyConf() +// reloadSettings reloads the existing settings and starts up the database +func reloadSettings(ctx context.Context) { + setting.NewContextFromExistingConf() setting.InitDBConfig() if setting.InstallLock { if err := common.InitDBEngine(ctx); err == nil { From 4fb54ffe6ada9063b1ad14783861dfd4fcaf48c2 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sat, 27 Nov 2021 12:56:33 +0800 Subject: [PATCH 20/35] fix lint --- modules/setting/directory.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/modules/setting/directory.go b/modules/setting/directory.go index 6d33c0e8161aa..f7cc248a55714 100644 --- a/modules/setting/directory.go +++ b/modules/setting/directory.go @@ -1,3 +1,7 @@ +// Copyright 2021 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + package setting import ( From bef7eddbb29eae7f69672df48b822cc3ab60d074 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sat, 27 Nov 2021 13:03:00 +0800 Subject: [PATCH 21/35] Fix NewContext call --- cmd/embedded.go | 2 +- modules/setting/setting.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/embedded.go b/cmd/embedded.go index 2aeaba4786fc4..ebe585145e4e7 100644 --- a/cmd/embedded.go +++ b/cmd/embedded.go @@ -115,7 +115,7 @@ func initEmbeddedExtractor(c *cli.Context) error { log.DelNamedLogger(log.DEFAULT) // Read configuration file - setting.NewContext() + setting.NewContextAllowEmptyConf() pats, err := getPatterns(c.Args()) if err != nil { diff --git a/modules/setting/setting.go b/modules/setting/setting.go index 0394d4f6e325a..67324863133df 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -583,7 +583,7 @@ func newContextFromConf(allowEmpty bool) { } } else if !allowEmpty { log.Fatal("Unable to find configuration file: %q.\nEnsure you are running in the correct environment or set the correct configuration file with -c.", CustomConf) - } // else: no config file, a config file might be created at CustomConf (might not) + } // else: no config file, a config file might be created at CustomConf later (might not) Cfg.NameMapper = ini.SnackCase From 7fe52645ff6c9b0c8dcfffb7fb1f928e950da35c Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sat, 27 Nov 2021 13:19:55 +0800 Subject: [PATCH 22/35] Fix unit test --- integrations/integration_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/integrations/integration_test.go b/integrations/integration_test.go index 519e527933955..56264e70541e3 100644 --- a/integrations/integration_test.go +++ b/integrations/integration_test.go @@ -254,6 +254,8 @@ func prepareTestEnv(t testing.TB, skip ...int) func() { assert.NoError(t, util.RemoveAll(setting.RepoRootPath)) assert.NoError(t, util.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"), setting.RepoRootPath)) + assert.NoError(t, setting.PrepareAppDataPath()) + return deferFn } From 1bcd824a209e7bd70ff661e7e3950711336844bd Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sat, 27 Nov 2021 14:05:36 +0800 Subject: [PATCH 23/35] fix unit test --- integrations/integration_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/integrations/integration_test.go b/integrations/integration_test.go index 56264e70541e3..b9039b5a88759 100644 --- a/integrations/integration_test.go +++ b/integrations/integration_test.go @@ -165,7 +165,7 @@ func initIntegrationTest() { setting.SetCustomPathAndConf("", "", "") setting.NewContextForTest() - util.RemoveAll(models.LocalCopyPath()) + _ = util.RemoveAll(models.LocalCopyPath()) git.CheckLFSVersion() setting.InitDBConfig() if err := storage.Init(); err != nil { @@ -240,6 +240,8 @@ func initIntegrationTest() { } defer db.Close() } + + _ = setting.PrepareAppDataPath() routers.GlobalInit(graceful.GetManager().HammerContext()) } @@ -254,7 +256,6 @@ func prepareTestEnv(t testing.TB, skip ...int) func() { assert.NoError(t, util.RemoveAll(setting.RepoRootPath)) assert.NoError(t, util.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"), setting.RepoRootPath)) - assert.NoError(t, setting.PrepareAppDataPath()) return deferFn } From 13eaef55784ee672518c94cb9aaac753692d03bf Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sat, 27 Nov 2021 14:47:23 +0800 Subject: [PATCH 24/35] Fix unit test --- cmd/web.go | 3 ++- integrations/integration_test.go | 3 +-- routers/init.go | 5 ++--- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/cmd/web.go b/cmd/web.go index 0b3eaf67bc1e8..29e945649baf9 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -149,7 +149,8 @@ func runWeb(ctx *cli.Context) error { log.Info("Global init") // Perform global initialization - routers.GlobalInit(graceful.GetManager().HammerContext()) + setting.NewContextFromExistingConf() + routers.GlobalInitInstalled(graceful.GetManager().HammerContext()) // Override the provided port number within the configuration if ctx.IsSet("port") { diff --git a/integrations/integration_test.go b/integrations/integration_test.go index b9039b5a88759..b958d389883ab 100644 --- a/integrations/integration_test.go +++ b/integrations/integration_test.go @@ -241,8 +241,7 @@ func initIntegrationTest() { defer db.Close() } - _ = setting.PrepareAppDataPath() - routers.GlobalInit(graceful.GetManager().HammerContext()) + routers.GlobalInitInstalled(graceful.GetManager().HammerContext()) } func prepareTestEnv(t testing.TB, skip ...int) func() { diff --git a/routers/init.go b/routers/init.go index 58d3201f56efa..4cce7992dbd83 100644 --- a/routers/init.go +++ b/routers/init.go @@ -95,9 +95,8 @@ func syncAppPathForGit(ctx context.Context) error { return nil } -// GlobalInit is for global configuration reload-able. -func GlobalInit(ctx context.Context) { - setting.NewContextFromExistingConf() +// GlobalInitInstalled is for global installed configuration. +func GlobalInitInstalled(ctx context.Context) { if !setting.InstallLock { log.Fatal("Gitea is not installed") } From acda572c902b95ab8ba468b67c6601328a5c7263 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sat, 27 Nov 2021 15:03:33 +0800 Subject: [PATCH 25/35] test --- modules/setting/setting.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/setting/setting.go b/modules/setting/setting.go index 67324863133df..36b2a7dff8778 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -712,7 +712,7 @@ func newContextFromConf(allowEmpty bool) { AppDataPath = sec.Key("APP_DATA_PATH").MustString(path.Join(AppWorkPath, "data")) if _, err = os.Stat(AppDataPath); err != nil { if !allowEmpty { - log.Fatal("Can not find APP_DATA_PATH '%s'", AppDataPath) + log.Fatal("Can not find APP_DATA_PATH '%s', stack:\n%s", AppDataPath, log.Stack(0)) } } EnableGzip = sec.Key("ENABLE_GZIP").MustBool() From 47dded11d7c4dcf2b26bd46df867e42ddd57e858 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sat, 27 Nov 2021 15:19:41 +0800 Subject: [PATCH 26/35] fix test --- cmd/web.go | 7 +++++++ modules/setting/setting.go | 6 +----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/cmd/web.go b/cmd/web.go index 29e945649baf9..f1f2a1fa20175 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -152,6 +152,13 @@ func runWeb(ctx *cli.Context) error { setting.NewContextFromExistingConf() routers.GlobalInitInstalled(graceful.GetManager().HammerContext()) + // Now we can only check the AppDataPath here (the directory should already been created during installation) + // We can not check it in `GlobalInitInstalled`, because some integration tests + // use cmd -> GlobalInitInstalled, but the AppDataPath doesn't exist during the test. + if _, err := os.Stat(setting.AppDataPath); err != nil { + log.Fatal("Can not find APP_DATA_PATH '%s'", setting.AppDataPath) + } + // Override the provided port number within the configuration if ctx.IsSet("port") { if err := setPort(ctx.String("port")); err != nil { diff --git a/modules/setting/setting.go b/modules/setting/setting.go index 36b2a7dff8778..d7ab297596408 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -710,11 +710,7 @@ func newContextFromConf(allowEmpty bool) { StaticRootPath = sec.Key("STATIC_ROOT_PATH").MustString(StaticRootPath) StaticCacheTime = sec.Key("STATIC_CACHE_TIME").MustDuration(6 * time.Hour) AppDataPath = sec.Key("APP_DATA_PATH").MustString(path.Join(AppWorkPath, "data")) - if _, err = os.Stat(AppDataPath); err != nil { - if !allowEmpty { - log.Fatal("Can not find APP_DATA_PATH '%s', stack:\n%s", AppDataPath, log.Stack(0)) - } - } + EnableGzip = sec.Key("ENABLE_GZIP").MustBool() EnablePprof = sec.Key("ENABLE_PPROF").MustBool(false) PprofDataPath = sec.Key("PPROF_DATA_PATH").MustString(path.Join(AppWorkPath, "data/tmp/pprof")) From 007a7d47fa07cbdcacd00d3be498404a04172b69 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sat, 27 Nov 2021 18:54:35 +0800 Subject: [PATCH 27/35] Update modules/setting/setting.go Co-authored-by: zeripath --- modules/setting/setting.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/setting/setting.go b/modules/setting/setting.go index d7ab297596408..bc33e3a9f6a4f 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -1179,7 +1179,7 @@ func CreateOrAppendToCustomConf(callback func(cfg *ini.File)) { callback(cfg) - log.Info("Save settings to custom config file %s", CustomConf) + log.Info("Settings saved to: %q", CustomConf) if err := os.MkdirAll(filepath.Dir(CustomConf), os.ModePerm); err != nil { log.Fatal("failed to create '%s': %v", CustomConf, err) From bc36dc532d98d1214c5d6a41946663447b9855a0 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sat, 27 Nov 2021 18:55:26 +0800 Subject: [PATCH 28/35] Update modules/setting/directory.go Co-authored-by: zeripath --- modules/setting/directory.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/setting/directory.go b/modules/setting/directory.go index f7cc248a55714..7c6ba7fc2087e 100644 --- a/modules/setting/directory.go +++ b/modules/setting/directory.go @@ -33,7 +33,7 @@ func PrepareAppDataPath() error { } if !st.IsDir() /* also works for symlink */ { - return fmt.Errorf("can not use non-directory '%s' as app data path", AppDataPath) + return fmt.Errorf("the APP_DATA_PATH %q is not a directory (or symlink to a directory) and can't be used", AppDataPath) } return nil From 815d8f9edb5e2f5a85bbbb81a31ebb796cffe449 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sat, 27 Nov 2021 18:55:33 +0800 Subject: [PATCH 29/35] Update modules/setting/directory.go Co-authored-by: zeripath --- modules/setting/directory.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/setting/directory.go b/modules/setting/directory.go index 7c6ba7fc2087e..7f4c59ff33429 100644 --- a/modules/setting/directory.go +++ b/modules/setting/directory.go @@ -29,7 +29,7 @@ func PrepareAppDataPath() error { } if err != nil { - return fmt.Errorf("can not use '%s' as app data path, err: %v", AppDataPath, err) + return fmt.Errorf("unable to use APP_DATA_PATH %q. Error: %v", AppDataPath, err) } if !st.IsDir() /* also works for symlink */ { From 3c240c56bf2c53790bbd0e611234a560c3b1ebdc Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sat, 27 Nov 2021 18:55:55 +0800 Subject: [PATCH 30/35] Update cmd/web.go Co-authored-by: zeripath --- cmd/web.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/web.go b/cmd/web.go index f1f2a1fa20175..7163bb9b5dbe7 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -152,9 +152,9 @@ func runWeb(ctx *cli.Context) error { setting.NewContextFromExistingConf() routers.GlobalInitInstalled(graceful.GetManager().HammerContext()) - // Now we can only check the AppDataPath here (the directory should already been created during installation) - // We can not check it in `GlobalInitInstalled`, because some integration tests - // use cmd -> GlobalInitInstalled, but the AppDataPath doesn't exist during the test. + // We check that AppDataPath exists here (it should have been created during installation) + // We can't check it in `GlobalInitInstalled`, because some integration tests + // use cmd -> GlobalInitInstalled, but the AppDataPath doesn't exist during those tests. if _, err := os.Stat(setting.AppDataPath); err != nil { log.Fatal("Can not find APP_DATA_PATH '%s'", setting.AppDataPath) } From 17b98f94ceb1818423def5d4dbc2bb885ddf3d97 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sat, 27 Nov 2021 18:56:03 +0800 Subject: [PATCH 31/35] Update contrib/environment-to-ini/environment-to-ini.go Co-authored-by: zeripath --- contrib/environment-to-ini/environment-to-ini.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/environment-to-ini/environment-to-ini.go b/contrib/environment-to-ini/environment-to-ini.go index 5f63a494cc5d7..ccda03fa92555 100644 --- a/contrib/environment-to-ini/environment-to-ini.go +++ b/contrib/environment-to-ini/environment-to-ini.go @@ -156,7 +156,7 @@ func runEnvironmentToIni(c *cli.Context) error { destination = setting.CustomConf } if destination != setting.CustomConf || changed { - log.Info("Save settings to custom config file %s", destination) + log.Info("Settings saved to: %q", destination) err = cfg.SaveTo(destination) if err != nil { return err From cedb4529ce443b187985011cd2c0adca1f296585 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sat, 27 Nov 2021 18:56:11 +0800 Subject: [PATCH 32/35] Update models/db/engine.go Co-authored-by: zeripath --- models/db/engine.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/db/engine.go b/models/db/engine.go index b07fb9ff4c33e..63e1d5547a275 100755 --- a/models/db/engine.go +++ b/models/db/engine.go @@ -125,7 +125,7 @@ func SyncAllTables() error { return x.StoreEngine("InnoDB").Sync2(tables...) } -// InitEngine initializes the xorm.Engine and set it as db.DefaultContext +// InitEngine initializes the xorm.Engine and sets it as db.DefaultContext func InitEngine(ctx context.Context) error { xormEngine, err := newXORMEngine() if err != nil { From fc0b0d373ec9402ef1c6a2f385e4de943c03c838 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sat, 27 Nov 2021 18:56:44 +0800 Subject: [PATCH 33/35] Update modules/private/internal.go Co-authored-by: zeripath --- modules/private/internal.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/private/internal.go b/modules/private/internal.go index da706f26f4b0e..de07776df85c5 100644 --- a/modules/private/internal.go +++ b/modules/private/internal.go @@ -19,7 +19,8 @@ import ( func newRequest(ctx context.Context, url, method string) *httplib.Request { if setting.InternalToken == "" { - log.Fatal("no internal token, can not send internal request to server. please use correct app.ini") + log.Fatal(`The INTERNAL_TOKEN setting is missing from the configuration file: %q. +Ensure you are running in the correct environment or set the correct configuration file with -c.`, setting.CustomConf) } return httplib.NewRequest(url, method). SetContext(ctx). From d95d1db4e0a57d5b6b249e721f1508a39c7ba1bc Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sat, 27 Nov 2021 18:56:51 +0800 Subject: [PATCH 34/35] Update modules/setting/directory.go Co-authored-by: zeripath --- modules/setting/directory.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/setting/directory.go b/modules/setting/directory.go index 7f4c59ff33429..5dcdd89c04ff4 100644 --- a/modules/setting/directory.go +++ b/modules/setting/directory.go @@ -23,7 +23,7 @@ func PrepareAppDataPath() error { if os.IsNotExist(err) { err = os.MkdirAll(AppDataPath, os.ModePerm) if err != nil { - return fmt.Errorf("failed to create the directory for app data path '%s', err: %v", AppDataPath, err) + return fmt.Errorf("unable to create the APP_DATA_PATH directory: %q, Error: %v", AppDataPath, err) } return nil } From 8dbbc10b8e1c50e439b8be76e769a67038edb243 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sat, 27 Nov 2021 19:06:07 +0800 Subject: [PATCH 35/35] Rename setting.NewContextXxx to setting.LoadXxx --- cmd/cmd.go | 2 +- cmd/doctor.go | 2 +- cmd/dump.go | 2 +- cmd/embedded.go | 2 +- cmd/mailer.go | 2 +- cmd/restore_repo.go | 2 +- cmd/serv.go | 2 +- cmd/web.go | 2 +- contrib/pr/checkout.go | 2 +- integrations/integration_test.go | 2 +- integrations/migration-test/migration_test.go | 2 +- models/migrations/migrations_test.go | 2 +- models/ssh_key_test.go | 2 +- modules/doctor/doctor.go | 2 +- modules/doctor/paths.go | 2 +- modules/private/internal.go | 5 ++--- modules/setting/setting.go | 22 +++++++++---------- routers/install/setting.go | 4 ++-- 18 files changed, 30 insertions(+), 31 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index 4d934cd01d833..fd713b9affd95 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -58,7 +58,7 @@ func confirm() (bool, error) { } func initDB(ctx context.Context) error { - setting.NewContextFromExistingConf() + setting.LoadFromExisting() setting.InitDBConfig() setting.NewXORMLogService(false) diff --git a/cmd/doctor.go b/cmd/doctor.go index d7ebaa2eca30f..54c18b83dc967 100644 --- a/cmd/doctor.go +++ b/cmd/doctor.go @@ -87,7 +87,7 @@ func runRecreateTable(ctx *cli.Context) error { golog.SetPrefix("") golog.SetOutput(log.NewLoggerAsWriter("INFO", log.GetLogger(log.DEFAULT))) - setting.NewContextFromExistingConf() + setting.LoadFromExisting() setting.InitDBConfig() setting.EnableXORMLog = ctx.Bool("debug") diff --git a/cmd/dump.go b/cmd/dump.go index a06bf74126ccc..c158ee21f1479 100644 --- a/cmd/dump.go +++ b/cmd/dump.go @@ -159,7 +159,7 @@ func runDump(ctx *cli.Context) error { fatal("Deleting default logger failed. Can not write to stdout: %v", err) } } - setting.NewContextFromExistingConf() + setting.LoadFromExisting() // make sure we are logging to the console no matter what the configuration tells us do to if _, err := setting.Cfg.Section("log").NewKey("MODE", "console"); err != nil { diff --git a/cmd/embedded.go b/cmd/embedded.go index ebe585145e4e7..c608667bf85ac 100644 --- a/cmd/embedded.go +++ b/cmd/embedded.go @@ -115,7 +115,7 @@ func initEmbeddedExtractor(c *cli.Context) error { log.DelNamedLogger(log.DEFAULT) // Read configuration file - setting.NewContextAllowEmptyConf() + setting.LoadAllowEmpty() pats, err := getPatterns(c.Args()) if err != nil { diff --git a/cmd/mailer.go b/cmd/mailer.go index dcb464de9a5e6..35fcb302f8970 100644 --- a/cmd/mailer.go +++ b/cmd/mailer.go @@ -18,7 +18,7 @@ func runSendMail(c *cli.Context) error { ctx, cancel := installSignals() defer cancel() - setting.NewContextFromExistingConf() + setting.LoadFromExisting() if err := argsSet(c, "title"); err != nil { return err diff --git a/cmd/restore_repo.go b/cmd/restore_repo.go index 549e606a63b8b..357bd92c77551 100644 --- a/cmd/restore_repo.go +++ b/cmd/restore_repo.go @@ -50,7 +50,7 @@ func runRestoreRepository(c *cli.Context) error { ctx, cancel := installSignals() defer cancel() - setting.NewContextFromExistingConf() + setting.LoadFromExisting() statusCode, errStr := private.RestoreRepo( ctx, diff --git a/cmd/serv.go b/cmd/serv.go index 815d97e0befdb..338f3946dcbcc 100644 --- a/cmd/serv.go +++ b/cmd/serv.go @@ -57,7 +57,7 @@ func setup(logPath string, debug bool) { } else { _ = log.NewLogger(1000, "console", "console", `{"level":"fatal","stacktracelevel":"NONE","stderr":true}`) } - setting.NewContextFromExistingConf() + setting.LoadFromExisting() if debug { setting.RunMode = "dev" } diff --git a/cmd/web.go b/cmd/web.go index 7163bb9b5dbe7..fbd62191a6f44 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -149,7 +149,7 @@ func runWeb(ctx *cli.Context) error { log.Info("Global init") // Perform global initialization - setting.NewContextFromExistingConf() + setting.LoadFromExisting() routers.GlobalInitInstalled(graceful.GetManager().HammerContext()) // We check that AppDataPath exists here (it should have been created during installation) diff --git a/contrib/pr/checkout.go b/contrib/pr/checkout.go index feb23dc0d59b4..18c0585f47c55 100644 --- a/contrib/pr/checkout.go +++ b/contrib/pr/checkout.go @@ -49,7 +49,7 @@ func runPR() { log.Fatal(err) } setting.SetCustomPathAndConf("", "", "") - setting.NewContextAllowEmptyConf() + setting.LoadAllowEmpty() setting.RepoRootPath, err = os.MkdirTemp(os.TempDir(), "repos") if err != nil { diff --git a/integrations/integration_test.go b/integrations/integration_test.go index b958d389883ab..6dfc7350ded00 100644 --- a/integrations/integration_test.go +++ b/integrations/integration_test.go @@ -164,7 +164,7 @@ func initIntegrationTest() { } setting.SetCustomPathAndConf("", "", "") - setting.NewContextForTest() + setting.LoadForTest() _ = util.RemoveAll(models.LocalCopyPath()) git.CheckLFSVersion() setting.InitDBConfig() diff --git a/integrations/migration-test/migration_test.go b/integrations/migration-test/migration_test.go index 9e13022f9ccd2..57354c39cb81f 100644 --- a/integrations/migration-test/migration_test.go +++ b/integrations/migration-test/migration_test.go @@ -56,7 +56,7 @@ func initMigrationTest(t *testing.T) func() { setting.CustomConf = giteaConf } - setting.NewContextForTest() + setting.LoadForTest() assert.True(t, len(setting.RepoRootPath) != 0) assert.NoError(t, util.RemoveAll(setting.RepoRootPath)) diff --git a/models/migrations/migrations_test.go b/models/migrations/migrations_test.go index 583ff1e09354b..2effcb99ebc6c 100644 --- a/models/migrations/migrations_test.go +++ b/models/migrations/migrations_test.go @@ -58,7 +58,7 @@ func TestMain(m *testing.M) { } setting.SetCustomPathAndConf("", "", "") - setting.NewContextForTest() + setting.LoadForTest() git.CheckLFSVersion() setting.InitDBConfig() setting.NewLogServices(true) diff --git a/models/ssh_key_test.go b/models/ssh_key_test.go index b3740e2204b4f..b52a36bdbd29f 100644 --- a/models/ssh_key_test.go +++ b/models/ssh_key_test.go @@ -16,7 +16,7 @@ import ( func init() { setting.SetCustomPathAndConf("", "", "") - setting.NewContextForTest() + setting.LoadForTest() } func Test_SSHParsePublicKey(t *testing.T) { diff --git a/modules/doctor/doctor.go b/modules/doctor/doctor.go index 1e4e412763042..20a32f1865dd3 100644 --- a/modules/doctor/doctor.go +++ b/modules/doctor/doctor.go @@ -44,7 +44,7 @@ func (w *wrappedLevelLogger) Log(skip int, level log.Level, format string, v ... } func initDBDisableConsole(ctx context.Context, disableConsole bool) error { - setting.NewContextFromExistingConf() + setting.LoadFromExisting() setting.InitDBConfig() setting.NewXORMLogService(disableConsole) diff --git a/modules/doctor/paths.go b/modules/doctor/paths.go index d7fb49ddb6768..b4eab631ba172 100644 --- a/modules/doctor/paths.go +++ b/modules/doctor/paths.go @@ -67,7 +67,7 @@ func checkConfigurationFiles(logger log.Logger, autofix bool) error { return err } - setting.NewContextFromExistingConf() + setting.LoadFromExisting() configurationFiles := []configurationFile{ {"Configuration File Path", setting.CustomConf, false, true, false}, diff --git a/modules/private/internal.go b/modules/private/internal.go index de07776df85c5..0a39ca7b8e076 100644 --- a/modules/private/internal.go +++ b/modules/private/internal.go @@ -8,12 +8,12 @@ import ( "context" "crypto/tls" "fmt" - "log" "net" "net/http" "code.gitea.io/gitea/modules/httplib" "code.gitea.io/gitea/modules/json" + "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" ) @@ -24,8 +24,7 @@ Ensure you are running in the correct environment or set the correct configurati } return httplib.NewRequest(url, method). SetContext(ctx). - Header("Authorization", - fmt.Sprintf("Bearer %s", setting.InternalToken)) + Header("Authorization", fmt.Sprintf("Bearer %s", setting.InternalToken)) } // Response internal request response diff --git a/modules/setting/setting.go b/modules/setting/setting.go index bc33e3a9f6a4f..19b00a1351eba 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -546,27 +546,27 @@ func SetCustomPathAndConf(providedCustom, providedConf, providedWorkPath string) } } -// NewContextFromExistingConf initializes configuration context from an exisint config file (app.ini) -func NewContextFromExistingConf() { - newContextFromConf(false) +// LoadFromExisting initializes setting options from an existing config file (app.ini) +func LoadFromExisting() { + loadFromConf(false) } -// NewContextAllowEmptyConf initializes configuration context, it's also fine that if the config file (app.ini) doesn't exist -func NewContextAllowEmptyConf() { - newContextFromConf(true) +// LoadAllowEmpty initializes setting options, it's also fine that if the config file (app.ini) doesn't exist +func LoadAllowEmpty() { + loadFromConf(true) } -// NewContextForTest initializes configuration context for tests -func NewContextForTest() { - newContextFromConf(true) +// LoadForTest initializes setting options for tests +func LoadForTest() { + loadFromConf(true) if err := PrepareAppDataPath(); err != nil { log.Fatal("Can not prepare APP_DATA_PATH: %v", err) } } -// newContextFromConf initializes configuration context. +// loadFromConf initializes configuration context. // NOTE: do not print any log except error. -func newContextFromConf(allowEmpty bool) { +func loadFromConf(allowEmpty bool) { Cfg = ini.Empty() if WritePIDFile && len(PIDFile) > 0 { diff --git a/routers/install/setting.go b/routers/install/setting.go index f718854124123..cf0a01ce31f57 100644 --- a/routers/install/setting.go +++ b/routers/install/setting.go @@ -16,7 +16,7 @@ import ( // PreloadSettings preloads the configuration to check if we need to run install func PreloadSettings(ctx context.Context) bool { - setting.NewContextAllowEmptyConf() + setting.LoadAllowEmpty() if !setting.InstallLock { log.Info("AppPath: %s", setting.AppPath) log.Info("AppWorkPath: %s", setting.AppWorkPath) @@ -38,7 +38,7 @@ func PreloadSettings(ctx context.Context) bool { // reloadSettings reloads the existing settings and starts up the database func reloadSettings(ctx context.Context) { - setting.NewContextFromExistingConf() + setting.LoadFromExisting() setting.InitDBConfig() if setting.InstallLock { if err := common.InitDBEngine(ctx); err == nil {