-
Notifications
You must be signed in to change notification settings - Fork 35
/
helpers.go
105 lines (95 loc) · 2.5 KB
/
helpers.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net"
"time"
"github.com/codegangsta/cli"
"github.com/ehazlett/element"
"github.com/ehazlett/stellar"
"github.com/sirupsen/logrus"
)
func getIP(ctx *cli.Context) string {
ip := "127.0.0.1"
devName := ctx.String("nic")
ifaces, err := net.Interfaces()
if err != nil {
logrus.Warnf("unable to detect network interfaces")
return ip
}
for _, i := range ifaces {
if devName == "" || i.Name == devName {
a := getInterfaceIP(i)
if a != "" {
return a
}
}
}
logrus.Warnf("unable to find interface %s", devName)
return ip
}
func getInterfaceIP(iface net.Interface) string {
addrs, err := iface.Addrs()
if err != nil {
return ""
}
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
return ip.To4().String()
}
return ""
}
func defaultConfig(ctx *cli.Context) (*stellar.Config, error) {
nodeID := ctx.String("node-id")
ip := getIP(ctx)
peers := ctx.StringSlice("peer")
agentConfig := &element.Config{
ConnectionType: "local",
ClusterAddress: fmt.Sprintf("%s:%d", ip, 7946),
AdvertiseAddress: fmt.Sprintf("%s:%d", ip, 7946),
Peers: peers,
}
_, subnet, err := net.ParseCIDR("172.16.0.0/12")
if err != nil {
return nil, err
}
return &stellar.Config{
NodeID: nodeID,
GRPCAddress: fmt.Sprintf("%s:%d", ip, 9000),
AgentConfig: agentConfig,
ContainerdAddr: "/run/containerd/containerd.sock",
Namespace: ctx.String("namespace"),
Subnet: subnet,
DataDir: "/var/lib/stellar",
StateDir: "/run/stellar",
Bridge: "stellar0",
UpstreamDNSAddr: "8.8.8.8:53",
ProxyHTTPPort: 80,
ProxyHTTPSPort: 443,
ProxyTLSEmail: "",
ProxyHealthcheckInterval: time.Second * 5,
GatewayAddress: fmt.Sprintf("%s:%d", ip, 9001),
EventsAddress: fmt.Sprintf("%s:%d", ip, 4222),
EventsHTTPAddress: fmt.Sprintf("%s:%d", ip, 4322),
EventsClusterAddress: fmt.Sprintf("%s:%d", ip, 5222),
CNIBinPaths: []string{"/opt/containerd/bin", "/opt/cni/bin"},
}, nil
}
func loadConfigFromFile(path string) (*stellar.Config, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
var cfg *stellar.Config
if err := json.Unmarshal(data, &cfg); err != nil {
return nil, err
}
return cfg, nil
}