You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

doctor.go 2.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package doctor
  5. import (
  6. "fmt"
  7. "sort"
  8. "strings"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/log"
  11. "code.gitea.io/gitea/modules/setting"
  12. )
  13. // Check represents a Doctor check
  14. type Check struct {
  15. Title string
  16. Name string
  17. IsDefault bool
  18. Run func(logger log.Logger, autofix bool) error
  19. AbortIfFailed bool
  20. SkipDatabaseInitialization bool
  21. Priority int
  22. }
  23. type wrappedLevelLogger struct {
  24. log.LevelLogger
  25. }
  26. func (w *wrappedLevelLogger) Log(skip int, level log.Level, format string, v ...interface{}) error {
  27. return w.LevelLogger.Log(
  28. skip+1,
  29. level,
  30. " - %s "+format,
  31. append(
  32. []interface{}{
  33. log.NewColoredValueBytes(
  34. fmt.Sprintf("[%s]", strings.ToUpper(level.String()[0:1])),
  35. level.Color()),
  36. }, v...)...)
  37. }
  38. func initDBDisableConsole(disableConsole bool) error {
  39. setting.NewContext()
  40. setting.InitDBConfig()
  41. setting.NewXORMLogService(disableConsole)
  42. if err := models.SetEngine(); err != nil {
  43. return fmt.Errorf("models.SetEngine: %v", err)
  44. }
  45. return nil
  46. }
  47. // Checks is the list of available commands
  48. var Checks []*Check
  49. // RunChecks runs the doctor checks for the provided list
  50. func RunChecks(logger log.Logger, autofix bool, checks []*Check) error {
  51. wrappedLogger := log.LevelLoggerLogger{
  52. LevelLogger: &wrappedLevelLogger{logger},
  53. }
  54. dbIsInit := false
  55. for i, check := range checks {
  56. if !dbIsInit && !check.SkipDatabaseInitialization {
  57. // Only open database after the most basic configuration check
  58. setting.EnableXORMLog = false
  59. if err := initDBDisableConsole(true); err != nil {
  60. logger.Error("Error whilst initializing the database: %v", err)
  61. logger.Error("Check if you are using the right config file. You can use a --config directive to specify one.")
  62. return nil
  63. }
  64. dbIsInit = true
  65. }
  66. logger.Info("[%d] %s", log.NewColoredIDValue(i+1), check.Title)
  67. logger.Flush()
  68. if err := check.Run(&wrappedLogger, autofix); err != nil {
  69. if check.AbortIfFailed {
  70. logger.Critical("FAIL")
  71. return err
  72. }
  73. logger.Error("ERROR")
  74. } else {
  75. logger.Info("OK")
  76. logger.Flush()
  77. }
  78. }
  79. return nil
  80. }
  81. // Register registers a command with the list
  82. func Register(command *Check) {
  83. Checks = append(Checks, command)
  84. sort.SliceStable(Checks, func(i, j int) bool {
  85. if Checks[i].Priority == Checks[j].Priority {
  86. return Checks[i].Name < Checks[j].Name
  87. }
  88. if Checks[i].Priority == 0 {
  89. return false
  90. }
  91. return Checks[i].Priority < Checks[j].Priority
  92. })
  93. }