You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

cloudbrain.go 19 kB

3 years ago
5 years ago
5 years ago
5 years ago
3 years ago
4 years ago
4 years ago
3 years ago
4 years ago
5 years ago
5 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
4 years ago
4 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
5 years ago
3 years ago
3 years ago
5 years ago
3 years ago
5 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
3 years ago
3 years ago
4 years ago
4 years ago
3 years ago
4 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. package cloudbrain
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "os"
  6. "strconv"
  7. "code.gitea.io/gitea/modules/timeutil"
  8. "code.gitea.io/gitea/modules/storage"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/context"
  11. "code.gitea.io/gitea/modules/log"
  12. "code.gitea.io/gitea/modules/notification"
  13. "code.gitea.io/gitea/modules/setting"
  14. )
  15. const (
  16. //Command = `pip3 install jupyterlab==2.2.5 -i https://pypi.tuna.tsinghua.edu.cn/simple;service ssh stop;jupyter lab --no-browser --ip=0.0.0.0 --allow-root --notebook-dir="/code" --port=80 --LabApp.token="" --LabApp.allow_origin="self https://cloudbrain.pcl.ac.cn"`
  17. //CommandBenchmark = `echo "start benchmark";python /code/test.py;echo "end benchmark"`
  18. CommandBenchmark = `echo "start benchmark";cd /benchmark && bash run_bk.sh;echo "end benchmark"`
  19. CodeMountPath = "/code"
  20. DataSetMountPath = "/dataset"
  21. ModelMountPath = "/model"
  22. LogFile = "log.txt"
  23. BenchMarkMountPath = "/benchmark"
  24. BenchMarkResourceID = 1
  25. Snn4imagenetMountPath = "/snn4imagenet"
  26. BrainScoreMountPath = "/brainscore"
  27. TaskInfoName = "/taskInfo"
  28. Snn4imagenetCommand = `/opt/conda/bin/python /snn4imagenet/testSNN_script.py --modelname '%s' --modelpath '/dataset' --modeldescription '%s'`
  29. BrainScoreCommand = `bash /brainscore/brainscore_test_par4shSrcipt.sh -b '%s' -n '%s' -p '/dataset' -d '%s'`
  30. SubTaskName = "task1"
  31. Success = "S000"
  32. DefaultBranchName = "master"
  33. ResultPath = "/result"
  34. )
  35. var (
  36. ResourceSpecs *models.ResourceSpecs
  37. TrainResourceSpecs *models.ResourceSpecs
  38. InferenceResourceSpecs *models.ResourceSpecs
  39. SpecialPools *models.SpecialPools
  40. )
  41. type GenerateCloudBrainTaskReq struct {
  42. Ctx *context.Context
  43. DisplayJobName string
  44. JobName string
  45. Image string
  46. Command string
  47. CodePath string
  48. ModelPath string
  49. BenchmarkPath string
  50. Snn4ImageNetPath string
  51. BrainScorePath string
  52. JobType string
  53. GpuQueue string
  54. Description string
  55. BranchName string
  56. BootFile string
  57. Params string
  58. CommitID string
  59. Uuids string
  60. DatasetNames string
  61. DatasetInfos map[string]models.DatasetInfo
  62. BenchmarkTypeID int
  63. BenchmarkChildTypeID int
  64. ResourceSpecId int
  65. ResultPath string
  66. TrainUrl string
  67. ModelName string
  68. ModelVersion string
  69. CkptName string
  70. LabelName string
  71. Spec *models.Specification
  72. }
  73. func GetCloudbrainDebugCommand() string {
  74. var command = `pip3 install jupyterlab==3 -i https://pypi.tuna.tsinghua.edu.cn/simple;service ssh stop;jupyter lab --ServerApp.shutdown_no_activity_timeout=` + setting.CullIdleTimeout + ` --TerminalManager.cull_inactive_timeout=` + setting.CullIdleTimeout + ` --TerminalManager.cull_interval=` + setting.CullInterval + ` --MappingKernelManager.cull_idle_timeout=` + setting.CullIdleTimeout + ` --MappingKernelManager.cull_interval=` + setting.CullInterval + ` --MappingKernelManager.cull_connected=True --MappingKernelManager.cull_busy=True --no-browser --ip=0.0.0.0 --allow-root --notebook-dir="/code" --port=80 --ServerApp.token="" --ServerApp.allow_origin="self https://cloudbrain.pcl.ac.cn" `
  75. return command
  76. }
  77. func isAdminOrOwnerOrJobCreater(ctx *context.Context, job *models.Cloudbrain, err error) bool {
  78. if !ctx.IsSigned {
  79. return false
  80. }
  81. if err != nil {
  82. return ctx.IsUserRepoOwner() || ctx.IsUserSiteAdmin()
  83. } else {
  84. return ctx.IsUserRepoOwner() || ctx.IsUserSiteAdmin() || ctx.User.ID == job.UserID
  85. }
  86. }
  87. func CanDeleteJob(ctx *context.Context, job *models.Cloudbrain) bool {
  88. return isAdminOrOwnerOrJobCreater(ctx, job, nil)
  89. }
  90. func CanCreateOrDebugJob(ctx *context.Context) bool {
  91. if !ctx.IsSigned {
  92. return false
  93. }
  94. return ctx.Repo.CanWrite(models.UnitTypeCloudBrain)
  95. }
  96. func CanModifyJob(ctx *context.Context, job *models.Cloudbrain) bool {
  97. return isAdminOrJobCreater(ctx, job, nil)
  98. }
  99. func isAdminOrJobCreater(ctx *context.Context, job *models.Cloudbrain, err error) bool {
  100. if !ctx.IsSigned {
  101. return false
  102. }
  103. if err != nil {
  104. return ctx.IsUserSiteAdmin()
  105. } else {
  106. return ctx.IsUserSiteAdmin() || ctx.User.ID == job.UserID
  107. }
  108. }
  109. func isAdminOrImageCreater(ctx *context.Context, image *models.Image, err error) bool {
  110. if !ctx.IsSigned {
  111. return false
  112. }
  113. if err != nil {
  114. return ctx.IsUserSiteAdmin()
  115. } else {
  116. return ctx.IsUserSiteAdmin() || ctx.User.ID == image.UID
  117. }
  118. }
  119. func AdminOrOwnerOrJobCreaterRight(ctx *context.Context) {
  120. var ID = ctx.Params(":id")
  121. job, err := models.GetCloudbrainByID(ID)
  122. if err != nil {
  123. log.Error("GetCloudbrainByID failed:%v", err.Error())
  124. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  125. }
  126. ctx.Cloudbrain = job
  127. if !isAdminOrOwnerOrJobCreater(ctx, job, err) {
  128. log.Error("!isAdminOrOwnerOrJobCreater error:%v", err.Error())
  129. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  130. }
  131. }
  132. func AdminOrJobCreaterRight(ctx *context.Context) {
  133. var ID = ctx.Params(":id")
  134. job, err := models.GetCloudbrainByID(ID)
  135. if err != nil {
  136. log.Error("GetCloudbrainByID failed:%v", err.Error())
  137. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  138. }
  139. ctx.Cloudbrain = job
  140. if !isAdminOrJobCreater(ctx, job, err) {
  141. log.Error("!isAdminOrJobCreater error:%v", err.Error())
  142. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  143. }
  144. }
  145. func AdminOrOwnerOrJobCreaterRightForTrain(ctx *context.Context) {
  146. var jobID = ctx.Params(":jobid")
  147. job, err := models.GetCloudbrainByJobID(jobID)
  148. if err != nil {
  149. log.Error("GetCloudbrainByJobID failed:%v", err.Error())
  150. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  151. }
  152. ctx.Cloudbrain = job
  153. if !isAdminOrOwnerOrJobCreater(ctx, job, err) {
  154. log.Error("!isAdminOrOwnerOrJobCreater failed:%v", err.Error())
  155. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  156. }
  157. }
  158. func AdminOrJobCreaterRightForTrain(ctx *context.Context) {
  159. var jobID = ctx.Params(":jobid")
  160. job, err := models.GetCloudbrainByJobID(jobID)
  161. if err != nil {
  162. log.Error("GetCloudbrainByJobID failed:%v", err.Error())
  163. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  164. }
  165. ctx.Cloudbrain = job
  166. if !isAdminOrJobCreater(ctx, job, err) {
  167. log.Error("!isAdminOrJobCreater errot:%v", err.Error())
  168. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  169. }
  170. }
  171. func AdminOrImageCreaterRight(ctx *context.Context) {
  172. id, err := strconv.ParseInt(ctx.Params(":id"), 10, 64)
  173. var image *models.Image
  174. if err != nil {
  175. log.Error("Get Image by ID failed:%v", err.Error())
  176. } else {
  177. image, err = models.GetImageByID(id)
  178. if err != nil {
  179. log.Error("Get Image by ID failed:%v", err.Error())
  180. return
  181. }
  182. }
  183. if !isAdminOrImageCreater(ctx, image, err) {
  184. log.Error("!isAdminOrImageCreater error:%v", err.Error())
  185. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  186. }
  187. }
  188. func GenerateTask(req GenerateCloudBrainTaskReq) error {
  189. var versionCount int
  190. if req.JobType == string(models.JobTypeTrain) {
  191. versionCount = 1
  192. }
  193. volumes := []models.Volume{
  194. {
  195. HostPath: models.StHostPath{
  196. Path: req.CodePath,
  197. MountPath: CodeMountPath,
  198. ReadOnly: false,
  199. },
  200. },
  201. {
  202. HostPath: models.StHostPath{
  203. Path: req.ModelPath,
  204. MountPath: ModelMountPath,
  205. ReadOnly: false,
  206. },
  207. },
  208. {
  209. HostPath: models.StHostPath{
  210. Path: req.BenchmarkPath,
  211. MountPath: BenchMarkMountPath,
  212. ReadOnly: true,
  213. },
  214. },
  215. {
  216. HostPath: models.StHostPath{
  217. Path: req.Snn4ImageNetPath,
  218. MountPath: Snn4imagenetMountPath,
  219. ReadOnly: true,
  220. },
  221. },
  222. {
  223. HostPath: models.StHostPath{
  224. Path: req.BrainScorePath,
  225. MountPath: BrainScoreMountPath,
  226. ReadOnly: true,
  227. },
  228. },
  229. {
  230. HostPath: models.StHostPath{
  231. Path: req.ResultPath,
  232. MountPath: ResultPath,
  233. ReadOnly: false,
  234. },
  235. },
  236. }
  237. if len(req.DatasetInfos) == 1 {
  238. volumes = append(volumes, models.Volume{
  239. HostPath: models.StHostPath{
  240. Path: req.DatasetInfos[req.Uuids].DataLocalPath,
  241. MountPath: DataSetMountPath,
  242. ReadOnly: true,
  243. },
  244. })
  245. } else if len(req.DatasetInfos) > 1 {
  246. for _, dataset := range req.DatasetInfos {
  247. volumes = append(volumes, models.Volume{
  248. HostPath: models.StHostPath{
  249. Path: dataset.DataLocalPath,
  250. MountPath: DataSetMountPath + "/" + dataset.Name,
  251. ReadOnly: true,
  252. },
  253. })
  254. }
  255. }
  256. createTime := timeutil.TimeStampNow()
  257. jobResult, err := CreateJob(req.JobName, models.CreateJobParams{
  258. JobName: req.JobName,
  259. RetryCount: 1,
  260. GpuType: req.Spec.QueueCode,
  261. Image: req.Image,
  262. TaskRoles: []models.TaskRole{
  263. {
  264. Name: SubTaskName,
  265. TaskNumber: 1,
  266. MinSucceededTaskCount: 1,
  267. MinFailedTaskCount: 1,
  268. CPUNumber: req.Spec.CpuCores,
  269. GPUNumber: req.Spec.AccCardsNum,
  270. MemoryMB: int(req.Spec.MemGiB * 1024),
  271. ShmMB: int(req.Spec.ShareMemGiB * 1024),
  272. Command: req.Command,
  273. NeedIBDevice: false,
  274. IsMainRole: false,
  275. UseNNI: false,
  276. },
  277. },
  278. Volumes: volumes,
  279. })
  280. if err != nil {
  281. log.Error("CreateJob failed:", err.Error(), req.Ctx.Data["MsgID"])
  282. return err
  283. }
  284. if jobResult.Code != Success {
  285. log.Error("CreateJob(%s) failed:%s", req.JobName, jobResult.Msg, req.Ctx.Data["MsgID"])
  286. return errors.New(jobResult.Msg)
  287. }
  288. var jobID = jobResult.Payload["jobId"].(string)
  289. err = models.CreateCloudbrain(&models.Cloudbrain{
  290. Status: string(models.JobWaiting),
  291. UserID: req.Ctx.User.ID,
  292. RepoID: req.Ctx.Repo.Repository.ID,
  293. JobID: jobID,
  294. JobName: req.JobName,
  295. DisplayJobName: req.DisplayJobName,
  296. SubTaskName: SubTaskName,
  297. JobType: req.JobType,
  298. Type: models.TypeCloudBrainOne,
  299. Uuid: req.Uuids,
  300. Image: req.Image,
  301. GpuQueue: req.GpuQueue,
  302. ResourceSpecId: req.ResourceSpecId,
  303. ComputeResource: models.GPUResource,
  304. BenchmarkTypeID: req.BenchmarkTypeID,
  305. BenchmarkChildTypeID: req.BenchmarkChildTypeID,
  306. Description: req.Description,
  307. IsLatestVersion: "1",
  308. VersionCount: versionCount,
  309. BranchName: req.BranchName,
  310. BootFile: req.BootFile,
  311. DatasetName: req.DatasetNames,
  312. Parameters: req.Params,
  313. TrainUrl: req.TrainUrl,
  314. ModelName: req.ModelName,
  315. ModelVersion: req.ModelVersion,
  316. CkptName: req.CkptName,
  317. ResultUrl: req.ResultPath,
  318. LabelName: req.LabelName,
  319. CreatedUnix: createTime,
  320. UpdatedUnix: createTime,
  321. CommitID: req.CommitID,
  322. })
  323. if err != nil {
  324. return err
  325. }
  326. task, err := models.GetCloudbrainByJobID(jobID)
  327. if err != nil {
  328. log.Error("GetCloudbrainByJobID failed: %v", err.Error())
  329. return err
  330. }
  331. stringId := strconv.FormatInt(task.ID, 10)
  332. if IsBenchmarkJob(req.JobType) {
  333. notification.NotifyOtherTask(req.Ctx.User, req.Ctx.Repo.Repository, stringId, req.DisplayJobName, models.ActionCreateBenchMarkTask)
  334. } else if string(models.JobTypeTrain) == req.JobType {
  335. notification.NotifyOtherTask(req.Ctx.User, req.Ctx.Repo.Repository, jobID, req.DisplayJobName, models.ActionCreateGPUTrainTask)
  336. } else if string(models.JobTypeInference) == req.JobType {
  337. notification.NotifyOtherTask(req.Ctx.User, req.Ctx.Repo.Repository, jobID, req.DisplayJobName, models.ActionCreateInferenceTask)
  338. } else {
  339. notification.NotifyOtherTask(req.Ctx.User, req.Ctx.Repo.Repository, stringId, req.DisplayJobName, models.ActionCreateDebugGPUTask)
  340. }
  341. return nil
  342. }
  343. func IsBenchmarkJob(jobType string) bool {
  344. return string(models.JobTypeBenchmark) == jobType || string(models.JobTypeBrainScore) == jobType || string(models.JobTypeSnn4imagenet) == jobType
  345. }
  346. func GetWaitingCloudbrainCount(cloudbrainType int, computeResource string, jobTypes ...models.JobType) int64 {
  347. num, err := models.GetWaitingCloudbrainCount(cloudbrainType, computeResource, jobTypes...)
  348. if err != nil {
  349. log.Warn("Get waiting count err", err)
  350. num = 0
  351. }
  352. return num
  353. }
  354. func RestartTask(ctx *context.Context, task *models.Cloudbrain, newID *string) error {
  355. jobName := task.JobName
  356. var resourceSpec *models.ResourceSpec
  357. if ResourceSpecs == nil {
  358. json.Unmarshal([]byte(setting.ResourceSpecs), &ResourceSpecs)
  359. }
  360. for _, spec := range ResourceSpecs.ResourceSpec {
  361. if task.ResourceSpecId == spec.Id {
  362. resourceSpec = spec
  363. }
  364. }
  365. //如果没有匹配到spec信息,尝试从专属资源池获取
  366. if resourceSpec == nil && SpecialPools != nil {
  367. resourceSpec = geMatchResourceSpec(task.JobType, task.GpuQueue, task.ResourceSpecId)
  368. }
  369. if resourceSpec == nil {
  370. log.Error("no such resourceSpecId(%d)", task.ResourceSpecId, ctx.Data["MsgID"])
  371. return errors.New("no such resourceSpec")
  372. }
  373. var datasetInfos map[string]models.DatasetInfo
  374. if task.Uuid != "" {
  375. var err error
  376. datasetInfos, _, err = models.GetDatasetInfo(task.Uuid)
  377. if err != nil {
  378. log.Error("GetDatasetInfo failed:%v", err, ctx.Data["MsgID"])
  379. return err
  380. }
  381. }
  382. volumes := []models.Volume{
  383. {
  384. HostPath: models.StHostPath{
  385. Path: storage.GetMinioPath(jobName, CodeMountPath+"/"),
  386. MountPath: CodeMountPath,
  387. ReadOnly: false,
  388. },
  389. },
  390. {
  391. HostPath: models.StHostPath{
  392. Path: storage.GetMinioPath(jobName, ModelMountPath+"/"),
  393. MountPath: ModelMountPath,
  394. ReadOnly: false,
  395. },
  396. },
  397. {
  398. HostPath: models.StHostPath{
  399. Path: storage.GetMinioPath(jobName, BenchMarkMountPath+"/"),
  400. MountPath: BenchMarkMountPath,
  401. ReadOnly: true,
  402. },
  403. },
  404. {
  405. HostPath: models.StHostPath{
  406. Path: storage.GetMinioPath(jobName, Snn4imagenetMountPath+"/"),
  407. MountPath: Snn4imagenetMountPath,
  408. ReadOnly: true,
  409. },
  410. },
  411. {
  412. HostPath: models.StHostPath{
  413. Path: storage.GetMinioPath(jobName, BrainScoreMountPath+"/"),
  414. MountPath: BrainScoreMountPath,
  415. ReadOnly: true,
  416. },
  417. },
  418. }
  419. if datasetInfos != nil {
  420. if len(datasetInfos) == 1 {
  421. volumes = append(volumes, models.Volume{
  422. HostPath: models.StHostPath{
  423. Path: datasetInfos[task.Uuid].DataLocalPath,
  424. MountPath: DataSetMountPath,
  425. ReadOnly: true,
  426. },
  427. })
  428. } else {
  429. for _, dataset := range datasetInfos {
  430. volumes = append(volumes, models.Volume{
  431. HostPath: models.StHostPath{
  432. Path: dataset.DataLocalPath,
  433. MountPath: DataSetMountPath + "/" + dataset.Name,
  434. ReadOnly: true,
  435. },
  436. })
  437. }
  438. }
  439. }
  440. createTime := timeutil.TimeStampNow()
  441. jobResult, err := CreateJob(jobName, models.CreateJobParams{
  442. JobName: jobName,
  443. RetryCount: 1,
  444. GpuType: task.GpuQueue,
  445. Image: task.Image,
  446. TaskRoles: []models.TaskRole{
  447. {
  448. Name: SubTaskName,
  449. TaskNumber: 1,
  450. MinSucceededTaskCount: 1,
  451. MinFailedTaskCount: 1,
  452. CPUNumber: resourceSpec.CpuNum,
  453. GPUNumber: resourceSpec.GpuNum,
  454. MemoryMB: resourceSpec.MemMiB,
  455. ShmMB: resourceSpec.ShareMemMiB,
  456. Command: GetCloudbrainDebugCommand(), //Command,
  457. NeedIBDevice: false,
  458. IsMainRole: false,
  459. UseNNI: false,
  460. },
  461. },
  462. Volumes: volumes,
  463. })
  464. if err != nil {
  465. log.Error("CreateJob failed:%v", err.Error(), ctx.Data["MsgID"])
  466. return err
  467. }
  468. if jobResult.Code != Success {
  469. log.Error("CreateJob(%s) failed:%s", jobName, jobResult.Msg, ctx.Data["MsgID"])
  470. return errors.New(jobResult.Msg)
  471. }
  472. var jobID = jobResult.Payload["jobId"].(string)
  473. newTask := &models.Cloudbrain{
  474. Status: string(models.JobWaiting),
  475. UserID: task.UserID,
  476. RepoID: task.RepoID,
  477. JobID: jobID,
  478. JobName: task.JobName,
  479. DisplayJobName: task.DisplayJobName,
  480. SubTaskName: task.SubTaskName,
  481. JobType: task.JobType,
  482. Type: task.Type,
  483. Uuid: task.Uuid,
  484. DatasetName: task.DatasetName,
  485. Image: task.Image,
  486. GpuQueue: task.GpuQueue,
  487. ResourceSpecId: task.ResourceSpecId,
  488. ComputeResource: task.ComputeResource,
  489. CreatedUnix: createTime,
  490. UpdatedUnix: createTime,
  491. BranchName: task.BranchName,
  492. }
  493. err = models.RestartCloudbrain(task, newTask)
  494. if err != nil {
  495. log.Error("RestartCloudbrain(%s) failed:%v", jobName, err.Error(), ctx.Data["MsgID"])
  496. return err
  497. }
  498. stringId := strconv.FormatInt(newTask.ID, 10)
  499. *newID = stringId
  500. notification.NotifyOtherTask(ctx.User, ctx.Repo.Repository, stringId, task.DisplayJobName, models.ActionCreateDebugGPUTask)
  501. return nil
  502. }
  503. func geMatchResourceSpec(jobType string, gpuQueue string, resourceSpecId int) *models.ResourceSpec {
  504. for _, specialPool := range SpecialPools.Pools {
  505. if specialPool.ResourceSpec != nil {
  506. if IsElementExist(specialPool.JobType, jobType) && IsQueueInSpecialtPool(specialPool.Pool, gpuQueue) {
  507. for _, spec := range specialPool.ResourceSpec {
  508. if resourceSpecId == spec.Id {
  509. return spec
  510. }
  511. }
  512. }
  513. }
  514. }
  515. return nil
  516. }
  517. func DelCloudBrainJob(jobId string) string {
  518. task, err := models.GetCloudbrainByJobID(jobId)
  519. if err != nil {
  520. log.Error("get cloud brain err:", err)
  521. return "cloudbrain.Delete_failed"
  522. }
  523. if task.Status != string(models.JobStopped) && task.Status != string(models.JobFailed) && task.Status != string(models.JobSucceeded) {
  524. log.Error("the job(%s) has not been stopped", task.JobName)
  525. return "cloudbrain.Not_Stopped"
  526. }
  527. err = models.DeleteJob(task)
  528. if err != nil {
  529. log.Error("DeleteJob failed:", err)
  530. return "cloudbrain.Delete_failed"
  531. }
  532. deleteJobStorage(task.JobName)
  533. return ""
  534. }
  535. func deleteJobStorage(jobName string) error {
  536. //delete local
  537. localJobPath := setting.JobPath + jobName
  538. err := os.RemoveAll(localJobPath)
  539. if err != nil {
  540. log.Error("RemoveAll(%s) failed:%v", localJobPath, err)
  541. }
  542. dirPath := setting.CBCodePathPrefix + jobName + "/"
  543. err = storage.Attachments.DeleteDir(dirPath)
  544. if err != nil {
  545. log.Error("DeleteDir(%s) failed:%v", localJobPath, err)
  546. }
  547. return nil
  548. }
  549. func InitSpecialPool() {
  550. if SpecialPools == nil && setting.SpecialPools != "" {
  551. json.Unmarshal([]byte(setting.SpecialPools), &SpecialPools)
  552. }
  553. }
  554. func IsResourceSpecInSpecialPool(resourceSpecs []*models.ResourceSpec, resourceSpecId int) bool {
  555. if resourceSpecs == nil || len(resourceSpecs) == 0 {
  556. return true
  557. }
  558. for _, v := range resourceSpecs {
  559. if v.Id == resourceSpecId {
  560. return true
  561. }
  562. }
  563. return false
  564. }
  565. func IsQueueInSpecialtPool(pool []*models.GpuInfo, queue string) bool {
  566. for _, v := range pool {
  567. if v.Queue == queue {
  568. return true
  569. }
  570. }
  571. return false
  572. }
  573. func IsElementExist(s []string, str string) bool {
  574. for _, v := range s {
  575. if v == str {
  576. return true
  577. }
  578. }
  579. return false
  580. }