fix.go 4.0 KB

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