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