moving to a cluster.proto config file
This commit is contained in:
parent
276c5cec2f
commit
e4345c8ad6
7
Makefile
7
Makefile
|
@ -3,7 +3,7 @@
|
|||
# cd ~/go/src/google.golang.org/protobuf/cmd/protoc-gen-go
|
||||
# go install
|
||||
|
||||
all: droplet.pb.go hypervisor.pb.go event.pb.go goimports vet
|
||||
all: proto goimports vet
|
||||
|
||||
vet:
|
||||
@GO111MODULE=off go vet
|
||||
|
@ -22,6 +22,8 @@ clean:
|
|||
rm -f *.pb.go
|
||||
-rm -f go.*
|
||||
|
||||
proto:droplet.pb.go hypervisor.pb.go event.pb.go cluster.pb.go
|
||||
|
||||
droplet.pb.go: droplet.proto
|
||||
autogenpb --proto droplet.proto
|
||||
|
||||
|
@ -31,6 +33,9 @@ hypervisor.pb.go: hypervisor.proto
|
|||
event.pb.go: event.proto
|
||||
autogenpb --proto event.proto
|
||||
|
||||
cluster.pb.go: cluster.proto
|
||||
autogenpb --proto cluster.proto
|
||||
|
||||
deps:
|
||||
apt install golang-goprotobuf-dev
|
||||
apt install protobuf-compiler
|
||||
|
|
20
add.go
20
add.go
|
@ -9,7 +9,7 @@ import (
|
|||
)
|
||||
|
||||
/*
|
||||
func (c *Cluster) InitDroplet(hostname string) (*Droplet, error) {
|
||||
func (c *OldCluster) InitDroplet(hostname string) (*Droplet, error) {
|
||||
var d *Droplet
|
||||
d = new(Droplet)
|
||||
d.Current = new(Current)
|
||||
|
@ -50,7 +50,7 @@ func (x *Hypervisor) SetMemoryGB(gb int) {
|
|||
x.Memory = int64(gb * 1024 * 1024 * 1024)
|
||||
}
|
||||
|
||||
func (c *Cluster) FindDropletByName(name string) *Droplet {
|
||||
func (c *OldCluster) FindDropletByName(name string) *Droplet {
|
||||
loop := c.d.All() // get the list of droplets
|
||||
for loop.Scan() {
|
||||
d := loop.Next()
|
||||
|
@ -61,7 +61,7 @@ func (c *Cluster) FindDropletByName(name string) *Droplet {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (c *Cluster) FindHypervisorByName(name string) *Hypervisor {
|
||||
func (c *OldCluster) FindHypervisorByName(name string) *Hypervisor {
|
||||
for _, h := range c.H.Hypervisors {
|
||||
if h.Hostname == name {
|
||||
return h
|
||||
|
@ -70,7 +70,7 @@ func (c *Cluster) FindHypervisorByName(name string) *Hypervisor {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (c *Cluster) AddHypervisor(hostname string, cpus int, mem int) *Hypervisor {
|
||||
func (c *OldCluster) AddHypervisor(hostname string, cpus int, mem int) *Hypervisor {
|
||||
h := c.FindHypervisorByName(hostname)
|
||||
if h != nil {
|
||||
return h
|
||||
|
@ -91,7 +91,7 @@ func (c *Cluster) AddHypervisor(hostname string, cpus int, mem int) *Hypervisor
|
|||
return h
|
||||
}
|
||||
|
||||
func (c *Cluster) AddEvent(e *Event) {
|
||||
func (c *OldCluster) AddEvent(e *Event) {
|
||||
c.e.Events = append(c.e.Events, e)
|
||||
}
|
||||
|
||||
|
@ -109,7 +109,7 @@ func NewDefaultDroplet(hostname string) *Droplet {
|
|||
return d
|
||||
}
|
||||
|
||||
func (c *Cluster) AddDropletSimple(uuid string, hostname string, cpus int, mem int) *Droplet {
|
||||
func (c *OldCluster) AddDropletSimple(uuid string, hostname string, cpus int, mem int) *Droplet {
|
||||
d := c.FindDropletByName(hostname)
|
||||
if d != nil {
|
||||
return d
|
||||
|
@ -131,7 +131,7 @@ func (c *Cluster) AddDropletSimple(uuid string, hostname string, cpus int, mem i
|
|||
}
|
||||
|
||||
// This isn't for the marketing department
|
||||
func (c *Cluster) AddDropletLocal(name string, hypername string) *Droplet {
|
||||
func (c *OldCluster) AddDropletLocal(name string, hypername string) *Droplet {
|
||||
d := &Droplet{
|
||||
Hostname: name,
|
||||
}
|
||||
|
@ -149,7 +149,7 @@ func (c *Cluster) AddDropletLocal(name string, hypername string) *Droplet {
|
|||
return d
|
||||
}
|
||||
|
||||
func (c *Cluster) BlankFields() {
|
||||
func (c *OldCluster) BlankFields() {
|
||||
loop := c.d.All() // get the list of droplets
|
||||
for loop.Scan() {
|
||||
d := loop.Next()
|
||||
|
@ -161,7 +161,7 @@ func (epb *Events) AppendEvent(e *Event) {
|
|||
epb.Events = append(epb.Events, e)
|
||||
}
|
||||
|
||||
func (c *Cluster) ClusterStable() (bool, string) {
|
||||
func (c *OldCluster) ClusterStable() (bool, string) {
|
||||
last := time.Since(c.Unstable.AsTime())
|
||||
if last > c.UnstableTimeout.AsDuration() {
|
||||
// the cluster has not been stable for 133 seconds
|
||||
|
@ -173,7 +173,7 @@ func (c *Cluster) ClusterStable() (bool, string) {
|
|||
}
|
||||
|
||||
// check the cluster and droplet to make sure it's ready to start
|
||||
func (c *Cluster) DropletReady(d *Droplet) (bool, string) {
|
||||
func (c *OldCluster) DropletReady(d *Droplet) (bool, string) {
|
||||
if c == nil {
|
||||
return false, "cluster == nil"
|
||||
}
|
||||
|
|
|
@ -163,7 +163,7 @@ func (d *Droplet) SetState(newState DropletState) {
|
|||
}
|
||||
|
||||
// records an event that the droplet changed state (aka turned on, turned off, etc)
|
||||
func (c *Cluster) ChangeDropletState(d *Droplet, newState DropletState) error {
|
||||
func (c *OldCluster) ChangeDropletState(d *Droplet, newState DropletState) error {
|
||||
if c == nil {
|
||||
return errors.New("cluster is nil")
|
||||
}
|
||||
|
@ -190,7 +190,7 @@ func (c *Cluster) ChangeDropletState(d *Droplet, newState DropletState) error {
|
|||
}
|
||||
|
||||
// records an event that the droplet migrated to another hypervisor
|
||||
func (c *Cluster) DropletMoved(d *Droplet, newh *Hypervisor) error {
|
||||
func (c *OldCluster) DropletMoved(d *Droplet, newh *Hypervisor) error {
|
||||
if c == nil {
|
||||
return errors.New("cluster is nil")
|
||||
}
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
syntax = "proto3";
|
||||
package virtpb;
|
||||
|
||||
import "google/protobuf/timestamp.proto";
|
||||
|
||||
message Cluster {
|
||||
string uuid = 1; // `autogenpb:unique`
|
||||
string URL = 2; // `autogenpb:unique`
|
||||
google.protobuf.Timestamp ctime = 3; // when the cluster was created
|
||||
}
|
||||
|
||||
message Clusters { // `autogenpb:marshal`
|
||||
string uuid = 1; // `autogenpb:uuid:57ddd763-75f6-4003-bf0e-8dd0f8a44044`
|
||||
string version = 2; // `autogenpb:version:v0.0.1`
|
||||
repeated Cluster clusters = 3;
|
||||
}
|
42
config.go
42
config.go
|
@ -16,7 +16,7 @@ import (
|
|||
|
||||
// writes out the cluster information it seperate files
|
||||
// to make it humanly possible to hand edit things as needed
|
||||
func (c *Cluster) ConfigSave() error {
|
||||
func (c *OldCluster) ConfigSave() error {
|
||||
// try to backup the current cluster config files
|
||||
if err := backupConfig(); err != nil {
|
||||
return err
|
||||
|
@ -64,7 +64,7 @@ func (c *Cluster) ConfigSave() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (c *Cluster) ConfigLoad() error {
|
||||
func (c *OldCluster) ConfigLoad() error {
|
||||
if c == nil {
|
||||
return errors.New("It's not safe to run ConfigLoad() on a nil cluster")
|
||||
}
|
||||
|
@ -157,6 +157,23 @@ func ConfigWriteJSON(a any, filename string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (c *OldCluster) configWriteDroplets() error {
|
||||
fullname := filepath.Join(os.Getenv("VIRTIGO_HOME"), "droplets.new.text")
|
||||
cfgfile, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
|
||||
defer cfgfile.Close()
|
||||
if err != nil {
|
||||
fmt.Println("open config file :", err)
|
||||
return err
|
||||
}
|
||||
loop := c.d.All() // get the list of droplets
|
||||
for loop.Scan() {
|
||||
d := loop.Next()
|
||||
text := prototext.Format(d)
|
||||
fmt.Fprintln(cfgfile, text)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ConfigWriteTEXT(a any, filename string) error {
|
||||
fullname := filepath.Join(os.Getenv("VIRTIGO_HOME"), filename)
|
||||
cfgfile, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
|
||||
|
@ -174,19 +191,18 @@ func ConfigWriteTEXT(a any, filename string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (c *Cluster) configWriteDroplets() error {
|
||||
fullname := filepath.Join(os.Getenv("VIRTIGO_HOME"), "droplets.new.text")
|
||||
cfgfile, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
|
||||
defer cfgfile.Close()
|
||||
if err != nil {
|
||||
fmt.Println("open config file :", err)
|
||||
func (c *Clusters) ConfigLoad() error {
|
||||
if c == nil {
|
||||
return errors.New("It's not safe to run ConfigLoad() on a nil cluster")
|
||||
}
|
||||
|
||||
if data, err := loadFile("cluster.text"); err == nil {
|
||||
if err = prototext.Unmarshal(data, c); err != nil {
|
||||
fmt.Println("broken cluster.textconfig file")
|
||||
return err
|
||||
}
|
||||
loop := c.d.All() // get the list of droplets
|
||||
for loop.Scan() {
|
||||
d := loop.Next()
|
||||
text := prototext.Format(d)
|
||||
fmt.Fprintln(cfgfile, text)
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -3,15 +3,15 @@ package virtpb
|
|||
// functions to import and export the protobuf
|
||||
// data to and from config files
|
||||
|
||||
func InitCluster() *Cluster {
|
||||
var c *Cluster
|
||||
c = new(Cluster)
|
||||
func InitCluster() *OldCluster {
|
||||
var c *OldCluster
|
||||
c = new(OldCluster)
|
||||
c.d = new(Droplets)
|
||||
c.H = new(Hypervisors)
|
||||
c.e = new(Events)
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *Cluster) DropletsAll() *DropletIterator {
|
||||
func (c *OldCluster) DropletsAll() *DropletIterator {
|
||||
return c.d.All()
|
||||
}
|
||||
|
|
2
human.go
2
human.go
|
@ -185,7 +185,7 @@ func (d *Droplet) DumpDroplet(w http.ResponseWriter, r *http.Request) (string, e
|
|||
return t, nil
|
||||
}
|
||||
|
||||
func (c *Cluster) DumpDroplet(w http.ResponseWriter, r *http.Request) (string, error) {
|
||||
func (c *OldCluster) DumpDroplet(w http.ResponseWriter, r *http.Request) (string, error) {
|
||||
hostname := r.URL.Query().Get("hostname")
|
||||
d := c.FindDropletByName(hostname)
|
||||
if d == nil {
|
||||
|
|
|
@ -7,7 +7,7 @@ import (
|
|||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
)
|
||||
|
||||
type Cluster struct {
|
||||
type OldCluster struct {
|
||||
sync.RWMutex
|
||||
|
||||
Dirs []string
|
||||
|
@ -18,20 +18,20 @@ type Cluster struct {
|
|||
UnstableTimeout *durationpb.Duration
|
||||
}
|
||||
|
||||
func (c *Cluster) GetDropletsPB() *Droplets {
|
||||
func (c *OldCluster) GetDropletsPB() *Droplets {
|
||||
return c.d
|
||||
}
|
||||
|
||||
func (c *Cluster) GetHypervisorsPB() *Hypervisors {
|
||||
func (c *OldCluster) GetHypervisorsPB() *Hypervisors {
|
||||
return c.H
|
||||
}
|
||||
|
||||
func (c *Cluster) GetEventsPB() *Events {
|
||||
func (c *OldCluster) GetEventsPB() *Events {
|
||||
return c.e
|
||||
}
|
||||
|
||||
// adds a new droplet. enforce unique hostnames
|
||||
func (c *Cluster) AddDroplet(newd *Droplet) bool {
|
||||
func (c *OldCluster) AddDroplet(newd *Droplet) bool {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
|
|
@ -51,7 +51,7 @@ func CreateSampleEvents(total int) *Events {
|
|||
return e
|
||||
}
|
||||
|
||||
func CreateSampleCluster(total int) *Cluster {
|
||||
func CreateSampleCluster(total int) *OldCluster {
|
||||
c := InitCluster()
|
||||
|
||||
for i := 0; i < total; i++ {
|
||||
|
|
Loading…
Reference in New Issue