You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

580 lines
18 KiB

  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. package cmd
  5. import (
  6. "bufio"
  7. "bytes"
  8. "context"
  9. "fmt"
  10. "io/ioutil"
  11. golog "log"
  12. "os"
  13. "os/exec"
  14. "path/filepath"
  15. "strings"
  16. "text/tabwriter"
  17. "code.gitea.io/gitea/models"
  18. "code.gitea.io/gitea/models/migrations"
  19. "code.gitea.io/gitea/modules/git"
  20. "code.gitea.io/gitea/modules/log"
  21. "code.gitea.io/gitea/modules/options"
  22. "code.gitea.io/gitea/modules/repository"
  23. "code.gitea.io/gitea/modules/setting"
  24. "xorm.io/builder"
  25. "github.com/urfave/cli"
  26. )
  27. // CmdDoctor represents the available doctor sub-command.
  28. var CmdDoctor = cli.Command{
  29. Name: "doctor",
  30. Usage: "Diagnose problems",
  31. Description: "A command to diagnose problems with the current Gitea instance according to the given configuration.",
  32. Action: runDoctor,
  33. Flags: []cli.Flag{
  34. cli.BoolFlag{
  35. Name: "list",
  36. Usage: "List the available checks",
  37. },
  38. cli.BoolFlag{
  39. Name: "default",
  40. Usage: "Run the default checks (if neither --run or --all is set, this is the default behaviour)",
  41. },
  42. cli.StringSliceFlag{
  43. Name: "run",
  44. Usage: "Run the provided checks - (if --default is set, the default checks will also run)",
  45. },
  46. cli.BoolFlag{
  47. Name: "all",
  48. Usage: "Run all the available checks",
  49. },
  50. cli.BoolFlag{
  51. Name: "fix",
  52. Usage: "Automatically fix what we can",
  53. },
  54. cli.StringFlag{
  55. Name: "log-file",
  56. Usage: `Name of the log file (default: "doctor.log"). Set to "-" to output to stdout, set to "" to disable`,
  57. },
  58. },
  59. }
  60. type check struct {
  61. title string
  62. name string
  63. isDefault bool
  64. f func(ctx *cli.Context) ([]string, error)
  65. abortIfFailed bool
  66. skipDatabaseInit bool
  67. }
  68. // checklist represents list for all checks
  69. var checklist = []check{
  70. {
  71. // NOTE: this check should be the first in the list
  72. title: "Check paths and basic configuration",
  73. name: "paths",
  74. isDefault: true,
  75. f: runDoctorPathInfo,
  76. abortIfFailed: true,
  77. skipDatabaseInit: true,
  78. },
  79. {
  80. title: "Check Database Version",
  81. name: "check-db-version",
  82. isDefault: true,
  83. f: runDoctorCheckDBVersion,
  84. abortIfFailed: false,
  85. },
  86. {
  87. title: "Check consistency of database",
  88. name: "check-db-consistency",
  89. isDefault: false,
  90. f: runDoctorCheckDBConsistency,
  91. },
  92. {
  93. title: "Check if OpenSSH authorized_keys file is up-to-date",
  94. name: "authorized_keys",
  95. isDefault: true,
  96. f: runDoctorAuthorizedKeys,
  97. },
  98. {
  99. title: "Check if SCRIPT_TYPE is available",
  100. name: "script-type",
  101. isDefault: false,
  102. f: runDoctorScriptType,
  103. },
  104. {
  105. title: "Check if hook files are up-to-date and executable",
  106. name: "hooks",
  107. isDefault: false,
  108. f: runDoctorHooks,
  109. },
  110. {
  111. title: "Recalculate merge bases",
  112. name: "recalculate_merge_bases",
  113. isDefault: false,
  114. f: runDoctorPRMergeBase,
  115. },
  116. // more checks please append here
  117. }
  118. func runDoctor(ctx *cli.Context) error {
  119. // Silence the default loggers
  120. log.DelNamedLogger("console")
  121. log.DelNamedLogger(log.DEFAULT)
  122. // Now setup our own
  123. logFile := ctx.String("log-file")
  124. if !ctx.IsSet("log-file") {
  125. logFile = "doctor.log"
  126. }
  127. if len(logFile) == 0 {
  128. log.NewLogger(1000, "doctor", "console", `{"level":"NONE","stacktracelevel":"NONE","colorize":"%t"}`)
  129. } else if logFile == "-" {
  130. log.NewLogger(1000, "doctor", "console", `{"level":"trace","stacktracelevel":"NONE"}`)
  131. } else {
  132. log.NewLogger(1000, "doctor", "file", fmt.Sprintf(`{"filename":%q,"level":"trace","stacktracelevel":"NONE"}`, logFile))
  133. }
  134. // Finally redirect the default golog to here
  135. golog.SetFlags(0)
  136. golog.SetPrefix("")
  137. golog.SetOutput(log.NewLoggerAsWriter("INFO", log.GetLogger(log.DEFAULT)))
  138. if ctx.IsSet("list") {
  139. w := tabwriter.NewWriter(os.Stdout, 0, 8, 0, '\t', 0)
  140. _, _ = w.Write([]byte("Default\tName\tTitle\n"))
  141. for _, check := range checklist {
  142. if check.isDefault {
  143. _, _ = w.Write([]byte{'*'})
  144. }
  145. _, _ = w.Write([]byte{'\t'})
  146. _, _ = w.Write([]byte(check.name))
  147. _, _ = w.Write([]byte{'\t'})
  148. _, _ = w.Write([]byte(check.title))
  149. _, _ = w.Write([]byte{'\n'})
  150. }
  151. return w.Flush()
  152. }
  153. var checks []check
  154. if ctx.Bool("all") {
  155. checks = checklist
  156. } else if ctx.IsSet("run") {
  157. addDefault := ctx.Bool("default")
  158. names := ctx.StringSlice("run")
  159. for i, name := range names {
  160. names[i] = strings.ToLower(strings.TrimSpace(name))
  161. }
  162. for _, check := range checklist {
  163. if addDefault && check.isDefault {
  164. checks = append(checks, check)
  165. continue
  166. }
  167. for _, name := range names {
  168. if name == check.name {
  169. checks = append(checks, check)
  170. break
  171. }
  172. }
  173. }
  174. } else {
  175. for _, check := range checklist {
  176. if check.isDefault {
  177. checks = append(checks, check)
  178. }
  179. }
  180. }
  181. dbIsInit := false
  182. for i, check := range checks {
  183. if !dbIsInit && !check.skipDatabaseInit {
  184. // Only open database after the most basic configuration check
  185. setting.EnableXORMLog = false
  186. if err := initDBDisableConsole(true); err != nil {
  187. fmt.Println(err)
  188. fmt.Println("Check if you are using the right config file. You can use a --config directive to specify one.")
  189. return nil
  190. }
  191. dbIsInit = true
  192. }
  193. fmt.Println("[", i+1, "]", check.title)
  194. messages, err := check.f(ctx)
  195. for _, message := range messages {
  196. fmt.Println("-", message)
  197. }
  198. if err != nil {
  199. fmt.Println("Error:", err)
  200. if check.abortIfFailed {
  201. return nil
  202. }
  203. } else {
  204. fmt.Println("OK.")
  205. }
  206. fmt.Println()
  207. }
  208. return nil
  209. }
  210. func runDoctorPathInfo(ctx *cli.Context) ([]string, error) {
  211. res := make([]string, 0, 10)
  212. if fi, err := os.Stat(setting.CustomConf); err != nil || !fi.Mode().IsRegular() {
  213. res = append(res, fmt.Sprintf("Failed to find configuration file at '%s'.", setting.CustomConf))
  214. res = append(res, fmt.Sprintf("If you've never ran Gitea yet, this is normal and '%s' will be created for you on first run.", setting.CustomConf))
  215. res = append(res, "Otherwise check that you are running this command from the correct path and/or provide a `--config` parameter.")
  216. return res, fmt.Errorf("can't proceed without a configuration file")
  217. }
  218. setting.NewContext()
  219. fail := false
  220. check := func(name, path string, is_dir, required, is_write bool) {
  221. res = append(res, fmt.Sprintf("%-25s '%s'", name+":", path))
  222. fi, err := os.Stat(path)
  223. if err != nil {
  224. if os.IsNotExist(err) && ctx.Bool("fix") && is_dir {
  225. if err := os.MkdirAll(path, 0777); err != nil {
  226. res = append(res, fmt.Sprintf(" ERROR: %v", err))
  227. fail = true
  228. return
  229. }
  230. fi, err = os.Stat(path)
  231. }
  232. }
  233. if err != nil {
  234. if required {
  235. res = append(res, fmt.Sprintf(" ERROR: %v", err))
  236. fail = true
  237. return
  238. }
  239. res = append(res, fmt.Sprintf(" NOTICE: not accessible (%v)", err))
  240. return
  241. }
  242. if is_dir && !fi.IsDir() {
  243. res = append(res, " ERROR: not a directory")
  244. fail = true
  245. return
  246. } else if !is_dir && !fi.Mode().IsRegular() {
  247. res = append(res, " ERROR: not a regular file")
  248. fail = true
  249. } else if is_write {
  250. if err := runDoctorWritableDir(path); err != nil {
  251. res = append(res, fmt.Sprintf(" ERROR: not writable: %v", err))
  252. fail = true
  253. }
  254. }
  255. }
  256. // Note print paths inside quotes to make any leading/trailing spaces evident
  257. check("Configuration File Path", setting.CustomConf, false, true, false)
  258. check("Repository Root Path", setting.RepoRootPath, true, true, true)
  259. check("Data Root Path", setting.AppDataPath, true, true, true)
  260. check("Custom File Root Path", setting.CustomPath, true, false, false)
  261. check("Work directory", setting.AppWorkPath, true, true, false)
  262. check("Log Root Path", setting.LogRootPath, true, true, true)
  263. if options.IsDynamic() {
  264. // Do not check/report on StaticRootPath if data is embedded in Gitea (-tags bindata)
  265. check("Static File Root Path", setting.StaticRootPath, true, true, false)
  266. }
  267. if fail {
  268. return res, fmt.Errorf("please check your configuration file and try again")
  269. }
  270. return res, nil
  271. }
  272. func runDoctorWritableDir(path string) error {
  273. // There's no platform-independent way of checking if a directory is writable
  274. // https://stackoverflow.com/questions/20026320/how-to-tell-if-folder-exists-and-is-writable
  275. tmpFile, err := ioutil.TempFile(path, "doctors-order")
  276. if err != nil {
  277. return err
  278. }
  279. if err := os.Remove(tmpFile.Name()); err != nil {
  280. fmt.Printf("Warning: can't remove temporary file: '%s'\n", tmpFile.Name())
  281. }
  282. tmpFile.Close()
  283. return nil
  284. }
  285. const tplCommentPrefix = `# gitea public key`
  286. func runDoctorAuthorizedKeys(ctx *cli.Context) ([]string, error) {
  287. if setting.SSH.StartBuiltinServer || !setting.SSH.CreateAuthorizedKeysFile {
  288. return nil, nil
  289. }
  290. fPath := filepath.Join(setting.SSH.RootPath, "authorized_keys")
  291. f, err := os.Open(fPath)
  292. if err != nil {
  293. if ctx.Bool("fix") {
  294. return []string{fmt.Sprintf("Error whilst opening authorized_keys: %v. Attempting regeneration", err)}, models.RewriteAllPublicKeys()
  295. }
  296. return nil, err
  297. }
  298. defer f.Close()
  299. linesInAuthorizedKeys := map[string]bool{}
  300. scanner := bufio.NewScanner(f)
  301. for scanner.Scan() {
  302. line := scanner.Text()
  303. if strings.HasPrefix(line, tplCommentPrefix) {
  304. continue
  305. }
  306. linesInAuthorizedKeys[line] = true
  307. }
  308. f.Close()
  309. // now we regenerate and check if there are any lines missing
  310. regenerated := &bytes.Buffer{}
  311. if err := models.RegeneratePublicKeys(regenerated); err != nil {
  312. return nil, err
  313. }
  314. scanner = bufio.NewScanner(regenerated)
  315. for scanner.Scan() {
  316. line := scanner.Text()
  317. if strings.HasPrefix(line, tplCommentPrefix) {
  318. continue
  319. }
  320. if ok := linesInAuthorizedKeys[line]; ok {
  321. continue
  322. }
  323. if ctx.Bool("fix") {
  324. return []string{"authorized_keys is out of date, attempting regeneration"}, models.RewriteAllPublicKeys()
  325. }
  326. return nil, fmt.Errorf(`authorized_keys is out of date and should be regenerated with "gitea admin regenerate keys" or "gitea doctor --run authorized_keys --fix"`)
  327. }
  328. return nil, nil
  329. }
  330. func runDoctorCheckDBVersion(ctx *cli.Context) ([]string, error) {
  331. if err := models.NewEngine(context.Background(), migrations.EnsureUpToDate); err != nil {
  332. if ctx.Bool("fix") {
  333. return []string{fmt.Sprintf("WARN: Got Error %v during ensure up to date", err), "Attempting to migrate to the latest DB version to fix this."}, models.NewEngine(context.Background(), migrations.Migrate)
  334. }
  335. return nil, err
  336. }
  337. return nil, nil
  338. }
  339. func iterateRepositories(each func(*models.Repository) ([]string, error)) ([]string, error) {
  340. results := []string{}
  341. err := models.Iterate(
  342. models.DefaultDBContext(),
  343. new(models.Repository),
  344. builder.Gt{"id": 0},
  345. func(idx int, bean interface{}) error {
  346. res, err := each(bean.(*models.Repository))
  347. results = append(results, res...)
  348. return err
  349. },
  350. )
  351. return results, err
  352. }
  353. func iteratePRs(repo *models.Repository, each func(*models.Repository, *models.PullRequest) ([]string, error)) ([]string, error) {
  354. results := []string{}
  355. err := models.Iterate(
  356. models.DefaultDBContext(),
  357. new(models.PullRequest),
  358. builder.Eq{"base_repo_id": repo.ID},
  359. func(idx int, bean interface{}) error {
  360. res, err := each(repo, bean.(*models.PullRequest))
  361. results = append(results, res...)
  362. return err
  363. },
  364. )
  365. return results, err
  366. }
  367. func runDoctorHooks(ctx *cli.Context) ([]string, error) {
  368. // Need to iterate across all of the repositories
  369. return iterateRepositories(func(repo *models.Repository) ([]string, error) {
  370. results, err := repository.CheckDelegateHooks(repo.RepoPath())
  371. if err != nil {
  372. return nil, err
  373. }
  374. if len(results) > 0 && ctx.Bool("fix") {
  375. return []string{fmt.Sprintf("regenerated hooks for %s", repo.FullName())}, repository.CreateDelegateHooks(repo.RepoPath())
  376. }
  377. return results, nil
  378. })
  379. }
  380. func runDoctorPRMergeBase(ctx *cli.Context) ([]string, error) {
  381. numRepos := 0
  382. numPRs := 0
  383. numPRsUpdated := 0
  384. results, err := iterateRepositories(func(repo *models.Repository) ([]string, error) {
  385. numRepos++
  386. return iteratePRs(repo, func(repo *models.Repository, pr *models.PullRequest) ([]string, error) {
  387. numPRs++
  388. results := []string{}
  389. pr.BaseRepo = repo
  390. repoPath := repo.RepoPath()
  391. oldMergeBase := pr.MergeBase
  392. if !pr.HasMerged {
  393. var err error
  394. pr.MergeBase, err = git.NewCommand("merge-base", "--", pr.BaseBranch, pr.GetGitRefName()).RunInDir(repoPath)
  395. if err != nil {
  396. var err2 error
  397. pr.MergeBase, err2 = git.NewCommand("rev-parse", git.BranchPrefix+pr.BaseBranch).RunInDir(repoPath)
  398. if err2 != nil {
  399. results = append(results, fmt.Sprintf("WARN: Unable to get merge base for PR ID %d, #%d onto %s in %s/%s", pr.ID, pr.Index, pr.BaseBranch, pr.BaseRepo.OwnerName, pr.BaseRepo.Name))
  400. log.Error("Unable to get merge base for PR ID %d, Index %d in %s/%s. Error: %v & %v", pr.ID, pr.Index, pr.BaseRepo.OwnerName, pr.BaseRepo.Name, err, err2)
  401. return results, nil
  402. }
  403. }
  404. } else {
  405. parentsString, err := git.NewCommand("rev-list", "--parents", "-n", "1", pr.MergedCommitID).RunInDir(repoPath)
  406. if err != nil {
  407. results = append(results, fmt.Sprintf("WARN: Unable to get parents for merged PR ID %d, #%d onto %s in %s/%s", pr.ID, pr.Index, pr.BaseBranch, pr.BaseRepo.OwnerName, pr.BaseRepo.Name))
  408. log.Error("Unable to get parents for merged PR ID %d, Index %d in %s/%s. Error: %v", pr.ID, pr.Index, pr.BaseRepo.OwnerName, pr.BaseRepo.Name, err)
  409. return results, nil
  410. }
  411. parents := strings.Split(strings.TrimSpace(parentsString), " ")
  412. if len(parents) < 2 {
  413. return results, nil
  414. }
  415. args := append([]string{"merge-base", "--"}, parents[1:]...)
  416. args = append(args, pr.GetGitRefName())
  417. pr.MergeBase, err = git.NewCommand(args...).RunInDir(repoPath)
  418. if err != nil {
  419. results = append(results, fmt.Sprintf("WARN: Unable to get merge base for merged PR ID %d, #%d onto %s in %s/%s", pr.ID, pr.Index, pr.BaseBranch, pr.BaseRepo.OwnerName, pr.BaseRepo.Name))
  420. log.Error("Unable to get merge base for merged PR ID %d, Index %d in %s/%s. Error: %v", pr.ID, pr.Index, pr.BaseRepo.OwnerName, pr.BaseRepo.Name, err)
  421. return results, nil
  422. }
  423. }
  424. pr.MergeBase = strings.TrimSpace(pr.MergeBase)
  425. if pr.MergeBase != oldMergeBase {
  426. if ctx.Bool("fix") {
  427. if err := pr.UpdateCols("merge_base"); err != nil {
  428. return results, err
  429. }
  430. } else {
  431. results = append(results, fmt.Sprintf("#%d onto %s in %s/%s: MergeBase should be %s but is %s", pr.Index, pr.BaseBranch, pr.BaseRepo.OwnerName, pr.BaseRepo.Name, oldMergeBase, pr.MergeBase))
  432. }
  433. numPRsUpdated++
  434. }
  435. return results, nil
  436. })
  437. })
  438. if ctx.Bool("fix") {
  439. results = append(results, fmt.Sprintf("%d PR mergebases updated of %d PRs total in %d repos", numPRsUpdated, numPRs, numRepos))
  440. } else {
  441. if numPRsUpdated > 0 && err == nil {
  442. return results, fmt.Errorf("%d PRs with incorrect mergebases of %d PRs total in %d repos", numPRsUpdated, numPRs, numRepos)
  443. }
  444. results = append(results, fmt.Sprintf("%d PRs with incorrect mergebases of %d PRs total in %d repos", numPRsUpdated, numPRs, numRepos))
  445. }
  446. return results, err
  447. }
  448. func runDoctorScriptType(ctx *cli.Context) ([]string, error) {
  449. path, err := exec.LookPath(setting.ScriptType)
  450. if err != nil {
  451. return []string{fmt.Sprintf("ScriptType %s is not on the current PATH", setting.ScriptType)}, err
  452. }
  453. return []string{fmt.Sprintf("ScriptType %s is on the current PATH at %s", setting.ScriptType, path)}, nil
  454. }
  455. func runDoctorCheckDBConsistency(ctx *cli.Context) ([]string, error) {
  456. var results []string
  457. // make sure DB version is uptodate
  458. if err := models.NewEngine(context.Background(), migrations.EnsureUpToDate); err != nil {
  459. return nil, fmt.Errorf("model version on the database does not match the current Gitea version. Model consistency will not be checked until the database is upgraded")
  460. }
  461. //find labels without existing repo or org
  462. count, err := models.CountOrphanedLabels()
  463. if err != nil {
  464. return nil, err
  465. }
  466. if count > 0 {
  467. if ctx.Bool("fix") {
  468. if err = models.DeleteOrphanedLabels(); err != nil {
  469. return nil, err
  470. }
  471. results = append(results, fmt.Sprintf("%d labels without existing repository/organisation deleted", count))
  472. } else {
  473. results = append(results, fmt.Sprintf("%d labels without existing repository/organisation", count))
  474. }
  475. }
  476. //find issues without existing repository
  477. count, err = models.CountOrphanedIssues()
  478. if err != nil {
  479. return nil, err
  480. }
  481. if count > 0 {
  482. if ctx.Bool("fix") {
  483. if err = models.DeleteOrphanedIssues(); err != nil {
  484. return nil, err
  485. }
  486. results = append(results, fmt.Sprintf("%d issues without existing repository deleted", count))
  487. } else {
  488. results = append(results, fmt.Sprintf("%d issues without existing repository", count))
  489. }
  490. }
  491. //find pulls without existing issues
  492. count, err = models.CountOrphanedObjects("pull_request", "issue", "pull_request.issue_id=issue.id")
  493. if err != nil {
  494. return nil, err
  495. }
  496. if count > 0 {
  497. if ctx.Bool("fix") {
  498. if err = models.DeleteOrphanedObjects("pull_request", "issue", "pull_request.issue_id=issue.id"); err != nil {
  499. return nil, err
  500. }
  501. results = append(results, fmt.Sprintf("%d pull requests without existing issue deleted", count))
  502. } else {
  503. results = append(results, fmt.Sprintf("%d pull requests without existing issue", count))
  504. }
  505. }
  506. //find tracked times without existing issues/pulls
  507. count, err = models.CountOrphanedObjects("tracked_time", "issue", "tracked_time.issue_id=issue.id")
  508. if err != nil {
  509. return nil, err
  510. }
  511. if count > 0 {
  512. if ctx.Bool("fix") {
  513. if err = models.DeleteOrphanedObjects("tracked_time", "issue", "tracked_time.issue_id=issue.id"); err != nil {
  514. return nil, err
  515. }
  516. results = append(results, fmt.Sprintf("%d tracked times without existing issue deleted", count))
  517. } else {
  518. results = append(results, fmt.Sprintf("%d tracked times without existing issue", count))
  519. }
  520. }
  521. //ToDo: function to recalc all counters
  522. return results, nil
  523. }