-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnginx.go
More file actions
241 lines (209 loc) · 5.55 KB
/
nginx.go
File metadata and controls
241 lines (209 loc) · 5.55 KB
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
package warp
import (
"context"
"errors"
"fmt"
"net/netip"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"syscall"
"time"
)
// **important** warpctl config should align `worker_shutdown_timeout` with this
const DefaultDrainTimeout = 60 * time.Minute
func DefaultNginxSettings() *NginxSettings {
return &NginxSettings{
DrainTimeout: DefaultDrainTimeout,
}
}
type NginxSettings struct {
// this should align with the `worker_shutdown_timeout` setting
DrainTimeout time.Duration
}
func NginxWithDefaults(configPath string, convertedConfigPath string) (error, int) {
return Nginx(configPath, convertedConfigPath, DefaultNginxSettings())
}
// `convertedConfigPath` is needed to support host networking
func Nginx(configPath string, convertedConfigPath string, settings *NginxSettings) (error, int) {
path := configPath
if hostNetwork, err := warpHostNetwork(); err == nil {
// use a predictable path to help debugging
err := convertNginxConfigToHostNetwork(configPath, convertedConfigPath, hostNetwork)
if err != nil {
return err, -1
}
path = convertedConfigPath
Err.Printf("Using converted nginx config: %s", path)
}
event := NewEvent()
eventClose := event.SetOnSignals(syscall.SIGQUIT, syscall.SIGTERM)
defer eventClose()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cmd := exec.Command("nginx", "-g", "daemon off;", "-c", path)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Start()
if err != nil {
return err, -1
}
defer cmd.Process.Kill()
go func() {
select {
case <-ctx.Done():
return
case <-event.Ctx.Done():
}
cmd.Process.Signal(syscall.SIGQUIT)
select {
case <-ctx.Done():
return
case <-time.After(settings.DrainTimeout):
}
cmd.Process.Kill()
}()
err = cmd.Wait()
if err != nil {
return err, -1
}
return nil, cmd.ProcessState.ExitCode()
}
type HostNetwork struct {
Ipv4 *netip.Addr
Ipv6 *netip.Addr
HostPorts map[int]int
}
func warpHostNetwork() (*HostNetwork, error) {
ipv4 := os.Getenv("WARP_HOST_IPV4")
ipv6 := os.Getenv("WARP_HOST_IPV6")
if ipv4 == "" && ipv6 == "" {
return nil, errors.New("WARP_HOST_IPV4 and WARP_HOST_IPV6 not set")
}
var ipv4Addr *netip.Addr
var ipv6Addr *netip.Addr
if ipv4 != "" {
ipv4Addr_, err := netip.ParseAddr(ipv4)
if err != nil {
return nil, err
}
ipv4Addr = &ipv4Addr_
}
if ipv6 != "" {
ipv6Addr_, err := netip.ParseAddr(ipv6)
if err != nil {
return nil, err
}
ipv6Addr = &ipv6Addr_
}
// service port -> host port
hostPorts := map[int]int{}
if ports := os.Getenv("WARP_PORTS"); ports != "" {
portPairs := strings.Split(ports, ",")
for _, portPair := range portPairs {
parts := strings.Split(portPair, ":")
if len(parts) != 2 {
return nil, errors.New("Port pair must be service_port:host_port")
}
servicePort, err := strconv.Atoi(parts[0])
if err != nil {
return nil, err
}
hostPort, err := strconv.Atoi(parts[1])
if err != nil {
return nil, err
}
hostPorts[servicePort] = hostPort
}
}
return &HostNetwork{
Ipv4: ipv4Addr,
Ipv6: ipv6Addr,
HostPorts: hostPorts,
}, nil
}
func convertNginxConfigToHostNetwork(path string, outPath string, hostNetwork *HostNetwork) error {
content, err := os.ReadFile(path)
if err != nil {
return nil
}
reusePort := false
portCounts := map[netip.AddrPort]int{}
out := []byte{}
// groups:
// 1 = indent
// 2 = ip:port
// 3 = port
// 4 = options
listenRe := regexp.MustCompile("(?m)(?:^|;)(\\s*)listen\\s+((?:[^;]+:)?(\\d+))(\\s+[^;]+)?;")
allSubmatches := listenRe.FindAllSubmatchIndex(content, -1)
i := 0
for _, submatches := range allSubmatches {
if i < submatches[0] {
out = append(out, content[i:submatches[0]]...)
}
i = submatches[1]
var addr netip.Addr
addrOk := false
var port int
ipPort := string(content[submatches[4]:submatches[5]])
addrPort, err := netip.ParseAddrPort(ipPort)
if err == nil {
addr = addrPort.Addr()
addrOk = true
port = int(addrPort.Port())
} else {
// just parse the port
port, err = strconv.Atoi(string(content[submatches[6]:submatches[7]]))
if err != nil {
return err
}
}
hostPort, portOk := hostNetwork.HostPorts[port]
if !portOk {
return fmt.Errorf("Missing host port for service port %d", port)
}
var hostAddr netip.Addr
if addrOk {
if addr.Is6() {
if hostNetwork.Ipv6 == nil {
return fmt.Errorf("IPv6 host network needed for port %d", port)
}
hostAddr = *hostNetwork.Ipv6
} else {
if hostNetwork.Ipv4 == nil {
return fmt.Errorf("IPv4 host network needed for port %d", port)
}
hostAddr = *hostNetwork.Ipv4
}
} else {
// the default nginx interface is ipv4
if hostNetwork.Ipv4 == nil {
return fmt.Errorf("IPv4 host network needed for port %d", port)
}
hostAddr = *hostNetwork.Ipv4
}
hostAddrPort := netip.AddrPortFrom(hostAddr, uint16(hostPort))
portCounts[hostAddrPort] += 1
firstListenOnHostPort := (portCounts[hostAddrPort] == 1)
var template string
// host network uses SO_REUSEPORT
if reusePort && firstListenOnHostPort && !strings.Contains(string(content[submatches[8]:submatches[9]]), "reuseport") {
template = fmt.Sprintf("${1}listen %s${4} reuseport;", hostAddrPort)
} else {
template = fmt.Sprintf("${1}listen %s${4};", hostAddrPort)
}
out = listenRe.Expand(out, []byte(template), content, submatches)
}
if i < len(content) {
out = append(out, content[i:len(content)]...)
}
Err.Printf("Converted nginx config (%s): %s", outPath, string(out))
err = os.WriteFile(outPath, out, 0555)
if err != nil {
return err
}
return nil
}