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.4 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 minio storage endpoint",
  41. },
  42. cli.StringFlag{
  43. Name: "minio-access-key-id",
  44. Value: "",
  45. Usage: "New minio storage accessKeyID",
  46. },
  47. cli.StringFlag{
  48. Name: "minio-secret-access-key",
  49. Value: "",
  50. Usage: "New minio storage secretAccessKey",
  51. },
  52. cli.StringFlag{
  53. Name: "minio-bucket",
  54. Value: "",
  55. Usage: "New minio storage bucket",
  56. },
  57. cli.StringFlag{
  58. Name: "minio-location",
  59. Value: "",
  60. Usage: "New minio storage location to create bucket",
  61. },
  62. cli.StringFlag{
  63. Name: "minio-base-path",
  64. Value: "",
  65. Usage: "New minio storage basepath on the bucket",
  66. },
  67. cli.BoolFlag{
  68. Name: "minio-use-ssl",
  69. Usage: "New minio storage SSL enabled",
  70. },
  71. },
  72. }
  73. func migrateAttachments(dstStorage storage.ObjectStorage) error {
  74. return models.IterateAttachment(func(attach *models.Attachment) error {
  75. _, err := storage.Copy(dstStorage, attach.RelativePath(), storage.Attachments, attach.RelativePath())
  76. return err
  77. })
  78. }
  79. func runMigrateStorage(ctx *cli.Context) error {
  80. if err := initDB(); err != nil {
  81. return err
  82. }
  83. log.Trace("AppPath: %s", setting.AppPath)
  84. log.Trace("AppWorkPath: %s", setting.AppWorkPath)
  85. log.Trace("Custom path: %s", setting.CustomPath)
  86. log.Trace("Log path: %s", setting.LogRootPath)
  87. setting.InitDBConfig()
  88. if err := models.NewEngine(context.Background(), migrations.Migrate); err != nil {
  89. log.Fatal("Failed to initialize ORM engine: %v", err)
  90. return err
  91. }
  92. if err := storage.Init(); err != nil {
  93. return err
  94. }
  95. tp := ctx.String("type")
  96. switch tp {
  97. case "attachments":
  98. var dstStorage storage.ObjectStorage
  99. var err error
  100. switch ctx.String("store") {
  101. case "local":
  102. dstStorage, err = storage.NewLocalStorage(ctx.String("path"))
  103. case "minio":
  104. dstStorage, err = storage.NewMinioStorage(
  105. ctx.String("minio-endpoint"),
  106. ctx.String("minio-access-key-id"),
  107. ctx.String("minio-secret-access-key"),
  108. ctx.String("minio-bucket"),
  109. ctx.String("minio-location"),
  110. ctx.String("minio-base-path"),
  111. ctx.Bool("minio-use-ssl"),
  112. )
  113. default:
  114. return fmt.Errorf("Unsupported attachments store type: %s", ctx.String("store"))
  115. }
  116. if err != nil {
  117. return err
  118. }
  119. return migrateAttachments(dstStorage)
  120. }
  121. return nil
  122. }