forked from flynn/flynn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroute.go
160 lines (141 loc) · 3.88 KB
/
route.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
155
156
157
158
159
160
package main
import (
"bytes"
"encoding/pem"
"errors"
"fmt"
"io/ioutil"
"os"
"strconv"
"github.com/flynn/flynn/Godeps/_workspace/src/github.com/flynn/go-docopt"
"github.com/flynn/flynn/controller/client"
"github.com/flynn/flynn/router/types"
)
func init() {
register("route", runRoute, `
usage: flynn route
flynn route add [-t <type>] [-s <service>] [-c <tls-cert> -k <tls-key>] [--sticky] <domain>
flynn route remove <id>
Manage routes for application.
Options:
-t <type> route's type (Currently only http supported) [default: http]
-s, --service <service> service name to route domain to (defaults to APPNAME-web)
-c, --tls-cert <tls-cert> path to PEM encoded certificate for TLS, - for stdin
-k, --tls-key <tls-key> path to PEM encoded private key for TLS, - for stdin
--sticky enable cookie-based sticky routing
Commands:
With no arguments, shows a list of routes.
add adds a route to an app
remove removes a route
`)
}
func runRoute(args *docopt.Args, client *controller.Client) error {
if args.Bool["add"] {
if args.String["-t"] == "http" {
return runRouteAddHTTP(args, client)
} else {
return fmt.Errorf("Route type %s not supported.", args.String["-t"])
}
} else if args.Bool["remove"] {
return runRouteRemove(args, client)
}
routes, err := client.RouteList(mustApp())
if err != nil {
return err
}
w := tabWriter()
defer w.Flush()
var route, protocol, service string
listRec(w, "ROUTE", "SERVICE", "ID")
for _, k := range routes {
switch k.Type {
case "tcp":
protocol = "tcp"
route = strconv.Itoa(k.TCPRoute().Port)
service = k.TCPRoute().Service
case "http":
route = k.HTTPRoute().Domain
service = k.TCPRoute().Service
if k.HTTPRoute().TLSCert == "" {
protocol = "http"
} else {
protocol = "https"
}
}
listRec(w, protocol+":"+route, service, k.ID)
}
return nil
}
func runRouteAddHTTP(args *docopt.Args, client *controller.Client) error {
var tlsCert []byte
var tlsKey []byte
var routeHTTPService string
if args.String["--service"] == "" {
routeHTTPService = mustApp() + "-web"
} else {
routeHTTPService = args.String["--service"]
}
tlsCertPath := args.String["--tls-cert"]
tlsKeyPath := args.String["--tls-key"]
if tlsCertPath != "" && tlsKeyPath != "" {
var stdin []byte
var err error
if tlsCertPath == "-" || tlsKeyPath == "-" {
stdin, err = ioutil.ReadAll(os.Stdin)
if err != nil {
return fmt.Errorf("Failed to read from stdin: %s", err)
}
}
tlsCert, err = readPEM("CERTIFICATE", tlsCertPath, stdin)
if err != nil {
return fmt.Errorf("Failed to read TLS cert: %s", err)
}
tlsKey, err = readPEM("PRIVATE KEY", tlsKeyPath, stdin)
if err != nil {
return fmt.Errorf("Failed to read TLS key: %s", err)
}
} else if tlsCertPath != "" || tlsKeyPath != "" {
return errors.New("Both the TLS certificate AND private key need to be specified")
}
hr := &router.HTTPRoute{
Service: routeHTTPService,
Domain: args.String["<domain>"],
TLSCert: string(tlsCert),
TLSKey: string(tlsKey),
Sticky: args.Bool["sticky"],
}
route := hr.ToRoute()
if err := client.CreateRoute(mustApp(), route); err != nil {
return err
}
fmt.Println(route.ID)
return nil
}
func readPEM(typ string, path string, stdin []byte) ([]byte, error) {
if path == "-" {
var buf bytes.Buffer
var block *pem.Block
for {
block, stdin = pem.Decode(stdin)
if block == nil {
break
}
if block.Type == typ {
pem.Encode(&buf, block)
}
}
if buf.Len() > 0 {
return buf.Bytes(), nil
}
return nil, errors.New("No PEM blocks found in stdin")
}
return ioutil.ReadFile(path)
}
func runRouteRemove(args *docopt.Args, client *controller.Client) error {
routeID := args.String["<id>"]
if err := client.DeleteRoute(mustApp(), routeID); err != nil {
return err
}
fmt.Printf("Route %s removed.\n", routeID)
return nil
}