From 67ca1b85bee28b099981dc3ae6105cdfdfaf7521 Mon Sep 17 00:00:00 2001 From: Paul Payne Date: Tue, 14 Oct 2025 19:23:16 +0000 Subject: [PATCH] Functions for common paths. --- BUILDING_WILD_API.md | 6 ++-- internal/api/v1/handlers.go | 4 +-- internal/api/v1/handlers_operations.go | 5 +-- internal/api/v1/handlers_services.go | 4 +-- internal/api/v1/handlers_utilities.go | 6 ++-- internal/apps/apps.go | 20 +++++------ internal/backup/backup.go | 2 +- internal/cluster/cluster.go | 11 +++--- internal/config/manager.go | 9 +++-- internal/context/context.go | 7 ++-- internal/discovery/discovery.go | 4 +-- internal/instance/instance.go | 16 +++++---- internal/node/node.go | 2 +- internal/operations/operations.go | 3 +- internal/pxe/pxe.go | 3 +- internal/services/config.go | 5 ++- internal/services/services.go | 13 ++++--- internal/services/status.go | 4 +-- internal/tools/context.go | 50 ++++++++++++++++++++++++++ 19 files changed, 114 insertions(+), 60 deletions(-) diff --git a/BUILDING_WILD_API.md b/BUILDING_WILD_API.md index 9a877e9..0164188 100644 --- a/BUILDING_WILD_API.md +++ b/BUILDING_WILD_API.md @@ -71,8 +71,8 @@ func (api *API) UtilitiesDashboardToken(w http.ResponseWriter, r *http.Request) return } - // 3. Construct instance-specific paths - kubeconfigPath := filepath.Join(api.dataDir, "instances", instanceName, "kubeconfig") + // 3. Construct instance-specific paths using tools helpers + kubeconfigPath := tools.GetKubeconfigPath(api.dataDir, instanceName) // 4. Perform instance-specific operations token, err := utilities.GetDashboardToken(kubeconfigPath) @@ -113,6 +113,6 @@ func GetDashboardToken(kubeconfigPath string) (*DashboardToken, error) { 1. **Instance name in URL**: Always include instance name as a path parameter (`{name}`) 2. **Extract from mux.Vars()**: Get instance name from `mux.Vars(r)["name"]`, not from context 3. **Validate instance**: Always validate the instance exists before operations -4. **Construct paths explicitly**: Build instance-specific file paths from the instance name +4. **Use path helpers**: Use `tools.GetKubeconfigPath()`, `tools.GetInstanceConfigPath()`, etc. instead of inline `filepath.Join()` constructions 5. **Stateless handlers**: Handlers should not depend on session state or current context 6. **Use tools helpers**: Use `tools.WithKubeconfig()` for kubectl/talosctl commands diff --git a/internal/api/v1/handlers.go b/internal/api/v1/handlers.go index 9ccdae2..ab800f7 100644 --- a/internal/api/v1/handlers.go +++ b/internal/api/v1/handlers.go @@ -7,7 +7,6 @@ import ( "log" "net/http" "os" - "path/filepath" "time" "github.com/gorilla/mux" @@ -19,6 +18,7 @@ import ( "github.com/wild-cloud/wild-central/daemon/internal/instance" "github.com/wild-cloud/wild-central/daemon/internal/operations" "github.com/wild-cloud/wild-central/daemon/internal/secrets" + "github.com/wild-cloud/wild-central/daemon/internal/tools" ) // API holds all dependencies for API handlers @@ -37,7 +37,7 @@ type API struct { // Note: Setup files (cluster-services, cluster-nodes, etc.) are now embedded in the binary func NewAPI(dataDir, appsDir string) (*API, error) { // Ensure base directories exist - instancesDir := filepath.Join(dataDir, "instances") + instancesDir := tools.GetInstancesPath(dataDir) if err := os.MkdirAll(instancesDir, 0755); err != nil { return nil, fmt.Errorf("failed to create instances directory: %w", err) } diff --git a/internal/api/v1/handlers_operations.go b/internal/api/v1/handlers_operations.go index 9566c98..f17853b 100644 --- a/internal/api/v1/handlers_operations.go +++ b/internal/api/v1/handlers_operations.go @@ -11,6 +11,7 @@ import ( "github.com/gorilla/mux" "github.com/wild-cloud/wild-central/daemon/internal/operations" + "github.com/wild-cloud/wild-central/daemon/internal/tools" ) // OperationGet returns operation status @@ -110,7 +111,7 @@ func (api *API) OperationStream(w http.ResponseWriter, r *http.Request) { } // Check if operation is already completed - statusFile := filepath.Join(api.dataDir, "instances", instanceName, "operations", opID+".json") + statusFile := filepath.Join(tools.GetInstanceOperationsPath(api.dataDir, instanceName), opID+".json") isCompleted := false if data, err := os.ReadFile(statusFile); err == nil { var op map[string]interface{} @@ -122,7 +123,7 @@ func (api *API) OperationStream(w http.ResponseWriter, r *http.Request) { } // Send existing log file content first (if exists) - logPath := filepath.Join(api.dataDir, "instances", instanceName, "operations", opID, "output.log") + logPath := filepath.Join(tools.GetInstanceOperationsPath(api.dataDir, instanceName), opID, "output.log") if _, err := os.Stat(logPath); err == nil { file, err := os.Open(logPath) if err == nil { diff --git a/internal/api/v1/handlers_services.go b/internal/api/v1/handlers_services.go index 551bd1d..6d0d2bc 100644 --- a/internal/api/v1/handlers_services.go +++ b/internal/api/v1/handlers_services.go @@ -5,7 +5,6 @@ import ( "fmt" "net/http" "os" - "path/filepath" "strings" "github.com/gorilla/mux" @@ -14,6 +13,7 @@ import ( "github.com/wild-cloud/wild-central/daemon/internal/contracts" "github.com/wild-cloud/wild-central/daemon/internal/operations" "github.com/wild-cloud/wild-central/daemon/internal/services" + "github.com/wild-cloud/wild-central/daemon/internal/tools" ) // ServicesList lists all base services @@ -297,7 +297,7 @@ func (api *API) ServicesGetInstanceConfig(w http.ResponseWriter, r *http.Request } // Load instance config as map for dynamic path extraction - configPath := filepath.Join(api.dataDir, "instances", instanceName, "config.yaml") + configPath := tools.GetInstanceConfigPath(api.dataDir, instanceName) configData, err := os.ReadFile(configPath) if err != nil { respondError(w, http.StatusInternalServerError, fmt.Sprintf("Failed to read instance config: %v", err)) diff --git a/internal/api/v1/handlers_utilities.go b/internal/api/v1/handlers_utilities.go index f06cc2c..8ca9c56 100644 --- a/internal/api/v1/handlers_utilities.go +++ b/internal/api/v1/handlers_utilities.go @@ -4,9 +4,9 @@ import ( "encoding/json" "fmt" "net/http" - "path/filepath" "github.com/gorilla/mux" + "github.com/wild-cloud/wild-central/daemon/internal/tools" "github.com/wild-cloud/wild-central/daemon/internal/utilities" ) @@ -36,7 +36,7 @@ func (api *API) InstanceUtilitiesHealth(w http.ResponseWriter, r *http.Request) } // Get kubeconfig path for this instance - kubeconfigPath := filepath.Join(api.dataDir, "instances", instanceName, "kubeconfig") + kubeconfigPath := tools.GetKubeconfigPath(api.dataDir, instanceName) status, err := utilities.GetClusterHealth(kubeconfigPath) if err != nil { @@ -62,7 +62,7 @@ func (api *API) UtilitiesDashboardToken(w http.ResponseWriter, r *http.Request) } // Get kubeconfig path for the instance - kubeconfigPath := filepath.Join(api.dataDir, "instances", instanceName, "kubeconfig") + kubeconfigPath := tools.GetKubeconfigPath(api.dataDir, instanceName) token, err := utilities.GetDashboardToken(kubeconfigPath) if err != nil { diff --git a/internal/apps/apps.go b/internal/apps/apps.go index 358e0f0..d5ead47 100644 --- a/internal/apps/apps.go +++ b/internal/apps/apps.go @@ -115,7 +115,7 @@ func (m *Manager) Get(appName string) (*App, error) { // ListDeployed lists deployed apps for an instance func (m *Manager) ListDeployed(instanceName string) ([]DeployedApp, error) { kubeconfigPath := tools.GetKubeconfigPath(m.dataDir, instanceName) - instancePath := filepath.Join(m.dataDir, "instances", instanceName) + instancePath := tools.GetInstancePath(m.dataDir, instanceName) appsDir := filepath.Join(instancePath, "apps") apps := []DeployedApp{} @@ -190,9 +190,9 @@ func (m *Manager) Add(instanceName, appName string, config map[string]string) er return fmt.Errorf("app %s not found at %s", appName, manifestPath) } - instancePath := filepath.Join(m.dataDir, "instances", instanceName) - configFile := filepath.Join(instancePath, "config.yaml") - secretsFile := filepath.Join(instancePath, "secrets.yaml") + instancePath := tools.GetInstancePath(m.dataDir, instanceName) + configFile := tools.GetInstanceConfigPath(m.dataDir, instanceName) + secretsFile := tools.GetInstanceSecretsPath(m.dataDir, instanceName) appDestDir := filepath.Join(instancePath, "apps", appName) // Check instance config exists @@ -306,8 +306,8 @@ func (m *Manager) Add(instanceName, appName string, config map[string]string) er // Deploy deploys an app to the cluster func (m *Manager) Deploy(instanceName, appName string) error { kubeconfigPath := tools.GetKubeconfigPath(m.dataDir, instanceName) - instancePath := filepath.Join(m.dataDir, "instances", instanceName) - secretsFile := filepath.Join(instancePath, "secrets.yaml") + instancePath := tools.GetInstancePath(m.dataDir, instanceName) + secretsFile := tools.GetInstanceSecretsPath(m.dataDir, instanceName) // Get compiled app manifests from instance directory appDir := filepath.Join(instancePath, "apps", appName) @@ -369,9 +369,9 @@ func (m *Manager) Deploy(instanceName, appName string) error { // Delete removes an app from the cluster and configuration func (m *Manager) Delete(instanceName, appName string) error { kubeconfigPath := tools.GetKubeconfigPath(m.dataDir, instanceName) - instancePath := filepath.Join(m.dataDir, "instances", instanceName) - configFile := filepath.Join(instancePath, "config.yaml") - secretsFile := filepath.Join(instancePath, "secrets.yaml") + instancePath := tools.GetInstancePath(m.dataDir, instanceName) + configFile := tools.GetInstanceConfigPath(m.dataDir, instanceName) + secretsFile := tools.GetInstanceSecretsPath(m.dataDir, instanceName) // Get compiled app manifests from instance directory appDir := filepath.Join(instancePath, "apps", appName) @@ -425,7 +425,7 @@ func (m *Manager) Delete(instanceName, appName string) error { // GetStatus returns the status of a deployed app func (m *Manager) GetStatus(instanceName, appName string) (*DeployedApp, error) { kubeconfigPath := tools.GetKubeconfigPath(m.dataDir, instanceName) - instancePath := filepath.Join(m.dataDir, "instances", instanceName) + instancePath := tools.GetInstancePath(m.dataDir, instanceName) appDir := filepath.Join(instancePath, "apps", appName) app := &DeployedApp{ diff --git a/internal/backup/backup.go b/internal/backup/backup.go index 2fd150c..62a13e5 100644 --- a/internal/backup/backup.go +++ b/internal/backup/backup.go @@ -46,7 +46,7 @@ func NewManager(dataDir string) *Manager { // GetBackupDir returns the backup directory for an instance func (m *Manager) GetBackupDir(instanceName string) string { - return filepath.Join(m.dataDir, "instances", instanceName, "backups") + return tools.GetInstanceBackupsPath(m.dataDir, instanceName) } // GetStagingDir returns the staging directory for backups diff --git a/internal/cluster/cluster.go b/internal/cluster/cluster.go index 7653d7a..b7a9dcf 100644 --- a/internal/cluster/cluster.go +++ b/internal/cluster/cluster.go @@ -48,7 +48,7 @@ type ClusterStatus struct { // GetTalosDir returns the talos directory for an instance func (m *Manager) GetTalosDir(instanceName string) string { - return filepath.Join(m.dataDir, "instances", instanceName, "talos") + return tools.GetInstanceTalosPath(m.dataDir, instanceName) } // GetGeneratedDir returns the generated config directory @@ -99,8 +99,7 @@ func (m *Manager) GenerateConfig(instanceName string, config *ClusterConfig) err // Bootstrap bootstraps the cluster on the specified node func (m *Manager) Bootstrap(instanceName, nodeName string) error { // Get node configuration to find the target IP - instancePath := filepath.Join(m.dataDir, "instances", instanceName) - configPath := filepath.Join(instancePath, "config.yaml") + configPath := tools.GetInstanceConfigPath(m.dataDir, instanceName) yq := tools.NewYQ() @@ -183,8 +182,7 @@ func (m *Manager) retrieveKubeconfigFromCluster(instanceName, nodeIP string, tim // RegenerateKubeconfig regenerates the kubeconfig by retrieving it from the cluster func (m *Manager) RegenerateKubeconfig(instanceName string) error { - instancePath := filepath.Join(m.dataDir, "instances", instanceName) - configPath := filepath.Join(instancePath, "config.yaml") + configPath := tools.GetInstanceConfigPath(m.dataDir, instanceName) yq := tools.NewYQ() @@ -206,8 +204,7 @@ func (m *Manager) RegenerateKubeconfig(instanceName string) error { // ConfigureEndpoints updates talosconfig to use VIP and retrieves kubeconfig func (m *Manager) ConfigureEndpoints(instanceName string, includeNodes bool) error { - instancePath := filepath.Join(m.dataDir, "instances", instanceName) - configPath := filepath.Join(instancePath, "config.yaml") + configPath := tools.GetInstanceConfigPath(m.dataDir, instanceName) talosconfigPath := tools.GetTalosconfigPath(m.dataDir, instanceName) yq := tools.NewYQ() diff --git a/internal/config/manager.go b/internal/config/manager.go index 6609afc..1721d9b 100644 --- a/internal/config/manager.go +++ b/internal/config/manager.go @@ -152,16 +152,19 @@ func (m *Manager) CopyConfig(srcPath, dstPath string) error { } // GetInstanceConfigPath returns the path to an instance's config file +// Deprecated: Use tools.GetInstanceConfigPath instead func GetInstanceConfigPath(dataDir, instanceName string) string { - return filepath.Join(dataDir, "instances", instanceName, "config.yaml") + return tools.GetInstanceConfigPath(dataDir, instanceName) } // GetInstanceSecretsPath returns the path to an instance's secrets file +// Deprecated: Use tools.GetInstanceSecretsPath instead func GetInstanceSecretsPath(dataDir, instanceName string) string { - return filepath.Join(dataDir, "instances", instanceName, "secrets.yaml") + return tools.GetInstanceSecretsPath(dataDir, instanceName) } // GetInstancePath returns the path to an instance directory +// Deprecated: Use tools.GetInstancePath instead func GetInstancePath(dataDir, instanceName string) string { - return filepath.Join(dataDir, "instances", instanceName) + return tools.GetInstancePath(dataDir, instanceName) } diff --git a/internal/context/context.go b/internal/context/context.go index 3dc5023..3c47b67 100644 --- a/internal/context/context.go +++ b/internal/context/context.go @@ -6,6 +6,7 @@ import ( "strings" "github.com/wild-cloud/wild-central/daemon/internal/storage" + "github.com/wild-cloud/wild-central/daemon/internal/tools" ) // Manager handles current instance context tracking @@ -53,7 +54,7 @@ func (m *Manager) SetCurrentContext(instanceName string) error { } // Verify instance exists - instancePath := filepath.Join(m.dataDir, "instances", instanceName) + instancePath := tools.GetInstancePath(m.dataDir, instanceName) if !storage.FileExists(instancePath) { return fmt.Errorf("instance %s does not exist", instanceName) } @@ -101,7 +102,7 @@ func (m *Manager) ValidateContext() error { return err } - instancePath := filepath.Join(m.dataDir, "instances", contextName) + instancePath := tools.GetInstancePath(m.dataDir, contextName) if !storage.FileExists(instancePath) { return fmt.Errorf("current context %s points to non-existent instance", contextName) } @@ -116,7 +117,7 @@ func (m *Manager) GetCurrentInstancePath() (string, error) { return "", err } - return filepath.Join(m.dataDir, "instances", contextName), nil + return tools.GetInstancePath(m.dataDir, contextName), nil } // GetCurrentInstanceConfigPath returns the path to the current instance's config file diff --git a/internal/discovery/discovery.go b/internal/discovery/discovery.go index a3580f9..f2e7f02 100644 --- a/internal/discovery/discovery.go +++ b/internal/discovery/discovery.go @@ -24,7 +24,7 @@ type Manager struct { // NewManager creates a new discovery manager func NewManager(dataDir string, instanceName string) *Manager { // Get talosconfig path for the instance - talosconfigPath := filepath.Join(dataDir, "instances", instanceName, "setup", "cluster-nodes", "generated", "talosconfig") + talosconfigPath := tools.GetTalosconfigPath(dataDir, instanceName) return &Manager{ dataDir: dataDir, @@ -53,7 +53,7 @@ type DiscoveryStatus struct { // GetDiscoveryDir returns the discovery directory for an instance func (m *Manager) GetDiscoveryDir(instanceName string) string { - return filepath.Join(m.dataDir, "instances", instanceName, "discovery") + return tools.GetInstanceDiscoveryPath(m.dataDir, instanceName) } // GetDiscoveryStatusPath returns the path to discovery status file diff --git a/internal/instance/instance.go b/internal/instance/instance.go index 6e55603..d0cc886 100644 --- a/internal/instance/instance.go +++ b/internal/instance/instance.go @@ -9,6 +9,7 @@ import ( "github.com/wild-cloud/wild-central/daemon/internal/context" "github.com/wild-cloud/wild-central/daemon/internal/secrets" "github.com/wild-cloud/wild-central/daemon/internal/storage" + "github.com/wild-cloud/wild-central/daemon/internal/tools" ) // Manager handles instance lifecycle operations @@ -38,18 +39,21 @@ type Instance struct { } // GetInstancePath returns the path to an instance directory +// Deprecated: Use tools.GetInstancePath instead func (m *Manager) GetInstancePath(name string) string { - return filepath.Join(m.dataDir, "instances", name) + return tools.GetInstancePath(m.dataDir, name) } // GetInstanceConfigPath returns the path to an instance's config file +// Deprecated: Use tools.GetInstanceConfigPath instead func (m *Manager) GetInstanceConfigPath(name string) string { - return filepath.Join(m.GetInstancePath(name), "config.yaml") + return tools.GetInstanceConfigPath(m.dataDir, name) } // GetInstanceSecretsPath returns the path to an instance's secrets file +// Deprecated: Use tools.GetInstanceSecretsPath instead func (m *Manager) GetInstanceSecretsPath(name string) string { - return filepath.Join(m.GetInstancePath(name), "secrets.yaml") + return tools.GetInstanceSecretsPath(m.dataDir, name) } // InstanceExists checks if an instance exists @@ -71,7 +75,7 @@ func (m *Manager) CreateInstance(name string) error { } // Acquire lock for instance creation - lockPath := filepath.Join(m.dataDir, "instances", ".lock") + lockPath := tools.GetInstancesLockPath(m.dataDir) return storage.WithLock(lockPath, func() error { // Create instance directory if err := storage.EnsureDir(instancePath, 0755); err != nil { @@ -123,7 +127,7 @@ func (m *Manager) DeleteInstance(name string) error { } // Acquire lock for instance deletion - lockPath := filepath.Join(m.dataDir, "instances", ".lock") + lockPath := tools.GetInstancesLockPath(m.dataDir) return storage.WithLock(lockPath, func() error { // Remove instance directory if err := os.RemoveAll(instancePath); err != nil { @@ -136,7 +140,7 @@ func (m *Manager) DeleteInstance(name string) error { // ListInstances returns a list of all instance names func (m *Manager) ListInstances() ([]string, error) { - instancesDir := filepath.Join(m.dataDir, "instances") + instancesDir := tools.GetInstancesPath(m.dataDir) // Ensure instances directory exists if !storage.FileExists(instancesDir) { diff --git a/internal/node/node.go b/internal/node/node.go index c47d444..6ac763a 100644 --- a/internal/node/node.go +++ b/internal/node/node.go @@ -59,7 +59,7 @@ type ApplyOptions struct { // GetInstancePath returns the path to an instance's nodes directory func (m *Manager) GetInstancePath(instanceName string) string { - return filepath.Join(m.dataDir, "instances", instanceName) + return tools.GetInstancePath(m.dataDir, instanceName) } // List returns all nodes for an instance diff --git a/internal/operations/operations.go b/internal/operations/operations.go index 1728656..93496f8 100644 --- a/internal/operations/operations.go +++ b/internal/operations/operations.go @@ -8,6 +8,7 @@ import ( "time" "github.com/wild-cloud/wild-central/daemon/internal/storage" + "github.com/wild-cloud/wild-central/daemon/internal/tools" ) // Manager handles async operation tracking @@ -38,7 +39,7 @@ type Operation struct { // GetOperationsDir returns the operations directory for an instance func (m *Manager) GetOperationsDir(instanceName string) string { - return filepath.Join(m.dataDir, "instances", instanceName, "operations") + return tools.GetInstanceOperationsPath(m.dataDir, instanceName) } // generateID generates a unique operation ID diff --git a/internal/pxe/pxe.go b/internal/pxe/pxe.go index 4e9aa97..b96f1ad 100644 --- a/internal/pxe/pxe.go +++ b/internal/pxe/pxe.go @@ -9,6 +9,7 @@ import ( "path/filepath" "github.com/wild-cloud/wild-central/daemon/internal/storage" + "github.com/wild-cloud/wild-central/daemon/internal/tools" ) // Manager handles PXE boot asset management @@ -35,7 +36,7 @@ type Asset struct { // GetPXEDir returns the PXE directory for an instance func (m *Manager) GetPXEDir(instanceName string) string { - return filepath.Join(m.dataDir, "instances", instanceName, "pxe") + return tools.GetInstancePXEPath(m.dataDir, instanceName) } // ListAssets returns available PXE assets for an instance diff --git a/internal/services/config.go b/internal/services/config.go index 473bba7..5aca7da 100644 --- a/internal/services/config.go +++ b/internal/services/config.go @@ -3,7 +3,6 @@ package services import ( "fmt" "os" - "path/filepath" "strings" "gopkg.in/yaml.v3" @@ -11,6 +10,7 @@ import ( "github.com/wild-cloud/wild-central/daemon/internal/contracts" "github.com/wild-cloud/wild-central/daemon/internal/operations" "github.com/wild-cloud/wild-central/daemon/internal/storage" + "github.com/wild-cloud/wild-central/daemon/internal/tools" ) // UpdateConfig updates service configuration and optionally redeploys @@ -27,8 +27,7 @@ func (m *Manager) UpdateConfig(instanceName, serviceName string, update contract } // 2. Load instance config - instanceDir := filepath.Join(m.dataDir, "instances", instanceName) - configPath := filepath.Join(instanceDir, "config.yaml") + configPath := tools.GetInstanceConfigPath(m.dataDir, instanceName) if !storage.FileExists(configPath) { return nil, fmt.Errorf("config file not found for instance %s", instanceName) diff --git a/internal/services/services.go b/internal/services/services.go index 8e1e413..883eedb 100644 --- a/internal/services/services.go +++ b/internal/services/services.go @@ -264,7 +264,7 @@ func (m *Manager) Delete(instanceName, serviceName string) error { } // Get manifests file from embedded setup or instance directory - instanceServiceDir := filepath.Join(m.dataDir, "instances", instanceName, "setup", "cluster-services", serviceName) + instanceServiceDir := filepath.Join(tools.GetInstancePath(m.dataDir, instanceName), "setup", "cluster-services", serviceName) manifestsFile := filepath.Join(instanceServiceDir, "manifests.yaml") if !storage.FileExists(manifestsFile) { @@ -332,7 +332,7 @@ func (m *Manager) Fetch(instanceName, serviceName string) error { } // 2. Create instance service directory - instanceDir := filepath.Join(m.dataDir, "instances", instanceName, + instanceDir := filepath.Join(tools.GetInstancePath(m.dataDir, instanceName), "setup", "cluster-services", serviceName) if err := os.MkdirAll(instanceDir, 0755); err != nil { return fmt.Errorf("failed to create service directory: %w", err) @@ -376,7 +376,7 @@ func (m *Manager) Fetch(instanceName, serviceName string) error { // serviceFilesExist checks if service files exist in the instance func (m *Manager) serviceFilesExist(instanceName, serviceName string) bool { - serviceDir := filepath.Join(m.dataDir, "instances", instanceName, + serviceDir := filepath.Join(tools.GetInstancePath(m.dataDir, instanceName), "setup", "cluster-services", serviceName) installSh := filepath.Join(serviceDir, "install.sh") return fileExists(installSh) @@ -422,7 +422,7 @@ func extractFS(fsys fs.FS, dst string) error { // Compile processes gomplate templates into final Kubernetes manifests func (m *Manager) Compile(instanceName, serviceName string) error { - instanceDir := filepath.Join(m.dataDir, "instances", instanceName) + instanceDir := tools.GetInstancePath(m.dataDir, instanceName) serviceDir := filepath.Join(instanceDir, "setup", "cluster-services", serviceName) templateDir := filepath.Join(serviceDir, "kustomize.template") outputDir := filepath.Join(serviceDir, "kustomize") @@ -500,7 +500,7 @@ func (m *Manager) Compile(instanceName, serviceName string) error { func (m *Manager) Deploy(instanceName, serviceName, opID string, broadcaster *operations.Broadcaster) error { fmt.Printf("[DEBUG] Deploy() called for service=%s instance=%s opID=%s\n", serviceName, instanceName, opID) - instanceDir := filepath.Join(m.dataDir, "instances", instanceName) + instanceDir := tools.GetInstancePath(m.dataDir, instanceName) serviceDir := filepath.Join(instanceDir, "setup", "cluster-services", serviceName) installScript := filepath.Join(serviceDir, "install.sh") @@ -596,8 +596,7 @@ func (m *Manager) validateConfig(instanceName, serviceName string) error { } // Load instance config - instanceDir := filepath.Join(m.dataDir, "instances", instanceName) - configFile := filepath.Join(instanceDir, "config.yaml") + configFile := tools.GetInstanceConfigPath(m.dataDir, instanceName) configData, err := os.ReadFile(configFile) if err != nil { diff --git a/internal/services/status.go b/internal/services/status.go index 4ebd5e1..2850786 100644 --- a/internal/services/status.go +++ b/internal/services/status.go @@ -3,7 +3,6 @@ package services import ( "fmt" "os" - "path/filepath" "time" "gopkg.in/yaml.v3" @@ -91,8 +90,7 @@ func (m *Manager) GetDetailedStatus(instanceName, serviceName string) (*contract } // 5. Load current config values - instanceDir := filepath.Join(m.dataDir, "instances", instanceName) - configPath := filepath.Join(instanceDir, "config.yaml") + configPath := tools.GetInstanceConfigPath(m.dataDir, instanceName) configValues := make(map[string]interface{}) if storage.FileExists(configPath) { diff --git a/internal/tools/context.go b/internal/tools/context.go index 293e52a..1c1e3fe 100644 --- a/internal/tools/context.go +++ b/internal/tools/context.go @@ -35,3 +35,53 @@ func GetTalosconfigPath(dataDir, instanceName string) string { func GetKubeconfigPath(dataDir, instanceName string) string { return filepath.Join(dataDir, "instances", instanceName, "kubeconfig") } + +// GetInstancePath returns the path to an instance directory +func GetInstancePath(dataDir, instanceName string) string { + return filepath.Join(dataDir, "instances", instanceName) +} + +// GetInstanceConfigPath returns the path to an instance's config file +func GetInstanceConfigPath(dataDir, instanceName string) string { + return filepath.Join(dataDir, "instances", instanceName, "config.yaml") +} + +// GetInstanceSecretsPath returns the path to an instance's secrets file +func GetInstanceSecretsPath(dataDir, instanceName string) string { + return filepath.Join(dataDir, "instances", instanceName, "secrets.yaml") +} + +// GetInstanceTalosPath returns the path to an instance's talos directory +func GetInstanceTalosPath(dataDir, instanceName string) string { + return filepath.Join(dataDir, "instances", instanceName, "talos") +} + +// GetInstancePXEPath returns the path to an instance's PXE directory +func GetInstancePXEPath(dataDir, instanceName string) string { + return filepath.Join(dataDir, "instances", instanceName, "pxe") +} + +// GetInstanceOperationsPath returns the path to an instance's operations directory +func GetInstanceOperationsPath(dataDir, instanceName string) string { + return filepath.Join(dataDir, "instances", instanceName, "operations") +} + +// GetInstanceBackupsPath returns the path to an instance's backups directory +func GetInstanceBackupsPath(dataDir, instanceName string) string { + return filepath.Join(dataDir, "instances", instanceName, "backups") +} + +// GetInstanceDiscoveryPath returns the path to an instance's discovery directory +func GetInstanceDiscoveryPath(dataDir, instanceName string) string { + return filepath.Join(dataDir, "instances", instanceName, "discovery") +} + +// GetInstancesPath returns the path to the instances directory +func GetInstancesPath(dataDir string) string { + return filepath.Join(dataDir, "instances") +} + +// GetInstancesLockPath returns the path to the instances directory lock file +func GetInstancesLockPath(dataDir string) string { + return filepath.Join(dataDir, "instances", ".lock") +}