code.gitea.io/gitea@v1.21.7/services/task/migrate.go (about) 1 // Copyright 2019 Gitea. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package task 5 6 import ( 7 "context" 8 "errors" 9 "fmt" 10 "strings" 11 "time" 12 13 admin_model "code.gitea.io/gitea/models/admin" 14 "code.gitea.io/gitea/models/db" 15 repo_model "code.gitea.io/gitea/models/repo" 16 user_model "code.gitea.io/gitea/models/user" 17 "code.gitea.io/gitea/modules/graceful" 18 "code.gitea.io/gitea/modules/json" 19 "code.gitea.io/gitea/modules/log" 20 "code.gitea.io/gitea/modules/migration" 21 "code.gitea.io/gitea/modules/process" 22 "code.gitea.io/gitea/modules/structs" 23 "code.gitea.io/gitea/modules/timeutil" 24 "code.gitea.io/gitea/modules/util" 25 "code.gitea.io/gitea/services/migrations" 26 notify_service "code.gitea.io/gitea/services/notify" 27 ) 28 29 func handleCreateError(owner *user_model.User, err error) error { 30 switch { 31 case repo_model.IsErrReachLimitOfRepo(err): 32 return fmt.Errorf("you have already reached your limit of %d repositories", owner.MaxCreationLimit()) 33 case repo_model.IsErrRepoAlreadyExist(err): 34 return errors.New("the repository name is already used") 35 case db.IsErrNameReserved(err): 36 return fmt.Errorf("the repository name '%s' is reserved", err.(db.ErrNameReserved).Name) 37 case db.IsErrNamePatternNotAllowed(err): 38 return fmt.Errorf("the pattern '%s' is not allowed in a repository name", err.(db.ErrNamePatternNotAllowed).Pattern) 39 default: 40 return err 41 } 42 } 43 44 func runMigrateTask(ctx context.Context, t *admin_model.Task) (err error) { 45 defer func(ctx context.Context) { 46 if e := recover(); e != nil { 47 err = fmt.Errorf("PANIC whilst trying to do migrate task: %v", e) 48 log.Critical("PANIC during runMigrateTask[%d] by DoerID[%d] to RepoID[%d] for OwnerID[%d]: %v\nStacktrace: %v", t.ID, t.DoerID, t.RepoID, t.OwnerID, e, log.Stack(2)) 49 } 50 51 if err == nil { 52 err = admin_model.FinishMigrateTask(ctx, t) 53 if err == nil { 54 notify_service.MigrateRepository(ctx, t.Doer, t.Owner, t.Repo) 55 return 56 } 57 58 log.Error("FinishMigrateTask[%d] by DoerID[%d] to RepoID[%d] for OwnerID[%d] failed: %v", t.ID, t.DoerID, t.RepoID, t.OwnerID, err) 59 } 60 61 log.Error("runMigrateTask[%d] by DoerID[%d] to RepoID[%d] for OwnerID[%d] failed: %v", t.ID, t.DoerID, t.RepoID, t.OwnerID, err) 62 63 t.EndTime = timeutil.TimeStampNow() 64 t.Status = structs.TaskStatusFailed 65 t.Message = err.Error() 66 67 if err := t.UpdateCols(ctx, "status", "message", "end_time"); err != nil { 68 log.Error("Task UpdateCols failed: %v", err) 69 } 70 71 // then, do not delete the repository, otherwise the users won't be able to see the last error 72 }(graceful.GetManager().ShutdownContext()) // even if the parent ctx is canceled, this defer-function still needs to update the task record in database 73 74 if err = t.LoadRepo(ctx); err != nil { 75 return err 76 } 77 78 // if repository is ready, then just finish the task 79 if t.Repo.Status == repo_model.RepositoryReady { 80 return nil 81 } 82 83 if err = t.LoadDoer(ctx); err != nil { 84 return err 85 } 86 if err = t.LoadOwner(ctx); err != nil { 87 return err 88 } 89 90 var opts *migration.MigrateOptions 91 opts, err = t.MigrateConfig() 92 if err != nil { 93 return err 94 } 95 96 opts.MigrateToRepoID = t.RepoID 97 98 pm := process.GetManager() 99 ctx, cancel, finished := pm.AddContext(graceful.GetManager().ShutdownContext(), fmt.Sprintf("MigrateTask: %s/%s", t.Owner.Name, opts.RepoName)) 100 defer finished() 101 102 t.StartTime = timeutil.TimeStampNow() 103 t.Status = structs.TaskStatusRunning 104 if err = t.UpdateCols(ctx, "start_time", "status"); err != nil { 105 return err 106 } 107 108 // check whether the task should be canceled, this goroutine is also managed by process manager 109 go func() { 110 for { 111 select { 112 case <-time.After(2 * time.Second): 113 case <-ctx.Done(): 114 return 115 } 116 task, _ := admin_model.GetMigratingTask(ctx, t.RepoID) 117 if task != nil && task.Status != structs.TaskStatusRunning { 118 log.Debug("MigrateTask[%d] by DoerID[%d] to RepoID[%d] for OwnerID[%d] is canceled due to status is not 'running'", t.ID, t.DoerID, t.RepoID, t.OwnerID) 119 cancel() 120 return 121 } 122 } 123 }() 124 125 t.Repo, err = migrations.MigrateRepository(ctx, t.Doer, t.Owner.Name, *opts, func(format string, args ...any) { 126 message := admin_model.TranslatableMessage{ 127 Format: format, 128 Args: args, 129 } 130 bs, _ := json.Marshal(message) 131 t.Message = string(bs) 132 _ = t.UpdateCols(ctx, "message") 133 }) 134 135 if err == nil { 136 log.Trace("Repository migrated [%d]: %s/%s", t.Repo.ID, t.Owner.Name, t.Repo.Name) 137 return nil 138 } 139 140 if repo_model.IsErrRepoAlreadyExist(err) { 141 return errors.New("the repository name is already used") 142 } 143 144 // remoteAddr may contain credentials, so we sanitize it 145 err = util.SanitizeErrorCredentialURLs(err) 146 if strings.Contains(err.Error(), "Authentication failed") || 147 strings.Contains(err.Error(), "could not read Username") { 148 return fmt.Errorf("authentication failed: %w", err) 149 } else if strings.Contains(err.Error(), "fatal:") { 150 return fmt.Errorf("migration failed: %w", err) 151 } 152 153 // do not be tempted to coalesce this line with the return 154 err = handleCreateError(t.Owner, err) 155 return err 156 }