From 4faca63da8c335f85b4a352c318cb1ad3a03db29 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Wed, 23 Apr 2025 02:40:16 -0500 Subject: [PATCH] check for changes and save the config files --- doDroplet.go | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/doDroplet.go b/doDroplet.go index 89e88e7..e39d110 100644 --- a/doDroplet.go +++ b/doDroplet.go @@ -109,7 +109,72 @@ func doEvent(e *virtpb.Event) *virtpb.Event { if err != nil { result.Error = fmt.Sprintf("%v", err) } + return result } + if e.Etype == virtpb.EventType_EDIT { + log.Println("edit event", e.DropletUuid) + result.State = virtpb.Event_DONE + if e.Droplet != nil { + return updateDroplet(e.Droplet) + } + log.Println("unknown edit event") + result.State = virtpb.Event_FAIL + return result + } + + log.Println("unknown event", e) + result.Etype = e.Etype + result.State = virtpb.Event_FAIL + return result +} + +func updateDroplet(newd *virtpb.Droplet) *virtpb.Event { + var changed bool = false + result := new(virtpb.Event) + + if newd == nil { + result.Error = "updateDroplet() d == nil" + result.State = virtpb.Event_FAIL + return result + } + + d := me.cluster.FindDropletByUuid(newd.Uuid) + if d == nil { + result.Error = "updateDroplet() could not find uuid" + result.State = virtpb.Event_FAIL + return result + } + log.Println("found droplet to update:", newd.Uuid, newd.Hostname, newd.Cpus, newd.Memory) + + if d.Hostname != newd.Hostname && newd.Hostname != "" { + d.Hostname = newd.Hostname + changed = true + } + + if d.Cpus != newd.Cpus && newd.Cpus > 0 { + d.Cpus = newd.Cpus + changed = true + } + + // arbitrary check. don't make vm's with less than 64 MB of RAM + // big enough most things will load with some stdout + if d.Memory != newd.Memory && newd.Memory > (64*1024*1024) { + d.Memory = newd.Memory + changed = true + } + + if changed { + if err := me.cluster.ConfigSave(); err != nil { + log.Info("configsave error", err) + result.Error = fmt.Sprintf("%v", err) + result.State = virtpb.Event_FAIL + return result + } + } else { + log.Println("nothing changed in", newd.Uuid, newd.Hostname) + } + + result.State = virtpb.Event_DONE return result }