2020-01-11 19:54:57 +05:30
// 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 16:14:47 +05:30
golog "log"
2020-01-11 19:54:57 +05:30
"os"
"strings"
2020-04-06 16:14:47 +05:30
"text/tabwriter"
2020-01-11 19:54:57 +05:30
2021-09-19 17:19:59 +05:30
"code.gitea.io/gitea/models/db"
2020-04-06 16:14:47 +05:30
"code.gitea.io/gitea/models/migrations"
2020-12-02 10:26:04 +05:30
"code.gitea.io/gitea/modules/doctor"
2020-01-30 07:30:27 +05:30
"code.gitea.io/gitea/modules/log"
2020-01-11 19:54:57 +05:30
"code.gitea.io/gitea/modules/setting"
2020-12-02 10:26:04 +05:30
2020-01-11 19:54:57 +05:30
"github.com/urfave/cli"
2021-11-17 18:04:35 +05:30
"xorm.io/xorm"
2020-01-11 19:54:57 +05:30
)
// CmdDoctor represents the available doctor sub-command.
var CmdDoctor = cli . Command {
Name : "doctor" ,
2022-05-20 09:09:52 +05:30
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 19:54:57 +05:30
Action : runDoctor ,
2020-04-06 16:14:47 +05:30
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 10:26:04 +05:30
cli . BoolFlag {
Name : "color, H" ,
Usage : "Use color for outputted information" ,
} ,
2020-04-06 16:14:47 +05:30
} ,
2020-09-07 03:22:01 +05:30
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 19:54:57 +05:30
}
2020-09-07 03:22:01 +05:30
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 13:20:01 +05:30
setting . LoadFromExisting ( )
2020-09-07 03:22:01 +05:30
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 08:41:27 +05:30
stdCtx , cancel := installSignals ( )
defer cancel ( )
if err := db . InitEngine ( stdCtx ) ; err != nil {
2020-09-07 03:22:01 +05:30
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 17:19:59 +05:30
beans , err := db . NamesToBean ( names ... )
2020-09-07 03:22:01 +05:30
if err != nil {
return err
}
recreateTables := migrations . RecreateTables ( beans ... )
2022-01-20 04:56:57 +05:30
return db . InitEngineWithMigration ( stdCtx , func ( x * xorm . Engine ) error {
2020-09-07 03:22:01 +05:30
if err := migrations . EnsureUpToDate ( x ) ; err != nil {
return err
}
return recreateTables ( x )
} )
}
2020-01-11 19:54:57 +05:30
func runDoctor ( ctx * cli . Context ) error {
2022-08-09 08:52:24 +05:30
stdCtx , cancel := installSignals ( )
defer cancel ( )
2020-04-06 16:14:47 +05:30
// Silence the default loggers
2020-01-30 07:30:27 +05:30
log . DelNamedLogger ( "console" )
log . DelNamedLogger ( log . DEFAULT )
2020-04-06 16:14:47 +05:30
// Now setup our own
logFile := ctx . String ( "log-file" )
if ! ctx . IsSet ( "log-file" ) {
logFile = "doctor.log"
}
2020-12-02 10:26:04 +05:30
colorize := log . CanColorStdout
if ctx . IsSet ( "color" ) {
colorize = ctx . Bool ( "color" )
}
2020-04-06 16:14:47 +05:30
if len ( logFile ) == 0 {
2020-12-02 10:26:04 +05:30
log . NewLogger ( 1000 , "doctor" , "console" , fmt . Sprintf ( ` { "level":"NONE","stacktracelevel":"NONE","colorize":%t} ` , colorize ) )
2020-04-06 16:14:47 +05:30
} else if logFile == "-" {
2020-12-02 10:26:04 +05:30
log . NewLogger ( 1000 , "doctor" , "console" , fmt . Sprintf ( ` { "level":"trace","stacktracelevel":"NONE","colorize":%t} ` , colorize ) )
2020-04-06 16:14:47 +05:30
} 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 10:26:04 +05:30
w := tabwriter . NewWriter ( os . Stdout , 0 , 8 , 1 , '\t' , 0 )
2020-04-06 16:14:47 +05:30
_ , _ = w . Write ( [ ] byte ( "Default\tName\tTitle\n" ) )
2020-12-02 10:26:04 +05:30
for _ , check := range doctor . Checks {
if check . IsDefault {
2020-04-06 16:14:47 +05:30
_ , _ = w . Write ( [ ] byte { '*' } )
}
_ , _ = w . Write ( [ ] byte { '\t' } )
2020-12-02 10:26:04 +05:30
_ , _ = w . Write ( [ ] byte ( check . Name ) )
2020-04-06 16:14:47 +05:30
_ , _ = w . Write ( [ ] byte { '\t' } )
2020-12-02 10:26:04 +05:30
_ , _ = w . Write ( [ ] byte ( check . Title ) )
2020-04-06 16:14:47 +05:30
_ , _ = w . Write ( [ ] byte { '\n' } )
}
return w . Flush ( )
}
2020-12-02 10:26:04 +05:30
var checks [ ] * doctor . Check
2020-04-06 16:14:47 +05:30
if ctx . Bool ( "all" ) {
2020-12-02 10:26:04 +05:30
checks = doctor . Checks
2020-04-06 16:14:47 +05:30
} 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 10:26:04 +05:30
for _ , check := range doctor . Checks {
if addDefault && check . IsDefault {
2020-04-06 16:14:47 +05:30
checks = append ( checks , check )
continue
}
for _ , name := range names {
2020-12-02 10:26:04 +05:30
if name == check . Name {
2020-04-06 16:14:47 +05:30
checks = append ( checks , check )
break
}
}
}
} else {
2020-12-02 10:26:04 +05:30
for _ , check := range doctor . Checks {
if check . IsDefault {
2020-04-06 16:14:47 +05:30
checks = append ( checks , check )
}
}
}
2020-12-02 10:26:04 +05:30
// 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 14:40:33 +05:30
0 ,
2020-12-02 10:26:04 +05:30
"console" ,
"console" ,
fmt . Sprintf ( ` { "level":"INFO","stacktracelevel":"NONE","colorize":%t,"flags":-1} ` , colorize ) ) ; err != nil {
fmt . Println ( err )
2020-01-30 07:30:27 +05:30
return err
}
2020-05-29 18:54:15 +05:30
2020-12-02 10:26:04 +05:30
logger := log . GetLogger ( "doctorouter" )
defer logger . Close ( )
2021-11-07 08:41:27 +05:30
return doctor . RunChecks ( stdCtx , logger , ctx . Bool ( "fix" ) , checks )
2020-08-23 21:32:35 +05:30
}