Skip to content

Commit

Permalink
Merge pull request #1 in ~LIZEXI/onecloud from ~WANYAOQI/onecloud:bug…
Browse files Browse the repository at this point in the history
…fix/wyq/fix-BaremetalSyncAllGuestsStatusTask to bugfix/lzx-baremetal-api

* commit 'ea81fa26dbcb56eefe131387b2fab8c3b731029d':
  fix BaremetalSyncAllGuestsStatusTask
  region: baremetal api compatible
  • Loading branch information
zexi committed Dec 4, 2018
2 parents 1b3d339 + ea81fa2 commit 3f3f170
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 66 deletions.
4 changes: 1 addition & 3 deletions pkg/cloudcommon/db/taskman/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -654,9 +654,7 @@ func (self *STask) GetObjects() []db.IStandaloneModel {
}

func (task *STask) GetTaskRequestHeader() http.Header {
header := http.Header{}
header.Set(mcclient.AUTH_TOKEN, task.GetUserCred().GetTokenString())
header := mcclient.GetTokenHeaders(task.GetUserCred())
header.Set(mcclient.TASK_ID, task.GetTaskId())
header.Set(mcclient.REGION_VERSION, "v2")
return header
}
8 changes: 5 additions & 3 deletions pkg/cloudprovider/resources.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package cloudprovider

import (
"context"
"time"

"context"
"yunion.io/x/jsonutils"
"yunion.io/x/onecloud/pkg/mcclient"
"yunion.io/x/pkg/tristate"
"yunion.io/x/pkg/util/secrules"

"yunion.io/x/onecloud/pkg/mcclient"
)

type ICloudResource interface {
Expand Down Expand Up @@ -356,7 +358,7 @@ type ICloudHostNetInterface interface {
GetDriver() string
GetMac() string
GetIndex() int8
IsLinkUp() bool
IsLinkUp() tristate.TriState
GetIpAddr() string
GetMtu() int16
GetNicType() string
Expand Down
17 changes: 4 additions & 13 deletions pkg/compute/guestdrivers/baremetals.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package guestdrivers
import (
"context"
"fmt"
"net/http"
"regexp"
"strings"

Expand Down Expand Up @@ -168,9 +167,7 @@ func (self *SBaremetalGuestDriver) RequestStartOnHost(ctx context.Context, guest
desc := guest.GetJsonDescAtBaremetal(ctx, host)
config := jsonutils.NewDict()
config.Set("desc", desc)
headers := http.Header{}
headers.Set("X-Auth-Token", task.GetUserCred().GetTokenString())
headers.Set("X-Task-Id", task.GetTaskId())
headers := task.GetTaskRequestHeader()
url := fmt.Sprintf("/baremetals/%s/servers/%s/start", host.Id, guest.Id)
return host.BaremetalSyncRequest(ctx, "POST", url, headers, config)
}
Expand Down Expand Up @@ -208,9 +205,7 @@ func (self *SBaremetalGuestDriver) RequestStopOnHost(ctx context.Context, guest
timeout = 0
}
body.Set("timeout", jsonutils.NewInt(timeout))
headers := http.Header{}
headers.Set("X-Auth-Token", task.GetUserCred().GetTokenString())
headers.Set("X-Task-Id", task.GetTaskId())
headers := task.GetTaskRequestHeader()
url := fmt.Sprintf("/baremetals/%s/servers/%s/stop", host.Id, guest.Id)
_, err = host.BaremetalSyncRequest(ctx, "POST", url, headers, body)
return err
Expand All @@ -227,9 +222,7 @@ func (self *SBaremetalGuestDriver) StartGuestStopTask(guest *models.SGuest, ctx

func (self *SBaremetalGuestDriver) RequestUndeployGuestOnHost(ctx context.Context, guest *models.SGuest, host *models.SHost, task taskman.ITask) error {
url := fmt.Sprintf("/baremetals/%s/servers/%s", host.Id, guest.Id)
headers := http.Header{}
headers.Set("X-Auth-Token", task.GetUserCred().GetTokenString())
headers.Set("X-Task-Id", task.GetTaskId())
headers := task.GetTaskRequestHeader()
_, err := host.BaremetalSyncRequest(ctx, "DELETE", url, headers, nil)
return err
}
Expand Down Expand Up @@ -361,9 +354,7 @@ func (self *SBaremetalGuestDriver) RequestDeployGuestOnHost(ctx context.Context,
config.Set("on_finish", jsonutils.NewString("restart"))
}
url := fmt.Sprintf("/baremetals/%s/servers/%s/%s", host.Id, guest.Id, val)
headers := http.Header{}
headers.Set("X-Auth-Token", task.GetUserCred().GetTokenString())
headers.Set("X-Task-Id", task.GetTaskId())
headers := task.GetTaskRequestHeader()
_, err := host.BaremetalSyncRequest(ctx, "POST", url, headers, config)
return err
}
Expand Down
75 changes: 56 additions & 19 deletions pkg/compute/models/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ type SHost struct {

StorageSize int `nullable:"true" list:"admin" update:"admin" create:"admin_optional"` // Column(Integer, nullable=True) # storage size in MB
StorageType string `width:"20" charset:"ascii" nullable:"true" list:"admin" update:"admin" create:"admin_optional"` // Column(VARCHAR(20, charset='ascii'), nullable=True)
StorageDriver string `width:"20" charset:"ascii" nullable:"true" update:"admin" create:"admin_optional"` // Column(VARCHAR(20, charset='ascii'), nullable=True)
StorageDriver string `width:"20" charset:"ascii" nullable:"true" get:"admin" update:"admin" create:"admin_optional"` // Column(VARCHAR(20, charset='ascii'), nullable=True)
StorageInfo jsonutils.JSONObject `nullable:"true" get:"admin" update:"admin" create:"admin_optional"` // Column(JSONEncodedDict, nullable=True)

IpmiInfo jsonutils.JSONObject `nullable:"true" get:"admin" update:"admin" create:"admin_optional"` // Column(JSONEncodedDict, nullable=True)
Expand Down Expand Up @@ -507,6 +507,22 @@ func (self *SHost) GetBaremetalstorage() *SHoststorage {
return nil
}

func (self *SHost) SaveCleanUpdates(doUpdate func() error) (map[string]sqlchemy.SUpdateDiff, error) {
return self.saveUpdates(doUpdate, true)
}

func (self *SHost) SaveUpdates(doUpdate func() error) (map[string]sqlchemy.SUpdateDiff, error) {
return self.saveUpdates(doUpdate, false)
}

func (self *SHost) saveUpdates(doUpdate func() error, doSchedClean bool) (map[string]sqlchemy.SUpdateDiff, error) {
diff, err := self.GetModelManager().TableSpec().Update(self, doUpdate)
if err == nil && doSchedClean {
self.ClearSchedDescCache()
}
return diff, err
}

func (self *SHost) AllowPerformUpdateStorage(
ctx context.Context,
userCred mcclient.TokenCredential,
Expand Down Expand Up @@ -625,6 +641,15 @@ func (self *SHost) SyncAttachedStorageStatus() {
}
}

func (self *SHostManager) IsNewNameUnique(name string, userCred mcclient.TokenCredential, kwargs *jsonutils.JSONDict) bool {
q := self.Query().Equals("name", name)
if kwargs != nil && kwargs.Contains("zone_id") {
zoneId, _ := kwargs.GetString("zone_id")
q.Equals("zone_id", zoneId)
}
return q.Count() == 0
}

func (self *SHostManager) AllowGetPropertyBmStartRegisterScript(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) bool {
return true
}
Expand Down Expand Up @@ -1110,7 +1135,7 @@ func (manager *SHostManager) SyncHosts(ctx context.Context, userCred mcclient.To
}

func (self *SHost) syncWithCloudHost(extHost cloudprovider.ICloudHost, projectSync bool) error {
_, err := self.GetModelManager().TableSpec().Update(self, func() error {
_, err := self.SaveUpdates(func() error {
self.Name = extHost.GetName()

self.Status = extHost.GetStatus()
Expand Down Expand Up @@ -2020,7 +2045,7 @@ func (self *SHost) PostCreate(ctx context.Context, userCred mcclient.TokenCreden
return
}
if ipmiInfo.Length() > 0 {
_, err := HostManager.TableSpec().Update(self, func() error {
_, err := self.SaveUpdates(func() error {
self.IpmiInfo = ipmiInfo
return nil
})
Expand Down Expand Up @@ -2206,7 +2231,6 @@ func (self *SHost) FetchIpmiInfo(data *jsonutils.JSONDict) (*jsonutils.JSONDict,
for key := range kv {
if strings.HasPrefix(key, IPMI_KEY_PERFIX) {
value, _ := data.GetString(key)
log.Errorf("---------fetch ipmiinfo key: %s, val: %s", key, value)
subkey := key[len(IPMI_KEY_PERFIX):]
data.Remove(key)
if subkey == "password" {
Expand Down Expand Up @@ -2423,10 +2447,13 @@ func (self *SHost) AllowPerformOffline(ctx context.Context,

func (self *SHost) PerformOffline(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error) {
if self.HostStatus != HOST_OFFLINE {
self.GetModelManager().TableSpec().Update(self, func() error {
_, err := self.SaveCleanUpdates(func() error {
self.HostStatus = HOST_OFFLINE
return nil
})
if err != nil {
return nil, err
}
db.OpsLog.LogEvent(self, db.ACT_OFFLINE, "", userCred)
logclient.AddActionLog(self, logclient.ACT_ONLINE, nil, userCred, true)
}
Expand All @@ -2442,7 +2469,7 @@ func (self *SHost) AllowPerformOnline(ctx context.Context,

func (self *SHost) PerformOnline(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error) {
if self.HostStatus != HOST_ONLINE {
_, err := self.GetModelManager().TableSpec().Update(self, func() error {
_, err := self.SaveCleanUpdates(func() error {
self.LastPingAt = time.Now()
self.HostStatus = HOST_ONLINE
self.Status = BAREMETAL_RUNNING
Expand Down Expand Up @@ -2480,7 +2507,7 @@ func (self *SHost) PerformPing(ctx context.Context, userCred mcclient.TokenCrede
if self.HostStatus != HOST_ONLINE {
self.PerformOnline(ctx, userCred, query, data)
} else {
self.GetModelManager().TableSpec().Update(self, func() error {
self.SaveUpdates(func() error {
self.LastPingAt = time.Now()
return nil
})
Expand Down Expand Up @@ -2557,18 +2584,26 @@ func (self *SHost) PerformAddNetif(ctx context.Context, userCred mcclient.TokenC
reserve := jsonutils.QueryBoolean(data, "reserve", false)
requireDesignatedIp := jsonutils.QueryBoolean(data, "require_designated_ip", false)

err := self.addNetif(ctx, userCred, mac, wire, ipAddr, int(rate), nicType, int8(index), utils.ToBool(linkUp),
isLinkUp := tristate.None
if linkUp != "" {
if utils.ToBool(linkUp) {
isLinkUp = tristate.True
} else {
isLinkUp = tristate.False
}
}

err := self.addNetif(ctx, userCred, mac, wire, ipAddr, int(rate), nicType, int8(index), isLinkUp,
int16(mtu), reset, strInterface, bridge, reserve, requireDesignatedIp)
return nil, err
}

func (self *SHost) addNetif(ctx context.Context, userCred mcclient.TokenCredential,
mac string, wire string, ipAddr string,
rate int, nicType string, index int8, linkUp bool, mtu int16,
rate int, nicType string, index int8, linkUp tristate.TriState, mtu int16,
reset bool, strInterface string, bridge string,
reserve bool, requireDesignatedIp bool,
) error {

var sw *SWire
if len(wire) > 0 && len(ipAddr) == 0 {
iWire, err := WireManager.FetchByIdOrName(userCred, wire)
Expand Down Expand Up @@ -2603,7 +2638,9 @@ func (self *SHost) addNetif(ctx context.Context, userCred mcclient.TokenCredenti
netif.Rate = rate
netif.NicType = nicType
netif.Index = index
netif.LinkUp = linkUp
if !linkUp.IsNone() {
netif.LinkUp = linkUp.Bool()
}
netif.Mtu = mtu
err = NetInterfaceManager.TableSpec().Insert(netif)
if err != nil {
Expand All @@ -2620,20 +2657,20 @@ func (self *SHost) addNetif(ctx context.Context, userCred mcclient.TokenCredenti
changed = true
netif.WireId = sw.Id
}
if rate != netif.Rate {
if rate > 0 && rate != netif.Rate {
netif.Rate = rate
}
if nicType != netif.NicType {
if nicType != "" && nicType != netif.NicType {
netif.NicType = nicType
}
if index >= 0 && index != netif.Index {
netif.Index = int8(index)
netif.Index = index
}
if linkUp != netif.LinkUp {
netif.LinkUp = linkUp
if !linkUp.IsNone() && linkUp.Bool() != netif.LinkUp {
netif.LinkUp = linkUp.Bool()
}
if mtu != netif.Mtu {
netif.Mtu = int16(mtu)
if mtu > 0 && mtu != netif.Mtu {
netif.Mtu = mtu
}
return nil
})
Expand Down Expand Up @@ -2938,7 +2975,7 @@ func (self *SHost) AllowPerformDisable(ctx context.Context,

func (self *SHost) PerformDisable(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error) {
if self.Enabled {
_, err := self.GetModelManager().TableSpec().Update(self, func() error {
_, err := self.SaveCleanUpdates(func() error {
self.Enabled = false
return nil
})
Expand Down
7 changes: 2 additions & 5 deletions pkg/compute/models/hoststorages.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package models
import (
"context"
"fmt"
"net/http"

"yunion.io/x/jsonutils"
"yunion.io/x/log"
Expand Down Expand Up @@ -110,8 +109,7 @@ func (self *SHoststorage) PostCreate(ctx context.Context, userCred mcclient.Toke
host := storage.GetMasterHost()
log.Infof("Attach SharedStorage[%s] on host %s ...", storage.Name, host.Name)
url := fmt.Sprintf("%s/storages/attach", host.ManagerUri)
headers := http.Header{}
headers.Set("X-Auth-Token", userCred.GetTokenString())
headers := mcclient.GetTokenHeaders(userCred)
body := jsonutils.NewDict()
body.Set("mount_point", jsonutils.NewString(self.MountPoint))
body.Set("name", jsonutils.NewString(storage.Name))
Expand Down Expand Up @@ -142,8 +140,7 @@ func (self *SHoststorage) PreDelete(ctx context.Context, userCred mcclient.Token
host := storage.GetMasterHost()
log.Infof("Attach SharedStorage[%s] on host %s ...", storage.Name, host.Name)
url := fmt.Sprintf("%s/storages/detach", host.ManagerUri)
headers := http.Header{}
headers.Set("X-Auth-Token", userCred.GetTokenString())
headers := mcclient.GetTokenHeaders(userCred)
body := jsonutils.NewDict()
body.Set("mount_point", jsonutils.NewString(self.MountPoint))
body.Set("name", jsonutils.NewString(storage.Name))
Expand Down
29 changes: 28 additions & 1 deletion pkg/compute/tasks/baremetal_sync_status_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"yunion.io/x/onecloud/pkg/cloudcommon/db"
"yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
"yunion.io/x/onecloud/pkg/compute/models"
"yunion.io/x/onecloud/pkg/util/logclient"
)

type BaremetalSyncStatusTask struct {
Expand Down Expand Up @@ -42,7 +43,33 @@ func (self *BaremetalSyncAllGuestsStatusTask) OnInit(ctx context.Context, obj db
baremetal := obj.(*models.SHost)
guest := baremetal.GetBaremetalServer()
if guest != nil {

var first bool
if !guest.IsSystem {
first = true
}
guest.GetModelManager().TableSpec().Update(guest, func() error {
guest.IsSystem = true
guest.VmemSize = 0
guest.VcpuCount = 0
return nil
})
bs := baremetal.GetBaremetalstorage().GetStorage()
bs.SetStatus(self.UserCred, models.STORAGE_OFFLINE, "")
if first && baremetal.Name != guest.Name {
baremetal.GetModelManager().TableSpec().Update(baremetal, func() error {
if models.HostManager.IsNewNameUnique(guest.Name, self.UserCred, nil) {
baremetal.Name = guest.Name
} else {
baremetal.Name = db.GenerateName(baremetal.GetModelManager(),
self.UserCred.GetTokenString(), guest.Name)
}
return nil
})
}
if first {
db.OpsLog.LogEvent(guest, db.ACT_CONVERT_COMPLETE, "", self.UserCred)
logclient.AddActionLog(guest, logclient.ACT_BM_CONVERT_HYPER, "", self.UserCred, true)
}
}
self.SetStage("OnGuestSyncStatusComplete", nil)
self.OnGuestSyncStatusComplete(ctx, baremetal, nil)
Expand Down
3 changes: 2 additions & 1 deletion pkg/compute/tasks/guest_delete_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (

"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/pkg/utils"

"yunion.io/x/onecloud/pkg/cloudcommon/db"
"yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
"yunion.io/x/onecloud/pkg/cloudcommon/notifyclient"
"yunion.io/x/onecloud/pkg/compute/models"
"yunion.io/x/onecloud/pkg/compute/options"
"yunion.io/x/onecloud/pkg/util/logclient"
"yunion.io/x/pkg/utils"
)

type GuestDeleteTask struct {
Expand All @@ -27,6 +27,7 @@ func (self *GuestDeleteTask) OnInit(ctx context.Context, obj db.IStandaloneModel
guest := obj.(*models.SGuest)
host := guest.GetHost()
if guest.Hypervisor == models.HYPERVISOR_BAREMETAL && host.HostType != models.HOST_TYPE_BAREMETAL {
// if a fake server for converted hypervisor, then just skip stop
self.OnGuestStopComplete(ctx, obj, data)
return
}
Expand Down
Loading

0 comments on commit 3f3f170

Please sign in to comment.