This repository has been archived by the owner on Feb 21, 2020. It is now read-only.
/
main.go
154 lines (141 loc) · 4.22 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"os"
"os/signal"
"syscall"
log "github.com/Sirupsen/logrus"
"github.com/TrilliumIT/docker-drouter/drouter"
"github.com/urfave/cli"
)
const (
version = "0.2"
)
func main() {
var flagDebug = cli.BoolFlag{
Name: "debug, d",
Usage: "Enable debugging.",
}
var flagIPOffset = cli.IntFlag{
Name: "ip-offset",
Value: 0,
Usage: "",
}
var flagDRouterInstance = cli.StringFlag{
Name: "drouter-instance",
Value: "drouter",
Usage: "String to tell this drouter instance which networks to connect to. (must match 'docker network create -o drouter=<instance>')",
}
var flagNoAggressive = cli.BoolFlag{
Name: "no-aggressive",
Usage: "Set false to make drouter only connect to docker networks with local containers.",
}
var flagHostShortcut = cli.BoolFlag{
Name: "host-shortcut",
Usage: "Set true to insert routes in the host destined for docker networks pointing to drouter over a host<->drouter p2p link.",
}
var flagContainerGateway = cli.BoolFlag{
Name: "container-gateway",
Usage: "Set true to set the container gateway to drouter.",
}
var flagHostGateway = cli.BoolFlag{
Name: "host-gateway",
Usage: "Set true to insert a default route on drouter pointing to the host over the host<->drouter p2p link. (implies --host-shortcut)",
}
var flagMasquerade = cli.BoolFlag{
Name: "masquerade",
Usage: "Set true to masquerade container traffic to it's host's interface IP address.",
}
var flagP2PNet = cli.StringFlag{
Name: "p2p-net",
Value: "172.29.255.252/30",
Usage: "Use this option to customize the network used for the host<->drouter p2p link.",
}
var flagStaticRoutes = cli.StringSliceFlag{
Name: "static-route",
Usage: "Specify one or many CIDR addresses that will be installed as routes via drouter to all containers.",
}
var flagTransitNet = cli.StringFlag{
Name: "transit-net",
Usage: "Set a transit network for drouter to always connect to. Network should have 'drouter' option set. If network has a gateway, and --host-gateway=false, drouter's default gateway will be through this network's gateway. (this option is required with --no-aggressive)",
}
app := cli.NewApp()
app.Name = "docker-drouter"
app.Usage = "Docker Distributed Router"
app.Version = version
app.Flags = []cli.Flag{
flagDebug,
flagIPOffset,
flagDRouterInstance,
flagNoAggressive,
flagHostShortcut,
flagContainerGateway,
flagHostGateway,
flagMasquerade,
flagP2PNet,
flagStaticRoutes,
flagTransitNet,
}
app.Action = Run
err := app.Run(os.Args)
if err != nil {
panic(err)
}
}
// Run initializes the driver
func Run(ctx *cli.Context) error {
log.SetFormatter(&log.TextFormatter{
//ForceColors: false,
//DisableColors: true,
DisableTimestamp: false,
FullTimestamp: true,
})
if ctx.Bool("debug") {
log.SetLevel(log.DebugLevel)
log.Info("Debug logging enabled")
}
if ctx.Bool("no-aggressive") {
panic("Sorry, --no-aggressive mode is not supported yet.")
}
if ctx.Bool("host-shortcut") {
panic("Sorry, --host-shortcut mode is not supported yet.")
}
if ctx.Bool("container-gateway") {
panic("Sorry, --container-gateway mode is not supported yet.")
}
if ctx.Bool("host-gateway") {
panic("Sorry, --host-gateway mode is not supported yet.")
}
if ctx.Bool("masquerade") {
panic("Sorry, --masquerade mode is not supported yet.")
}
if ctx.String("transit-net") != "" {
panic("Sorry, --transit-net is not supported yet.")
}
opts := &drouter.DistributedRouterOptions{
IPOffset: ctx.Int("ip-offset"),
Aggressive: !ctx.Bool("no-aggressive"),
HostShortcut: ctx.Bool("host-shortcut"),
ContainerGateway: ctx.Bool("container-gateway"),
HostGateway: ctx.Bool("host-gateway"),
Masquerade: ctx.Bool("masquerade"),
P2PAddr: ctx.String("p2p-net"),
StaticRoutes: ctx.StringSlice("static-route"),
TransitNet: ctx.String("transit-net"),
InstanceName: ctx.String("drouter-instance"),
}
quit := make(chan struct{})
c := make(chan os.Signal)
defer close(c)
signal.Notify(c, os.Interrupt)
signal.Notify(c, syscall.SIGTERM)
go func() {
<-c
close(quit)
}()
err := drouter.Run(opts, quit)
if err != nil {
log.WithFields(log.Fields{"Error": err}).Error("Error running drouter.")
return err
}
return nil
}