145 lines
4.1 KiB
Go
145 lines
4.1 KiB
Go
package cron
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"github.com/go-resty/resty/v2"
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
coreClient "gitlink.org.cn/JointCloud/pcm-coordinator/api/client"
|
|
"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils/httputils"
|
|
"gitlink.org.cn/JointCloud/pcm-openstack/internal/common"
|
|
"gitlink.org.cn/JointCloud/pcm-openstack/internal/config"
|
|
"k8s.io/apimachinery/pkg/util/json"
|
|
"log"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
var AdapterId int64
|
|
var CoreUrl string
|
|
var C config.Config
|
|
|
|
func PullTaskInfo() {
|
|
|
|
httpClient := resty.New().R()
|
|
result, _ := httpClient.SetHeader("Content-Type", "application/json").
|
|
SetQueryParam("adapterId", strconv.FormatInt(1706858330967773113, 10)).
|
|
Get("http://127.0.0.1:8999" + "/pcm/v1/core/pullTaskInfo")
|
|
|
|
var resp coreClient.PullTaskInfoResp
|
|
err := json.Unmarshal(result.Body(), &resp)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
if resp.VmInfoList != nil && len(resp.VmInfoList) != 0 {
|
|
var oldVmInfoList []coreClient.VmInfo
|
|
CreateServerReq := common.CreateSerReq{}
|
|
|
|
for i, vmInfo := range resp.VmInfoList {
|
|
if vmInfo.Status == "Saved" {
|
|
CreateServerReq.CrServer.Server.Networks = make([]common.Networks, len(resp.VmInfoList))
|
|
CreateServerReq.Platform = vmInfo.Platform
|
|
CreateServerReq.CrServer.Server.Name = vmInfo.VmName
|
|
CreateServerReq.CrServer.Server.FlavorRef = vmInfo.FlavorRef
|
|
CreateServerReq.CrServer.Server.ImageRef = vmInfo.ImageRef
|
|
CreateServerReq.CrServer.Server.Networks[i].Uuid = vmInfo.Uuid
|
|
CreateServerReq.CrServer.Server.MinCount = vmInfo.MinCount
|
|
respServer, err := CreateServer(CreateServerReq)
|
|
if err != nil {
|
|
log.Print(err)
|
|
}
|
|
vmInfo.ServerId = respServer.Server.Id
|
|
oldVmInfoList = append(oldVmInfoList, *vmInfo)
|
|
vmInfo.Status = "Running"
|
|
} else { //if state is not "Saved" ,then get state from participant and sync to core
|
|
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
// push submitted mark to coordinator
|
|
PushReq := coreClient.PushTaskInfoReq{
|
|
AdapterId: 1706858330967773113,
|
|
VmInfoList: resp.VmInfoList,
|
|
}
|
|
|
|
if len(PushReq.VmInfoList) != 0 {
|
|
url := "http://127.0.0.1:8999" + "/pcm/v1/core/pushTaskInfo"
|
|
method := "POST"
|
|
|
|
jsonStr, _ := json.Marshal(PushReq)
|
|
payload := bytes.NewBuffer(jsonStr)
|
|
client := &http.Client{}
|
|
req, _ := http.NewRequest(method, url, payload)
|
|
req.Header.Add("Content-Type", "application/json")
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return
|
|
}
|
|
fmt.Print(resp)
|
|
}
|
|
|
|
}
|
|
|
|
func CreateServer(req common.CreateSerReq) (common.CreateServerResp, error) {
|
|
var resp common.CreateServerResp
|
|
platform, err := common.GetOpenstackConfWithPlatform(req.Platform)
|
|
token := common.GetToken(req.Platform)
|
|
fmt.Println("错误", err)
|
|
reqByte, err := json.Marshal(req.CrServer)
|
|
if err != nil {
|
|
log.Print(err)
|
|
}
|
|
payload := strings.NewReader(string(reqByte))
|
|
openstackUrl := platform.OpenstackComputeUrl
|
|
statusCode, body, err := httputils.HttpClientWithBodyAndCode(httputils.POST, openstackUrl+"/servers", payload, token)
|
|
fmt.Println("返回值", body)
|
|
if err != nil {
|
|
log.Print(err)
|
|
}
|
|
logx.Infof("pcm-openstack call createServer 返回结果! statusCode %s: ,\n body: %s ,\n err: %s", statusCode, body, err)
|
|
if statusCode == 202 {
|
|
err := json.Unmarshal(body, &resp)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
resp.Code = 200
|
|
resp.Msg = "Success"
|
|
} else if statusCode != 202 {
|
|
json.Unmarshal(body, &resp)
|
|
resp.Code = 400
|
|
resp.Msg = "Failure"
|
|
}
|
|
return resp, nil
|
|
}
|
|
|
|
func GetServerDetail(req common.GetServersDetailedByIdReq) (common.GetServersDetailedByIdResp, error) {
|
|
var resp common.GetServersDetailedByIdResp
|
|
platform, err1 := common.GetOpenstackConfWithPlatform(req.Platform)
|
|
openstackUrl := platform.OpenstackComputeUrl
|
|
if err1 != nil {
|
|
log.Print(err1)
|
|
}
|
|
token := common.GetToken(req.Platform)
|
|
statusCode, body, err := httputils.HttpClientWithBodyAndCode(httputils.GET, openstackUrl+"/servers/"+req.ServerId, nil, token)
|
|
if err != nil {
|
|
log.Print(err)
|
|
}
|
|
if statusCode == 200 {
|
|
err := json.Unmarshal(body, &resp)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
resp.Code = 200
|
|
resp.Msg = "Success"
|
|
} else if statusCode != 200 {
|
|
json.Unmarshal(body, &resp)
|
|
resp.Code = 400
|
|
resp.Msg = "Failure"
|
|
}
|
|
return resp, nil
|
|
}
|