forked from saymedia/consul-ec2-alb
/
main.go
87 lines (68 loc) · 1.69 KB
/
main.go
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
package main
import (
"fmt"
"log"
"os"
"github.com/jessevdk/go-flags"
)
type CLIOptions struct {
ConfigFiles struct {
List []string `positional-arg-name:"config-files"`
} `positional-args:"yes" required:"yes"`
}
func main() {
options := &CLIOptions{}
_, err := flags.Parse(options)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n\n", err)
os.Exit(2)
}
err = realMain(options)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n\n", err)
os.Exit(3)
}
}
func realMain(opts *CLIOptions) error {
configFiles := opts.ConfigFiles.List
config, err := LoadConfigFiles(configFiles)
if err != nil {
return err
}
targetGroups := map[string]*TargetGroup{}
for _, tgConfig := range config.TargetGroups {
if tgConfig.TargetGroupARN == "" {
return fmt.Errorf("cannot have target group with empty ARN")
}
if tgConfig.ServiceName == "" {
return fmt.Errorf("no 'service' specified for target group %q", tgConfig.TargetGroupARN)
}
if targetGroups[tgConfig.TargetGroupARN] != nil {
return fmt.Errorf("duplicate declaration of target group %q", tgConfig.TargetGroupARN)
}
tg, err := NewTargetGroup(
tgConfig,
config.Consul,
config.AWS,
)
if err != nil {
log.Printf("skipping target group %q: %s\n", tgConfig.TargetGroupARN, err)
continue
}
targetGroups[tgConfig.TargetGroupARN] = tg
}
if len(targetGroups) == 0 {
log.Println(
"No valid target_group blocks found in config!",
)
}
// If we get down here then our config seems valid enough and so
// we'll start up a goroutine for each target group and then hang
// out waiting for a signal.
for _, tg := range targetGroups {
go tg.KeepSyncing()
}
c := make(chan struct{})
c <- struct{}{}
return nil
}