From 93a52baa19e62ec4238e3b85fd994c02859a8622 Mon Sep 17 00:00:00 2001 From: qiwang <1364512070@qq.com> Date: Fri, 7 Jun 2024 15:00:18 +0800 Subject: [PATCH] Modify the configuration information of OpenStack --- etc/pcmopenstack.yaml | 7 ++-- go.mod | 2 +- go.sum | 8 +++-- internal/common/Entity.go | 25 ------------- internal/common/server.go | 36 +++++++++++++++++++ internal/common/tokenService.go | 64 --------------------------------- internal/pkg/cron/taskcron.go | 35 ++++++++++-------- pcmopenstack.go | 38 +++----------------- 8 files changed, 71 insertions(+), 144 deletions(-) create mode 100644 internal/common/server.go diff --git a/etc/pcmopenstack.yaml b/etc/pcmopenstack.yaml index 16fb9fd..8f52732 100644 --- a/etc/pcmopenstack.yaml +++ b/etc/pcmopenstack.yaml @@ -1,5 +1,6 @@ Name: pcm.openstack.rpc ListenOn: 0.0.0.0:2010 +AdapterId: 1706858330967773113 Timeout: 100000 # 100s,设置rpc服务的响应的超时时间,若超过10s还未返回则结束请求 @@ -40,15 +41,15 @@ openstackConfig: USER: "admin" PASSWORD: "Nudt@1234" DOMAIN: "Default" - TokenUrl: "http://192.168.249.113:35357/v3/auth/tokens?nocatalog" + TokenUrl: "http://192.168.249.113:5000/v3/auth/tokens?nocatalog" Status_created: 201 ProjectName: "admin" TokenHeader: X-Subject-Token AuthMethod: "password" OpenstackNetworkUrl: "http://192.168.249.113:9696" OpenstackImageUrl: "http://192.168.249.113:9292" - OpenstackComputeUrl: "http://192.168.249.113:8774/v2.1/9f6db6c0f4704390b34991faf60c1822" - OpenstackVolumev2Url: "http://192.168.249.113:8776/v2/9f6db6c0f4704390b34991faf60c1822" + OpenstackComputeUrl: "http://192.168.249.113:8774/v2.1/96f6c96fb5d44dc99f0820b723339495" + OpenstackVolumev2Url: "http://192.168.249.113:8776/v2/96f6c96fb5d44dc99f0820b723339495" OpenstackLimitsUrl: "http://192.168.249.113:8774" OpenstackBareMetalUrl: "" Platform: "Nudt_Openstack_T" diff --git a/go.mod b/go.mod index 0802e70..c0e7b78 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/prometheus/common v0.52.2 github.com/robfig/cron/v3 v3.0.1 github.com/zeromicro/go-zero v1.6.3 - gitlink.org.cn/JointCloud/pcm-coordinator v0.0.0-20240526092556-fbc927603839 + gitlink.org.cn/JointCloud/pcm-coordinator v0.0.0-20240607015319-029eeaba931d google.golang.org/grpc v1.63.0 google.golang.org/protobuf v1.33.0 k8s.io/apimachinery v0.29.3 diff --git a/go.sum b/go.sum index 0251aa2..e2e16b8 100644 --- a/go.sum +++ b/go.sum @@ -154,8 +154,12 @@ github.com/yuin/gopher-lua v1.1.0 h1:BojcDhfyDWgU2f2TOzYK/g5p2gxMrku8oupLDqlnSqE github.com/yuin/gopher-lua v1.1.0/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw= github.com/zeromicro/go-zero v1.6.3 h1:OL0NnHD5LdRNDolfcK9vUkJt7K8TcBE3RkzfM8poOVw= github.com/zeromicro/go-zero v1.6.3/go.mod h1:XZL435ZxVi9MSXXtw2MRQhHgx6OoX3++MRMOE9xU70c= -gitlink.org.cn/JointCloud/pcm-coordinator v0.0.0-20240526092556-fbc927603839 h1:Jpbjes+D5jkiNitw2aY625I4xGHy8O4ivZgsBKD0Lk8= -gitlink.org.cn/JointCloud/pcm-coordinator v0.0.0-20240526092556-fbc927603839/go.mod h1:bVCQZ/6Qa5nHzMbQE0oEl75xkUX09Dvs3B2uwVVEpiQ= +gitlink.org.cn/JointCloud/pcm-coordinator v0.0.0-20240606042411-8d2c4ce27e51 h1:ucM/+ORzMI0lOKo8fKpTkFPEtWl/BpUd3KI0vNLLVdM= +gitlink.org.cn/JointCloud/pcm-coordinator v0.0.0-20240606042411-8d2c4ce27e51/go.mod h1:bVCQZ/6Qa5nHzMbQE0oEl75xkUX09Dvs3B2uwVVEpiQ= +gitlink.org.cn/JointCloud/pcm-coordinator v0.0.0-20240607011205-ccb597154a3a h1:LAvMDJK5HiRq14kQARl4U62t64gve9bx0Q/AifvYHWI= +gitlink.org.cn/JointCloud/pcm-coordinator v0.0.0-20240607011205-ccb597154a3a/go.mod h1:bVCQZ/6Qa5nHzMbQE0oEl75xkUX09Dvs3B2uwVVEpiQ= +gitlink.org.cn/JointCloud/pcm-coordinator v0.0.0-20240607015319-029eeaba931d h1:mlFuWBVyaIUHoK7vd1SPZSbrMq69Cci0LeVZU95uCg4= +gitlink.org.cn/JointCloud/pcm-coordinator v0.0.0-20240607015319-029eeaba931d/go.mod h1:bVCQZ/6Qa5nHzMbQE0oEl75xkUX09Dvs3B2uwVVEpiQ= go.etcd.io/etcd/api/v3 v3.5.13 h1:8WXU2/NBge6AUF1K1gOexB6e07NgsN1hXK0rSTtgSp4= go.etcd.io/etcd/api/v3 v3.5.13/go.mod h1:gBqlqkcMMZMVTMm4NDZloEVJzxQOQIls8splbqBDa0c= go.etcd.io/etcd/client/pkg/v3 v3.5.13 h1:RVZSAnWWWiI5IrYAXjQorajncORbS0zI48LQlE2kQWg= diff --git a/internal/common/Entity.go b/internal/common/Entity.go index d1302c4..2decf30 100644 --- a/internal/common/Entity.go +++ b/internal/common/Entity.go @@ -2,31 +2,6 @@ package common import "time" -/*type Auth struct { - Auth struct { - Identity struct { - Methods []string `json:"methods"` - Password struct { - User struct { - Name string `json:"name"` - Password string `json:"password"` - Domain struct { - Name string `json:"name"` - } `json:"domain"` - } `json:"user"` - } `json:"password"` - } `json:"identity"` - Scope struct { - Project struct { - Name string `json:"name"` - Domain struct { - Name string `json:"name"` - } `json:"domain"` - } `json:"project"` - } `json:"scope"` - } `json:"auth"` -}*/ - type Auth struct { Auth struct { Identity struct { diff --git a/internal/common/server.go b/internal/common/server.go new file mode 100644 index 0000000..f143af3 --- /dev/null +++ b/internal/common/server.go @@ -0,0 +1,36 @@ +package common + +type CreateSerReq struct { + Platform string `json:"platform,omitempty"` + CrServer CrServer `json:"crServer,omitempty"` +} + +type CrServer struct { + Server Server `json:"server,omitempty"` +} + +type Server struct { + FlavorRef string `json:"flavorRef,omitempty"` + Name string `json:"name,omitempty"` + Networks []Networks `json:"networks,omitempty"` + ImageRef string `json:"imageRef,omitempty"` + AccessIPv4 string `json:"accessIPv4,omitempty"` + AccessIPv6 string `json:"accessIPv6,omitempty"` + AdminPass string `json:"adminPass,omitempty"` + MinCount int64 `json:"min_count,omitempty"` +} + +type Networks struct { + Uuid string `json:"uuid,omitempty"` +} + +type CreateServerResp struct { + Code int64 `json:"code"` + Msg string `json:"msg"` + Data string `json:"data"` + Server ServerResp `json:"server,omitempty"` +} + +type ServerResp struct { + Id string `json:"id,omitempty"` +} diff --git a/internal/common/tokenService.go b/internal/common/tokenService.go index d34cc1d..024ef3c 100644 --- a/internal/common/tokenService.go +++ b/internal/common/tokenService.go @@ -14,76 +14,12 @@ import ( var FileName string var C config.Config -//var ( -// token, expiredAt = GenerateToken() -//) - var ( token string expiredAt time.Time platform string ) -/*func GenerateToken() (string, string, time.Time) { - a := Auth{} - var ( - TokenUrl string - ) - switch Platform { - case C.OpenstackConfig.ZhiJiang.Platform: - a.Auth.Scope.Project.Name = C.OpenstackConfig.ZhiJiang.USER - a.Auth.Scope.Project.Domain.Id = C.OpenstackConfig.ZhiJiang.Id - a.Auth.Identity.Methods = append(a.Auth.Identity.Methods, C.OpenstackConfig.ZhiJiang.AuthMethod) - a.Auth.Identity.Password.User.Name = C.OpenstackConfig.ZhiJiang.USER - a.Auth.Identity.Password.User.Password = C.OpenstackConfig.ZhiJiang.PASSWORD - a.Auth.Identity.Password.User.Domain.Name = C.OpenstackConfig.ZhiJiang.DOMAIN - TokenUrl = C.OpenstackConfig.ZhiJiang.TokenUrl - case C.OpenstackConfig.Nudt.Platform: - a.Auth.Scope.Project.Name = C.OpenstackConfig.Nudt.USER - a.Auth.Scope.Project.Domain.Id = C.OpenstackConfig.Nudt.Id - a.Auth.Identity.Methods = append(a.Auth.Identity.Methods, C.OpenstackConfig.Nudt.AuthMethod) - a.Auth.Identity.Password.User.Name = C.OpenstackConfig.Nudt.USER - a.Auth.Identity.Password.User.Password = C.OpenstackConfig.Nudt.PASSWORD - a.Auth.Identity.Password.User.Domain.Name = C.OpenstackConfig.Nudt.DOMAIN - TokenUrl = C.OpenstackConfig.Nudt.TokenUrl - default: - //return nil, fmt.Errorf("platform %s not supported", Platform) - } - jsonStr, _ := json.Marshal(a) - req_url, err := http.NewRequest("POST", TokenUrl, bytes.NewBuffer(jsonStr)) - - if err != nil { - log.Fatal(err) - } - - c := http.Client{Timeout: time.Duration(3) * time.Second} - - respUrl, err := c.Do(req_url) - if err != nil { - log.Fatal(err) - } - - if respUrl.StatusCode != C.OpenstackConfig.Status_created { - panic("获取token失败") - } - - defer respUrl.Body.Close() - - var t Token - - result, _ := io.ReadAll(respUrl.Body) - json.Unmarshal(result, &t) - - return Platform, respUrl.Header.Get(C.OpenstackConfig.TokenHeader), t.Token.ExpiresAt -}*/ - -/*func GetToken() string { - if time.Now().After(expiredAt) { - Platform, token, expiredAt = GenerateToken() - } - return token -}*/ - func GetOpenstackConfWithPlatform(platform string) (*config.Conf, error) { var conf config.Conf switch platform { diff --git a/internal/pkg/cron/taskcron.go b/internal/pkg/cron/taskcron.go index 3fc6ca8..6dc3758 100644 --- a/internal/pkg/cron/taskcron.go +++ b/internal/pkg/cron/taskcron.go @@ -1,13 +1,14 @@ 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/openstack" + "gitlink.org.cn/JointCloud/pcm-openstack/internal/config" "k8s.io/apimachinery/pkg/util/json" "log" "net/http" @@ -17,16 +18,16 @@ import ( 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(AdapterId, 10)). - Get(CoreUrl + "/pcm/v1/core/pullTaskInfo") + SetQueryParam("adapterId", strconv.FormatInt(1706858330967773113, 10)). + Get("http://127.0.0.1:8999" + "/pcm/v1/core/pullTaskInfo") var resp coreClient.PullTaskInfoResp - //var respserver openstack.CreateServerResp err := json.Unmarshal(result.Body(), &resp) if err != nil { return @@ -34,14 +35,17 @@ func PullTaskInfo() { if resp.VmInfoList != nil && len(resp.VmInfoList) != 0 { var oldVmInfoList []coreClient.VmInfo - CreateServerReq := &openstack.CreateServerReq{} + 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.NetworkUuid + CreateServerReq.CrServer.Server.Networks[i].Uuid = vmInfo.Uuid + CreateServerReq.CrServer.Server.MinCount = vmInfo.MinCount respServer, err := CreateServer(CreateServerReq) if err != nil { log.Print(err) @@ -58,42 +62,43 @@ func PullTaskInfo() { // push submitted mark to coordinator PushReq := coreClient.PushTaskInfoReq{ - AdapterId: AdapterId, + AdapterId: 1706858330967773113, VmInfoList: resp.VmInfoList, } if len(PushReq.VmInfoList) != 0 { - url := CoreUrl + "/pcm/v1/core/pushTaskInfo" + url := "http://127.0.0.1:8999" + "/pcm/v1/core/pushTaskInfo" method := "POST" jsonStr, _ := json.Marshal(PushReq) - payload := strings.NewReader(string(jsonStr)) + payload := bytes.NewBuffer(jsonStr) client := &http.Client{} req, _ := http.NewRequest(method, url, payload) req.Header.Add("Content-Type", "application/json") - _, err := client.Do(req) + resp, err := client.Do(req) if err != nil { return } + fmt.Print(resp) } } -func CreateServer(req *openstack.CreateServerReq) (*openstack.CreateServerResp, error) { - var resp openstack.CreateServerResp +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 { - return nil, err + 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 { - return nil, err + log.Print(err) } logx.Infof("pcm-openstack call createServer 返回结果! statusCode %s: ,\n body: %s ,\n err: %s", statusCode, body, err) if statusCode == 202 { @@ -108,5 +113,5 @@ func CreateServer(req *openstack.CreateServerReq) (*openstack.CreateServerResp, resp.Code = 400 resp.Msg = "Failure" } - return &resp, nil + return resp, nil } diff --git a/pcmopenstack.go b/pcmopenstack.go index 2e3b894..75b8771 100644 --- a/pcmopenstack.go +++ b/pcmopenstack.go @@ -1,25 +1,17 @@ package main import ( - "context" "flag" "github.com/zeromicro/go-zero/core/conf" - "github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/service" "github.com/zeromicro/go-zero/zrpc" - "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils" - "gitlink.org.cn/JointCloud/pcm-coordinator/rpc/client/participantservice" - "gitlink.org.cn/JointCloud/pcm-coordinator/rpc/pcmCore" "gitlink.org.cn/JointCloud/pcm-openstack/internal/common" - "gitlink.org.cn/JointCloud/pcm-openstack/internal/config" "gitlink.org.cn/JointCloud/pcm-openstack/internal/pkg/cron" "gitlink.org.cn/JointCloud/pcm-openstack/internal/server" "gitlink.org.cn/JointCloud/pcm-openstack/internal/svc" "gitlink.org.cn/JointCloud/pcm-openstack/openstack" - "google.golang.org/grpc" "google.golang.org/grpc/reflection" - "strconv" ) // var configFile = flag.String("f", "etc/pcmopenstack.yaml", "the config file") @@ -30,9 +22,6 @@ func main() { conf.MustLoad(common.FileName, &common.C) ctx := svc.NewServiceContext(common.C) //解析业务配置 - //var c config.Config - //conf.MustLoad(*configFile, &c) - //ctx := svc.NewServiceContext(c) s := zrpc.MustNewServer(common.C.RpcServerConf, func(grpcServer *grpc.Server) { openstack.RegisterOpenstackServer(grpcServer, server.NewOpenstackServer(ctx)) if common.C.Mode == service.DevMode || common.C.Mode == service.TestMode { @@ -44,32 +33,13 @@ func main() { ctx.Cron.Start() cron.AddCronGroup(ctx) // 推送p端静态信息 - PushParticipantInfo(ctx.Config, ctx.ParticipantRpc) - logx.Infof("Starting rpc server at %s...\n", common.C.ListenOn) + // PushParticipantInfo(ctx.Config, ctx.ParticipantRpc) + // logx.Infof("Starting rpc server at %s...\n", common.C.ListenOn) s.Start() - //var bootstrapConfig commonConfig.BootstrapConfig - //nacosConfig := bootstrapConfig.NacosConfig - /* serviceConfigContent := nacosConfig.InitConfig(func(data string) { - err := conf.LoadFromYamlBytes([]byte(data), &c) - if err != nil { - panic(err) - } - })*/ - //err := conf.LoadFromYamlBytes([]byte(serviceConfigContent), &c) - /*if err != nil { - panic(err) - }*/ - // start log component - //logx.MustSetup(c.LogConf) - // 注册到nacos - // nacosConfig.Discovery(&c.RpcServerConf) - //rpc log - //s.AddUnaryInterceptors(rpcserver.LoggerInterceptor) - //logic.InitCron(ctx) } -// PushParticipantInfo 推送p端静态信息 +/*// PushParticipantInfo 推送p端静态信息 func PushParticipantInfo(config config.Config, participantService participantservice.ParticipantService) { participantId, err := utils.GetParticipantId(*&common.FileName) if err != nil { @@ -96,4 +66,4 @@ func PushParticipantInfo(config config.Config, participantService participantser // 更新本地配置文件ParticipantId utils.UpdateParticipantId(*&common.FileName, strconv.FormatInt(resp.ParticipantId, 10)) -} +}*/