aboutsummaryrefslogtreecommitdiffstats
path: root/modules/task/migrate.go
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2019-10-13 21:23:14 +0800
committerGitHub <noreply@github.com>2019-10-13 21:23:14 +0800
commitf2a3abc683ad4b2177b7c7c6160a2c0b4316120a (patch)
tree3b92f34b9bb9a015072f511dc5cf6340af18eda5 /modules/task/migrate.go
parent0a96e59884ca5c4fedc8c3d166d97f35b245ad6e (diff)
downloadgitea-f2a3abc683ad4b2177b7c7c6160a2c0b4316120a.tar.gz
gitea-f2a3abc683ad4b2177b7c7c6160a2c0b4316120a.zip
Move migrating repository from frontend to backend (#6200)
* move migrating to backend * add loading image when migrating and fix tests * fix format * fix lint * add redis task queue support and improve docs * add redis vendor * fix vet * add database migrations and fix app.ini sample * add comments for task section on app.ini.sample * Update models/migrations/v84.go Co-Authored-By: lunny <xiaolunwen@gmail.com> * Update models/repo.go Co-Authored-By: lunny <xiaolunwen@gmail.com> * move migrating to backend * add loading image when migrating and fix tests * fix fmt * add redis task queue support and improve docs * fix fixtures * fix fixtures * fix duplicate function on index.js * fix tests * rename repository statuses * check if repository is being create when SSH request * fix lint * fix template * some improvements * fix template * unified migrate options * fix lint * fix loading page * refactor * When gitea restart, don't restart the running tasks because we may have servel gitea instances, that may break the migration * fix js * Update models/repo.go Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Update docs/content/doc/advanced/config-cheat-sheet.en-us.md Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * fix tests * rename ErrTaskIsNotExist to ErrTaskDoesNotExist * delete release after add one on tests to make it run happy * fix tests * fix tests * improve codes * fix lint * fix lint * fix migrations
Diffstat (limited to 'modules/task/migrate.go')
-rw-r--r--modules/task/migrate.go120
1 files changed, 120 insertions, 0 deletions
diff --git a/modules/task/migrate.go b/modules/task/migrate.go
new file mode 100644
index 0000000000..5d15a506d7
--- /dev/null
+++ b/modules/task/migrate.go
@@ -0,0 +1,120 @@
+// Copyright 2019 Gitea. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package task
+
+import (
+ "bytes"
+ "errors"
+ "fmt"
+ "strings"
+
+ "code.gitea.io/gitea/models"
+ "code.gitea.io/gitea/modules/log"
+ "code.gitea.io/gitea/modules/migrations"
+ "code.gitea.io/gitea/modules/notification"
+ "code.gitea.io/gitea/modules/structs"
+ "code.gitea.io/gitea/modules/timeutil"
+ "code.gitea.io/gitea/modules/util"
+)
+
+func handleCreateError(owner *models.User, err error, name string) error {
+ switch {
+ case models.IsErrReachLimitOfRepo(err):
+ return fmt.Errorf("You have already reached your limit of %d repositories", owner.MaxCreationLimit())
+ case models.IsErrRepoAlreadyExist(err):
+ return errors.New("The repository name is already used")
+ case models.IsErrNameReserved(err):
+ return fmt.Errorf("The repository name '%s' is reserved", err.(models.ErrNameReserved).Name)
+ case models.IsErrNamePatternNotAllowed(err):
+ return fmt.Errorf("The pattern '%s' is not allowed in a repository name", err.(models.ErrNamePatternNotAllowed).Pattern)
+ default:
+ return err
+ }
+}
+
+func runMigrateTask(t *models.Task) (err error) {
+ defer func() {
+ if e := recover(); e != nil {
+ var buf bytes.Buffer
+ fmt.Fprintf(&buf, "Handler crashed with error: %v", log.Stack(2))
+
+ err = errors.New(buf.String())
+ }
+
+ if err == nil {
+ err = models.FinishMigrateTask(t)
+ if err == nil {
+ notification.NotifyMigrateRepository(t.Doer, t.Owner, t.Repo)
+ return
+ }
+
+ log.Error("FinishMigrateTask failed: %s", err.Error())
+ }
+
+ t.EndTime = timeutil.TimeStampNow()
+ t.Status = structs.TaskStatusFailed
+ t.Errors = err.Error()
+ if err := t.UpdateCols("status", "errors", "end_time"); err != nil {
+ log.Error("Task UpdateCols failed: %s", err.Error())
+ }
+
+ if t.Repo != nil {
+ if errDelete := models.DeleteRepository(t.Doer, t.OwnerID, t.Repo.ID); errDelete != nil {
+ log.Error("DeleteRepository: %v", errDelete)
+ }
+ }
+ }()
+
+ if err := t.LoadRepo(); err != nil {
+ return err
+ }
+
+ // if repository is ready, then just finsih the task
+ if t.Repo.Status == models.RepositoryReady {
+ return nil
+ }
+
+ if err := t.LoadDoer(); err != nil {
+ return err
+ }
+ if err := t.LoadOwner(); err != nil {
+ return err
+ }
+ t.StartTime = timeutil.TimeStampNow()
+ t.Status = structs.TaskStatusRunning
+ if err := t.UpdateCols("start_time", "status"); err != nil {
+ return err
+ }
+
+ var opts *structs.MigrateRepoOption
+ opts, err = t.MigrateConfig()
+ if err != nil {
+ return err
+ }
+
+ opts.MigrateToRepoID = t.RepoID
+ repo, err := migrations.MigrateRepository(t.Doer, t.Owner.Name, *opts)
+ if err == nil {
+ notification.NotifyMigrateRepository(t.Doer, t.Owner, repo)
+
+ log.Trace("Repository migrated [%d]: %s/%s", repo.ID, t.Owner.Name, repo.Name)
+ return nil
+ }
+
+ if models.IsErrRepoAlreadyExist(err) {
+ return errors.New("The repository name is already used")
+ }
+
+ // remoteAddr may contain credentials, so we sanitize it
+ err = util.URLSanitizedError(err, opts.CloneAddr)
+ if strings.Contains(err.Error(), "Authentication failed") ||
+ strings.Contains(err.Error(), "could not read Username") {
+ return fmt.Errorf("Authentication failed: %v", err.Error())
+ } else if strings.Contains(err.Error(), "fatal:") {
+ return fmt.Errorf("Migration failed: %v", err.Error())
+ }
+
+ return handleCreateError(t.Owner, err, "MigratePost")
+}