Browse Source

1.2 - 50%: templates generate/save/load

dev
akumas 1 year ago
parent
commit
d53ed32b69
  1. 2
      go.sum
  2. 12
      src/modules/go.mod
  3. 5
      src/modules/go.sum
  4. 144
      src/modules/resource_manager.go
  5. 10
      src/modules/system_template.go

2
go.sum

@ -20,6 +20,8 @@ github.com/project-nano/framework v1.0.1 h1:7UVdCNVMeb9D5wRRACV0SohljlXmcYYFT93c
github.com/project-nano/framework v1.0.1/go.mod h1:tlK/6uw7c0RfqW9es+nbqhCTlJchGmHcPjTdhNUUxXo=
github.com/project-nano/sonar v0.0.0-20190628085230-df7942628d6f h1:NorTDWkZl22V1v/2t0gG01ylSACOmjDmrGpN3R/Pbgg=
github.com/project-nano/sonar v0.0.0-20190628085230-df7942628d6f/go.mod h1:VYPy/Adnn0NLwbDfa/7vv12vWbftUqTHZzwt83Q5QAo=
github.com/rs/xid v1.2.1 h1:mhH9Nq+C1fY2l1XIpgxIiUOfNpRBYH1kKcr+qfKgjRc=
github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/sevlyar/go-daemon v0.1.5 h1:Zy/6jLbM8CfqJ4x4RPr7MJlSKt90f00kNM1D401C+Qk=

12
src/modules/go.mod

@ -2,24 +2,16 @@ module github.com/project-nano/core/modules
go 1.13
replace (
github.com/project-nano/framework => /home/develop/nano/framework
)
replace github.com/project-nano/framework => /home/develop/nano/framework
require (
github.com/julienschmidt/httprouter v1.3.0
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 // indirect
github.com/klauspost/cpuid v1.2.3 // indirect
github.com/klauspost/reedsolomon v1.9.3 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/pkg/errors v0.9.1
github.com/project-nano/framework v1.0.1
github.com/project-nano/sonar v0.0.0-20190628085230-df7942628d6f // indirect
github.com/rs/xid v1.2.1
github.com/satori/go.uuid v1.2.0
github.com/sevlyar/go-daemon v0.1.5 // indirect
github.com/templexxx/cpufeat v0.0.0-20180724012125-cef66df7f161 // indirect
github.com/templexxx/xor v0.0.0-20191217153810-f85b25db303b // indirect
github.com/tjfoc/gmsm v1.3.0 // indirect
github.com/xtaci/kcp-go v5.4.20+incompatible // indirect
github.com/xtaci/lossyconn v0.0.0-20200209145036-adba10fffc37 // indirect
golang.org/x/crypto v0.0.0-20200406173513-056763e48d71 // indirect

5
src/modules/go.sum

@ -13,10 +13,10 @@ github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWb
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/project-nano/framework v1.0.1 h1:7UVdCNVMeb9D5wRRACV0SohljlXmcYYFT93cLvXSIR8=
github.com/project-nano/framework v1.0.1/go.mod h1:tlK/6uw7c0RfqW9es+nbqhCTlJchGmHcPjTdhNUUxXo=
github.com/project-nano/sonar v0.0.0-20190628085230-df7942628d6f h1:NorTDWkZl22V1v/2t0gG01ylSACOmjDmrGpN3R/Pbgg=
github.com/project-nano/sonar v0.0.0-20190628085230-df7942628d6f/go.mod h1:VYPy/Adnn0NLwbDfa/7vv12vWbftUqTHZzwt83Q5QAo=
github.com/rs/xid v1.2.1 h1:mhH9Nq+C1fY2l1XIpgxIiUOfNpRBYH1kKcr+qfKgjRc=
github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/sevlyar/go-daemon v0.1.5 h1:Zy/6jLbM8CfqJ4x4RPr7MJlSKt90f00kNM1D401C+Qk=
@ -27,6 +27,7 @@ github.com/templexxx/xor v0.0.0-20191217153810-f85b25db303b h1:fj5tQ8acgNUr6O8LE
github.com/templexxx/xor v0.0.0-20191217153810-f85b25db303b/go.mod h1:5XA7W9S6mni3h5uvOC75dA3m9CCCaS83lltmc0ukdi4=
github.com/tjfoc/gmsm v1.3.0 h1:i7c6Za/IlgBvnGxYpfD7L3TGuaS+v6oGcgq+J9/ecEA=
github.com/tjfoc/gmsm v1.3.0/go.mod h1:HaUcFuY0auTiaHB9MHFGCPx5IaLhTUd2atbCFBQXn9w=
github.com/xtaci/kcp-go v4.3.4+incompatible/go.mod h1:bN6vIwHQbfHaHtFpEssmWsN45a+AZwO7eyRCmEIbtvE=
github.com/xtaci/kcp-go v5.4.20+incompatible h1:TN1uey3Raw0sTz0Fg8GkfM0uH3YwzhnZWQ1bABv5xAg=
github.com/xtaci/kcp-go v5.4.20+incompatible/go.mod h1:bN6vIwHQbfHaHtFpEssmWsN45a+AZwO7eyRCmEIbtvE=
github.com/xtaci/lossyconn v0.0.0-20200209145036-adba10fffc37 h1:EWU6Pktpas0n8lLQwDsRyZfmkPeRbdgPtW609es+/9E=

144
src/modules/resource_manager.go

@ -48,11 +48,12 @@ type addressPoolDefine struct {
Ranges []AddressRangeStatus `json:"ranges,omitempty"`
}
type zoneDefine struct {
Zone string `json:"zone"`
Pools []poolDefine `json:"pools"`
StoragePools []storageDefine `json:"storage_pools,omitempty"`
AddressPools []addressPoolDefine `json:"address_pools,omitempty"`
type ResourceData struct {
Zone string `json:"zone"`
Pools []poolDefine `json:"pools"`
StoragePools []storageDefine `json:"storage_pools,omitempty"`
AddressPools []addressPoolDefine `json:"address_pools,omitempty"`
SystemTemplates []SystemTemplate `json:"system_templates,omitempty"`
}
//memory status define
@ -143,6 +144,8 @@ type ResourceManager struct {
batchCreateTasks map[string]BatchCreateGuestTask
batchDeleteTasks map[string]BatchDeleteGuestTask
batchStopTasks map[string]BatchStopGuestTask
templates map[string]SystemTemplate
allTemplateID []string
generator *rand.Rand
zone ManagedZone
startTime time.Time
@ -420,6 +423,7 @@ func CreateResourceManager(dataPath string) (manager *ResourceManager, err error
manager.pendingError = map[string]error{}
manager.storagePools = map[string]StoragePoolInfo{}
manager.addressPools = map[string]ManagedAddressPool{}
manager.templates = map[string]SystemTemplate{}
manager.migrations = map[string]MigrationStatus{}
manager.batchCreateTasks = map[string]BatchCreateGuestTask{}
manager.batchDeleteTasks = map[string]BatchDeleteGuestTask{}
@ -3831,7 +3835,7 @@ func (manager *ResourceManager) evaluateConfigureCapacity(cell ManagedComputeCel
}
func (manager *ResourceManager) saveConfig() error {
var config zoneDefine
var config ResourceData
var totalPools, totalCells = 0, 0
config.Zone = manager.zone.Name
for poolName, poolStatus := range manager.pools {
@ -3879,6 +3883,9 @@ func (manager *ResourceManager) saveConfig() error {
}
config.AddressPools = append(config.AddressPools, define)
}
for _, template := range manager.templates{
config.SystemTemplates = append(config.SystemTemplates, template)
}
data, err := json.MarshalIndent(config, "", " ")
if err != nil {
return err
@ -3892,30 +3899,101 @@ func (manager *ResourceManager) saveConfig() error {
return nil
}
func (manager *ResourceManager) loadConfig() error {
if _, err := os.Stat(manager.dataFile); os.IsNotExist(err) {
const (
DefaultPoolName = "default"
DefaultZoneName = "default"
)
manager.zone = ManagedZone{}
manager.zone.Name = DefaultZoneName
manager.cells = map[string]ManagedComputeCell{}
var defaultPool = ManagedComputePool{}
defaultPool.Name = DefaultPoolName
defaultPool.Enabled = true
defaultPool.Cells = map[string]bool{}
defaultPool.InstanceNames = map[string]string{}
manager.pools = map[string]ManagedComputePool{DefaultPoolName: defaultPool}
log.Println("<resource_manager> no config available, create default compute pool")
func (manager *ResourceManager) generateDefaultTemplates() (templates []SystemTemplate, err error){
templates = append(templates, CreateSystemTemplate(SystemTemplateConfig{
Name: "CentOS 7",
OperatingSystem: SystemNameLinux,
Disk: DiskBusSCSI,
Network: NetworkModelVIRTIO,
Display: DisplayDriverVGA,
Control: RemoteControlVNC,
USB: USBModelXHCI,
Tablet: TabletBusUSB,
}))
templates = append(templates, CreateSystemTemplate(SystemTemplateConfig{
Name: "CentOS 6",
OperatingSystem: SystemNameLinux,
Disk: DiskBusSATA,
Network: NetworkModelVIRTIO,
Display: DisplayDriverVGA,
Control: RemoteControlVNC,
USB: USBModelXHCI,
Tablet: TabletBusUSB,
}))
templates = append(templates, CreateSystemTemplate(SystemTemplateConfig{
Name: "Windows Server 2012",
OperatingSystem: SystemNameWindows,
Disk: DiskBusSATA,
Network: NetworkModelE1000,
Display: DisplayDriverVGA,
Control: RemoteControlVNC,
USB: USBModelXHCI,
Tablet: TabletBusUSB,
}))
templates = append(templates, CreateSystemTemplate(SystemTemplateConfig{
Name: "General",
OperatingSystem: SystemNameLinux,
Disk: DiskBusSATA,
Network: NetworkModelRTL8139,
Display: DisplayDriverVGA,
Control: RemoteControlVNC,
USB: USBModelNone,
Tablet: TabletBusUSB,
}))
templates = append(templates, CreateSystemTemplate(SystemTemplateConfig{
Name: "Legacy",
OperatingSystem: SystemNameLinux,
Disk: DiskBusIDE,
Network: NetworkModelRTL8139,
Display: DisplayDriverCirrus,
Control: RemoteControlVNC,
USB: USBModelNone,
Tablet: TabletBusNone,
}))
log.Printf("<resource_manager> %d default system template(s) generated", len(templates))
return
}
func (manager *ResourceManager) generateDefaultConfig() (err error){
const (
DefaultPoolName = "default"
DefaultZoneName = "default"
)
manager.zone = ManagedZone{}
manager.zone.Name = DefaultZoneName
manager.cells = map[string]ManagedComputeCell{}
var defaultPool = ManagedComputePool{}
defaultPool.Name = DefaultPoolName
defaultPool.Enabled = true
defaultPool.Cells = map[string]bool{}
defaultPool.InstanceNames = map[string]string{}
manager.pools = map[string]ManagedComputePool{DefaultPoolName: defaultPool}
var templates []SystemTemplate
if templates, err = manager.generateDefaultTemplates(); err != nil{
err = fmt.Errorf("genereate templates fail: %s", err.Error())
return
}
for _, template := range templates{
manager.templates[template.ID] = template
manager.allTemplateID = append(manager.allTemplateID, template.ID)
}
log.Println("<resource_manager> default configure generated")
return nil
}
func (manager *ResourceManager) loadConfig() (err error) {
if _, err = os.Stat(manager.dataFile); os.IsNotExist(err) {
if err = manager.generateDefaultConfig(); err != nil{
err = fmt.Errorf("generate default config fail: %s", err.Error())
return
}
return manager.saveConfig()
}
data, err := ioutil.ReadFile(manager.dataFile)
if err != nil {
return err
}
var config zoneDefine
var config ResourceData
if err = json.Unmarshal(data, &config); err != nil {
return err
}
@ -3979,10 +4057,24 @@ func (manager *ResourceManager) loadConfig() error {
var pool = StoragePoolInfo{define.Name, define.Type, define.Host, define.Target}
manager.storagePools[pool.Name] = pool
}
var templates []SystemTemplate
if 0 != len(config.SystemTemplates){
templates = config.SystemTemplates
}else{
if templates, err = manager.generateDefaultTemplates(); err != nil{
err = fmt.Errorf("generate templates fail: %s", err.Error())
return
}
}
for _, template := range templates{
manager.templates[template.ID] = template
manager.allTemplateID = append(manager.allTemplateID, template.ID)
}
manager.zone.Name = config.Zone
log.Printf("<resource_manager> %d pools, %d storage, %d address pool(s), %d cells, %d instances loaded from config",
log.Printf("<resource_manager> load resource success, %d compute/ %d storage/ %d address pools, %d templates, %d cell. %d instance available",
len(manager.pools), len(manager.storagePools), len(manager.addressPools),
len(manager.cells), totalInstances)
len(manager.allTemplateID), len(manager.cells), totalInstances)
return nil
}

10
src/modules/system_template.go

@ -1,5 +1,7 @@
package modules
import "github.com/rs/xid"
type TemplateOperatingSystem int
const (
@ -189,3 +191,11 @@ const (
TabletBusVIRTIO = "virtio"
TabletBusUSB = "usb"
)
func CreateSystemTemplate(config SystemTemplateConfig) SystemTemplate {
var t = SystemTemplate{
ID: xid.New().String(),
SystemTemplateConfig: config,
}
return t
}
Loading…
Cancel
Save