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 6.1KB

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