Skip to content

Commit 042cac5

Browse files
authored
Improve install code to avoid low-level mistakes. (#17779)
* 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. Also some small (related) refactoring: * Refactor db.InitEngine related logic make it more clean (especially for the install code) * Move some i18n strings out from setting.go to make the setting.go can be easily maintained. * Show errors in CLI code if an incorrect app.ini is used. * APP_DATA_PATH is created when installing, and checked when starting (no empty directory is created any more).
1 parent a3517d8 commit 042cac5

File tree

36 files changed

+472
-177
lines changed

36 files changed

+472
-177
lines changed

cmd/cmd.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"syscall"
1717

1818
"code.gitea.io/gitea/models/db"
19+
"code.gitea.io/gitea/modules/log"
1920
"code.gitea.io/gitea/modules/setting"
2021
"code.gitea.io/gitea/modules/util"
2122

@@ -57,15 +58,17 @@ func confirm() (bool, error) {
5758
}
5859

5960
func initDB(ctx context.Context) error {
60-
return initDBDisableConsole(ctx, false)
61-
}
62-
63-
func initDBDisableConsole(ctx context.Context, disableConsole bool) error {
64-
setting.NewContext()
61+
setting.LoadFromExisting()
6562
setting.InitDBConfig()
66-
setting.NewXORMLogService(disableConsole)
63+
setting.NewXORMLogService(false)
64+
65+
if setting.Database.Type == "" {
66+
log.Fatal(`Database settings are missing from the configuration file: %q.
67+
Ensure you are running in the correct environment or set the correct configuration file with -c.
68+
If this is the intended configuration file complete the [database] section.`, setting.CustomConf)
69+
}
6770
if err := db.InitEngine(ctx); err != nil {
68-
return fmt.Errorf("models.SetEngine: %v", err)
71+
return fmt.Errorf("unable to initialise the database using the configuration in %q. Error: %v", setting.CustomConf, err)
6972
}
7073
return nil
7174
}

cmd/convert.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ func runConvert(ctx *cli.Context) error {
3535
log.Info("Custom path: %s", setting.CustomPath)
3636
log.Info("Log path: %s", setting.LogRootPath)
3737
log.Info("Configuration file: %s", setting.CustomConf)
38-
setting.InitDBConfig()
3938

4039
if !setting.Database.UseMySQL {
4140
fmt.Println("This command can only be used with a MySQL database")

cmd/doctor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func runRecreateTable(ctx *cli.Context) error {
8787
golog.SetPrefix("")
8888
golog.SetOutput(log.NewLoggerAsWriter("INFO", log.GetLogger(log.DEFAULT)))
8989

90-
setting.NewContext()
90+
setting.LoadFromExisting()
9191
setting.InitDBConfig()
9292

9393
setting.EnableXORMLog = ctx.Bool("debug")

cmd/dump.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ func runDump(ctx *cli.Context) error {
159159
fatal("Deleting default logger failed. Can not write to stdout: %v", err)
160160
}
161161
}
162-
setting.NewContext()
162+
setting.LoadFromExisting()
163+
163164
// make sure we are logging to the console no matter what the configuration tells us do to
164165
if _, err := setting.Cfg.Section("log").NewKey("MODE", "console"); err != nil {
165166
fatal("Setting logging mode to console failed: %v", err)

cmd/dump_repo.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ func runDumpRepository(ctx *cli.Context) error {
8888
log.Info("Custom path: %s", setting.CustomPath)
8989
log.Info("Log path: %s", setting.LogRootPath)
9090
log.Info("Configuration file: %s", setting.CustomConf)
91-
setting.InitDBConfig()
9291

9392
var (
9493
serviceType structs.GitServiceType

cmd/embedded.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func initEmbeddedExtractor(c *cli.Context) error {
115115
log.DelNamedLogger(log.DEFAULT)
116116

117117
// Read configuration file
118-
setting.NewContext()
118+
setting.LoadAllowEmpty()
119119

120120
pats, err := getPatterns(c.Args())
121121
if err != nil {

cmd/mailer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func runSendMail(c *cli.Context) error {
1818
ctx, cancel := installSignals()
1919
defer cancel()
2020

21-
setting.NewContext()
21+
setting.LoadFromExisting()
2222

2323
if err := argsSet(c, "title"); err != nil {
2424
return err

cmd/migrate.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ func runMigrate(ctx *cli.Context) error {
3636
log.Info("Custom path: %s", setting.CustomPath)
3737
log.Info("Log path: %s", setting.LogRootPath)
3838
log.Info("Configuration file: %s", setting.CustomConf)
39-
setting.InitDBConfig()
4039

4140
if err := db.InitEngineWithMigration(context.Background(), migrations.Migrate); err != nil {
4241
log.Fatal("Failed to initialize ORM engine: %v", err)

cmd/migrate_storage.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ func runMigrateStorage(ctx *cli.Context) error {
121121
log.Info("Custom path: %s", setting.CustomPath)
122122
log.Info("Log path: %s", setting.LogRootPath)
123123
log.Info("Configuration file: %s", setting.CustomConf)
124-
setting.InitDBConfig()
125124

126125
if err := db.InitEngineWithMigration(context.Background(), migrations.Migrate); err != nil {
127126
log.Fatal("Failed to initialize ORM engine: %v", err)

cmd/restore_repo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func runRestoreRepository(c *cli.Context) error {
5050
ctx, cancel := installSignals()
5151
defer cancel()
5252

53-
setting.NewContext()
53+
setting.LoadFromExisting()
5454

5555
statusCode, errStr := private.RestoreRepo(
5656
ctx,

0 commit comments

Comments
 (0)