~egtann/inv2config

ref: f27c93b9252a8eefb0aba01952b02a46e6458677 inv2config/main.go -rw-r--r-- 2.6 KiB
f27c93b9 — Evan Tann rename module to inv2config, remove up dependency 3 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main

import (
	"bytes"
	"encoding/json"
	"flag"
	"fmt"
	"os"
	"sort"
	"strings"
)

func main() {
	if err := run(); err != nil {
		fmt.Fprintln(os.Stderr, fmt.Sprintf("\nerror: %v", err))
		os.Exit(1)
	}
}

func run() error {
	// Read an inventory file and an SRP config file
	var (
		invFile = flag.String("i", "inventory.json", "inventory file")
		srpFile = flag.String("c", "config_service.json", "srp config file")
	)
	flag.Parse()

	conf, err := parseSRPConfig(*srpFile)
	if err != nil {
		return fmt.Errorf("parse srp config: %w", err)
	}
	inventory, err := parseInventory(*invFile)
	if err != nil {
		return fmt.Errorf("parse inventory: %w", err)
	}

	// Convert our inventory into a data structure for fast lookups
	serviceToIPs := map[string][]string{}
	for ip, serviceNames := range inventory {
		for _, name := range serviceNames {
			serviceToIPs[name] = append(serviceToIPs[name], ip)
		}
	}

	// Update ip:port combos for each backend
	missingServices := serviceMap{}
	for uri, backend := range conf.Services {
		service := backend.Service
		if service == "" {
			continue
		}
		ips, ok := serviceToIPs[service]
		if !ok {
			missingServices[service] = struct{}{}
			continue
		}
		ips = append([]string{}, ips...)
		if backend.Port > 0 {
			for i, ip := range ips {
				ips[i] = fmt.Sprintf("%s:%d", ip, backend.Port)
			}
		}
		sort.Strings(ips)
		backend.Backends = append([]string{}, ips...)
		conf.Services[uri] = backend
	}
	if len(missingServices) > 0 {
		return fmt.Errorf("services undefined in inventory: %v",
			missingServices)
	}

	buf := &bytes.Buffer{}
	enc := json.NewEncoder(buf)
	enc.SetIndent("", "\t")
	if err := enc.Encode(conf); err != nil {
		return fmt.Errorf("encode config: %w", err)
	}
	fmt.Print(string(buf.Bytes()))
	return nil
}

func parseSRPConfig(filename string) (*srpConfig, error) {
	fi, err := os.Open(filename)
	if err != nil {
		return nil, err
	}
	defer func() { _ = fi.Close() }()

	conf := &srpConfig{}
	if err = json.NewDecoder(fi).Decode(conf); err != nil {
		return nil, fmt.Errorf("decode: %w", err)
	}
	return conf, nil
}

type inventory map[string][]string

func parseInventory(filename string) (inventory, error) {
	fi, err := os.Open(filename)
	if err != nil {
		return nil, err
	}
	defer func() { _ = fi.Close() }()

	inv := inventory{}
	if err = json.NewDecoder(fi).Decode(&inv); err != nil {
		return nil, fmt.Errorf("decode: %w", err)
	}
	return inv, nil
}

type serviceMap map[string]struct{}

func (s serviceMap) String() string {
	out := make([]string, 0, len(s))
	for srv := range s {
		out = append(out, srv)
	}
	return strings.Join(out, ", ")
}