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 6.6 kB

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
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
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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. package grampus
  2. import (
  3. "code.gitea.io/gitea/models"
  4. "code.gitea.io/gitea/modules/log"
  5. "code.gitea.io/gitea/modules/setting"
  6. "crypto/tls"
  7. "encoding/json"
  8. "fmt"
  9. "github.com/go-resty/resty/v2"
  10. "net/http"
  11. )
  12. var (
  13. restyClient *resty.Client
  14. HOST string
  15. TOKEN string
  16. )
  17. const (
  18. urlOpenApiV1 = "/openapi/v1/"
  19. urlGetToken = urlOpenApiV1 + "token"
  20. urlTrainJob = urlOpenApiV1 + "trainjob"
  21. urlGetResourceSpecs = urlOpenApiV1 + "resourcespec"
  22. urlGetImages = urlOpenApiV1 + "image"
  23. urlTrainJobConfig = "/training-job-configs"
  24. errorCodeExceedLimit = "ModelArts.0118"
  25. urlNotebook2 = ""
  26. errorIllegalToken = 1005
  27. )
  28. type GetTokenParams struct {
  29. UserName string `json:"username"`
  30. Password string `json:"password"`
  31. }
  32. type GetTokenResult struct {
  33. Token string `json:"token"`
  34. Expiration int64 `json:"expiration"`
  35. }
  36. func getRestyClient() *resty.Client {
  37. if restyClient == nil {
  38. restyClient = resty.New()
  39. restyClient.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
  40. }
  41. return restyClient
  42. }
  43. func checkSetting() {
  44. if len(HOST) != 0 && len(TOKEN) != 0 && restyClient != nil {
  45. return
  46. }
  47. err := getToken()
  48. if err != nil {
  49. log.Error("getToken failed:%v", err)
  50. }
  51. }
  52. func getToken() error {
  53. HOST = setting.Grampus.Host
  54. client := getRestyClient()
  55. params := GetTokenParams{
  56. UserName: setting.Grampus.UserName,
  57. Password: setting.Grampus.Password,
  58. }
  59. var result GetTokenResult
  60. res, err := client.R().
  61. SetHeader("Content-Type", "application/json").
  62. SetBody(params).
  63. SetResult(&result).
  64. Post(HOST + urlGetToken)
  65. if err != nil {
  66. return fmt.Errorf("resty getToken: %v", err)
  67. }
  68. if res.StatusCode() != http.StatusOK {
  69. return fmt.Errorf("getToken failed:%s", res.String())
  70. }
  71. TOKEN = result.Token
  72. log.Info(TOKEN)
  73. return nil
  74. }
  75. func createJob(req models.CreateGrampusJobRequest) (*models.CreateGrampusJobResponse, error) {
  76. checkSetting()
  77. client := getRestyClient()
  78. var result models.CreateGrampusJobResponse
  79. retry := 0
  80. sendjob:
  81. _, err := client.R().
  82. SetHeader("Content-Type", "application/json").
  83. SetAuthToken(TOKEN).
  84. SetBody(req).
  85. SetResult(&result).
  86. Post(HOST + urlTrainJob)
  87. if err != nil {
  88. return nil, fmt.Errorf("resty CreateJob: %s", err)
  89. }
  90. if result.ErrorCode == errorIllegalToken && retry < 1 {
  91. retry++
  92. _ = getToken()
  93. goto sendjob
  94. }
  95. if result.ErrorCode != 0 {
  96. log.Error("CreateJob failed(%d): %s", result.ErrorCode, result.ErrorMsg)
  97. return &result, fmt.Errorf("CreateJob failed(%d): %s", result.ErrorCode, result.ErrorMsg)
  98. }
  99. return &result, nil
  100. }
  101. func GetJob(jobID string) (*models.GetGrampusJobResponse, error) {
  102. checkSetting()
  103. client := getRestyClient()
  104. var result models.GetGrampusJobResponse
  105. log.Info(jobID, TOKEN)
  106. retry := 0
  107. sendjob:
  108. _, err := client.R().
  109. SetAuthToken(TOKEN).
  110. SetResult(&result).
  111. Get(HOST + urlTrainJob + "/" + jobID)
  112. if err != nil {
  113. return nil, fmt.Errorf("resty GetJob: %v", err)
  114. }
  115. if result.ErrorCode == errorIllegalToken && retry < 1 {
  116. retry++
  117. log.Info("retry get token")
  118. _ = getToken()
  119. goto sendjob
  120. }
  121. if result.ErrorCode != 0 {
  122. log.Error("GetJob failed(%d): %s", result.ErrorCode, result.ErrorMsg)
  123. return &result, fmt.Errorf("GetJob failed(%d): %s", result.ErrorCode, result.ErrorMsg)
  124. }
  125. return &result, nil
  126. }
  127. func GetResourceSpecs(processorType string) (*models.GetGrampusResourceSpecsResult, error) {
  128. checkSetting()
  129. client := getRestyClient()
  130. var result models.GetGrampusResourceSpecsResult
  131. retry := 0
  132. sendjob:
  133. _, err := client.R().
  134. SetAuthToken(TOKEN).
  135. SetResult(&result).
  136. Get(HOST + urlGetResourceSpecs + "?processorType=" + processorType)
  137. if err != nil {
  138. return nil, fmt.Errorf("resty GetResourceSpecs: %v", err)
  139. }
  140. if result.ErrorCode == errorIllegalToken && retry < 1 {
  141. retry++
  142. log.Info("retry get token")
  143. _ = getToken()
  144. goto sendjob
  145. }
  146. if result.ErrorCode != 0 {
  147. log.Error("GetResourceSpecs failed(%d): %s", result.ErrorCode, result.ErrorMsg)
  148. return &result, fmt.Errorf("GetResourceSpecs failed(%d): %s", result.ErrorCode, result.ErrorMsg)
  149. }
  150. return &result, nil
  151. }
  152. func GetImages(processorType string) (*models.GetGrampusImagesResult, error) {
  153. checkSetting()
  154. client := getRestyClient()
  155. var result models.GetGrampusImagesResult
  156. retry := 0
  157. sendjob:
  158. _, err := client.R().
  159. SetAuthToken(TOKEN).
  160. SetResult(&result).
  161. Get(HOST + urlGetImages + "?processorType=" + processorType)
  162. if err != nil {
  163. return nil, fmt.Errorf("resty GetImages: %v", err)
  164. }
  165. if result.ErrorCode == errorIllegalToken && retry < 1 {
  166. retry++
  167. log.Info("retry get token")
  168. _ = getToken()
  169. goto sendjob
  170. }
  171. if result.ErrorCode != 0 {
  172. log.Error("GetImages failed(%d): %s", result.ErrorCode, result.ErrorMsg)
  173. return &result, fmt.Errorf("GetImages failed(%d): %s", result.ErrorCode, result.ErrorMsg)
  174. }
  175. return &result, nil
  176. }
  177. func GetTrainJobLog(jobID string) (string, error) {
  178. checkSetting()
  179. client := getRestyClient()
  180. var logContent string
  181. res, err := client.R().
  182. SetAuthToken(TOKEN).
  183. SetResult(&logContent).
  184. Get(HOST + urlTrainJob + "/" + jobID + "/log")
  185. if err != nil {
  186. return logContent, fmt.Errorf("resty GetTrainJobLog: %v", err)
  187. }
  188. if res.StatusCode() != http.StatusOK {
  189. var temp models.GrampusResult
  190. if err = json.Unmarshal([]byte(res.String()), &temp); err != nil {
  191. log.Error("json.Unmarshal failed(%s): %v", res.String(), err.Error())
  192. return logContent, fmt.Errorf("json.Unmarshal failed(%s): %v", res.String(), err.Error())
  193. }
  194. log.Error("GetTrainJobLog failed(%d):%s(%s)", res.StatusCode(), temp.ErrorCode, temp.ErrorMsg)
  195. return logContent, fmt.Errorf("GetTrainJobLog failed(%d):%s(%s)", res.StatusCode(), temp.ErrorCode, temp.ErrorMsg)
  196. }
  197. logContent = res.String()
  198. return logContent, nil
  199. }
  200. func StopJob(jobID string) (*models.GrampusStopJobResponse, error) {
  201. checkSetting()
  202. client := getRestyClient()
  203. var result models.GrampusStopJobResponse
  204. retry := 0
  205. sendjob:
  206. _, err := client.R().
  207. //SetHeader("Content-Type", "application/json").
  208. SetAuthToken(TOKEN).
  209. SetResult(&result).
  210. Post(HOST + urlTrainJob + "/" + jobID + "/stop")
  211. if err != nil {
  212. return &result, fmt.Errorf("resty StopTrainJob: %v", err)
  213. }
  214. if result.ErrorCode == errorIllegalToken && retry < 1 {
  215. retry++
  216. log.Info("retry get token")
  217. _ = getToken()
  218. goto sendjob
  219. }
  220. if result.ErrorCode != 0 {
  221. log.Error("GetJob failed(%d): %s", result.ErrorCode, result.ErrorMsg)
  222. return &result, fmt.Errorf("GetJob failed(%d): %s", result.ErrorCode, result.ErrorMsg)
  223. }
  224. return &result, nil
  225. }