You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

migrate_storage.go 3.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/models/migrations"
  10. "code.gitea.io/gitea/modules/log"
  11. "code.gitea.io/gitea/modules/setting"
  12. "code.gitea.io/gitea/modules/storage"
  13. "github.com/urfave/cli"
  14. )
  15. // CmdMigrateStorage represents the available migrate storage sub-command.
  16. var CmdMigrateStorage = cli.Command{
  17. Name: "migrate-storage",
  18. Usage: "Migrate the storage",
  19. Description: "This is a command for migrating storage.",
  20. Action: runMigrateStorage,
  21. Flags: []cli.Flag{
  22. cli.StringFlag{
  23. Name: "type, t",
  24. Value: "",
  25. Usage: "Files type to migrate, currently should be attachments",
  26. },
  27. cli.StringFlag{
  28. Name: "store, s",
  29. Value: "local",
  30. Usage: "New storage type, local or minio",
  31. },
  32. cli.StringFlag{
  33. Name: "path, p",
  34. Value: "",
  35. Usage: "New storage placement if store is local",
  36. },
  37. cli.StringFlag{
  38. Name: "minio-endpoint",
  39. Value: "",
  40. Usage: "New storage placement if store is local",
  41. },
  42. cli.StringFlag{
  43. Name: "minio-access-key-id",
  44. Value: "",
  45. Usage: "New storage placement if store is local",
  46. },
  47. cli.StringFlag{
  48. Name: "minio-scret-access-key",
  49. Value: "",
  50. Usage: "New storage placement if store is local",
  51. },
  52. cli.StringFlag{
  53. Name: "minio-bucket",
  54. Value: "",
  55. Usage: "New storage placement if store is local",
  56. },
  57. cli.StringFlag{
  58. Name: "minio-location",
  59. Value: "",
  60. Usage: "New storage placement if store is local",
  61. },
  62. cli.StringFlag{
  63. Name: "minio-use-ssl",
  64. Value: "",
  65. Usage: "New storage placement if store is local",
  66. },
  67. },
  68. }
  69. func migrateAttachments(dstStorage storage.ObjectStorage) error {
  70. return models.IterateAttachment(func(attach *models.Attachment) error {
  71. _, err := storage.Copy(dstStorage, attach.UUID, storage.Attachments, attach.RelativePath())
  72. return err
  73. })
  74. }
  75. func runMigrateStorage(ctx *cli.Context) error {
  76. if err := initDB(); err != nil {
  77. return err
  78. }
  79. log.Trace("AppPath: %s", setting.AppPath)
  80. log.Trace("AppWorkPath: %s", setting.AppWorkPath)
  81. log.Trace("Custom path: %s", setting.CustomPath)
  82. log.Trace("Log path: %s", setting.LogRootPath)
  83. setting.InitDBConfig()
  84. if err := models.NewEngine(context.Background(), migrations.Migrate); err != nil {
  85. log.Fatal("Failed to initialize ORM engine: %v", err)
  86. return err
  87. }
  88. if err := storage.Init(); err != nil {
  89. return err
  90. }
  91. tp := ctx.String("type")
  92. switch tp {
  93. case "attachments":
  94. var dstStorage storage.ObjectStorage
  95. var err error
  96. switch ctx.String("store") {
  97. case "local":
  98. dstStorage, err = storage.NewLocalStorage(ctx.String("dst"))
  99. case "minio":
  100. dstStorage, err = storage.NewMinioStorage(
  101. ctx.String("minio-endpoint"),
  102. ctx.String("minio-access-key-id"),
  103. ctx.String("minio-secret-access-key"),
  104. ctx.String("minio-bucket"),
  105. ctx.String("minio-location"),
  106. ctx.String("minio-basePath"),
  107. ctx.Bool("minio-useSSL"),
  108. )
  109. default:
  110. return fmt.Errorf("Unsupported attachments store type: %s", ctx.String("store"))
  111. }
  112. if err != nil {
  113. return err
  114. }
  115. return migrateAttachments(dstStorage)
  116. }
  117. return nil
  118. }