forked from docker-flow/docker-flow-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
265 lines (249 loc) · 8.61 KB
/
server.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
"strconv"
"strings"
)
const (
DISTRIBUTED = "Distributed to all instances"
)
type Server interface {
Execute(args []string) error
ServeHTTP(w http.ResponseWriter, req *http.Request)
}
type Serve struct {
IP string `short:"i" long:"ip" default:"0.0.0.0" env:"IP" description:"IP the server listens to."`
Mode string `short:"m" long:"mode" env:"MODE" description:"If set to 'swarm', proxy will operate assuming that Docker service from v1.12+ is used."`
ListenerAddress string `short:"l" long:"listener-address" env:"LISTENER_ADDRESS" description:"The address of the Docker Flow: Swarm Listener. The address matches the name of the Swarm service (e.g. swarm-listener)"`
Port string `short:"p" long:"port" default:"8080" env:"PORT" description:"Port the server listens to."`
ServiceName string `short:"n" long:"service-name" default:"proxy" env:"SERVICE_NAME" description:"The name of the proxy service. It is used only when running in 'swarm' mode and must match the '--name' parameter used to launch the service."`
BaseReconfigure
}
var server = Serve{}
type Response struct {
Status string
Message string
ServiceName string
ServiceColor string
ServicePath []string
ServiceDomain string
ConsulTemplateFePath string
ConsulTemplateBePath string
PathType string
SkipCheck bool
Mode string
Port string
Distribute bool
}
func (m *Serve) Execute(args []string) error {
logPrintf("Starting HAProxy")
m.setConsulAddresses()
NewRun().Execute([]string{})
address := fmt.Sprintf("%s:%s", m.IP, m.Port)
recon := NewReconfigure(m.BaseReconfigure, ServiceReconfigure{})
lAddr := ""
if len(m.ListenerAddress) > 0 {
lAddr = fmt.Sprintf("http://%s:8080", m.ListenerAddress)
}
if err := recon.ReloadAllServices(
m.ConsulAddresses,
m.InstanceName,
m.Mode,
lAddr,
); err != nil {
return err
}
logPrintf(`Starting "Docker Flow: Proxy"`)
if err := httpListenAndServe(address, m); err != nil {
return err
}
return nil
}
func (m *Serve) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if !strings.EqualFold(req.URL.Path, "/v1/test") {
logPrintf("Processing request %s", req.URL)
}
switch req.URL.Path {
case "/v1/docker-flow-proxy/reconfigure":
m.reconfigure(w, req)
case "/v1/docker-flow-proxy/remove":
m.remove(w, req)
case "/v1/docker-flow-proxy/config":
m.config(w, req)
case "/v1/test", "/v2/test":
js, _ := json.Marshal(Response{Status: "OK"})
httpWriterSetContentType(w, "application/json")
if !strings.EqualFold(req.URL.Path, "/v1/test") {
logPrintf("Invoked %s", req.URL.Path)
}
w.WriteHeader(http.StatusOK)
w.Write(js)
default:
w.WriteHeader(http.StatusNotFound)
}
}
func (m *Serve) SendDistributeRequests(req *http.Request, serviceName string) (status int, err error) {
values := req.URL.Query()
values.Set("distribute", "false")
req.URL.RawQuery = values.Encode()
dns := fmt.Sprintf("tasks.%s", m.ServiceName)
failedDns := []string{}
if ips, err := lookupHost(dns); err == nil {
for i := 0; i < len(ips); i++ {
req.URL.Host = fmt.Sprintf("%s:%s", ips[i], m.Port)
client := &http.Client{}
addr := fmt.Sprintf("http://%s:%s%s?%s", ips[i], m.Port, req.URL.Path, req.URL.RawQuery)
logPrintf("Sending distribution request to %s", addr)
if resp, err := client.Get(addr); err != nil || resp.StatusCode >= 300 {
if err != nil {
logPrintf(err.Error())
}
failedDns = append(failedDns, ips[i])
}
}
} else {
return http.StatusBadRequest, fmt.Errorf("Could not perform DNS %s lookup", dns)
}
if len(failedDns) > 0 {
return http.StatusBadRequest, fmt.Errorf("Could not send distribute request to the following addresses: %s", failedDns)
}
return http.StatusOK, err
}
func (m *Serve) isValidReconf(name string, path []string, templateFePath string) bool {
return len(name) > 0 && (len(path) > 0 || len(templateFePath) > 0)
}
func (m *Serve) reconfigure(w http.ResponseWriter, req *http.Request) {
sr := ServiceReconfigure{
ServiceName: req.URL.Query().Get("serviceName"),
ServiceColor: req.URL.Query().Get("serviceColor"),
ServiceDomain: req.URL.Query().Get("serviceDomain"),
ConsulTemplateFePath: req.URL.Query().Get("consulTemplateFePath"),
ConsulTemplateBePath: req.URL.Query().Get("consulTemplateBePath"),
PathType: req.URL.Query().Get("pathType"),
Port: req.URL.Query().Get("port"),
Mode: m.Mode,
}
if len(req.URL.Query().Get("servicePath")) > 0 {
sr.ServicePath = strings.Split(req.URL.Query().Get("servicePath"), ",")
}
if len(req.URL.Query().Get("skipCheck")) > 0 {
sr.SkipCheck, _ = strconv.ParseBool(req.URL.Query().Get("skipCheck"))
}
if len(req.URL.Query().Get("distribute")) > 0 {
sr.Distribute, _ = strconv.ParseBool(req.URL.Query().Get("distribute"))
}
response := Response{
Status: "OK",
ServiceName: sr.ServiceName,
ServiceColor: sr.ServiceColor,
ServicePath: sr.ServicePath,
ServiceDomain: sr.ServiceDomain,
ConsulTemplateFePath: sr.ConsulTemplateFePath,
ConsulTemplateBePath: sr.ConsulTemplateBePath,
PathType: sr.PathType,
SkipCheck: sr.SkipCheck,
Mode: sr.Mode,
Port: sr.Port,
Distribute: sr.Distribute,
}
if m.isValidReconf(sr.ServiceName, sr.ServicePath, sr.ConsulTemplateFePath) {
if (strings.EqualFold("service", m.Mode) || strings.EqualFold("swarm", m.Mode)) && len(sr.Port) == 0 {
m.writeBadRequest(w, &response, `When MODE is set to "service" or "swarm", the port query is mandatory`)
} else if sr.Distribute {
if status, err := m.SendDistributeRequests(req, sr.ServiceName); err != nil || status >= 300 {
m.writeInternalServerError(w, &response, err.Error())
} else {
response.Message = DISTRIBUTED
w.WriteHeader(http.StatusOK)
}
} else {
action := NewReconfigure(m.BaseReconfigure, sr)
if err := action.Execute([]string{}); err != nil {
m.writeInternalServerError(w, &response, err.Error())
} else {
w.WriteHeader(http.StatusOK)
}
}
} else {
m.writeBadRequest(w, &response, "The following queries are mandatory: (serviceName and servicePath) or (serviceName, consulTemplateFePath, and consulTemplateBePath)")
}
httpWriterSetContentType(w, "application/json")
js, _ := json.Marshal(response)
w.Write(js)
}
func (m *Serve) writeBadRequest(w http.ResponseWriter, resp *Response, msg string) {
resp.Status = "NOK"
resp.Message = msg
w.WriteHeader(http.StatusBadRequest)
}
func (m *Serve) writeInternalServerError(w http.ResponseWriter, resp *Response, msg string) {
resp.Status = "NOK"
resp.Message = msg
w.WriteHeader(http.StatusInternalServerError)
}
func (m *Serve) remove(w http.ResponseWriter, req *http.Request) {
serviceName := req.URL.Query().Get("serviceName")
distribute := false
response := Response{
Status: "OK",
ServiceName: serviceName,
}
if len(req.URL.Query().Get("distribute")) > 0 {
distribute, _ = strconv.ParseBool(req.URL.Query().Get("distribute"))
if distribute {
response.Distribute = distribute
response.Message = DISTRIBUTED
}
}
if len(serviceName) == 0 {
response.Status = "NOK"
response.Message = "The serviceName query is mandatory"
w.WriteHeader(http.StatusBadRequest)
} else if distribute {
if status, err := m.SendDistributeRequests(req, serviceName); err != nil || status >= 300 {
m.writeInternalServerError(w, &response, err.Error())
} else {
response.Message = DISTRIBUTED
w.WriteHeader(http.StatusOK)
}
} else {
logPrintf("Processing remove request %s", req.URL.Path)
action := NewRemove(
serviceName,
m.BaseReconfigure.ConfigsPath,
m.BaseReconfigure.TemplatesPath,
m.ConsulAddresses,
m.InstanceName,
m.Mode,
)
action.Execute([]string{})
w.WriteHeader(http.StatusOK)
}
httpWriterSetContentType(w, "application/json")
js, _ := json.Marshal(response)
w.Write(js)
}
func (m *Serve) config(w http.ResponseWriter, req *http.Request) {
httpWriterSetContentType(w, "text/html")
out, err := proxy.ReadConfig(m.BaseReconfigure.ConfigsPath)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
} else {
w.WriteHeader(http.StatusOK)
}
w.Write([]byte(out))
}
func (m *Serve) setConsulAddresses() {
m.ConsulAddresses = []string{}
if len(os.Getenv("CONSUL_ADDRESS")) > 0 {
for _, address := range strings.Split(os.Getenv("CONSUL_ADDRESS"), ",") {
if !strings.HasPrefix(address, "http") {
address = fmt.Sprintf("http://%s", address)
}
m.ConsulAddresses = append(m.ConsulAddresses, address)
}
}
}