2020-08-18 09:53:45 +05:30
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2020-08-18 09:53:45 +05:30
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-09-29 14:35:13 +05:30
|
|
|
"strings"
|
2020-08-18 09:53:45 +05:30
|
|
|
|
2021-09-19 17:19:59 +05:30
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-12 21:21:54 +05:30
|
|
|
git_model "code.gitea.io/gitea/models/git"
|
2020-08-18 09:53:45 +05:30
|
|
|
"code.gitea.io/gitea/models/migrations"
|
2022-08-16 09:35:15 +05:30
|
|
|
packages_model "code.gitea.io/gitea/models/packages"
|
2021-11-19 19:09:57 +05:30
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-24 15:19:20 +05:30
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2020-08-18 09:53:45 +05:30
|
|
|
"code.gitea.io/gitea/modules/log"
|
2022-08-16 09:35:15 +05:30
|
|
|
packages_module "code.gitea.io/gitea/modules/packages"
|
2020-08-18 09:53:45 +05:30
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
"code.gitea.io/gitea/modules/storage"
|
|
|
|
|
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CmdMigrateStorage represents the available migrate storage sub-command.
|
|
|
|
var CmdMigrateStorage = cli.Command{
|
|
|
|
Name: "migrate-storage",
|
|
|
|
Usage: "Migrate the storage",
|
2022-08-16 09:35:15 +05:30
|
|
|
Description: "Copies stored files from storage configured in app.ini to parameter-configured storage",
|
2020-08-18 09:53:45 +05:30
|
|
|
Action: runMigrateStorage,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "type, t",
|
|
|
|
Value: "",
|
2022-08-16 09:35:15 +05:30
|
|
|
Usage: "Type of stored files to copy. Allowed types: 'attachments', 'lfs', 'avatars', 'repo-avatars', 'repo-archivers', 'packages'",
|
2020-08-18 09:53:45 +05:30
|
|
|
},
|
|
|
|
cli.StringFlag{
|
2020-09-29 14:35:13 +05:30
|
|
|
Name: "storage, s",
|
2020-10-13 09:28:34 +05:30
|
|
|
Value: "",
|
|
|
|
Usage: "New storage type: local (default) or minio",
|
2020-08-18 09:53:45 +05:30
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "path, p",
|
|
|
|
Value: "",
|
|
|
|
Usage: "New storage placement if store is local (leave blank for default)",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "minio-endpoint",
|
|
|
|
Value: "",
|
|
|
|
Usage: "Minio storage endpoint",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "minio-access-key-id",
|
|
|
|
Value: "",
|
|
|
|
Usage: "Minio storage accessKeyID",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "minio-secret-access-key",
|
|
|
|
Value: "",
|
|
|
|
Usage: "Minio storage secretAccessKey",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "minio-bucket",
|
|
|
|
Value: "",
|
|
|
|
Usage: "Minio storage bucket",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "minio-location",
|
|
|
|
Value: "",
|
|
|
|
Usage: "Minio storage location to create bucket",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "minio-base-path",
|
|
|
|
Value: "",
|
|
|
|
Usage: "Minio storage basepath on the bucket",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "minio-use-ssl",
|
|
|
|
Usage: "Enable SSL for minio",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-08-16 09:35:15 +05:30
|
|
|
func migrateAttachments(ctx context.Context, dstStorage storage.ObjectStorage) error {
|
2022-10-31 21:21:14 +05:30
|
|
|
return db.Iterate(ctx, nil, func(ctx context.Context, attach *repo_model.Attachment) error {
|
2020-08-18 09:53:45 +05:30
|
|
|
_, err := storage.Copy(dstStorage, attach.RelativePath(), storage.Attachments, attach.RelativePath())
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-16 09:35:15 +05:30
|
|
|
func migrateLFS(ctx context.Context, dstStorage storage.ObjectStorage) error {
|
2022-10-31 21:21:14 +05:30
|
|
|
return db.Iterate(ctx, nil, func(ctx context.Context, mo *git_model.LFSMetaObject) error {
|
2020-09-08 21:15:10 +05:30
|
|
|
_, err := storage.Copy(dstStorage, mo.RelativePath(), storage.LFS, mo.RelativePath())
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-16 09:35:15 +05:30
|
|
|
func migrateAvatars(ctx context.Context, dstStorage storage.ObjectStorage) error {
|
2022-10-31 21:21:14 +05:30
|
|
|
return db.Iterate(ctx, nil, func(ctx context.Context, user *user_model.User) error {
|
2020-10-14 18:37:51 +05:30
|
|
|
_, err := storage.Copy(dstStorage, user.CustomAvatarRelativePath(), storage.Avatars, user.CustomAvatarRelativePath())
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-16 09:35:15 +05:30
|
|
|
func migrateRepoAvatars(ctx context.Context, dstStorage storage.ObjectStorage) error {
|
2022-10-31 21:21:14 +05:30
|
|
|
return db.Iterate(ctx, nil, func(ctx context.Context, repo *repo_model.Repository) error {
|
2020-10-14 18:37:51 +05:30
|
|
|
_, err := storage.Copy(dstStorage, repo.CustomAvatarRelativePath(), storage.RepoAvatars, repo.CustomAvatarRelativePath())
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-16 09:35:15 +05:30
|
|
|
func migrateRepoArchivers(ctx context.Context, dstStorage storage.ObjectStorage) error {
|
2022-10-31 21:21:14 +05:30
|
|
|
return db.Iterate(ctx, nil, func(ctx context.Context, archiver *repo_model.RepoArchiver) error {
|
2022-08-29 15:15:20 +05:30
|
|
|
p := archiver.RelativePath()
|
|
|
|
_, err := storage.Copy(dstStorage, p, storage.RepoArchives, p)
|
2022-08-16 09:35:15 +05:30
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func migratePackages(ctx context.Context, dstStorage storage.ObjectStorage) error {
|
2022-10-31 21:21:14 +05:30
|
|
|
return db.Iterate(ctx, nil, func(ctx context.Context, pb *packages_model.PackageBlob) error {
|
2022-08-16 09:35:15 +05:30
|
|
|
p := packages_module.KeyToRelativePath(packages_module.BlobHash256Key(pb.HashSHA256))
|
|
|
|
_, err := storage.Copy(dstStorage, p, storage.Packages, p)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-08-18 09:53:45 +05:30
|
|
|
func runMigrateStorage(ctx *cli.Context) error {
|
2021-11-07 08:41:27 +05:30
|
|
|
stdCtx, cancel := installSignals()
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
if err := initDB(stdCtx); err != nil {
|
2020-08-18 09:53:45 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-27 06:26:58 +05:30
|
|
|
log.Info("AppPath: %s", setting.AppPath)
|
|
|
|
log.Info("AppWorkPath: %s", setting.AppWorkPath)
|
|
|
|
log.Info("Custom path: %s", setting.CustomPath)
|
2023-02-19 21:42:01 +05:30
|
|
|
log.Info("Log path: %s", setting.Log.RootPath)
|
2021-09-14 06:54:57 +05:30
|
|
|
log.Info("Configuration file: %s", setting.CustomConf)
|
2020-08-18 09:53:45 +05:30
|
|
|
|
2021-10-30 20:02:11 +05:30
|
|
|
if err := db.InitEngineWithMigration(context.Background(), migrations.Migrate); err != nil {
|
2020-08-18 09:53:45 +05:30
|
|
|
log.Fatal("Failed to initialize ORM engine: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := storage.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-08 21:15:10 +05:30
|
|
|
var dstStorage storage.ObjectStorage
|
|
|
|
var err error
|
2020-09-29 14:35:13 +05:30
|
|
|
switch strings.ToLower(ctx.String("storage")) {
|
2020-10-13 09:28:34 +05:30
|
|
|
case "":
|
|
|
|
fallthrough
|
|
|
|
case string(storage.LocalStorageType):
|
2020-09-08 21:15:10 +05:30
|
|
|
p := ctx.String("path")
|
|
|
|
if p == "" {
|
2020-09-29 14:35:13 +05:30
|
|
|
log.Fatal("Path must be given when storage is loal")
|
2020-09-08 21:15:10 +05:30
|
|
|
return nil
|
|
|
|
}
|
2020-10-13 09:28:34 +05:30
|
|
|
dstStorage, err = storage.NewLocalStorage(
|
2022-08-16 09:35:15 +05:30
|
|
|
stdCtx,
|
2020-10-13 09:28:34 +05:30
|
|
|
storage.LocalStorageConfig{
|
|
|
|
Path: p,
|
|
|
|
})
|
|
|
|
case string(storage.MinioStorageType):
|
2020-09-08 21:15:10 +05:30
|
|
|
dstStorage, err = storage.NewMinioStorage(
|
2022-08-16 09:35:15 +05:30
|
|
|
stdCtx,
|
2020-10-13 09:28:34 +05:30
|
|
|
storage.MinioStorageConfig{
|
|
|
|
Endpoint: ctx.String("minio-endpoint"),
|
|
|
|
AccessKeyID: ctx.String("minio-access-key-id"),
|
|
|
|
SecretAccessKey: ctx.String("minio-secret-access-key"),
|
|
|
|
Bucket: ctx.String("minio-bucket"),
|
|
|
|
Location: ctx.String("minio-location"),
|
|
|
|
BasePath: ctx.String("minio-base-path"),
|
|
|
|
UseSSL: ctx.Bool("minio-use-ssl"),
|
|
|
|
})
|
2020-09-08 21:15:10 +05:30
|
|
|
default:
|
2022-08-16 09:35:15 +05:30
|
|
|
return fmt.Errorf("unsupported storage type: %s", ctx.String("storage"))
|
2020-09-08 21:15:10 +05:30
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-16 09:35:15 +05:30
|
|
|
migratedMethods := map[string]func(context.Context, storage.ObjectStorage) error{
|
|
|
|
"attachments": migrateAttachments,
|
|
|
|
"lfs": migrateLFS,
|
|
|
|
"avatars": migrateAvatars,
|
|
|
|
"repo-avatars": migrateRepoAvatars,
|
|
|
|
"repo-archivers": migrateRepoArchivers,
|
|
|
|
"packages": migratePackages,
|
|
|
|
}
|
|
|
|
|
2020-09-29 14:35:13 +05:30
|
|
|
tp := strings.ToLower(ctx.String("type"))
|
2022-08-16 09:35:15 +05:30
|
|
|
if m, ok := migratedMethods[tp]; ok {
|
|
|
|
if err := m(stdCtx, dstStorage); err != nil {
|
2020-10-14 18:37:51 +05:30
|
|
|
return err
|
|
|
|
}
|
2022-08-16 09:35:15 +05:30
|
|
|
log.Info("%s files have successfully been copied to the new storage.", tp)
|
|
|
|
return nil
|
2020-08-18 09:53:45 +05:30
|
|
|
}
|
|
|
|
|
2022-08-16 09:35:15 +05:30
|
|
|
return fmt.Errorf("unsupported storage: %s", ctx.String("type"))
|
2020-08-18 09:53:45 +05:30
|
|
|
}
|