2020-01-11 23:24:57 +09:00
// Copyright 2019 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package cmd
import (
"fmt"
2020-04-06 19:44:47 +09:00
golog "log"
2020-01-11 23:24:57 +09:00
"os"
"strings"
2020-04-06 19:44:47 +09:00
"text/tabwriter"
2020-01-11 23:24:57 +09:00
2021-09-19 20:49:59 +09:00
"code.gitea.io/gitea/models/db"
2020-04-06 19:44:47 +09:00
"code.gitea.io/gitea/models/migrations"
2020-12-02 13:56:04 +09:00
"code.gitea.io/gitea/modules/doctor"
2020-01-30 11:00:27 +09:00
"code.gitea.io/gitea/modules/log"
2020-01-11 23:24:57 +09:00
"code.gitea.io/gitea/modules/setting"
2020-12-02 13:56:04 +09:00
2020-01-11 23:24:57 +09:00
"github.com/urfave/cli"
2021-11-17 21:34:35 +09:00
"xorm.io/xorm"
2020-01-11 23:24:57 +09:00
)
// CmdDoctor represents the available doctor sub-command.
var CmdDoctor = cli . Command {
Name : "doctor" ,
2022-05-20 12:39:52 +09:00
Usage : "Diagnose and optionally fix problems" ,
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." ,
2020-01-11 23:24:57 +09:00
Action : runDoctor ,
2020-04-06 19:44:47 +09:00
Flags : [ ] cli . Flag {
cli . BoolFlag {
Name : "list" ,
Usage : "List the available checks" ,
} ,
cli . BoolFlag {
Name : "default" ,
Usage : "Run the default checks (if neither --run or --all is set, this is the default behaviour)" ,
} ,
cli . StringSliceFlag {
Name : "run" ,
Usage : "Run the provided checks - (if --default is set, the default checks will also run)" ,
} ,
cli . BoolFlag {
Name : "all" ,
Usage : "Run all the available checks" ,
} ,
cli . BoolFlag {
Name : "fix" ,
Usage : "Automatically fix what we can" ,
} ,
cli . StringFlag {
Name : "log-file" ,
Usage : ` Name of the log file (default: "doctor.log"). Set to "-" to output to stdout, set to "" to disable ` ,
} ,
2020-12-02 13:56:04 +09:00
cli . BoolFlag {
Name : "color, H" ,
Usage : "Use color for outputted information" ,
} ,
2020-04-06 19:44:47 +09:00
} ,
2020-09-07 06:52:01 +09:00
Subcommands : [ ] cli . Command {
cmdRecreateTable ,
} ,
}
var cmdRecreateTable = cli . Command {
Name : "recreate-table" ,
Usage : "Recreate tables from XORM definitions and copy the data." ,
ArgsUsage : "[TABLE]... : (TABLEs to recreate - leave blank for all)" ,
Flags : [ ] cli . Flag {
cli . BoolFlag {
Name : "debug" ,
Usage : "Print SQL commands sent" ,
} ,
} ,
Description : ` The database definitions Gitea uses change across versions , sometimes changing default values and leaving old unused columns .
This command will cause Xorm to recreate tables , copying over the data and deleting the old table .
You should back - up your database before doing this and ensure that your database is up - to - date first . ` ,
Action : runRecreateTable ,
2020-01-11 23:24:57 +09:00
}
2020-09-07 06:52:01 +09:00
func runRecreateTable ( ctx * cli . Context ) error {
// Redirect the default golog to here
golog . SetFlags ( 0 )
golog . SetPrefix ( "" )
golog . SetOutput ( log . NewLoggerAsWriter ( "INFO" , log . GetLogger ( log . DEFAULT ) ) )
2021-12-01 16:50:01 +09:00
setting . LoadFromExisting ( )
2020-09-07 06:52:01 +09:00
setting . InitDBConfig ( )
setting . EnableXORMLog = ctx . Bool ( "debug" )
setting . Database . LogSQL = ctx . Bool ( "debug" )
setting . Cfg . Section ( "log" ) . Key ( "XORM" ) . SetValue ( "," )
setting . NewXORMLogService ( ! ctx . Bool ( "debug" ) )
2021-11-07 12:11:27 +09:00
stdCtx , cancel := installSignals ( )
defer cancel ( )
if err := db . InitEngine ( stdCtx ) ; err != nil {
2020-09-07 06:52:01 +09:00
fmt . Println ( err )
fmt . Println ( "Check if you are using the right config file. You can use a --config directive to specify one." )
return nil
}
args := ctx . Args ( )
names := make ( [ ] string , 0 , ctx . NArg ( ) )
for i := 0 ; i < ctx . NArg ( ) ; i ++ {
names = append ( names , args . Get ( i ) )
}
2021-09-19 20:49:59 +09:00
beans , err := db . NamesToBean ( names ... )
2020-09-07 06:52:01 +09:00
if err != nil {
return err
}
recreateTables := migrations . RecreateTables ( beans ... )
2022-01-20 08:26:57 +09:00
return db . InitEngineWithMigration ( stdCtx , func ( x * xorm . Engine ) error {
2020-09-07 06:52:01 +09:00
if err := migrations . EnsureUpToDate ( x ) ; err != nil {
return err
}
return recreateTables ( x )
} )
}
2020-01-11 23:24:57 +09:00
func runDoctor ( ctx * cli . Context ) error {
2022-08-09 12:22:24 +09:00
stdCtx , cancel := installSignals ( )
defer cancel ( )
2020-04-06 19:44:47 +09:00
// Silence the default loggers
2020-01-30 11:00:27 +09:00
log . DelNamedLogger ( "console" )
log . DelNamedLogger ( log . DEFAULT )
2020-04-06 19:44:47 +09:00
// Now setup our own
logFile := ctx . String ( "log-file" )
if ! ctx . IsSet ( "log-file" ) {
logFile = "doctor.log"
}
2020-12-02 13:56:04 +09:00
colorize := log . CanColorStdout
if ctx . IsSet ( "color" ) {
colorize = ctx . Bool ( "color" )
}
2020-04-06 19:44:47 +09:00
if len ( logFile ) == 0 {
2020-12-02 13:56:04 +09:00
log . NewLogger ( 1000 , "doctor" , "console" , fmt . Sprintf ( ` { "level":"NONE","stacktracelevel":"NONE","colorize":%t} ` , colorize ) )
2020-04-06 19:44:47 +09:00
} else if logFile == "-" {
2020-12-02 13:56:04 +09:00
log . NewLogger ( 1000 , "doctor" , "console" , fmt . Sprintf ( ` { "level":"trace","stacktracelevel":"NONE","colorize":%t} ` , colorize ) )
2020-04-06 19:44:47 +09:00
} else {
log . NewLogger ( 1000 , "doctor" , "file" , fmt . Sprintf ( ` { "filename":%q,"level":"trace","stacktracelevel":"NONE"} ` , logFile ) )
}
// Finally redirect the default golog to here
golog . SetFlags ( 0 )
golog . SetPrefix ( "" )
golog . SetOutput ( log . NewLoggerAsWriter ( "INFO" , log . GetLogger ( log . DEFAULT ) ) )
if ctx . IsSet ( "list" ) {
2020-12-02 13:56:04 +09:00
w := tabwriter . NewWriter ( os . Stdout , 0 , 8 , 1 , '\t' , 0 )
2020-04-06 19:44:47 +09:00
_ , _ = w . Write ( [ ] byte ( "Default\tName\tTitle\n" ) )
2020-12-02 13:56:04 +09:00
for _ , check := range doctor . Checks {
if check . IsDefault {
2020-04-06 19:44:47 +09:00
_ , _ = w . Write ( [ ] byte { '*' } )
}
_ , _ = w . Write ( [ ] byte { '\t' } )
2020-12-02 13:56:04 +09:00
_ , _ = w . Write ( [ ] byte ( check . Name ) )
2020-04-06 19:44:47 +09:00
_ , _ = w . Write ( [ ] byte { '\t' } )
2020-12-02 13:56:04 +09:00
_ , _ = w . Write ( [ ] byte ( check . Title ) )
2020-04-06 19:44:47 +09:00
_ , _ = w . Write ( [ ] byte { '\n' } )
}
return w . Flush ( )
}
2020-12-02 13:56:04 +09:00
var checks [ ] * doctor . Check
2020-04-06 19:44:47 +09:00
if ctx . Bool ( "all" ) {
2020-12-02 13:56:04 +09:00
checks = doctor . Checks
2020-04-06 19:44:47 +09:00
} else if ctx . IsSet ( "run" ) {
addDefault := ctx . Bool ( "default" )
names := ctx . StringSlice ( "run" )
for i , name := range names {
names [ i ] = strings . ToLower ( strings . TrimSpace ( name ) )
}
2020-12-02 13:56:04 +09:00
for _ , check := range doctor . Checks {
if addDefault && check . IsDefault {
2020-04-06 19:44:47 +09:00
checks = append ( checks , check )
continue
}
for _ , name := range names {
2020-12-02 13:56:04 +09:00
if name == check . Name {
2020-04-06 19:44:47 +09:00
checks = append ( checks , check )
break
}
}
}
} else {
2020-12-02 13:56:04 +09:00
for _ , check := range doctor . Checks {
if check . IsDefault {
2020-04-06 19:44:47 +09:00
checks = append ( checks , check )
}
}
}
2020-12-02 13:56:04 +09:00
// Now we can set up our own logger to return information about what the doctor is doing
if err := log . NewNamedLogger ( "doctorouter" ,
2022-06-16 18:10:33 +09:00
0 ,
2020-12-02 13:56:04 +09:00
"console" ,
"console" ,
fmt . Sprintf ( ` { "level":"INFO","stacktracelevel":"NONE","colorize":%t,"flags":-1} ` , colorize ) ) ; err != nil {
fmt . Println ( err )
2020-01-30 11:00:27 +09:00
return err
}
2020-05-29 22:24:15 +09:00
2020-12-02 13:56:04 +09:00
logger := log . GetLogger ( "doctorouter" )
defer logger . Close ( )
2021-11-07 12:11:27 +09:00
return doctor . RunChecks ( stdCtx , logger , ctx . Bool ( "fix" ) , checks )
2020-08-24 01:02:35 +09:00
}