2020-08-18 00:23:45 -04:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-08-18 00:23:45 -04:00
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-06-05 00:00:56 -04:00
|
|
|
"errors"
|
2020-08-18 00:23:45 -04:00
|
|
|
"fmt"
|
2024-06-05 00:00:56 -04:00
|
|
|
"io/fs"
|
2020-09-29 05:05:13 -04:00
|
|
|
"strings"
|
2020-08-18 00:23:45 -04:00
|
|
|
|
2023-05-12 09:30:28 -04:00
|
|
|
actions_model "code.gitea.io/gitea/models/actions"
|
2021-09-19 07:49:59 -04:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-12 11:51:54 -04:00
|
|
|
git_model "code.gitea.io/gitea/models/git"
|
2020-08-18 00:23:45 -04:00
|
|
|
"code.gitea.io/gitea/models/migrations"
|
2022-08-16 00:05:15 -04:00
|
|
|
packages_model "code.gitea.io/gitea/models/packages"
|
2021-11-19 08:39:57 -05:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-24 04:49:20 -05:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2020-08-18 00:23:45 -04:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2022-08-16 00:05:15 -04:00
|
|
|
packages_module "code.gitea.io/gitea/modules/packages"
|
2020-08-18 00:23:45 -04:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
"code.gitea.io/gitea/modules/storage"
|
|
|
|
|
2023-07-21 05:28:19 -04:00
|
|
|
"github.com/urfave/cli/v2"
|
2020-08-18 00:23:45 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// CmdMigrateStorage represents the available migrate storage sub-command.
|
2023-07-21 05:28:19 -04:00
|
|
|
var CmdMigrateStorage = &cli.Command{
|
2020-08-18 00:23:45 -04:00
|
|
|
Name: "migrate-storage",
|
|
|
|
Usage: "Migrate the storage",
|
2022-08-16 00:05:15 -04:00
|
|
|
Description: "Copies stored files from storage configured in app.ini to parameter-configured storage",
|
2020-08-18 00:23:45 -04:00
|
|
|
Action: runMigrateStorage,
|
|
|
|
Flags: []cli.Flag{
|
2023-07-21 05:28:19 -04:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "type",
|
|
|
|
Aliases: []string{"t"},
|
|
|
|
Value: "",
|
2024-05-07 02:45:30 -04:00
|
|
|
Usage: "Type of stored files to copy. Allowed types: 'attachments', 'lfs', 'avatars', 'repo-avatars', 'repo-archivers', 'packages', 'actions-log', 'actions-artifacts",
|
2020-08-18 00:23:45 -04:00
|
|
|
},
|
2023-07-21 05:28:19 -04:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "storage",
|
|
|
|
Aliases: []string{"s"},
|
|
|
|
Value: "",
|
2024-05-30 03:33:50 -04:00
|
|
|
Usage: "New storage type: local (default), minio or azureblob",
|
2020-08-18 00:23:45 -04:00
|
|
|
},
|
2023-07-21 05:28:19 -04:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "path",
|
|
|
|
Aliases: []string{"p"},
|
|
|
|
Value: "",
|
|
|
|
Usage: "New storage placement if store is local (leave blank for default)",
|
2020-08-18 00:23:45 -04:00
|
|
|
},
|
2024-05-30 03:33:50 -04:00
|
|
|
// Minio Storage special configurations
|
2023-07-21 05:28:19 -04:00
|
|
|
&cli.StringFlag{
|
2020-08-18 00:23:45 -04:00
|
|
|
Name: "minio-endpoint",
|
|
|
|
Value: "",
|
|
|
|
Usage: "Minio storage endpoint",
|
|
|
|
},
|
2023-07-21 05:28:19 -04:00
|
|
|
&cli.StringFlag{
|
2020-08-18 00:23:45 -04:00
|
|
|
Name: "minio-access-key-id",
|
|
|
|
Value: "",
|
|
|
|
Usage: "Minio storage accessKeyID",
|
|
|
|
},
|
2023-07-21 05:28:19 -04:00
|
|
|
&cli.StringFlag{
|
2020-08-18 00:23:45 -04:00
|
|
|
Name: "minio-secret-access-key",
|
|
|
|
Value: "",
|
|
|
|
Usage: "Minio storage secretAccessKey",
|
|
|
|
},
|
2023-07-21 05:28:19 -04:00
|
|
|
&cli.StringFlag{
|
2020-08-18 00:23:45 -04:00
|
|
|
Name: "minio-bucket",
|
|
|
|
Value: "",
|
|
|
|
Usage: "Minio storage bucket",
|
|
|
|
},
|
2023-07-21 05:28:19 -04:00
|
|
|
&cli.StringFlag{
|
2020-08-18 00:23:45 -04:00
|
|
|
Name: "minio-location",
|
|
|
|
Value: "",
|
|
|
|
Usage: "Minio storage location to create bucket",
|
|
|
|
},
|
2023-07-21 05:28:19 -04:00
|
|
|
&cli.StringFlag{
|
2020-08-18 00:23:45 -04:00
|
|
|
Name: "minio-base-path",
|
|
|
|
Value: "",
|
2023-03-28 11:10:24 -04:00
|
|
|
Usage: "Minio storage base path on the bucket",
|
2020-08-18 00:23:45 -04:00
|
|
|
},
|
2023-07-21 05:28:19 -04:00
|
|
|
&cli.BoolFlag{
|
2020-08-18 00:23:45 -04:00
|
|
|
Name: "minio-use-ssl",
|
|
|
|
Usage: "Enable SSL for minio",
|
|
|
|
},
|
2023-07-21 05:28:19 -04:00
|
|
|
&cli.BoolFlag{
|
2023-03-28 11:10:24 -04:00
|
|
|
Name: "minio-insecure-skip-verify",
|
|
|
|
Usage: "Skip SSL verification",
|
|
|
|
},
|
2023-07-21 05:28:19 -04:00
|
|
|
&cli.StringFlag{
|
2023-03-28 11:10:24 -04:00
|
|
|
Name: "minio-checksum-algorithm",
|
|
|
|
Value: "",
|
|
|
|
Usage: "Minio checksum algorithm (default/md5)",
|
|
|
|
},
|
2024-05-15 09:56:17 -04:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "minio-bucket-lookup-type",
|
|
|
|
Value: "",
|
|
|
|
Usage: "Minio bucket lookup type",
|
|
|
|
},
|
2024-05-30 03:33:50 -04:00
|
|
|
// Azure Blob Storage special configurations
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "azureblob-endpoint",
|
|
|
|
Value: "",
|
|
|
|
Usage: "Azure Blob storage endpoint",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "azureblob-account-name",
|
|
|
|
Value: "",
|
|
|
|
Usage: "Azure Blob storage account name",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "azureblob-account-key",
|
|
|
|
Value: "",
|
|
|
|
Usage: "Azure Blob storage account key",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "azureblob-container",
|
|
|
|
Value: "",
|
|
|
|
Usage: "Azure Blob storage container",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "azureblob-base-path",
|
|
|
|
Value: "",
|
|
|
|
Usage: "Azure Blob storage base path",
|
|
|
|
},
|
2020-08-18 00:23:45 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-08-16 00:05:15 -04:00
|
|
|
func migrateAttachments(ctx context.Context, dstStorage storage.ObjectStorage) error {
|
2022-10-31 11:51:14 -04:00
|
|
|
return db.Iterate(ctx, nil, func(ctx context.Context, attach *repo_model.Attachment) error {
|
2020-08-18 00:23:45 -04:00
|
|
|
_, err := storage.Copy(dstStorage, attach.RelativePath(), storage.Attachments, attach.RelativePath())
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-16 00:05:15 -04:00
|
|
|
func migrateLFS(ctx context.Context, dstStorage storage.ObjectStorage) error {
|
2022-10-31 11:51:14 -04:00
|
|
|
return db.Iterate(ctx, nil, func(ctx context.Context, mo *git_model.LFSMetaObject) error {
|
2020-09-08 11:45:10 -04:00
|
|
|
_, err := storage.Copy(dstStorage, mo.RelativePath(), storage.LFS, mo.RelativePath())
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-16 00:05:15 -04:00
|
|
|
func migrateAvatars(ctx context.Context, dstStorage storage.ObjectStorage) error {
|
2022-10-31 11:51:14 -04:00
|
|
|
return db.Iterate(ctx, nil, func(ctx context.Context, user *user_model.User) error {
|
2024-01-20 10:27:31 -05:00
|
|
|
if user.CustomAvatarRelativePath() == "" {
|
|
|
|
return nil
|
|
|
|
}
|
2020-10-14 09:07:51 -04:00
|
|
|
_, err := storage.Copy(dstStorage, user.CustomAvatarRelativePath(), storage.Avatars, user.CustomAvatarRelativePath())
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-16 00:05:15 -04:00
|
|
|
func migrateRepoAvatars(ctx context.Context, dstStorage storage.ObjectStorage) error {
|
2022-10-31 11:51:14 -04:00
|
|
|
return db.Iterate(ctx, nil, func(ctx context.Context, repo *repo_model.Repository) error {
|
2024-01-20 10:27:31 -05:00
|
|
|
if repo.CustomAvatarRelativePath() == "" {
|
|
|
|
return nil
|
|
|
|
}
|
2020-10-14 09:07:51 -04:00
|
|
|
_, err := storage.Copy(dstStorage, repo.CustomAvatarRelativePath(), storage.RepoAvatars, repo.CustomAvatarRelativePath())
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-16 00:05:15 -04:00
|
|
|
func migrateRepoArchivers(ctx context.Context, dstStorage storage.ObjectStorage) error {
|
2022-10-31 11:51:14 -04:00
|
|
|
return db.Iterate(ctx, nil, func(ctx context.Context, archiver *repo_model.RepoArchiver) error {
|
2022-08-29 05:45:20 -04:00
|
|
|
p := archiver.RelativePath()
|
|
|
|
_, err := storage.Copy(dstStorage, p, storage.RepoArchives, p)
|
2022-08-16 00:05:15 -04:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func migratePackages(ctx context.Context, dstStorage storage.ObjectStorage) error {
|
2022-10-31 11:51:14 -04:00
|
|
|
return db.Iterate(ctx, nil, func(ctx context.Context, pb *packages_model.PackageBlob) error {
|
2022-08-16 00:05:15 -04:00
|
|
|
p := packages_module.KeyToRelativePath(packages_module.BlobHash256Key(pb.HashSHA256))
|
|
|
|
_, err := storage.Copy(dstStorage, p, storage.Packages, p)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-05-12 09:30:28 -04:00
|
|
|
func migrateActionsLog(ctx context.Context, dstStorage storage.ObjectStorage) error {
|
|
|
|
return db.Iterate(ctx, nil, func(ctx context.Context, task *actions_model.ActionTask) error {
|
|
|
|
if task.LogExpired {
|
|
|
|
// the log has been cleared
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if !task.LogInStorage {
|
|
|
|
// running tasks store logs in DBFS
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
p := task.LogFilename
|
|
|
|
_, err := storage.Copy(dstStorage, p, storage.Actions, p)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-05-07 02:45:30 -04:00
|
|
|
func migrateActionsArtifacts(ctx context.Context, dstStorage storage.ObjectStorage) error {
|
|
|
|
return db.Iterate(ctx, nil, func(ctx context.Context, artifact *actions_model.ActionArtifact) error {
|
2024-06-05 00:00:56 -04:00
|
|
|
if artifact.Status == int64(actions_model.ArtifactStatusExpired) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := storage.Copy(dstStorage, artifact.StoragePath, storage.ActionsArtifacts, artifact.StoragePath)
|
|
|
|
if err != nil {
|
|
|
|
// ignore files that do not exist
|
|
|
|
if errors.Is(err, fs.ErrNotExist) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2024-05-07 02:45:30 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-08-18 00:23:45 -04:00
|
|
|
func runMigrateStorage(ctx *cli.Context) error {
|
2021-11-06 23:11:27 -04:00
|
|
|
stdCtx, cancel := installSignals()
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
if err := initDB(stdCtx); err != nil {
|
2020-08-18 00:23:45 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-26 20:56:58 -04:00
|
|
|
log.Info("AppPath: %s", setting.AppPath)
|
|
|
|
log.Info("AppWorkPath: %s", setting.AppWorkPath)
|
|
|
|
log.Info("Custom path: %s", setting.CustomPath)
|
2023-02-19 11:12:01 -05:00
|
|
|
log.Info("Log path: %s", setting.Log.RootPath)
|
2021-09-13 21:24:57 -04:00
|
|
|
log.Info("Configuration file: %s", setting.CustomConf)
|
2020-08-18 00:23:45 -04:00
|
|
|
|
2021-10-30 10:32:11 -04:00
|
|
|
if err := db.InitEngineWithMigration(context.Background(), migrations.Migrate); err != nil {
|
2020-08-18 00:23:45 -04:00
|
|
|
log.Fatal("Failed to initialize ORM engine: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := storage.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-08 11:45:10 -04:00
|
|
|
var dstStorage storage.ObjectStorage
|
|
|
|
var err error
|
2020-09-29 05:05:13 -04:00
|
|
|
switch strings.ToLower(ctx.String("storage")) {
|
2020-10-12 23:58:34 -04:00
|
|
|
case "":
|
|
|
|
fallthrough
|
2023-06-13 23:42:38 -04:00
|
|
|
case string(setting.LocalStorageType):
|
2020-09-08 11:45:10 -04:00
|
|
|
p := ctx.String("path")
|
|
|
|
if p == "" {
|
2023-09-11 05:30:18 -04:00
|
|
|
log.Fatal("Path must be given when storage is local")
|
2020-09-08 11:45:10 -04:00
|
|
|
return nil
|
|
|
|
}
|
2020-10-12 23:58:34 -04:00
|
|
|
dstStorage, err = storage.NewLocalStorage(
|
2022-08-16 00:05:15 -04:00
|
|
|
stdCtx,
|
2023-06-13 23:42:38 -04:00
|
|
|
&setting.Storage{
|
2020-10-12 23:58:34 -04:00
|
|
|
Path: p,
|
|
|
|
})
|
2023-06-13 23:42:38 -04:00
|
|
|
case string(setting.MinioStorageType):
|
2020-09-08 11:45:10 -04:00
|
|
|
dstStorage, err = storage.NewMinioStorage(
|
2022-08-16 00:05:15 -04:00
|
|
|
stdCtx,
|
2023-06-13 23:42:38 -04:00
|
|
|
&setting.Storage{
|
|
|
|
MinioConfig: setting.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"),
|
|
|
|
InsecureSkipVerify: ctx.Bool("minio-insecure-skip-verify"),
|
|
|
|
ChecksumAlgorithm: ctx.String("minio-checksum-algorithm"),
|
2024-05-15 09:56:17 -04:00
|
|
|
BucketLookUpType: ctx.String("minio-bucket-lookup-type"),
|
2023-06-13 23:42:38 -04:00
|
|
|
},
|
2020-10-12 23:58:34 -04:00
|
|
|
})
|
2024-05-30 03:33:50 -04:00
|
|
|
case string(setting.AzureBlobStorageType):
|
|
|
|
dstStorage, err = storage.NewAzureBlobStorage(
|
|
|
|
stdCtx,
|
|
|
|
&setting.Storage{
|
|
|
|
AzureBlobConfig: setting.AzureBlobStorageConfig{
|
|
|
|
Endpoint: ctx.String("azureblob-endpoint"),
|
|
|
|
AccountName: ctx.String("azureblob-account-name"),
|
|
|
|
AccountKey: ctx.String("azureblob-account-key"),
|
|
|
|
Container: ctx.String("azureblob-container"),
|
|
|
|
BasePath: ctx.String("azureblob-base-path"),
|
|
|
|
},
|
|
|
|
})
|
2020-09-08 11:45:10 -04:00
|
|
|
default:
|
2022-08-16 00:05:15 -04:00
|
|
|
return fmt.Errorf("unsupported storage type: %s", ctx.String("storage"))
|
2020-09-08 11:45:10 -04:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-16 00:05:15 -04:00
|
|
|
migratedMethods := map[string]func(context.Context, storage.ObjectStorage) error{
|
2024-05-07 02:45:30 -04:00
|
|
|
"attachments": migrateAttachments,
|
|
|
|
"lfs": migrateLFS,
|
|
|
|
"avatars": migrateAvatars,
|
|
|
|
"repo-avatars": migrateRepoAvatars,
|
|
|
|
"repo-archivers": migrateRepoArchivers,
|
|
|
|
"packages": migratePackages,
|
|
|
|
"actions-log": migrateActionsLog,
|
|
|
|
"actions-artifacts": migrateActionsArtifacts,
|
2022-08-16 00:05:15 -04:00
|
|
|
}
|
|
|
|
|
2020-09-29 05:05:13 -04:00
|
|
|
tp := strings.ToLower(ctx.String("type"))
|
2022-08-16 00:05:15 -04:00
|
|
|
if m, ok := migratedMethods[tp]; ok {
|
|
|
|
if err := m(stdCtx, dstStorage); err != nil {
|
2020-10-14 09:07:51 -04:00
|
|
|
return err
|
|
|
|
}
|
2022-08-16 00:05:15 -04:00
|
|
|
log.Info("%s files have successfully been copied to the new storage.", tp)
|
|
|
|
return nil
|
2020-08-18 00:23:45 -04:00
|
|
|
}
|
|
|
|
|
2022-08-16 00:05:15 -04:00
|
|
|
return fmt.Errorf("unsupported storage: %s", ctx.String("type"))
|
2020-08-18 00:23:45 -04:00
|
|
|
}
|