2019-10-13 18:53:14 +05:30
|
|
|
// Copyright 2019 Gitea. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2019-10-13 18:53:14 +05:30
|
|
|
|
|
|
|
package task
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2022-08-25 08:01:57 +05:30
|
|
|
admin_model "code.gitea.io/gitea/models/admin"
|
2021-12-10 06:57:50 +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-01-07 16:53:09 +05:30
|
|
|
"code.gitea.io/gitea/modules/graceful"
|
2021-07-24 21:33:58 +05:30
|
|
|
"code.gitea.io/gitea/modules/json"
|
2019-10-13 18:53:14 +05:30
|
|
|
"code.gitea.io/gitea/modules/log"
|
2021-11-16 20:55:33 +05:30
|
|
|
base "code.gitea.io/gitea/modules/migration"
|
2020-01-07 16:53:09 +05:30
|
|
|
"code.gitea.io/gitea/modules/queue"
|
2020-01-12 17:41:17 +05:30
|
|
|
repo_module "code.gitea.io/gitea/modules/repository"
|
2021-05-31 13:55:47 +05:30
|
|
|
"code.gitea.io/gitea/modules/secret"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2019-10-13 18:53:14 +05:30
|
|
|
"code.gitea.io/gitea/modules/structs"
|
2020-01-12 17:41:17 +05:30
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
2021-05-31 13:55:47 +05:30
|
|
|
"code.gitea.io/gitea/modules/util"
|
2019-10-13 18:53:14 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// taskQueue is a global queue of tasks
|
2020-01-07 16:53:09 +05:30
|
|
|
var taskQueue queue.Queue
|
2019-10-13 18:53:14 +05:30
|
|
|
|
|
|
|
// Run a task
|
2022-08-25 08:01:57 +05:30
|
|
|
func Run(t *admin_model.Task) error {
|
2019-10-13 18:53:14 +05:30
|
|
|
switch t.Type {
|
|
|
|
case structs.TaskTypeMigrateRepo:
|
|
|
|
return runMigrateTask(t)
|
|
|
|
default:
|
2020-01-07 16:53:09 +05:30
|
|
|
return fmt.Errorf("Unknown task type: %d", t.Type)
|
2019-10-13 18:53:14 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init will start the service to get all unfinished tasks and run them
|
|
|
|
func Init() error {
|
2022-08-25 08:01:57 +05:30
|
|
|
taskQueue = queue.CreateQueue("task", handle, &admin_model.Task{})
|
2020-01-07 16:53:09 +05:30
|
|
|
|
|
|
|
if taskQueue == nil {
|
|
|
|
return fmt.Errorf("Unable to create Task Queue")
|
2019-10-13 18:53:14 +05:30
|
|
|
}
|
|
|
|
|
2020-01-07 16:53:09 +05:30
|
|
|
go graceful.GetManager().RunWithShutdownFns(taskQueue.Run)
|
2019-10-13 18:53:14 +05:30
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-23 02:52:14 +05:30
|
|
|
func handle(data ...queue.Data) []queue.Data {
|
2020-01-07 16:53:09 +05:30
|
|
|
for _, datum := range data {
|
2022-08-25 08:01:57 +05:30
|
|
|
task := datum.(*admin_model.Task)
|
2020-01-07 16:53:09 +05:30
|
|
|
if err := Run(task); err != nil {
|
|
|
|
log.Error("Run task failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2022-01-23 02:52:14 +05:30
|
|
|
return nil
|
2020-01-07 16:53:09 +05:30
|
|
|
}
|
|
|
|
|
2019-10-13 18:53:14 +05:30
|
|
|
// MigrateRepository add migration repository to task
|
2021-11-24 15:19:20 +05:30
|
|
|
func MigrateRepository(doer, u *user_model.User, opts base.MigrateOptions) error {
|
2020-01-12 17:41:17 +05:30
|
|
|
task, err := CreateMigrateTask(doer, u, opts)
|
2019-10-13 18:53:14 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return taskQueue.Push(task)
|
|
|
|
}
|
2020-01-12 17:41:17 +05:30
|
|
|
|
|
|
|
// CreateMigrateTask creates a migrate task
|
2022-08-25 08:01:57 +05:30
|
|
|
func CreateMigrateTask(doer, u *user_model.User, opts base.MigrateOptions) (*admin_model.Task, error) {
|
2021-05-31 13:55:47 +05:30
|
|
|
// encrypt credentials for persistence
|
|
|
|
var err error
|
|
|
|
opts.CloneAddrEncrypted, err = secret.EncryptSecret(setting.SecretKey, opts.CloneAddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-03-31 07:55:40 +05:30
|
|
|
opts.CloneAddr = util.SanitizeCredentialURLs(opts.CloneAddr)
|
2021-05-31 13:55:47 +05:30
|
|
|
opts.AuthPasswordEncrypted, err = secret.EncryptSecret(setting.SecretKey, opts.AuthPassword)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
opts.AuthPassword = ""
|
|
|
|
opts.AuthTokenEncrypted, err = secret.EncryptSecret(setting.SecretKey, opts.AuthToken)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
opts.AuthToken = ""
|
2020-01-12 17:41:17 +05:30
|
|
|
bs, err := json.Marshal(&opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-08-25 08:01:57 +05:30
|
|
|
task := &admin_model.Task{
|
2020-01-12 17:41:17 +05:30
|
|
|
DoerID: doer.ID,
|
|
|
|
OwnerID: u.ID,
|
|
|
|
Type: structs.TaskTypeMigrateRepo,
|
|
|
|
Status: structs.TaskStatusQueue,
|
|
|
|
PayloadContent: string(bs),
|
|
|
|
}
|
|
|
|
|
2022-08-25 08:01:57 +05:30
|
|
|
if err := admin_model.CreateTask(task); err != nil {
|
2020-01-12 17:41:17 +05:30
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-08-25 08:01:57 +05:30
|
|
|
repo, err := repo_module.CreateRepository(doer, u, repo_module.CreateRepoOptions{
|
2020-01-12 17:41:17 +05:30
|
|
|
Name: opts.RepoName,
|
|
|
|
Description: opts.Description,
|
|
|
|
OriginalURL: opts.OriginalURL,
|
|
|
|
GitServiceType: opts.GitServiceType,
|
|
|
|
IsPrivate: opts.Private,
|
|
|
|
IsMirror: opts.Mirror,
|
2021-12-10 06:57:50 +05:30
|
|
|
Status: repo_model.RepositoryBeingMigrated,
|
2020-01-12 17:41:17 +05:30
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
task.EndTime = timeutil.TimeStampNow()
|
|
|
|
task.Status = structs.TaskStatusFailed
|
|
|
|
err2 := task.UpdateCols("end_time", "status")
|
|
|
|
if err2 != nil {
|
|
|
|
log.Error("UpdateCols Failed: %v", err2.Error())
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
task.RepoID = repo.ID
|
|
|
|
if err = task.UpdateCols("repo_id"); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-11-13 16:58:50 +05:30
|
|
|
return task, nil
|
2020-01-12 17:41:17 +05:30
|
|
|
}
|