This repository has been archived by the owner on Jun 18, 2020. It is now read-only.
/
forwarder.go
144 lines (131 loc) · 3.72 KB
/
forwarder.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
package v2
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"text/template"
"time"
"github.com/buildpack/forge/engine"
"github.com/buildpack/forge/internal"
)
const forwardScriptTmpl = `
{{if .Forwards -}}
echo 'Forwarding:{{range .Forwards}} {{.Name}}{{end}}'
sshpass -f /tmp/ssh-code ssh -4 -N \
-o PermitLocalCommand=yes -o LocalCommand="touch /tmp/healthy" \
-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no \
-o LogLevel=ERROR -o ExitOnForwardFailure=yes \
-o ServerAliveInterval=10 -o ServerAliveCountMax=60 \
-p '{{.Port}}' '{{.User}}@{{.Host}}' \
{{- range $i, $_ := .Forwards}}
{{- if $i}} \{{end}}
-L '{{.From}}:{{.To}}'
{{- end}}
rm -f /tmp/healthy
{{- end}}
`
type Forwarder struct {
Logs io.Writer
engine Engine
}
type ForwardConfig struct {
AppName string
Stack string
Color Colorizer
Details *ForwardDetails
ContainerPort string
HostIP, HostPort string
Wait <-chan time.Time
}
func NewForwarder(engine Engine) *Forwarder {
return &Forwarder{
Logs: os.Stdout,
engine: engine,
}
}
func (f *Forwarder) Forward(config *ForwardConfig) (health <-chan string, done func(), id string, err error) {
output := internal.NewLockWriter(f.Logs)
netContr, err := f.engine.NewContainer(f.buildNetConfig(config.AppName, config.Stack, config.ContainerPort, config.HostIP, config.HostPort))
if err != nil {
return nil, nil, "", err
}
// TODO: wait for network container to fully start in Background
if err := netContr.Background(); err != nil {
return nil, nil, "", err
}
containerConfig, err := f.buildConfig(config.Details, config.Stack, netContr.ID())
if err != nil {
return nil, nil, "", err
}
contr, err := f.engine.NewContainer(containerConfig)
if err != nil {
return nil, nil, "", err
}
prefix := config.Color("[%s tunnel] ", config.AppName)
wait := config.Wait
exit := make(chan struct{})
go func() {
for {
select {
case <-exit:
return
case <-wait:
code, err := config.Details.Code()
if err != nil {
fmt.Fprintf(output, "%sError: %s\n", prefix, err)
continue
}
codeStream := engine.NewStream(ioutil.NopCloser(bytes.NewBufferString(code)), int64(len(code)))
if err := contr.StreamFileTo(codeStream, "/tmp/ssh-code"); err != nil {
fmt.Fprintf(output, "%sError: %s\n", prefix, err)
continue
}
status, err := contr.Start(prefix, output, nil)
if err != nil {
fmt.Fprintf(output, "%sError: %s\n", prefix, err)
continue
}
fmt.Fprintf(output, "%sExited with status: %d\n", prefix, status)
}
}
}()
done = func() {
defer netContr.Close()
defer contr.Close()
wait = nil
close(exit)
output.Disable()
}
return contr.HealthCheck(), done, netContr.ID(), nil
}
func (f *Forwarder) buildConfig(forward *ForwardDetails, stack, netID string) (*engine.ContainerConfig, error) {
scriptBuf := &bytes.Buffer{}
tmpl := template.Must(template.New("").Parse(forwardScriptTmpl))
if err := tmpl.Execute(scriptBuf, forward); err != nil {
return nil, err
}
return &engine.ContainerConfig{
Name: "service",
Image: stack,
Entrypoint: []string{"/bin/bash", "-c", scriptBuf.String()},
NetContainer: netID,
Test: []string{"CMD", "test", "-f", "/tmp/healthy"},
Interval: time.Second,
Retries: 30,
Exit: make(<-chan struct{}),
}, nil
}
func (f *Forwarder) buildNetConfig(name, stack, containerPort, hostIP, hostPort string) *engine.ContainerConfig {
return &engine.ContainerConfig{
Name: "network",
Hostname: name,
Image: stack,
Port: containerPort,
Entrypoint: []string{"tail", "-f", "/dev/null"},
HostIP: hostIP,
HostPort: hostPort,
Exit: make(<-chan struct{}),
}
}