/
consul-join.go
137 lines (108 loc) · 2.75 KB
/
consul-join.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"fmt"
"net"
"os"
"os/exec"
"strconv"
"syscall"
)
const consul = "consul"
func joinIPArg(ip string) string {
return fmt.Sprintf("-join=%s", ip)
}
func joinIPArgs(ips []string) []string {
acc := []string{}
for _, ip := range ips {
acc = append(acc, joinIPArg(ip))
}
return acc
}
type Resolver interface {
LookupHost(host string) ([]string, error)
}
type resolver struct{}
func (r resolver) LookupHost(host string) ([]string, error) {
return net.LookupHost(host)
}
func resolveARecords(res Resolver, domain string) []string {
ips, err := res.LookupHost(domain)
if err == nil {
return ips
} else {
errorStr := fmt.Sprintf("error resolving %s\n%s", domain, err.Error())
fmt.Fprint(os.Stderr, errorStr)
return []string{}
}
}
func bootstrapExpectFromLookup(s string, exists bool) int64 {
if exists {
parsed, err := strconv.ParseInt(s, 10, 64)
if err == nil {
return parsed
}
}
return 3
}
func fullCommandArgs(joinArgs []string, node string, advertise string, bootstrapExpect int64, osArgs []string) []string {
commandArgs := []string{consul, "agent"}
if node != "" {
commandArgs = append(commandArgs, fmt.Sprintf("-node=%s", node))
}
if advertise != "" {
commandArgs = append(commandArgs, fmt.Sprintf("-advertise=%s", advertise))
}
for _, joinArg := range joinArgs {
commandArgs = append(commandArgs, joinArg)
}
for index, arg := range osArgs {
if index != 0 {
commandArgs = append(commandArgs, arg)
}
if arg == "-server" {
commandArgs = append(commandArgs, fmt.Sprintf("-bootstrap-expect=%d", bootstrapExpect))
}
}
return commandArgs
}
func runCommand(argv []string, debug bool) {
fullCommandPath, pathErr := exec.LookPath(consul)
if pathErr != nil {
fmt.Fprint(os.Stderr, pathErr.Error())
syscall.Exit(1)
}
if debug {
fmt.Println(fullCommandPath, argv)
syscall.Exit(0)
} else {
envv := os.Environ()
execErr := syscall.Exec(fullCommandPath, argv, envv)
if execErr != nil {
fmt.Fprint(os.Stderr, execErr.Error())
syscall.Exit(1)
}
}
}
func main() {
joinIP, joinIPExists := os.LookupEnv("JOINIP")
joinDNS, joinDNSExists := os.LookupEnv("JOINDNS")
bootstrapExpect := bootstrapExpectFromLookup(os.LookupEnv("BOOTSTRAP_EXPECT"))
advertise, _ := os.LookupEnv("ADVERTISE")
node, _ := os.LookupEnv("NODE")
_, debug := os.LookupEnv("DEBUG")
res := resolver{}
var args []string
if joinIPExists && joinDNSExists {
fmt.Fprint(os.Stderr, "cannot have both JOINIP and JOINDNS together")
syscall.Exit(1)
}
if joinIPExists {
args = []string{joinIPArg(joinIP)}
} else if joinDNSExists {
args = joinIPArgs(resolveARecords(res, joinDNS))
} else {
args = []string{}
}
runArgs := fullCommandArgs(args, node, advertise, bootstrapExpect, os.Args)
runCommand(runArgs, debug)
}