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.

resty.go 22 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. package modelarts
  2. import (
  3. "crypto/tls"
  4. "encoding/json"
  5. "fmt"
  6. "net/http"
  7. "strconv"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/log"
  10. "code.gitea.io/gitea/modules/setting"
  11. "github.com/go-resty/resty/v2"
  12. )
  13. var (
  14. restyClient *resty.Client
  15. HOST string
  16. TOKEN string
  17. )
  18. const (
  19. methodPassword = "password"
  20. urlGetToken = "/v3/auth/tokens"
  21. urlNotebook = "/demanager/instances"
  22. urlTrainJob = "/training-jobs"
  23. urlResourceSpecs = "/job/resource-specs"
  24. urlTrainJobConfig = "/training-job-configs"
  25. errorCodeExceedLimit = "ModelArts.0118"
  26. )
  27. func getRestyClient() *resty.Client {
  28. if restyClient == nil {
  29. restyClient = resty.New()
  30. restyClient.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
  31. }
  32. return restyClient
  33. }
  34. func checkSetting() {
  35. if len(HOST) != 0 && len(TOKEN) != 0 && restyClient != nil {
  36. return
  37. }
  38. err := getToken()
  39. if err != nil {
  40. log.Error("getToken failed:%v", err)
  41. }
  42. }
  43. func getToken() error {
  44. HOST = setting.ModelArtsHost
  45. client := getRestyClient()
  46. params := models.GetTokenParams{
  47. Auth: models.Auth{
  48. Identity: models.Identity{
  49. Methods: []string{methodPassword},
  50. Password: models.Password{
  51. User: models.NotebookUser{
  52. Name: setting.ModelArtsUsername,
  53. Password: setting.ModelArtsPassword,
  54. Domain: models.Domain{
  55. Name: setting.ModelArtsDomain,
  56. },
  57. },
  58. },
  59. },
  60. Scope: models.Scope{
  61. Project: models.Project{
  62. Name: setting.ProjectName,
  63. },
  64. },
  65. },
  66. }
  67. res, err := client.R().
  68. SetHeader("Content-Type", "application/json").
  69. SetBody(params).
  70. Post(setting.IamHost + urlGetToken)
  71. if err != nil {
  72. return fmt.Errorf("resty getToken: %v", err)
  73. }
  74. if res.StatusCode() != http.StatusCreated {
  75. return fmt.Errorf("getToken failed:%s", res.String())
  76. }
  77. TOKEN = res.Header().Get("X-Subject-Token")
  78. return nil
  79. }
  80. func CreateJob(createJobParams models.CreateNotebookParams) (*models.CreateNotebookResult, error) {
  81. checkSetting()
  82. client := getRestyClient()
  83. var result models.CreateNotebookResult
  84. retry := 0
  85. sendjob:
  86. res, err := client.R().
  87. SetHeader("Content-Type", "application/json").
  88. SetAuthToken(TOKEN).
  89. SetBody(createJobParams).
  90. SetResult(&result).
  91. Post(HOST + "/v1/" + setting.ProjectID + urlNotebook)
  92. if err != nil {
  93. return nil, fmt.Errorf("resty create notebook: %s", err)
  94. }
  95. if res.StatusCode() == http.StatusUnauthorized && retry < 1 {
  96. retry++
  97. _ = getToken()
  98. goto sendjob
  99. }
  100. var response models.NotebookResult
  101. err = json.Unmarshal(res.Body(), &response)
  102. if err != nil {
  103. log.Error("json.Unmarshal failed: %s", err.Error())
  104. return &result, fmt.Errorf("son.Unmarshal failed: %s", err.Error())
  105. }
  106. if len(response.ErrorCode) != 0 {
  107. log.Error("createNotebook failed(%s): %s", response.ErrorCode, response.ErrorMsg)
  108. if response.ErrorCode == errorCodeExceedLimit {
  109. response.ErrorMsg = "所选规格使用数量已超过最大配额限制。"
  110. }
  111. return &result, fmt.Errorf("createNotebook failed(%s): %s", response.ErrorCode, response.ErrorMsg)
  112. }
  113. return &result, nil
  114. }
  115. func GetJob(jobID string) (*models.GetNotebookResult, error) {
  116. checkSetting()
  117. client := getRestyClient()
  118. var result models.GetNotebookResult
  119. retry := 0
  120. sendjob:
  121. res, err := client.R().
  122. SetHeader("Content-Type", "application/json").
  123. SetAuthToken(TOKEN).
  124. SetResult(&result).
  125. Get(HOST + "/v1/" + setting.ProjectID + urlNotebook + "/" + jobID)
  126. if err != nil {
  127. return nil, fmt.Errorf("resty GetJob: %v", err)
  128. }
  129. if res.StatusCode() == http.StatusUnauthorized && retry < 1 {
  130. retry++
  131. _ = getToken()
  132. goto sendjob
  133. }
  134. var response models.NotebookResult
  135. err = json.Unmarshal(res.Body(), &response)
  136. if err != nil {
  137. log.Error("json.Unmarshal failed: %s", err.Error())
  138. return &result, fmt.Errorf("son.Unmarshal failed: %s", err.Error())
  139. }
  140. if len(response.ErrorCode) != 0 {
  141. log.Error("GetJob failed(%s): %s", response.ErrorCode, response.ErrorMsg)
  142. return &result, fmt.Errorf("GetJob failed(%s): %s", response.ErrorCode, response.ErrorMsg)
  143. }
  144. return &result, nil
  145. }
  146. func StopJob(jobID string, param models.NotebookAction) (*models.NotebookActionResult, error) {
  147. checkSetting()
  148. client := getRestyClient()
  149. var result models.NotebookActionResult
  150. retry := 0
  151. sendjob:
  152. res, err := client.R().
  153. SetHeader("Content-Type", "application/json").
  154. SetBody(param).
  155. SetAuthToken(TOKEN).
  156. SetResult(&result).
  157. Post(HOST + "/v1/" + setting.ProjectID + urlNotebook + "/" + jobID + "/action")
  158. if err != nil {
  159. return &result, fmt.Errorf("resty StopJob: %v", err)
  160. }
  161. if res.StatusCode() == http.StatusUnauthorized && retry < 1 {
  162. retry++
  163. _ = getToken()
  164. goto sendjob
  165. }
  166. var response models.NotebookResult
  167. err = json.Unmarshal(res.Body(), &response)
  168. if err != nil {
  169. log.Error("json.Unmarshal failed: %s", err.Error())
  170. return &result, fmt.Errorf("son.Unmarshal failed: %s", err.Error())
  171. }
  172. if len(response.ErrorCode) != 0 {
  173. log.Error("StopJob failed(%s): %s", response.ErrorCode, response.ErrorMsg)
  174. return &result, fmt.Errorf("StopJob failed(%s): %s", response.ErrorCode, response.ErrorMsg)
  175. }
  176. return &result, nil
  177. }
  178. func DelNotebook(jobID string) (*models.NotebookDelResult, error) {
  179. checkSetting()
  180. client := getRestyClient()
  181. var result models.NotebookDelResult
  182. retry := 0
  183. sendjob:
  184. res, err := client.R().
  185. SetHeader("Content-Type", "application/json").
  186. SetAuthToken(TOKEN).
  187. SetResult(&result).
  188. Delete(HOST + "/v1/" + setting.ProjectID + urlNotebook + "/" + jobID)
  189. if err != nil {
  190. return &result, fmt.Errorf("resty DelJob: %v", err)
  191. }
  192. if res.StatusCode() == http.StatusUnauthorized && retry < 1 {
  193. retry++
  194. _ = getToken()
  195. goto sendjob
  196. }
  197. var response models.NotebookResult
  198. err = json.Unmarshal(res.Body(), &response)
  199. if err != nil {
  200. log.Error("json.Unmarshal failed: %s", err.Error())
  201. return &result, fmt.Errorf("son.Unmarshal failed: %s", err.Error())
  202. }
  203. if len(response.ErrorCode) != 0 {
  204. log.Error("DelJob failed(%s): %s", response.ErrorCode, response.ErrorMsg)
  205. return &result, fmt.Errorf("DelJob failed(%s): %s", response.ErrorCode, response.ErrorMsg)
  206. }
  207. return &result, nil
  208. }
  209. func DelJob(jobID string) (*models.NotebookDelResult, error) {
  210. checkSetting()
  211. client := getRestyClient()
  212. var result models.NotebookDelResult
  213. retry := 0
  214. sendjob:
  215. res, err := client.R().
  216. SetHeader("Content-Type", "application/json").
  217. SetAuthToken(TOKEN).
  218. SetResult(&result).
  219. Delete(HOST + "/v1/" + setting.ProjectID + urlNotebook + "/" + jobID)
  220. if err != nil {
  221. return &result, fmt.Errorf("resty DelJob: %v", err)
  222. }
  223. if res.StatusCode() == http.StatusUnauthorized && retry < 1 {
  224. retry++
  225. _ = getToken()
  226. goto sendjob
  227. }
  228. var response models.NotebookResult
  229. err = json.Unmarshal(res.Body(), &response)
  230. if err != nil {
  231. log.Error("json.Unmarshal failed: %s", err.Error())
  232. return &result, fmt.Errorf("son.Unmarshal failed: %s", err.Error())
  233. }
  234. if len(response.ErrorCode) != 0 {
  235. log.Error("DelJob failed(%s): %s", response.ErrorCode, response.ErrorMsg)
  236. return &result, fmt.Errorf("DelJob failed(%s): %s", response.ErrorCode, response.ErrorMsg)
  237. }
  238. return &result, nil
  239. }
  240. func GetJobToken(jobID string) (*models.NotebookGetJobTokenResult, error) {
  241. checkSetting()
  242. client := getRestyClient()
  243. var result models.NotebookGetJobTokenResult
  244. retry := 0
  245. sendjob:
  246. res, err := client.R().
  247. SetHeader("Content-Type", "application/json").
  248. SetAuthToken(TOKEN).
  249. SetResult(&result).
  250. Get(HOST + "/v1/" + setting.ProjectID + urlNotebook + "/" + jobID + "/token")
  251. if err != nil {
  252. return &result, fmt.Errorf("resty GetJobToken: %v", err)
  253. }
  254. if res.StatusCode() == http.StatusUnauthorized && retry < 1 {
  255. retry++
  256. _ = getToken()
  257. goto sendjob
  258. }
  259. var response models.NotebookResult
  260. err = json.Unmarshal(res.Body(), &response)
  261. if err != nil {
  262. log.Error("json.Unmarshal failed: %s", err.Error())
  263. return &result, fmt.Errorf("son.Unmarshal failed: %s", err.Error())
  264. }
  265. if len(response.ErrorCode) != 0 {
  266. log.Error("GetJobToken failed(%s): %s", response.ErrorCode, response.ErrorMsg)
  267. return &result, fmt.Errorf("GetJobToken failed(%s): %s", response.ErrorCode, response.ErrorMsg)
  268. }
  269. return &result, nil
  270. }
  271. func createTrainJob(createJobParams models.CreateTrainJobParams) (*models.CreateTrainJobResult, error) {
  272. checkSetting()
  273. client := getRestyClient()
  274. var result models.CreateTrainJobResult
  275. retry := 0
  276. sendjob:
  277. res, err := client.R().
  278. SetHeader("Content-Type", "application/json").
  279. SetAuthToken(TOKEN).
  280. SetBody(createJobParams).
  281. SetResult(&result).
  282. Post(HOST + "/v1/" + setting.ProjectID + urlTrainJob)
  283. if err != nil {
  284. return nil, fmt.Errorf("resty create train-job: %s", err)
  285. }
  286. req, _ := json.Marshal(createJobParams)
  287. log.Info("%s", req)
  288. if res.StatusCode() == http.StatusUnauthorized && retry < 1 {
  289. retry++
  290. _ = getToken()
  291. goto sendjob
  292. }
  293. if res.StatusCode() != http.StatusOK {
  294. var temp models.ErrorResult
  295. if err = json.Unmarshal([]byte(res.String()), &temp); err != nil {
  296. log.Error("json.Unmarshal failed(%s): %v", res.String(), err.Error())
  297. return &result, fmt.Errorf("json.Unmarshal failed(%s): %v", res.String(), err.Error())
  298. }
  299. log.Error("createTrainJob failed(%d):%s(%s)", res.StatusCode(), temp.ErrorCode, temp.ErrorMsg)
  300. if temp.ErrorCode == "ModelArts.0404" {
  301. temp.ErrorCode = "0404"
  302. temp.ErrorMsg = "启动文件未找到!"
  303. }
  304. if temp.ErrorCode == "ModelArts.0104" {
  305. temp.ErrorCode = "0104"
  306. temp.ErrorMsg = "运行参数错误!"
  307. }
  308. return &result, fmt.Errorf("createTrainJob failed(%d):%s(%s)", res.StatusCode(), temp.ErrorCode, temp.ErrorMsg)
  309. }
  310. if !result.IsSuccess {
  311. log.Error("createTrainJob failed(%s): %s", result.ErrorCode, result.ErrorMsg)
  312. return &result, fmt.Errorf("createTrainJob failed(%s): %s", result.ErrorCode, result.ErrorMsg)
  313. }
  314. return &result, nil
  315. }
  316. func GetResourceSpecs() (*models.GetResourceSpecsResult, error) {
  317. checkSetting()
  318. client := getRestyClient()
  319. var result models.GetResourceSpecsResult
  320. retry := 0
  321. sendjob:
  322. res, err := client.R().
  323. SetHeader("Content-Type", "application/json").
  324. SetAuthToken(TOKEN).
  325. SetResult(&result).
  326. Get(HOST + "/v1/" + setting.ProjectID + urlResourceSpecs)
  327. if err != nil {
  328. return nil, fmt.Errorf("resty GetResourceSpecs: %v", err)
  329. }
  330. if res.StatusCode() == http.StatusUnauthorized && retry < 1 {
  331. retry++
  332. _ = getToken()
  333. goto sendjob
  334. }
  335. if res.StatusCode() != http.StatusOK {
  336. var temp models.ErrorResult
  337. if err = json.Unmarshal([]byte(res.String()), &temp); err != nil {
  338. log.Error("json.Unmarshal failed(%s): %v", res.String(), err.Error())
  339. return &result, fmt.Errorf("json.Unmarshal failed(%s): %v", res.String(), err.Error())
  340. }
  341. log.Error("GetResourceSpecs failed(%d):%s(%s)", res.StatusCode(), temp.ErrorCode, temp.ErrorMsg)
  342. return &result, fmt.Errorf("GetResourceSpecs failed(%d):%s(%s)", res.StatusCode(), temp.ErrorCode, temp.ErrorMsg)
  343. }
  344. if !result.IsSuccess {
  345. log.Error("GetResourceSpecs failed(%s): %s", result.ErrorCode, result.ErrorMsg)
  346. return &result, fmt.Errorf("GetResourceSpecs failed(%s): %s", result.ErrorCode, result.ErrorMsg)
  347. }
  348. return &result, nil
  349. }
  350. func CreateTrainJobConfig(req models.CreateConfigParams) (*models.CreateTrainJobConfigResult, error) {
  351. checkSetting()
  352. client := getRestyClient()
  353. var result models.CreateTrainJobConfigResult
  354. retry := 0
  355. sendjob:
  356. res, err := client.R().
  357. SetHeader("Content-Type", "application/json").
  358. SetAuthToken(TOKEN).
  359. SetBody(req).
  360. SetResult(&result).
  361. Post(HOST + "/v1/" + setting.ProjectID + urlTrainJobConfig)
  362. if err != nil {
  363. return nil, fmt.Errorf("resty CreateTrainJobConfig: %s", err)
  364. }
  365. if res.StatusCode() == http.StatusUnauthorized && retry < 1 {
  366. retry++
  367. _ = getToken()
  368. goto sendjob
  369. }
  370. //temp, _ := json.Marshal(req)
  371. //log.Info("%s", temp)
  372. if res.StatusCode() != http.StatusOK {
  373. var temp models.ErrorResult
  374. if err = json.Unmarshal([]byte(res.String()), &temp); err != nil {
  375. log.Error("json.Unmarshal failed(%s): %v", res.String(), err.Error())
  376. return &result, fmt.Errorf("json.Unmarshal failed(%s): %v", res.String(), err.Error())
  377. }
  378. log.Error("CreateTrainJobConfig failed(%d):%s(%s)", res.StatusCode(), temp.ErrorCode, temp.ErrorMsg)
  379. return &result, fmt.Errorf("CreateTrainJobConfig failed(%d):%s(%s)", res.StatusCode(), temp.ErrorCode, temp.ErrorMsg)
  380. }
  381. if !result.IsSuccess {
  382. log.Error("CreateTrainJobConfig failed(%s): %s", result.ErrorCode, result.ErrorMsg)
  383. return &result, fmt.Errorf("CreateTrainJobConfig failed(%s): %s", result.ErrorCode, result.ErrorMsg)
  384. }
  385. return &result, nil
  386. }
  387. func GetConfigList(perPage, page int, sortBy, order, searchContent, configType string) (*models.GetConfigListResult, error) {
  388. checkSetting()
  389. client := getRestyClient()
  390. var result models.GetConfigListResult
  391. retry := 0
  392. sendjob:
  393. res, err := client.R().
  394. SetQueryParams(map[string]string{
  395. "per_page": strconv.Itoa(perPage),
  396. "page": strconv.Itoa(page),
  397. "sortBy": sortBy,
  398. "order": order,
  399. "search_content": searchContent,
  400. "config_type": configType,
  401. }).
  402. SetAuthToken(TOKEN).
  403. SetResult(&result).
  404. Get(HOST + "/v1/" + setting.ProjectID + urlTrainJobConfig)
  405. if err != nil {
  406. return nil, fmt.Errorf("resty GetConfigList: %v", err)
  407. }
  408. if res.StatusCode() == http.StatusUnauthorized && retry < 1 {
  409. retry++
  410. _ = getToken()
  411. goto sendjob
  412. }
  413. if res.StatusCode() != http.StatusOK {
  414. var temp models.ErrorResult
  415. if err = json.Unmarshal([]byte(res.String()), &temp); err != nil {
  416. log.Error("json.Unmarshal failed(%s): %v", res.String(), err.Error())
  417. return &result, fmt.Errorf("json.Unmarshal failed(%s): %v", res.String(), err.Error())
  418. }
  419. log.Error("GetConfigList failed(%d):%s(%s)", res.StatusCode(), temp.ErrorCode, temp.ErrorMsg)
  420. return &result, fmt.Errorf("获取参数配置列表失败(%d):%s(%s)", res.StatusCode(), temp.ErrorCode, temp.ErrorMsg)
  421. }
  422. if !result.IsSuccess {
  423. log.Error("GetConfigList failed(%s): %s", result.ErrorCode, result.ErrorMsg)
  424. return &result, fmt.Errorf("获取参数配置列表失败(%s): %s", result.ErrorCode, result.ErrorMsg)
  425. }
  426. return &result, nil
  427. }
  428. func GetParaConfig(configName, configType string) (models.GetConfigResult, error) {
  429. checkSetting()
  430. client := getRestyClient()
  431. var result models.GetConfigResult
  432. retry := 0
  433. sendjob:
  434. res, err := client.R().
  435. SetQueryParams(map[string]string{
  436. "config_type": configType,
  437. }).
  438. SetAuthToken(TOKEN).
  439. SetResult(&result).
  440. Get(HOST + "/v1/" + setting.ProjectID + urlTrainJobConfig + "/" + configName)
  441. if err != nil {
  442. return result, fmt.Errorf("resty GetParaConfig: %v", err)
  443. }
  444. if res.StatusCode() == http.StatusUnauthorized && retry < 1 {
  445. retry++
  446. _ = getToken()
  447. goto sendjob
  448. }
  449. if res.StatusCode() != http.StatusOK {
  450. var temp models.ErrorResult
  451. if err = json.Unmarshal([]byte(res.String()), &temp); err != nil {
  452. log.Error("json.Unmarshal failed(%s): %v", res.String(), err.Error())
  453. return result, fmt.Errorf("json.Unmarshal failed(%s): %v", res.String(), err.Error())
  454. }
  455. log.Error("GetParaConfig failed(%d):%s(%s)", res.StatusCode(), temp.ErrorCode, temp.ErrorMsg)
  456. return result, fmt.Errorf("获取参数配置详情失败(%d):%s(%s)", res.StatusCode(), temp.ErrorCode, temp.ErrorMsg)
  457. }
  458. if !result.IsSuccess {
  459. log.Error("GetParaConfig failed(%s): %s", result.ErrorCode, result.ErrorMsg)
  460. return result, fmt.Errorf("获取参数配置详情失败(%s): %s", result.ErrorCode, result.ErrorMsg)
  461. }
  462. return result, nil
  463. }
  464. func GetTrainJob(jobID, versionID string) (*models.GetTrainJobResult, error) {
  465. checkSetting()
  466. client := getRestyClient()
  467. var result models.GetTrainJobResult
  468. retry := 0
  469. sendjob:
  470. res, err := client.R().
  471. SetAuthToken(TOKEN).
  472. SetResult(&result).
  473. Get(HOST + "/v1/" + setting.ProjectID + urlTrainJob + "/" + jobID + "/versions/" + versionID)
  474. if err != nil {
  475. return nil, fmt.Errorf("resty GetTrainJob: %v", err)
  476. }
  477. if res.StatusCode() == http.StatusUnauthorized && retry < 1 {
  478. retry++
  479. _ = getToken()
  480. goto sendjob
  481. }
  482. if res.StatusCode() != http.StatusOK {
  483. var temp models.ErrorResult
  484. if err = json.Unmarshal([]byte(res.String()), &temp); err != nil {
  485. log.Error("json.Unmarshal failed(%s): %v", res.String(), err.Error())
  486. return &result, fmt.Errorf("json.Unmarshal failed(%s): %v", res.String(), err.Error())
  487. }
  488. log.Error("GetTrainJob failed(%d):%s(%s)", res.StatusCode(), temp.ErrorCode, temp.ErrorMsg)
  489. return &result, fmt.Errorf("获取作业详情失败(%d):%s(%s)", res.StatusCode(), temp.ErrorCode, temp.ErrorMsg)
  490. }
  491. if !result.IsSuccess {
  492. log.Error("GetTrainJob(%s) failed", jobID)
  493. return &result, fmt.Errorf("获取作业详情失败")
  494. }
  495. return &result, nil
  496. }
  497. func GetTrainJobLog(jobID, versionID, baseLine, logFile, order string, lines int) (*models.GetTrainJobLogResult, error) {
  498. checkSetting()
  499. client := getRestyClient()
  500. var result models.GetTrainJobLogResult
  501. retry := 0
  502. sendjob:
  503. res, err := client.R().
  504. SetQueryParams(map[string]string{
  505. "base_line": baseLine,
  506. "lines": strconv.Itoa(lines),
  507. "log_file": logFile,
  508. "order": order,
  509. }).
  510. SetAuthToken(TOKEN).
  511. SetResult(&result).
  512. Get(HOST + "/v1/" + setting.ProjectID + urlTrainJob + "/" + jobID + "/versions/" + versionID + "/aom-log")
  513. if err != nil {
  514. return nil, fmt.Errorf("resty GetTrainJobLog: %v", err)
  515. }
  516. if res.StatusCode() == http.StatusUnauthorized && retry < 1 {
  517. retry++
  518. _ = getToken()
  519. goto sendjob
  520. }
  521. if res.StatusCode() != http.StatusOK {
  522. var temp models.ErrorResult
  523. if err = json.Unmarshal([]byte(res.String()), &temp); err != nil {
  524. log.Error("json.Unmarshal failed(%s): %v", res.String(), err.Error())
  525. return &result, fmt.Errorf("json.Unmarshal failed(%s): %v", res.String(), err.Error())
  526. }
  527. log.Error("GetTrainJobLog failed(%d):%s(%s)", res.StatusCode(), temp.ErrorCode, temp.ErrorMsg)
  528. return &result, fmt.Errorf("获取作业日志失败(%d):%s(%s)", res.StatusCode(), temp.ErrorCode, temp.ErrorMsg)
  529. }
  530. if !result.IsSuccess {
  531. log.Error("GetTrainJobLog(%s) failed", jobID)
  532. return &result, fmt.Errorf("获取作业日志失败:%s", result.ErrorMsg)
  533. }
  534. return &result, nil
  535. }
  536. func GetTrainJobLogFileNames(jobID, versionID string) (*models.GetTrainJobLogFileNamesResult, error) {
  537. checkSetting()
  538. client := getRestyClient()
  539. var result models.GetTrainJobLogFileNamesResult
  540. retry := 0
  541. sendjob:
  542. res, err := client.R().
  543. SetAuthToken(TOKEN).
  544. SetResult(&result).
  545. Get(HOST + "/v1/" + setting.ProjectID + urlTrainJob + "/" + jobID + "/versions/" + versionID + "/log/file-names")
  546. if err != nil {
  547. return nil, fmt.Errorf("resty GetTrainJobLogFileNames: %v", err)
  548. }
  549. if res.StatusCode() == http.StatusUnauthorized && retry < 1 {
  550. retry++
  551. _ = getToken()
  552. goto sendjob
  553. }
  554. if res.StatusCode() != http.StatusOK {
  555. var temp models.ErrorResult
  556. if err = json.Unmarshal([]byte(res.String()), &temp); err != nil {
  557. log.Error("json.Unmarshal failed(%s): %v", res.String(), err.Error())
  558. return &result, fmt.Errorf("json.Unmarshal failed(%s): %v", res.String(), err.Error())
  559. }
  560. log.Error("GetTrainJobLogFileNames failed(%d):%s(%s)", res.StatusCode(), temp.ErrorCode, temp.ErrorMsg)
  561. return &result, fmt.Errorf("GetTrainJobLogFileNames failed(%d):%s(%s)", res.StatusCode(), temp.ErrorCode, temp.ErrorMsg)
  562. }
  563. if !result.IsSuccess {
  564. log.Error("GetTrainJobLogFileNames(%s) failed", jobID)
  565. return &result, fmt.Errorf("获取作业日志文件失败:%s", result.ErrorMsg)
  566. }
  567. return &result, nil
  568. }
  569. func DelTrainJob(jobID string) (*models.TrainJobResult, error) {
  570. checkSetting()
  571. client := getRestyClient()
  572. var result models.TrainJobResult
  573. retry := 0
  574. sendjob:
  575. res, err := client.R().
  576. SetAuthToken(TOKEN).
  577. SetResult(&result).
  578. Delete(HOST + "/v1/" + setting.ProjectID + urlTrainJob + "/" + jobID)
  579. if err != nil {
  580. return &result, fmt.Errorf("resty DelTrainJob: %v", err)
  581. }
  582. if res.StatusCode() == http.StatusUnauthorized && retry < 1 {
  583. retry++
  584. _ = getToken()
  585. goto sendjob
  586. }
  587. if res.StatusCode() != http.StatusOK {
  588. var temp models.ErrorResult
  589. if err = json.Unmarshal([]byte(res.String()), &temp); err != nil {
  590. log.Error("json.Unmarshal failed(%s): %v", res.String(), err.Error())
  591. return &result, fmt.Errorf("json.Unmarshal failed(%s): %v", res.String(), err.Error())
  592. }
  593. log.Error("DelTrainJob failed(%d):%s(%s)", res.StatusCode(), temp.ErrorCode, temp.ErrorMsg)
  594. return &result, fmt.Errorf("删除训练作业失败(%d):%s(%s)", res.StatusCode(), temp.ErrorCode, temp.ErrorMsg)
  595. }
  596. if !result.IsSuccess {
  597. log.Error("DelTrainJob(%s) failed", jobID)
  598. return &result, fmt.Errorf("删除训练作业失败:%s", result.ErrorMsg)
  599. }
  600. return &result, nil
  601. }
  602. func StopTrainJob(jobID, versionID string) (*models.TrainJobResult, error) {
  603. checkSetting()
  604. client := getRestyClient()
  605. var result models.TrainJobResult
  606. retry := 0
  607. sendjob:
  608. res, err := client.R().
  609. SetAuthToken(TOKEN).
  610. SetResult(&result).
  611. Post(HOST + "/v1/" + setting.ProjectID + urlTrainJob + "/" + jobID + "/versions/" + versionID + "/stop")
  612. if err != nil {
  613. return &result, fmt.Errorf("resty StopTrainJob: %v", err)
  614. }
  615. if res.StatusCode() == http.StatusUnauthorized && retry < 1 {
  616. retry++
  617. _ = getToken()
  618. goto sendjob
  619. }
  620. if res.StatusCode() != http.StatusOK {
  621. var temp models.ErrorResult
  622. if err = json.Unmarshal([]byte(res.String()), &temp); err != nil {
  623. log.Error("json.Unmarshal failed(%s): %v", res.String(), err.Error())
  624. return &result, fmt.Errorf("json.Unmarshal failed(%s): %v", res.String(), err.Error())
  625. }
  626. log.Error("StopTrainJob failed(%d):%s(%s)", res.StatusCode(), temp.ErrorCode, temp.ErrorMsg)
  627. return &result, fmt.Errorf("停止训练作业失败(%d):%s(%s)", res.StatusCode(), temp.ErrorCode, temp.ErrorMsg)
  628. }
  629. if !result.IsSuccess {
  630. log.Error("StopTrainJob(%s) failed", jobID)
  631. return &result, fmt.Errorf("停止训练作业失败:%s", result.ErrorMsg)
  632. }
  633. return &result, nil
  634. }