forked from JointCloud/pcm-hpc
66 lines
1.7 KiB
Go
66 lines
1.7 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"gitlink.org.cn/jcce-pcm/pcm-slurm/internal/pkg/utils"
|
|
"gitlink.org.cn/jcce-pcm/pcm-slurm/internal/pkg/utils/httputils"
|
|
gossh "golang.org/x/crypto/ssh"
|
|
"strings"
|
|
|
|
"gitlink.org.cn/jcce-pcm/pcm-slurm/internal/svc"
|
|
"gitlink.org.cn/jcce-pcm/pcm-slurm/slurm"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type SyncTokenLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
}
|
|
|
|
func NewSyncTokenLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SyncTokenLogic {
|
|
return &SyncTokenLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
func (l *SyncTokenLogic) SyncToken(in *slurm.SyncTokenReq) (*slurm.SyncTokenResp, error) {
|
|
var resp slurm.SyncTokenResp
|
|
|
|
pingResp := slurm.PingResp{}
|
|
slurmHttpRequest := httputils.GetHttpRequest()
|
|
_, err := slurmHttpRequest.SetHeader(httputils.ContentType, httputils.ApplicationJson).
|
|
SetHeader("X-SLURM-USER-NAME", l.svcCtx.Config.SlurmRestUser).
|
|
SetHeader("X-SLURM-USER-TOKEN", l.svcCtx.Config.SlurmToken).
|
|
SetResult(&pingResp).Get(l.svcCtx.Config.RestUrl + l.svcCtx.Config.Path.Ping)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(pingResp.Pings) > 0 {
|
|
return nil, err
|
|
} else {
|
|
cli := utils.Cli{
|
|
Addr: l.svcCtx.Config.SSH.Url,
|
|
User: l.svcCtx.Config.SSH.Username,
|
|
Pwd: l.svcCtx.Config.SSH.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, "=")
|
|
l.svcCtx.Config.SlurmToken = strings.Replace(arr[1], "\n", "", -1)
|
|
}
|
|
|
|
return &resp, nil
|
|
}
|