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.

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. user_model "code.gitea.io/gitea/models/user"
  14. "code.gitea.io/gitea/modules/log"
  15. "code.gitea.io/gitea/modules/setting"
  16. "code.gitea.io/gitea/modules/storage"
  17. "github.com/urfave/cli"
  18. )
  19. // CmdMigrateStorage represents the available migrate storage sub-command.
  20. var CmdMigrateStorage = cli.Command{
  21. Name: "migrate-storage",
  22. Usage: "Migrate the storage",
  23. Description: "This is a command for migrating storage.",
  24. Action: runMigrateStorage,
  25. Flags: []cli.Flag{
  26. cli.StringFlag{
  27. Name: "type, t",
  28. Value: "",
  29. Usage: "Kinds of files to migrate, currently only 'attachments' is supported",
  30. },
  31. cli.StringFlag{
  32. Name: "storage, s",
  33. Value: "",
  34. Usage: "New storage type: local (default) or minio",
  35. },
  36. cli.StringFlag{
  37. Name: "path, p",
  38. Value: "",
  39. Usage: "New storage placement if store is local (leave blank for default)",
  40. },
  41. cli.StringFlag{
  42. Name: "minio-endpoint",
  43. Value: "",
  44. Usage: "Minio storage endpoint",
  45. },
  46. cli.StringFlag{
  47. Name: "minio-access-key-id",
  48. Value: "",
  49. Usage: "Minio storage accessKeyID",
  50. },
  51. cli.StringFlag{
  52. Name: "minio-secret-access-key",
  53. Value: "",
  54. Usage: "Minio storage secretAccessKey",
  55. },
  56. cli.StringFlag{
  57. Name: "minio-bucket",
  58. Value: "",
  59. Usage: "Minio storage bucket",
  60. },
  61. cli.StringFlag{
  62. Name: "minio-location",
  63. Value: "",
  64. Usage: "Minio storage location to create bucket",
  65. },
  66. cli.StringFlag{
  67. Name: "minio-base-path",
  68. Value: "",
  69. Usage: "Minio storage basepath on the bucket",
  70. },
  71. cli.BoolFlag{
  72. Name: "minio-use-ssl",
  73. Usage: "Enable SSL for minio",
  74. },
  75. },
  76. }
  77. func migrateAttachments(dstStorage storage.ObjectStorage) error {
  78. return repo_model.IterateAttachment(func(attach *repo_model.Attachment) error {
  79. _, err := storage.Copy(dstStorage, attach.RelativePath(), storage.Attachments, attach.RelativePath())
  80. return err
  81. })
  82. }
  83. func migrateLFS(dstStorage storage.ObjectStorage) error {
  84. return models.IterateLFS(func(mo *models.LFSMetaObject) error {
  85. _, err := storage.Copy(dstStorage, mo.RelativePath(), storage.LFS, mo.RelativePath())
  86. return err
  87. })
  88. }
  89. func migrateAvatars(dstStorage storage.ObjectStorage) error {
  90. return user_model.IterateUser(func(user *user_model.User) error {
  91. _, err := storage.Copy(dstStorage, user.CustomAvatarRelativePath(), storage.Avatars, user.CustomAvatarRelativePath())
  92. return err
  93. })
  94. }
  95. func migrateRepoAvatars(dstStorage storage.ObjectStorage) error {
  96. return models.IterateRepository(func(repo *models.Repository) error {
  97. _, err := storage.Copy(dstStorage, repo.CustomAvatarRelativePath(), storage.RepoAvatars, repo.CustomAvatarRelativePath())
  98. return err
  99. })
  100. }
  101. func runMigrateStorage(ctx *cli.Context) error {
  102. stdCtx, cancel := installSignals()
  103. defer cancel()
  104. if err := initDB(stdCtx); err != nil {
  105. return err
  106. }
  107. log.Info("AppPath: %s", setting.AppPath)
  108. log.Info("AppWorkPath: %s", setting.AppWorkPath)
  109. log.Info("Custom path: %s", setting.CustomPath)
  110. log.Info("Log path: %s", setting.LogRootPath)
  111. log.Info("Configuration file: %s", setting.CustomConf)
  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. }