Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

doctor.go 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // Copyright 2019 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 cmd
  5. import (
  6. "context"
  7. "fmt"
  8. golog "log"
  9. "os"
  10. "strings"
  11. "text/tabwriter"
  12. "code.gitea.io/gitea/models"
  13. "code.gitea.io/gitea/models/migrations"
  14. "code.gitea.io/gitea/modules/doctor"
  15. "code.gitea.io/gitea/modules/log"
  16. "code.gitea.io/gitea/modules/setting"
  17. "xorm.io/xorm"
  18. "github.com/urfave/cli"
  19. )
  20. // CmdDoctor represents the available doctor sub-command.
  21. var CmdDoctor = cli.Command{
  22. Name: "doctor",
  23. Usage: "Diagnose problems",
  24. Description: "A command to diagnose problems with the current Gitea instance according to the given configuration.",
  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.NewContext()
  81. setting.InitDBConfig()
  82. setting.EnableXORMLog = ctx.Bool("debug")
  83. setting.Database.LogSQL = ctx.Bool("debug")
  84. setting.Cfg.Section("log").Key("XORM").SetValue(",")
  85. setting.NewXORMLogService(!ctx.Bool("debug"))
  86. if err := models.SetEngine(); err != nil {
  87. fmt.Println(err)
  88. fmt.Println("Check if you are using the right config file. You can use a --config directive to specify one.")
  89. return nil
  90. }
  91. args := ctx.Args()
  92. names := make([]string, 0, ctx.NArg())
  93. for i := 0; i < ctx.NArg(); i++ {
  94. names = append(names, args.Get(i))
  95. }
  96. beans, err := models.NamesToBean(names...)
  97. if err != nil {
  98. return err
  99. }
  100. recreateTables := migrations.RecreateTables(beans...)
  101. return models.NewEngine(context.Background(), func(x *xorm.Engine) error {
  102. if err := migrations.EnsureUpToDate(x); err != nil {
  103. return err
  104. }
  105. return recreateTables(x)
  106. })
  107. }
  108. func runDoctor(ctx *cli.Context) error {
  109. // Silence the default loggers
  110. log.DelNamedLogger("console")
  111. log.DelNamedLogger(log.DEFAULT)
  112. // Now setup our own
  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. } else if logFile == "-" {
  124. log.NewLogger(1000, "doctor", "console", fmt.Sprintf(`{"level":"trace","stacktracelevel":"NONE","colorize":%t}`, colorize))
  125. } else {
  126. log.NewLogger(1000, "doctor", "file", fmt.Sprintf(`{"filename":%q,"level":"trace","stacktracelevel":"NONE"}`, logFile))
  127. }
  128. // Finally redirect the default golog to here
  129. golog.SetFlags(0)
  130. golog.SetPrefix("")
  131. golog.SetOutput(log.NewLoggerAsWriter("INFO", log.GetLogger(log.DEFAULT)))
  132. if ctx.IsSet("list") {
  133. w := tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', 0)
  134. _, _ = w.Write([]byte("Default\tName\tTitle\n"))
  135. for _, check := range doctor.Checks {
  136. if check.IsDefault {
  137. _, _ = w.Write([]byte{'*'})
  138. }
  139. _, _ = w.Write([]byte{'\t'})
  140. _, _ = w.Write([]byte(check.Name))
  141. _, _ = w.Write([]byte{'\t'})
  142. _, _ = w.Write([]byte(check.Title))
  143. _, _ = w.Write([]byte{'\n'})
  144. }
  145. return w.Flush()
  146. }
  147. var checks []*doctor.Check
  148. if ctx.Bool("all") {
  149. checks = doctor.Checks
  150. } else if ctx.IsSet("run") {
  151. addDefault := ctx.Bool("default")
  152. names := ctx.StringSlice("run")
  153. for i, name := range names {
  154. names[i] = strings.ToLower(strings.TrimSpace(name))
  155. }
  156. for _, check := range doctor.Checks {
  157. if addDefault && check.IsDefault {
  158. checks = append(checks, check)
  159. continue
  160. }
  161. for _, name := range names {
  162. if name == check.Name {
  163. checks = append(checks, check)
  164. break
  165. }
  166. }
  167. }
  168. } else {
  169. for _, check := range doctor.Checks {
  170. if check.IsDefault {
  171. checks = append(checks, check)
  172. }
  173. }
  174. }
  175. // Now we can set up our own logger to return information about what the doctor is doing
  176. if err := log.NewNamedLogger("doctorouter",
  177. 1000,
  178. "console",
  179. "console",
  180. fmt.Sprintf(`{"level":"INFO","stacktracelevel":"NONE","colorize":%t,"flags":-1}`, colorize)); err != nil {
  181. fmt.Println(err)
  182. return err
  183. }
  184. logger := log.GetLogger("doctorouter")
  185. defer logger.Close()
  186. return doctor.RunChecks(logger, ctx.Bool("fix"), checks)
  187. }