mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-10-28 03:23:25 +08:00
security sync
This commit is contained in:
@@ -57,7 +57,7 @@ type SStoragecachedimage struct {
|
||||
StoragecacheId string `width:"36" charset:"ascii" nullable:"false" list:"admin" create:"admin_required" key_index:"true"`
|
||||
CachedimageId string `width:"36" charset:"ascii" nullable:"false" list:"admin" create:"admin_required" key_index:"true"`
|
||||
|
||||
ExternalId string `width:"128" charset:"ascii" nullable:"false" get:"admin"`
|
||||
ExternalId string `width:"256" charset:"ascii" nullable:"false" get:"admin"`
|
||||
|
||||
Status string `width:"32" charset:"ascii" nullable:"false" default:"init" list:"admin" update:"admin" create:"admin_required"` // = Column(VARCHAR(32, charset='ascii'), nullable=False,
|
||||
Path string `width:"256" charset:"utf8" nullable:"true" list:"admin" update:"admin" create:"admin_optional"` // = Column(VARCHAR(256, charset='utf8'), nullable=True)
|
||||
|
||||
@@ -178,7 +178,9 @@ func (self *SRegion) GetDisks() ([]SDisk, error) {
|
||||
}
|
||||
|
||||
func (self *SDisk) GetMetadata() *jsonutils.JSONDict {
|
||||
return nil
|
||||
data := jsonutils.NewDict()
|
||||
data.Add(jsonutils.NewString(models.HYPERVISOR_AZURE), "hypervisor")
|
||||
return data
|
||||
}
|
||||
|
||||
func (self *SDisk) GetStatus() string {
|
||||
|
||||
@@ -130,10 +130,7 @@ func (region *SRegion) GetEip(eipId string) (*SEipAddress, error) {
|
||||
}
|
||||
|
||||
func (self *SEipAddress) Associate(instanceId string) error {
|
||||
if err := self.region.AssociateEip(self.ID, instanceId); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
return self.region.AssociateEip(self.ID, instanceId)
|
||||
}
|
||||
|
||||
func (region *SRegion) AssociateEip(eipId string, instanceId string) error {
|
||||
@@ -162,12 +159,12 @@ func (region *SRegion) AssociateEip(eipId string, instanceId string) error {
|
||||
params := network.Interface{
|
||||
Location: ®ion.Name,
|
||||
InterfacePropertiesFormat: &network.InterfacePropertiesFormat{
|
||||
IPConfigurations: &InterfaceIPConfiguration,
|
||||
NetworkSecurityGroup: &network.SecurityGroup{},
|
||||
IPConfigurations: &InterfaceIPConfiguration,
|
||||
},
|
||||
}
|
||||
if len(nic.Properties.NetworkSecurityGroup.ID) > 0 {
|
||||
params.InterfacePropertiesFormat.NetworkSecurityGroup.ID = &nic.Properties.NetworkSecurityGroup.ID
|
||||
networkSecurityGroup := network.SecurityGroup{ID: &nic.Properties.NetworkSecurityGroup.ID}
|
||||
params.InterfacePropertiesFormat.NetworkSecurityGroup = &networkSecurityGroup
|
||||
}
|
||||
_, resourceGroup, nicName := pareResourceGroupWithName(nic.ID, NIC_RESOURCE)
|
||||
if result, err := interfaceClinet.CreateOrUpdate(context.Background(), resourceGroup, nicName, params); err != nil {
|
||||
@@ -190,7 +187,7 @@ func (region *SRegion) GetIEipById(eipId string) (cloudprovider.ICloudEIP, error
|
||||
}
|
||||
|
||||
func (self *SEipAddress) ChangeBandwidth(bw int) error {
|
||||
return cloudprovider.ErrNotImplemented
|
||||
return cloudprovider.ErrNotSupported
|
||||
}
|
||||
|
||||
func (self *SEipAddress) Delete() error {
|
||||
|
||||
@@ -151,7 +151,7 @@ func (self *SHost) _createVM(name string, imgId string, sysDiskSize int, cpu int
|
||||
}
|
||||
|
||||
params := compute.VirtualMachine{Location: &self.zone.region.Name, Name: &name, VirtualMachineProperties: &properties}
|
||||
log.Debugf("Create instance params: %s", jsonutils.Marshal(params).PrettyString())
|
||||
//log.Debugf("Create instance params: %s", jsonutils.Marshal(params).PrettyString())
|
||||
for _, profile := range self.zone.region.getHardwareProfile(cpu, memMB) {
|
||||
params.HardwareProfile.VMSize = compute.VirtualMachineSizeTypes(profile)
|
||||
log.Debugf("Try HardwareProfile : %s", profile)
|
||||
|
||||
@@ -93,13 +93,10 @@ func (self *SImage) GetStatus() string {
|
||||
switch self.Properties.ProvisioningState {
|
||||
case "created":
|
||||
return models.IMAGE_STATUS_QUEUED
|
||||
// case ImageStatusAvailable:
|
||||
// return models.IMAGE_STATUS_ACTIVE
|
||||
// case ImageStatusUnAvailable:
|
||||
// return models.IMAGE_STATUS_DELETED
|
||||
// case ImageStatusCreateFailed:
|
||||
// return models.IMAGE_STATUS_KILLED
|
||||
case "Succeeded":
|
||||
return models.IMAGE_STATUS_ACTIVE
|
||||
default:
|
||||
log.Errorf("Unknow image status: %s", self.Properties.ProvisioningState)
|
||||
return models.IMAGE_STATUS_KILLED
|
||||
}
|
||||
}
|
||||
|
||||
@@ -637,13 +637,6 @@ func (self *SInstance) DeleteVM() error {
|
||||
if err := self.host.zone.region.DeleteVM(self.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
// if osDisk, err := self.getOsDisk(); err != nil {
|
||||
// return err
|
||||
// } else if err := osDisk.Delete(); err != nil {
|
||||
// if err != cloudprovider.ErrNotFound {
|
||||
// return err
|
||||
// }
|
||||
// }
|
||||
if nics, err := self.getNics(); err != nil {
|
||||
return err
|
||||
} else {
|
||||
|
||||
@@ -111,7 +111,6 @@ func (self *SNetwork) GetIpStart() string {
|
||||
}
|
||||
|
||||
func (self *SNetwork) GetIsPublic() bool {
|
||||
// return self.IsDefault
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
+25
-27
@@ -32,13 +32,12 @@ type SRegion struct {
|
||||
|
||||
storageCache *SStoragecache
|
||||
|
||||
vmSize map[string]SVMSize
|
||||
ID string
|
||||
SubscriptionID string
|
||||
Name string
|
||||
DisplayName string
|
||||
Latitude string
|
||||
Longitude string
|
||||
Latitude float32
|
||||
Longitude float32
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
@@ -51,44 +50,43 @@ func (self *SRegion) GetClient() *SAzureClient {
|
||||
return self.client
|
||||
}
|
||||
|
||||
func (self *SRegion) fetchVMSize() error {
|
||||
func (self *SRegion) GetVMSize() (map[string]SVMSize, error) {
|
||||
computeClient := compute.NewVirtualMachineSizesClientWithBaseURI(self.client.baseUrl, self.client.subscriptionId)
|
||||
computeClient.Authorizer = self.client.authorizer
|
||||
if vmSizeList, err := computeClient.List(context.Background(), self.Name); err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
} else {
|
||||
self.vmSize = make(map[string]SVMSize, len(*vmSizeList.Value))
|
||||
vmSizes := make(map[string]SVMSize, len(*vmSizeList.Value))
|
||||
for _, _vmSize := range *vmSizeList.Value {
|
||||
vmSize := SVMSize{}
|
||||
jsonutils.Update(&vmSize, _vmSize)
|
||||
self.vmSize[*_vmSize.Name] = vmSize
|
||||
if err := jsonutils.Update(&vmSize, _vmSize); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
vmSizes[*_vmSize.Name] = vmSize
|
||||
}
|
||||
}
|
||||
return vmSizes, nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *SRegion) getHardwareProfile(cpu, memMB int) []string {
|
||||
if self.vmSize == nil || len(self.vmSize) == 0 {
|
||||
if err := self.fetchVMSize(); err != nil {
|
||||
return []string{}
|
||||
if vmSizes, err := self.GetVMSize(); err != nil {
|
||||
return []string{}
|
||||
} else {
|
||||
profiles := make([]string, 0)
|
||||
for vmSize, info := range vmSizes {
|
||||
if info.MemoryInMB == int32(memMB) && info.NumberOfCores == int32(cpu) {
|
||||
profiles = append(profiles, vmSize)
|
||||
}
|
||||
}
|
||||
return profiles
|
||||
}
|
||||
profiles := make([]string, 0)
|
||||
for vmSize, info := range self.vmSize {
|
||||
if info.MemoryInMB == int32(memMB) && info.NumberOfCores == int32(cpu) {
|
||||
profiles = append(profiles, vmSize)
|
||||
}
|
||||
}
|
||||
return profiles
|
||||
}
|
||||
|
||||
func (self *SRegion) getVMSize(size string) (*SVMSize, error) {
|
||||
if self.vmSize == nil || len(self.vmSize) == 0 {
|
||||
if err := self.fetchVMSize(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if vmSize, ok := self.vmSize[size]; !ok {
|
||||
if vmSizes, err := self.GetVMSize(); err != nil {
|
||||
return nil, err
|
||||
} else if vmSize, ok := vmSizes[size]; !ok {
|
||||
return nil, cloudprovider.ErrNotFound
|
||||
} else {
|
||||
return &vmSize, nil
|
||||
@@ -120,11 +118,11 @@ func (self *SRegion) GetProvider() string {
|
||||
}
|
||||
|
||||
func (self *SRegion) GetLatitude() float32 {
|
||||
return 0.0
|
||||
return self.Latitude
|
||||
}
|
||||
|
||||
func (self *SRegion) GetLongitude() float32 {
|
||||
return 0.0
|
||||
return self.Longitude
|
||||
}
|
||||
|
||||
func (self *SRegion) GetStatus() string {
|
||||
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
"strings"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/log"
|
||||
"yunion.io/x/onecloud/pkg/cloudprovider"
|
||||
"yunion.io/x/pkg/util/secrules"
|
||||
"yunion.io/x/pkg/utils"
|
||||
@@ -74,6 +73,7 @@ func (v SecurityRulesSet) Less(i, j int) bool {
|
||||
}
|
||||
|
||||
type Interface struct {
|
||||
ID string
|
||||
}
|
||||
|
||||
type SecurityGroupPropertiesFormat struct {
|
||||
@@ -375,7 +375,7 @@ func (region *SRegion) updateSecurityGroupRules(secgroupId string, rules []secru
|
||||
SecurityRules: &securityRules,
|
||||
},
|
||||
}
|
||||
log.Debugf("Update SecurityGroup rules: %s", jsonutils.Marshal(params).PrettyString())
|
||||
//log.Debugf("Update SecurityGroup rules: %s", jsonutils.Marshal(params).PrettyString())
|
||||
if result, err := secClient.CreateOrUpdate(context.Background(), resourceGroup, secName, params); err != nil {
|
||||
return "", err
|
||||
} else if err := result.WaitForCompletion(context.Background(), secClient.Client); err != nil {
|
||||
@@ -384,6 +384,57 @@ func (region *SRegion) updateSecurityGroupRules(secgroupId string, rules []secru
|
||||
return secgroupId, nil
|
||||
}
|
||||
|
||||
func (region *SRegion) AttachSecurityToInterfaces(secgroupId string, nicIds []string) error {
|
||||
_, resourceGroup, secName := pareResourceGroupWithName(secgroupId, SECGRP_RESOURCE)
|
||||
if secgroup, err := region.GetSecurityGroupDetails(secgroupId); err != nil {
|
||||
return err
|
||||
} else {
|
||||
networkInterfaces := []network.Interface{}
|
||||
interfaceIds := []string{}
|
||||
for i := 0; i < len(secgroup.Properties.NetworkInterfaces); i++ {
|
||||
networkInterfaces = append(networkInterfaces, network.Interface{
|
||||
ID: &secgroup.Properties.NetworkInterfaces[i].ID,
|
||||
})
|
||||
interfaceIds = append(interfaceIds, secgroup.Properties.NetworkInterfaces[i].ID)
|
||||
}
|
||||
for _, nicId := range nicIds {
|
||||
if nic, err := region.GetNetworkInterfaceDetail(nicId); err != nil {
|
||||
return err
|
||||
} else if !utils.IsInStringArray(nic.ID, interfaceIds) {
|
||||
networkInterfaces = append(networkInterfaces, network.Interface{
|
||||
ID: &nic.ID,
|
||||
})
|
||||
}
|
||||
}
|
||||
secClient := network.NewSecurityGroupsClientWithBaseURI(region.client.baseUrl, region.SubscriptionID)
|
||||
secClient.Authorizer = region.client.authorizer
|
||||
params := network.SecurityGroup{
|
||||
Location: &secgroup.Location,
|
||||
SecurityGroupPropertiesFormat: &network.SecurityGroupPropertiesFormat{
|
||||
NetworkInterfaces: &networkInterfaces,
|
||||
},
|
||||
}
|
||||
if result, err := secClient.CreateOrUpdate(context.Background(), resourceGroup, secName, params); err != nil {
|
||||
return err
|
||||
} else if err := result.WaitForCompletion(context.Background(), secClient.Client); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (region *SRegion) AssiginSecurityGroup(instanceId, secgroupId string) error {
|
||||
if instance, err := region.GetInstance(instanceId); err != nil {
|
||||
return err
|
||||
} else {
|
||||
nicIds := []string{}
|
||||
for _, nic := range instance.Properties.NetworkProfile.NetworkInterfaces {
|
||||
nicIds = append(nicIds, nic.ID)
|
||||
}
|
||||
return region.AttachSecurityToInterfaces(secgroupId, nicIds)
|
||||
}
|
||||
}
|
||||
|
||||
func (self *SRegion) syncSecgroupRules(secgroupId string, rules []secrules.SecurityRule) (string, error) {
|
||||
if secgroup, err := self.GetSecurityGroupDetails(secgroupId); err != nil {
|
||||
return "", err
|
||||
@@ -427,10 +478,8 @@ func (self *SRegion) syncSecgroupRules(secgroupId string, rules []secrules.Secur
|
||||
|
||||
func (self *SRegion) syncSecurityGroup(secgroupId, name string, rules []secrules.SecurityRule) (string, error) {
|
||||
if secgroup, err := self.checkSecurityGroup(name, secgroupId); err != nil {
|
||||
log.Errorf("check err: %v", err)
|
||||
return "", err
|
||||
} else {
|
||||
|
||||
return self.syncSecgroupRules(secgroup.ID, rules)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,20 @@ func init() {
|
||||
}
|
||||
})
|
||||
|
||||
type InstanceSizeListOptions struct {
|
||||
Limit int `help:"page size"`
|
||||
Offset int `help:"page offset"`
|
||||
}
|
||||
|
||||
shellutils.R(&InstanceSizeListOptions{}, "instance-size-list", "List intances", func(cli *azure.SRegion, args *InstanceSizeListOptions) error {
|
||||
if vmSize, err := cli.GetVMSize(); err != nil {
|
||||
return err
|
||||
} else {
|
||||
printObject(vmSize)
|
||||
return nil
|
||||
}
|
||||
})
|
||||
|
||||
type InstanceCrateOptions struct {
|
||||
NAME string `help:"name of instance"`
|
||||
IMAGE string `help:"image ID"`
|
||||
@@ -108,4 +122,13 @@ func init() {
|
||||
shellutils.R(&InstanceDeployOptions{}, "instance-reset-password", "Reset intance password", func(cli *azure.SRegion, args *InstanceDeployOptions) error {
|
||||
return cli.DeployVM(args.ID, "", args.Password, args.PublicKey, false, "")
|
||||
})
|
||||
|
||||
type InstanceSecurityGroupOptions struct {
|
||||
ID string `help:"Instance ID"`
|
||||
SecurityGroup string `help:"Security Group ID or Name"`
|
||||
}
|
||||
|
||||
shellutils.R(&InstanceSecurityGroupOptions{}, "instance-assign-secgrp", "Attach a disk to intance", func(cli *azure.SRegion, args *InstanceSecurityGroupOptions) error {
|
||||
return cli.AssiginSecurityGroup(args.ID, args.SecurityGroup)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -106,8 +106,8 @@ func (self *SStoragecache) uploadImage(userCred mcclient.TokenCredential, imageI
|
||||
if !strings.HasSuffix(imageNameOnBlob, ".vhd") {
|
||||
imageNameOnBlob = fmt.Sprintf("%s.vhd", imageNameOnBlob)
|
||||
}
|
||||
tmpFile := fmt.Sprintf("/tmp/%s", imageNameOnBlob)
|
||||
|
||||
tmpFile := fmt.Sprintf("/opt/cloud/workspace/data/glance/image-cache/%s", imageNameOnBlob)
|
||||
defer os.Remove(tmpFile)
|
||||
f, err := os.Create(tmpFile)
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -129,7 +129,6 @@ func (self *SStoragecache) uploadImage(userCred mcclient.TokenCredential, imageI
|
||||
size, _ := meta.Int("size")
|
||||
|
||||
blobURI, err := self.region.UploadVHD(storageAccount, DefaultContainer, tmpFile)
|
||||
os.Remove(tmpFile)
|
||||
if err != nil {
|
||||
log.Errorf("uploadContainerFileByPath error: %v", err)
|
||||
return "", err
|
||||
|
||||
Reference in New Issue
Block a user