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.

173 lines
3.8 KiB

10 years ago
10 years ago
10 years ago
  1. // Copyright 2014 The Gogs 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. "fmt"
  8. "io"
  9. "io/ioutil"
  10. "os"
  11. "path"
  12. "strings"
  13. "github.com/codegangsta/cli"
  14. "github.com/gogits/gogs/models"
  15. "github.com/gogits/gogs/modules/setting"
  16. )
  17. var CmdFix = cli.Command{
  18. Name: "fix",
  19. Usage: "This command for upgrade from old version",
  20. Action: runFix,
  21. Subcommands: fixCommands,
  22. Flags: []cli.Flag{},
  23. }
  24. func runFix(ctx *cli.Context) {
  25. }
  26. var fixCommands = []cli.Command{
  27. {
  28. Name: "location",
  29. Usage: "Change Gogs app location",
  30. Description: `Command location fixes location change of Gogs
  31. gogs fix location <old Gogs path>
  32. `,
  33. Action: runFixLocation,
  34. },
  35. }
  36. // rewriteAuthorizedKeys replaces old Gogs path to the new one.
  37. func rewriteAuthorizedKeys(sshPath, oldPath, newPath string) error {
  38. fr, err := os.Open(sshPath)
  39. if err != nil {
  40. return err
  41. }
  42. defer fr.Close()
  43. tmpPath := sshPath + ".tmp"
  44. fw, err := os.Create(tmpPath)
  45. if err != nil {
  46. return err
  47. }
  48. defer fw.Close()
  49. oldPath = "command=\"" + oldPath + " serv"
  50. newPath = "command=\"" + newPath + " serv"
  51. buf := bufio.NewReader(fr)
  52. for {
  53. line, errRead := buf.ReadString('\n')
  54. line = strings.TrimSpace(line)
  55. if errRead != nil {
  56. if errRead != io.EOF {
  57. return errRead
  58. }
  59. // Reached end of file, if nothing to read then break,
  60. // otherwise handle the last line.
  61. if len(line) == 0 {
  62. break
  63. }
  64. }
  65. // Still finding the line, copy the line that currently read.
  66. if _, err = fw.WriteString(strings.Replace(line, oldPath, newPath, 1) + "\n"); err != nil {
  67. return err
  68. }
  69. if errRead == io.EOF {
  70. break
  71. }
  72. }
  73. if err = os.Remove(sshPath); err != nil {
  74. return err
  75. }
  76. return os.Rename(tmpPath, sshPath)
  77. }
  78. func rewriteUpdateHook(path, appPath string) error {
  79. rp := strings.NewReplacer("\\", "/", " ", "\\ ")
  80. if err := ioutil.WriteFile(path, []byte(fmt.Sprintf(models.TPL_UPDATE_HOOK,
  81. setting.ScriptType, rp.Replace(appPath))), os.ModePerm); err != nil {
  82. return err
  83. }
  84. return nil
  85. }
  86. func walkDir(rootPath, recPath, appPath string, depth int) error {
  87. depth++
  88. if depth > 3 {
  89. return nil
  90. } else if depth == 3 {
  91. if err := rewriteUpdateHook(path.Join(rootPath, "hooks/update"), appPath); err != nil {
  92. return err
  93. }
  94. }
  95. dir, err := os.Open(rootPath)
  96. if err != nil {
  97. return err
  98. }
  99. defer dir.Close()
  100. fis, err := dir.Readdir(0)
  101. if err != nil {
  102. return err
  103. }
  104. for _, fi := range fis {
  105. if strings.Contains(fi.Name(), ".DS_Store") {
  106. continue
  107. }
  108. relPath := path.Join(recPath, fi.Name())
  109. curPath := path.Join(rootPath, fi.Name())
  110. if fi.IsDir() {
  111. if err = walkDir(curPath, relPath, appPath, depth); err != nil {
  112. return err
  113. }
  114. }
  115. }
  116. return nil
  117. }
  118. func runFixLocation(ctx *cli.Context) {
  119. if len(ctx.Args()) != 1 {
  120. fmt.Println("Incorrect arguments number, expect 1")
  121. os.Exit(2)
  122. }
  123. execPath, _ := setting.ExecPath()
  124. oldPath := ctx.Args().First()
  125. fmt.Printf("Old location: %s\n", oldPath)
  126. fmt.Println("This command should be executed in the new Gogs path")
  127. fmt.Printf("Do you want to change Gogs app path from old location to:\n")
  128. fmt.Printf("-> %s?\n", execPath)
  129. fmt.Print("Press <enter> to continue, use <Ctrl+c> to exit.")
  130. fmt.Scanln()
  131. // Fix in authorized_keys file.
  132. sshPath := path.Join(models.SshPath, "authorized_keys")
  133. fmt.Printf("Fixing pathes in file: %s\n", sshPath)
  134. if err := rewriteAuthorizedKeys(sshPath, oldPath, execPath); err != nil {
  135. fmt.Println(err)
  136. os.Exit(1)
  137. }
  138. // Fix position in gogs-repositories.
  139. setting.NewConfigContext()
  140. fmt.Printf("Fixing pathes in repositories: %s\n", setting.RepoRootPath)
  141. if err := walkDir(setting.RepoRootPath, "", execPath, 0); err != nil {
  142. fmt.Println(err)
  143. os.Exit(1)
  144. }
  145. fmt.Println("Fix position finished!")
  146. }