yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aws/host.go (about)

     1  // Copyright 2019 Yunion
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package aws
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/pkg/errors"
    21  
    22  	"yunion.io/x/jsonutils"
    23  	"yunion.io/x/log"
    24  
    25  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    26  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    27  	"yunion.io/x/cloudmux/pkg/multicloud"
    28  )
    29  
    30  type SHost struct {
    31  	multicloud.SHostBase
    32  	zone *SZone
    33  }
    34  
    35  func (self *SHost) GetId() string {
    36  	return fmt.Sprintf("%s-%s", self.zone.region.client.cpcfg.Id, self.zone.GetId())
    37  }
    38  
    39  func (self *SHost) GetName() string {
    40  	return fmt.Sprintf("%s-%s", self.zone.region.client.cpcfg.Name, self.zone.GetId())
    41  }
    42  
    43  func (self *SHost) GetGlobalId() string {
    44  	return fmt.Sprintf("%s-%s", self.zone.region.client.cpcfg.Id, self.zone.GetId())
    45  }
    46  
    47  func (self *SHost) GetStatus() string {
    48  	return api.HOST_STATUS_RUNNING
    49  }
    50  
    51  func (self *SHost) Refresh() error {
    52  	return nil
    53  }
    54  
    55  func (self *SHost) IsEmulated() bool {
    56  	return true
    57  }
    58  
    59  func (self *SHost) GetIVMs() ([]cloudprovider.ICloudVM, error) {
    60  	vms := make([]SInstance, 0)
    61  	vms, _, err := self.zone.region.GetInstances(self.zone.ZoneId, nil, len(vms), 50)
    62  	if err != nil {
    63  		return nil, errors.Wrap(err, "GetInstances")
    64  	}
    65  
    66  	ivms := make([]cloudprovider.ICloudVM, len(vms))
    67  	for i := 0; i < len(vms); i += 1 {
    68  		vms[i].host = self
    69  		ivms[i] = &vms[i]
    70  	}
    71  	return ivms, nil
    72  }
    73  
    74  func (self *SHost) GetIVMById(gid string) (cloudprovider.ICloudVM, error) {
    75  	if len(gid) == 0 {
    76  		log.Errorf("GetIVMById guest id is empty")
    77  		return nil, errors.Wrap(cloudprovider.ErrNotFound, "GetIVMById")
    78  	}
    79  
    80  	ivms, _, err := self.zone.region.GetInstances(self.zone.ZoneId, []string{gid}, 0, 1)
    81  	if err != nil {
    82  		return nil, errors.Wrap(err, "GetInstances")
    83  	}
    84  	if len(ivms) == 0 {
    85  		return nil, errors.Wrap(cloudprovider.ErrNotFound, "GetInstances")
    86  	}
    87  	if len(ivms) > 1 {
    88  		return nil, cloudprovider.ErrDuplicateId
    89  	}
    90  	ivms[0].host = self
    91  	return &ivms[0], nil
    92  }
    93  
    94  func (self *SHost) GetIWires() ([]cloudprovider.ICloudWire, error) {
    95  	return self.zone.GetIWires()
    96  }
    97  
    98  func (self *SHost) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
    99  	return self.zone.GetIStorages()
   100  }
   101  
   102  func (self *SHost) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
   103  	return self.zone.GetIStorageById(id)
   104  }
   105  
   106  func (self *SHost) GetEnabled() bool {
   107  	return true
   108  }
   109  
   110  func (self *SHost) GetHostStatus() string {
   111  	return api.HOST_ONLINE
   112  }
   113  
   114  func (self *SHost) GetAccessIp() string {
   115  	return ""
   116  }
   117  
   118  func (self *SHost) GetAccessMac() string {
   119  	return ""
   120  }
   121  
   122  func (self *SHost) GetSysInfo() jsonutils.JSONObject {
   123  	info := jsonutils.NewDict()
   124  	info.Add(jsonutils.NewString(CLOUD_PROVIDER_AWS), "manufacture")
   125  	return info
   126  }
   127  
   128  func (self *SHost) GetSN() string {
   129  	return ""
   130  }
   131  
   132  func (self *SHost) GetCpuCount() int {
   133  	return 0
   134  }
   135  
   136  func (self *SHost) GetNodeCount() int8 {
   137  	return 0
   138  }
   139  
   140  func (self *SHost) GetCpuDesc() string {
   141  	return ""
   142  }
   143  
   144  func (self *SHost) GetCpuMhz() int {
   145  	return 0
   146  }
   147  
   148  func (self *SHost) GetMemSizeMB() int {
   149  	return 0
   150  }
   151  
   152  func (self *SHost) GetStorageSizeMB() int {
   153  	return 0
   154  }
   155  
   156  func (self *SHost) GetStorageType() string {
   157  	return api.DISK_TYPE_HYBRID
   158  }
   159  
   160  func (self *SHost) GetHostType() string {
   161  	return api.HOST_TYPE_AWS
   162  }
   163  
   164  func (self *SHost) GetInstanceById(instanceId string) (*SInstance, error) {
   165  	inst, err := self.zone.region.GetInstance(instanceId)
   166  	if err != nil {
   167  		log.Errorf("GetInstance %s: %s", instanceId, err)
   168  		return nil, errors.Wrap(err, "GetInstance")
   169  	}
   170  	inst.host = self
   171  	return inst, nil
   172  }
   173  
   174  func (self *SHost) CreateVM(desc *cloudprovider.SManagedVMCreateConfig) (cloudprovider.ICloudVM, error) {
   175  	vmId, err := self._createVM(desc.Name, desc.ExternalImageId, desc.SysDisk, desc.InstanceType,
   176  		desc.ExternalNetworkId, desc.IpAddr, desc.Description, desc.Password, desc.DataDisks,
   177  		desc.PublicKey, desc.ExternalSecgroupId, desc.UserData, desc.Tags)
   178  	if err != nil {
   179  		return nil, errors.Wrap(err, "_createVM")
   180  	}
   181  
   182  	vm, err := self.GetInstanceById(vmId)
   183  	if err != nil {
   184  		log.Errorf("GetInstanceById %s: %s", vmId, err)
   185  		return nil, errors.Wrap(err, "GetInstanceById")
   186  	}
   187  
   188  	return vm, err
   189  }
   190  
   191  func (self *SHost) _createVM(name, imgId string, sysDisk cloudprovider.SDiskInfo, instanceType string,
   192  	networkId, ipAddr, desc, passwd string,
   193  	dataDisks []cloudprovider.SDiskInfo, publicKey string, secgroupId string, userData string,
   194  	tags map[string]string,
   195  ) (string, error) {
   196  	// 网络配置及安全组绑定
   197  	net := self.zone.getNetworkById(networkId)
   198  	if net == nil {
   199  		return "", fmt.Errorf("invalid network ID %s", networkId)
   200  	}
   201  
   202  	if net.wire == nil {
   203  		log.Errorf("network's wire is empty")
   204  		return "", fmt.Errorf("network's wire is empty")
   205  	}
   206  
   207  	if net.wire.vpc == nil {
   208  		log.Errorf("wire's vpc is empty")
   209  		return "", fmt.Errorf("wire's vpc is empty")
   210  	}
   211  
   212  	if len(secgroupId) == 0 {
   213  		secgroups, err := net.wire.vpc.GetISecurityGroups()
   214  		if err != nil {
   215  			return "", fmt.Errorf("get security group error %s", err)
   216  		}
   217  
   218  		if len(secgroups) == 0 {
   219  			// aws 默认就已经创建好了一个默认安全组。正常情况下并不需要手动创建
   220  			secId, err := self.zone.region.createDefaultSecurityGroup(net.wire.vpc.VpcId)
   221  			if err != nil {
   222  				return "", fmt.Errorf("no secgroup for vpc and failed to create a default One!!")
   223  			} else {
   224  				secgroupId = secId
   225  			}
   226  		} else {
   227  			secgroupId = secgroups[0].GetId()
   228  		}
   229  	}
   230  	// 同步keypair
   231  	var err error
   232  	keypair := ""
   233  	if len(publicKey) > 0 {
   234  		keypair, err = self.zone.region.syncKeypair(publicKey)
   235  		if err != nil {
   236  			return "", err
   237  		}
   238  	}
   239  
   240  	// 镜像及硬盘配置
   241  	img, err := self.zone.region.GetImage(imgId)
   242  	if err != nil {
   243  		log.Errorf("getiamge %s fail %s", imgId, err)
   244  		return "", err
   245  	}
   246  	if img.Status != ImageStatusAvailable {
   247  		log.Errorf("image %s status %s", imgId, img.Status)
   248  		return "", fmt.Errorf("image not ready")
   249  	}
   250  
   251  	disks := make([]SDisk, len(dataDisks)+1)
   252  	disks[0].Size = img.SizeGB
   253  	if sysDisk.SizeGB > 0 && sysDisk.SizeGB > img.SizeGB {
   254  		disks[0].Size = sysDisk.SizeGB
   255  	}
   256  	disks[0].Category = sysDisk.StorageType
   257  
   258  	for i, dataDisk := range dataDisks {
   259  		disks[i+1].Size = dataDisk.SizeGB
   260  		disks[i+1].Category = dataDisk.StorageType
   261  	}
   262  
   263  	// 创建实例
   264  	if len(instanceType) > 0 {
   265  		log.Debugf("Try instancetype : %s", instanceType)
   266  		vmId, err := self.zone.region.CreateInstance(name, img, instanceType, networkId, secgroupId, self.zone.ZoneId, desc, disks, ipAddr, keypair, userData, tags)
   267  		if err != nil {
   268  			log.Errorf("Failed for %s: %s", instanceType, err)
   269  			return "", fmt.Errorf("Failed to create specification %s.%s", instanceType, err.Error())
   270  		} else {
   271  			return vmId, nil
   272  		}
   273  	}
   274  
   275  	return "", fmt.Errorf("Failed to create, instance type should not be empty")
   276  }
   277  
   278  func (self *SHost) GetIHostNics() ([]cloudprovider.ICloudHostNetInterface, error) {
   279  	return nil, cloudprovider.ErrNotSupported
   280  }
   281  
   282  func (self *SHost) GetIsMaintenance() bool {
   283  	return false
   284  }
   285  
   286  func (self *SHost) GetVersion() string {
   287  	return AWS_API_VERSION
   288  }