Merge pull request #991 from Zexi/feature/scheduler-remove-gorm

scheduler: remove gorm code
This commit is contained in:
yunion-ci-robot
2019-05-30 22:43:21 +08:00
committed by GitHub
60 changed files with 366 additions and 4971 deletions
+41 -66
View File
@@ -19,17 +19,14 @@ import (
"strings"
"time"
fjson "github.com/json-iterator/go"
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/sqlchemy"
computeapi "yunion.io/x/onecloud/pkg/apis/compute"
"yunion.io/x/onecloud/pkg/compute/baremetal"
"yunion.io/x/onecloud/pkg/scheduler/cache"
"yunion.io/x/onecloud/pkg/scheduler/cache/db"
"yunion.io/x/onecloud/pkg/scheduler/core"
"yunion.io/x/onecloud/pkg/scheduler/db/models"
computemodels "yunion.io/x/onecloud/pkg/compute/models"
"yunion.io/x/onecloud/pkg/scheduler/core"
)
type baremetalGetter struct {
@@ -63,16 +60,15 @@ func (h baremetalGetter) StorageInfo() []*baremetal.BaremetalStorage {
type BaremetalDesc struct {
*BaseHostDesc
StorageInfo []*baremetal.BaremetalStorage `json:"storage_info"`
StorageType string `json:"storage_type"`
StorageSize int64 `json:"storage_size"`
StorageDriver string `json:"storage_driver"`
ServerID string `json:"server_id"`
StorageInfo []*baremetal.BaremetalStorage `json:"storage_info"`
StorageType string `json:"storage_type"`
StorageSize int64 `json:"storage_size"`
ServerID string `json:"server_id"`
}
type BaremetalBuilder struct {
baremetalAgents cache.Cache
baremetals []interface{}
*baseBuilder
baremetals []computemodels.SHost
residentTenantDict map[string]map[string]interface{}
}
@@ -82,8 +78,7 @@ func (bd *BaremetalDesc) Getter() core.CandidatePropertyGetter {
}
func (bd *BaremetalDesc) String() string {
s, _ := fjson.Marshal(bd)
return string(s)
return jsonutils.Marshal(bd).String()
}
func (bd *BaremetalDesc) Type() int {
@@ -123,18 +118,19 @@ func (bd *BaremetalDesc) FreeStorageSize() int64 {
return 0
}
func (bb *BaremetalBuilder) init(ids []string, dbCache DBGroupCacher, syncCache SyncGroupCacher) error {
agents, err := dbCache.Get(db.BaremetalAgentDBCache)
func newBaremetalBuilder() *BaremetalBuilder {
return &BaremetalBuilder{
baseBuilder: newBaseBuilder(BaremetalDescBuilder),
}
}
func (bb *BaremetalBuilder) init(ids []string) error {
bms, err := FetchHostsByIds(ids)
if err != nil {
return err
}
bms, err := models.FetchBaremetalHostByIDs(ids)
if err != nil {
return err
}
bb.baremetalAgents = agents
//bb.baremetalAgents = agents
bb.baremetals = bms
wg := &WaitGroupWrapper{}
@@ -164,19 +160,19 @@ func (bb *BaremetalBuilder) init(ids []string, dbCache DBGroupCacher, syncCache
}
func (bb *BaremetalBuilder) Clone() BuildActor {
return &BaremetalBuilder{}
}
func (bb *BaremetalBuilder) Type() string {
return BaremetalDescBuilder
return &BaremetalBuilder{
baseBuilder: newBaseBuilder(BaremetalDescBuilder),
}
}
func (bb *BaremetalBuilder) AllIDs() ([]string, error) {
return models.AllBaremetalIDs()
q := computemodels.HostManager.Query("id")
q = q.Filter(sqlchemy.Equals(q.Field("host_type"), computeapi.HOST_TYPE_BAREMETAL))
return FetchModelIds(q)
}
func (bb *BaremetalBuilder) Do(ids []string, dbCache DBGroupCacher, syncCache SyncGroupCacher) ([]interface{}, error) {
err := bb.init(ids, dbCache, syncCache)
func (bb *BaremetalBuilder) Do(ids []string) ([]interface{}, error) {
err := bb.init(ids)
if err != nil {
return nil, err
}
@@ -191,7 +187,7 @@ func (bb *BaremetalBuilder) Do(ids []string, dbCache DBGroupCacher, syncCache Sy
func (bb *BaremetalBuilder) build() ([]interface{}, error) {
schedDescs := []interface{}{}
for _, bm := range bb.baremetals {
desc, err := bb.buildOne(bm.(*models.Host))
desc, err := bb.buildOne(&bm)
if err != nil {
log.Errorf("BaremetalBuilder error: %v", err)
continue
@@ -201,8 +197,7 @@ func (bb *BaremetalBuilder) build() ([]interface{}, error) {
return schedDescs, nil
}
func (bb *BaremetalBuilder) buildOne(bm *models.Host) (interface{}, error) {
hostObj := computemodels.HostManager.FetchHostById(bm.ID)
func (bb *BaremetalBuilder) buildOne(hostObj *computemodels.SHost) (interface{}, error) {
baseDesc, err := newBaseHostDesc(hostObj)
if err != nil {
return nil, err
@@ -211,22 +206,15 @@ func (bb *BaremetalBuilder) buildOne(bm *models.Host) (interface{}, error) {
BaseHostDesc: baseDesc,
}
desc.StorageDriver = bm.StorageDriver
desc.StorageType = bm.StorageType
desc.StorageSize = int64(bm.StorageSize)
desc.StorageDriver = hostObj.StorageDriver
desc.StorageType = hostObj.StorageType
desc.StorageSize = int64(hostObj.StorageSize)
var baremetalStorages []*baremetal.BaremetalStorage
err = fjson.Unmarshal([]byte(bm.StorageInfo), &baremetalStorages)
if err != nil {
// StorageInfo maybe is NULL
if bm.StorageInfo != "" {
log.Errorln(err)
}
}
baremetalStorages := computemodels.ConvertStorageInfo2BaremetalStorages(hostObj.StorageInfo)
desc.StorageInfo = baremetalStorages
desc.Tenants = make(map[string]int64, 0)
err = bb.fillServerID(desc, bm)
err = bb.fillServerID(desc, hostObj)
if err != nil {
return nil, err
}
@@ -234,26 +222,13 @@ func (bb *BaremetalBuilder) buildOne(bm *models.Host) (interface{}, error) {
return desc, nil
}
func (bb *BaremetalBuilder) fillServerID(desc *BaremetalDesc, b *models.Host) error {
guests, err := models.FetchGuestByHostIDsWithCond([]string{b.ID},
map[string]interface{}{
"hypervisor": "baremetal",
})
if err != nil {
return err
}
if len(guests) == 0 {
desc.ServerID = ""
} else if len(guests) == 1 {
desc.ServerID = guests[0].(*models.Guest).ID
} else {
return fmt.Errorf("One baremetal %q contains %d guests, %v", b.Name, len(guests), guests)
func (bb *BaremetalBuilder) fillServerID(desc *BaremetalDesc, b *computemodels.SHost) error {
guest := b.GetBaremetalServer()
srvId := ""
if guest != nil {
srvId = guest.GetId()
}
desc.ServerID = srvId
return nil
}
func (b *BaremetalBuilder) getZoneID(bm *models.Host) string {
return bm.ZoneID
}
+5 -10
View File
@@ -23,7 +23,6 @@ import (
"yunion.io/x/sqlchemy"
"yunion.io/x/onecloud/pkg/scheduler/api"
"yunion.io/x/onecloud/pkg/scheduler/db/models"
computeapi "yunion.io/x/onecloud/pkg/apis/compute"
computedb "yunion.io/x/onecloud/pkg/cloudcommon/db"
@@ -332,7 +331,7 @@ func (h *BaseHostDesc) GetHostType() string {
}
func HostsResidentTenantStats(hostIDs []string) (map[string]map[string]interface{}, error) {
residentTenantStats, err := models.ResidentTenantsInHosts(hostIDs)
residentTenantStats, err := FetchHostsResidentTenants(hostIDs)
if err != nil {
return nil, err
}
@@ -361,19 +360,15 @@ func HostResidentTenantCount(id string) (map[string]int64, error) {
}
type DescBuilder struct {
dbGroupCache DBGroupCacher
syncGroupCache SyncGroupCacher
actor BuildActor
actor BuildActor
}
func NewDescBuilder(db DBGroupCacher, sync SyncGroupCacher, act BuildActor) *DescBuilder {
func NewDescBuilder(act BuildActor) *DescBuilder {
return &DescBuilder{
dbGroupCache: db,
syncGroupCache: sync,
actor: act,
actor: act,
}
}
func (d *DescBuilder) Build(ids []string) ([]interface{}, error) {
return d.actor.Do(ids, d.dbGroupCache, d.syncGroupCache)
return d.actor.Do(ids)
}
+105
View File
@@ -0,0 +1,105 @@
package candidate
import (
"time"
"yunion.io/x/sqlchemy"
computeapi "yunion.io/x/onecloud/pkg/apis/compute"
"yunion.io/x/onecloud/pkg/cloudcommon/db"
computemodels "yunion.io/x/onecloud/pkg/compute/models"
)
type baseBuilder struct {
resourceType string
}
func newBaseBuilder(resourceType string) *baseBuilder {
return &baseBuilder{
resourceType: resourceType,
}
}
func (b *baseBuilder) Type() string {
return b.resourceType
}
func FetchModelIds(q *sqlchemy.SQuery) ([]string, error) {
rs, err := q.Rows()
if err != nil {
return nil, err
}
ret := []string{}
defer rs.Close()
for rs.Next() {
var id string
if err := rs.Scan(&id); err != nil {
return nil, err
}
ret = append(ret, id)
}
return ret, nil
}
func FetchHostsByIds(ids []string) ([]computemodels.SHost, error) {
hosts := computemodels.HostManager.Query()
q := hosts.In("id", ids)
hostObjs := make([]computemodels.SHost, 0)
if err := db.FetchModelObjects(computemodels.HostManager, q, &hostObjs); err != nil {
return nil, err
}
return hostObjs, nil
}
type UpdateStatus struct {
Id string `json:"id"`
UpdatedAt time.Time `json:"updated_at"`
}
func FetchModelUpdateStatus(man db.IStandaloneModelManager, cond sqlchemy.ICondition) ([]UpdateStatus, error) {
ret := make([]UpdateStatus, 0)
err := man.Query("id", "updated_at").Filter(cond).All(&ret)
return ret, err
}
func FetchHostsUpdateStatus(isBaremetal bool) ([]UpdateStatus, error) {
q := computemodels.HostManager.Query("id", "updated_at")
if isBaremetal {
q = q.Equals("host_type", computeapi.HOST_TYPE_BAREMETAL)
} else {
q = q.NotEquals("host_type", computeapi.HOST_TYPE_BAREMETAL)
}
ret := make([]UpdateStatus, 0)
err := q.All(&ret)
return ret, err
}
type ResidentTenant struct {
HostId string `json:"host_id"`
TenantId string `json:"tenant_id"`
TenantCount int64 `json:"tenant_count"`
}
func (t ResidentTenant) First() string {
return t.HostId
}
func (t ResidentTenant) Second() string {
return t.TenantId
}
func (t ResidentTenant) Third() interface{} {
return t.TenantCount
}
func FetchHostsResidentTenants(hostIds []string) ([]ResidentTenant, error) {
guests := computemodels.GuestManager.Query().SubQuery()
q := guests.Query(
guests.Field("host_id"),
guests.Field("tenant_id"),
sqlchemy.COUNT("tenant_count", guests.Field("tenant_id")),
).In("host_id", hostIds).GroupBy("tenant_id", "host_id")
ret := make([]ResidentTenant, 0)
err := q.All(&ret)
return ret, err
}
+2 -2
View File
@@ -22,7 +22,7 @@ type candidateItem struct {
cache.CachedItem
}
func NewCandidateManager(db DBGroupCacher, sync SyncGroupCacher, stopCh <-chan struct{}) *cache.GroupManager {
items := defaultCadidateItems(db, sync)
func NewCandidateManager(stopCh <-chan struct{}) *cache.GroupManager {
items := defaultCadidateItems()
return cache.NewGroupManager(CacheKind, items, stopCh)
}
+16 -17
View File
@@ -24,7 +24,6 @@ import (
u "yunion.io/x/pkg/utils"
"yunion.io/x/onecloud/pkg/scheduler/cache"
"yunion.io/x/onecloud/pkg/scheduler/db/models"
"yunion.io/x/onecloud/pkg/scheduler/options"
)
@@ -38,10 +37,10 @@ const (
BaremetalDescBuilder = BaremetalCandidateCache
)
func defaultCadidateItems(db DBGroupCacher, sync SyncGroupCacher) []cache.CachedItem {
func defaultCadidateItems() []cache.CachedItem {
return []cache.CachedItem{
newHostCache(db, sync),
newBaremetalCache(db, sync),
newHostCache(),
newBaremetalCache(),
}
}
@@ -49,12 +48,12 @@ func uuidKey(obj interface{}) (string, error) {
return obj.(descer).GetId(), nil
}
func generalUpdateFunc(db DBGroupCacher, sync SyncGroupCacher, act BuildActor, mutex *gosync.Mutex) cache.UpdateFunc {
func generalUpdateFunc(act BuildActor, mutex *gosync.Mutex) cache.UpdateFunc {
return func(ids []string) ([]interface{}, error) {
mutex.Lock()
defer mutex.Unlock()
newAct := act.Clone()
builder := NewDescBuilder(db, sync, newAct)
builder := NewDescBuilder(newAct)
descs, err := builder.Build(ids)
if err != nil {
return nil, err
@@ -64,12 +63,12 @@ func generalUpdateFunc(db DBGroupCacher, sync SyncGroupCacher, act BuildActor, m
}
}
func generalLoadFunc(db DBGroupCacher, sync SyncGroupCacher, act BuildActor, mutex *gosync.Mutex) cache.LoadFunc {
func generalLoadFunc(act BuildActor, mutex *gosync.Mutex) cache.LoadFunc {
return func() ([]interface{}, error) {
mutex.Lock()
defer mutex.Unlock()
newAct := act.Clone()
builder := NewDescBuilder(db, sync, newAct)
builder := NewDescBuilder(newAct)
ids, err := act.AllIDs()
if err != nil {
@@ -119,7 +118,7 @@ func generalGetUpdateFunc(isBaremetal bool) cache.GetUpdateFunc {
}
fullUpdateCounter++
modified, err := models.AllHostStatus(isBaremetal)
modified, err := FetchHostsUpdateStatus(isBaremetal)
if err != nil {
return nil, err
}
@@ -127,8 +126,8 @@ func generalGetUpdateFunc(isBaremetal bool) cache.GetUpdateFunc {
// Aggregate the updated hosts
for _, status := range modified {
// If host does not exist[ok=false] or has updated will be in update list.
if t, ok := allStatus[status.ID]; !ok || !t.Equal(status.UpdatedAt) {
modifiedIds = append(modifiedIds, status.ID)
if t, ok := allStatus[status.Id]; !ok || !t.Equal(status.UpdatedAt) {
modifiedIds = append(modifiedIds, status.Id)
}
}
if len(modifiedIds) == 0 {
@@ -138,10 +137,10 @@ func generalGetUpdateFunc(isBaremetal bool) cache.GetUpdateFunc {
}
}
func newHostCache(db DBGroupCacher, sync SyncGroupCacher) cache.CachedItem {
func newHostCache() cache.CachedItem {
mutex := new(gosync.Mutex)
update := generalUpdateFunc(db, sync, &HostBuilder{}, mutex)
load := generalLoadFunc(db, sync, &HostBuilder{}, mutex)
update := generalUpdateFunc(newHostBuilder(), mutex)
load := generalLoadFunc(newHostBuilder(), mutex)
getUpdate := generalGetUpdateFunc(false)
item := new(candidateItem)
@@ -157,11 +156,11 @@ func newHostCache(db DBGroupCacher, sync SyncGroupCacher) cache.CachedItem {
return item
}
func newBaremetalCache(db DBGroupCacher, sync SyncGroupCacher) cache.CachedItem {
func newBaremetalCache() cache.CachedItem {
// The mutex solves the possible dirty data asked lead to over-commit.
mutex := new(gosync.Mutex)
update := generalUpdateFunc(db, sync, &BaremetalBuilder{}, mutex)
load := generalLoadFunc(db, sync, &BaremetalBuilder{}, mutex)
update := generalUpdateFunc(newBaremetalBuilder(), mutex)
load := generalLoadFunc(newBaremetalBuilder(), mutex)
getUpdate := generalGetUpdateFunc(true)
item := new(candidateItem)
+189 -245
View File
@@ -34,7 +34,6 @@ import (
"yunion.io/x/onecloud/pkg/compute/baremetal"
computemodels "yunion.io/x/onecloud/pkg/compute/models"
"yunion.io/x/onecloud/pkg/scheduler/core"
"yunion.io/x/onecloud/pkg/scheduler/db/models"
o "yunion.io/x/onecloud/pkg/scheduler/options"
)
@@ -129,7 +128,7 @@ type HostDesc struct {
CreatingGuestCount int64 `json:"creating_guest_count"`
RunningGuestCount int64 `json:"running_guest_count"`
Groups *GroupCounts `json:"groups"`
//Groups *GroupCounts `json:"groups"`
Metadata map[string]string `json:"metadata"`
IsolatedDevices []*IsolatedDeviceDesc `json:"isolated_devices"`
IsMaintenance bool `json:"is_maintenance"`
@@ -219,6 +218,8 @@ func NewGuestReservedResourceUsedByBuilder(b *HostBuilder, host *computemodels.S
}
type HostBuilder struct {
*baseBuilder
residentTenantDict map[string]map[string]interface{}
hosts []computemodels.SHost
@@ -236,17 +237,17 @@ type HostBuilder struct {
hostGuests map[string][]interface{}
hostBackupGuests map[string][]interface{}
groupGuests []interface{}
groups []interface{}
groupDict map[string]interface{}
hostGroupCountDict HostGroupCountDict
//groupGuests []interface{}
//groups []interface{}
//groupDict map[string]interface{}
//hostGroupCountDict HostGroupCountDict
hostMetadatas []interface{}
hostMetadatasDict map[string][]interface{}
guestMetadatas []interface{}
guestMetadatasDict map[string][]interface{}
//hostMetadatas []interface{}
//hostMetadatasDict map[string][]interface{}
//guestMetadatas []interface{}
//guestMetadatasDict map[string][]interface{}
diskStats []models.StorageCapacity
//diskStats []models.StorageCapacity
isolatedDevicesDict map[string][]interface{}
cpuIOLoads map[string]map[string]float64
@@ -255,6 +256,12 @@ type HostBuilder struct {
zoneSkus map[string][]computemodels.SServerSku
}
func newHostBuilder() *HostBuilder {
return &HostBuilder{
baseBuilder: newBaseBuilder(HostDescBuilder),
}
}
func (h *HostDesc) String() string {
s, _ := json.Marshal(h)
return string(s)
@@ -499,7 +506,7 @@ func waitTimeOut(wg *WaitGroupWrapper, timeout time.Duration) bool {
}
}
func (b *HostBuilder) init(ids []string, dbCache DBGroupCacher, syncCache SyncGroupCacher) error {
func (b *HostBuilder) init(ids []string) error {
wg := &WaitGroupWrapper{}
errMessageChannel := make(chan error, 12)
defer close(errMessageChannel)
@@ -508,16 +515,8 @@ func (b *HostBuilder) init(ids []string, dbCache DBGroupCacher, syncCache SyncGr
func() { b.setSchedtags(ids, errMessageChannel) },
func() {
b.setGuests(ids, errMessageChannel)
b.setGroupInfo(errMessageChannel)
b.setMetadataInfo(ids, errMessageChannel)
b.setIsolatedDevs(ids, errMessageChannel)
},
func() {
//b.setStorages(ids, errMessageChannel)
b.setDiskStats(errMessageChannel)
},
func() { b.setMetadataInfo(ids, errMessageChannel) },
func() { b.setIsolatedDevs(ids, errMessageChannel) },
func() { b.setCPUIOLoadInfo(errMessageChannel) },
}
for _, f := range setFuncs {
@@ -577,33 +576,6 @@ func (b *HostBuilder) setSchedtags(ids []string, errMessageChannel chan error) {
b.schedtags = tags
}
//func (b *HostBuilder) setStorages(ids []string, errMessageChannel chan error) {
//q := computemodels.HoststorageManager.Query().In("host_id", ids)
//hostStorages := make([]computemodels.SHoststorage, 0)
//err := computedb.FetchModelObjects(computemodels.HoststorageManager, q, &hostStorages)
//if err != nil {
//errMessageChannel <- err
//return
//}
////hostStoragesDict := make(map[string][]*computemodels.SStorage)
//for _, s := range hostStorages {
//if ss, ok := hostStoragesDict[s.HostId]; !ok {
//storage := s.GetStorage()
//ss = make([]*computemodels.SStorage, 0)
//ss = append(ss, storage)
//hostStoragesDict[s.HostId] = ss
//} else {
//ss = append(ss, s.GetStorage())
//}
//}
//b.hostStorages = hostStorages
//b.hostStoragesDict = hostStoragesDict
//return
//}
func (b *HostBuilder) setGuests(ids []string, errMessageChannel chan error) {
guests, err := FetchGuestByHostIDs(ids)
if err != nil {
@@ -660,158 +632,154 @@ func (b *HostBuilder) setGuests(ids []string, errMessageChannel chan error) {
return
}
func (b *HostBuilder) setGroupInfo(errMessageChannel chan error) {
groupGuests, err := models.FetchByGuestIDs(models.GroupGuests, b.guestIDs)
if err != nil {
errMessageChannel <- err
return
}
//func (b *HostBuilder) setGroupInfo(errMessageChannel chan error) {
//groupGuests, err := models.FetchByGuestIDs(models.GroupGuests, b.guestIDs)
//if err != nil {
//errMessageChannel <- err
//return
//}
groupIds, err := utils.SelectDistinct(groupGuests, func(obj interface{}) (string, error) {
g, ok := obj.(*models.GroupGuest)
if !ok {
return "", utils.ConvertError(obj, "*models.GroupGuest")
}
return g.GroupID, nil
})
//groupIds, err := utils.SelectDistinct(groupGuests, func(obj interface{}) (string, error) {
//g, ok := obj.(*models.GroupGuest)
//if !ok {
//return "", utils.ConvertError(obj, "*models.GroupGuest")
//}
//return g.GroupID, nil
//})
if err != nil {
errMessageChannel <- err
return
}
//if err != nil {
//errMessageChannel <- err
//return
//}
groups, err := models.FetchGroupByIDs(groupIds)
if err != nil {
errMessageChannel <- err
return
}
//groups, err := models.FetchGroupByIDs(groupIds)
//if err != nil {
//errMessageChannel <- err
//return
//}
groupDict, err := utils.ToDict(groups, func(obj interface{}) (string, error) {
grp, ok := obj.(*models.Group)
if !ok {
return "", utils.ConvertError(obj, "*models.Group")
}
return grp.ID, nil
})
if err != nil {
errMessageChannel <- err
return
}
b.groups = groups
b.groupDict = groupDict
b.groupGuests = groupGuests
hostGroupCountDict, err := b.toHostGroupCountDict(groupGuests)
if err != nil {
errMessageChannel <- err
return
}
b.hostGroupCountDict = hostGroupCountDict
return
}
//groupDict, err := utils.ToDict(groups, func(obj interface{}) (string, error) {
//grp, ok := obj.(*models.Group)
//if !ok {
//return "", utils.ConvertError(obj, "*models.Group")
//}
//return grp.ID, nil
//})
//if err != nil {
//errMessageChannel <- err
//return
//}
//b.groups = groups
//b.groupDict = groupDict
//b.groupGuests = groupGuests
//hostGroupCountDict, err := b.toHostGroupCountDict(groupGuests)
//if err != nil {
//errMessageChannel <- err
//return
//}
//b.hostGroupCountDict = hostGroupCountDict
//return
//}
type GroupCount struct {
ID string `json:"id"` // group id
Name string `json:"name"` // group name
Count int64 `json:"count"` // guest count
}
//type GroupCount struct {
//ID string `json:"id"` // group id
//Name string `json:"name"` // group name
//Count int64 `json:"count"` // guest count
//}
type GroupCounts struct {
Data map[string]*GroupCount `json:"data"` // group_id: group_count
}
//type GroupCounts struct {
//Data map[string]*GroupCount `json:"data"` // group_id: group_count
//}
func NewGroupCounts() *GroupCounts {
return &GroupCounts{
Data: make(map[string]*GroupCount),
}
}
//func NewGroupCounts() *GroupCounts {
//return &GroupCounts{
//Data: make(map[string]*GroupCount),
//}
//}
type HostGroupCountDict map[string]*GroupCounts
//type HostGroupCountDict map[string]*GroupCounts
func (b *HostBuilder) toHostGroupCountDict(groupGuests []interface{}) (HostGroupCountDict, error) {
d := make(map[string]*GroupCounts)
for _, groupGuestObj := range groupGuests {
groupGuest := groupGuestObj.(*models.GroupGuest)
groupObj, grpOK := b.groupDict[groupGuest.GroupID]
guestObj, gstOK := b.guestDict[*groupGuest.GuestID]
if !grpOK || !gstOK {
continue
}
hostObj, ok := b.hostDict[guestObj.(*models.Guest).HostID]
if !ok {
continue
}
host := hostObj.(*models.Host)
group := groupObj.(*models.Group)
//func (b *HostBuilder) toHostGroupCountDict(groupGuests []interface{}) (HostGroupCountDict, error) {
//d := make(map[string]*GroupCounts)
//for _, groupGuestObj := range groupGuests {
//groupGuest := groupGuestObj.(*models.GroupGuest)
//groupObj, grpOK := b.groupDict[groupGuest.GroupID]
//guestObj, gstOK := b.guestDict[*groupGuest.GuestID]
//if !grpOK || !gstOK {
//continue
//}
//hostObj, ok := b.hostDict[guestObj.(*models.Guest).HostID]
//if !ok {
//continue
//}
//host := hostObj.(*models.Host)
//group := groupObj.(*models.Group)
counts, ok := d[host.ID]
if !ok {
counts = NewGroupCounts()
d[host.ID] = counts
}
count, ok := counts.Data[group.ID]
if !ok {
count = &GroupCount{ID: group.ID, Name: group.Name, Count: 1}
counts.Data[group.ID] = count
} else {
count.Count++
}
counts.Data[host.ID] = count
}
return d, nil
}
//counts, ok := d[host.ID]
//if !ok {
//counts = NewGroupCounts()
//d[host.ID] = counts
//}
//count, ok := counts.Data[group.ID]
//if !ok {
//count = &GroupCount{ID: group.ID, Name: group.Name, Count: 1}
//counts.Data[group.ID] = count
//} else {
//count.Count++
//}
//counts.Data[host.ID] = count
//}
//return d, nil
//}
func (b *HostBuilder) setMetadataInfo(hostIDs []string, errMessageChannel chan error) {
hostMetadataNames := []string{"dynamic_load_cpu_percent", "dynamic_load_io_util",
"enable_sriov", "bridge_driver"}
hostMetadataNames = append(hostMetadataNames, models.HostExtraFeature...)
hostMetadatas, err := models.FetchMetadatas(models.HostResourceName, hostIDs, hostMetadataNames)
if err != nil {
errMessageChannel <- err
return
}
guestMetadataNames := []string{"app_tags"}
guestMetadatas, err := models.FetchMetadatas(models.GuestResourceName, b.guestIDs, guestMetadataNames)
if err != nil {
errMessageChannel <- err
return
}
idFunc := func(obj interface{}) (string, error) {
metadata, ok := obj.(*models.Metadata)
if !ok {
return "", utils.ConvertError(obj, "*models.Metadata")
}
id := strings.Split(metadata.ID, "::")[1]
return id, nil
}
hostMetadatasDict, err := utils.GroupBy(hostMetadatas, idFunc)
if err != nil {
errMessageChannel <- err
return
}
guestMetadatasDict, err := utils.GroupBy(guestMetadatas, idFunc)
if err != nil {
errMessageChannel <- err
return
}
b.hostMetadatas = hostMetadatas
b.hostMetadatasDict = hostMetadatasDict
b.guestMetadatas = guestMetadatas
b.guestMetadatasDict = guestMetadatasDict
return
}
//func (b *HostBuilder) setMetadataInfo(hostIDs []string, errMessageChannel chan error) {
//hostMetadataNames := []string{"dynamic_load_cpu_percent", "dynamic_load_io_util",
//"enable_sriov", "bridge_driver"}
//hostMetadataNames = append(hostMetadataNames, models.HostExtraFeature...)
//hostMetadatas, err := models.FetchMetadatas(models.HostResourceName, hostIDs, hostMetadataNames)
//if err != nil {
//errMessageChannel <- err
//return
//}
//guestMetadataNames := []string{"app_tags"}
//guestMetadatas, err := models.FetchMetadatas(models.GuestResourceName, b.guestIDs, guestMetadataNames)
//if err != nil {
//errMessageChannel <- err
//return
//}
//idFunc := func(obj interface{}) (string, error) {
//metadata, ok := obj.(*models.Metadata)
//if !ok {
//return "", utils.ConvertError(obj, "*models.Metadata")
//}
//id := strings.Split(metadata.ID, "::")[1]
//return id, nil
//}
//hostMetadatasDict, err := utils.GroupBy(hostMetadatas, idFunc)
//if err != nil {
//errMessageChannel <- err
//return
//}
//guestMetadatasDict, err := utils.GroupBy(guestMetadatas, idFunc)
//if err != nil {
//errMessageChannel <- err
//return
//}
//b.hostMetadatas = hostMetadatas
//b.hostMetadatasDict = hostMetadatasDict
//b.guestMetadatas = guestMetadatas
//b.guestMetadatasDict = guestMetadatasDict
//return
//}
func (b *HostBuilder) setIsolatedDevs(ids []string, errMessageChannel chan error) {
devs, err := models.FetchByHostIDs(models.IsolatedDevices, ids)
if err != nil {
errMessageChannel <- err
return
}
devs := computemodels.IsolatedDeviceManager.FindByHosts(ids)
dict, err := utils.GroupBy(devs, func(obj interface{}) (string, error) {
dev, ok := obj.(*models.IsolatedDevice)
dev, ok := obj.(computemodels.SIsolatedDevice)
if !ok {
return "", utils.ConvertError(obj, "*models.IsolatedDevice")
return "", utils.ConvertError(obj, "computemodels.SIsolatedDevice")
}
return dev.HostID, nil
return dev.HostId, nil
})
if err != nil {
errMessageChannel <- err
@@ -820,7 +788,7 @@ func (b *HostBuilder) setIsolatedDevs(ids []string, errMessageChannel chan error
b.isolatedDevicesDict = dict
}
func (b *HostBuilder) setDiskStats(errMessageChannel chan error) {
/*func (b *HostBuilder) setDiskStats(errMessageChannel chan error) {
storageIDs := make([]string, len(b.storages))
func() {
for i, s := range b.storages {
@@ -840,41 +808,22 @@ func (b *HostBuilder) setDiskStats(errMessageChannel chan error) {
b.storageStatesSizeDict = storageStatesSizeDict
b.diskStats = capacities
return
}
func (b *HostBuilder) setCPUIOLoadInfo(errMessageChannel chan error) {
return
}
}*/
func (b *HostBuilder) Clone() BuildActor {
return &HostBuilder{}
}
func (b *HostBuilder) Type() string {
return HostDescBuilder
return &HostBuilder{
baseBuilder: newBaseBuilder(HostDescBuilder),
}
}
func (b *HostBuilder) AllIDs() ([]string, error) {
q := computemodels.HostManager.Query("id")
q = q.Filter(sqlchemy.NotEquals(q.Field("host_type"), computeapi.HOST_TYPE_BAREMETAL))
rs, err := q.Rows()
if err != nil {
return nil, err
}
ret := []string{}
defer rs.Close()
for rs.Next() {
var id string
if err := rs.Scan(&id); err != nil {
return nil, err
}
ret = append(ret, id)
}
return ret, nil
return FetchModelIds(q)
}
func (b *HostBuilder) Do(ids []string, dbCache DBGroupCacher, syncCache SyncGroupCacher) ([]interface{}, error) {
err := b.init(ids, dbCache, syncCache)
func (b *HostBuilder) Do(ids []string) ([]interface{}, error) {
err := b.init(ids)
if err != nil {
return nil, err
}
@@ -943,7 +892,7 @@ func (b *HostBuilder) buildOne(host *computemodels.SHost) (interface{}, error) {
fillFuncs := []func(*HostDesc, *computemodels.SHost) error{
b.fillGuestsResourceInfo,
b.fillResidentGroups,
//b.fillResidentGroups,
b.fillMetadata,
b.fillIsolatedDevices,
b.fillCPUIOLoads,
@@ -1011,14 +960,14 @@ func (b *HostBuilder) fillGuestsResourceInfo(desc *HostDesc, host *computemodels
cpuReqCount += int64(guest.VcpuCount)
memReqSize += int64(guest.VmemSize)
appTags := b.guestAppTags(guest)
for _, tag := range appTags {
if tag == "cpu_bound" {
cpuBoundCount += int64(guest.VcpuCount)
} else if tag == "io_bound" {
ioBoundCount++
}
}
//appTags := b.guestAppTags(guest)
//for _, tag := range appTags {
//if tag == "cpu_bound" {
//cpuBoundCount += int64(guest.VcpuCount)
//} else if tag == "io_bound" {
//ioBoundCount++
//}
//}
}
desc.GuestCount = guestCount
desc.CreatingGuestCount = creatingGuestCount
@@ -1073,7 +1022,7 @@ func (b *HostBuilder) fillGuestsResourceInfo(desc *HostDesc, host *computemodels
return nil
}
func (b *HostBuilder) guestAppTags(guest computemodels.SGuest) []string {
/*func (b *HostBuilder) guestAppTags(guest computemodels.SGuest) []string {
metadatas, ok := b.guestMetadatasDict[guest.GetId()]
if !ok {
return []string{}
@@ -1123,20 +1072,15 @@ func (b *HostBuilder) fillResidentGroups(desc *HostDesc, host *computemodels.SHo
}
desc.Groups = groups
return nil
}
}*/
func (b *HostBuilder) fillMetadata(desc *HostDesc, host *computemodels.SHost) error {
metadataObjs, ok := b.hostMetadatasDict[host.Id]
if !ok {
metadata, err := host.GetAllMetadata(nil)
if err != nil {
log.Errorf("Get host %s metadata: %v", desc.GetId(), err)
return nil
}
for _, obj := range metadataObjs {
metadata, ok := obj.(*models.Metadata)
if !ok {
return utils.ConvertError(obj, "*models.Metadata")
}
desc.Metadata[metadata.Key] = metadata.Value
}
desc.Metadata = metadata
return nil
}
@@ -1200,23 +1144,23 @@ func (i *IsolatedDeviceDesc) GetVendorModel() *VendorModel {
}
}
func (b *HostBuilder) getIsolatedDevices(hostID string) (devs []*models.IsolatedDevice) {
func (b *HostBuilder) getIsolatedDevices(hostID string) (devs []computemodels.SIsolatedDevice) {
devObjs, ok := b.isolatedDevicesDict[hostID]
devs = make([]*models.IsolatedDevice, 0)
devs = make([]computemodels.SIsolatedDevice, 0)
if !ok {
return
}
for _, obj := range devObjs {
dev := obj.(*models.IsolatedDevice)
dev := obj.(computemodels.SIsolatedDevice)
devs = append(devs, dev)
}
return
}
func (b *HostBuilder) getUsedIsolatedDevices(hostID string) (devs []*models.IsolatedDevice) {
devs = make([]*models.IsolatedDevice, 0)
func (b *HostBuilder) getUsedIsolatedDevices(hostID string) (devs []computemodels.SIsolatedDevice) {
devs = make([]computemodels.SIsolatedDevice, 0)
for _, dev := range b.getIsolatedDevices(hostID) {
if len(dev.GuestID) != 0 {
if len(dev.GuestId) != 0 {
devs = append(devs, dev)
}
}
@@ -1231,7 +1175,7 @@ func (b *HostBuilder) getIsolatedDeviceGuests(hostID string) (guests []computemo
}
ids := sets.NewString()
for _, dev := range usedDevs {
g, ok := b.guestDict[dev.GuestID]
g, ok := b.guestDict[dev.GuestId]
if !ok {
continue
}
@@ -1244,10 +1188,10 @@ func (b *HostBuilder) getIsolatedDeviceGuests(hostID string) (guests []computemo
return
}
func (b *HostBuilder) getUnusedIsolatedDevices(hostID string) (devs []*models.IsolatedDevice) {
devs = make([]*models.IsolatedDevice, 0)
func (b *HostBuilder) getUnusedIsolatedDevices(hostID string) (devs []computemodels.SIsolatedDevice) {
devs = make([]computemodels.SIsolatedDevice, 0)
for _, dev := range b.getIsolatedDevices(hostID) {
if len(dev.GuestID) == 0 {
if len(dev.GuestId) == 0 {
devs = append(devs, dev)
}
}
@@ -1264,13 +1208,13 @@ func (b *HostBuilder) fillIsolatedDevices(desc *HostDesc, host *computemodels.SH
devs := make([]*IsolatedDeviceDesc, len(allDevs))
for index, devModel := range allDevs {
dev := &IsolatedDeviceDesc{
ID: devModel.ID,
GuestID: devModel.GuestID,
HostID: devModel.HostID,
ID: devModel.Id,
GuestID: devModel.GuestId,
HostID: devModel.HostId,
DevType: devModel.DevType,
Model: devModel.Model,
Addr: devModel.Addr,
VendorDeviceID: devModel.VendorDeviceID,
VendorDeviceID: devModel.VendorDeviceId,
}
devs[index] = dev
}
+2 -1
View File
@@ -38,5 +38,6 @@ type BuildActor interface {
Clone() BuildActor
Type() string
AllIDs() ([]string, error)
Do(ids []string, db DBGroupCacher, sync SyncGroupCacher) ([]interface{}, error)
//Do(ids []string, db DBGroupCacher, sync SyncGroupCacher) ([]interface{}, error)
Do(ids []string) ([]interface{}, error)
}
-28
View File
@@ -1,28 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package db
import (
"yunion.io/x/onecloud/pkg/scheduler/cache"
)
type dbItem struct {
cache.CachedItem
fields []string
}
func NewCacheManager(stopCh <-chan struct{}) *cache.GroupManager {
return cache.NewGroupManager(CacheKind, DefaultCachedItems(), stopCh)
}
-148
View File
@@ -1,148 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package db
import (
"time"
u "yunion.io/x/pkg/utils"
"yunion.io/x/onecloud/pkg/scheduler/cache"
"yunion.io/x/onecloud/pkg/scheduler/db/models"
o "yunion.io/x/onecloud/pkg/scheduler/options"
)
const (
CacheKind = "DBCache"
StorageDBCache = "Storages"
WireDBCache = "Wires"
GroupDBCache = "Groups"
GroupGuestDBCache = "Groupguests"
GroupHostDBCache = "Grouphosts"
HostDBCache = "Hosts"
ClusterDBCache = "Clusters"
ClusterHostDBCache = "Clusterhosts"
HostWireDBCache = "Hostwires"
AggregateDBCache = "Aggregates"
AggregateHostDBCache = "AggregateHosts"
AggregateBaremetalDBCache = "AggregateBaremetals"
BaremetalAgentDBCache = "BaremetalAgents"
NetworksDBCache = "Networks"
NetInterfacesDBCache = "NetInterfaces"
WiresDBCache = "Wires"
)
func getUpdate(d []interface{}) ([]string, error) {
return nil, nil
}
func DefaultCachedItems() []cache.CachedItem {
if !models.DBValid() {
panic("DB not init before cache items")
}
return []cache.CachedItem{
newClusterDBCache(),
newBaremetalAgentDBCache(),
newAggregateDBCache(),
newAggregateHostDBCache(),
}
}
func NewCachedItems(items []string) (cachedItems []cache.CachedItem) {
if !models.DBValid() {
panic("DB not init before cache items")
}
for _, item := range items {
switch item {
case NetworksDBCache:
cachedItems = append(cachedItems, newNetworksDBCache())
case NetInterfacesDBCache:
cachedItems = append(cachedItems, newNetInterfacesDBCache())
case WiresDBCache:
cachedItems = append(cachedItems, newWiresDBCache())
default:
return nil
}
}
return
}
func uuidKey(obj interface{}) (string, error) {
return obj.(models.Modeler).UUID(), nil
}
func newDBCache(name string, r models.Resourcer, ttl, period time.Duration) cache.CachedItem {
update := func(ids []string) ([]interface{}, error) {
return models.FetchByIDs(r, ids)
}
load := func() ([]interface{}, error) {
return models.All(r)
}
item := new(dbItem)
item.CachedItem = cache.NewCacheItem(
name, ttl, period, uuidKey, update, load, getUpdate,
)
return item
}
func newClusterDBCache() cache.CachedItem {
return newDBCache(ClusterDBCache, models.Clusters,
u.ToDuration(o.GetOptions().ClusterDBCacheTTL),
u.ToDuration(o.GetOptions().ClusterDBCachePeriod))
}
func newBaremetalAgentDBCache() cache.CachedItem {
return newDBCache(BaremetalAgentDBCache, models.BaremetalAgents,
u.ToDuration(o.GetOptions().BaremetalAgentDBCacheTTL),
u.ToDuration(o.GetOptions().BaremetalAgentDBCachePeriod))
}
func newAggregateDBCache() cache.CachedItem {
return newDBCache(AggregateDBCache, models.Aggregates,
u.ToDuration(o.GetOptions().AggregateDBCacheTTL),
u.ToDuration(o.GetOptions().AggregateDBCachePeriod))
}
func newAggregateHostDBCache() cache.CachedItem {
return newDBCache(AggregateHostDBCache, models.AggregateHosts,
u.ToDuration(o.GetOptions().AggregateHostDBCacheTTL),
u.ToDuration(o.GetOptions().AggregateHostDBCachePeriod))
}
func newNetworksDBCache() cache.CachedItem {
return newDBCache(NetworksDBCache, models.Networks,
u.ToDuration(o.GetOptions().NetworksDBCacheTTL),
u.ToDuration(o.GetOptions().NetworksDBCachePeriod))
}
func newNetInterfacesDBCache() cache.CachedItem {
return newDBCache(AggregateHostDBCache, models.NetInterfaces,
u.ToDuration(o.GetOptions().NetinterfaceDBCacheTTL),
u.ToDuration(o.GetOptions().NetinterfaceDBCachePeriod))
}
func newWiresDBCache() cache.CachedItem {
return newDBCache(AggregateHostDBCache, models.Wires,
u.ToDuration(o.GetOptions().WireDBCacheTTL),
u.ToDuration(o.GetOptions().WireDBCachePeriod))
}
-15
View File
@@ -1,15 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package db // import "yunion.io/x/onecloud/pkg/scheduler/cache/db"
-95
View File
@@ -1,95 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sync
import (
u "yunion.io/x/pkg/utils"
"yunion.io/x/onecloud/pkg/scheduler/cache"
networks_db "yunion.io/x/onecloud/pkg/scheduler/cache/sync/networks/db"
o "yunion.io/x/onecloud/pkg/scheduler/options"
)
const (
CacheKind = "SyncCache"
//GlanceSyncCache = "Glance"
NetworkSyncCache = "Network"
NetworksDataSyncCache = "NetworkData"
)
func getUpdate(d []interface{}) ([]string, error) {
return nil, nil
}
func defaultSyncItems() []cache.CachedItem {
return []cache.CachedItem{
//newGlanceCache(),
//newNetworkCache(),
//newNetworksDataCache(),
}
}
func noneUpdate(id []string) ([]interface{}, error) {
return []interface{}{}, nil
}
/*
func newGlanceCache() cache.CachedItem {
item := new(syncItem)
item.CachedItem = cache.NewCacheItem(
GlanceSyncCache,
viper.GetDuration("cache.glance_cache.ttl"),
viper.GetDuration("cache.glance_cache.period"),
imageUUIDKey,
noneUpdate,
loadImages,
getUpdate,
)
return item
}*/
func newNetworkCache() cache.CachedItem {
item := new(syncItem)
// data from db
item.CachedItem = cache.NewCacheItem(
NetworkSyncCache,
u.ToDuration(o.GetOptions().NetworkCacheTTL),
u.ToDuration(o.GetOptions().NetworkCachePeriod),
networks_db.BuilderCacheKey,
networks_db.UpdateNetworkDescBuilder,
networks_db.LoadNetworkDescBuilder,
getUpdate,
)
return item
}
func newNetworksDataCache() cache.CachedItem {
item := new(syncItem)
item.CachedItem = cache.NewCacheItem(
NetworksDataSyncCache,
u.ToDuration(o.GetOptions().NetworkCacheTTL),
u.ToDuration(o.GetOptions().NetworkCachePeriod),
BuilderNetworkCacheKey,
updateNetworksBuilder,
loadNetworksBuilder,
getUpdate,
)
return item
}
-15
View File
@@ -1,15 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sync // import "yunion.io/x/onecloud/pkg/scheduler/cache/sync"
-48
View File
@@ -1,48 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sync
import (
"yunion.io/x/pkg/utils"
"yunion.io/x/onecloud/pkg/scheduler/db/models"
)
const (
HostNetworkDescBuilderCache = "HostNetworkDescBuilderCache"
BaremetalNetworkDescBuilderCache = "BaremetalNetworkDescBuilderCache"
)
func avaliableAddress(network *models.WireNetwork) (int, error) {
totalAddress := utils.IpRangeCount(network.GuestIpStart, network.GuestIpEnd)
guestNicCount, err := models.GuestNicCountsWithNetworkID(network.ID)
if err != nil {
return 0, err
}
groupNicCount, err := models.GroupNicCountsWithNetworkID(network.ID)
if err != nil {
return 0, err
}
baremetalNicCount, err := models.BaremetalNicCountsWithNetworkID(network.ID)
if err != nil {
return 0, err
}
reserveNicCount, err := models.ReserveNicCountsWithNetworkID(network.ID)
if err != nil {
return 0, err
}
return totalAddress - guestNicCount.Count - groupNicCount.Count - baremetalNicCount.Count - reserveNicCount.Count, nil
}
-260
View File
@@ -1,260 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sync
import (
"fmt"
"yunion.io/x/log"
"yunion.io/x/pkg/utils"
"yunion.io/x/onecloud/pkg/scheduler/db/models"
)
const (
NetworksBuilderCache = "NetworksBuilderCache"
GuestNiCount = "GuestNiCount"
GroupNicCount = "GroupNicCount"
BaremetalNicCount = "BaremetalNicCount"
ReserveDipNicCount = "ReserveDipNicCount"
)
type SchedNetworkBuildResult struct {
ID string `json:"id"`
Name string `json:"name"`
TenantID string `json:"tenant_id"`
IsPublic bool `json:"is_public"`
ServerType string `json:"server_type"`
Ports int `json:"ports"`
IsExit bool `json:"is_exit"`
Wire string `json:"wire_name"`
WireID string `json:"wire_id"`
}
func BuilderNetworkCacheKey(obj interface{}) (string, error) {
builder, ok := obj.(NetworksBuilder)
if !ok {
return "", fmt.Errorf("Not a NetworksDataSyncCache: %v", obj)
}
return builder.GetKey(), nil
}
func updateNetworksBuilder(keys []string) ([]interface{}, error) {
builders := make([]NetworksBuilder, 0)
for _, key := range keys {
switch key {
case NetworksBuilderCache:
builders = append(builders, NewNetworksBuilder())
default:
return nil, fmt.Errorf("Not support update, key: %v", key)
}
}
ret := []interface{}{}
for _, builder := range builders {
_, err := builder.LoadAll()
if err != nil {
log.Errorf("Network load error: %v", err)
}
ret = append(ret, builder)
}
return ret, nil
}
type NetworksBuilder interface {
LoadAll() (map[string]*SchedNetworkBuildResult, error)
Load(ids []string) (map[string]*SchedNetworkBuildResult, error)
GetKey() string
GetNetworksData(ids []string) []*SchedNetworkBuildResult
}
func loadNetworksBuilder() ([]interface{}, error) {
builders := []NetworksBuilder{
NewNetworksBuilder(),
}
rets := []interface{}{}
for _, builder := range builders {
_, err := builder.LoadAll()
if err != nil {
log.Errorf("Network load error: %v", err)
} else {
rets = append(rets, builder)
}
}
return rets, nil
}
type NetworksDataBuilder struct {
GuestNicCount map[string]int
GroupNicCount map[string]int
BaremetalNicCount map[string]int
ReserveDipNicCount map[string]int
Wires map[string]string
data map[string]*SchedNetworkBuildResult
}
func NewNetworksBuilder() *NetworksDataBuilder {
return &NetworksDataBuilder{
GuestNicCount: make(map[string]int),
GroupNicCount: make(map[string]int),
BaremetalNicCount: make(map[string]int),
ReserveDipNicCount: make(map[string]int),
data: make(map[string]*SchedNetworkBuildResult),
}
}
func (b *NetworksDataBuilder) GetKey() string {
return NetworksBuilderCache
}
func (b *NetworksDataBuilder) LoadAll() (map[string]*SchedNetworkBuildResult, error) {
return b.Load(nil)
}
func (b *NetworksDataBuilder) toSchedNetworkBuildResult(network *models.Network) (*SchedNetworkBuildResult, error) {
if network == nil {
return nil, fmt.Errorf("empty network resource.")
}
res := new(SchedNetworkBuildResult)
res.WireID = network.WireID
res.Wire = b.Wires[network.WireID]
res.ID = network.ID
ports, err := b.avaliableAddress(network)
if err != nil {
return nil, err
} else {
res.Ports = ports
}
res.Name = network.Name
res.TenantID = network.TenantID
res.IsPublic = network.IsPublic == 1
res.ServerType = network.ServerType
res.IsExit = utils.IsExitAddress(network.GuestIpStart)
return res, nil
}
func (b *NetworksDataBuilder) avaliableAddress(network *models.Network) (int, error) {
totalAddress := utils.IpRangeCount(network.GuestIpStart, network.GuestIpEnd)
return totalAddress - b.GuestNicCount[network.ID] - b.GroupNicCount[network.ID] - b.BaremetalNicCount[network.ID] - b.ReserveDipNicCount[network.ID], nil
}
func getNiCount(nicName string) (map[string]int, error) {
countsMap := make(map[string]int)
switch nicName {
case GuestNiCount:
counts, err := models.GuestNicCounts()
if err != nil {
return nil, err
}
for _, count := range counts {
countsMap[count.NetworkID] = count.Count
}
case GroupNicCount:
counts, err := models.GroupNicCounts()
if err != nil {
return nil, err
}
for _, count := range counts {
countsMap[count.NetworkID] = count.Count
}
case BaremetalNicCount:
counts, err := models.BaremetalNicCounts()
if err != nil {
return nil, err
}
for _, count := range counts {
countsMap[count.NetworkID] = count.Count
}
case ReserveDipNicCount:
counts, err := models.ReserveNicCounts()
if err != nil {
return nil, err
}
for _, count := range counts {
countsMap[count.NetworkID] = count.Count
}
}
return countsMap, nil
}
func (b *NetworksDataBuilder) Load(ids []string) (map[string]*SchedNetworkBuildResult, error) {
wireInfos, err := models.LoadAllWires()
if err != nil {
return nil, err
}
wiresMap := make(map[string]string, len(wireInfos))
for _, wire := range wireInfos {
wiresMap[wire.ID] = wire.Name
}
b.Wires = wiresMap
b.GuestNicCount, err = getNiCount(GuestNiCount)
if err != nil {
log.Errorln(err)
}
b.GroupNicCount, err = getNiCount(GroupNicCount)
if err != nil {
log.Errorln(err)
}
b.BaremetalNicCount, err = getNiCount(BaremetalNicCount)
if err != nil {
log.Errorln(err)
}
b.ReserveDipNicCount, err = getNiCount(ReserveDipNicCount)
if err != nil {
log.Errorln(err)
}
networks, err := models.All(models.Networks)
if err != nil {
return nil, err
}
for _, network := range networks {
n := network.(*models.Network)
b.data[n.ID], err = b.toSchedNetworkBuildResult(n)
if err != nil {
log.Errorln(err)
}
}
return b.data, nil
}
func (b *NetworksDataBuilder) GetNetworksData(ids []string) (networks []*SchedNetworkBuildResult) {
for _, networkID := range ids {
if network, ok := b.data[networkID]; ok {
networks = append(networks, network)
}
}
return
}
-308
View File
@@ -1,308 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package db
import (
"fmt"
"strings"
"yunion.io/x/log"
"yunion.io/x/onecloud/pkg/scheduler/db/models"
)
const (
HostNetworkDescBuilderCache = "HostNetworkDescBuilderCache"
BaremetalNetworkDescBuilderCache = "BaremetalNetworkDescBuilderCache"
)
type NetworkDescBuilder interface {
LoadAll() (map[string][]string, error)
Load(ids []string) (map[string][]string, error)
GetKey() string
GetNetworkDesc(id string) ([]string, error)
}
func BuilderCacheKey(obj interface{}) (string, error) {
builder, ok := obj.(NetworkDescBuilder)
if !ok {
return "", fmt.Errorf("Not a NetworkDescBuilder: %v", obj)
}
return builder.GetKey(), nil
}
func LoadNetworkDescBuilder() ([]interface{}, error) {
builders := []NetworkDescBuilder{
NewHostNetworkDescBuilder(),
//NewBaremetalNetworkDescBuilder(),
}
rets := []interface{}{}
for _, builder := range builders {
_, err := builder.LoadAll()
if err != nil {
log.Errorf("Network load error: %v", err)
} else {
rets = append(rets, builder)
}
}
return rets, nil
}
func UpdateNetworkDescBuilder(keys []string) ([]interface{}, error) {
builders := make([]NetworkDescBuilder, 0)
for _, key := range keys {
switch key {
case HostNetworkDescBuilderCache:
builders = append(builders, NewHostNetworkDescBuilder())
case BaremetalNetworkDescBuilderCache:
builders = append(builders, NewBaremetalNetworkDescBuilder())
default:
return nil, fmt.Errorf("Not support update, key: %v", key)
}
}
ret := []interface{}{}
for _, builder := range builders {
_, err := builder.LoadAll()
if err != nil {
log.Errorf("Network load error: %v", err)
}
ret = append(ret, builder)
}
return ret, nil
}
type HostNetworkDescBuilder struct {
data map[string][]string
host2Wires map[string]string
wire2Networks map[string]string
}
func NewHostNetworkDescBuilder() *HostNetworkDescBuilder {
return &HostNetworkDescBuilder{
data: make(map[string][]string),
host2Wires: make(map[string]string),
wire2Networks: make(map[string]string),
}
}
func (b *HostNetworkDescBuilder) GetKey() string {
return HostNetworkDescBuilderCache
}
func (b *HostNetworkDescBuilder) LoadAll() (map[string][]string, error) {
return b.Load(nil)
}
func (b *HostNetworkDescBuilder) Load(ids []string) (map[string][]string, error) {
// wireInfos, err := models.LoadAllWires()
// if err != nil {
// return nil, err
// }
// wiresMap := make(map[string]string, len(wireInfos))
// for _, wire := range wireInfos {
// wiresMap[wire.ID] = wire.Name
// }
hostAndWires, err := models.SelectHostHasWires()
if err != nil {
return nil, err
}
hostHasWires := make(map[string]string)
for _, hostAndWire := range hostAndWires {
if _, ok := hostHasWires[hostAndWire.HostID]; ok {
if !strings.Contains(hostHasWires[hostAndWire.HostID], hostAndWire.WireID) {
hostHasWires[hostAndWire.HostID] = fmt.Sprintf("%s;%s", hostHasWires[hostAndWire.HostID], hostAndWire.WireID)
}
} else {
hostHasWires[hostAndWire.HostID] = hostAndWire.WireID
}
}
b.host2Wires = hostHasWires
wiresAndNetworks, err := models.SelectWireIDsHasNetworks()
if err != nil {
return nil, err
}
wireHasNetworks := make(map[string]string)
for _, wireAndNetwork := range wiresAndNetworks {
if _, ok := wireHasNetworks[wireAndNetwork.WireID]; ok {
if !strings.Contains(wireHasNetworks[wireAndNetwork.WireID], wireAndNetwork.ID) {
wireHasNetworks[wireAndNetwork.WireID] = fmt.Sprintf("%s;%s", wireHasNetworks[wireAndNetwork.WireID], wireAndNetwork.ID)
}
} else {
wireHasNetworks[wireAndNetwork.WireID] = wireAndNetwork.ID
}
}
b.wire2Networks = wireHasNetworks
if len(ids) == 0 {
hostIDs, err := models.AllHostIDs()
if err != nil {
log.Errorln(err)
}
for _, hostID := range hostIDs {
networkResults, err := b.loadNetworks(hostID)
if err != nil {
log.Errorln(err)
} else {
b.data[hostID] = networkResults
}
}
} else {
for _, hostID := range ids {
networkResults, err := b.loadNetworks(hostID)
if err != nil {
log.Errorln(err)
} else {
b.data[hostID] = networkResults
}
}
}
return b.data, nil
}
func (b *HostNetworkDescBuilder) loadNetworks(hostID string) (networkResults []string, err error) {
wires := strings.Split(b.host2Wires[hostID], ";")
for _, wire := range wires {
networkResults = append(networkResults, strings.Split(b.wire2Networks[wire], ";")...)
}
return networkResults, nil
}
func (b *HostNetworkDescBuilder) GetNetworkDesc(id string) ([]string, error) {
if r, ok := b.data[id]; ok {
return r, nil
}
return nil, fmt.Errorf("can not find networks")
}
type BaremetalNetworkDescBuilder struct {
data map[string][]string
baremetal2Wires map[string]string
wire2Networks map[string]string
}
// TODO:we should not new a object every time, the map memory leak, if want map
// was GCed, you must set b.data = nil and so on.
func NewBaremetalNetworkDescBuilder() *BaremetalNetworkDescBuilder {
return &BaremetalNetworkDescBuilder{
data: make(map[string][]string, 30000), // the max number of baremetal if about 27000.
baremetal2Wires: make(map[string]string, 30000),
wire2Networks: make(map[string]string, 0),
}
}
func (b *BaremetalNetworkDescBuilder) GetKey() string {
return BaremetalNetworkDescBuilderCache
}
func (b *BaremetalNetworkDescBuilder) LoadAll() (map[string][]string, error) {
return b.Load(nil)
}
func (b *BaremetalNetworkDescBuilder) Load(ids []string) (map[string][]string, error) {
// wireInfos, err := models.LoadAllWires()
// if err != nil {
// return nil, err
// }
// wiresMap := make(map[string]string, len(wireInfos))
// for _, wire := range wireInfos {
// wiresMap[wire.ID] = wire.Name
// }
baremetalsAndWires, err := models.SelectWiresAndBaremetals()
if err != nil {
return nil, err
}
baremetalHasWires := make(map[string]string)
for _, baremetalsAndWire := range baremetalsAndWires {
if _, ok := baremetalHasWires[baremetalsAndWire.BaremetalID]; ok {
if !strings.Contains(baremetalHasWires[baremetalsAndWire.BaremetalID], baremetalsAndWire.WireID) {
baremetalHasWires[baremetalsAndWire.BaremetalID] = fmt.Sprintf("%s;%s", baremetalHasWires[baremetalsAndWire.BaremetalID], baremetalsAndWire.WireID)
}
} else {
baremetalHasWires[baremetalsAndWire.BaremetalID] = baremetalsAndWire.WireID
}
}
b.baremetal2Wires = baremetalHasWires
wiresAndNetworks, err := models.SelectWireIDsHasNetworks()
if err != nil {
return nil, err
}
wireHasNetworks := make(map[string]string)
for _, wireAndNetwork := range wiresAndNetworks {
if _, ok := wireHasNetworks[wireAndNetwork.WireID]; ok {
if !strings.Contains(wireHasNetworks[wireAndNetwork.WireID], wireAndNetwork.ID) {
wireHasNetworks[wireAndNetwork.WireID] = fmt.Sprintf("%s;%s", wireHasNetworks[wireAndNetwork.WireID], wireAndNetwork.ID)
}
} else {
wireHasNetworks[wireAndNetwork.WireID] = wireAndNetwork.ID
}
}
b.wire2Networks = wireHasNetworks
if len(ids) == 0 {
baremetalIDs, err := models.AllBaremetalIDs()
if err != nil {
log.Errorln(err)
}
for _, baremetalID := range baremetalIDs {
networkResults, err := b.loadNetworks(baremetalID)
if err != nil {
log.Errorln(err)
} else {
b.data[baremetalID] = networkResults
}
}
} else {
for _, baremetalID := range ids {
networkResults, err := b.loadNetworks(baremetalID)
if err != nil {
log.Errorln(err)
} else {
b.data[baremetalID] = networkResults
}
}
}
return b.data, nil
}
func (b *BaremetalNetworkDescBuilder) loadNetworks(baremetalID string) (networkResults []string, err error) {
wires := strings.Split(b.baremetal2Wires[baremetalID], ";")
for _, wire := range wires {
networkResults = append(networkResults, strings.Split(b.wire2Networks[wire], ";")...)
}
return networkResults, nil
}
func (b *BaremetalNetworkDescBuilder) GetNetworkDesc(id string) ([]string, error) {
if result, ok := b.data[id]; ok {
return result, nil
}
return nil, fmt.Errorf("can not find networks")
}
-15
View File
@@ -1,15 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package db // import "yunion.io/x/onecloud/pkg/scheduler/cache/sync/networks/db"
-28
View File
@@ -1,28 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sync
import (
"yunion.io/x/onecloud/pkg/scheduler/cache"
)
func NewSyncManager(stopCh <-chan struct{}) *cache.GroupManager {
items := defaultSyncItems()
return cache.NewGroupManager(CacheKind, items, stopCh)
}
type syncItem struct {
cache.CachedItem
}
@@ -18,13 +18,10 @@ import (
"fmt"
"time"
//"yunion.io/x/pkg/util/ttlpool"
"yunion.io/x/pkg/utils"
"yunion.io/x/onecloud/pkg/scheduler/cache"
candidatecache "yunion.io/x/onecloud/pkg/scheduler/cache/candidate"
dbcache "yunion.io/x/onecloud/pkg/scheduler/cache/db"
synccache "yunion.io/x/onecloud/pkg/scheduler/cache/sync"
"yunion.io/x/onecloud/pkg/scheduler/core"
)
@@ -37,24 +34,20 @@ type CandidateGetArgs struct {
}
type DataManager struct {
DBCacheGroup cache.CacheGroup
SyncCacheGroup cache.CacheGroup
CandidateGroup cache.CacheGroup
}
func NewDataManager(stopCh <-chan struct{}) *DataManager {
m := new(DataManager)
m.DBCacheGroup = dbcache.NewCacheManager(stopCh)
m.SyncCacheGroup = synccache.NewSyncManager(stopCh)
m.CandidateGroup = candidatecache.NewCandidateManager(
m.DBCacheGroup, m.SyncCacheGroup, stopCh)
//m.SyncCacheGroup = synccache.NewSyncManager(stopCh)
m.CandidateGroup = candidatecache.NewCandidateManager(stopCh)
return m
}
func (m *DataManager) Run() {
go m.DBCacheGroup.Run()
go m.SyncCacheGroup.Run()
//go m.SyncCacheGroup.Run()
go m.CandidateGroup.Run()
}
@@ -332,16 +325,6 @@ func (cm *CandidateManager) Run() {
}
}
func (cm *CandidateManager) GetData(name string) ([]interface{}, error) {
cache, err := cm.dataManager.DBCacheGroup.Get(name)
if err != nil {
return nil, err
}
cache.WaitForReady()
return cache.List(), nil
}
func (cm *CandidateManager) Reload(resType string, candidateIds []string) (
[]interface{}, error) {
@@ -14,7 +14,7 @@
package data_manager
import (
/*import (
"sync"
"yunion.io/x/log"
@@ -179,4 +179,4 @@ func (m *NetworkManager) getNetworkCache() (cache.Cache, error) {
cache.WaitForReady()
return cache, nil
}
}*/
@@ -1,268 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package data_manager
import (
"yunion.io/x/pkg/utils"
candidatecache "yunion.io/x/onecloud/pkg/scheduler/cache/candidate"
)
type ResAlgorithm interface {
Sum(values []value_t) value_t
Subtract(sum value_t, value value_t) value_t
}
//////// //////// //////// //////// //////// ////////
type DefaultResAlgorithm struct {
}
func (al *DefaultResAlgorithm) Sum(values []value_t) value_t {
var ret int64 = 0
for _, value := range values {
if value != nil {
ret += value.(int64)
}
}
return ret
}
func (al *DefaultResAlgorithm) Subtract(sum value_t, value value_t) value_t {
if sum == nil {
return nil
}
if value == nil {
return sum
}
return sum.(int64) - value.(int64)
}
//////// //////// //////// //////// //////// ////////
type GroupResAlgorithmResult struct {
Groups []*candidatecache.GroupCounts
}
func NewGroupResAlgorithmResult() *GroupResAlgorithmResult {
return &GroupResAlgorithmResult{
Groups: nil,
}
}
func (r *GroupResAlgorithmResult) GuestCountOfGroup(groupId string) int64 {
count := int64(0)
for _, groupCounts := range r.Groups {
if groupCount, ok := groupCounts.Data[groupId]; ok {
count += groupCount.Count
}
}
return count
}
func (r *GroupResAlgorithmResult) ExistsGroup(groupId string) bool {
for _, groupCounts := range r.Groups {
if groupCount, ok := groupCounts.Data[groupId]; ok {
if groupCount.Count > 0 {
return true
}
}
}
return false
}
type GroupResAlgorithm struct {
}
func (al *GroupResAlgorithm) Sum(values []value_t) value_t {
r := NewGroupResAlgorithmResult()
for _, value := range values {
if value != nil {
if v, ok := value.(*candidatecache.GroupCounts); ok {
r.Groups = append(r.Groups, v)
} else if v, ok := value.(*GroupResAlgorithmResult); ok {
r.Groups = append(r.Groups, v.Groups...)
}
}
}
return r
}
func (al *GroupResAlgorithm) Subtract(sum value_t, value value_t) value_t {
r := NewGroupResAlgorithmResult()
if sum != nil {
groups, _ := sum.(*candidatecache.GroupCounts)
if groups != nil && len(groups.Data) > 0 {
r.Groups = append(r.Groups, groups)
}
}
if value != nil {
grar, _ := value.(*GroupResAlgorithmResult)
if grar != nil && len(grar.Groups) > 0 {
r.Groups = append(r.Groups, grar.Groups...)
}
}
return r
}
//////// //////// //////// //////// //////// ////////
type NetworksResAlgorithmResult struct {
Networks map[string]int
}
type NetworksResAlgorithm struct {
}
func (a *NetworksResAlgorithm) Sum(values []value_t) value_t {
countOfNetworks := 0
for _, value := range values {
if value != nil {
if v, ok := value.(int); ok {
countOfNetworks += v
}
}
}
return countOfNetworks
}
func (a *NetworksResAlgorithm) Subtract(sum value_t, value value_t) value_t {
r := make(map[string]int)
return r
}
type GroupGuestRelation struct {
Data map[string]*candidatecache.GroupCount
}
//////// //////// //////// //////// //////// ////////
type IsolatedDeviceResAlgorithmResult struct {
IDs map[string]int
}
func newIsolatedDeviceResAlgorithmResult() *IsolatedDeviceResAlgorithmResult {
return &IsolatedDeviceResAlgorithmResult{
IDs: make(map[string]int),
}
}
func (r *IsolatedDeviceResAlgorithmResult) appendDevices(value value_t) {
if devices, ok := value.([]*candidatecache.IsolatedDeviceDesc); ok {
for _, dev := range devices {
r.IDs[dev.ID] = 0
}
} else if r2, ok := value.(*IsolatedDeviceResAlgorithmResult); ok {
for id := range r2.IDs {
r.IDs[id] = 0
}
} else if ids, ok := value.([]string); ok {
for _, id := range ids {
r.IDs[id] = 0
}
}
}
func (r *IsolatedDeviceResAlgorithmResult) removeDevices(value value_t) {
if devices, ok := value.([]*candidatecache.IsolatedDeviceDesc); ok {
for _, dev := range devices {
delete(r.IDs, dev.ID)
}
} else if r2, ok := value.(*IsolatedDeviceResAlgorithmResult); ok {
for id := range r2.IDs {
delete(r.IDs, id)
}
} else if ids, ok := value.([]string); ok {
for _, id := range ids {
delete(r.IDs, id)
}
}
}
type IsolatedDeviceResAlgorithm struct {
}
func (a *IsolatedDeviceResAlgorithm) Sum(values []value_t) value_t {
r := newIsolatedDeviceResAlgorithmResult()
for _, v := range values {
r.appendDevices(v)
}
return r
}
func (al *IsolatedDeviceResAlgorithm) Subtract(sum value_t, value value_t) value_t {
if sum == nil || value == nil {
return sum
}
reserved := value.(*IsolatedDeviceResAlgorithmResult)
var isolatedDevices []*candidatecache.IsolatedDeviceDesc
for _, dev := range sum.([]*candidatecache.IsolatedDeviceDesc) {
if _, ok := reserved.IDs[dev.ID]; !ok {
isolatedDevices = append(isolatedDevices, dev)
}
}
return isolatedDevices
}
//////// //////// //////// //////// //////// ////////
var (
g_defaultResAlgorithm *DefaultResAlgorithm = &DefaultResAlgorithm{}
g_groupResAlgorithm *GroupResAlgorithm = &GroupResAlgorithm{}
g_networksResAlgorithm *NetworksResAlgorithm = &NetworksResAlgorithm{}
g_isolatedDeviceResAlgorithm *IsolatedDeviceResAlgorithm = &IsolatedDeviceResAlgorithm{}
)
func GetResAlgorithm(res_name string) ResAlgorithm {
switch res_name {
case "Groups":
return g_groupResAlgorithm
case "IsolatedDevices":
return g_isolatedDeviceResAlgorithm
case "FreeCPUCount", "FreeMemSize", "FreeLocalStorageSize":
return g_defaultResAlgorithm
case "Ports":
return g_defaultResAlgorithm
default:
if utils.HasPrefix(res_name, "FreeStorageSize:") {
return g_defaultResAlgorithm
}
return nil
}
}
@@ -1,102 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package data_manager
import (
"fmt"
"sync"
)
// Reserved pool manager is mainly to cloud resources to do state
// updates, currently divided into host, baemetal there are three
// network resources management.
type ReservedPoolManager struct {
// store all reserved data
pools map[string]*ReservedPool
stopCh <-chan struct{}
sync.RWMutex
}
func (pm *ReservedPoolManager) GetPool(name string) (*ReservedPool, error) {
pm.RLock()
pool, ok := pm.pools[name]
if !ok {
return nil, fmt.Errorf("reserved pool %v not found", name)
}
pm.RUnlock()
return pool, nil
}
func (pm *ReservedPoolManager) addPool(pool *ReservedPool) {
pm.Lock()
// add or update
pm.pools[pool.Name] = pool
pm.Unlock()
pool.Start()
}
func NewReservedPoolManager(stopCh <-chan struct{}) *ReservedPoolManager {
pm := &ReservedPoolManager{
pools: make(map[string]*ReservedPool),
stopCh: stopCh,
}
pm.addPool(NewReservedPool("host", stopCh))
pm.addPool(NewReservedPool("baremetal", stopCh))
pm.addPool(NewReservedPool("networks", stopCh))
return pm
}
func (pm *ReservedPoolManager) SearchReservedPoolBySessionID(sessionId string) (
*ReservedPool, error) {
for _, pool := range pm.pools {
if pool.GetSessionItem(sessionId) != nil {
return pool, nil
}
}
return nil, fmt.Errorf("session id: %v not found", sessionId)
}
func (pm *ReservedPoolManager) InSession(resType string, candidateId string) bool {
if pool, err := pm.GetPool(resType); err == nil {
return pool.InSession(candidateId)
}
return false
}
func (pm *ReservedPoolManager) RemoveSession(sessionId string) bool {
for _, pool := range pm.pools {
if pool.RemoveSession(sessionId) {
return true
}
}
return false
}
func ReservedSubtract(key string, value value_t, reserved value_t) value_t {
var al ResAlgorithm = GetResAlgorithm(key)
if al != nil {
return al.Subtract(value, reserved)
}
return value
}
func ReservedSum(key string, values []value_t) value_t {
var al ResAlgorithm = GetResAlgorithm(key)
if al != nil {
return al.Sum(values)
}
return nil
}
-320
View File
@@ -1,320 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package data_manager
import (
"sync"
"time"
"yunion.io/x/pkg/util/wait"
)
const (
SessionExpiredTime = 30 * 60 // Seconds
)
type value_t interface{}
type KeyValue interface {
Get(key string) interface{}
}
// ReservedItem
type ReservedItem struct {
CandidateId string
data map[string]value_t
sync.RWMutex
}
func NewReservedItem(candidateID string) *ReservedItem {
return &ReservedItem{
CandidateId: candidateID,
data: make(map[string]value_t),
}
}
func (item *ReservedItem) Get(key string, def_value value_t) value_t {
item.RLock()
defer item.RUnlock()
value, ok := item.data[key]
if ok {
return value
}
return def_value
}
func (item *ReservedItem) Set(key string, value value_t) {
item.set(key, value)
}
func (item *ReservedItem) set(key string, value value_t) {
item.Lock()
item.Unlock()
item.data[key] = value
}
func (item *ReservedItem) SetAll(values map[string]interface{}) {
for key, value := range values {
if value != nil {
item.set(key, value)
}
}
}
func (item *ReservedItem) GetAll() (values map[string]interface{}) {
item.RLock()
defer item.RUnlock()
values = make(map[string]interface{})
for key, value := range item.data {
values[key] = value
}
return values
}
func (item *ReservedItem) ToDict() map[string]interface{} {
item.RLock()
defer item.RUnlock()
dict := make(map[string]interface{})
for id, value := range item.data {
dict[id] = value
}
return dict
}
type SessionItem struct {
Time time.Time
data map[string]*ReservedItem // candidateID -> ReservedItem
sync.RWMutex
}
func (si *SessionItem) get(candidateID string) *ReservedItem {
si.RLock()
defer si.RUnlock()
reservedItem := si.data[candidateID] // *ReservedItem
return reservedItem
}
func (si *SessionItem) set(candidateID string, reservedItem *ReservedItem) {
si.Lock()
defer si.Unlock()
si.data[candidateID] = reservedItem
}
func (si *SessionItem) AllCandidateIDs() []string {
si.RLock()
defer si.RUnlock()
candidateIds := []string{}
for candidateID := range si.data {
candidateIds = append(candidateIds, candidateID)
}
return candidateIds
}
func NewSessionItem() *SessionItem {
return &SessionItem{
Time: time.Now(),
data: make(map[string]*ReservedItem),
}
}
func (si *SessionItem) ToDict() map[string]interface{} {
si.RLock()
defer si.RUnlock()
dict := make(map[string]interface{})
dict["time"] = si.Time.Local().Format("2006-01-02 15:04:05")
for key, rItem := range si.data {
dict[key] = rItem.ToDict()
}
return dict
}
type CandidateItem struct {
candidateID string
data map[string]*ReservedItem // sessionID -> ReservedItem
result *ReservedItem
dirty bool
sync.RWMutex
}
func (ci *CandidateItem) get(sessionID string) *ReservedItem {
ci.RLock()
defer ci.RUnlock()
reservedItem := ci.data[sessionID]
return reservedItem
}
func (ci *CandidateItem) set(sessionID string, reservedItem *ReservedItem) {
ci.Lock()
defer ci.Unlock()
ci.data[sessionID] = reservedItem
ci.dirty = true
}
func NewCandidateItem(candidateID string) *CandidateItem {
return &CandidateItem{
candidateID: candidateID,
data: make(map[string]*ReservedItem),
dirty: true,
}
}
func (ci *CandidateItem) caculate() *ReservedItem {
ci.RLock()
defer ci.RUnlock()
data := make(map[string][]value_t)
for _, reservedItem := range ci.data {
for key, value := range reservedItem.data {
values, _ := data[key]
data[key] = append(values, value)
}
}
reservedItem := NewReservedItem(ci.candidateID)
for key, values := range data {
reservedItem.Set(key, ReservedSum(key, values))
}
return reservedItem
}
type ReservedPool struct {
Name string
sessionDict map[string]*SessionItem
candidateDict map[string]*CandidateItem
data map[string]value_t
stopCh <-chan struct{}
sync.RWMutex
}
func NewReservedPool(name string, stopCh <-chan struct{}) *ReservedPool {
return &ReservedPool{
Name: name,
sessionDict: make(map[string]*SessionItem),
candidateDict: make(map[string]*CandidateItem),
data: make(map[string]value_t),
stopCh: stopCh,
}
}
func (pool *ReservedPool) Start() {
go wait.Until(pool.checkSessionExpires, time.Duration(10)*time.Second, pool.stopCh)
}
func (pool *ReservedPool) checkSessionExpires() {
pool.Lock()
defer pool.Unlock()
now := time.Now()
for sessionID, sessionItem := range pool.sessionDict {
if now.Sub(sessionItem.Time).Seconds() > SessionExpiredTime {
pool.removeSession(sessionID)
}
}
}
func (pool *ReservedPool) Add(sessionID string, candidateID string,
reservedItem *ReservedItem) {
pool.Lock()
defer pool.Unlock()
session_item, ok := pool.sessionDict[sessionID]
if !ok {
session_item = NewSessionItem()
pool.sessionDict[sessionID] = session_item
}
session_item.set(candidateID, reservedItem)
candidateItem, ok := pool.candidateDict[candidateID]
if !ok {
candidateItem = NewCandidateItem(candidateID)
pool.candidateDict[candidateID] = candidateItem
}
candidateItem.set(sessionID, reservedItem)
}
func (pool *ReservedPool) GetReservedItem(candidateID string) *ReservedItem {
pool.RLock()
defer pool.RUnlock()
candidateItem, ok := pool.candidateDict[candidateID]
if !ok {
return nil
}
if candidateItem.dirty {
candidateItem.result = candidateItem.caculate()
candidateItem.dirty = false
}
return candidateItem.result
}
func (pool *ReservedPool) GetSessionItem(sessionID string) *SessionItem {
pool.RLock()
defer pool.RUnlock()
if sessionItem, ok := pool.sessionDict[sessionID]; ok {
return sessionItem
}
return nil
}
func (pool *ReservedPool) RemoveSession(sessionID string) bool {
pool.Lock()
defer pool.Unlock()
return pool.removeSession(sessionID)
}
func (pool *ReservedPool) removeSession(sessionID string) bool {
if sessionItem, ok := pool.sessionDict[sessionID]; ok {
delete(pool.sessionDict, sessionID)
if len(pool.sessionDict) == 0 {
pool.candidateDict = make(map[string]*CandidateItem)
} else {
for _, candidateId := range sessionItem.AllCandidateIDs() {
if candidateItem, ok := pool.candidateDict[candidateId]; ok {
if _, ok := candidateItem.data[sessionID]; ok {
delete(candidateItem.data, sessionID)
}
}
}
}
return true
}
return false
}
func (pool *ReservedPool) InSession(candidateId string) bool {
pool.RLock()
defer pool.RUnlock()
if candidateItem, ok := pool.candidateDict[candidateId]; ok {
return len(candidateItem.data) > 0
}
return false
}
func (pool *ReservedPool) ToDict() interface{} {
pool.RLock()
defer pool.RUnlock()
data := make(map[string]interface{})
for sessionId, sessionItem := range pool.sessionDict {
data[sessionId] = sessionItem.ToDict()
}
return data
}
-37
View File
@@ -1,37 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package db
import (
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
"yunion.io/x/log"
)
var DB *gorm.DB
func Init(dialect string, args ...interface{}) error {
if DB == nil {
db, err := gorm.Open(dialect, args...)
if err != nil {
return err
}
DB = db
return nil
}
log.Warningf("DB: %s , Conn: %v already connected...", dialect, args)
return nil
}
-15
View File
@@ -1,15 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package db // import "yunion.io/x/onecloud/pkg/scheduler/db"
-47
View File
@@ -1,47 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package models
import (
"encoding/json"
"github.com/jinzhu/gorm"
)
type Aggregate struct {
StandaloneModel
DefaultStrategy string `json:"default_strategy" gorm:"not null"`
}
func (c Aggregate) TableName() string {
return aggregatesTable
}
func (c Aggregate) String() string {
s, _ := json.Marshal(c)
return string(s)
}
func NewAggregateResource(db *gorm.DB) (Resourcer, error) {
model := func() interface{} {
return &Aggregate{}
}
models := func() interface{} {
aggregates := []Aggregate{}
return &aggregates
}
return newResource(db, aggregatesTable, model, models)
}
-56
View File
@@ -1,56 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package models
import (
"encoding/json"
"github.com/jinzhu/gorm"
)
type AggregateHost struct {
JointBaseModel
HostID string `json:"host_id" gorm:"column:host_id;not null"`
AggregateID string `json:"schedtag_id" gorm:"column:schedtag_id;not null"`
}
func (c AggregateHost) TableName() string {
return aggregateHostsTable
}
func (c AggregateHost) String() string {
s, _ := json.Marshal(c)
return string(s)
}
func (c AggregateHost) Aggregate() (*Aggregate, error) {
a, err := FetchByID(Aggregates, c.AggregateID)
if err != nil {
return nil, err
}
return a.(*Aggregate), nil
}
func NewAggregateHostResource(db *gorm.DB) (Resourcer, error) {
model := func() interface{} {
return &AggregateHost{}
}
models := func() interface{} {
aggregate_hosts := []AggregateHost{}
return &aggregate_hosts
}
return newResource(db, aggregateHostsTable, model, models)
}
-68
View File
@@ -1,68 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package models
import (
"github.com/jinzhu/gorm"
)
type Baremetal struct {
StandaloneModel
Status string `json:"status" gorm:"not null"`
Enabled bool `json:"enabled" gorm:"not null"`
CliGUID string `json:"cli_guid,omitempty"`
AgentID string `json:"agent_id,omitempty"`
CPUCount int `json:"cpu_count,omitempty"`
NodeCount int `json:"node_count,omitempty"`
CPUDesc string `json:"cpu_desc,omitempty"`
CPUMHZ int `json:"cpu_mhz,omitempty"`
MemSize int `json:"mem_size,omitempty"`
StorageSize int `json:"storage_size,omitempty"`
StorageType string `json:"storage_type,omitempty"`
StorageDriver string `json:"storage_driver,omitempty"`
StorageInfo string `json:"storage_info,omitempty"`
IpmiInfo string `json:"ipmi_info,omitempty" gorm:"type:text"`
Rack string `json:"rack,omitempty"`
Slots string `json:"slots,omitempty"`
ServerID string `json:"server_id,omitempty"`
UseCount int `json:"use_count,omitempty"`
PoolID string `json:"pool_id,omitempty"`
}
func (b Baremetal) TableName() string {
return baremetalsTable
}
func (b Baremetal) String() string {
str, _ := JsonString(b)
return str
}
func NewBaremetalResource(db *gorm.DB) (Resourcer, error) {
model := func() interface{} {
return &Baremetal{}
}
models := func() interface{} {
baremetals := []Baremetal{}
return &baremetals
}
return newResource(db, baremetalsTable, model, models)
}
@@ -1,99 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package models
import (
"fmt"
"github.com/jinzhu/gorm"
)
const (
BaremetalNetworksResourceName = "baremetalnetworks"
)
type BaremetalNetwork struct {
StandaloneModel
BaremetalID string `json:"baremetal_id,omitempty" gorm:"column:Baremetal_id;not null"`
NetworkID string `json:"network_id,omitempty" gorm:"column:network_id;not null"`
MacAddr string `json:"mac_addr" gorm:"column:mac_addr;not null"`
IpAddr string `json:"ip_addr,omitempty" gorm:"column:ip_addr"`
Ip6Addr string `json:"ip6_addr" gorm:"column:ip6_addr"`
Driver string `json:"driver" gorm:"column:driver"`
BwLimit int64 `json:"bw_limit" gorm:"column:bw_limit;not null"`
Index int `json:"index" gorm:"column:index;not null"`
Virtual int `json:"virtual" gorm:"column:virtual"`
IfName string `json:"if_name,omitempty" gorm:"column:if_name"`
MappingIpAddr string `json:"mapping_ip_addr" gorm:"column:mapping_ip_addr"`
}
func (n BaremetalNetwork) TableName() string {
return baremetalNetworksTable
}
func (n BaremetalNetwork) String() string {
s, _ := JsonString(n)
return string(s)
}
func NewBaremetalNetworksResource(db *gorm.DB) (Resourcer, error) {
model := func() interface{} {
return &BaremetalNetwork{}
}
models := func() interface{} {
baremetalNetworks := []BaremetalNetwork{}
return &baremetalNetworks
}
return newResource(db, baremetalNetworksTable, model, models)
}
type BaremetalNicCount struct {
NetworkID string `json:"network_id,omitempty" gorm:"column:network_id;not null"`
Count int `json:"count" gorm:"column:count;not null"`
}
func (c BaremetalNicCount) First() string {
return c.NetworkID
}
func (c BaremetalNicCount) Second() int {
return c.Count
}
func BaremetalNicCounts() ([]BaremetalNicCount, error) {
counts := []BaremetalNicCount{}
err := BaremetalNetworks.DB().Table(baremetalNetworksTable).
Select("network_id,count(*) as count").
Where("deleted=0").
Group("network_id").
Scan(&counts).Error
return counts, err
}
type BaremetalNicCounti struct {
Count int `json:"count" gorm:"column:count;not null"`
}
func (c BaremetalNicCounti) First() int {
return c.Count
}
func BaremetalNicCountsWithNetworkID(networkID string) (BaremetalNicCounti, error) {
counts := BaremetalNicCounti{0}
err := BaremetalNetworks.DB().Table(baremetalNetworksTable).
Select("count(*) as count").
Where(fmt.Sprintf("network_id = '%s' and deleted=0", networkID)).
Scan(&counts).Error
return counts, err
}
-49
View File
@@ -1,49 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package models
import (
"github.com/jinzhu/gorm"
)
type BaremetalAgent struct {
StandaloneModel
AccessIP string `json:"access_ip" gorm:"not null"`
ManagerURI string `json:"manager_uri,omitempty"`
Status string `json:"status" gorm:"not null"`
ZoneID string `json:"zone_id,omitempty"`
Version string `json:"version,omitempty"`
}
func (b BaremetalAgent) TableName() string {
return baremetalAgentsTable
}
func (b BaremetalAgent) String() string {
s, _ := JsonString(b)
return s
}
func NewBaremetalAgentResource(db *gorm.DB) (Resourcer, error) {
model := func() interface{} {
return &BaremetalAgent{}
}
models := func() interface{} {
agents := []BaremetalAgent{}
return &agents
}
return newResource(db, baremetalAgentsTable, model, models)
}
-323
View File
@@ -1,323 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package models
import (
"database/sql"
"encoding/json"
"fmt"
"strings"
"time"
"github.com/jinzhu/gorm"
)
type objectFunc func() interface{}
type BaseModel struct {
CreatedAt time.Time `json:"created_at" gorm:"column:created_at;type:datetime" sql:"DEFAULT:NULL"`
UpdatedAt time.Time `json:"updated_at" gorm:"column:updated_at;type:datetime" sql:"DEFAULT:NULL"`
DeletedAt time.Time `json:"deleted_at" gorm:"column:deleted_at;type:datetime" sql:"DEFAULT:NULL"`
Deleted bool `json:"deleted" gorm:"column:deleted;not null;index" sql:"DEFAULT:false"`
}
type StandaloneModel struct {
BaseModel
ID string `json:"id" gorm:"primary_key;column:id;type:varchar(36) CHARACTER SET ascii"`
Name string `json:"name" gorm:"column:name;type:varchar(128) CHARACTER SET utf8"`
Description string `json:"description,omitempty" gorm:"column:description"`
}
func (m *StandaloneModel) UUID() string {
return m.ID
}
type JointBaseModel struct {
BaseModel
RowID string `json:"row_id" gorm:"primary_key;column:row_id"`
}
func (m *JointBaseModel) UUID() string {
return m.RowID
}
type VirtualResourceModel struct {
StandaloneModel
Status string `json:"status" gorm:"column:status;not null"`
TenantID string `json:"tenant_id" gorm:"column:tenant_id;not null"`
UserID string `json:"user_id" gorm:"column:user_id;not null"`
BillingType string `json:"billing_type" gorm:"column:billing_type"`
IsSystem bool `json:"is_system" gorm:"column:is_system"`
PendingDeletedAt time.Time `json:"pending_deleted_at" gorm:"column:pending_deleted_at;type:datetime" sql:"DEFAULT:NULL"`
PendingDeleted bool `json:"pending_deleted" gorm:"column:pending_deleted;not null;index" sql:"DEFAULT:false"`
}
type SharableVirtualResourceModel struct {
VirtualResourceModel
IsPublic bool `json:"is_public" gorm:"column:is_public;not null"`
}
type resource struct {
db *gorm.DB
tableName string
getModel objectFunc
getModels objectFunc
}
func newResource(db *gorm.DB, tbl string, model, models objectFunc) (Resourcer, error) {
if db == nil {
return nil, fmt.Errorf("gorm db is nil")
}
r := new(resource)
r.db = db
r.tableName = tbl
r.getModel = model
r.getModels = models
return r, nil
}
func (r *resource) DB() *gorm.DB {
return r.db
}
func (r *resource) TableName() string {
return r.tableName
}
func (r *resource) Model() interface{} {
return r.getModel()
}
func (r *resource) Models() interface{} {
return r.getModels()
}
func JsonString(obj interface{}) (string, error) {
bytes, err := json.Marshal(obj)
if err != nil {
return "", err
}
return string(bytes), nil
}
func FetchByID(r Resourcer, id string) (interface{}, error) {
cond := map[string]interface{}{
"deleted": 0,
"id": id,
}
obj := r.Model()
if err := r.DB().Where(condition2String(cond)).First(obj).Error; err != nil {
return nil, err
}
return obj, nil
}
func rowsWithCondIn(r Resourcer, key string, set []string, cond map[string]interface{}) (*sql.Rows, error) {
return r.DB().Table(r.TableName()).Where(condition2String(cond)).
Where(fmt.Sprintf("%s in ('%s')", key, strings.Join(set, "','"))).Rows()
}
func rowsNotDeletedIn(r Resourcer, key string, set []string) (*sql.Rows, error) {
cond := map[string]interface{}{
"deleted": false,
}
return rowsWithCondIn(r, key, set, cond)
}
func rowsNotDeletedInWithCond(r Resourcer, key string, set []string, cond map[string]interface{}) (*sql.Rows, error) {
cond["deleted"] = false
return rowsWithCondIn(r, key, set, cond)
}
func virtualResourceRowsNotDeletedIn(r Resourcer, key string, set []string) (*sql.Rows, error) {
cond := map[string]interface{}{
"deleted": false,
"pending_deleted": false,
}
return rowsWithCondIn(r, key, set, cond)
}
func rowsToArray(r Resourcer, rows *sql.Rows) ([]interface{}, error) {
defer rows.Close()
columns, _ := rows.Columns()
objs := make([]interface{}, 0, len(columns))
for rows.Next() {
obj := r.Model()
err := r.DB().ScanRows(rows, obj)
if err != nil {
return nil, err
}
objs = append(objs, obj)
}
return objs, nil
}
func FetchByIDsWithKey(r Resourcer, key string, ids []string) ([]interface{}, error) {
rows, err := rowsNotDeletedIn(r, key, ids)
if err != nil {
return nil, err
}
return rowsToArray(r, rows)
}
func FetchByIDsWithKeyAndCond(r Resourcer, key string, ids []string, cond map[string]interface{}) ([]interface{}, error) {
rows, err := rowsNotDeletedInWithCond(r, key, ids, cond)
if err != nil {
return nil, err
}
return rowsToArray(r, rows)
}
func FetchByIDs(r Resourcer, ids []string) ([]interface{}, error) {
return FetchByIDsWithKey(r, "id", ids)
}
func FetchByHostIDs(r Resourcer, ids []string) ([]interface{}, error) {
return FetchByIDsWithKey(r, "host_id", ids)
}
func FetchGuestByIDs(ids []string) ([]interface{}, error) {
return FetchByIDs(Guests, ids)
}
func FetchGuestByHostIDs(ids []string) ([]interface{}, error) {
return FetchByIDsWithKey(Guests, "host_id", ids)
}
func FetchGuestByHostIDsWithCond(ids []string, cond map[string]interface{}) ([]interface{}, error) {
return FetchByIDsWithKeyAndCond(Guests, "host_id", ids, cond)
}
func FetchHostByIDs(ids []string) ([]interface{}, error) {
return FetchByIDs(Hosts, ids)
}
func FetchDiskByIDs(ids []string) ([]interface{}, error) {
return FetchByIDs(Disks, ids)
}
func FetchGroupByIDs(ids []string) ([]interface{}, error) {
return FetchByIDs(Groups, ids)
}
func FetchByWireIDs(r Resourcer, ids []string) ([]interface{}, error) {
return FetchByIDsWithKey(r, "wire_id", ids)
}
func FetchByBaremetalIDs(r Resourcer, ids []string) ([]interface{}, error) {
return FetchByIDsWithKey(r, "baremetal_id", ids)
}
func FetchByGuestIDs(r Resourcer, ids []string) ([]interface{}, error) {
return FetchByIDsWithKey(r, "guest_id", ids)
}
func AllWithDeleted(r Resourcer) ([]interface{}, error) {
return AllWithCond(r, map[string]interface{}{})
}
func All(r Resourcer) ([]interface{}, error) {
cond := map[string]interface{}{
"deleted": 0,
}
return AllWithCond(r, cond)
}
func AllWithCond(r Resourcer, cond map[string]interface{}) ([]interface{}, error) {
rows, err := r.DB().Model(r.Model()).Where(condition2String(cond)).Rows()
if err != nil {
return nil, err
}
return rowsToArray(r, rows)
}
func AllIDs(r Resourcer) ([]string, error) {
cond := map[string]interface{}{
"deleted": 0,
}
return AllIDsWithCond(r, cond)
}
func AllHostIDs() ([]string, error) {
cond := map[string]interface{}{
"deleted": 0,
"host_type!": "baremetal",
}
return AllIDsWithCond(Hosts, cond)
}
func AllBaremetalIDs() ([]string, error) {
cond := map[string]interface{}{
"deleted": 0,
"host_type": "baremetal",
}
return AllIDsWithCond(Hosts, cond)
}
func condition2String(cond map[string]interface{}) string {
result := make([]string, 0)
for key, value := range cond {
if _, ok := value.(string); ok {
result = append(result, fmt.Sprintf("%s='%s'", key, value.(string)))
} else if _, ok := value.(int); ok {
result = append(result, fmt.Sprintf("%s=%d", key, value.(int)))
} else if _, ok := value.(int64); ok {
result = append(result, fmt.Sprintf("%s=%d", key, value.(int64)))
} else if _, ok := value.(bool); ok {
result = append(result, fmt.Sprintf("%s=%v", key, value.(bool)))
}
}
return strings.Join(result, " and ")
}
func AllIDsWithCond(r Resourcer, cond map[string]interface{}) ([]string, error) {
rows, err := r.DB().Table(r.TableName()).Where(condition2String(cond)).Select("id").Rows()
if err != nil {
return nil, err
}
defer rows.Close()
ids := []string{}
for rows.Next() {
var id string
rows.Scan(&id)
ids = append(ids, id)
}
return ids, nil
}
type StatusOfHost struct {
ID string `json:"id" gorm:"primary_key;column:id;type:varchar(36) CHARACTER SET ascii"`
UpdatedAt time.Time `json:"updated_at" gorm:"column:updated_at;type:datetime" sql:"DEFAULT:NULL"`
}
func AllHostStatus(isBaremetal bool) ([]StatusOfHost, error) {
whereState := "deleted=0 and host_type%s"
if isBaremetal {
whereState = fmt.Sprintf(whereState, "='baremetal'")
} else {
whereState = fmt.Sprintf(whereState, "!='baremtal'")
}
status := make([]StatusOfHost, 0)
err := Hosts.DB().Table(Hosts.TableName()).
Select("id, updated_at").
Where(whereState).
Scan(&status).Error
return status, err
}
-25
View File
@@ -1,25 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package models
import (
"time"
)
type BillingResourceBase struct {
BillingType string `json:"billing_type" gorm:"column:billing_type"`
ExpiredAt time.Time `json:"expired_at" gorm:"column:expired_at;type:datetime"`
BillingCycle string `json:"billing_cycle" gorm:"column:billing_cycle"`
}
-57
View File
@@ -1,57 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package models
import (
"github.com/jinzhu/gorm"
)
type Cloudprovider struct {
StandaloneModel
Status string `json:"status" gorm:"column:status;not null"`
Enabled bool `json:"enabled" gorm:"column:enabled;not null"`
AccessUrl string `json:"access_url" gorm:"column:access_url"`
Provider string `json:"provider" gorm:"column:provider"`
CloudaccountId string `json:"cloudaccount_id" gorm:"column:cloudaccount_id"`
ProjectId string `json:"tenant_id" gorm:"column:tenant_id"`
}
func (c Cloudprovider) TableName() string {
return cloudproviderTable
}
func (c Cloudprovider) String() string {
s, _ := JsonString(c)
return s
}
func NewCloudproviderResource(db *gorm.DB) (Resourcer, error) {
return newResource(db, cloudproviderTable,
func() interface{} {
return &Cloudprovider{}
},
func() interface{} {
cs := []Cloudprovider{}
return &cs
})
}
func FetchCloudproviderById(id string) (*Cloudprovider, error) {
obj, err := FetchByID(CloudProviders, id)
if err != nil {
return nil, err
}
return obj.(*Cloudprovider), nil
}
-53
View File
@@ -1,53 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package models
import (
"encoding/json"
"github.com/jinzhu/gorm"
)
type Cluster struct {
StandaloneModel
HostIPStart string `json:"host_ip_start" gorm:"not null"`
HostIPEnd string `json:"host_ip_end" gorm:"not null"`
HostNetmask int `json:"host_netmask,omitempty"`
HostGateway string `json:"host_gateway,omitempty"`
HostDNS string `json:"host_dns,omitempty"`
ScheduleRank int `json:"schedule_rank,omitempty"`
ZoneID string `json:"zone_id" gorm:"not null"`
}
func (c Cluster) TableName() string {
return clustersTable
}
func (c Cluster) String() string {
s, _ := json.Marshal(c)
return string(s)
}
func NewClusterResource(db *gorm.DB) (Resourcer, error) {
model := func() interface{} {
return &Cluster{}
}
models := func() interface{} {
clusters := []Cluster{}
return &clusters
}
return newResource(db, clustersTable, model, models)
}
-148
View File
@@ -1,148 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package models
import (
"yunion.io/x/onecloud/pkg/scheduler/db"
)
const (
hostsTable = "hosts_tbl"
clustersTable = "clusters_tbl"
zonesTable = "zones_tbl"
guestsTable = "guests_tbl"
baremetalsTable = "baremetals_tbl"
baremetalAgentsTable = "baremetalagents_tbl"
baremetalNetworksTable = "baremetalnetworks_tbl"
storageTable = "storages_tbl"
hostStorageTable = "hoststorages_tbl"
groupGuestTable = "guestgroups_tbl"
groupTable = "groups_tbl"
groupNetworksTable = "groupnetworks_tbl"
metadataTable = "metadata_tbl"
isolatedDeviceTable = "isolated_devices_tbl"
disksTable = "disks_tbl"
guestDiskTable = "guestdisks_tbl"
guestNetworksTable = "guestnetworks_tbl"
aggregatesTable = "aggregates_tbl"
aggregateHostsTable = "aggregate_hosts_tbl"
networksTable = "networks_tbl"
netinterfacesTable = "netinterfaces_tbl"
wiresTable = "wires_tbl"
hostWiresTable = "hostwires_tbl"
reserveDipsTable = "reservedips_tbl"
dynamicschedtagTable = "dynamicschedtags_tbl"
cloudproviderTable = "cloudproviders_tbl"
)
var (
Hosts Resourcer
HostWires Resourcer
Clusters Resourcer
Zones Resourcer
Guests Resourcer
CloudProviders Resourcer
Baremetals Resourcer
BaremetalAgents Resourcer
BaremetalNetworks Resourcer
Storages Resourcer
HostStorages Resourcer
GroupGuests Resourcer
Groups Resourcer
GroupNetworks Resourcer
Metadatas Resourcer
IsolatedDevices Resourcer
Disks Resourcer
GuestDisks Resourcer
GuestNetworks Resourcer
Aggregates Resourcer
AggregateHosts Resourcer
Dynamicschedtags Resourcer
Networks Resourcer
NetInterfaces Resourcer
Wires Resourcer
ReserveDipsNerworks Resourcer
)
func Init(dialect string, args ...interface{}) error {
err := db.Init(dialect, args...)
if err != nil {
return err
}
Hosts, _ = NewHostResource(db.DB)
Clusters, _ = NewClusterResource(db.DB)
Zones, _ = NewZoneResource(db.DB)
Guests, _ = NewGuestResource(db.DB)
CloudProviders, _ = NewCloudproviderResource(db.DB)
Baremetals, _ = NewBaremetalResource(db.DB)
BaremetalAgents, _ = NewBaremetalAgentResource(db.DB)
Storages, _ = NewStorageResource(db.DB)
HostStorages, _ = NewHostStorageResource(db.DB)
Groups, _ = NewGroupResource(db.DB)
GroupGuests, _ = NewGroupGuestResource(db.DB)
Metadatas, _ = NewMetadataResource(db.DB)
IsolatedDevices, _ = NewIsolatedDeviceResource(db.DB)
Disks, _ = NewDiskResource(db.DB)
GuestDisks, _ = NewGuestDiskResource(db.DB)
Aggregates, _ = NewAggregateResource(db.DB)
AggregateHosts, _ = NewAggregateHostResource(db.DB)
Dynamicschedtags, _ = NewDynaimcschedtagResource(db.DB)
Networks, _ = NewNetworksResource(db.DB)
NetInterfaces, _ = NewNetInterfacesResource(db.DB)
Wires, _ = NewWiresResource(db.DB)
HostWires, _ = NewHostWiresResource(db.DB)
GuestNetworks, _ = NewGuestNetworksResource(db.DB)
GroupNetworks, _ = NewGroupNetworksResource(db.DB)
BaremetalNetworks, _ = NewBaremetalNetworksResource(db.DB)
ReserveDipsNerworks, _ = NewReserveDipsNetworksResource(db.DB)
return nil
}
func DBValid() bool {
if db.DB == nil {
return false
}
return true
}
-107
View File
@@ -1,107 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package models
import (
"github.com/jinzhu/gorm"
"yunion.io/x/pkg/utils"
)
const (
DiskResourceName = "disk"
DiskInit = "init"
DiskRebuild = "rebuild"
DiskAllocFailed = "alloc_failed"
DiskStartAlloc = "start_alloc"
DiskAllocating = "allocating"
DiskReady = "ready"
DiskFrozen = "frozen"
DiskDealloc = "deallocating"
DiskDeallocFailed = "dealloc_failed"
DiskStartSave = "start_save"
DiskSaving = "saving"
DiskStartResize = "start_resize"
DiskResizing = "resizing"
DiskStartMigrate = "start_migrate"
DiskPostMigrate = "post_migrate"
DiskMigrating = "migrating"
TakeMebsSnapshot = "take_mebs_snapshot"
TakeMebsSnapshotFailed = "take_mebs_snapshot_failed"
ApplyMebsSnapshot = "apply_mebs_snapshot"
ApplyMebsSnapshotFailed = "apply_mebs_snapshot_failed"
CloneMebsSnapshot = "clone_mebs_snapshot"
CloneMebsSnapshotFailed = "clone_mebs_snapshot_failed"
PerformMebsBackup = "perform_mebs_backup"
PerformMebsBackupFailed = "perform_mebs_backup_failed"
RestoreMebsBackup = "restore_mebs_backup"
RestoreMebsBackupFailed = "restore_mebs_backup_failed"
SaveMebsTemplate = "save_mebs_template"
SaveMebsTemplateFailed = "save_mebs_template_failed"
ActionThrottle = "throttle"
ActionFreeze = "freeze"
ActionUnfreeze = "unfreeze"
)
var (
IOThrottleActions = []string{ActionThrottle, ActionFreeze, ActionUnfreeze}
)
type Disk struct {
SharableVirtualResourceModel
DiskFormat string `json:"disk_format" gorm:"column:disk_format;not null"`
DiskSize int64 `json:"disk_size" gorm:"column:disk_size;not null"`
AccessPath string `json:"access_path" gorm:"column:access_path;not null"`
AutoDelete bool `json:"auto_delete" gorm:"column:auto_delete;not null"`
StorageID string `json:"storage_id" gorm:"column:storage_id;not null"`
MebsInfo string `json:"mebs_info" gorm:"column:mebs_info;type:text"`
}
func (d Disk) TableName() string {
return disksTable
}
func (d Disk) String() string {
str, _ := JsonString(d)
return str
}
func NewDiskResource(db *gorm.DB) (Resourcer, error) {
return newResource(db, disksTable,
func() interface{} { return &Disk{} },
func() interface{} { return &([]Disk{}) })
}
func (d Disk) Storage() (*Storage, error) {
s, err := FetchByID(Storages, d.StorageID)
if err != nil {
return nil, err
}
return s.(*Storage), nil
}
func (d Disk) IsLocal() (bool, error) {
s, err := d.Storage()
if err != nil {
return false, err
}
return utils.IsLocalStorage(s.StorageType), nil
}
-15
View File
@@ -1,15 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package models // import "yunion.io/x/onecloud/pkg/scheduler/db/models"
@@ -1,72 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package models
import (
"github.com/jinzhu/gorm"
)
type Dynamicschedtag struct {
StandaloneModel
Condition string `gorm:"column:condition;not null"`
SchedtagId string `gorm:"column:schedtag_id;not null"`
Enabled bool `gorm:"column:enabled"`
}
func (d Dynamicschedtag) TableName() string {
return dynamicschedtagTable
}
func (d Dynamicschedtag) String() string {
s, _ := JsonString(d)
return s
}
func NewDynaimcschedtagResource(db *gorm.DB) (Resourcer, error) {
model := func() interface{} {
return &Dynamicschedtag{}
}
models := func() interface{} {
tags := []Dynamicschedtag{}
return &tags
}
return newResource(db, dynamicschedtagTable, model, models)
}
func FetchEnabledDynamicschedtags() ([]*Dynamicschedtag, error) {
cond := map[string]interface{}{
"deleted": false,
"enabled": true,
}
objs, err := AllWithCond(Dynamicschedtags, cond)
if err != nil {
return nil, err
}
tags := []*Dynamicschedtag{}
for _, obj := range objs {
tags = append(tags, obj.(*Dynamicschedtag))
}
return tags, nil
}
func (d Dynamicschedtag) FetchSchedTag() (*Aggregate, error) {
obj, err := FetchByID(Aggregates, d.SchedtagId)
if err != nil {
return nil, err
}
return obj.(*Aggregate), err
}
-47
View File
@@ -1,47 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package models
import (
"github.com/jinzhu/gorm"
)
type Group struct {
VirtualResourceModel
ServiceType string `json:"service_type" gorm:"column:service_type"`
ParentID string `json:"parent_id" gorm:"column:parent_id"`
ZoneID string `json:"zone_id" gorm:"column:zone_id"`
SchedStrategy string `json:"sched_strategy" gorm:"column:sched_strategy"`
}
func (g Group) TableName() string {
return groupTable
}
func (g Group) String() string {
str, _ := JsonString(g)
return str
}
func NewGroupResource(db *gorm.DB) (Resourcer, error) {
model := func() interface{} {
return &Group{}
}
models := func() interface{} {
groups := []Group{}
return &groups
}
return newResource(db, groupTable, model, models)
}
-46
View File
@@ -1,46 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package models
import (
"github.com/jinzhu/gorm"
)
type GroupGuest struct {
GroupJointModel
Tag *string `json:"tag" gorm:"column:tag"`
GuestID *string `json:"guest_id" gorm:"column:guest_id"`
}
func (g GroupGuest) TableName() string {
return groupGuestTable
}
func (g GroupGuest) String() string {
str, _ := JsonString(g)
return str
}
func NewGroupGuestResource(db *gorm.DB) (Resourcer, error) {
model := func() interface{} {
return &GroupGuest{}
}
models := func() interface{} {
groupGuests := []GroupGuest{}
return &groupGuests
}
return newResource(db, groupGuestTable, model, models)
}
-20
View File
@@ -1,20 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package models
type GroupJointModel struct {
JointBaseModel
GroupID string `json:"group_id" gorm:"not null"`
}
-96
View File
@@ -1,96 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package models
import (
"fmt"
"github.com/jinzhu/gorm"
)
const (
GroupNetworksResourceName = "groupnetworks"
)
type GroupNetwork struct {
StandaloneModel
GroupID string `json:"group_id,omitempty" gorm:"column:group_id;not null"`
NetworkID string `json:"network_id,omitempty" gorm:"column:network_id;not null"`
IpAddr string `json:"ip_addr,omitempty" gorm:"column:ip_addr"`
Index int `json:"index" gorm:"column:index;not null"`
EipID string `json:"eip_id,omitempty" gorm:"column:if_name"`
MappingIpAddr string `json:"mapping_ip_addr" gorm:"column:mapping_ip_addr"`
}
func (n GroupNetwork) TableName() string {
return groupNetworksTable
}
func (n GroupNetwork) String() string {
s, _ := JsonString(n)
return string(s)
}
func NewGroupNetworksResource(db *gorm.DB) (Resourcer, error) {
model := func() interface{} {
return &GroupNetwork{}
}
models := func() interface{} {
groupNetworks := []GroupNetwork{}
return &groupNetworks
}
return newResource(db, groupNetworksTable, model, models)
}
type GroupNicCount struct {
NetworkID string `json:"network_id,omitempty" gorm:"column:network_id;not null"`
Count int `json:"count" gorm:"column:count;not null"`
}
func (c GroupNicCount) First() string {
return c.NetworkID
}
func (c GroupNicCount) Second() int {
return c.Count
}
func GroupNicCounts() ([]GroupNicCount, error) {
counts := []GroupNicCount{}
err := Groups.DB().Table(groupNetworksTable).
Select("network_id,count(*) as count").
Where("deleted=0").
Group("network_id").
Scan(&counts).Error
return counts, err
}
type GroupNicCounti struct {
Count int `json:"count" gorm:"column:count;not null"`
}
func (c GroupNicCounti) First() int {
return c.Count
}
func GroupNicCountsWithNetworkID(networkID string) (GroupNicCounti, error) {
counts := GroupNicCounti{0}
err := Groups.DB().Table(groupNetworksTable).
Select("count(*) as count").
Where(fmt.Sprintf("network_id = '%s' and deleted=0", networkID)).
Scan(&counts).Error
return counts, err
}
-224
View File
@@ -1,224 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package models
import (
"fmt"
"strings"
"github.com/jinzhu/gorm"
"yunion.io/x/pkg/util/sets"
)
const (
GuestResourceName = "server"
VmInit = "init"
VmUnknown = "unknown"
VmSchedule = "schedule"
VmScheduleFailed = "schedule_fail"
VmCreateNetwork = "network"
VmNetworkFailed = "net_fail"
VmCreateDisk = "disk"
VmDiskFailed = "disk_fail"
VmStartDeploy = "start_deploy"
VmDeploying = "deploying"
VmDeployFailed = "deploy_fail"
VmReady = "ready"
VmStartStart = "start_start"
VmStarting = "starting"
VmStartFailed = "start_fail"
VmStartRestart = "start_restart"
VmRunning = "running"
VmStartStop = "start_stop"
VmStopping = "stopping"
VmStopFailed = "stop_fail"
VmStartSuspend = "start_suspend"
VmSuspending = "suspending"
VmSuspend = "suspend"
VmSuspendFailed = "suspend_failed"
VmReset = "reset"
VmStartDelete = "start_delete"
VmDeleteFail = "delete_fail"
VmDeleting = "deleting"
VmStartMigrate = "start_migrate"
VmMigrating = "migrating"
VmMigrateFailed = "migrate_failed"
VmDiskMigrating = "disk_migrating"
VmDiskMigrateFailed = "disk_migrate_fail"
VmChangeFlavor = "change_flavor"
VmChangeFlavorFail = "change_flavor_fail"
VmRebuildRoot = "rebuild_root"
VmRebuildRootFail = "rebuild_root_fail"
VmRebuildDisk = "rebuild_disk"
VmRebuildDiskFail = "rebuild_disk_fail"
VmBlockStream = "block_stream"
VmStartSnapshot = "snapshot_start"
VmSnapshot = "snapshot"
VmSnapshotSucc = "snapshot_succ"
VmSnapshotFailed = "snapshot_failed"
VmSyncConfig = "sync_config"
VmSyncConfigFail = "sync_config_failed"
VmResizeDisk = "resize_disk"
VmStartSaveDisk = "start_save_disk"
VmSaveDisk = "save_disk"
VmSaveDiskFailed = "save_disk_failed"
VmRestoringSnapshot = "restoring_snapshot"
VmRestoreDisk = "restore_disk"
VmRestoreState = "restore_state"
VmRestoreFailed = "restore_failed"
VmRemoveStatefile = "remove_state"
VmHotplugCPUMEM = "hotplug_cpu_mem"
VmAdmin = "admin"
ShutdownStop = "stop"
ShutdownTerminate = "terminate"
HostTypeHost = "host"
HostTypeBaremetal = "baremetal"
GuestTypeVm = "vm"
GuestTypeContainer = "container"
QGAStatusUnknown = "unknown"
QGAStatusStop = "stop"
QGAStatusStarting = "starting"
QGAStatusStartFailed = "start_failed"
QGAStatusRunning = "running"
QGAStatusCrashed = "crashed"
)
var (
VmRunningStatus = sets.NewString(VmStartStart, VmStarting, VmRunning, VmStopFailed, VmBlockStream)
VmCreatingStatus = sets.NewString(VmCreateNetwork, VmCreateDisk, VmStartDeploy, VmDeploying)
GuestExtraFeature = sets.NewString("kvm", "storage_type")
)
type Guest struct {
VirtualResourceModel
VCPUCount int64 `json:"vcpu_count" gorm:"column:vcpu_count;type:tinyint64(4);not null"`
VMemSize int64 `json:"vmem_size" gorm:"column:vmem_size;type:int64(11);not null"`
DimmSlots string `json:"dimm_slots,omitempty" gorm:"column:dimm_slots;type:text"`
BootOrder string `json:"boot_order,omitempty" gorm:"column:boot_order"`
DisableDelete bool `json:"disable_delete" gorm:"column:disable_delete"`
ShutdownBehavior string `json:"shutdown_behavior,omitempty" gorm:"column:shutdown_behavior"`
KeypairID string `json:"keypair_id,omitempty" gorm:"column:keypair_id"`
HostID string `json:"host_id,omitempty" gorm:"column:host_id"`
BackupHostID string `json:"backup_host_id,omitempty" gorm:"column:backup_host_id"`
VNCPort int64 `json:"vnc_port,omitempty" gorm:"column:vnc_port"`
VGA string `json:"vga" gorm:"column:vga"`
FlavorID string `json:"flavor_id,omitempty" gorm:"column:flavor_id"`
SecgrpID string `json:"secgrp_id,omitempty" gorm:"column:secgrp_id"`
AdminSecgrpID string `json:"admin_secgrp_id,omitempty" gorm:"column:admin_secgrp_id"`
VrouterID string `json:"vrouter_id,omitempty" gorm:"column:vrouter_id"`
HostType string `json:"host_type,omitempty" gorm:"column:host_type"`
PreferZoneID string `json:"prefer_zone_id,omitempty" gorm:"column:prefer_zone_id"`
GuestType string `json:"guest_type,omitempty" gorm:"column:guest_type"`
QGAStatus string `json:"qga_status" gorm:"column:qga_status"`
}
func (g Guest) TableName() string {
return guestsTable
}
func (g Guest) String() string {
s, _ := JsonString(g)
return s
}
func (g Guest) DisksQuery(diskFormat ...string) *gorm.DB {
q := GuestDisks.DB().Table(guestDiskTable).
Where(map[string]interface{}{
"deleted": false,
"guest_id": g.ID,
})
if len(diskFormat) != 0 {
joinStr := fmt.Sprintf("JOIN %s on %s.id = %s.disk_id AND %s.disk_format = ?", disksTable, disksTable, guestDiskTable, disksTable)
q.Joins(joinStr, diskFormat[0])
}
return q
}
func (g Guest) Disks(diskFormat ...string) *gorm.DB {
q := g.DisksQuery(diskFormat...)
q.Order(fmt.Sprintf("%s.index", g.TableName()))
return q
}
func (g Guest) DiskSize(onlyLocal bool) (int64, error) {
var size int64
q := g.Disks()
disks := []GuestDisk{}
err := q.Scan(&disks).Error
if err != nil {
return 0, err
}
for _, gstDisk := range disks {
disk, err := gstDisk.Disk()
if err != nil {
return 0, err
}
isLocal, err := disk.IsLocal()
if err != nil {
return 0, err
}
if !onlyLocal || isLocal {
size += disk.DiskSize
}
}
return size, nil
}
func (g Guest) IsRunning() bool {
return VmRunningStatus.Has(g.Status)
}
func (g Guest) IsCreating() bool {
return VmCreatingStatus.Has(g.Status)
}
func (g Guest) IsGuestFakeDeleted() bool {
return strings.HasSuffix(g.Name, "_deleted")
}
func NewGuestResource(db *gorm.DB) (Resourcer, error) {
model := func() interface{} {
return &Guest{}
}
models := func() interface{} {
guests := []Guest{}
return &guests
}
return newResource(db, guestsTable, model, models)
}
-52
View File
@@ -1,52 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package models
import (
"github.com/jinzhu/gorm"
)
type GuestDisk struct {
GuestJointModel
DiskID string `json:"disk_id" gorm:"column:disk_id;not null;index"`
ImagePath string `json:"image_path" gorm:"column:image_path;not null"`
Driver string `json:"driver" gorm:"column:driver"`
CacheMode string `json:"cache_mode" gorm:"column:cache_mode"`
AioMode string `json:"aio_mode" gorm:"column:aio_mode"`
Index int `json:"index" gorm:"column:index;not null"`
}
func (d GuestDisk) TableName() string {
return guestDiskTable
}
func (d GuestDisk) String() string {
str, _ := JsonString(d)
return str
}
func (d GuestDisk) Disk() (*Disk, error) {
disk, err := FetchByID(Disks, d.DiskID)
if err != nil {
return nil, err
}
return disk.(*Disk), nil
}
func NewGuestDiskResource(db *gorm.DB) (Resourcer, error) {
return newResource(db, guestDiskTable,
func() interface{} { return &GuestDisk{} },
func() interface{} { return &([]GuestDisk{}) })
}
-20
View File
@@ -1,20 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package models
type GuestJointModel struct {
JointBaseModel
GuestID string `json:"guest_id" gorm:"column:guest_id;not null;index"`
}
-100
View File
@@ -1,100 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package models
import (
"fmt"
"github.com/jinzhu/gorm"
)
const (
GuestNetworksResourceName = "guestnetworks"
)
type GuestNetwork struct {
StandaloneModel
GuestID string `json:"guest_id,omitempty" gorm:"column:guest_id;not null"`
NetworkID string `json:"network_id,omitempty" gorm:"column:network_id;not null"`
MacAddr string `json:"mac_addr" gorm:"column:mac_addr;not null"`
IpAddr string `json:"ip_addr,omitempty" gorm:"column:ip_addr"`
Ip6Addr string `json:"ip6_addr" gorm:"column:ip6_addr"`
Driver string `json:"driver" gorm:"column:driver"`
BwLimit int64 `json:"bw_limit" gorm:"column:bw_limit;not null"`
Index int `json:"index" gorm:"column:index;not null"`
Virtual int `json:"virtual" gorm:"column:virtual"`
IfName string `json:"if_name,omitempty" gorm:"column:if_name"`
MappingIpAddr string `json:"mapping_ip_addr" gorm:"column:mapping_ip_addr"`
}
func (n GuestNetwork) TableName() string {
return guestNetworksTable
}
func (n GuestNetwork) String() string {
s, _ := JsonString(n)
return string(s)
}
func NewGuestNetworksResource(db *gorm.DB) (Resourcer, error) {
model := func() interface{} {
return &GuestNetwork{}
}
models := func() interface{} {
guestNetworks := []GuestNetwork{}
return &guestNetworks
}
return newResource(db, guestNetworksTable, model, models)
}
type GuestNicCount struct {
NetworkID string `json:"network_id,omitempty" gorm:"column:network_id;not null"`
Count int `json:"count" gorm:"column:count;not null"`
}
func (c GuestNicCount) First() string {
return c.NetworkID
}
func (c GuestNicCount) Second() int {
return c.Count
}
func GuestNicCounts() ([]GuestNicCount, error) {
counts := []GuestNicCount{}
err := GuestNetworks.DB().Table(guestNetworksTable).
Select("network_id,count(*) as count").
Where("deleted=0").
Group("network_id").
Scan(&counts).Error
return counts, err
}
type GuestNicCounti struct {
Count int `json:"count" gorm:"column:count;not null"`
}
func (c GuestNicCounti) First() int {
return c.Count
}
func GuestNicCountsWithNetworkID(networkID string) (GuestNicCounti, error) {
counts := GuestNicCounti{0}
err := GuestNetworks.DB().Table(guestNetworksTable).
Select("count(*) as count").
Where(fmt.Sprintf("network_id = '%s' and deleted=0", networkID)).
Scan(&counts).Error
return counts, err
}
-188
View File
@@ -1,188 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package models
import (
"fmt"
"strings"
"github.com/jinzhu/gorm"
"yunion.io/x/onecloud/pkg/scheduler/api"
o "yunion.io/x/onecloud/pkg/scheduler/options"
)
const (
HostResourceName = "host"
)
var (
HostExtraFeature = []string{"nest", "storage_type", "vip_reserved"}
)
type Host struct {
StandaloneModel
BillingResourceBase
Rack string `json:"rack,omitempty" gorm:"column:rack"`
Slots string `json:"slots,omitempty" gorm:"column:slots"`
AccessMAC string `json:"access_mac" gorm:"not null"`
AccessIP string `json:"access_ip" gorm:"column:access_ip"`
ManagerURI string `json:"manager_uri,omitempty" gorm:"column:manager_uri"`
SysInfo string `json:"sys_info,omitempty" gorm:"type:text"`
Sn string `json:"sn,omitempty" gorm:"column:sn"`
CPUCount int64 `json:"cpu_count" gorm:"column:cpu_count"`
NodeCount int64 `json:"node_count" gorm:"column:node_count"`
CPUDesc string `json:"cpu_desc" gorm:"column:cpu_desc"`
CPUMHZ int64 `json:"cpu_mhz" gorm:"column:cpu_mhz"`
CPUCache int64 `json:"cpu_cache" gorm:"column:cpu_cache"`
CPUReserved int64 `json:"cpu_reserved" gorm:"column:cpu_reserved"`
CPUCmtbound *float64 `json:"cpu_cmtbound" gorm:"column:cpu_cmtbound"`
MemSize int64 `json:"mem_size" gorm:"column:mem_size"`
MemReserved int64 `json:"mem_reserved" gorm:"column:mem_reserved"`
MemCmtbound *float64 `json:"mem_cmtbound" gorm:"column:mem_cmtbound"`
StorageSize int `json:"storage_size,omitempty" gorm:"column:storage_size"`
StorageType string `json:"storage_type,omitempty" gorm:"column:storage_type"`
StorageDriver string `json:"storage_driver,omitempty" gorm:"column:storage_driver"`
StorageInfo string `json:"storage_info,omitempty" gorm:"column:storage_info"`
IpmiInfo string `json:"ipmi_info,omitempty" gorm:"type:text"`
Status string `json:"status" gorm:"column:status;not null"`
HostStatus string `json:"host_status" gorm:"column:host_status;not null"`
Enabled bool `json:"enabled" gorm:"column:enabled;not null"`
ZoneID string `json:"zone_id" gorm:"column:zone_id;not null"`
HostType string `json:"host_type" gorm:"column:host_type"`
Version string `json:"version" gorm:"column:version"`
IsBaremetal bool `json:"is_baremetal" gorm:"column:is_baremetal"`
ManagerID *string `json:"manager_id" gorm:"column:manager_id"`
IsMaintenance bool `json:"is_maintenance" gorm:"column:is_maintenance"`
ResourceType string `json:"resource_type" gorm:"column:resource_type"`
RealExternalId string `json:"real_external_id" gorm:"column:real_external_id;type:varchar(256) CHARACTER SET utf8"`
// DECAPITATE
ClusterID string `json:"cluster_id" gorm:"column:cluster_id"`
PoolID string `json:"pool_id,omitempty" gorm:"column:pool_id"`
}
func (h Host) TableName() string {
return hostsTable
}
func (h Host) String() string {
s, _ := JsonString(h)
return string(s)
}
func (h Host) IsHypervisor() bool {
if h.HostType == api.HostTypeBaremetal {
return false
}
return true
}
func NewHostResource(db *gorm.DB) (Resourcer, error) {
model := func() interface{} {
return &Host{}
}
models := func() interface{} {
hosts := []Host{}
return &hosts
}
return newResource(db, hostsTable, model, models)
}
func (h Host) CPUOverCommitBound() float64 {
if h.CPUCmtbound != nil {
return *h.CPUCmtbound
}
return float64(o.GetOptions().DefaultCPUOvercommitBound)
}
func (h Host) MemOverCommitBound() float64 {
if h.MemCmtbound != nil {
return *h.MemCmtbound
}
return float64(o.GetOptions().DefaultMemoryOvercommitBound)
}
func HostAggregates(hostID string) ([]*Aggregate, error) {
hAggs, err := FetchByHostIDs(AggregateHosts, []string{hostID})
if err != nil {
return nil, err
}
aggs := make([]*Aggregate, 0)
for _, obj := range hAggs {
ha := obj.(*AggregateHost)
agg, err := ha.Aggregate()
if err != nil {
return nil, err
}
aggs = append(aggs, agg)
}
return aggs, nil
}
type ResidentTenant struct {
HostID string `json:"host_id" gorm:"column:host_id;not null"`
TenantID string `json:"tenant_id" gorm:"column:tenant_id;not null"`
TenantCount int64 `json:"tenant_count" gorm:"column:tenant_count"`
}
func (t ResidentTenant) First() string {
return t.HostID
}
func (t ResidentTenant) Second() string {
return t.TenantID
}
func (t ResidentTenant) Third() interface{} {
return t.TenantCount
}
func ResidentTenantsInHosts(hostIDs []string) ([]ResidentTenant, error) {
tenants := []ResidentTenant{}
err := Guests.DB().Table(guestsTable).
Select("host_id, tenant_id, count(tenant_id) as tenant_count").
Where(fmt.Sprintf("host_id in ('%s') and deleted=0", strings.Join(hostIDs, "','"))).
Group("tenant_id, host_id").Scan(&tenants).Error
return tenants, err
}
func FetchHypervisorHostByIDs(ids []string) ([]interface{}, error) {
rows, err := rowsNotDeletedInWithCond(Hosts, "id", ids,
map[string]interface{}{
"host_type!": "baremetal",
})
if err != nil {
return nil, err
}
return rowsToArray(Hosts, rows)
}
func FetchBaremetalHostByIDs(ids []string) ([]interface{}, error) {
rows, err := rowsNotDeletedInWithCond(Hosts, "id", ids,
map[string]interface{}{
"host_type": "baremetal",
})
if err != nil {
return nil, err
}
return rowsToArray(Hosts, rows)
}
-20
View File
@@ -1,20 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package models
type HostJointModel struct {
JointBaseModel
HostID string `json:"host_id" gorm:"not null"`
}
-46
View File
@@ -1,46 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package models
import (
"github.com/jinzhu/gorm"
)
type HostStorage struct {
HostJointModel
MountPoint string `json:"mount_point" gorm:"not null"`
StorageID string `json:"storage_id" gorm:"not null"`
}
func (s HostStorage) TableName() string {
return hostStorageTable
}
func (s HostStorage) String() string {
str, _ := JsonString(s)
return str
}
func NewHostStorageResource(db *gorm.DB) (Resourcer, error) {
model := func() interface{} {
return &HostStorage{}
}
models := func() interface{} {
storages := []HostStorage{}
return &storages
}
return newResource(db, hostStorageTable, model, models)
}
-82
View File
@@ -1,82 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package models
import (
"fmt"
"github.com/jinzhu/gorm"
)
const (
HostWireResourceName = "hostwires"
)
type HostWire struct {
StandaloneModel
Bridge string `json:"bridge,omitempty" gorm:"not null"`
Interface string `json:"interface,omitempty" gorm:"not null"`
HostID string `json:"host_id,omitempty" gorm:"not null"`
WireID string `json:"wire_id,omitempty" gorm:"not null"`
}
func (w HostWire) TableName() string {
return hostWiresTable
}
func (w HostWire) String() string {
str, _ := JsonString(w)
return str
}
func NewHostWiresResource(db *gorm.DB) (Resourcer, error) {
model := func() interface{} {
return &HostWire{}
}
models := func() interface{} {
hostWires := []HostWire{}
return &hostWires
}
return newResource(db, hostWiresTable, model, models)
}
type Host2Wire struct {
HostID string `json:"host_id" gorm:"column:host_id;not null"`
WireID string `json:"wire_id" gorm:"column:wire_id;not null"`
}
func (c Host2Wire) First() string {
return c.WireID
}
func SelectWiresWithHostID(hostID string) ([]Host2Wire, error) {
wires := []Host2Wire{}
err := HostWires.DB().Table(hostWiresTable).
Select("distinct wire_id").
Where(fmt.Sprintf("host_id = '%s' and deleted=0", hostID)).
Scan(&wires).Error
return wires, err
}
func SelectHostHasWires() ([]Host2Wire, error) {
wires := []Host2Wire{}
err := HostWires.DB().Table(hostWiresTable).
Select("host_id,wire_id").
Where("deleted=0").
Scan(&wires).Error
return wires, err
}
-30
View File
@@ -1,30 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package models
import (
"github.com/jinzhu/gorm"
)
type Resourcer interface {
DB() *gorm.DB
TableName() string
Model() interface{}
Models() interface{}
}
type Modeler interface {
UUID() string
}
@@ -1,44 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package models
import (
"github.com/jinzhu/gorm"
)
type IsolatedDevice struct {
StandaloneModel
HostID string `json:"host_id,omitempty" gorm:"column:host_id;not null"`
DevType string `json:"dev_type" gorm:"column:dev_type;not null"`
Model string `json:"model" gorm:"column:model;not null"`
GuestID string `json:"guest_id" gorm:"column:guest_id"`
Addr string `json:"addr" gorm:"column:addr"`
VendorDeviceID string `json:"vendor_device_id" gorm:"column:vendor_device_id"`
}
func (d IsolatedDevice) TableName() string {
return isolatedDeviceTable
}
func (d IsolatedDevice) String() string {
str, _ := JsonString(d)
return str
}
func NewIsolatedDeviceResource(db *gorm.DB) (Resourcer, error) {
return newResource(db, isolatedDeviceTable,
func() interface{} { return &IsolatedDevice{} },
func() interface{} { return &([]IsolatedDevice{}) })
}
-58
View File
@@ -1,58 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package models
import (
"fmt"
"strings"
"time"
"github.com/jinzhu/gorm"
)
type Metadata struct {
ID string `json:"id" gorm:"primary_key;column:id;type:varchar(128) CHARACTER SET ascii"`
Key string `json:"key" gorm:"primary_key;column:key;type:varchar(64) CHARACTER SET ascii"`
Value string `json:"value" gorm:"type:text CHARACTER SET utf8"`
UpdatedAt time.Time `json:"updated_at" gorm:"column:updated_at;type:datetime" sql:"DEFAULT:NULL"`
}
func (m Metadata) TableName() string {
return metadataTable
}
func (m Metadata) String() string {
str, _ := JsonString(m)
return str
}
func NewMetadataResource(db *gorm.DB) (Resourcer, error) {
return newResource(db, metadataTable,
func() interface{} { return &Metadata{} },
func() interface{} { return &([]Metadata{}) })
}
func FetchMetadatas(resourceName string, ids, keys []string) ([]interface{}, error) {
idsWithRes := make([]string, len(ids))
for i, id := range ids {
idsWithRes[i] = fmt.Sprintf("%s::%s", resourceName, id)
}
rows, err := Metadatas.DB().Table(metadataTable).
Where(fmt.Sprintf("id in ('%s') AND `key` in ('%s')", strings.Join(idsWithRes, "','"), strings.Join(keys, "','"))).Rows()
if err != nil {
return nil, err
}
return rowsToArray(Metadatas, rows)
}
-85
View File
@@ -1,85 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package models
import (
"fmt"
"github.com/jinzhu/gorm"
)
const (
NetInterfaceResourceName = "netinterface"
)
type NetInterface struct {
Mac string `json:"mac,omitempty" gorm:"not null"`
BaremetalId string `json:"baremetal_id,omitempty"`
WireId string `json:"wire_id,omitempty"`
Rate int64 `json:"rate,omitempty"`
NicType string `json:"nic_type,omitempty"`
Index int `json:"index,omitempty"`
LinkUp int `json:"link_up,omitempty"`
Mtu int64 `json:"mtu,omitempty"`
}
func (n NetInterface) TableName() string {
return netinterfacesTable
}
func (n NetInterface) String() string {
str, _ := JsonString(n)
return str
}
func NewNetInterfacesResource(db *gorm.DB) (Resourcer, error) {
model := func() interface{} {
return &NetInterface{}
}
models := func() interface{} {
netInterfaces := []NetInterface{}
return &netInterfaces
}
return newResource(db, netinterfacesTable, model, models)
}
type BaremetalWire struct {
BaremetalID string `json:"baremetal_id" gorm:"column:baremetal_id;not null"`
WireID string `json:"wire_id" gorm:"column:wire_id;not null"`
}
func (c BaremetalWire) First() string {
return c.WireID
}
func SelectWiresWithBaremetalID(baremetalID string) ([]BaremetalWire, error) {
baremetalWires := []BaremetalWire{}
err := NetInterfaces.DB().Table(netinterfacesTable).
Select("distinct wire_id").
Where(fmt.Sprintf("baremetal_id = '%s'", baremetalID)).
Scan(&baremetalWires).Error
return baremetalWires, err
}
func SelectWiresAndBaremetals() ([]BaremetalWire, error) {
baremetalWires := []BaremetalWire{}
err := NetInterfaces.DB().Table(netinterfacesTable).
Select("baremetal_id,wire_id").
Scan(&baremetalWires).Error
return baremetalWires, err
}
-291
View File
@@ -1,291 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package models
import (
"fmt"
"strings"
"github.com/jinzhu/gorm"
"yunion.io/x/pkg/util/netutils"
"yunion.io/x/pkg/utils"
)
const (
NetworkResourceName = "network"
GuestNicCountC = "GuestNiCount"
GroupNicCountC = "GroupNicCount"
BaremetalNicCountC = "BaremetalNicCount"
ReserveDipNicCountC = "ReserveDipNicCount"
)
type Network struct {
StandaloneModel
Status string `json:"status,omitempty" gorm:"not null"`
TenantID string `json:"tenant_id,omitempty" gorm:"not null"`
UserId string `json:"user_id,omitempty" gorm:"not null"`
IsPublic int `json:"is_public,omitempty" gorm:"not null"`
GuestIpStart string `json:"guest_ip_start,omitempty" gorm:"not null"`
GuestIpEnd string `json:"guest_ip_end,omitempty" gorm:"not null"`
GuestIpMask int `json:"guest_ip_mask,omitempty" gorm:"not null"`
GuestGateway string `json:"guest_gateway,omitempty"`
GuestDns string `json:"guest_dns,omitempty"`
GuestIp6Start string `json:"guest_ip6_start,omitempty"`
GuestIp6End string `json:"guest_ip6_end,omitempty"`
GuestIp6Mask int `json:"guest_ip6_mask,omitempty"`
GuestGateway6 string `json:"guest_gateway6,omitempty"`
GuestDns6 string `json:"guest_dns6,omitempty"`
GuestDomain6 string `json:"guest_domain6,omitempty"`
VlanId int64 `json:"vlan_id,omitempty" gorm:"not null"`
DhcpHostId string `json:"dhcp_host_id,omitempty"`
WireID string `json:"wire_id,omitempty"`
IsChanged int `json:"is_changed,omitempty" gorm:"not null"`
IsSystem int `json:"is_system,omitempty"`
GuestDhcp string `json:"guest_dhcp,omitempty"`
BillingType string `json:"billing_type,omitempty"`
ServerType string `json:"server_type,omitempty"`
VpcId string `json:"vpc_id,omitempty"`
ZoneId string `json:"zone_id,omitempty"`
AcSubnetId string `json:"ac_subnet_id,omitempty"`
}
func (n Network) TableName() string {
return networksTable
}
func (n Network) String() string {
str, _ := JsonString(n)
return str
}
func NewNetworksResource(db *gorm.DB) (Resourcer, error) {
model := func() interface{} {
return &Network{}
}
models := func() interface{} {
networks := []Network{}
return &networks
}
return newResource(db, networksTable, model, models)
}
func SelectNetworksWithByWireIDs(wireIDs []string) ([]WireNetwork, error) {
networks := []WireNetwork{}
err := Networks.DB().Table(networksTable).
Select("distinct id").
Where(fmt.Sprintf("wire_id in ('%s') and deleted=0", strings.Join(wireIDs, "','"))).
Scan(&networks).Error
return networks, err
}
func SelectWireIDsHasNetworks() ([]WireNetwork, error) {
networks := []WireNetwork{}
err := Networks.DB().Table(networksTable).
Select("id,wire_id").
Where("deleted=0").
Scan(&networks).Error
return networks, err
}
type WireNetwork struct {
ID string `json:"id,omitempty" gorm:"not null"`
TenantID string `json:"tenant_id,omitempty" gorm:"not null"`
GuestIpStart string `json:"guest_ip_start,omitempty" gorm:"not null"`
GuestIpEnd string `json:"guest_ip_end,omitempty" gorm:"not null"`
IsPublic int `json:"is_public,omitempty" gorm:"not null"`
WireID string `json:"wire_id,omitempty"`
ServerType string `json:"server_type,omitempty"`
}
func (c WireNetwork) First() string {
return c.ID
}
func SelectNetworksWithByWireIDsi(wireIDs []string) ([]WireNetwork, error) {
networks := []WireNetwork{}
err := Networks.DB().Table(networksTable).
Select("distinct id,wire_id,tenant_id,is_public,server_type,guest_ip_start,guest_ip_end").
Where(fmt.Sprintf("wire_id in ('%s') and deleted=0", strings.Join(wireIDs, "','"))).
Scan(&networks).Error
return networks, err
}
type NetworkSchedResult struct {
ID string `json:"id"`
Name string `json:"name"`
TenantID string `json:"tenant_id"`
IsPublic bool `json:"is_public"`
ServerType string `json:"server_type"`
Ports int `json:"ports"`
IsExit bool `json:"is_exit"`
Wire string `json:"wire_name"`
WireID string `json:"wire_id"`
StartIp string `json:"start_ip"`
EndIp string `json:"end_ip"`
}
func HostNetworkSchedResults(hostID string) ([]*NetworkSchedResult, error) {
hostAndWires, err := SelectWiresWithHostID(hostID)
if err != nil {
return nil, err
}
if len(hostAndWires) == 0 {
return nil, fmt.Errorf("Host %q not in wire.", hostID)
}
wireIDs := []string{}
for _, hostWire := range hostAndWires {
wireIDs = append(wireIDs, hostWire.WireID)
}
hostNets, err := FetchByWireIDs(Networks, wireIDs)
if err != nil {
return nil, err
}
netRes := []*NetworkSchedResult{}
for _, n := range hostNets {
r, err := NewNetworkSchedResult(n.(*Network))
if err != nil {
return nil, fmt.Errorf("NewNetworkBuildResult err: %v", err)
}
netRes = append(netRes, r)
}
return netRes, nil
}
func NewNetworkSchedResult(net *Network) (*NetworkSchedResult, error) {
if net == nil {
return nil, fmt.Errorf("empty network model resource")
}
wire, err := FetchByID(Wires, net.WireID)
if err != nil {
return nil, fmt.Errorf("fetch wire %q err: %v", net.WireID, err)
}
res := &NetworkSchedResult{
ID: net.ID,
WireID: net.WireID,
Name: net.Name,
Wire: wire.(*Wire).Name,
TenantID: net.TenantID,
ServerType: net.ServerType,
IsExit: utils.IsExitAddress(net.GuestIpStart),
StartIp: net.GuestIpStart,
EndIp: net.GuestIpEnd,
}
res.IsPublic = net.IsPublic == 1
ports, err := NetworkAvaliableAddress(net)
if err != nil {
return nil, err
}
res.Ports = ports
return res, nil
}
func NetworkAvaliableAddress(net *Network) (ports int, err error) {
totalAddress := utils.IpRangeCount(net.GuestIpStart, net.GuestIpEnd)
guestNicCount, err := NicCount(GuestNicCountC)
if err != nil {
return
}
groupNicCount, err := NicCount(GroupNicCountC)
if err != nil {
return
}
baremetalNicCount, err := NicCount(BaremetalNicCountC)
if err != nil {
return
}
reserveDipNicCount, err := NicCount(ReserveDipNicCountC)
if err != nil {
return
}
ports = totalAddress - guestNicCount[net.ID] - groupNicCount[net.ID] - baremetalNicCount[net.ID] - reserveDipNicCount[net.ID]
return
}
func NicCount(nicName string) (map[string]int, error) {
countsMap := make(map[string]int)
switch nicName {
case GuestNicCountC:
counts, err := GuestNicCounts()
if err != nil {
return nil, err
}
for _, count := range counts {
countsMap[count.NetworkID] = count.Count
}
case GroupNicCountC:
counts, err := GroupNicCounts()
if err != nil {
return nil, err
}
for _, count := range counts {
countsMap[count.NetworkID] = count.Count
}
case BaremetalNicCountC:
counts, err := BaremetalNicCounts()
if err != nil {
return nil, err
}
for _, count := range counts {
countsMap[count.NetworkID] = count.Count
}
case ReserveDipNicCountC:
counts, err := ReserveNicCounts()
if err != nil {
return nil, err
}
for _, count := range counts {
countsMap[count.NetworkID] = count.Count
}
}
return countsMap, nil
}
func (net *NetworkSchedResult) ContainsIp(ip string) (bool, error) {
address, err := netutils.NewIPV4Addr(ip)
if err != nil {
return false, err
}
start, err := netutils.NewIPV4Addr(net.StartIp)
if err != nil {
return false, err
}
end, err := netutils.NewIPV4Addr(net.EndIp)
if err != nil {
return false, err
}
return netutils.NewIPV4AddrRange(start, end).Contains(address), nil
}
-91
View File
@@ -1,91 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package models
import (
"fmt"
"github.com/jinzhu/gorm"
)
const (
ReserveDipsResourceName = "reservedips"
)
type ReserveDipNetwork struct {
StandaloneModel
NetworkID string `json:"network_id,omitempty" gorm:"column:network_id;not null"`
IpAddr string `json:"ip_addr,omitempty" gorm:"column:ip_addr"`
Notes string `json:"notes" gorm:"column:notes"`
}
func (n ReserveDipNetwork) TableName() string {
return reserveDipsTable
}
func (n ReserveDipNetwork) String() string {
s, _ := JsonString(n)
return string(s)
}
func NewReserveDipsNetworksResource(db *gorm.DB) (Resourcer, error) {
model := func() interface{} {
return &ReserveDipNetwork{}
}
models := func() interface{} {
reserveDips := []ReserveDipNetwork{}
return &reserveDips
}
return newResource(db, reserveDipsTable, model, models)
}
type ReserveNicCount struct {
NetworkID string `json:"network_id,omitempty" gorm:"column:network_id;not null"`
Count int `json:"count" gorm:"column:count;not null"`
}
func (c ReserveNicCount) First() string {
return c.NetworkID
}
func (c ReserveNicCount) Second() int {
return c.Count
}
func ReserveNicCounts() ([]ReserveNicCount, error) {
counts := []ReserveNicCount{}
err := ReserveDipsNerworks.DB().Table(reserveDipsTable).
Select("network_id,count(*) as count").
Where("deleted=0").
Group("network_id").
Scan(&counts).Error
return counts, err
}
type ReserveNicCounti struct {
Count int `json:"count" gorm:"column:count;not null"`
}
func (c ReserveNicCounti) First() int {
return c.Count
}
func ReserveNicCountsWithNetworkID(networkID string) (ReserveNicCounti, error) {
counts := ReserveNicCounti{0}
err := ReserveDipsNerworks.DB().Table(reserveDipsTable).
Select("count(*) as count").
Where(fmt.Sprintf("network_id = '%s' and deleted=0", networkID)).
Scan(&counts).Error
return counts, err
}
-105
View File
@@ -1,105 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package models
import (
"fmt"
"strings"
"github.com/jinzhu/gorm"
"yunion.io/x/log"
o "yunion.io/x/onecloud/pkg/scheduler/options"
)
type Storage struct {
StandaloneModel
Capacity int64 `json:"capacity" gorm:"not null"`
StorageType string `json:"storage_type" gorm:"not null"`
MediumType string `json:"medium_type" gorm:"not null"`
Cmtbound *float64 `json:"cmtbound"`
Status string `json:"status" gorm:"not null"`
StorageConf string `json:"storage_conf" gorm:"type:text"`
ZoneID string `json:"zone_id"`
}
func (s Storage) TableName() string {
return storageTable
}
func (s Storage) String() string {
str, _ := JsonString(s)
return str
}
func NewStorageResource(db *gorm.DB) (Resourcer, error) {
model := func() interface{} {
return &Storage{}
}
models := func() interface{} {
storages := []Storage{}
return &storages
}
return newResource(db, storageTable, model, models)
}
type StorageCapacity struct {
StorageID string `json:"storage_id" gorm:"column:storage_id;not null"`
Status string `json:"status" gorm:"not null"`
TotalSize int64 `json:"total_size" gorm:"column:total_size"`
}
func (s StorageCapacity) First() string {
return s.StorageID
}
func (s StorageCapacity) Second() string {
return s.Status
}
func (s StorageCapacity) Third() interface{} {
return s.TotalSize
}
func GetStorageCapacities(storageIDs []string) ([]StorageCapacity, error) {
results := make([]StorageCapacity, 0)
err := Disks.DB().Table(disksTable).
Select("storage_id, status, sum(disk_size) as total_size").
Where(fmt.Sprintf("storage_id in ('%s') and deleted=0", strings.Join(storageIDs, "','"))).
Group("storage_id, status").Scan(&results).Error
if err != nil {
return nil, err
}
backupResults := make([]StorageCapacity, 0)
err = Disks.DB().Table(disksTable).
Select("backup_storage_id as storage_id, status, sum(disk_size) as total_size").
Where(fmt.Sprintf("storage_id in ('%s') and deleted=0", strings.Join(storageIDs, "','"))).
Group("storage_id, status").Scan(&backupResults).Error
if err != nil {
log.Errorf("Get backup storage error: %v", err)
return results, nil
}
results = append(results, backupResults...)
return results, nil
}
func (s Storage) OverCommitBound() float64 {
if s.Cmtbound != nil {
return *s.Cmtbound
}
return float64(o.GetOptions().DefaultStorageOvercommitBound)
}
-74
View File
@@ -1,74 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package models
import (
"github.com/jinzhu/gorm"
)
const (
NetWireResourceName = "wire"
)
type Wire struct {
StandaloneModel
Bandwidth int64 `json:"bandwidth,omitempty" gorm:"not null"`
NetDns string `json:"net_dns,omitempty"`
NetDomain string `json:"net_domain,omitempty"`
VpcVersion int64 `json:"vpc_version,omitempty" gorm:"not null"`
}
func (w Wire) TableName() string {
return wiresTable
}
func (w Wire) String() string {
str, _ := JsonString(w)
return str
}
func NewWiresResource(db *gorm.DB) (Resourcer, error) {
model := func() interface{} {
return &Wire{}
}
models := func() interface{} {
wires := []Wire{}
return &wires
}
return newResource(db, wiresTable, model, models)
}
type WireInfo struct {
ID string `json:"id" gorm:"column:id;not null"`
Name string `json:"name" gorm:"column:name;not null"`
}
func (i WireInfo) First() string {
return i.ID
}
func (i WireInfo) Second() string {
return i.Name
}
func LoadAllWires() ([]WireInfo, error) {
wires := []WireInfo{}
err := Wires.DB().Table(wiresTable).
Select("id,name").
Where("deleted=0").
Scan(&wires).Error
return wires, err
}
-56
View File
@@ -1,56 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package models
import (
"github.com/jinzhu/gorm"
)
type Zone struct {
StandaloneModel
Status string `json:"status" gorm:"column:status;not null"`
Location string `gorm:"column:location"`
ManagerUri string `gorm:"column:manager_uri"`
CloudregionId string `gorm:"column:cloudregion_id"`
}
func (z Zone) TableName() string {
return zonesTable
}
func (z Zone) String() string {
s, _ := JsonString(z)
return s
}
func NewZoneResource(db *gorm.DB) (Resourcer, error) {
return newResource(db, zonesTable,
func() interface{} {
return &Zone{}
},
func() interface{} {
zones := []Zone{}
return &zones
},
)
}
func FetchZoneByID(id string) (*Zone, error) {
zone, err := FetchByID(Zones, id)
if err != nil {
return nil, err
}
return zone.(*Zone), nil
}
-3
View File
@@ -74,9 +74,6 @@ type SchedOptions struct {
NetworkCacheTTL string `help:"Build network info from database to cache TTL" default:"0s"`
NetworkCachePeriod string `help:"Build network info from database to cache TTL" default:"1m"`
ClusterDBCacheTTL string `help:"Cluster database cache TTL" default:"0s"`
ClusterDBCachePeriod string `help:"Cluster database cache period" default:"5m"`
BaremetalAgentDBCacheTTL string `help:"BaremetalAgent database cache TTL" default:"0s"`
BaremetalAgentDBCachePeriod string `help:"BaremetalAgent database cache period" default:"5m"`
+1 -9
View File
@@ -31,12 +31,12 @@ import (
"yunion.io/x/onecloud/pkg/cloudcommon/db"
computemodels "yunion.io/x/onecloud/pkg/compute/models"
skuman "yunion.io/x/onecloud/pkg/scheduler/data_manager/sku"
"yunion.io/x/onecloud/pkg/scheduler/db/models"
schedhandler "yunion.io/x/onecloud/pkg/scheduler/handler"
schedman "yunion.io/x/onecloud/pkg/scheduler/manager"
o "yunion.io/x/onecloud/pkg/scheduler/options"
"yunion.io/x/onecloud/pkg/util/gin/middleware"
_ "github.com/go-sql-driver/mysql"
_ "yunion.io/x/onecloud/pkg/compute/guestdrivers"
_ "yunion.io/x/onecloud/pkg/compute/hostdrivers"
_ "yunion.io/x/onecloud/pkg/scheduler/algorithmprovider"
@@ -51,14 +51,6 @@ func StartService() error {
gin.SetMode(opts.GinMode)
startSched := func() {
sqlDialect, sqlConn, err := utils.TransSQLAchemyURL(opts.SqlConnection)
if err != nil {
log.Fatalf("Invalid SqlConnection: %v", err)
}
if err := models.Init(sqlDialect, sqlConn); err != nil {
log.Fatalf("DB init error: %v, dialect: %s, url: %s", err, sqlDialect, sqlConn)
}
stopEverything := make(chan struct{})
go skuman.Start(utils.ToDuration(opts.SkuRefreshInterval))
schedman.InitAndStart(stopEverything)