-
Notifications
You must be signed in to change notification settings - Fork 0
/
srv2env.go
75 lines (65 loc) · 1.71 KB
/
srv2env.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
package main
import (
"fmt"
"net"
"os"
"os/exec"
"strconv"
"strings"
"syscall"
)
func main() {
if len(os.Args) < 2 {
panic("missing required fqdn parameter")
}
if len(os.Args) < 3 {
panic("missing required command line arguments")
}
fqdn := os.Args[1]
cmd := os.Args[2]
binary, err := exec.LookPath(cmd)
if err != nil {
panic(fmt.Sprintf("failed to locate command binary %q", cmd))
}
cname, addrs, err := net.LookupSRV("", "", fqdn)
if err != nil {
if dnserr, ok := err.(*net.DNSError); ok {
fmt.Fprintln(os.Stderr, "error:", dnserr.Err)
os.Exit(1)
}
panic(fmt.Sprintf("srv lookup failed: %#+v", err))
}
s := strings.SplitN(cname, ".", 3)
prefix := strings.ToUpper(strings.Replace(s[0], "-", "_", -1))
scheme := ""
if strings.HasPrefix(s[0], "_") {
scheme = s[0][1:] + "://"
}
if strings.HasPrefix(s[1], "_") {
prefix += strings.ToUpper(strings.Replace(s[1], "-", "_", -1))
}
envvar := os.Environ()
addons := make([]string, 4*len(addrs))
eps := make([]string, len(addrs))
for i, a := range addrs {
j := i * 4
ii := strconv.Itoa(i)
port := strconv.Itoa(int(a.Port))
host := a.Target
if strings.HasSuffix(host, ".") {
host = host[:len(host)-1]
}
hostport := net.JoinHostPort(host, port)
addons[j] = prefix + "_HOST" + ii + "=" + host
addons[j+1] = prefix + "_PORT" + ii + "=" + port
addons[j+2] = prefix + "_ADDR" + ii + "=" + hostport
addons[j+3] = prefix + "_ENDPOINT" + ii + "=" + scheme + hostport
eps[i] = hostport
}
envvar = append(envvar, addons...)
envvar = append(envvar, prefix+"_ENDPOINTS="+scheme+strings.Join(eps, ","))
err = syscall.Exec(binary, os.Args[2:], envvar)
if err != nil {
panic(fmt.Sprintf("failed to exec %q: %v", binary, err))
}
}