|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- package grampus
-
- import (
- "code.gitea.io/gitea/models"
- "code.gitea.io/gitea/modules/log"
- "code.gitea.io/gitea/modules/setting"
- "crypto/tls"
- "encoding/json"
- "fmt"
- "github.com/go-resty/resty/v2"
- "net/http"
- )
-
- var (
- restyClient *resty.Client
- HOST string
- TOKEN string
- )
-
- const (
- urlOpenApiV1 = "/openapi/v1/"
-
- urlGetToken = urlOpenApiV1 + "token"
- urlTrainJob = urlOpenApiV1 + "trainjob"
- urlGetResourceSpecs = urlOpenApiV1 + "resourcespec"
- urlGetImages = urlOpenApiV1 + "image"
-
- errorIllegalToken = 1005
- )
-
- type GetTokenParams struct {
- UserName string `json:"username"`
- Password string `json:"password"`
- }
-
- type GetTokenResult struct {
- Token string `json:"token"`
- Expiration int64 `json:"expiration"`
- }
-
- func getRestyClient() *resty.Client {
- if restyClient == nil {
- restyClient = resty.New()
- restyClient.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
- }
- return restyClient
- }
-
- func checkSetting() {
- if len(HOST) != 0 && len(TOKEN) != 0 && restyClient != nil {
- return
- }
-
- err := getToken()
- if err != nil {
- log.Error("getToken failed:%v", err)
- }
- }
-
- func getToken() error {
- HOST = setting.Grampus.Host
-
- client := getRestyClient()
- params := GetTokenParams{
- UserName: setting.Grampus.UserName,
- Password: setting.Grampus.Password,
- }
-
- var result GetTokenResult
- res, err := client.R().
- SetHeader("Content-Type", "application/json").
- SetBody(params).
- SetResult(&result).
- Post(HOST + urlGetToken)
- if err != nil {
- return fmt.Errorf("resty getToken: %v", err)
- }
-
- if res.StatusCode() != http.StatusOK {
- return fmt.Errorf("getToken failed:%s", res.String())
- }
-
- TOKEN = result.Token
-
- return nil
- }
-
- func createJob(req models.CreateGrampusJobRequest) (*models.CreateGrampusJobResponse, error) {
- checkSetting()
- client := getRestyClient()
- var result models.CreateGrampusJobResponse
-
- retry := 0
-
- sendjob:
- _, err := client.R().
- SetHeader("Content-Type", "application/json").
- SetAuthToken(TOKEN).
- SetBody(req).
- SetResult(&result).
- Post(HOST + urlTrainJob)
-
- if err != nil {
- return nil, fmt.Errorf("resty CreateJob: %s", err)
- }
-
- if result.ErrorCode == errorIllegalToken && retry < 1 {
- retry++
- _ = getToken()
- goto sendjob
- }
-
- if result.ErrorCode != 0 {
- log.Error("CreateJob failed(%d): %s", result.ErrorCode, result.ErrorMsg)
- return &result, fmt.Errorf("CreateJob failed(%d): %s", result.ErrorCode, result.ErrorMsg)
- }
-
- return &result, nil
- }
-
- func GetJob(jobID string) (*models.GetGrampusJobResponse, error) {
- checkSetting()
- client := getRestyClient()
- var result models.GetGrampusJobResponse
-
- retry := 0
-
- sendjob:
- _, err := client.R().
- SetAuthToken(TOKEN).
- SetResult(&result).
- Get(HOST + urlTrainJob + "/" + jobID)
-
- if err != nil {
- return nil, fmt.Errorf("resty GetJob: %v", err)
- }
-
- if result.ErrorCode == errorIllegalToken && retry < 1 {
- retry++
- log.Info("retry get token")
- _ = getToken()
- goto sendjob
- }
-
- if result.ErrorCode != 0 {
- log.Error("GetJob failed(%d): %s", result.ErrorCode, result.ErrorMsg)
- return nil, fmt.Errorf("GetJob failed(%d): %s", result.ErrorCode, result.ErrorMsg)
- }
-
- return &result, nil
- }
-
- func GetResourceSpecs(processorType string) (*models.GetGrampusResourceSpecsResult, error) {
- checkSetting()
- client := getRestyClient()
- var result models.GetGrampusResourceSpecsResult
-
- retry := 0
-
- sendjob:
- _, err := client.R().
- SetAuthToken(TOKEN).
- SetResult(&result).
- Get(HOST + urlGetResourceSpecs + "?processorType=" + processorType)
-
- if err != nil {
- return nil, fmt.Errorf("resty GetResourceSpecs: %v", err)
- }
-
- if result.ErrorCode == errorIllegalToken && retry < 1 {
- retry++
- log.Info("retry get token")
- _ = getToken()
- goto sendjob
- }
-
- if result.ErrorCode != 0 {
- log.Error("GetResourceSpecs failed(%d): %s", result.ErrorCode, result.ErrorMsg)
- return &result, fmt.Errorf("GetResourceSpecs failed(%d): %s", result.ErrorCode, result.ErrorMsg)
- }
-
- return &result, nil
- }
-
- func GetImages(processorType string) (*models.GetGrampusImagesResult, error) {
- checkSetting()
- client := getRestyClient()
- var result models.GetGrampusImagesResult
-
- retry := 0
-
- sendjob:
- _, err := client.R().
- SetAuthToken(TOKEN).
- SetResult(&result).
- Get(HOST + urlGetImages + "?processorType=" + processorType)
-
- if err != nil {
- return nil, fmt.Errorf("resty GetImages: %v", err)
- }
-
- if result.ErrorCode == errorIllegalToken && retry < 1 {
- retry++
- log.Info("retry get token")
- _ = getToken()
- goto sendjob
- }
-
- if result.ErrorCode != 0 {
- log.Error("GetImages failed(%d): %s", result.ErrorCode, result.ErrorMsg)
- return &result, fmt.Errorf("GetImages failed(%d): %s", result.ErrorCode, result.ErrorMsg)
- }
-
- return &result, nil
- }
-
- func GetTrainJobLog(jobID string) (string, error) {
- checkSetting()
- client := getRestyClient()
- var logContent string
-
- res, err := client.R().
- SetAuthToken(TOKEN).
- SetResult(&logContent).
- Get(HOST + urlTrainJob + "/" + jobID + "/task/0/replica/0/log")
-
- if err != nil {
- return logContent, fmt.Errorf("resty GetTrainJobLog: %v", err)
- }
-
- if res.StatusCode() != http.StatusOK {
- var temp models.GrampusResult
- if err = json.Unmarshal([]byte(res.String()), &temp); err != nil {
- log.Error("json.Unmarshal failed(%s): %v", res.String(), err.Error())
- return logContent, fmt.Errorf("json.Unmarshal failed(%s): %v", res.String(), err.Error())
- }
- log.Error("GetTrainJobLog failed(%d):%s(%s)", res.StatusCode(), temp.ErrorCode, temp.ErrorMsg)
- return logContent, fmt.Errorf("GetTrainJobLog failed(%d):%s(%s)", res.StatusCode(), temp.ErrorCode, temp.ErrorMsg)
- }
-
- logContent = res.String()
-
- return logContent, nil
- }
-
- func StopJob(jobID string) (*models.GrampusStopJobResponse, error) {
- checkSetting()
- client := getRestyClient()
- var result models.GrampusStopJobResponse
-
- retry := 0
-
- sendjob:
- _, err := client.R().
- //SetHeader("Content-Type", "application/json").
- SetAuthToken(TOKEN).
- SetResult(&result).
- Post(HOST + urlTrainJob + "/" + jobID + "/stop")
-
- if err != nil {
- return &result, fmt.Errorf("resty StopTrainJob: %v", err)
- }
-
- if result.ErrorCode == errorIllegalToken && retry < 1 {
- retry++
- log.Info("retry get token")
- _ = getToken()
- goto sendjob
- }
-
- if result.ErrorCode != 0 {
- log.Error("GetJob failed(%d): %s", result.ErrorCode, result.ErrorMsg)
- return &result, fmt.Errorf("GetJob failed(%d): %s", result.ErrorCode, result.ErrorMsg)
- }
-
- return &result, nil
- }
|