mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-24 16:03:43 +08:00
Merge remote-tracking branch 'origin/release/2.6.0' into confict/lzx-resolv-20190309
This commit is contained in:
@@ -753,6 +753,7 @@ func (b *SBaremetalInstance) attachWire(mac net.HardwareAddr, wireId string, nic
|
||||
params.Add(jsonutils.NewString(nicType), "nic_type")
|
||||
}
|
||||
params.Add(jsonutils.NewString(wireId), "wire")
|
||||
params.Add(jsonutils.NewInt(-1), "index")
|
||||
params.Add(jsonutils.JSONTrue, "link_up")
|
||||
return modules.Hosts.PerformAction(session, b.GetId(), "add-netif", params)
|
||||
}
|
||||
|
||||
@@ -251,26 +251,26 @@ func SetLanStatic(
|
||||
mask string,
|
||||
gateway string,
|
||||
) error {
|
||||
config, err := GetLanConfig(exector, channel)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var argss []Args
|
||||
if config.IPAddr == ip && config.Netmask == mask && config.Gateway == gateway {
|
||||
argss = []Args{
|
||||
newArgs("lan", "set", channel, "ipsrc", "static"),
|
||||
newArgs("lan", "set", channel, "ipaddr", ip),
|
||||
newArgs("lan", "set", channel, "netmask", mask),
|
||||
newArgs("lan", "set", channel, "defgw", "ipaddr", gateway),
|
||||
}
|
||||
} else {
|
||||
argss = []Args{
|
||||
newArgs("lan", "set", channel, "ipaddr", ip),
|
||||
newArgs("lan", "set", channel, "defgw", "ipaddr", gateway),
|
||||
newArgs("lan", "set", channel, "netmask", mask),
|
||||
newArgs("lan", "set", channel, "ipsrc", "static"),
|
||||
}
|
||||
// config, err := GetLanConfig(exector, channel)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// var argss []Args
|
||||
// if config.IPAddr == ip && config.Netmask == mask && config.Gateway == gateway {
|
||||
argss := []Args{
|
||||
newArgs("lan", "set", channel, "ipsrc", "static"),
|
||||
newArgs("lan", "set", channel, "ipaddr", ip),
|
||||
newArgs("lan", "set", channel, "netmask", mask),
|
||||
newArgs("lan", "set", channel, "defgw", "ipaddr", gateway),
|
||||
}
|
||||
// } else {
|
||||
// argss = []Args{
|
||||
// newArgs("lan", "set", channel, "ipsrc", "static"),
|
||||
// newArgs("lan", "set", channel, "ipaddr", ip),
|
||||
// newArgs("lan", "set", channel, "netmask", mask),
|
||||
// newArgs("lan", "set", channel, "defgw", "ipaddr", gateway),
|
||||
// }
|
||||
// }
|
||||
return doActions(exector, "set_lan_static", argss...)
|
||||
}
|
||||
|
||||
|
||||
@@ -205,6 +205,15 @@ func (dispatcher *DBJointModelDispatcher) Attach(ctx context.Context, id1 string
|
||||
return nil, httperrors.NewGeneralError(err)
|
||||
}
|
||||
}
|
||||
|
||||
_, _, joinItem, err := fetchJointItem(dispatcher, ctx, userCred, master.GetId(), slave.GetId(), query)
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
return nil, err
|
||||
}
|
||||
if joinItem != nil {
|
||||
return nil, httperrors.NewNotAcceptableError("Object %s %s has attached %s %s", master.KeywordPlural(), master.GetId(), slave.KeywordPlural(), slave.GetId())
|
||||
}
|
||||
|
||||
lockman.LockJointObject(ctx, master, slave)
|
||||
defer lockman.ReleaseJointObject(ctx, master, slave)
|
||||
return attachItems(dispatcher, master.(IStandaloneModel), slave.(IStandaloneModel), ctx, userCred, query, data)
|
||||
|
||||
@@ -1402,7 +1402,8 @@ func (self *SDisk) PerformPurge(ctx context.Context, userCred mcclient.TokenCred
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if self.GetCloudprovider().Provider == CLOUD_PROVIDER_HUAWEI && self.GetSnapshotCount() > 0 {
|
||||
provider := self.GetCloudprovider()
|
||||
if provider != nil && provider.Provider == CLOUD_PROVIDER_HUAWEI && self.GetSnapshotCount() > 0 {
|
||||
return nil, httperrors.NewForbiddenError("not allow to purge. Virtual disk must not have snapshots")
|
||||
}
|
||||
|
||||
|
||||
@@ -690,6 +690,15 @@ func (self *SHost) AllowPerformRenewPrepaidRecycle(ctx context.Context, userCred
|
||||
}
|
||||
|
||||
func (self *SHost) PerformRenewPrepaidRecycle(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error) {
|
||||
if !self.IsPrepaidRecycle() {
|
||||
return nil, httperrors.NewInputParameterError("Not a prepaid recycle host")
|
||||
}
|
||||
if len(self.RealExternalId) == 0 {
|
||||
return nil, httperrors.NewGeneralError(fmt.Errorf("host RealExternalId is empty"))
|
||||
}
|
||||
if len(self.ExternalId) == 0 {
|
||||
return nil, httperrors.NewGeneralError(fmt.Errorf("host ExternalId is empty"))
|
||||
}
|
||||
durationStr := jsonutils.GetAnyString(data, []string{"duration"})
|
||||
if len(durationStr) == 0 {
|
||||
return nil, httperrors.NewMissingParameterError("duration")
|
||||
|
||||
@@ -2908,7 +2908,9 @@ func (self *SHost) addNetif(ctx context.Context, userCred mcclient.TokenCredenti
|
||||
}
|
||||
netif.Rate = rate
|
||||
netif.NicType = nicType
|
||||
netif.Index = index
|
||||
if index >= 0 {
|
||||
netif.Index = index
|
||||
}
|
||||
if !linkUp.IsNone() {
|
||||
netif.LinkUp = linkUp.Bool()
|
||||
}
|
||||
@@ -2962,7 +2964,7 @@ func (self *SHost) addNetif(ctx context.Context, userCred mcclient.TokenCredenti
|
||||
bridge = fmt.Sprintf("br%s", sw.GetName())
|
||||
}
|
||||
var isMaster = netif.NicType == NIC_TYPE_ADMIN
|
||||
ihw, err := db.FetchJointByIds(HostwireManager, self.Id, sw.Id, nil)
|
||||
ihw, err := HostwireManager.FetchByIdsAndMac(self.Id, sw.Id, mac)
|
||||
if err != nil {
|
||||
hw := &SHostwire{}
|
||||
hw.Bridge = bridge
|
||||
@@ -2976,11 +2978,11 @@ func (self *SHost) addNetif(ctx context.Context, userCred mcclient.TokenCredenti
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
hw := ihw.(*SHostwire)
|
||||
hw := ihw
|
||||
db.Update(hw, func() error {
|
||||
hw.Bridge = bridge
|
||||
hw.Interface = strInterface
|
||||
hw.MacAddr = mac
|
||||
// hw.MacAddr = mac
|
||||
hw.IsMaster = isMaster
|
||||
return nil
|
||||
})
|
||||
@@ -3046,7 +3048,7 @@ func (self *SHost) EnableNetif(ctx context.Context, userCred mcclient.TokenCrede
|
||||
if wire == nil {
|
||||
return fmt.Errorf("No wire attached")
|
||||
}
|
||||
hw, err := db.FetchJointByIds(HostwireManager, self.Id, wire.Id, nil)
|
||||
hw, err := HostwireManager.FetchByIdsAndMac(self.Id, wire.Id, netif.Mac)
|
||||
if hw == nil {
|
||||
return fmt.Errorf("host not attach to this wire")
|
||||
}
|
||||
@@ -3172,7 +3174,7 @@ func (self *SHost) RemoveNetif(ctx context.Context, userCred mcclient.TokenCrede
|
||||
log.Infof("Remove wire")
|
||||
others := self.GetNetifsOnWire(wire)
|
||||
if len(others) == 0 {
|
||||
hw, _ := db.FetchJointByIds(HostwireManager, self.Id, wire.Id, nil)
|
||||
hw, _ := HostwireManager.FetchByIdsAndMac(self.Id, wire.Id, netif.Mac)
|
||||
if hw != nil {
|
||||
db.OpsLog.LogDetachEvent(ctx, self, wire, userCred, jsonutils.NewString(fmt.Sprintf("disable netif %s", self.AccessMac)))
|
||||
log.Infof("Detach host wire because of remove netif %s", netif.Mac)
|
||||
|
||||
@@ -35,7 +35,7 @@ type SHostwire struct {
|
||||
|
||||
Bridge string `width:"16" charset:"ascii" nullable:"false" list:"admin" update:"admin" create:"admin_required"` // Column(VARCHAR(16, charset='ascii'), nullable=False)
|
||||
Interface string `width:"16" charset:"ascii" nullable:"false" list:"admin" update:"admin" create:"admin_required"` // Column(VARCHAR(16, charset='ascii'), nullable=False)
|
||||
IsMaster bool `nullable:"true" default:"false" update:"admin" create:"admin_optional"` // Column(Boolean, nullable=True, default=False)
|
||||
IsMaster bool `nullable:"true" default:"false" list:"admin" update:"admin" create:"admin_optional"` // Column(Boolean, nullable=True, default=False)
|
||||
MacAddr string `width:"18" charset:"ascii" list:"admin" update:"admin" create:"admin_required"` // Column(VARCHAR(18, charset='ascii'))
|
||||
|
||||
HostId string `width:"128" charset:"ascii" nullable:"false" list:"admin" create:"admin_required"` // = Column(VARCHAR(ID_LENGTH, charset='ascii'), nullable=False)
|
||||
@@ -119,3 +119,21 @@ func (self *SHostwire) Delete(ctx context.Context, userCred mcclient.TokenCreden
|
||||
func (self *SHostwire) Detach(ctx context.Context, userCred mcclient.TokenCredential) error {
|
||||
return db.DetachJoint(ctx, userCred, self)
|
||||
}
|
||||
|
||||
func (manager *SHostwireManager) FilterByParams(q *sqlchemy.SQuery, params jsonutils.JSONObject) *sqlchemy.SQuery {
|
||||
macStr := jsonutils.GetAnyString(params, []string{"mac", "mac_addr"})
|
||||
if len(macStr) > 0 {
|
||||
q = q.Filter(sqlchemy.Equals(q.Field("mac_addr"), macStr))
|
||||
}
|
||||
return q
|
||||
}
|
||||
|
||||
func (manager *SHostwireManager) FetchByIdsAndMac(hostId string, wireId string, mac string) (*SHostwire, error) {
|
||||
query := jsonutils.NewDict()
|
||||
query.Add(jsonutils.NewString(mac), "mac_addr")
|
||||
ihw, err := db.FetchJointByIds(manager, hostId, wireId, query)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ihw.(*SHostwire), nil
|
||||
}
|
||||
|
||||
@@ -219,7 +219,7 @@ func (self *SNetInterface) Remove(ctx context.Context, userCred mcclient.TokenCr
|
||||
host := self.GetBaremetal()
|
||||
wire := self.GetWire()
|
||||
if host != nil && wire != nil {
|
||||
hw, err := db.FetchJointByIds(HostwireManager, host.Id, wire.Id, nil)
|
||||
hw, err := HostwireManager.FetchByIdsAndMac(host.Id, wire.Id, self.Mac)
|
||||
if err != nil {
|
||||
log.Errorf("NetInterface remove HostwireManager.FetchByIds error %s", err)
|
||||
return err
|
||||
|
||||
@@ -26,6 +26,7 @@ type IBridgeDriver interface {
|
||||
Exists() bool
|
||||
Interfaces() []string
|
||||
|
||||
FetchConfig()
|
||||
GetMac() string
|
||||
GenerateIfupScripts(scriptPath string, nic jsonutils.JSONObject) error
|
||||
GenerateIfdownScripts(scriptPath string, nic jsonutils.JSONObject) error
|
||||
@@ -56,6 +57,11 @@ func NewBaseBridgeDriver(bridge, inter, ip string) (*SBaseBridgeDriver, error) {
|
||||
return bd, nil
|
||||
}
|
||||
|
||||
func (d *SBaseBridgeDriver) FetchConfig() {
|
||||
d.bridge.FetchConfig()
|
||||
d.inter.FetchConfig()
|
||||
}
|
||||
|
||||
func (d *SBaseBridgeDriver) GetMac() string {
|
||||
if len(d.bridge.Mac) == 0 {
|
||||
d.bridge.FetchConfig()
|
||||
|
||||
@@ -677,11 +677,21 @@ func (h *SHostInfo) GetMasterIp() string {
|
||||
}
|
||||
|
||||
func (h *SHostInfo) GetMasterMac() string {
|
||||
return h.getMasterMacWithRefresh(false)
|
||||
}
|
||||
|
||||
func (h *SHostInfo) getMasterMacWithRefresh(refresh bool) string {
|
||||
if h.MasterNic != nil {
|
||||
if refresh {
|
||||
h.MasterNic.FetchConfig()
|
||||
}
|
||||
return h.MasterNic.Mac
|
||||
}
|
||||
for _, n := range h.Nics {
|
||||
if len(n.Ip) > 0 {
|
||||
if refresh {
|
||||
n.BridgeDev.FetchConfig()
|
||||
}
|
||||
return n.BridgeDev.GetMac()
|
||||
}
|
||||
}
|
||||
@@ -768,7 +778,7 @@ func (h *SHostInfo) getZoneInfo(zoneId string, standalone bool) {
|
||||
}
|
||||
|
||||
func (h *SHostInfo) getHostInfo(zoneId string) {
|
||||
masterMac := h.GetMasterMac()
|
||||
masterMac := h.getMasterMacWithRefresh(true)
|
||||
if len(masterMac) == 0 {
|
||||
panic("master mac not found")
|
||||
}
|
||||
@@ -1011,8 +1021,10 @@ func (h *SHostInfo) doSyncNicInfo(nic *SNIC) {
|
||||
content := jsonutils.NewDict()
|
||||
content.Set("bridge", jsonutils.NewString(nic.Bridge))
|
||||
content.Set("interface", jsonutils.NewString(nic.Inter))
|
||||
query := jsonutils.NewDict()
|
||||
query.Set("mac_addr", jsonutils.NewString(nic.BridgeDev.GetMac()))
|
||||
_, err := modules.Hostwires.Update(h.GetSession(),
|
||||
h.HostId, nic.Network, nil, content)
|
||||
h.HostId, nic.Network, query, content)
|
||||
if err != nil {
|
||||
log.Errorln(err)
|
||||
h.onFail()
|
||||
|
||||
@@ -177,7 +177,9 @@ func init() {
|
||||
"mem_commit_rate", "cpu_commit_bound",
|
||||
"mem_commit_bound", "node_count", "sn", "storage_type",
|
||||
"host_type", "version", "schedtags",
|
||||
"storage_size"},
|
||||
"storage_size",
|
||||
"expired_at",
|
||||
},
|
||||
[]string{})}
|
||||
registerCompute(&Hosts)
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ var (
|
||||
func init() {
|
||||
Hostwires = NewJointComputeManager("hostwire", "hostwires",
|
||||
[]string{"Host_ID", "Host", "Wire_ID", "Wire",
|
||||
"Bridge", "Interface", "Mac_addr"},
|
||||
"Bridge", "Interface", "Mac_addr", "is_master"},
|
||||
[]string{},
|
||||
&Hosts,
|
||||
&Wires)
|
||||
|
||||
+18
-5
@@ -3,6 +3,7 @@ package esxi
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/vmware/govmomi/vim25/mo"
|
||||
@@ -77,15 +78,27 @@ func NewHost(manager *SESXiClient, host *mo.HostSystem, dc *SDatacenter) *SHost
|
||||
return &SHost{SManagedObject: newManagedObject(manager, host, dc)}
|
||||
}
|
||||
|
||||
func (self *SHost) GetName() string {
|
||||
name := self.SManagedObject.GetName()
|
||||
dotPos := strings.IndexByte(name, '.')
|
||||
if dotPos > 0 && !regutils.MatchIP4Addr(name) {
|
||||
name = name[:dotPos]
|
||||
var (
|
||||
ip4addrPattern = regexp.MustCompile(`\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}`)
|
||||
)
|
||||
|
||||
func formatName(name string) string {
|
||||
if ip4addrPattern.MatchString(name) {
|
||||
return strings.Replace(name, ".", "-", -1)
|
||||
} else {
|
||||
dotPos := strings.IndexByte(name, '.')
|
||||
if dotPos > 0 && !regutils.MatchIP4Addr(name) {
|
||||
name = name[:dotPos]
|
||||
}
|
||||
return name
|
||||
}
|
||||
return name
|
||||
}
|
||||
|
||||
func (self *SHost) GetName() string {
|
||||
return formatName(self.SManagedObject.GetName())
|
||||
}
|
||||
|
||||
func (self *SHost) GetMetadata() *jsonutils.JSONDict {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package esxi
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFormatName(t *testing.T) {
|
||||
cases := []struct {
|
||||
In string
|
||||
Want string
|
||||
}{
|
||||
{
|
||||
In: "esxi-172.16.23.1",
|
||||
Want: "esxi-172-16-23-1",
|
||||
},
|
||||
{
|
||||
In: "esxi6.yunion.cn",
|
||||
Want: "esxi6",
|
||||
},
|
||||
}
|
||||
for _, c := range cases {
|
||||
got := formatName(c.In)
|
||||
if got != c.Want {
|
||||
t.Errorf("got: %s want %s", got, c.Want)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user