From 347c58e15f39d28908264649504c5408a7251713 Mon Sep 17 00:00:00 2001 From: LordMathis Date: Wed, 1 Oct 2025 22:58:57 +0200 Subject: [PATCH] Enhance instance manager to persist remote instances and update tracking on modifications --- pkg/manager/operations.go | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/pkg/manager/operations.go b/pkg/manager/operations.go index b7fdda8..f1e5929 100644 --- a/pkg/manager/operations.go +++ b/pkg/manager/operations.go @@ -72,6 +72,11 @@ func (im *instanceManager) CreateInstance(name string, options *instance.CreateI im.instances[name] = inst im.instanceNodeMap[name] = nodeConfig + // Persist the remote instance locally for tracking across restarts + if err := im.persistInstance(inst); err != nil { + return nil, fmt.Errorf("failed to persist remote instance %s: %w", name, err) + } + return inst, nil } @@ -132,7 +137,24 @@ func (im *instanceManager) UpdateInstance(name string, options *instance.CreateI // Check if instance is remote and delegate to remote operation if node := im.getNodeForInstance(inst); node != nil { - return im.UpdateRemoteInstance(node, name, options) + updatedInst, err := im.UpdateRemoteInstance(node, name, options) + if err != nil { + return nil, err + } + + // Update local tracking + im.mu.Lock() + im.instances[name] = updatedInst + im.mu.Unlock() + + // Persist the updated remote instance locally + im.mu.Lock() + defer im.mu.Unlock() + if err := im.persistInstance(updatedInst); err != nil { + return nil, fmt.Errorf("failed to persist updated remote instance %s: %w", name, err) + } + + return updatedInst, nil } if options == nil { @@ -192,9 +214,15 @@ func (im *instanceManager) DeleteInstance(name string) error { // Clean up local tracking im.mu.Lock() + defer im.mu.Unlock() delete(im.instances, name) delete(im.instanceNodeMap, name) - im.mu.Unlock() + + // Delete the instance's config file if persistence is enabled + instancePath := filepath.Join(im.instancesConfig.InstancesDir, name+".json") + if err := os.Remove(instancePath); err != nil && !os.IsNotExist(err) { + return fmt.Errorf("failed to delete config file for remote instance %s: %w", name, err) + } return nil }