diff --git a/models/attachment.go b/models/attachment.go index 3ba4112dc..063230e57 100755 --- a/models/attachment.go +++ b/models/attachment.go @@ -384,6 +384,7 @@ func getPrivateAttachments(e Engine, userID int64) ([]*AttachmentUsername, error return attachments, nil } +/* func GetAllUserAttachments(userID int64) ([]*AttachmentUsername, error) { attachsPub, err := getAllPublicAttachments(x) if err != nil { @@ -400,3 +401,17 @@ func GetAllUserAttachments(userID int64) ([]*AttachmentUsername, error) { return append(attachsPub, attachsPri...), nil } + */ + +func getAllUserAttachments(e Engine, userID int64) ([]*AttachmentUsername, error) { + attachments := make([]*AttachmentUsername, 0, 10) + if err := e.Table("attachment").Join("LEFT", "`user`", "attachment.uploader_id " + + "= `user`.id").Where("decompress_state= ? and (uploader_id= ? or is_private = ?)", DecompressStateDone, userID, false).Find(&attachments); err != nil { + return nil, err + } + return attachments, nil +} + +func GetAllUserAttachments(userID int64) ([]*AttachmentUsername, error) { + return getAllUserAttachments(x, userID) +} diff --git a/models/cloudbrain.go b/models/cloudbrain.go index 44b272c15..7244bc3ab 100755 --- a/models/cloudbrain.go +++ b/models/cloudbrain.go @@ -13,6 +13,7 @@ import ( ) type CloudbrainStatus string +type JobType string const ( JobWaiting CloudbrainStatus = "WAITING" @@ -20,11 +21,15 @@ const ( JobSucceeded CloudbrainStatus = "SUCCEEDED" JobFailed CloudbrainStatus = "FAILED" JobRunning CloudbrainStatus = "RUNNING" + + JobTypeDebug JobType = "DEBUG" + JobTypeBenchmark JobType = "BENCHMARK" ) type Cloudbrain struct { ID int64 `xorm:"pk autoincr"` JobID string `xorm:"INDEX NOT NULL"` + JobType string `xorm:"INDEX NOT NULL DEFAULT 'DEBUG'"` JobName string `xorm:"INDEX"` Status string `xorm:"INDEX"` UserID int64 `xorm:"INDEX"` @@ -131,6 +136,13 @@ type TaskPod struct { } `json:"taskStatuses"` } + +type TaskInfo struct { + Username string `json:"username"` + TaskName string `json:"task_name"` + CodeName string `json:"code_name"` +} + func ConvertToTaskPod(input map[string]interface{}) (TaskPod, error) { data, _ := json.Marshal(input) var taskPod TaskPod diff --git a/modules/auth/cloudbrain.go b/modules/auth/cloudbrain.go index b7b59ce63..ce2733afb 100755 --- a/modules/auth/cloudbrain.go +++ b/modules/auth/cloudbrain.go @@ -11,6 +11,7 @@ type CreateCloudBrainForm struct { Image string `form:"image" binding:"Required"` Command string `form:"command" binding:"Required"` Attachment string `form:"attachment" binding:"Required"` + JobType string `form:"job_type" binding:"Required"` } type CommitImageCloudBrainForm struct { diff --git a/modules/cloudbrain/cloudbrain.go b/modules/cloudbrain/cloudbrain.go index 52057f0b8..dccbee5c6 100755 --- a/modules/cloudbrain/cloudbrain.go +++ b/modules/cloudbrain/cloudbrain.go @@ -14,13 +14,15 @@ const ( CodeMountPath = "/code" DataSetMountPath = "/dataset" ModelMountPath = "/model" + BenchMarkMountPath = "/benchmark" + TaskInfoName = "/taskInfo" SubTaskName = "task1" Success = "S000" ) -func GenerateTask(ctx *context.Context, jobName, image, command, uuid, codePath, modelPath string) error { +func GenerateTask(ctx *context.Context, jobName, image, command, uuid, codePath, modelPath, benchmarkPath, jobType string) error { dataActualPath := setting.Attachment.Minio.RealPath + setting.Attachment.Minio.Bucket + "/" + setting.Attachment.Minio.BasePath + @@ -69,6 +71,13 @@ func GenerateTask(ctx *context.Context, jobName, image, command, uuid, codePath, ReadOnly: false, }, }, + { + HostPath: models.StHostPath{ + Path: benchmarkPath, + MountPath: BenchMarkMountPath, + ReadOnly: true, + }, + }, }, }) if err != nil { @@ -88,6 +97,7 @@ func GenerateTask(ctx *context.Context, jobName, image, command, uuid, codePath, JobID: jobID, JobName: jobName, SubTaskName: SubTaskName, + JobType: jobType, }) if err != nil { diff --git a/modules/setting/setting.go b/modules/setting/setting.go index 3fa6403a1..a71617e8d 100755 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -438,6 +438,11 @@ var ( JobPath string JobType string DebugServerHost string + + //benchmark config + IsBenchmarkEnabled bool + BenchmarkCode string + BenchmarkServerHost string ) // DateLang transforms standard language locale name to corresponding value in datetime plugin. @@ -1113,6 +1118,11 @@ func NewContext() { JobPath = sec.Key("JOB_PATH").MustString("/datasets/minio/data/opendata/jobs/") DebugServerHost = sec.Key("DEBUG_SERVER_HOST").MustString("http://192.168.202.73") JobType = sec.Key("JOB_TYPE").MustString("debug_openi") + + sec = Cfg.Section("benchmark") + IsBenchmarkEnabled = sec.Key("ENABLED").MustBool(false) + BenchmarkCode = sec.Key("BENCHMARKCODE").MustString("https://yangzhx:justfortest123@git.openi.org.cn/yangzhx/detection_benchmark_script.git") + BenchmarkServerHost = sec.Key("HOST").MustString("http://192.168.202.90:3366/") } func loadInternalToken(sec *ini.Section) string { diff --git a/routers/repo/cloudbrain.go b/routers/repo/cloudbrain.go index 3300bb669..98c6457e4 100755 --- a/routers/repo/cloudbrain.go +++ b/routers/repo/cloudbrain.go @@ -2,8 +2,10 @@ package repo import ( "code.gitea.io/gitea/modules/git" + "encoding/json" "errors" "os" + "os/exec" "strconv" "strings" "time" @@ -109,6 +111,8 @@ func CloudBrainNew(ctx *context.Context) { ctx.Data["code_path"] = cloudbrain.CodeMountPath ctx.Data["dataset_path"] = cloudbrain.DataSetMountPath ctx.Data["model_path"] = cloudbrain.ModelMountPath + ctx.Data["benchmark_path"] = cloudbrain.BenchMarkMountPath + ctx.Data["is_benchmark_enabled"] = setting.IsBenchmarkEnabled ctx.HTML(200, tplCloudBrainNew) } @@ -118,18 +122,24 @@ func CloudBrainCreate(ctx *context.Context, form auth.CreateCloudBrainForm) { image := form.Image command := form.Command uuid := form.Attachment + jobType := form.JobType codePath := setting.JobPath + jobName + cloudbrain.CodeMountPath repo := ctx.Repo.Repository downloadCode(repo, codePath) - modelPath := setting.JobPath + jobName + "/model" + modelPath := setting.JobPath + jobName + cloudbrain.ModelMountPath err := os.MkdirAll(modelPath, os.ModePerm) if err != nil { ctx.RenderWithErr(err.Error(), tplCloudBrainNew, &form) return } - err = cloudbrain.GenerateTask(ctx, jobName, image, command, uuid, codePath, modelPath) + benchmarkPath := setting.JobPath + jobName + cloudbrain.BenchMarkMountPath + if setting.IsBenchmarkEnabled && jobType == string(models.JobTypeBenchmark) { + downloadBenchmarkCode(repo, jobName, benchmarkPath) + } + + err = cloudbrain.GenerateTask(ctx, jobName, image, command, uuid, codePath, modelPath, benchmarkPath, jobType) if err != nil { ctx.RenderWithErr(err.Error(), tplCloudBrainNew, &form) return @@ -270,6 +280,17 @@ func CloudBrainDel(ctx *context.Context) { ctx.Redirect(setting.AppSubURL + ctx.Repo.RepoLink + "/cloudbrain") } +func CloudBrainBenchmark(ctx *context.Context) { + var jobID = ctx.Params(":jobid") + _, err := models.GetCloudbrainByJobID(jobID) + if err != nil { + ctx.ServerError("GetCloudbrainByJobID failed", err) + return + } + + ctx.Redirect(setting.BenchmarkServerHost) +} + func downloadCode(repo *models.Repository, codePath string) error { if err := git.Clone(repo.RepoPath(), codePath, git.CloneRepoOptions{}); err != nil { log.Error("Failed to clone repository: %s (%v)", repo.FullName(), err) @@ -278,3 +299,47 @@ func downloadCode(repo *models.Repository, codePath string) error { return nil } + +func downloadBenchmarkCode(repo *models.Repository, taskName string, benchmarkPath string) error { + err := os.MkdirAll(benchmarkPath, os.ModePerm) + if err != nil { + log.Error("mkdir benchmarkPath failed", err.Error()) + return err + } + + command := "git clone " + setting.BenchmarkCode + " " + benchmarkPath + cmd := exec.Command("/bin/bash", "-c", command) + output, err := cmd.Output() + log.Info(string(output)) + if err != nil { + log.Error("exec.Command(%s) failed:%v", command, err) + return err + } + + fileName := benchmarkPath + cloudbrain.TaskInfoName + f, err := os.OpenFile(fileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.ModePerm) + if err != nil { + log.Error("OpenFile failed", err.Error()) + return err + } + + defer f.Close() + + data, err := json.Marshal(models.TaskInfo{ + Username: repo.Owner.Name, + TaskName: taskName, + CodeName: repo.Name, + }) + if err != nil { + log.Error("json.Marshal failed", err.Error()) + return err + } + + _, err = f.Write(data) + if err != nil { + log.Error("WriteString failed", err.Error()) + return err + } + + return nil +} diff --git a/routers/routes/routes.go b/routers/routes/routes.go index c7f969e76..f40b55e2b 100755 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -900,6 +900,7 @@ func RegisterRoutes(m *macaron.Macaron) { m.Post("/commit_image", reqRepoCloudBrainWriter, bindIgnErr(auth.CommitImageCloudBrainForm{}), repo.CloudBrainCommitImage) m.Post("/stop", reqRepoCloudBrainWriter, repo.CloudBrainStop) m.Post("/del", reqRepoCloudBrainWriter, repo.CloudBrainDel) + m.Get("/benchmark", reqRepoCloudBrainWriter, repo.CloudBrainBenchmark) }) m.Get("/create", reqRepoCloudBrainWriter, repo.CloudBrainNew) m.Post("/create", reqRepoCloudBrainWriter, bindIgnErr(auth.CreateCloudBrainForm{}), repo.CloudBrainCreate) diff --git a/templates/repo/cloudbrain/index.tmpl b/templates/repo/cloudbrain/index.tmpl index 18c5053e2..2a9320f27 100755 --- a/templates/repo/cloudbrain/index.tmpl +++ b/templates/repo/cloudbrain/index.tmpl @@ -169,6 +169,11 @@ .dis { margin-bottom: 20px; } + + .disabled { + cursor: pointer; + pointer-events: none; + } @@ -221,8 +226,8 @@
@@ -235,7 +240,7 @@
-
+
{{svg "octicon-tasklist" 16}} {{.JobName}} @@ -255,8 +260,17 @@ + + + @@ -264,19 +278,18 @@
-
- {{$.CsrfTokenHtml}} - - 删除 -
-
+
+ {{$.CsrfTokenHtml}} + 删除 +
+
@@ -284,11 +297,11 @@
-
- {{$.CsrfTokenHtml}} - 停止 -
-
+
+ {{$.CsrfTokenHtml}} + 停止 +
+
@@ -325,8 +338,8 @@
+ {{$.i18n.Tr "repo.cloudbrain.commit_image"}} +
@@ -373,6 +386,15 @@ {{template "base/footer" .}} \ No newline at end of file