Pull-based GitOps-style Docker Compose deployer: polls a (private) Git repo, detects changed stacks and reconciles only the affected

fix: selection was based on old inventory

Signed-off-by: A. Ottr <alex@otter.foo>

+16 -8
+6 -6
main.go
··· 37 37 } 38 38 fmt.Printf("Detected host: %s\n", currentHost) 39 39 40 - assignedStacks, err := getAssignedStacks(cfg.RepoPath, currentHost) 41 - if err != nil { 42 - log.Fatalf("Failed to get assigned stacks: %v", err) 43 - } 44 - fmt.Printf("Stacks assigned to this host: %v\n", assignedStacks) 45 - 46 40 fmt.Println("Pulling git repository...") 47 41 changedStacks, err := pullAndDetectChanges(cfg.RepoPath) 48 42 if err != nil { 49 43 log.Fatalf("Failed to pull or detect changes: %v", err) 50 44 } 45 + 46 + assignedStacks, err := getAssignedStacks(cfg.RepoPath, currentHost) 47 + if err != nil { 48 + log.Fatalf("Failed to get assigned stacks: %v", err) 49 + } 50 + fmt.Printf("Stacks assigned to this host: %v\n", assignedStacks) 51 51 52 52 if len(changedStacks) == 0 { 53 53 fmt.Println("No changes detected.")
+10 -2
sync.go
··· 75 75 76 76 data, err := os.ReadFile(inventoryFile) 77 77 if err != nil { 78 - return nil, fmt.Errorf("failed to read inventory file %s: %w", inventoryFile, err) 78 + return []string{}, fmt.Errorf("failed to read inventory file %s: %w", inventoryFile, err) 79 79 } 80 80 81 81 var inv inventory 82 82 if err := yaml.Unmarshal(data, &inv); err != nil { 83 - return nil, fmt.Errorf("failed to parse inventory file %s: %w", inventoryFile, err) 83 + return []string{}, fmt.Errorf("failed to parse inventory file %s: %w", inventoryFile, err) 84 + } 85 + 86 + if inv.Hosts == nil { 87 + return []string{}, fmt.Errorf("inventory file %s has no 'hosts' key", inventoryFile) 84 88 } 85 89 86 90 stacks, exists := inv.Hosts[hostname] 87 91 if !exists { 88 92 return []string{}, nil // Host not in inventory, no stacks assigned 93 + } 94 + 95 + if stacks == nil { 96 + return []string{}, nil 89 97 } 90 98 91 99 return stacks, nil