Skip to content

Commit

Permalink
feature: datastore and network.
Browse files Browse the repository at this point in the history
  • Loading branch information
danieldin95 committed Feb 27, 2020
1 parent 936415a commit 521287c
Show file tree
Hide file tree
Showing 16 changed files with 387 additions and 75 deletions.
24 changes: 24 additions & 0 deletions compute/libvirtc/diskutils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package libvirtc

import "fmt"

type Disk struct {
//
}

func (d *Disk) Slot2Dev(bus string, slot uint8) string {
prefix := "vd"
if bus == "ide" || bus == "scsi" {
prefix = "hd"
}
if slot <= 26 {
return prefix + string('a'+slot-1)
}
return ""
}

func (d *Disk) Slot2DiskName(slot uint8) string {
return fmt.Sprintf("disk%d.img", slot)
}

var DISK = &Disk{}
55 changes: 25 additions & 30 deletions compute/libvirtc/domain.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package libvirtc

import (
"fmt"
"github.com/libvirt/libvirt-go"
)

Expand Down Expand Up @@ -41,37 +40,33 @@ func (d *Domain) GetXMLDesc(secure bool) (string, error) {
}
}

type Disk struct {
//
}

func (d *Disk) Slot2Dev(bus string, slot uint8) string {
prefix := "vd"
if bus == "ide" || bus == "scsi" {
prefix = "hd"
}
if slot <= 26 {
return prefix + string('a'+slot-1)
func ListDomains() ([]Domain, error) {
hyper, err := GetHyper()
if err != nil {
return nil, err
}
return ""
}

func (d *Disk) Slot2DiskName(slot uint8) string {
return fmt.Sprintf("disk%d.img", slot)
return hyper.ListAllDomains()
}

var DISK = &Disk{}

type Interface struct {
//
}

func (int *Interface) Slot2Dev(slot uint8) string {
prefix := "vnet"
if slot <= 32 {
return fmt.Sprintf("%s%d", prefix, slot)
func DomainState2Str(state libvirt.DomainState) string {
switch state {
case libvirt.DOMAIN_NOSTATE:
return "no-state"
case libvirt.DOMAIN_RUNNING:
return "running"
case libvirt.DOMAIN_BLOCKED:
return "blocked"
case libvirt.DOMAIN_PAUSED:
return "paused"
case libvirt.DOMAIN_SHUTDOWN:
return "shutdown"
case libvirt.DOMAIN_CRASHED:
return "crashed"
case libvirt.DOMAIN_PMSUSPENDED:
return "pm-suspended"
case libvirt.DOMAIN_SHUTOFF:
return "shutoff"
default:
return "unknown"
}
return ""
}

var INTERFACE = &Interface{}
17 changes: 17 additions & 0 deletions compute/libvirtc/interfaceutils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package libvirtc

import "fmt"

type Interface struct {
//
}

func (int *Interface) Slot2Dev(slot uint8) string {
prefix := "vnet"
if slot <= 32 {
return fmt.Sprintf("%s%d", prefix, slot)
}
return ""
}

var INTERFACE = &Interface{}
2 changes: 1 addition & 1 deletion http/api/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func InstanceConf2XML(conf *schema.InstanceConf) (libvirtc.DomainXML, error) {
// cpu and memory
if conf.CpuMode != "" {
dom.CPUXml = libvirtc.CPUXML{
Mode: conf.CpuMode,
Mode: conf.CpuMode,
Check: "full",
}
}
Expand Down
26 changes: 26 additions & 0 deletions http/schema/datastore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package schema

import (
"github.com/danieldin95/lightstar/storage/libvirts"
)

type DataStore struct {
UUID string `json:"uuid"`
Name string `json:"name"`
State string `json:"state"`
Capacity uint64 `json:"capacity"` // bytes
Allocation uint64 `json:"capacity"` // bytes
Available uint64 `json:"available"` // Bytes
}

func NewDataStore(pol libvirts.Pool) DataStore {
obj := DataStore{}
obj.Name, _ = pol.GetName()
if info, err := pol.GetInfo(); err == nil {
obj.State = libvirts.PoolState2Str(info.State)
obj.Capacity = info.Capacity
obj.Available = info.Available
obj.Allocation = info.Allocation
}
return obj
}
32 changes: 5 additions & 27 deletions http/schema/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package schema
import (
"github.com/danieldin95/lightstar/compute/libvirtc"
"github.com/danieldin95/lightstar/libstar"
"github.com/libvirt/libvirt-go"
)

type Version struct {
Expand Down Expand Up @@ -40,31 +39,10 @@ func NewHyper() (hs Hyper) {
return hs
}

func InstanceState2Str(state libvirt.DomainState) string {
switch state {
case libvirt.DOMAIN_NOSTATE:
return "nostate"
case libvirt.DOMAIN_RUNNING:
return "running"
case libvirt.DOMAIN_BLOCKED:
return "blocked"
case libvirt.DOMAIN_PAUSED:
return "paused"
case libvirt.DOMAIN_SHUTDOWN:
return "shutdown"
case libvirt.DOMAIN_CRASHED:
return "crashed"
case libvirt.DOMAIN_PMSUSPENDED:
return "pmsuspended"
case libvirt.DOMAIN_SHUTOFF:
return "shutoff"
default:
return "unknown"
}
}

type Index struct {
Version Version `json:"version"`
Hyper Hyper `json:"hyper"`
Instances []Instance `json:"instances"`
Version Version `json:"version"`
Hyper Hyper `json:"hyper"`
Instances []Instance `json:"instances"`
DataStores []DataStore `json:"datastores"`
Networks []Network `json:"networks"`
}
2 changes: 1 addition & 1 deletion http/schema/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func NewInstance(dom libvirtc.Domain) Instance {
obj.UUID, _ = dom.GetUUIDString()
obj.Name, _ = dom.GetName()
if info, err := dom.GetInfo(); err == nil {
obj.State = InstanceState2Str(info.State)
obj.State = libvirtc.DomainState2Str(info.State)
obj.MaxMem = info.MaxMem
obj.Memory = info.Memory
obj.MaxCpu = info.NrVirtCpu
Expand Down
24 changes: 24 additions & 0 deletions http/schema/network.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package schema

import (
"github.com/danieldin95/lightstar/network/libvirtn"
)

type Network struct {
UUID string `json:"uuid"`
Name string `json:"name"`
State string `json:"state"`
Address string `json:"network"`
Mode string `json:"mode"` // nat, router.
}

func NewNetwork(net libvirtn.Network) Network {
obj := Network{}
obj.Name, _ = net.GetName()
if ok, _ := net.IsActive(); ok {
obj.State = "active"
} else {
obj.State = "inactive"
}
return obj
}
12 changes: 10 additions & 2 deletions http/static/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,19 @@ a:hover, a:focus {
color: darkgreen;
}

.paused, .blocked, .pmsuspended {
.paused, .blocked, .pm-suspended {
color: chocolate;
}

.nostate, .crashed, .unknown {
.no-state, .crashed, .unknown {
color: black;
}

.building {
color: chocolate;
}

.degraded, .inactive, .inaccessible, .unknown {
color: black;
}

Expand Down
2 changes: 1 addition & 1 deletion http/static/js/widget/instance/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class InstanceCreate extends ModalFormBase {
let selector = this.selector;

$.getJSON("/api/bridge", function (data) {
// selector.find("option").remove();
selector.find("option").remove();
data.forEach(function (e, i) {
if (e['type'] == 'bridge') {
selector.append(Option(`Linux Bridge #${e['name']}`, e['name']));
Expand Down
Loading

0 comments on commit 521287c

Please sign in to comment.