|
|
@@ -3,7 +3,7 @@ package cloudbrainTask |
|
|
|
import ( |
|
|
|
"fmt" |
|
|
|
"net/http" |
|
|
|
"os" |
|
|
|
"path" |
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/modelarts" |
|
|
|
"code.gitea.io/gitea/modules/modelarts_cd" |
|
|
@@ -28,11 +28,25 @@ import ( |
|
|
|
"code.gitea.io/gitea/modules/util" |
|
|
|
) |
|
|
|
|
|
|
|
const NoteBookExtension = ".ipynb" |
|
|
|
|
|
|
|
func FileNotebookCreate(ctx *context.Context, option api.CreateFileNotebookJobOption) { |
|
|
|
|
|
|
|
if ctx.Written() { |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
if path.Ext(option.File) != NoteBookExtension { |
|
|
|
ctx.JSON(http.StatusOK, models.BaseErrorMessageApi(ctx.Tr("repo.notebook_select_wrong"))) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
isNotebookFileExist, _ := isNoteBookFileExist(ctx, option) |
|
|
|
if !isNotebookFileExist { |
|
|
|
ctx.JSON(http.StatusOK, models.BaseErrorMessageApi(ctx.Tr("repo.notebook_file_not_exist"))) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
//create repo if not exist |
|
|
|
repo, err := models.GetRepositoryByName(ctx.User.ID, setting.FileNoteBook.ProjectName) |
|
|
|
if repo == nil { |
|
|
@@ -51,6 +65,12 @@ func FileNotebookCreate(ctx *context.Context, option api.CreateFileNotebookJobOp |
|
|
|
} |
|
|
|
if err != nil { |
|
|
|
ctx.JSON(http.StatusOK, models.BaseErrorMessageApi("repo.failed_to_create_repo")) |
|
|
|
return |
|
|
|
} |
|
|
|
if option.Type <= 1 { |
|
|
|
cloudBrainFileNoteBookCreate(ctx, option, repo) |
|
|
|
} else { |
|
|
|
modelartsFileNoteBookCreate(ctx, option, repo) |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
@@ -99,40 +119,19 @@ func cloudBrainFileNoteBookCreate(ctx *context.Context, option api.CreateFileNot |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
repoPath := models.RepoPath(repo.OwnerName, repo.Name) |
|
|
|
gitRepo, err := git.OpenRepository(repoPath) |
|
|
|
if err != nil { |
|
|
|
ctx.Error(500, "RepoRef Invalid repo "+repoPath, err.Error()) |
|
|
|
return |
|
|
|
} |
|
|
|
// We opened it, we should close it |
|
|
|
defer func() { |
|
|
|
// If it's been set to nil then assume someone else has closed it. |
|
|
|
if ctx.Repo.GitRepo != nil { |
|
|
|
ctx.Repo.GitRepo.Close() |
|
|
|
} |
|
|
|
}() |
|
|
|
|
|
|
|
ctx.Repo = &context.Repository{ |
|
|
|
Repository: repo, |
|
|
|
GitRepo: gitRepo, |
|
|
|
} |
|
|
|
|
|
|
|
fileExist, err := ctx.Repo.FileExists(option.File, option.BranchName) |
|
|
|
if err != nil || !fileExist { |
|
|
|
log.Error("Get file error:", err, ctx.Data["MsgID"]) |
|
|
|
sourceRepo, err := models.GetRepositoryByOwnerAndName(option.OwnerName, option.ProjectName) |
|
|
|
if err != nil { |
|
|
|
ctx.JSON(http.StatusOK, models.BaseErrorMessageApi(ctx.Tr("repo.notebook_file_not_exist"))) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
command := cloudbrain.GetCloudbrainDebugCommand() |
|
|
|
|
|
|
|
errStr := uploadCodeFile(repo, codePath, option.BranchName, option.File, jobName) |
|
|
|
errStr := uploadCodeFile(sourceRepo, codePath, option.BranchName, option.File, jobName) |
|
|
|
if errStr != "" { |
|
|
|
ctx.JSON(http.StatusOK, models.BaseErrorMessageApi(ctx.Tr("repo.notebook_file_not_exist"))) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
command := cloudbrain.GetCloudbrainDebugCommand() |
|
|
|
commitID, _ := ctx.Repo.GitRepo.GetBranchCommitID(option.BranchName) |
|
|
|
specId := setting.FileNoteBook.SpecIdGPU |
|
|
|
if option.Type == 0 { |
|
|
@@ -242,33 +241,14 @@ func modelartsFileNoteBookCreate(ctx *context.Context, option api.CreateFileNote |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
repoPath := models.RepoPath(repo.OwnerName, repo.Name) |
|
|
|
gitRepo, err := git.OpenRepository(repoPath) |
|
|
|
if err != nil { |
|
|
|
ctx.Error(500, "RepoRef Invalid repo "+repoPath, err.Error()) |
|
|
|
return |
|
|
|
} |
|
|
|
// We opened it, we should close it |
|
|
|
defer func() { |
|
|
|
// If it's been set to nil then assume someone else has closed it. |
|
|
|
if ctx.Repo.GitRepo != nil { |
|
|
|
ctx.Repo.GitRepo.Close() |
|
|
|
} |
|
|
|
}() |
|
|
|
|
|
|
|
ctx.Repo = &context.Repository{ |
|
|
|
Repository: repo, |
|
|
|
GitRepo: gitRepo, |
|
|
|
} |
|
|
|
|
|
|
|
fileExist, err := ctx.Repo.FileExists(option.File, option.BranchName) |
|
|
|
if err != nil || !fileExist { |
|
|
|
log.Error("Get file error:", err, ctx.Data["MsgID"]) |
|
|
|
sourceRepo, err := models.GetRepositoryByOwnerAndName(option.OwnerName, option.ProjectName) |
|
|
|
if err != nil { |
|
|
|
ctx.JSON(http.StatusOK, models.BaseErrorMessageApi(ctx.Tr("repo.notebook_file_not_exist"))) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
err = downloadCode(repo, getCodePath(jobName), option.BranchName) |
|
|
|
err = downloadCode(sourceRepo, getCodePath(jobName), option.BranchName) |
|
|
|
if err != nil { |
|
|
|
ctx.JSON(http.StatusOK, models.BaseErrorMessageApi(ctx.Tr("cloudbrain.load_code_failed"))) |
|
|
|
return |
|
|
@@ -292,6 +272,9 @@ func modelartsFileNoteBookCreate(ctx *context.Context, option api.CreateFileNote |
|
|
|
ctx.JSON(http.StatusOK, models.BaseErrorMessageApi(ctx.Tr("points.insufficient_points_balance"))) |
|
|
|
return |
|
|
|
} |
|
|
|
ctx.Repo = &context.Repository{ |
|
|
|
Repository: repo, |
|
|
|
} |
|
|
|
|
|
|
|
var jobId string |
|
|
|
if setting.ModelartsCD.Enabled { |
|
|
@@ -315,6 +298,30 @@ func modelartsFileNoteBookCreate(ctx *context.Context, option api.CreateFileNote |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
func isNoteBookFileExist(ctx *context.Context, option api.CreateFileNotebookJobOption) (bool, error) { |
|
|
|
repoPathOfNoteBook := models.RepoPath(option.OwnerName, option.ProjectName) |
|
|
|
|
|
|
|
gitRepoOfNoteBook, err := git.OpenRepository(repoPathOfNoteBook) |
|
|
|
if err != nil { |
|
|
|
log.Error("RepoRef Invalid repo "+repoPathOfNoteBook, err.Error()) |
|
|
|
return false, err |
|
|
|
} |
|
|
|
// We opened it, we should close it |
|
|
|
defer func() { |
|
|
|
// If it's been set to nil then assume someone else has closed it. |
|
|
|
if gitRepoOfNoteBook != nil { |
|
|
|
gitRepoOfNoteBook.Close() |
|
|
|
} |
|
|
|
}() |
|
|
|
fileExist, err := fileExists(gitRepoOfNoteBook, option.File, option.BranchName) |
|
|
|
if err != nil || !fileExist { |
|
|
|
log.Error("Get file error:", err, ctx.Data["MsgID"]) |
|
|
|
|
|
|
|
return false, err |
|
|
|
} |
|
|
|
return true, nil |
|
|
|
} |
|
|
|
|
|
|
|
func uploadCodeFile(repo *models.Repository, codePath string, branchName string, filePath string, jobName string) string { |
|
|
|
err := downloadCode(repo, codePath, branchName) |
|
|
|
if err != nil { |
|
|
@@ -325,6 +332,17 @@ func uploadCodeFile(repo *models.Repository, codePath string, branchName string, |
|
|
|
if err != nil { |
|
|
|
return "cloudbrain.load_code_failed" |
|
|
|
} |
|
|
|
go os.RemoveAll(codePath) |
|
|
|
return "" |
|
|
|
} |
|
|
|
|
|
|
|
func fileExists(gitRepo *git.Repository, path string, branch string) (bool, error) { |
|
|
|
|
|
|
|
commit, err := gitRepo.GetBranchCommit(branch) |
|
|
|
if err != nil { |
|
|
|
return false, err |
|
|
|
} |
|
|
|
if _, err := commit.GetTreeEntryByPath(path); err != nil { |
|
|
|
return false, err |
|
|
|
} |
|
|
|
return true, nil |
|
|
|
} |