Skip to content

Commit f69f5a9

Browse files
lunnysapk
authored andcommitted
Add a new command doctor to check if some wrong configurations on gitea instance (#9095)
* add doctor * Add a new command doctor to check if some wrong configurations on gitea instance * fix import * use regex match authorized_keys on doctor * Add documentation
1 parent f2e6c45 commit f69f5a9

File tree

3 files changed

+156
-0
lines changed

3 files changed

+156
-0
lines changed

cmd/doctor.go

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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+
5+
package cmd
6+
7+
import (
8+
"bufio"
9+
"errors"
10+
"fmt"
11+
"os"
12+
"os/exec"
13+
"path/filepath"
14+
"regexp"
15+
"strings"
16+
17+
"code.gitea.io/gitea/modules/setting"
18+
19+
"github.com/urfave/cli"
20+
)
21+
22+
// CmdDoctor represents the available doctor sub-command.
23+
var CmdDoctor = cli.Command{
24+
Name: "doctor",
25+
Usage: "Diagnose the problems",
26+
Description: "A command to diagnose the problems of current gitea instance according the given configuration.",
27+
Action: runDoctor,
28+
}
29+
30+
type check struct {
31+
title string
32+
f func(ctx *cli.Context) ([]string, error)
33+
}
34+
35+
// checklist represents list for all checks
36+
var checklist = []check{
37+
{
38+
title: "Check if OpenSSH authorized_keys file id correct",
39+
f: runDoctorLocationMoved,
40+
},
41+
// more checks please append here
42+
}
43+
44+
func runDoctor(ctx *cli.Context) error {
45+
err := initDB()
46+
fmt.Println("Using app.ini at", setting.CustomConf)
47+
if err != nil {
48+
fmt.Println(err)
49+
fmt.Println("Check if you are using the right config file. You can use a --config directive to specify one.")
50+
return nil
51+
}
52+
53+
for i, check := range checklist {
54+
fmt.Println("[", i+1, "]", check.title)
55+
if messages, err := check.f(ctx); err != nil {
56+
fmt.Println("Error:", err)
57+
} else if len(messages) > 0 {
58+
for _, message := range messages {
59+
fmt.Println("-", message)
60+
}
61+
} else {
62+
fmt.Println("OK.")
63+
}
64+
fmt.Println()
65+
}
66+
return nil
67+
}
68+
69+
func exePath() (string, error) {
70+
file, err := exec.LookPath(os.Args[0])
71+
if err != nil {
72+
return "", err
73+
}
74+
return filepath.Abs(file)
75+
}
76+
77+
func runDoctorLocationMoved(ctx *cli.Context) ([]string, error) {
78+
if setting.SSH.StartBuiltinServer || !setting.SSH.CreateAuthorizedKeysFile {
79+
return nil, nil
80+
}
81+
82+
fPath := filepath.Join(setting.SSH.RootPath, "authorized_keys")
83+
f, err := os.Open(fPath)
84+
if err != nil {
85+
return nil, err
86+
}
87+
defer f.Close()
88+
89+
var firstline string
90+
scanner := bufio.NewScanner(f)
91+
for scanner.Scan() {
92+
firstline = strings.TrimSpace(scanner.Text())
93+
if len(firstline) == 0 || firstline[0] == '#' {
94+
continue
95+
}
96+
break
97+
}
98+
99+
// command="/Volumes/data/Projects/gitea/gitea/gitea --config
100+
if len(firstline) > 0 {
101+
exp := regexp.MustCompile(`^[ \t]*(?:command=")([^ ]+) --config='([^']+)' serv key-([^"]+)",(?:[^ ]+) ssh-rsa ([^ ]+) ([^ ]+)[ \t]*$`)
102+
103+
// 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
104+
res := exp.FindStringSubmatch(firstline)
105+
if res == nil {
106+
return nil, errors.New("Unknow authorized_keys format")
107+
}
108+
109+
giteaPath := res[1] // => /home/user/gitea
110+
iniPath := res[2] // => /home/user/etc/app.ini
111+
112+
p, err := exePath()
113+
if err != nil {
114+
return nil, err
115+
}
116+
p, err = filepath.Abs(p)
117+
if err != nil {
118+
return nil, err
119+
}
120+
121+
if len(giteaPath) > 0 && giteaPath != p {
122+
return []string{fmt.Sprintf("Gitea exe path wants %s but %s on %s", p, giteaPath, fPath)}, nil
123+
}
124+
if len(iniPath) > 0 && iniPath != setting.CustomConf {
125+
return []string{fmt.Sprintf("Gitea config path wants %s but %s on %s", setting.CustomConf, iniPath, fPath)}, nil
126+
}
127+
}
128+
129+
return nil, nil
130+
}

docs/content/doc/usage/command-line.en-us.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,28 @@ This command is idempotent.
289289

290290
#### convert
291291
Converts an existing MySQL database from utf8 to utf8mb4.
292+
293+
#### doctor
294+
Diagnose the problems of current gitea instance according the given configuration.
295+
Currently there are a check list below:
296+
297+
- Check if OpenSSH authorized_keys file id correct
298+
When your gitea instance support OpenSSH, your gitea instance binary path will be written to `authorized_keys`
299+
when there is any public key added or changed on your gitea instance.
300+
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.
301+
This check will help you to check if it works well.
302+
303+
For contributors, if you want to add more checks, you can wrie ad new function like `func(ctx *cli.Context) ([]string, error)` and
304+
append it to `doctor.go`.
305+
306+
```go
307+
var checklist = []check{
308+
{
309+
title: "Check if OpenSSH authorized_keys file id correct",
310+
f: runDoctorLocationMoved,
311+
},
312+
// more checks please append here
313+
}
314+
```
315+
316+
This function will receive a command line context and return a list of details about the problems or error.

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ arguments - which can alternatively be run by running the subcommand web.`
6868
cmd.CmdMigrate,
6969
cmd.CmdKeys,
7070
cmd.CmdConvert,
71+
cmd.CmdDoctor,
7172
}
7273
// Now adjust these commands to add our global configuration options
7374

0 commit comments

Comments
 (0)