Automatic merge from release/2.6.0 -> release/2.7.0

* commit 'ead2078868214f073c88eabfac206d50a7c609b7':
  创建openstack指定对应的主机
  更改命名方式
  虚拟zone
  获取所有项目
  避免同步相同的host
  过滤zone底下的host
This commit is contained in:
邱剑
2019-03-03 19:28:45 +08:00
5 changed files with 52 additions and 24 deletions
+15 -3
View File
@@ -7,6 +7,7 @@ import (
"yunion.io/x/onecloud/pkg/cloudprovider"
"yunion.io/x/onecloud/pkg/compute/models"
"yunion.io/x/onecloud/pkg/util/version"
"yunion.io/x/pkg/utils"
)
const (
@@ -101,7 +102,7 @@ func (host *SHost) GetIStorageById(id string) (cloudprovider.ICloudStorage, erro
}
func (host *SHost) GetIVMs() ([]cloudprovider.ICloudVM, error) {
instances, err := host.zone.region.GetInstances(host.zone.ZoneName, host.GetName())
instances, err := host.zone.region.GetInstances(host.GetName())
if err != nil {
return nil, err
}
@@ -128,6 +129,17 @@ func (host *SHost) CreateVM(desc *cloudprovider.SManagedVMCreateConfig) (cloudpr
return nil, err
}
zoneName := ""
for zone, hosts := range host.zone.cachedHosts {
if utils.IsInStringArray(host.GetName(), hosts) {
zoneName = zone
break
}
}
if len(zoneName) == 0 {
return nil, fmt.Errorf("failed to find zone info for host %s", host.GetName())
}
secgroups := []map[string]string{}
for _, secgroupId := range desc.ExternalSecgroupIds {
@@ -172,7 +184,7 @@ func (host *SHost) CreateVM(desc *cloudprovider.SManagedVMCreateConfig) (cloudpr
if err != nil {
break
}
_disk, err = host.zone.region.CreateDisk(host.zone.ZoneName, storage.Name, "", disk.SizeGB, disk.Name)
_disk, err = host.zone.region.CreateDisk(zoneName, storage.Name, "", disk.SizeGB, disk.Name)
if err != nil {
break
}
@@ -203,7 +215,7 @@ func (host *SHost) CreateVM(desc *cloudprovider.SManagedVMCreateConfig) (cloudpr
"adminPass": desc.Password,
//"description": desc.Description,
"accessIPv4": desc.IpAddr,
"availability_zone": fmt.Sprintf("%s:%s", host.zone.ZoneName, host.GetName()),
"availability_zone": fmt.Sprintf("%s:%s", zoneName, host.GetName()),
"networks": []map[string]string{
{
"uuid": network.NetworkID,
+4 -6
View File
@@ -125,9 +125,9 @@ func (region *SRegion) GetSecurityGroupsByInstance(instanceId string) ([]Securit
return secgroups, resp.Unmarshal(&secgroups, "security_groups")
}
func (region *SRegion) GetInstances(zoneName string, hostName string) ([]SInstance, error) {
func (region *SRegion) GetInstances(hostName string) ([]SInstance, error) {
_, maxVersion, _ := region.GetVersion("compute")
_, resp, err := region.List("compute", "/servers/detail", maxVersion, nil)
_, resp, err := region.List("compute", "/servers/detail?all_tenants=True", maxVersion, nil)
if err != nil {
return nil, err
}
@@ -136,10 +136,8 @@ func (region *SRegion) GetInstances(zoneName string, hostName string) ([]SInstan
return nil, err
}
for i := 0; i < len(instances); i++ {
if len(zoneName) == 0 || instances[i].AvailabilityZone == zoneName {
if len(hostName) == 0 || hostName == instances[i].Host {
result = append(result, instances[i])
}
if len(hostName) == 0 || hostName == instances[i].Host {
result = append(result, instances[i])
}
}
return result, nil
+11 -4
View File
@@ -193,7 +193,8 @@ func (region *SRegion) GetIZoneById(id string) (cloudprovider.ICloudZone, error)
}
func (region *SRegion) fetchZones() error {
_, resp, err := region.List("compute", "/os-availability-zone", "", jsonutils.NewDict())
zone := &SZone{region: region, ZoneName: region.Name, cachedHosts: map[string][]string{}}
_, resp, err := region.List("compute", "/os-availability-zone/detail", "", jsonutils.NewDict())
if err != nil {
return err
}
@@ -201,14 +202,20 @@ func (region *SRegion) fetchZones() error {
if err := resp.Unmarshal(&zones, "availabilityZoneInfo"); err != nil {
return err
}
region.izones = []cloudprovider.ICloudZone{}
for i := 0; i < len(zones); i++ {
if zones[i].ZoneName == "internal" {
continue
}
zones[i].region = region
region.izones = append(region.izones, &zones[i])
zone.cachedHosts[zones[i].ZoneName] = []string{}
for hostname, hostInfo := range zones[i].Hosts {
for k := range hostInfo {
if k == "nova-compute" {
zone.cachedHosts[zones[i].ZoneName] = append(zone.cachedHosts[zones[i].ZoneName], hostname)
}
}
}
}
region.izones = []cloudprovider.ICloudZone{zone}
return nil
}
+2 -3
View File
@@ -9,11 +9,10 @@ import (
func init() {
type InstanceListOptions struct {
ZoneID string `help:"Zone ID for filter instance list"`
Host string `help:"Host name for filter instance list"`
Host string `help:"Host name for filter instance list"`
}
shellutils.R(&InstanceListOptions{}, "instance-list", "List instances", func(cli *openstack.SRegion, args *InstanceListOptions) error {
instances, err := cli.GetInstances(args.ZoneID, args.Host)
instances, err := cli.GetInstances(args.Host)
if err != nil {
return err
}
+20 -8
View File
@@ -3,6 +3,7 @@ package openstack
import (
"fmt"
"strings"
"time"
"yunion.io/x/jsonutils"
"yunion.io/x/log"
@@ -20,14 +21,23 @@ const (
HYPERVISORS_VERSION = "2.28"
)
type HostState struct {
Available bool
Active bool
UpdatedAt time.Time
}
type SZone struct {
region *SRegion
iwires []cloudprovider.ICloudWire
istorages []cloudprovider.ICloudStorage
ZoneName string
ZoneState ZoneState
ZoneName string
cachedHosts map[string][]string
Hosts map[string]map[string]HostState
}
func (zone *SZone) GetMetadata() *jsonutils.JSONDict {
@@ -47,14 +57,11 @@ func (zone *SZone) GetGlobalId() string {
}
func (zone *SZone) IsEmulated() bool {
return false
return true
}
func (zone *SZone) GetStatus() string {
if zone.ZoneState.Available {
return models.ZONE_ENABLE
}
return models.ZONE_SOLDOUT
return models.ZONE_ENABLE
}
func (zone *SZone) Refresh() error {
@@ -151,6 +158,11 @@ func (zone *SZone) GetIHosts() ([]cloudprovider.ICloudHost, error) {
return nil, err
}
for i := 0; i < len(hosts); i++ {
// 过滤vmware的机器
hypervisor := strings.ToLower(hosts[i].HypervisorType)
if strings.Index(hypervisor, "vmware") != -1 {
continue
}
hosts[i].zone = zone
ihosts = append(ihosts, &hosts[i])
}
@@ -168,7 +180,7 @@ func (zone *SZone) GetIHosts() ([]cloudprovider.ICloudHost, error) {
return nil, err
}
for i := 0; i < len(_hosts); i++ {
if _hosts[i].Zone == zone.ZoneName && _hosts[i].Service == "compute" {
if _hosts[i].Service == "compute" {
host := SHost{HostName: _hosts[i].HostName, Zone: _hosts[i].Zone, zone: zone}
ihosts = append(ihosts, &host)
}