您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

migrate_storage.go 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // Copyright 2020 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. "context"
  7. "fmt"
  8. "strings"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/models/db"
  11. "code.gitea.io/gitea/models/migrations"
  12. repo_model "code.gitea.io/gitea/models/repo"
  13. "code.gitea.io/gitea/modules/log"
  14. "code.gitea.io/gitea/modules/setting"
  15. "code.gitea.io/gitea/modules/storage"
  16. "github.com/urfave/cli"
  17. )
  18. // CmdMigrateStorage represents the available migrate storage sub-command.
  19. var CmdMigrateStorage = cli.Command{
  20. Name: "migrate-storage",
  21. Usage: "Migrate the storage",
  22. Description: "This is a command for migrating storage.",
  23. Action: runMigrateStorage,
  24. Flags: []cli.Flag{
  25. cli.StringFlag{
  26. Name: "type, t",
  27. Value: "",
  28. Usage: "Kinds of files to migrate, currently only 'attachments' is supported",
  29. },
  30. cli.StringFlag{
  31. Name: "storage, s",
  32. Value: "",
  33. Usage: "New storage type: local (default) or minio",
  34. },
  35. cli.StringFlag{
  36. Name: "path, p",
  37. Value: "",
  38. Usage: "New storage placement if store is local (leave blank for default)",
  39. },
  40. cli.StringFlag{
  41. Name: "minio-endpoint",
  42. Value: "",
  43. Usage: "Minio storage endpoint",
  44. },
  45. cli.StringFlag{
  46. Name: "minio-access-key-id",
  47. Value: "",
  48. Usage: "Minio storage accessKeyID",
  49. },
  50. cli.StringFlag{
  51. Name: "minio-secret-access-key",
  52. Value: "",
  53. Usage: "Minio storage secretAccessKey",
  54. },
  55. cli.StringFlag{
  56. Name: "minio-bucket",
  57. Value: "",
  58. Usage: "Minio storage bucket",
  59. },
  60. cli.StringFlag{
  61. Name: "minio-location",
  62. Value: "",
  63. Usage: "Minio storage location to create bucket",
  64. },
  65. cli.StringFlag{
  66. Name: "minio-base-path",
  67. Value: "",
  68. Usage: "Minio storage basepath on the bucket",
  69. },
  70. cli.BoolFlag{
  71. Name: "minio-use-ssl",
  72. Usage: "Enable SSL for minio",
  73. },
  74. },
  75. }
  76. func migrateAttachments(dstStorage storage.ObjectStorage) error {
  77. return repo_model.IterateAttachment(func(attach *repo_model.Attachment) error {
  78. _, err := storage.Copy(dstStorage, attach.RelativePath(), storage.Attachments, attach.RelativePath())
  79. return err
  80. })
  81. }
  82. func migrateLFS(dstStorage storage.ObjectStorage) error {
  83. return models.IterateLFS(func(mo *models.LFSMetaObject) error {
  84. _, err := storage.Copy(dstStorage, mo.RelativePath(), storage.LFS, mo.RelativePath())
  85. return err
  86. })
  87. }
  88. func migrateAvatars(dstStorage storage.ObjectStorage) error {
  89. return models.IterateUser(func(user *models.User) error {
  90. _, err := storage.Copy(dstStorage, user.CustomAvatarRelativePath(), storage.Avatars, user.CustomAvatarRelativePath())
  91. return err
  92. })
  93. }
  94. func migrateRepoAvatars(dstStorage storage.ObjectStorage) error {
  95. return models.IterateRepository(func(repo *models.Repository) error {
  96. _, err := storage.Copy(dstStorage, repo.CustomAvatarRelativePath(), storage.RepoAvatars, repo.CustomAvatarRelativePath())
  97. return err
  98. })
  99. }
  100. func runMigrateStorage(ctx *cli.Context) error {
  101. stdCtx, cancel := installSignals()
  102. defer cancel()
  103. if err := initDB(stdCtx); err != nil {
  104. return err
  105. }
  106. log.Info("AppPath: %s", setting.AppPath)
  107. log.Info("AppWorkPath: %s", setting.AppWorkPath)
  108. log.Info("Custom path: %s", setting.CustomPath)
  109. log.Info("Log path: %s", setting.LogRootPath)
  110. log.Info("Configuration file: %s", setting.CustomConf)
  111. setting.InitDBConfig()
  112. if err := db.InitEngineWithMigration(context.Background(), migrations.Migrate); err != nil {
  113. log.Fatal("Failed to initialize ORM engine: %v", err)
  114. return err
  115. }
  116. goCtx := context.Background()
  117. if err := storage.Init(); err != nil {
  118. return err
  119. }
  120. var dstStorage storage.ObjectStorage
  121. var err error
  122. switch strings.ToLower(ctx.String("storage")) {
  123. case "":
  124. fallthrough
  125. case string(storage.LocalStorageType):
  126. p := ctx.String("path")
  127. if p == "" {
  128. log.Fatal("Path must be given when storage is loal")
  129. return nil
  130. }
  131. dstStorage, err = storage.NewLocalStorage(
  132. goCtx,
  133. storage.LocalStorageConfig{
  134. Path: p,
  135. })
  136. case string(storage.MinioStorageType):
  137. dstStorage, err = storage.NewMinioStorage(
  138. goCtx,
  139. storage.MinioStorageConfig{
  140. Endpoint: ctx.String("minio-endpoint"),
  141. AccessKeyID: ctx.String("minio-access-key-id"),
  142. SecretAccessKey: ctx.String("minio-secret-access-key"),
  143. Bucket: ctx.String("minio-bucket"),
  144. Location: ctx.String("minio-location"),
  145. BasePath: ctx.String("minio-base-path"),
  146. UseSSL: ctx.Bool("minio-use-ssl"),
  147. })
  148. default:
  149. return fmt.Errorf("Unsupported storage type: %s", ctx.String("storage"))
  150. }
  151. if err != nil {
  152. return err
  153. }
  154. tp := strings.ToLower(ctx.String("type"))
  155. switch tp {
  156. case "attachments":
  157. if err := migrateAttachments(dstStorage); err != nil {
  158. return err
  159. }
  160. case "lfs":
  161. if err := migrateLFS(dstStorage); err != nil {
  162. return err
  163. }
  164. case "avatars":
  165. if err := migrateAvatars(dstStorage); err != nil {
  166. return err
  167. }
  168. case "repo-avatars":
  169. if err := migrateRepoAvatars(dstStorage); err != nil {
  170. return err
  171. }
  172. default:
  173. return fmt.Errorf("Unsupported storage: %s", ctx.String("type"))
  174. }
  175. log.Warn("All files have been copied to the new placement but old files are still on the original placement.")
  176. return nil
  177. }