pcm-hpc/slurm/token_impl.go

62 lines
1.3 KiB
Go

package slurm
import (
"github.com/go-resty/resty/v2"
"gitlink.org.cn/JointCloud/pcm-hpc/utils"
gossh "golang.org/x/crypto/ssh"
"log"
"strings"
"sync"
)
type token struct {
sync.RWMutex
client *client
options *JobOptions
log log.Logger
}
func newToken(client *client, options *TokenOptions) (*token, error) {
token := &token{
RWMutex: sync.RWMutex{},
client: nil,
options: nil,
log: log.Logger{},
}
return token, nil
}
func (t *token) GetToken(options TokenOptions) string {
cli := utils.Cli{
Addr: t.client.url,
User: t.client.cmdUsername,
Pwd: t.client.password,
}
// 建立连接对象
cl, _ := cli.Connect()
// 退出时关闭连接
defer func(Client *gossh.Client) {
err := Client.Close()
if err != nil {
}
}(cl.Client)
res, _ := cl.Run(`scontrol token username=slurmrestd lifespan=10000`)
arr := strings.Split(res, "=")
t.client.token = strings.Replace(arr[1], "\n", "", -1)
return t.client.token
}
func (t *token) ValidateToken(options TokenOptions) bool {
httpClient := resty.New().R()
result, _ := httpClient.SetHeader("Content-Type", "application/json").
SetHeader("X-SLURM-USER-NAME", t.client.restUsername).
SetHeader("X-SLURM-USER-TOKEN", t.client.token).Get(t.client.url + "/slurm/" + t.client.clientVersion + "/ping")
if len(result.String()) > 0 {
return true
} else {
return false
}
}