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