forked from docker/machine
/
plugin.go
213 lines (176 loc) · 5.14 KB
/
plugin.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
package localbinary
import (
"bufio"
"fmt"
"io"
"os"
"os/exec"
"strings"
"time"
"github.com/docker/machine/libmachine/log"
)
var (
// Timeout where we will bail if we're not able to properly contact the
// plugin server.
defaultTimeout = 10 * time.Second
)
const (
pluginOutPrefix = "(%s) OUT | "
pluginErrPrefix = "(%s) DBG | "
PluginEnvKey = "MACHINE_PLUGIN_TOKEN"
PluginEnvVal = "42"
)
type PluginStreamer interface {
// Return a channel for receiving the output of the stream line by
// line, and a channel for stopping the stream when we are finished
// reading from it.
//
// It happens to be the case that we do this all inside of the main
// plugin struct today, but that may not be the case forever.
AttachStream(*bufio.Scanner) (<-chan string, chan<- bool)
}
type PluginServer interface {
// Get the address where the plugin server is listening.
Address() (string, error)
// Serve kicks off the plugin server.
Serve() error
// Close shuts down the initialized server.
Close() error
}
type McnBinaryExecutor interface {
// Execute the driver plugin. Returns scanners for plugin binary
// stdout and stderr.
Start() (*bufio.Scanner, *bufio.Scanner, error)
// Stop reading from the plugins in question.
Close() error
}
// DriverPlugin interface wraps the underlying mechanics of starting a driver
// plugin server and then figuring out where it can be dialed.
type DriverPlugin interface {
PluginServer
PluginStreamer
}
type LocalBinaryPlugin struct {
Executor McnBinaryExecutor
Addr string
MachineName string
addrCh chan string
stopCh chan bool
}
type LocalBinaryExecutor struct {
pluginStdout, pluginStderr io.ReadCloser
DriverName string
}
func NewLocalBinaryPlugin(driverName string) *LocalBinaryPlugin {
return &LocalBinaryPlugin{
stopCh: make(chan bool, 1),
addrCh: make(chan string, 1),
Executor: &LocalBinaryExecutor{
DriverName: driverName,
},
}
}
func (lbe *LocalBinaryExecutor) Start() (*bufio.Scanner, *bufio.Scanner, error) {
log.Debugf("Launching plugin server for driver %s", lbe.DriverName)
binaryPath, err := exec.LookPath(fmt.Sprintf("docker-machine-driver-%s", lbe.DriverName))
if err != nil {
return nil, nil, fmt.Errorf("Driver %q not found. Do you have the plugin binary accessible in your PATH?", lbe.DriverName)
}
log.Debugf("Found binary path at %s", binaryPath)
cmd := exec.Command(binaryPath)
lbe.pluginStdout, err = cmd.StdoutPipe()
if err != nil {
return nil, nil, fmt.Errorf("Error getting cmd stdout pipe: %s", err)
}
lbe.pluginStderr, err = cmd.StderrPipe()
if err != nil {
return nil, nil, fmt.Errorf("Error getting cmd stderr pipe: %s", err)
}
outScanner := bufio.NewScanner(lbe.pluginStdout)
errScanner := bufio.NewScanner(lbe.pluginStderr)
os.Setenv(PluginEnvKey, PluginEnvVal)
if err := cmd.Start(); err != nil {
return nil, nil, fmt.Errorf("Error starting plugin binary: %s", err)
}
return outScanner, errScanner, nil
}
func (lbe *LocalBinaryExecutor) Close() error {
if err := lbe.pluginStdout.Close(); err != nil {
return err
}
if err := lbe.pluginStderr.Close(); err != nil {
return err
}
return nil
}
func stream(scanner *bufio.Scanner, streamOutCh chan<- string, stopCh <-chan bool) {
for scanner.Scan() {
select {
case <-stopCh:
close(streamOutCh)
return
default:
streamOutCh <- strings.Trim(scanner.Text(), "\n")
if err := scanner.Err(); err != nil {
log.Warnf("Scanning stream: %s", err)
}
}
}
}
func (lbp *LocalBinaryPlugin) AttachStream(scanner *bufio.Scanner) (<-chan string, chan<- bool) {
streamOutCh := make(chan string)
stopCh := make(chan bool, 1)
go stream(scanner, streamOutCh, stopCh)
return streamOutCh, stopCh
}
func (lbp *LocalBinaryPlugin) execServer() error {
outScanner, errScanner, err := lbp.Executor.Start()
if err != nil {
return err
}
// Scan just one line to get the address, then send it to the relevant
// channel.
outScanner.Scan()
addr := outScanner.Text()
if err := outScanner.Err(); err != nil {
return fmt.Errorf("Reading plugin address failed: %s", err)
}
lbp.addrCh <- strings.TrimSpace(addr)
stdOutCh, stopStdoutCh := lbp.AttachStream(outScanner)
stdErrCh, stopStderrCh := lbp.AttachStream(errScanner)
for {
select {
case out := <-stdOutCh:
log.Debug(fmt.Sprintf(pluginOutPrefix, lbp.MachineName), out)
case err := <-stdErrCh:
log.Debug(fmt.Sprintf(pluginErrPrefix, lbp.MachineName), err)
case _ = <-lbp.stopCh:
stopStdoutCh <- true
stopStderrCh <- true
if err := lbp.Executor.Close(); err != nil {
return fmt.Errorf("Error closing local plugin binary: %s", err)
}
return nil
}
}
}
func (lbp *LocalBinaryPlugin) Serve() error {
return lbp.execServer()
}
func (lbp *LocalBinaryPlugin) Address() (string, error) {
if lbp.Addr == "" {
select {
case lbp.Addr = <-lbp.addrCh:
log.Debugf("Plugin server listening at address %s", lbp.Addr)
close(lbp.addrCh)
return lbp.Addr, nil
case <-time.After(defaultTimeout):
return "", fmt.Errorf("Failed to dial the plugin server in %s", defaultTimeout)
}
}
return lbp.Addr, nil
}
func (lbp *LocalBinaryPlugin) Close() error {
lbp.stopCh <- true
return nil
}