2020-12-02 10:26:04 +05:30
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2020-12-02 10:26:04 +05:30
|
|
|
|
|
|
|
package doctor
|
|
|
|
|
|
|
|
import (
|
2021-11-07 08:41:27 +05:30
|
|
|
"context"
|
2020-12-02 10:26:04 +05:30
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
2021-09-19 17:19:59 +05:30
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-08-13 22:03:18 +05:30
|
|
|
"code.gitea.io/gitea/modules/git"
|
2020-12-02 10:26:04 +05:30
|
|
|
"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-20 04:56:57 +05:30
|
|
|
Run func(ctx context.Context, logger log.Logger, autofix bool) error
|
2020-12-02 10:26:04 +05:30
|
|
|
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-07 08:41:27 +05:30
|
|
|
func initDBDisableConsole(ctx context.Context, disableConsole bool) error {
|
2023-05-04 09:25:35 +05:30
|
|
|
setting.Init(&setting.Options{})
|
2023-02-19 21:42:01 +05:30
|
|
|
setting.LoadDBSetting()
|
|
|
|
setting.InitSQLLog(disableConsole)
|
2021-11-07 08:41:27 +05:30
|
|
|
if err := db.InitEngine(ctx); err != nil {
|
2022-08-13 22:03:18 +05:30
|
|
|
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-02 10:26:04 +05:30
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checks is the list of available commands
|
|
|
|
var Checks []*Check
|
|
|
|
|
|
|
|
// RunChecks runs the doctor checks for the provided list
|
2021-11-07 08:41:27 +05:30
|
|
|
func RunChecks(ctx context.Context, logger log.Logger, autofix bool, checks []*Check) error {
|
2020-12-02 10:26:04 +05:30
|
|
|
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 21:42:01 +05:30
|
|
|
setting.Log.EnableXORMLog = false
|
2021-11-07 08:41:27 +05:30
|
|
|
if err := initDBDisableConsole(ctx, true); err != nil {
|
2020-12-02 10:26:04 +05:30
|
|
|
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-20 04:56:57 +05:30
|
|
|
if err := check.Run(ctx, &wrappedLogger, autofix); err != nil {
|
2020-12-02 10:26:04 +05:30
|
|
|
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
|
|
|
|
})
|
|
|
|
}
|