From 23a288fbe92ad0165d24801e0d4797f826f4ef4a Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 6 Nov 2019 20:30:08 +0800 Subject: [PATCH 1/5] add doctor --- cmd/doctor.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 cmd/doctor.go diff --git a/cmd/doctor.go b/cmd/doctor.go new file mode 100644 index 0000000000000..2fb3a48997052 --- /dev/null +++ b/cmd/doctor.go @@ -0,0 +1,31 @@ +// 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 ( + "github.com/urfave/cli" + + "code.gitea.io/gitea/modules/setting" +) + +// CmdDoctor represents the available doctor sub-command. +var CmdDoctor = cli.Command{ + Name: "doctor", + Usage: "Diagnose the problems", + Description: "A command to diagnose the problems of current gitea instance according the given configuration.", + Action: runDoctor, +} + +func runDoctor(ctx *cli.Context) error { + if err := initDB(); err != nil { + return err + } + + runDoctorLocationMoved(ctx) +} + +func runDoctorLocationMoved(ctx *cliContext) { + setting.RepoRootPath +} From 24b0a0cfab3a446144607c4382b0730e37e41001 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 20 Nov 2019 23:52:33 +0800 Subject: [PATCH 2/5] Add a new command doctor to check if some wrong configurations on gitea instance --- cmd/doctor.go | 95 ++++++++++++++++++++++++++++++++++++++++++++++++--- main.go | 1 + 2 files changed, 92 insertions(+), 4 deletions(-) diff --git a/cmd/doctor.go b/cmd/doctor.go index 2fb3a48997052..1763cc4b9ef2e 100644 --- a/cmd/doctor.go +++ b/cmd/doctor.go @@ -5,9 +5,15 @@ package cmd import ( - "github.com/urfave/cli" + "bufio" + "fmt" + "os" + "os/exec" + "path/filepath" + "strings" "code.gitea.io/gitea/modules/setting" + "github.com/urfave/cli" ) // CmdDoctor represents the available doctor sub-command. @@ -18,14 +24,95 @@ var CmdDoctor = cli.Command{ Action: runDoctor, } +type check struct { + title string + f func(ctx *cli.Context) ([]string, error) +} + +var checklist = []check{ + { + title: "Check if openssh authorized_keys file correct", + f: runDoctorLocationMoved, + }, +} + func runDoctor(ctx *cli.Context) error { if err := initDB(); err != nil { return err } - runDoctorLocationMoved(ctx) + for i, check := range checklist { + fmt.Println("[", i+1, "]", check.title) + fmt.Println() + if messages, err := check.f(ctx); err != nil { + fmt.Println("Error:", err) + } else if len(messages) > 0 { + for _, message := range messages { + fmt.Println("-", message) + } + } else { + fmt.Println("OK.") + } + fmt.Println() + } + return nil +} + +func exePath() (string, error) { + file, err := exec.LookPath(os.Args[0]) + if err != nil { + return "", err + } + return filepath.Abs(file) } -func runDoctorLocationMoved(ctx *cliContext) { - setting.RepoRootPath +func runDoctorLocationMoved(ctx *cli.Context) ([]string, error) { + if setting.SSH.StartBuiltinServer { + return nil, nil + } + + fPath := filepath.Join(setting.SSH.RootPath, "authorized_keys") + f, err := os.Open(fPath) + if err != nil { + return nil, err + } + defer f.Close() + + var firstline string + scanner := bufio.NewScanner(f) + for scanner.Scan() { + firstline = scanner.Text() + if !strings.HasPrefix(firstline, "#") { + break + } + } + + // command="/Volumes/data/Projects/gitea/gitea/gitea --config + if len(firstline) > 0 { + var start, end int + for i, c := range firstline { + if c == ' ' { + end = i + break + } else if c == '"' { + start = i + 1 + } + } + if start > 0 && end > 0 { + p, err := exePath() + if err != nil { + return nil, err + } + p, err = filepath.Abs(p) + if err != nil { + return nil, err + } + + if firstline[start:end] != p { + return []string{fmt.Sprintf("Wants %s but %s on %s", p, firstline[start:end], fPath)}, nil + } + } + } + + return nil, nil } diff --git a/main.go b/main.go index 30dbf2766224c..c67eaf7692e1d 100644 --- a/main.go +++ b/main.go @@ -68,6 +68,7 @@ arguments - which can alternatively be run by running the subcommand web.` cmd.CmdMigrate, cmd.CmdKeys, cmd.CmdConvert, + cmd.CmdDoctor, } // Now adjust these commands to add our global configuration options From eb462b1fa53d99924b4d772e6d44b53daca7294f Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 20 Nov 2019 23:58:23 +0800 Subject: [PATCH 3/5] fix import --- cmd/doctor.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cmd/doctor.go b/cmd/doctor.go index 1763cc4b9ef2e..0850bd184ac4e 100644 --- a/cmd/doctor.go +++ b/cmd/doctor.go @@ -13,6 +13,7 @@ import ( "strings" "code.gitea.io/gitea/modules/setting" + "github.com/urfave/cli" ) From a4c12358c23112cdc2792ca07ddeeb3ce00aab9f Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 23 Nov 2019 16:57:59 +0800 Subject: [PATCH 4/5] use regex match authorized_keys on doctor --- cmd/doctor.go | 51 ++++++++++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/cmd/doctor.go b/cmd/doctor.go index 0850bd184ac4e..90a6bcd2fe96d 100644 --- a/cmd/doctor.go +++ b/cmd/doctor.go @@ -10,6 +10,7 @@ import ( "os" "os/exec" "path/filepath" + "regexp" "strings" "code.gitea.io/gitea/modules/setting" @@ -32,14 +33,18 @@ type check struct { var checklist = []check{ { - title: "Check if openssh authorized_keys file correct", + title: "Check if openssh authorized_keys file id correct", f: runDoctorLocationMoved, }, } func runDoctor(ctx *cli.Context) error { - if err := initDB(); err != nil { - return err + err := initDB() + fmt.Println("Using app.ini at ", setting.CustomConf) + if err != nil { + 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 } for i, check := range checklist { @@ -90,28 +95,28 @@ func runDoctorLocationMoved(ctx *cli.Context) ([]string, error) { // command="/Volumes/data/Projects/gitea/gitea/gitea --config if len(firstline) > 0 { - var start, end int - for i, c := range firstline { - if c == ' ' { - end = i - break - } else if c == '"' { - start = i + 1 - } + exp := regexp.MustCompile(`^[ \t]*(?:command=")([^ ]+) --config='([^']+)' serv key-([^"]+)",(?:[^ ]+) ssh-rsa ([^ ]+) ([^ ]+)[ \t]*$`) + + // command="/home/user/gitea --config='/home/user/etc/app.ini' serv key-999",option-1,option-2,option-n ssh-rsa public-key-value key-name + res := exp.FindAllStringSubmatch(firstline, -1) + + giteaPath := res[1] // => /home/user/gitea + iniPath := res[2] // => /home/user/etc/app.ini + + p, err := exePath() + if err != nil { + return nil, err + } + p, err = filepath.Abs(p) + if err != nil { + return nil, err } - if start > 0 && end > 0 { - p, err := exePath() - if err != nil { - return nil, err - } - p, err = filepath.Abs(p) - if err != nil { - return nil, err - } - if firstline[start:end] != p { - return []string{fmt.Sprintf("Wants %s but %s on %s", p, firstline[start:end], fPath)}, nil - } + if len(giteaPath) > 0 && giteaPath[0] != p { + return []string{fmt.Sprintf("Gitea exe path wants %s but %s on %s", p, giteaPath[0], fPath)}, nil + } + if len(iniPath) > 0 && iniPath[0] != setting.CustomConf { + return []string{fmt.Sprintf("Gitea config path wants %s but %s on %s", setting.CustomConf, iniPath[0], fPath)}, nil } } From bc573b636dad7efad17919f487a6142935eb1bb1 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 11 Jan 2020 19:42:33 +0800 Subject: [PATCH 5/5] Add documentation --- cmd/doctor.go | 30 ++++++++++++-------- docs/content/doc/usage/command-line.en-us.md | 25 ++++++++++++++++ 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/cmd/doctor.go b/cmd/doctor.go index 90a6bcd2fe96d..d81ead97c72cf 100644 --- a/cmd/doctor.go +++ b/cmd/doctor.go @@ -6,6 +6,7 @@ package cmd import ( "bufio" + "errors" "fmt" "os" "os/exec" @@ -31,16 +32,18 @@ type check struct { f func(ctx *cli.Context) ([]string, error) } +// checklist represents list for all checks var checklist = []check{ { - title: "Check if openssh authorized_keys file id correct", + title: "Check if OpenSSH authorized_keys file id correct", f: runDoctorLocationMoved, }, + // more checks please append here } func runDoctor(ctx *cli.Context) error { err := initDB() - fmt.Println("Using app.ini at ", setting.CustomConf) + fmt.Println("Using app.ini at", setting.CustomConf) if err != nil { fmt.Println(err) fmt.Println("Check if you are using the right config file. You can use a --config directive to specify one.") @@ -49,7 +52,6 @@ func runDoctor(ctx *cli.Context) error { for i, check := range checklist { fmt.Println("[", i+1, "]", check.title) - fmt.Println() if messages, err := check.f(ctx); err != nil { fmt.Println("Error:", err) } else if len(messages) > 0 { @@ -73,7 +75,7 @@ func exePath() (string, error) { } func runDoctorLocationMoved(ctx *cli.Context) ([]string, error) { - if setting.SSH.StartBuiltinServer { + if setting.SSH.StartBuiltinServer || !setting.SSH.CreateAuthorizedKeysFile { return nil, nil } @@ -87,10 +89,11 @@ func runDoctorLocationMoved(ctx *cli.Context) ([]string, error) { var firstline string scanner := bufio.NewScanner(f) for scanner.Scan() { - firstline = scanner.Text() - if !strings.HasPrefix(firstline, "#") { - break + firstline = strings.TrimSpace(scanner.Text()) + if len(firstline) == 0 || firstline[0] == '#' { + continue } + break } // command="/Volumes/data/Projects/gitea/gitea/gitea --config @@ -98,7 +101,10 @@ func runDoctorLocationMoved(ctx *cli.Context) ([]string, error) { exp := regexp.MustCompile(`^[ \t]*(?:command=")([^ ]+) --config='([^']+)' serv key-([^"]+)",(?:[^ ]+) ssh-rsa ([^ ]+) ([^ ]+)[ \t]*$`) // command="/home/user/gitea --config='/home/user/etc/app.ini' serv key-999",option-1,option-2,option-n ssh-rsa public-key-value key-name - res := exp.FindAllStringSubmatch(firstline, -1) + res := exp.FindStringSubmatch(firstline) + if res == nil { + return nil, errors.New("Unknow authorized_keys format") + } giteaPath := res[1] // => /home/user/gitea iniPath := res[2] // => /home/user/etc/app.ini @@ -112,11 +118,11 @@ func runDoctorLocationMoved(ctx *cli.Context) ([]string, error) { return nil, err } - if len(giteaPath) > 0 && giteaPath[0] != p { - return []string{fmt.Sprintf("Gitea exe path wants %s but %s on %s", p, giteaPath[0], fPath)}, nil + if len(giteaPath) > 0 && giteaPath != p { + return []string{fmt.Sprintf("Gitea exe path wants %s but %s on %s", p, giteaPath, fPath)}, nil } - if len(iniPath) > 0 && iniPath[0] != setting.CustomConf { - return []string{fmt.Sprintf("Gitea config path wants %s but %s on %s", setting.CustomConf, iniPath[0], fPath)}, nil + if len(iniPath) > 0 && iniPath != setting.CustomConf { + return []string{fmt.Sprintf("Gitea config path wants %s but %s on %s", setting.CustomConf, iniPath, fPath)}, nil } } diff --git a/docs/content/doc/usage/command-line.en-us.md b/docs/content/doc/usage/command-line.en-us.md index 0f7b4f61a20d7..60c2e26a7b7cd 100644 --- a/docs/content/doc/usage/command-line.en-us.md +++ b/docs/content/doc/usage/command-line.en-us.md @@ -289,3 +289,28 @@ This command is idempotent. #### convert Converts an existing MySQL database from utf8 to utf8mb4. + +#### doctor +Diagnose the problems of current gitea instance according the given configuration. +Currently there are a check list below: + +- Check if OpenSSH authorized_keys file id correct +When your gitea instance support OpenSSH, your gitea instance binary path will be written to `authorized_keys` +when there is any public key added or changed on your gitea instance. +Sometimes if you moved or renamed your gitea binary when upgrade and you haven't run `Update the '.ssh/authorized_keys' file with Gitea SSH keys. (Not needed for the built-in SSH server.)` on your Admin Panel. Then all pull/push via SSH will not be work. +This check will help you to check if it works well. + +For contributors, if you want to add more checks, you can wrie ad new function like `func(ctx *cli.Context) ([]string, error)` and +append it to `doctor.go`. + +```go +var checklist = []check{ + { + title: "Check if OpenSSH authorized_keys file id correct", + f: runDoctorLocationMoved, + }, + // more checks please append here +} +``` + +This function will receive a command line context and return a list of details about the problems or error. \ No newline at end of file