Pull-based GitOps-style Docker Compose deployer: polls a (private) Git repo, detects changed stacks and reconciles only the affected
1package core
2
3import (
4 "fmt"
5 "os"
6 "path/filepath"
7
8 "gopkg.in/yaml.v3"
9)
10
11func DetectHost() (string, error) {
12 hostname, err := os.Hostname()
13 if err != nil {
14 return "", fmt.Errorf("failed to get hostname: %w", err)
15 }
16 return hostname, nil
17}
18
19type inventory struct {
20 Hosts map[string][]string `yaml:"hosts"`
21}
22
23func GetAssignedStacks(repoPath, hostname string) ([]string, error) {
24 inventoryFile := filepath.Join(repoPath, "inventory.yml")
25
26 data, err := os.ReadFile(inventoryFile)
27 if err != nil {
28 return []string{}, fmt.Errorf("failed to read inventory file %s: %w", inventoryFile, err)
29 }
30
31 var inv inventory
32 if err := yaml.Unmarshal(data, &inv); err != nil {
33 return []string{}, fmt.Errorf("failed to parse inventory file %s: %w", inventoryFile, err)
34 }
35
36 if inv.Hosts == nil {
37 return []string{}, fmt.Errorf("inventory file %s has no 'hosts' key", inventoryFile)
38 }
39
40 stacks, exists := inv.Hosts[hostname]
41 if !exists {
42 return []string{}, nil
43 }
44
45 if stacks == nil {
46 return []string{}, nil
47 }
48
49 return stacks, nil
50}