您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

doctor.go 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package cmd
  4. import (
  5. "errors"
  6. "fmt"
  7. golog "log"
  8. "os"
  9. "strings"
  10. "text/tabwriter"
  11. "code.gitea.io/gitea/models/db"
  12. "code.gitea.io/gitea/models/migrations"
  13. migrate_base "code.gitea.io/gitea/models/migrations/base"
  14. "code.gitea.io/gitea/modules/doctor"
  15. "code.gitea.io/gitea/modules/log"
  16. "code.gitea.io/gitea/modules/setting"
  17. "github.com/urfave/cli"
  18. "xorm.io/xorm"
  19. )
  20. // CmdDoctor represents the available doctor sub-command.
  21. var CmdDoctor = cli.Command{
  22. Name: "doctor",
  23. Usage: "Diagnose and optionally fix problems",
  24. Description: "A command to diagnose problems with the current Gitea instance according to the given configuration. Some problems can optionally be fixed by modifying the database or data storage.",
  25. Action: runDoctor,
  26. Flags: []cli.Flag{
  27. cli.BoolFlag{
  28. Name: "list",
  29. Usage: "List the available checks",
  30. },
  31. cli.BoolFlag{
  32. Name: "default",
  33. Usage: "Run the default checks (if neither --run or --all is set, this is the default behaviour)",
  34. },
  35. cli.StringSliceFlag{
  36. Name: "run",
  37. Usage: "Run the provided checks - (if --default is set, the default checks will also run)",
  38. },
  39. cli.BoolFlag{
  40. Name: "all",
  41. Usage: "Run all the available checks",
  42. },
  43. cli.BoolFlag{
  44. Name: "fix",
  45. Usage: "Automatically fix what we can",
  46. },
  47. cli.StringFlag{
  48. Name: "log-file",
  49. Usage: `Name of the log file (default: "doctor.log"). Set to "-" to output to stdout, set to "" to disable`,
  50. },
  51. cli.BoolFlag{
  52. Name: "color, H",
  53. Usage: "Use color for outputted information",
  54. },
  55. },
  56. Subcommands: []cli.Command{
  57. cmdRecreateTable,
  58. },
  59. }
  60. var cmdRecreateTable = cli.Command{
  61. Name: "recreate-table",
  62. Usage: "Recreate tables from XORM definitions and copy the data.",
  63. ArgsUsage: "[TABLE]... : (TABLEs to recreate - leave blank for all)",
  64. Flags: []cli.Flag{
  65. cli.BoolFlag{
  66. Name: "debug",
  67. Usage: "Print SQL commands sent",
  68. },
  69. },
  70. Description: `The database definitions Gitea uses change across versions, sometimes changing default values and leaving old unused columns.
  71. This command will cause Xorm to recreate tables, copying over the data and deleting the old table.
  72. You should back-up your database before doing this and ensure that your database is up-to-date first.`,
  73. Action: runRecreateTable,
  74. }
  75. func runRecreateTable(ctx *cli.Context) error {
  76. // Redirect the default golog to here
  77. golog.SetFlags(0)
  78. golog.SetPrefix("")
  79. golog.SetOutput(log.NewLoggerAsWriter("INFO", log.GetLogger(log.DEFAULT)))
  80. setting.InitProviderFromExistingFile()
  81. setting.LoadCommonSettings()
  82. setting.LoadDBSetting()
  83. setting.Log.EnableXORMLog = ctx.Bool("debug")
  84. setting.Database.LogSQL = ctx.Bool("debug")
  85. // FIXME: don't use CfgProvider directly
  86. setting.CfgProvider.Section("log").Key("XORM").SetValue(",")
  87. setting.InitSQLLog(!ctx.Bool("debug"))
  88. stdCtx, cancel := installSignals()
  89. defer cancel()
  90. if err := db.InitEngine(stdCtx); err != nil {
  91. fmt.Println(err)
  92. fmt.Println("Check if you are using the right config file. You can use a --config directive to specify one.")
  93. return nil
  94. }
  95. args := ctx.Args()
  96. names := make([]string, 0, ctx.NArg())
  97. for i := 0; i < ctx.NArg(); i++ {
  98. names = append(names, args.Get(i))
  99. }
  100. beans, err := db.NamesToBean(names...)
  101. if err != nil {
  102. return err
  103. }
  104. recreateTables := migrate_base.RecreateTables(beans...)
  105. return db.InitEngineWithMigration(stdCtx, func(x *xorm.Engine) error {
  106. if err := migrations.EnsureUpToDate(x); err != nil {
  107. return err
  108. }
  109. return recreateTables(x)
  110. })
  111. }
  112. func setDoctorLogger(ctx *cli.Context) {
  113. logFile := ctx.String("log-file")
  114. if !ctx.IsSet("log-file") {
  115. logFile = "doctor.log"
  116. }
  117. colorize := log.CanColorStdout
  118. if ctx.IsSet("color") {
  119. colorize = ctx.Bool("color")
  120. }
  121. if len(logFile) == 0 {
  122. log.NewLogger(1000, "doctor", "console", fmt.Sprintf(`{"level":"NONE","stacktracelevel":"NONE","colorize":%t}`, colorize))
  123. return
  124. }
  125. defer func() {
  126. recovered := recover()
  127. if recovered == nil {
  128. return
  129. }
  130. err, ok := recovered.(error)
  131. if !ok {
  132. panic(recovered)
  133. }
  134. if errors.Is(err, os.ErrPermission) {
  135. fmt.Fprintf(os.Stderr, "ERROR: Unable to write logs to provided file due to permissions error: %s\n %v\n", logFile, err)
  136. } else {
  137. fmt.Fprintf(os.Stderr, "ERROR: Unable to write logs to provided file: %s\n %v\n", logFile, err)
  138. }
  139. fmt.Fprintf(os.Stderr, "WARN: Logging will be disabled\n Use `--log-file` to configure log file location\n")
  140. log.NewLogger(1000, "doctor", "console", fmt.Sprintf(`{"level":"NONE","stacktracelevel":"NONE","colorize":%t}`, colorize))
  141. }()
  142. if logFile == "-" {
  143. log.NewLogger(1000, "doctor", "console", fmt.Sprintf(`{"level":"trace","stacktracelevel":"NONE","colorize":%t}`, colorize))
  144. } else {
  145. log.NewLogger(1000, "doctor", "file", fmt.Sprintf(`{"filename":%q,"level":"trace","stacktracelevel":"NONE"}`, logFile))
  146. }
  147. }
  148. func runDoctor(ctx *cli.Context) error {
  149. stdCtx, cancel := installSignals()
  150. defer cancel()
  151. // Silence the default loggers
  152. log.DelNamedLogger("console")
  153. log.DelNamedLogger(log.DEFAULT)
  154. // Now setup our own
  155. setDoctorLogger(ctx)
  156. colorize := log.CanColorStdout
  157. if ctx.IsSet("color") {
  158. colorize = ctx.Bool("color")
  159. }
  160. // Finally redirect the default golog to here
  161. golog.SetFlags(0)
  162. golog.SetPrefix("")
  163. golog.SetOutput(log.NewLoggerAsWriter("INFO", log.GetLogger(log.DEFAULT)))
  164. if ctx.IsSet("list") {
  165. w := tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', 0)
  166. _, _ = w.Write([]byte("Default\tName\tTitle\n"))
  167. for _, check := range doctor.Checks {
  168. if check.IsDefault {
  169. _, _ = w.Write([]byte{'*'})
  170. }
  171. _, _ = w.Write([]byte{'\t'})
  172. _, _ = w.Write([]byte(check.Name))
  173. _, _ = w.Write([]byte{'\t'})
  174. _, _ = w.Write([]byte(check.Title))
  175. _, _ = w.Write([]byte{'\n'})
  176. }
  177. return w.Flush()
  178. }
  179. var checks []*doctor.Check
  180. if ctx.Bool("all") {
  181. checks = doctor.Checks
  182. } else if ctx.IsSet("run") {
  183. addDefault := ctx.Bool("default")
  184. names := ctx.StringSlice("run")
  185. for i, name := range names {
  186. names[i] = strings.ToLower(strings.TrimSpace(name))
  187. }
  188. for _, check := range doctor.Checks {
  189. if addDefault && check.IsDefault {
  190. checks = append(checks, check)
  191. continue
  192. }
  193. for _, name := range names {
  194. if name == check.Name {
  195. checks = append(checks, check)
  196. break
  197. }
  198. }
  199. }
  200. } else {
  201. for _, check := range doctor.Checks {
  202. if check.IsDefault {
  203. checks = append(checks, check)
  204. }
  205. }
  206. }
  207. // Now we can set up our own logger to return information about what the doctor is doing
  208. if err := log.NewNamedLogger("doctorouter",
  209. 0,
  210. "console",
  211. "console",
  212. fmt.Sprintf(`{"level":"INFO","stacktracelevel":"NONE","colorize":%t,"flags":-1}`, colorize)); err != nil {
  213. fmt.Println(err)
  214. return err
  215. }
  216. logger := log.GetLogger("doctorouter")
  217. defer logger.Close()
  218. return doctor.RunChecks(stdCtx, logger, ctx.Bool("fix"), checks)
  219. }