選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

doctor.go 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package cmd
  4. import (
  5. "fmt"
  6. golog "log"
  7. "os"
  8. "path/filepath"
  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/container"
  15. "code.gitea.io/gitea/modules/log"
  16. "code.gitea.io/gitea/modules/setting"
  17. "code.gitea.io/gitea/services/doctor"
  18. "github.com/urfave/cli/v2"
  19. "xorm.io/xorm"
  20. )
  21. // CmdDoctor represents the available doctor sub-command.
  22. var CmdDoctor = &cli.Command{
  23. Name: "doctor",
  24. Usage: "Diagnose and optionally fix problems, convert or re-create database tables",
  25. 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.",
  26. Subcommands: []*cli.Command{
  27. cmdDoctorCheck,
  28. cmdRecreateTable,
  29. cmdDoctorConvert,
  30. },
  31. }
  32. var cmdDoctorCheck = &cli.Command{
  33. Name: "check",
  34. Usage: "Diagnose and optionally fix problems",
  35. 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.",
  36. Action: runDoctorCheck,
  37. Flags: []cli.Flag{
  38. &cli.BoolFlag{
  39. Name: "list",
  40. Usage: "List the available checks",
  41. },
  42. &cli.BoolFlag{
  43. Name: "default",
  44. Usage: "Run the default checks (if neither --run or --all is set, this is the default behaviour)",
  45. },
  46. &cli.StringSliceFlag{
  47. Name: "run",
  48. Usage: "Run the provided checks - (if --default is set, the default checks will also run)",
  49. },
  50. &cli.BoolFlag{
  51. Name: "all",
  52. Usage: "Run all the available checks",
  53. },
  54. &cli.BoolFlag{
  55. Name: "fix",
  56. Usage: "Automatically fix what we can",
  57. },
  58. &cli.StringFlag{
  59. Name: "log-file",
  60. Usage: `Name of the log file (no verbose log output by default). Set to "-" to output to stdout`,
  61. },
  62. &cli.BoolFlag{
  63. Name: "color",
  64. Aliases: []string{"H"},
  65. Usage: "Use color for outputted information",
  66. },
  67. },
  68. }
  69. var cmdRecreateTable = &cli.Command{
  70. Name: "recreate-table",
  71. Usage: "Recreate tables from XORM definitions and copy the data.",
  72. ArgsUsage: "[TABLE]... : (TABLEs to recreate - leave blank for all)",
  73. Flags: []cli.Flag{
  74. &cli.BoolFlag{
  75. Name: "debug",
  76. Usage: "Print SQL commands sent",
  77. },
  78. },
  79. Description: `The database definitions Gitea uses change across versions, sometimes changing default values and leaving old unused columns.
  80. This command will cause Xorm to recreate tables, copying over the data and deleting the old table.
  81. You should back-up your database before doing this and ensure that your database is up-to-date first.`,
  82. Action: runRecreateTable,
  83. }
  84. func runRecreateTable(ctx *cli.Context) error {
  85. stdCtx, cancel := installSignals()
  86. defer cancel()
  87. // Redirect the default golog to here
  88. golog.SetFlags(0)
  89. golog.SetPrefix("")
  90. golog.SetOutput(log.LoggerToWriter(log.GetLogger(log.DEFAULT).Info))
  91. debug := ctx.Bool("debug")
  92. setting.MustInstalled()
  93. setting.LoadDBSetting()
  94. if debug {
  95. setting.InitSQLLoggersForCli(log.DEBUG)
  96. } else {
  97. setting.InitSQLLoggersForCli(log.INFO)
  98. }
  99. setting.Database.LogSQL = debug
  100. if err := db.InitEngine(stdCtx); err != nil {
  101. fmt.Println(err)
  102. fmt.Println("Check if you are using the right config file. You can use a --config directive to specify one.")
  103. return nil
  104. }
  105. args := ctx.Args()
  106. names := make([]string, 0, ctx.NArg())
  107. for i := 0; i < ctx.NArg(); i++ {
  108. names = append(names, args.Get(i))
  109. }
  110. beans, err := db.NamesToBean(names...)
  111. if err != nil {
  112. return err
  113. }
  114. recreateTables := migrate_base.RecreateTables(beans...)
  115. return db.InitEngineWithMigration(stdCtx, func(x *xorm.Engine) error {
  116. if err := migrations.EnsureUpToDate(x); err != nil {
  117. return err
  118. }
  119. return recreateTables(x)
  120. })
  121. }
  122. func setupDoctorDefaultLogger(ctx *cli.Context, colorize bool) {
  123. // Silence the default loggers
  124. setupConsoleLogger(log.FATAL, log.CanColorStderr, os.Stderr)
  125. logFile := ctx.String("log-file")
  126. if logFile == "" {
  127. return // if no doctor log-file is set, do not show any log from default logger
  128. } else if logFile == "-" {
  129. setupConsoleLogger(log.TRACE, colorize, os.Stdout)
  130. } else {
  131. logFile, _ = filepath.Abs(logFile)
  132. writeMode := log.WriterMode{Level: log.TRACE, WriterOption: log.WriterFileOption{FileName: logFile}}
  133. writer, err := log.NewEventWriter("console-to-file", "file", writeMode)
  134. if err != nil {
  135. log.FallbackErrorf("unable to create file log writer: %v", err)
  136. return
  137. }
  138. log.GetManager().GetLogger(log.DEFAULT).ReplaceAllWriters(writer)
  139. }
  140. }
  141. func runDoctorCheck(ctx *cli.Context) error {
  142. stdCtx, cancel := installSignals()
  143. defer cancel()
  144. colorize := log.CanColorStdout
  145. if ctx.IsSet("color") {
  146. colorize = ctx.Bool("color")
  147. }
  148. setupDoctorDefaultLogger(ctx, colorize)
  149. // Finally redirect the default golang's log to here
  150. golog.SetFlags(0)
  151. golog.SetPrefix("")
  152. golog.SetOutput(log.LoggerToWriter(log.GetLogger(log.DEFAULT).Info))
  153. if ctx.IsSet("list") {
  154. w := tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', 0)
  155. _, _ = w.Write([]byte("Default\tName\tTitle\n"))
  156. doctor.SortChecks(doctor.Checks)
  157. for _, check := range doctor.Checks {
  158. if check.IsDefault {
  159. _, _ = w.Write([]byte{'*'})
  160. }
  161. _, _ = w.Write([]byte{'\t'})
  162. _, _ = w.Write([]byte(check.Name))
  163. _, _ = w.Write([]byte{'\t'})
  164. _, _ = w.Write([]byte(check.Title))
  165. _, _ = w.Write([]byte{'\n'})
  166. }
  167. return w.Flush()
  168. }
  169. var checks []*doctor.Check
  170. if ctx.Bool("all") {
  171. checks = make([]*doctor.Check, len(doctor.Checks))
  172. copy(checks, doctor.Checks)
  173. } else if ctx.IsSet("run") {
  174. addDefault := ctx.Bool("default")
  175. runNamesSet := container.SetOf(ctx.StringSlice("run")...)
  176. for _, check := range doctor.Checks {
  177. if (addDefault && check.IsDefault) || runNamesSet.Contains(check.Name) {
  178. checks = append(checks, check)
  179. runNamesSet.Remove(check.Name)
  180. }
  181. }
  182. if len(runNamesSet) > 0 {
  183. return fmt.Errorf("unknown checks: %q", strings.Join(runNamesSet.Values(), ","))
  184. }
  185. } else {
  186. for _, check := range doctor.Checks {
  187. if check.IsDefault {
  188. checks = append(checks, check)
  189. }
  190. }
  191. }
  192. return doctor.RunChecks(stdCtx, colorize, ctx.Bool("fix"), checks)
  193. }