forked from machine-drivers/machine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
register_driver.go
56 lines (46 loc) · 1.13 KB
/
register_driver.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
package plugin
import (
"fmt"
"net"
"net/http"
"net/rpc"
"os"
"time"
"github.com/docker/machine/libmachine"
"github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/drivers/plugin/localbinary"
"github.com/docker/machine/libmachine/drivers/rpc"
)
var (
heartbeatTimeout = 500 * time.Millisecond
)
func RegisterDriver(d drivers.Driver) {
if os.Getenv(localbinary.PluginEnvKey) != localbinary.PluginEnvVal {
fmt.Fprintln(os.Stderr, `This is a Docker Machine plugin binary.
Plugin binaries are not intended to be invoked directly.
Please use this plugin through the main 'docker-machine' binary.`)
os.Exit(1)
}
libmachine.SetDebug(true)
rpcd := rpcdriver.NewRpcServerDriver(d)
rpc.Register(rpcd)
rpc.HandleHTTP()
listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
fmt.Fprintf(os.Stderr, "Error loading RPC server: %s\n", err)
os.Exit(1)
}
defer listener.Close()
fmt.Println(listener.Addr())
go http.Serve(listener, nil)
for {
select {
case <-rpcd.CloseCh:
os.Exit(0)
case <-rpcd.HeartbeatCh:
continue
case <-time.After(heartbeatTimeout):
os.Exit(1)
}
}
}