diff --git a/internal/storeLink/octopusHttp/octopusHttp.go b/internal/storeLink/octopusHttp/octopusHttp.go new file mode 100644 index 00000000..aec5b978 --- /dev/null +++ b/internal/storeLink/octopusHttp/octopusHttp.go @@ -0,0 +1,133 @@ +package octopusHttp + +import ( + "context" + "errors" + "fmt" + "gitlink.org.cn/JointCloud/pcm-coordinator/internal/scheduler/schedulers/option" + "gitlink.org.cn/JointCloud/pcm-coordinator/internal/scheduler/service/collector" + "gitlink.org.cn/JointCloud/pcm-coordinator/internal/scheduler/service/inference" + "mime/multipart" +) + +const ( + RESOURCE_POOL = "common-pool" +) + +const ( + NotImplementError = "not implemented" +) + +const ( + MyAlgorithmListUrl = "/api/v1/algorithm/myAlgorithmList" + ResourcespecsUrl = "/api/v1/resource/specs" +) + +type OctopusHttp struct { + host string + platform string + participantId int64 + token *Token +} + +func NewOctopusHttp(name string, id int64, host string, user string, pwd string) *OctopusHttp { + token, err := NewToken(host, user, pwd) + if err != nil { + fmt.Println(err.Error()) + } + return &OctopusHttp{platform: name, participantId: id, host: host, token: token} +} + +// executor +func (o *OctopusHttp) Execute(ctx context.Context, option *option.AiOption, mode int) (interface{}, error) { + //TODO implement me + panic("implement me") +} + +func (o *OctopusHttp) Stop(ctx context.Context, id string) error { + //TODO implement me + panic("implement me") +} + +// collector +func (o *OctopusHttp) GetResourceStats(ctx context.Context) (*collector.ResourceStats, error) { + return nil, nil +} + +func (o *OctopusHttp) GetDatasetsSpecs(ctx context.Context) ([]*collector.DatasetsSpecs, error) { + //TODO implement me + panic("implement me") +} + +func (o *OctopusHttp) GetAlgorithms(ctx context.Context) ([]*collector.Algorithm, error) { + //TODO implement me + panic("implement me") +} + +func (o *OctopusHttp) GetTrainingTaskLog(ctx context.Context, taskId string, instanceNum string) (string, error) { + //TODO implement me + panic("implement me") +} + +func (o *OctopusHttp) GetTrainingTask(ctx context.Context, taskId string) (*collector.Task, error) { + //TODO implement me + panic("implement me") +} + +func (o *OctopusHttp) DownloadAlgorithmCode(ctx context.Context, resourceType string, card string, taskType string, dataset string, algorithm string) (string, error) { + //TODO implement me + panic("implement me") +} + +func (o *OctopusHttp) UploadAlgorithmCode(ctx context.Context, resourceType string, card string, taskType string, dataset string, algorithm string, code string) error { + //TODO implement me + panic("implement me") +} + +func (o OctopusHttp) GetComputeCards(ctx context.Context) ([]string, error) { + //TODO implement me + panic("implement me") +} + +func (o *OctopusHttp) GetUserBalance(ctx context.Context) (float64, error) { + //TODO implement me + panic("implement me") +} + +func (o *OctopusHttp) GetResourceSpecs(ctx context.Context, resrcType string) (*collector.ResourceSpec, error) { + //TODO implement me + panic("implement me") +} + +// inference +func (o *OctopusHttp) GetClusterInferUrl(ctx context.Context, option *option.InferOption) (*inference.ClusterInferUrl, error) { + return nil, errors.New(NotImplementError) +} + +func (o *OctopusHttp) GetInferDeployInstanceList(ctx context.Context) ([]*inference.DeployInstance, error) { + return nil, errors.New(NotImplementError) +} + +func (o *OctopusHttp) StartInferDeployInstance(ctx context.Context, id string) bool { + return false +} + +func (o *OctopusHttp) StopInferDeployInstance(ctx context.Context, id string) bool { + return false +} + +func (o *OctopusHttp) GetInferDeployInstance(ctx context.Context, id string) (*inference.DeployInstance, error) { + return nil, errors.New(NotImplementError) +} + +func (o *OctopusHttp) CreateInferDeployInstance(ctx context.Context, option *option.InferOption) (string, error) { + return "", errors.New(NotImplementError) +} + +func (o *OctopusHttp) CheckModelExistence(ctx context.Context, modelName string, modelType string) bool { + return false +} + +func (o *OctopusHttp) GetImageInferResult(ctx context.Context, url string, file multipart.File, fileName string) (string, error) { + return "", errors.New(NotImplementError) +} diff --git a/internal/storeLink/octopusHttp/token.go b/internal/storeLink/octopusHttp/token.go new file mode 100644 index 00000000..5f87a46a --- /dev/null +++ b/internal/storeLink/octopusHttp/token.go @@ -0,0 +1,110 @@ +package octopusHttp + +import ( + "crypto/tls" + "encoding/json" + "github.com/go-resty/resty/v2" + "time" +) + +const ( + GetToken = "openaiserver/v1/authmanage/token" + Forward_Slash = "/" +) + +type TokenModel struct { + Success bool `json:"success"` + Payload struct { + Token string `json:"token"` + Expiration int `json:"expiration"` + } `json:"payload"` + Error interface{} `json:"error"` +} + +type TokenTimePair struct { + Token string + ExpiredAt time.Time +} + +type Login struct { + Username string `json:"username"` + Password string `json:"password"` +} + +type Token struct { + ip string + user string + pwd string + ttp *TokenTimePair +} + +func NewToken(ip, user, pwd string) (*Token, error) { + login := Login{ + Username: user, + Password: pwd, + } + jsonStr, _ := json.Marshal(login) + tokenUrl := ip + Forward_Slash + GetToken + token, tm, err := generateToken(jsonStr, tokenUrl) + if err != nil { + return nil, err + } + ttp := &TokenTimePair{ + Token: token, + ExpiredAt: tm, + } + return &Token{ttp: ttp, ip: ip, user: user, pwd: pwd}, nil +} + +func (t *Token) update() error { + login := Login{ + Username: t.user, + Password: t.pwd, + } + jsonStr, _ := json.Marshal(login) + tokenUrl := t.ip + Forward_Slash + GetToken + token, tm, err := generateToken(jsonStr, tokenUrl) + if err != nil { + return err + } + ttp := &TokenTimePair{ + Token: token, + ExpiredAt: tm, + } + t.ttp = ttp + return nil +} + +func (t *Token) Get() (string, error) { + if time.Now().After(t.ttp.ExpiredAt) { + err := t.update() + if err != nil { + return "", err + } + } + return t.ttp.Token, nil +} + +func generateToken(jsonStr []byte, tokenUrl string) (string, time.Time, error) { + client := resty.New().SetTimeout(time.Duration(5) * time.Second) + req := client.R() + + var tokenResp TokenModel + + client.SetTLSClientConfig(&tls.Config{ + InsecureSkipVerify: true, + }) + + _, err := req. + SetResult(&tokenResp). + SetHeader("Content-Type", "application/json"). + SetBody(jsonStr). + Post(tokenUrl) + if err != nil { + return "", time.Time{}, err + } + + var d time.Duration + d = time.Second * time.Duration(tokenResp.Payload.Expiration) + return tokenResp.Payload.Token, time.Now().Add(d), nil +} diff --git a/internal/storeLink/openi.go b/internal/storeLink/openi.go index 095cee0d..02868367 100644 --- a/internal/storeLink/openi.go +++ b/internal/storeLink/openi.go @@ -637,7 +637,7 @@ func (o *OpenI) GetTrainingTaskLog(ctx context.Context, taskId string, instanceN codePaths := strings.SplitN(task.Data.Task.Description, FORWARD_SLASH, 3) if len(codePaths) != 3 { - return "", errors.New("failed to stop, openI desc not set") + return "", errors.New("failed to get log, openI desc not set") } repoName := codePaths[0]