Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

4.8 KB
Raw Blame
package config

import (
	"encoding/json"
	"fmt"
	"os"
	"path/filepath"
	"sync"

	"github.com/rs/zerolog/log"
)

// HostMetadata holds additional metadata for a host
type HostMetadata struct {
	ID              string   `json:"id"`              // Host ID
	CustomURL       string   `json:"customUrl"`       // Custom URL for the host
	Description     string   `json:"description"`     // Optional description
	Tags            []string `json:"tags"`            // Optional tags for categorization
	Notes           []string `json:"notes"`           // User annotations for AI context
	CommandsEnabled *bool    `json:"commandsEnabled"` // Remote override for AI command execution (nil = use agent default)
}

func cloneHostMetadata(meta *HostMetadata) *HostMetadata {
	if meta == nil {
		return nil
	}

	clone := *meta
	if meta.Tags != nil {
		clone.Tags = append([]string{}, meta.Tags...)
	}
	if meta.Notes != nil {
		clone.Notes = append([]string{}, meta.Notes...)
	}
	if meta.CommandsEnabled != nil {
		commandsEnabled := *meta.CommandsEnabled
		clone.CommandsEnabled = &commandsEnabled
	}
	return &clone
}

// HostMetadataStore manages host metadata
type HostMetadataStore struct {
	mu       sync.RWMutex
	metadata map[string]*HostMetadata // keyed by host ID
	dataPath string
	fs       FileSystem
}

// NewHostMetadataStore creates a new host metadata store
func NewHostMetadataStore(dataPath string, fs FileSystem) *HostMetadataStore {
	store := &HostMetadataStore{
		metadata: make(map[string]*HostMetadata),
		dataPath: dataPath,
		fs:       fs,
	}

	if store.fs == nil {
		store.fs = defaultFileSystem{}
	}

	// Load existing metadata
	if err := store.Load(); err != nil {
		log.Warn().Err(err).Msg("Failed to load host metadata")
	}

	return store
}

// ... Get/Set/Delete/ReplaceAll ... (unchanged except struct definition)

// Load reads metadata from disk
func (s *HostMetadataStore) Load() error {
	filePath := filepath.Join(s.dataPath, "host_metadata.json")

	log.Debug().Str("path", filePath).Msg("Loading host metadata from disk")

	// Use configured FS
	data, err := readLimitedRegularFileFS(s.fs, filePath, maxHostMetadataFileSizeBytes)
	if err != nil {
		if os.IsNotExist(err) {
			// File doesn't exist yet, not an error
			log.Debug().Str("path", filePath).Msg("Host metadata file does not exist yet")
			return nil
		}
		return fmt.Errorf("failed to read metadata file: %w", err)
	}

	s.mu.Lock()
	defer s.mu.Unlock()

	if err := json.Unmarshal(data, &s.metadata); err != nil {
		return fmt.Errorf("failed to unmarshal metadata: %w", err)
	}

	log.Info().
		Int("hostCount", len(s.metadata)).
		Msg("Loaded host metadata")

	return nil
}

// save writes metadata to disk (must be called with lock held)
func (s *HostMetadataStore) save() error {
	filePath := filepath.Join(s.dataPath, "host_metadata.json")

	log.Debug().Str("path", filePath).Msg("Saving host metadata to disk")

	data, err := json.Marshal(s.metadata)
	if err != nil {
		return fmt.Errorf("failed to marshal metadata: %w", err)
	}

	if err := persistMetadata(s.fs, s.dataPath, "host_metadata.json", data); err != nil {
		return err
	}

	log.Debug().Str("path", filePath).Int("hosts", len(s.metadata)).Msg("Host metadata saved successfully")

	return nil
}

// Get retrieves metadata for a host
func (s *HostMetadataStore) Get(hostID string) *HostMetadata {
	s.mu.RLock()
	defer s.mu.RUnlock()

	if meta, exists := s.metadata[hostID]; exists {
		return cloneHostMetadata(meta)
	}
	return nil
}

// GetAll retrieves all host metadata
func (s *HostMetadataStore) GetAll() map[string]*HostMetadata {
	s.mu.RLock()
	defer s.mu.RUnlock()

	// Return a copy to prevent external modifications
	result := make(map[string]*HostMetadata)
	for k, v := range s.metadata {
		result[k] = cloneHostMetadata(v)
	}
	return result
}

// Set updates or creates metadata for a host
func (s *HostMetadataStore) Set(hostID string, meta *HostMetadata) error {
	s.mu.Lock()
	defer s.mu.Unlock()

	if meta == nil {
		return fmt.Errorf("metadata cannot be nil")
	}

	clone := cloneHostMetadata(meta)
	clone.ID = hostID
	s.metadata[hostID] = clone

	// Save to disk
	return s.save()
}

// Delete removes metadata for a host
func (s *HostMetadataStore) Delete(hostID string) error {
	s.mu.Lock()
	defer s.mu.Unlock()

	delete(s.metadata, hostID)

	// Save to disk
	return s.save()
}

// ReplaceAll replaces all metadata entries and persists them to disk.
func (s *HostMetadataStore) ReplaceAll(metadata map[string]*HostMetadata) error {
	s.mu.Lock()
	defer s.mu.Unlock()

	s.metadata = make(map[string]*HostMetadata)

	for hostID, meta := range metadata {
		if meta == nil {
			continue
		}

		clone := cloneHostMetadata(meta)
		clone.ID = hostID
		// Ensure slice copy is not nil to allow JSON marshalling of empty tags
		if clone.Tags == nil {
			clone.Tags = []string{}
		}
		s.metadata[hostID] = clone
	}

	return s.save()
}