2020-12-01 21:56:04 -07:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 11:20:29 -07:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-12-01 21:56:04 -07:00
|
|
|
|
|
|
|
package doctor
|
|
|
|
|
|
|
|
import (
|
2021-11-06 21:11:27 -06:00
|
|
|
"context"
|
2020-12-01 21:56:04 -07:00
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
2021-09-19 05:49:59 -06:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-08-13 10:33:18 -06:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
2020-12-01 21:56:04 -07:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Check represents a Doctor check
|
|
|
|
type Check struct {
|
|
|
|
Title string
|
|
|
|
Name string
|
|
|
|
IsDefault bool
|
2022-01-19 16:26:57 -07:00
|
|
|
Run func(ctx context.Context, logger log.Logger, autofix bool) error
|
2020-12-01 21:56:04 -07:00
|
|
|
AbortIfFailed bool
|
|
|
|
SkipDatabaseInitialization bool
|
|
|
|
Priority int
|
|
|
|
}
|
|
|
|
|
|
|
|
type wrappedLevelLogger struct {
|
|
|
|
log.LevelLogger
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wrappedLevelLogger) Log(skip int, level log.Level, format string, v ...interface{}) error {
|
|
|
|
return w.LevelLogger.Log(
|
|
|
|
skip+1,
|
|
|
|
level,
|
|
|
|
" - %s "+format,
|
|
|
|
append(
|
|
|
|
[]interface{}{
|
|
|
|
log.NewColoredValueBytes(
|
|
|
|
fmt.Sprintf("[%s]", strings.ToUpper(level.String()[0:1])),
|
|
|
|
level.Color()),
|
|
|
|
}, v...)...)
|
|
|
|
}
|
|
|
|
|
2021-11-06 21:11:27 -06:00
|
|
|
func initDBDisableConsole(ctx context.Context, disableConsole bool) error {
|
2023-02-19 09:12:01 -07:00
|
|
|
setting.InitProviderFromExistingFile()
|
|
|
|
setting.LoadCommonSettings()
|
|
|
|
setting.LoadDBSetting()
|
|
|
|
setting.InitSQLLog(disableConsole)
|
2021-11-06 21:11:27 -06:00
|
|
|
if err := db.InitEngine(ctx); err != nil {
|
2022-08-13 10:33:18 -06:00
|
|
|
return fmt.Errorf("db.InitEngine: %w", err)
|
|
|
|
}
|
|
|
|
// some doctor sub-commands need to use git command
|
|
|
|
if err := git.InitFull(ctx); err != nil {
|
|
|
|
return fmt.Errorf("git.InitFull: %w", err)
|
2020-12-01 21:56:04 -07:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checks is the list of available commands
|
|
|
|
var Checks []*Check
|
|
|
|
|
|
|
|
// RunChecks runs the doctor checks for the provided list
|
2021-11-06 21:11:27 -06:00
|
|
|
func RunChecks(ctx context.Context, logger log.Logger, autofix bool, checks []*Check) error {
|
2020-12-01 21:56:04 -07:00
|
|
|
wrappedLogger := log.LevelLoggerLogger{
|
|
|
|
LevelLogger: &wrappedLevelLogger{logger},
|
|
|
|
}
|
|
|
|
|
|
|
|
dbIsInit := false
|
|
|
|
for i, check := range checks {
|
|
|
|
if !dbIsInit && !check.SkipDatabaseInitialization {
|
|
|
|
// Only open database after the most basic configuration check
|
2023-02-19 09:12:01 -07:00
|
|
|
setting.Log.EnableXORMLog = false
|
2021-11-06 21:11:27 -06:00
|
|
|
if err := initDBDisableConsole(ctx, true); err != nil {
|
2020-12-01 21:56:04 -07:00
|
|
|
logger.Error("Error whilst initializing the database: %v", err)
|
|
|
|
logger.Error("Check if you are using the right config file. You can use a --config directive to specify one.")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
dbIsInit = true
|
|
|
|
}
|
|
|
|
logger.Info("[%d] %s", log.NewColoredIDValue(i+1), check.Title)
|
|
|
|
logger.Flush()
|
2022-01-19 16:26:57 -07:00
|
|
|
if err := check.Run(ctx, &wrappedLogger, autofix); err != nil {
|
2020-12-01 21:56:04 -07:00
|
|
|
if check.AbortIfFailed {
|
|
|
|
logger.Critical("FAIL")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
logger.Error("ERROR")
|
|
|
|
} else {
|
|
|
|
logger.Info("OK")
|
|
|
|
logger.Flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register registers a command with the list
|
|
|
|
func Register(command *Check) {
|
|
|
|
Checks = append(Checks, command)
|
|
|
|
sort.SliceStable(Checks, func(i, j int) bool {
|
|
|
|
if Checks[i].Priority == Checks[j].Priority {
|
|
|
|
return Checks[i].Name < Checks[j].Name
|
|
|
|
}
|
|
|
|
if Checks[i].Priority == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return Checks[i].Priority < Checks[j].Priority
|
|
|
|
})
|
|
|
|
}
|