mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-01 15:07:17 +08:00
detect bmagent and hostagent service
This commit is contained in:
@@ -677,26 +677,57 @@ func getUserAuthCookie(ctx context.Context, s *mcclient.ClientSession, token mcc
|
||||
return info.String(), nil
|
||||
}
|
||||
|
||||
func getLBAgentInfo(s *mcclient.ClientSession, token mcclient.TokenCredential) (*jsonutils.JSONDict, error) {
|
||||
func isLBAgentExists(s *mcclient.ClientSession) (bool, error) {
|
||||
params := jsonutils.NewDict()
|
||||
params.Add(jsonutils.NewString("hb_last_seen.isnotempty()"), "filter.0")
|
||||
params.Add(jsonutils.NewInt(1), "limit")
|
||||
params.Add(jsonutils.JSONFalse, "details")
|
||||
lbagents, err := modules.LoadbalancerAgents.List(s, params)
|
||||
agents, err := modules.LoadbalancerAgents.List(s, params)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "user %s get lbagent", token.GetUserName())
|
||||
return false, errors.Wrap(err, "modules.LoadbalancerAgents.List")
|
||||
}
|
||||
|
||||
item := jsonutils.NewDict()
|
||||
item.Add(jsonutils.NewString(""), "name")
|
||||
item.Add(jsonutils.NewString("lbagent"), "type")
|
||||
item.Add(jsonutils.JSONTrue, "as_menu")
|
||||
if len(lbagents.Data) > 0 {
|
||||
item.Add(jsonutils.JSONTrue, "status")
|
||||
if len(agents.Data) > 0 {
|
||||
return true, nil
|
||||
} else {
|
||||
item.Add(jsonutils.JSONFalse, "status")
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
|
||||
func isBaremetalAgentExists(s *mcclient.ClientSession) (bool, error) {
|
||||
params := jsonutils.NewDict()
|
||||
params.Add(jsonutils.NewString("agent_type.equals(baremetal)"), "filter.0")
|
||||
params.Add(jsonutils.NewInt(1), "limit")
|
||||
params.Add(jsonutils.JSONFalse, "details")
|
||||
agents, err := modules.Baremetalagents.List(s, params)
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "modules.LoadbalancerAgents.List")
|
||||
}
|
||||
|
||||
if len(agents.Data) > 0 {
|
||||
return true, nil
|
||||
} else {
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
|
||||
func isHostAgentExists(s *mcclient.ClientSession) (bool, error) {
|
||||
params := jsonutils.NewDict()
|
||||
params.Add(jsonutils.JSONFalse, "show_emulated")
|
||||
params.Add(jsonutils.JSONFalse, "baremetal")
|
||||
params.Add(jsonutils.NewString("system"), "scope")
|
||||
params.Add(jsonutils.NewInt(1), "limit")
|
||||
params.Add(jsonutils.JSONFalse, "details")
|
||||
agents, err := modules.Baremetalagents.List(s, params)
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "modules.LoadbalancerAgents.List")
|
||||
}
|
||||
|
||||
if len(agents.Data) > 0 {
|
||||
return true, nil
|
||||
} else {
|
||||
return false, nil
|
||||
}
|
||||
return item, nil
|
||||
}
|
||||
|
||||
func getUserInfo(ctx context.Context, s *mcclient.ClientSession, token mcclient.TokenCredential, req *http.Request) (*jsonutils.JSONDict, error) {
|
||||
@@ -787,43 +818,53 @@ func getUserInfo(ctx context.Context, s *mcclient.ClientSession, token mcclient.
|
||||
menus := jsonutils.NewArray()
|
||||
k8s := jsonutils.NewArray()
|
||||
|
||||
adminToken := auth.AdminCredential()
|
||||
curReg := FetchRegion(req)
|
||||
allsrv := adminToken.GetInternalServices(curReg)
|
||||
alleps := auth.Client().GetServiceCatalog().GetServicesByInterface(curReg, "console")
|
||||
|
||||
if allsrv != nil && len(allsrv) > 0 {
|
||||
for _, srv := range allsrv {
|
||||
item := jsonutils.NewDict()
|
||||
item.Add(jsonutils.NewString(""), "name")
|
||||
item.Add(jsonutils.NewString(srv), "type")
|
||||
item.Add(jsonutils.JSONTrue, "status")
|
||||
if srv == "notify" {
|
||||
item.Add(jsonutils.JSONFalse, "as_menu")
|
||||
} else {
|
||||
item.Add(jsonutils.JSONTrue, "as_menu")
|
||||
}
|
||||
services.Add(item)
|
||||
}
|
||||
} else {
|
||||
log.Errorf("fail to find services????: %#v %s", adminToken, curReg)
|
||||
srvCat := auth.Client().GetServiceCatalog()
|
||||
var allsrv []string
|
||||
var alleps []mcclient.ExternalService
|
||||
if srvCat != nil {
|
||||
allsrv = srvCat.GetInternalServices(curReg)
|
||||
alleps = srvCat.GetServicesByInterface(curReg, "console")
|
||||
}
|
||||
|
||||
log.Infof("getUserInfo getLBAgentInfo")
|
||||
lb, err := getLBAgentInfo(s, adminToken)
|
||||
if err != nil {
|
||||
log.Errorf("getLBAgentInfo fail %s", err)
|
||||
} else {
|
||||
services.Add(lb)
|
||||
log.Infof("getUserInfo checkAgent exists")
|
||||
for _, cf := range []struct {
|
||||
existFunc func(*mcclient.ClientSession) (bool, error)
|
||||
srvName string
|
||||
}{
|
||||
{
|
||||
existFunc: isLBAgentExists,
|
||||
srvName: "lbagent",
|
||||
},
|
||||
{
|
||||
existFunc: isBaremetalAgentExists,
|
||||
srvName: "bmagent",
|
||||
},
|
||||
{
|
||||
existFunc: isHostAgentExists,
|
||||
srvName: "hostagent",
|
||||
},
|
||||
} {
|
||||
exist, err := cf.existFunc(s)
|
||||
if err != nil {
|
||||
log.Errorf("isLBAgentExists fail %s", err)
|
||||
} else if exist {
|
||||
allsrv = append(allsrv, cf.srvName)
|
||||
}
|
||||
}
|
||||
|
||||
if alleps != nil {
|
||||
for _, ep := range alleps {
|
||||
item := jsonutils.NewDict()
|
||||
item.Add(jsonutils.NewString(ep.Url), "url")
|
||||
item.Add(jsonutils.NewString(ep.Name), "name")
|
||||
menus.Add(item)
|
||||
}
|
||||
for _, srv := range allsrv {
|
||||
item := jsonutils.NewDict()
|
||||
item.Add(jsonutils.NewString(srv), "type")
|
||||
item.Add(jsonutils.JSONTrue, "status")
|
||||
services.Add(item)
|
||||
}
|
||||
|
||||
for _, ep := range alleps {
|
||||
item := jsonutils.NewDict()
|
||||
item.Add(jsonutils.NewString(ep.Url), "url")
|
||||
item.Add(jsonutils.NewString(ep.Name), "name")
|
||||
menus.Add(item)
|
||||
}
|
||||
|
||||
log.Infof("getUserInfo modules.Hosts.Get")
|
||||
|
||||
@@ -18,5 +18,7 @@ type IServiceCatalog interface {
|
||||
Len() int
|
||||
GetServiceURL(service, region, zone, endpointType string) (string, error)
|
||||
GetServiceURLs(service, region, zone, endpointType string) ([]string, error)
|
||||
GetInternalServices(region string) []string
|
||||
GetExternalServices(region string) []ExternalService
|
||||
GetServicesByInterface(region string, infType string) []ExternalService
|
||||
}
|
||||
|
||||
@@ -87,8 +87,6 @@ type TokenCredential interface {
|
||||
GetServiceCatalog() IServiceCatalog
|
||||
GetCatalogData(serviceTypes []string, region string) jsonutils.JSONObject
|
||||
|
||||
GetInternalServices(region string) []string
|
||||
GetExternalServices(region string) []ExternalService
|
||||
GetEndpoints(region string, endpointType string) []Endpoint
|
||||
|
||||
ToJson() jsonutils.JSONObject
|
||||
|
||||
+12
-4
@@ -217,10 +217,6 @@ func (this *TokenCredentialV2) GetServiceURLs(service, region, zone, endpointTyp
|
||||
return this.ServiceCatalog.GetServiceURLs(service, region, zone, endpointType)
|
||||
}
|
||||
|
||||
func (this *TokenCredentialV2) GetServicesByInterface(region string, infType string) []ExternalService {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *TokenCredentialV2) GetInternalServices(region string) []string {
|
||||
return nil
|
||||
}
|
||||
@@ -229,6 +225,10 @@ func (this *TokenCredentialV2) GetExternalServices(region string) []ExternalServ
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *TokenCredentialV2) GetServicesByInterface(region string, infType string) []ExternalService {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *TokenCredentialV2) GetEndpoints(region string, endpointType string) []Endpoint {
|
||||
return nil
|
||||
}
|
||||
@@ -333,6 +333,14 @@ func (catalog KeystoneServiceCatalogV2) GetServiceURLs(service, region, zone, en
|
||||
return []string{url}, nil
|
||||
}
|
||||
|
||||
func (catalog KeystoneServiceCatalogV2) GetInternalServices(region string) []string {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (catalog KeystoneServiceCatalogV2) GetExternalServices(region string) []ExternalService {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (catalog KeystoneServiceCatalogV2) GetServicesByInterface(region string, infType string) []ExternalService {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -249,16 +249,16 @@ func (this *TokenCredentialV3) GetServiceURLs(service, region, zone, endpointTyp
|
||||
return this.Token.Catalog.GetServiceURLs(service, region, zone, endpointType)
|
||||
}
|
||||
|
||||
func (this *TokenCredentialV3) GetServicesByInterface(region string, infType string) []ExternalService {
|
||||
return this.Token.Catalog.GetServicesByInterface(region, infType)
|
||||
}
|
||||
|
||||
func (this *TokenCredentialV3) GetInternalServices(region string) []string {
|
||||
return this.Token.Catalog.getInternalServices(region)
|
||||
return this.Token.Catalog.GetInternalServices(region)
|
||||
}
|
||||
|
||||
func (this *TokenCredentialV3) GetExternalServices(region string) []ExternalService {
|
||||
return this.Token.Catalog.getExternalServices(region)
|
||||
return this.Token.Catalog.GetExternalServices(region)
|
||||
}
|
||||
|
||||
func (this *TokenCredentialV3) GetServicesByInterface(region string, infType string) []ExternalService {
|
||||
return this.Token.Catalog.GetServicesByInterface(region, infType)
|
||||
}
|
||||
|
||||
func (this *TokenCredentialV3) GetEndpoints(region string, endpointType string) []Endpoint {
|
||||
@@ -277,7 +277,7 @@ func (this *TokenCredentialV3) GetLoginIp() string {
|
||||
return this.Token.Context.Ip
|
||||
}
|
||||
|
||||
func (catalog KeystoneServiceCatalogV3) getInternalServices(region string) []string {
|
||||
func (catalog KeystoneServiceCatalogV3) GetInternalServices(region string) []string {
|
||||
services := make([]string, 0)
|
||||
for i := 0; i < len(catalog); i++ {
|
||||
exit := false
|
||||
@@ -295,7 +295,7 @@ func (catalog KeystoneServiceCatalogV3) getInternalServices(region string) []str
|
||||
return services
|
||||
}
|
||||
|
||||
func (catalog KeystoneServiceCatalogV3) getExternalServices(region string) []ExternalService {
|
||||
func (catalog KeystoneServiceCatalogV3) GetExternalServices(region string) []ExternalService {
|
||||
return catalog.GetServicesByInterface(region, "console")
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user